LCOV - code coverage report
Current view: top level - activity - sleep.go Coverage Total Hit
Test: coverage.lcov Lines: 91.1 % 90 82
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package activity
       2              : 
       3              : import (
       4              :         gc "github.com/kijimaD/ruins/internal/components"
       5              :         "github.com/kijimaD/ruins/internal/consts"
       6              :         "github.com/kijimaD/ruins/internal/gamelog"
       7              :         "github.com/kijimaD/ruins/internal/rawschema"
       8              :         w "github.com/kijimaD/ruins/internal/world"
       9              :         "github.com/kijimaD/ruins/internal/world/query"
      10              :         "github.com/mlange-42/ark/ecs"
      11              : )
      12              : 
      13              : // SleepBehavior は疲労が抜けるまで眠り続けるアクティビティ。rest と違い必要 AP を消化するのでなく、
      14              : // 毎ターン疲労が減り、疲労が尽きるか外因で中断されると終わる。起きる時間は指定できない。
      15              : type SleepBehavior struct{}
      16              : 
      17              : // Info はBehaviorの実装
      18              : func (sb *SleepBehavior) Info() Info {
      19            1 :         return Info{
      20            1 :                 Name:          "Sleep",
      21            1 :                 Description:   "Sleep until fatigue is gone",
      22            1 :                 Interruptible: true,
      23            1 :                 // 中断は Cancel で目覚めとして扱い、再入眠は B キーで新規に Execute する。
      24            1 :                 // Pause からの Resume 経路は使わないので false にし、寝具 Quality の再解決漏れも避ける
      25            1 :                 Resumable:       false,
      26            1 :                 ActionPointCost: consts.StandardActionCost,
      27            1 :                 TotalRequiredAP: 0,
      28            1 :         }
      29              : }
      30              : 
      31              : // Name はBehaviorの実装
      32              : func (sb *SleepBehavior) Name() gc.BehaviorName {
      33            1 :         return gc.BehaviorSleep
      34            1 : }
      35              : 
      36              : // NewSleepActivity は睡眠アクティビティを組む。進行は疲労側で管理するので必要 AP は 0
      37              : func NewSleepActivity() *gc.Activity {
      38            1 :         return NewActivity(gc.BehaviorSleep, 0)
      39            1 : }
      40              : 
      41              : // Validate は入眠可否を疲労・気温・安全の全条件で検証する。プロンプト表示と同じ
      42              : // EvaluateSleepConditions を唯一の判定点にし、失敗の種別ごとに理由を返す。
      43              : // 返した理由は呼び出し側が gamelog へ出す
      44              : func (sb *SleepBehavior) Validate(_ *gc.Activity, actor ecs.Entity, world w.World) error {
      45            1 :         sc := EvaluateSleepConditions(world, actor)
      46            1 :         switch {
      47              :         case sc.TooTired():
      48            1 :                 return &UserError{Msg: query.T(world, "Not tired enough to sleep.")}
      49              :         case !sc.TemperatureOK:
      50            0 :                 return &UserError{Msg: query.T(world, "Too cold or hot to sleep.")}
      51              :         case !sc.AreaSafe:
      52            0 :                 return &UserError{Msg: query.T(world, "Cannot sleep because enemies are nearby.")}
      53              :         }
      54            1 :         return nil
      55              : }
      56              : 
      57              : // Start は睡眠開始時に Sleeping マーカーを付ける。寝具 Quality を足元・隣接から解決して写す
      58              : func (sb *SleepBehavior) Start(_ *gc.Activity, actor ecs.Entity, world w.World) error {
      59            1 :         quality := beddingQualityAt(actor, world)
      60            1 :         if !world.Components.Sleeping.Has(actor) {
      61            1 :                 world.Components.Sleeping.Add(actor, &gc.Sleeping{Quality: quality})
      62            1 :         }
      63            1 :         if world.Components.Player.Has(actor) {
      64            0 :                 gamelog.New(query.GetGameLog(world)).
      65            0 :                         Markup(query.T(world, "Went to sleep.")).
      66            0 :                         Log()
      67            0 :         }
      68            1 :         return nil
      69              : }
      70              : 
      71              : // DoTurn は毎ターン中断条件を検査し、満たさなければ眠り続ける。疲労の減少はターン終了の
      72              : // progressTurnFatigue が Sleeping を見て行うので、ここでは起床・中断の判定に徹する
      73              : func (sb *SleepBehavior) DoTurn(comp *gc.Activity, actor ecs.Entity, world w.World) error {
      74            1 :         if !query.IsAreaSafe(world, actor) {
      75            1 :                 Cancel(comp, "sleep interrupted because enemies are nearby")
      76            1 :                 return nil
      77            1 :         }
      78            1 :         if hasActiveHypothermia(actor, world) {
      79            1 :                 Cancel(comp, "woke up from the cold")
      80            1 :                 return nil
      81            1 :         }
      82            1 :         if hungerWakes(actor, world) {
      83            1 :                 Cancel(comp, "woke up from hunger")
      84            1 :                 return nil
      85            1 :         }
      86            1 :         if world.Components.Fatigue.Has(actor) && world.Components.Fatigue.Get(actor).Current <= 0 {
      87            1 :                 Complete(comp)
      88            1 :         }
      89            1 :         return nil
      90              : }
      91              : 
      92              : // Finish は起床時に Sleeping を外す。Ark の Remove は不在で panic するので Has で守る
      93              : func (sb *SleepBehavior) Finish(_ *gc.Activity, actor ecs.Entity, world w.World) error {
      94            1 :         if world.Components.Sleeping.Has(actor) {
      95            1 :                 world.Components.Sleeping.Remove(actor)
      96            1 :         }
      97            1 :         if world.Components.Player.Has(actor) {
      98            1 :                 gamelog.New(query.GetGameLog(world)).
      99            1 :                         Markup(query.T(world, "Woke up refreshed.")).
     100            1 :                         Log()
     101            1 :         }
     102            1 :         return nil
     103              : }
     104              : 
     105              : // Canceled は中断時に Sleeping を外す
     106              : func (sb *SleepBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, world w.World) error {
     107              :         // Sleeping の除去はアーキタイプを変えて comp ポインタを無効化する。理由は除去より前に退避する。
     108              :         // Ark の Remove は不在で panic するので Has で守る
     109            1 :         reason := comp.CancelReason
     110            1 :         if world.Components.Sleeping.Has(actor) {
     111            1 :                 world.Components.Sleeping.Remove(actor)
     112            1 :         }
     113            1 :         if world.Components.Player.Has(actor) {
     114            1 :                 gamelog.New(query.GetGameLog(world)).
     115            1 :                         Markup(query.T(world, "Sleep interrupted: %s", query.T(world, reason))).
     116            1 :                         Log()
     117            1 :         }
     118            1 :         return nil
     119              : }
     120              : 
     121              : // hasActiveHypothermia は全身の低体温が体感に響く水準に達しているかを返す。
     122              : // わずかな残留では起こさず、IsActive すなわちペナルティを及ぼす段階で初めて起床させる。
     123              : // 入眠可能な適温帯の下端で寝ると低体温が浅く残るが、火のそばで回復中なら閾値に達さず眠り続けられる
     124              : func hasActiveHypothermia(actor ecs.Entity, world w.World) bool {
     125            1 :         if !world.Components.HealthStatus.Has(actor) {
     126            1 :                 return false
     127            1 :         }
     128            1 :         hs := world.Components.HealthStatus.Get(actor)
     129            1 :         cond := hs.Parts[gc.BodyPartWholeBody].GetCondition(world.Resources.ConditionDefs.ByOnset(rawschema.Cold))
     130            1 :         return cond != nil && cond.IsActive()
     131              : }
     132              : 
     133              : // hungerWakes は空腹の段階が寝ている者を起こすかを返す
     134              : func hungerWakes(actor ecs.Entity, world w.World) bool {
     135            1 :         if !world.Components.Hunger.Has(actor) {
     136            1 :                 return false
     137            1 :         }
     138            1 :         defs := world.Resources.NeedDefs
     139            1 :         stage, _ := defs.HungerStageOf(world.Components.Hunger.Get(actor).GetLevel(defs))
     140            1 :         return stage.WakesSleeper
     141              : }
     142              : 
     143              : // beddingQualityAt は足元か隣接の寝具から最大の Quality を返す。無ければ地べたの基準
     144              : func beddingQualityAt(actor ecs.Entity, world w.World) consts.Percent {
     145            1 :         quality := consts.PercentBase
     146            1 :         if !world.Components.GridElement.Has(actor) {
     147            0 :                 return quality
     148            0 :         }
     149            1 :         center := world.Components.GridElement.Get(actor).Coord
     150            1 :         for dy := consts.Tile(-1); dy <= 1; dy++ {
     151            1 :                 for dx := consts.Tile(-1); dx <= 1; dx++ {
     152            1 :                         for _, e := range query.GetEntitiesAt(world, center.X+dx, center.Y+dy) {
     153            1 :                                 if world.Components.Bedding.Has(e) {
     154            1 :                                         if q := world.Components.Bedding.Get(e).Quality; q > quality {
     155            1 :                                                 quality = q
     156            1 :                                         }
     157              :                                 }
     158              :                         }
     159              :                 }
     160              :         }
     161            1 :         return quality
     162              : }
        

Generated by: LCOV version 2.0-1