Skip to content

feat: implement critical threshold detection in NeedChangedEvents #114

@mcj-coder

Description

@mcj-coder

Context

Actions currently hardcode crossedCriticalThreshold: false when creating NeedChangedEvents. This prevents the system from detecting when actions push needs across critical thresholds, which is essential for triggering urgency behaviors.

Current Implementation

events.Add(new NeedChangedEvent(
    currentTime,
    character.Id,
    needType,
    oldValue,
    newValue,
    crossedCriticalThreshold: false  // Always false
));

Proposed Solution

Calculate whether the need crossed a critical threshold:

var threshold = needType switch
{
    NeedType.Stamina => 20,
    NeedType.Hunger => 30,
    NeedType.Rest => 20,
    NeedType.Social => 20,
    NeedType.Safety => 20,
    _ => 0
};

var oldCritical = oldValue < threshold;
var newCritical = newValue < threshold;
var crossedCriticalThreshold = oldCritical != newCritical;

Better approach: Add this logic to NeedsState or NeedValue to avoid duplication across actions.

Implementation Options

Option 1: Add to NeedValue

// In NeedValue.cs
public bool CrossesCriticalThreshold(int newValue) 
{
    var oldCritical = Value < CriticalThreshold;
    var newCritical = newValue < CriticalThreshold;
    return oldCritical != newCritical;
}

Option 2: Add to NeedsState

// In NeedsState.cs
public bool CheckCriticalThresholdCrossing(NeedType needType, int oldValue, int newValue)
{
    // Implementation
}

Acceptance Criteria

  • Critical threshold detection logic implemented
  • Logic is centralized (not duplicated in each action)
  • All action implementations updated to use the logic
  • Tests verify threshold crossing is detected correctly
  • Tests verify non-crossing scenarios return false

Test Cases Needed

[Fact]
public void Execute_WhenCrossingCriticalThreshold_ReturnsEventWithCrossedTrue()
{
    // Start above threshold (30), action brings below (15)
    // crossedCriticalThreshold should be true
}

[Fact]
public void Execute_WhenNotCrossingThreshold_ReturnsEventWithCrossedFalse()
{
    // Both old and new values are above or both below threshold
    // crossedCriticalThreshold should be false
}

Related

Identified in PR #112 review by .NET Specialist
Part of Epic 3: NPC Decision Making

Labels

  • type:feature
  • priority:high
  • area:game-logic

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions