LCOV - code coverage report
Current view: top level - world/query - turn.go Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 86 86
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package query
       2              : 
       3              : import (
       4              :         gc "github.com/kijimaD/ruins/internal/components"
       5              :         "github.com/kijimaD/ruins/internal/logger"
       6              :         "github.com/kijimaD/ruins/internal/rawschema"
       7              :         w "github.com/kijimaD/ruins/internal/world"
       8              :         "github.com/mlange-42/ark/ecs"
       9              : )
      10              : 
      11              : // Speed計算係数
      12              : const (
      13              :         speedBaseValue         = 100 // Speed計算の基本値
      14              :         speedAgilityMultiply   = 2   // Speed計算の敏捷係数
      15              :         speedDexterityMultiply = 1   // Speed計算の器用係数
      16              :         speedMinimum           = 25  // Speedの最小値。基本値の1/4
      17              : )
      18              : 
      19              : // CanPlayerAct はプレイヤーが行動可能かを判定する
      20              : // プレイヤーターンかつAP >= 0 の場合にtrueを返す
      21              : func CanPlayerAct(world w.World) bool {
      22              :         // プレイヤーターンでなければ行動不可
      23            1 :         turnState := GetTurnState(world)
      24            1 :         if turnState == nil || turnState.Phase != gc.TurnPhasePlayer {
      25            1 :                 return false
      26            1 :         }
      27              : 
      28              :         // プレイヤーのAPをチェック
      29            1 :         playerEntity, err := GetPlayerEntity(world)
      30            1 :         if err != nil {
      31            1 :                 return false
      32            1 :         }
      33              : 
      34            1 :         tb := world.Components.TurnBased.Get(playerEntity)
      35            1 :         if tb == nil {
      36            1 :                 return false
      37            1 :         }
      38            1 :         return tb.AP.Current >= 0
      39              : }
      40              : 
      41              : // ConsumeActionPoints はエンティティのアクションポイントを消費する。
      42              : // TurnBasedを持たない(生存)エンティティには false を返す。
      43              : // entity の生存は呼び出し側が保証すること。Ark の Has/Get は死亡エンティティで panic する
      44              : func ConsumeActionPoints(world w.World, entity ecs.Entity, cost int) bool {
      45            1 :         if !world.Components.TurnBased.Has(entity) {
      46            1 :                 return false
      47            1 :         }
      48            1 :         tb := world.Components.TurnBased.Get(entity)
      49            1 :         tb.AP.Current -= cost
      50            1 : 
      51            1 :         log := logger.New(logger.CategoryTurn)
      52            1 :         log.Debug("action points consumed",
      53            1 :                 "entity", entity,
      54            1 :                 "cost", cost,
      55            1 :                 "remaining", tb.AP.Current)
      56            1 : 
      57            1 :         return true
      58              : }
      59              : 
      60              : // RestoreAllActionPoints は全エンティティのAPを回復する
      61              : func RestoreAllActionPoints(world w.World) {
      62            1 :         log := logger.New(logger.CategoryTurn)
      63            1 : 
      64              :         // 退避中ステージの敵はAP回復しない。現ステージのみ対象にする
      65            1 :         turnBasedQuery := ActiveFilter1[gc.TurnBased](world).Query()
      66            1 :         for turnBasedQuery.Next() {
      67            1 :                 entity := turnBasedQuery.Entity()
      68            1 :                 tb := world.Components.TurnBased.Get(entity)
      69            1 : 
      70              :                 // MaxAPとSpeedを計算
      71            1 :                 maxAP := CalculateMaxActionPoints(world, entity)
      72            1 : 
      73            1 :                 speed := CalculateSpeed(world, entity)
      74            1 :                 tb.Speed = speed
      75            1 :                 tb.AP.Max = maxAP
      76            1 : 
      77              :                 // 現在AP + Speed で上限まで回復
      78            1 :                 newAP := min(tb.AP.Current+speed, maxAP)
      79            1 :                 tb.AP.Current = newAP
      80            1 : 
      81            1 :                 log.Debug("action points restored",
      82            1 :                         "entity", entity,
      83            1 :                         "speed", speed,
      84            1 :                         "current", tb.AP.Current,
      85            1 :                         "max", maxAP)
      86            1 :         }
      87              : }
      88              : 
      89              : // CalculateMaxActionPoints はエンティティの最大アクションポイントを計算する
      90              : // 敏捷性を重視したAP計算式
      91              : func CalculateMaxActionPoints(world w.World, entity ecs.Entity) int {
      92            1 :         baseAP := 100
      93            1 :         agilityMultiplier := 3
      94            1 :         dexterityMultiplier := 1
      95            1 : 
      96              :         // 能力値を持たない行動主体は基準値で動く。CalculateSpeed と同じ扱い
      97            1 :         abils := world.Components.Abilities.Get(entity)
      98            1 :         if abils == nil {
      99            1 :                 return baseAP
     100            1 :         }
     101              : 
     102            1 :         calculatedAP := max(baseAP+abils.Agility.Total*agilityMultiplier+abils.Dexterity.Total*dexterityMultiplier, 20)
     103            1 : 
     104            1 :         return calculatedAP
     105              : }
     106              : 
     107              : // CalculateSpeed はエンティティのSpeedを計算する。
     108              : // 能力値の素の速度に、過積載・移動コスト・身体機能を掛ける。疲労・空腹は身体機能の意識低下を通じて
     109              : // Moving 経由で効くので、ここで状態を直接読まない
     110              : func CalculateSpeed(world w.World, entity ecs.Entity) int {
     111            1 :         speed := speedBaseValue
     112            1 : 
     113              :         // 能力値による素の速度
     114            1 :         if abils := world.Components.Abilities.Get(entity); abils != nil {
     115            1 :                 speed += abils.Agility.Total*speedAgilityMultiply + abils.Dexterity.Total*speedDexterityMultiply
     116            1 :         }
     117              : 
     118              :         // 過積載は加算ペナルティ
     119            1 :         speed += calculateOverweightPenalty(world, entity)
     120            1 : 
     121              :         // MoveCost倍率を適用する。全エンティティへ毎ターン走る最頻経路なので、
     122              :         // 内訳を作らない単キー導出で読む。
     123              :         // 100% = 変化なし、90% = 速い(走破スキル)、130% = 遅い(低体温)
     124              :         // MoveCost はコスト倍率なので速度へは逆適用する(高いほど遅い)。ApplyInt は使わない
     125            1 :         moveCost := max(int(ProficiencyValue(world, entity, rawschema.ProficiencyIdMoveCost)), 10)
     126            1 :         speed = speed * 100 / moveCost
     127            1 :         // 実効身体機能の歩行を掛ける。歩行は脚・足の怪我に加え、全身性の不調・疲労・空腹が
     128              :         // EffectiveBodyFuncs で畳まれているので、ここは歩行を読むだけでよい
     129            1 :         moving := EffectiveBodyFuncs(world, entity).Moving
     130            1 :         speed = max(moving.ApplyInt(speed), speedMinimum) // 最小値制限
     131            1 : 
     132            1 :         return speed
     133              : }
     134              : 
     135              : // calculateOverweightPenalty は過積載によるSpeedペナルティを計算する
     136              : func calculateOverweightPenalty(world w.World, entity ecs.Entity) int {
     137            1 :         cw := world.Components.WeightCapacity.Get(entity)
     138            1 :         if cw == nil {
     139            1 :                 return 0
     140            1 :         }
     141            1 :         if cw.Max == 0 {
     142            1 :                 return 0
     143            1 :         }
     144              : 
     145            1 :         if cw.Current > cw.Max {
     146            1 :                 overweight := cw.Current - cw.Max
     147            1 :                 penalty := min(int((overweight*25)/cw.Max), 75)
     148            1 :                 return -penalty
     149            1 :         }
     150              : 
     151            1 :         return 0
     152              : }
        

Generated by: LCOV version 2.0-1