Class: Legion::Extensions::Agentic::Affect::Fatigue::Helpers::EnergyModel

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb

Constant Summary

Constants included from Constants

Constants::ACTIVE_DRAIN_RATE, Constants::BURNOUT_THRESHOLD, Constants::COGNITIVE_DRAIN_MULTIPLIER, Constants::CRITICAL_THRESHOLD, Constants::EMOTIONAL_DRAIN_MULTIPLIER, Constants::FATIGUE_LEVELS, Constants::MAX_ENERGY, Constants::MAX_HISTORY, Constants::MIN_ENERGY, Constants::PERFORMANCE_DEGRADATION, Constants::RECOVERY_MODES, Constants::RECOVERY_RATES, Constants::RESTING_RECOVERY_RATE, Constants::REST_THRESHOLD, Constants::SECOND_WIND_CHANCE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnergyModel

Returns a new instance of EnergyModel.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 16

def initialize
  @energy                 = Constants::MAX_ENERGY
  @fatigue_level          = :fresh
  @ticks_active           = 0
  @ticks_resting          = 0
  @consecutive_low_ticks  = 0
  @burnout                = false
  @recovery_mode          = nil
  @peak_energy            = Constants::MAX_ENERGY
  @history                = []
end

Instance Attribute Details

#burnoutObject (readonly)

Returns the value of attribute burnout.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 12

def burnout
  @burnout
end

#consecutive_low_ticksObject (readonly)

Returns the value of attribute consecutive_low_ticks.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 12

def consecutive_low_ticks
  @consecutive_low_ticks
end

#energyObject (readonly)

Returns the value of attribute energy.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 12

def energy
  @energy
end

#fatigue_levelObject (readonly)

Returns the value of attribute fatigue_level.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 12

def fatigue_level
  @fatigue_level
end

#historyObject (readonly)

Returns the value of attribute history.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 12

def history
  @history
end

#peak_energyObject (readonly)

Returns the value of attribute peak_energy.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 12

def peak_energy
  @peak_energy
end

#recovery_modeObject (readonly)

Returns the value of attribute recovery_mode.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 12

def recovery_mode
  @recovery_mode
end

#ticks_activeObject (readonly)

Returns the value of attribute ticks_active.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 12

def ticks_active
  @ticks_active
end

#ticks_restingObject (readonly)

Returns the value of attribute ticks_resting.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 12

def ticks_resting
  @ticks_resting
end

Instance Method Details

#burnout?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 61

def burnout?
  @burnout
end

#critically_fatigued?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 57

def critically_fatigued?
  @energy < Constants::CRITICAL_THRESHOLD
end

#enter_recovery(mode) ⇒ Object



65
66
67
68
69
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 65

def enter_recovery(mode)
  return unless Constants::RECOVERY_MODES.include?(mode)

  @recovery_mode = mode
end

#exit_recoveryObject



71
72
73
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 71

def exit_recovery
  @recovery_mode = nil
end

#needs_rest?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 53

def needs_rest?
  @energy < Constants::REST_THRESHOLD
end

#performance_factorObject



49
50
51
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 49

def performance_factor
  Constants::PERFORMANCE_DEGRADATION[@fatigue_level]
end

#tick(cognitive_load: 0.5, emotional_arousal: 0.5, is_resting: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 28

def tick(cognitive_load: 0.5, emotional_arousal: 0.5, is_resting: false)
  if is_resting
    recover
    @ticks_resting += 1
    @consecutive_low_ticks = 0
  else
    drain(cognitive_load, emotional_arousal)
    @ticks_active += 1
    check_second_wind
  end

  @energy = @energy.clamp(Constants::MIN_ENERGY, Constants::MAX_ENERGY)
  @peak_energy = @energy if @energy > @peak_energy
  classify_fatigue
  track_low_ticks
  check_burnout
  record_snapshot

  to_h
end

#time_to_full_recoveryObject



84
85
86
87
88
89
90
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 84

def time_to_full_recovery
  rate = @recovery_mode ? Constants::RECOVERY_RATES[@recovery_mode] : Constants::RESTING_RECOVERY_RATE
  return 0 if @energy >= Constants::MAX_ENERGY
  return Float::INFINITY if rate <= 0.0

  ((Constants::MAX_ENERGY - @energy) / rate).ceil
end

#time_to_rest_thresholdObject



75
76
77
78
79
80
81
82
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 75

def time_to_rest_threshold
  return 0 if needs_rest?

  current_drain = effective_drain(0.5, 0.5)
  return Float::INFINITY if current_drain <= 0.0

  ((@energy - Constants::REST_THRESHOLD) / current_drain).ceil
end

#to_hObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 106

def to_h
  {
    energy:                @energy.round(4),
    fatigue_level:         @fatigue_level,
    performance_factor:    performance_factor,
    needs_rest:            needs_rest?,
    critically_fatigued:   critically_fatigued?,
    burnout:               @burnout,
    recovery_mode:         @recovery_mode,
    peak_energy:           @peak_energy.round(4),
    ticks_active:          @ticks_active,
    ticks_resting:         @ticks_resting,
    consecutive_low_ticks: @consecutive_low_ticks,
    trend:                 trend,
    history_size:          @history.size
  }
end

#trendObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/energy_model.rb', line 92

def trend
  return :stable if @history.size < 5

  recent = @history.last(5).map { |s| s[:energy] }
  delta = recent.last - recent.first
  if delta > 0.01
    :recovering
  elsif delta < -0.01
    :draining
  else
    :stable
  end
end