LCOV - code coverage report
Current view: top level - systems - hunger.go Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 20 20
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package systems
       2              : 
       3              : import (
       4              :         gc "github.com/kijimaD/ruins/internal/components"
       5              :         "github.com/kijimaD/ruins/internal/consts"
       6              :         "github.com/kijimaD/ruins/internal/rawschema"
       7              :         w "github.com/kijimaD/ruins/internal/world"
       8              :         "github.com/kijimaD/ruins/internal/world/query"
       9              :         "github.com/mlange-42/ark/ecs"
      10              : )
      11              : 
      12              : // progressTurnHunger は Hunger を持つ現ステージの全エンティティの空腹を、1ターンにつき1回進める。ターン終了で
      13              : // 呼ぶので、移動や射撃をしないその場待機のエンティティも等しく空腹になる。行動種別に紐づけると動くものだけ空腹になり
      14              : // 不公平になるため、活動側でなくここへ集約する。
      15              : //
      16              : // ゲートは共有 RNG を使わず、entity と turn から決定的な擬似乱数を作る。共有 RNG を消費するとエンティティ数ぶん敵AIや
      17              : // ドロップの乱数列がずれて再現性が壊れるため。同じ seed・同じ盤面なら空腹の進みも一意に決まる。
      18              : func progressTurnHunger(world w.World) {
      19            1 :         turn := query.GetTurnState(world).TurnNumber
      20            1 :         q := query.ActiveFilter1[gc.Hunger](world).Query()
      21            1 :         for q.Next() {
      22            1 :                 entity := q.Entity()
      23            1 :                 hungerPct := int(query.ProficiencyValue(world, entity, rawschema.ProficiencyIdHungerProgress))
      24            1 :                 // 睡眠中は代謝が下がり腹が減りにくい。進行を半分に抑える
      25            1 :                 if world.Components.Sleeping.Has(entity) {
      26            1 :                         hungerPct /= 2
      27            1 :                 }
      28              :                 // 分母を HungerDrainTurns 倍に伸ばして基準速度を緩和する。耐性が高く hungerPct が 0 以下なら比較が常に
      29              :                 // 偽になり空腹が進まない。下限を置くかは進行系倍率の共通課題として将来まとめて検討する。
      30            1 :                 if int(hungerNoise(entity, turn)%uint64(int(consts.PercentBase)*gc.HungerDrainTurns)) < hungerPct {
      31            1 :                         world.Components.Hunger.Get(entity).Decrease(1)
      32            1 :                 }
      33              :                 // 空腹の効果は保存せず、EffectiveBodyFuncs が量から読み取り時に導出するのでここで同期は不要
      34              :         }
      35              : }
      36              : 
      37              : // hungerNoise は entity と turn から決定的な擬似乱数を撹拌する。splitmix64 の finalizer を使い、共有 RNG を
      38              : // 汚さずに確率ゲートを再現する。
      39              : func hungerNoise(entity ecs.Entity, turn consts.Turn) uint64 {
      40            1 :         x := uint64(entity.ID())*0x9E3779B97F4A7C15 ^ uint64(turn)*0xC2B2AE3D27D4EB4F
      41            1 :         x ^= x >> 30
      42            1 :         x *= 0xBF58476D1CE4E5B9
      43            1 :         x ^= x >> 27
      44            1 :         x *= 0x94D049BB133111EB
      45            1 :         x ^= x >> 31
      46            1 :         return x
      47            1 : }
        

Generated by: LCOV version 2.0-1