Line data Source code
1 : package systems
2 :
3 : import (
4 : "slices"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/rawschema"
9 : w "github.com/kijimaD/ruins/internal/world"
10 : "github.com/kijimaD/ruins/internal/world/gameaction"
11 : "github.com/kijimaD/ruins/internal/world/query"
12 : "github.com/mlange-42/ark/ecs"
13 : )
14 :
15 : // conditionMinorNaturalRecoveryPerTurn は RecoverAfterTend の不調が発症前のとき代謝によらず固定で癒える1ターンの量
16 : const conditionMinorNaturalRecoveryPerTurn = 1
17 :
18 : // managedConditionDef は ConditionSystem が扱う不調の定義を返す。
19 : // Recovery を持つ症状だけを管轄する。低体温は Recovery を持たず TemperatureSystem 管轄なので ok=false
20 : func managedConditionDef(defs gc.ConditionDefs, ct gc.ConditionType) (gc.ConditionDef, bool) {
21 1 : def, ok := defs.Def(ct)
22 1 : if !ok || def.Recovery == "" {
23 1 : return gc.ConditionDef{}, false
24 1 : }
25 1 : return def, true
26 : }
27 :
28 : // ConditionSystem は毎ターン怪我と病気の不調を進めるシステム。
29 : // 低体温の進行と回復は TemperatureSystem が担うので、ここでは扱わない。
30 : type ConditionSystem struct{}
31 :
32 : // String はシステム名を返す
33 : func (sys *ConditionSystem) String() string {
34 1 : return "ConditionSystem"
35 1 : }
36 :
37 : // conditionDamage は重症の不調がターン後に与える HP ダメージ
38 : type conditionDamage struct {
39 : entity ecs.Entity
40 : amount int
41 : cause gc.DeathCause
42 : }
43 :
44 : // Update は怪我と病気の回復軌道を進め、重症の不調で HP を削る
45 : func (sys *ConditionSystem) Update(world w.World) error {
46 1 : var toDamage []conditionDamage
47 1 : defs := world.Resources.ConditionDefs
48 1 :
49 1 : healthQuery := query.ActiveFilter1[gc.HealthStatus](world).Query()
50 1 : for healthQuery.Next() {
51 1 : entity := healthQuery.Entity()
52 1 : hs := world.Components.HealthStatus.Get(entity)
53 1 : metab := query.Metabolism(world, entity)
54 1 : hasHP := world.Components.HP.Has(entity)
55 1 :
56 1 : for p := range hs.Parts {
57 1 : part := &hs.Parts[p]
58 1 : for i := range part.Conditions {
59 1 : cond := &part.Conditions[i]
60 1 : def, ok := managedConditionDef(defs, cond.Type)
61 1 : if !ok {
62 1 : continue
63 : }
64 :
65 1 : if delta := conditionTimerDelta(def, cond, metab); delta != 0 {
66 1 : cond.UpdateTimer(delta)
67 1 : }
68 :
69 : }
70 :
71 : // Timer が 0 になった管理下の不調を除去する
72 1 : part.Conditions = slices.DeleteFunc(part.Conditions, func(c gc.HealthCondition) bool {
73 1 : _, managed := managedConditionDef(defs, c.Type)
74 1 : return managed && c.Timer == 0
75 1 : })
76 : }
77 :
78 : // 血液量が危険域まで落ちたら、じわじわ HP を削る。失血・凍死・衰弱はすべて血液量経由で死なせる
79 1 : if hasHP {
80 1 : if drain, cause := hs.BloodLossHPDrain(defs); drain > 0 {
81 1 : toDamage = append(toDamage, conditionDamage{entity: entity, amount: drain, cause: cause})
82 1 : }
83 : }
84 : }
85 :
86 1 : for _, d := range toDamage {
87 1 : gameaction.ApplyConditionDamage(world, d.entity, d.amount, d.cause)
88 1 : }
89 :
90 1 : return nil
91 : }
92 :
93 : // metabScale は回復量 rec に代謝を float で掛ける。整数切り捨てをしないので、代謝が低くても
94 : // 正の回復意図がある限り 0 で止まらない。代謝 0 のときだけ 0 になる
95 : func metabScale(rec int, metab consts.Percent) float64 {
96 1 : return float64(rec) * float64(metab) / float64(consts.PercentBase)
97 1 : }
98 :
99 : // conditionTimerDelta は不調の回復モードと治療の質から1ターンの Timer 増減を返す。
100 : // 負なら回復、正なら悪化。int の目盛りで計算し、UpdateTimer の境界で float64 にする。
101 : func conditionTimerDelta(def gc.ConditionDef, cond *gc.HealthCondition, metab consts.Percent) float64 {
102 1 : switch def.Recovery {
103 : case rawschema.RecoverAfterTend:
104 1 : if cond.TendQuality > 0 {
105 1 : return -float64(cond.TendQuality.ApplyInt(def.RecoverPer))
106 1 : }
107 : // 発症前の掠り傷相当は代謝によらず固定で僅かに癒える。発症後の未治療は保持する
108 1 : if !cond.IsActive() {
109 1 : return -float64(conditionMinorNaturalRecoveryPerTurn)
110 1 : }
111 1 : return 0
112 : case rawschema.ProgressUntilTend:
113 1 : if cond.TendQuality == 0 {
114 1 : return float64(def.WorsenPer)
115 1 : }
116 : // 治療済みは質と代謝の両方で回復が速まる。代謝は float で掛け、低くても 0 で止めない。
117 : // 不調が意識を下げ自分の回復を鈍らせても、整数切り捨てで治癒が停止しないようにする
118 1 : rec := cond.TendQuality.ApplyInt(def.RecoverPer)
119 1 : return -metabScale(rec, metab)
120 : case rawschema.RecoverOverTime:
121 : // 自己限定。未治療でも代謝で自然に治る。治療すればその質でさらに速まる
122 1 : rec := def.RecoverPer
123 1 : if cond.TendQuality > 0 {
124 1 : rec = cond.TendQuality.ApplyInt(rec)
125 1 : }
126 1 : return -metabScale(rec, metab)
127 : }
128 : // default を置くと exhaustive linter が沈黙するので置かない。内部の信頼できる値なので未知は panic する
129 0 : panic("unknown RecoveryMode: " + string(def.Recovery))
130 : }
|