Line data Source code
1 : package systems
2 :
3 : import (
4 : "errors"
5 : "math"
6 :
7 : "github.com/kijimaD/ruins/internal/i18n"
8 :
9 : gc "github.com/kijimaD/ruins/internal/components"
10 : "github.com/kijimaD/ruins/internal/consts"
11 : "github.com/kijimaD/ruins/internal/gamelog"
12 : "github.com/kijimaD/ruins/internal/rawschema"
13 : w "github.com/kijimaD/ruins/internal/world"
14 :
15 : "github.com/kijimaD/ruins/internal/world/query"
16 : "github.com/mlange-42/ark/ecs"
17 : )
18 :
19 : // TemperatureSystem は体温の更新を行うシステム
20 : // 環境気温から健康状態のタイマーを更新する
21 : type TemperatureSystem struct{}
22 :
23 : // String はシステム名を返す
24 : func (sys *TemperatureSystem) String() string {
25 1 : return "TemperatureSystem"
26 1 : }
27 :
28 : // naturalRecoveryPerTurn は悪化方向でないときに体温状態タイマーが1ターンで下がる量
29 : const naturalRecoveryPerTurn = 0.25
30 :
31 : // 体温の定数。値は実プレイで調整する
32 : const (
33 : // bodyTempColdBand は平熱からこれだけ下がるまでを正常とみなす帯。これより冷えると低体温タイマーが進む
34 : bodyTempColdBand = 2.0
35 : // bodyTempMin はオフセットの下限クランプ
36 : bodyTempMin = -5.0
37 : // bodyTempMax はオフセットの上限クランプ。平熱が上限
38 : bodyTempMax = 0.0
39 : // bodyTempHomeostasisPerTurn は外因が無いときに平熱へ戻る1ターンの量
40 : bodyTempHomeostasisPerTurn = 0.1
41 : )
42 :
43 : // Update は環境から体温を動かし、正常帯を外れた体温で健康状態のタイマーを進める
44 : func (sys *TemperatureSystem) Update(world w.World) error {
45 1 : if query.GetDungeon(world) == nil {
46 1 : return errors.New("dungeon resource is not set")
47 1 : }
48 :
49 : // 体温の生存機構はプレイヤーと味方だけに適用する。空腹・疲労と同じく敵には効かせない
50 1 : var toMark []ecs.Entity
51 1 : healthQuery := query.ActiveFilter3[gc.HealthStatus, gc.GridElement, gc.FactionAlly](world).Query()
52 1 : for healthQuery.Next() {
53 1 : entity := healthQuery.Entity()
54 1 : hs := world.Components.HealthStatus.Get(entity)
55 1 :
56 : // 体温を環境レートぶん動かす
57 1 : rate := bodyTempRate(world, entity)
58 1 : hs.BodyTempOffset = math.Max(bodyTempMin, math.Min(bodyTempMax, hs.BodyTempOffset+rate))
59 1 :
60 1 : isPlayer := world.Components.Player.Has(entity)
61 1 :
62 : // 低体温進行倍率を取得する。体温の物理には掛けず、タイマー進行にだけ掛ける
63 1 : coldProgressPct := query.ProficiencyValue(world, entity, rawschema.ProficiencyIdColdProgress)
64 1 :
65 : // 体温の帯判定から健康状態を更新
66 1 : hasChange := updateTemperatureConditions(world, hs, isPlayer, coldProgressPct)
67 1 :
68 : // 状態変化があれば属性を再計算
69 1 : if isPlayer && hasChange {
70 0 : toMark = append(toMark, entity)
71 0 : }
72 : }
73 :
74 1 : for _, entity := range toMark {
75 0 : if !world.Components.StatsChanged.Has(entity) {
76 0 : world.Components.StatsChanged.Add(entity, &gc.StatsChanged{})
77 0 : }
78 : }
79 :
80 1 : return nil
81 : }
82 :
83 : // bodyTempRate は現在地の環境が1ターンに動かす体温の変化量を返す。温まる向きが正。
84 : // Update の適用と HUD のトレンド矢印が同じこの関数を読む
85 : func bodyTempRate(world w.World, entity ecs.Entity) float64 {
86 1 : if !world.Components.HealthStatus.Has(entity) || !world.Components.GridElement.Has(entity) {
87 1 : return 0
88 1 : }
89 1 : grid := world.Components.GridElement.Get(entity)
90 1 : ambientTemp, err := query.AmbientTemperatureAt(world, grid.X, grid.Y)
91 1 : if err != nil {
92 0 : return 0
93 0 : }
94 1 : insulation := query.CalculateEquippedInsulation(world, entity)
95 1 : offset := world.Components.HealthStatus.Get(entity).BodyTempOffset
96 1 :
97 1 : var rate float64
98 1 : if cold := calcBodyTempRate(ambientTemp + insulation.Cold); cold < 0 {
99 1 : rate += cold
100 1 : }
101 : // 熱源は冷えた体だけを温める。平熱以上では効かせない
102 1 : if offset < 0 {
103 1 : rate += query.HeatSourceWarmthAt(world, grid.X, grid.Y)
104 1 : }
105 : // 外因が無ければ恒常性で平熱へ戻る
106 1 : if rate == 0 && offset < 0 {
107 1 : return math.Min(bodyTempHomeostasisPerTurn, -offset)
108 1 : }
109 1 : return rate
110 : }
111 :
112 : // updateTemperatureConditions は体温の正常帯判定から全身の体温状態タイマーを更新する。
113 : // - 帯の外では超過に応じてタイマーが進み、帯の中では自然回復する。
114 : // - isPlayerがtrueの場合、状態変化時にログを出力する。
115 : // - coldProgressPctは低体温進行倍率%。100が基準で、低いほど進行が遅くなる。
116 : // - 戻り値: 状態のSeverityが変化した場合trueを返す
117 : func updateTemperatureConditions(world w.World, hs *gc.HealthStatus, isPlayer bool, coldProgressPct consts.Percent) bool {
118 1 : hasChange := false
119 1 : partHealth := &hs.Parts[gc.BodyPartWholeBody]
120 1 : offset := hs.BodyTempOffset
121 1 :
122 1 : var changes []gc.SeverityChange
123 1 :
124 : // 低体温の処理
125 1 : cold := world.Resources.ConditionDefs.ByOnset(rawschema.Cold)
126 1 : switch {
127 : case offset < -bodyTempColdBand:
128 1 : delta := coldProgressPct.ApplyFloat(timerProgress(-offset - bodyTempColdBand))
129 1 : changes = append(changes, partHealth.UpdateConditionTimer(cold, delta))
130 : case partHealth.GetCondition(cold) != nil:
131 1 : changes = append(changes, partHealth.UpdateConditionTimer(cold, -naturalRecoveryPerTurn))
132 : }
133 :
134 1 : for _, change := range changes {
135 1 : if change.Prev != change.Current {
136 1 : hasChange = true
137 1 : if isPlayer {
138 0 : logTemperatureChange(world, change.Current, change.Prev)
139 0 : }
140 : }
141 : }
142 :
143 1 : return hasChange
144 : }
145 :
146 : // timerProgress は正常帯からの超過 excess °C に応じたタイマー進行量を返す。
147 : // 帯の縁で 0.25、超過1°Cごとに +0.25、上限 1.0
148 : func timerProgress(excess float64) float64 {
149 1 : return math.Min(naturalRecoveryPerTurn+0.25*excess, 1.0)
150 1 : }
151 :
152 : // BodyTempColdBand は平熱からこれだけ下がると低体温タイマーが進む帯。バランス導出が
153 : // 低体温の到達時間を出せるよう公開する。
154 : const BodyTempColdBand = bodyTempColdBand
155 :
156 : // CalcBodyTempRate は実効温度から1ターンの体温変化量を返す純関数。バランス導出用の公開窓。
157 1 : func CalcBodyTempRate(effectiveTemp int) float64 { return calcBodyTempRate(effectiveTemp) }
158 :
159 : // calcBodyTempRate は有効温度から体温の変化量を計算する。寒いほど負へ大きく、適温以上は0で正の値は返さない
160 : func calcBodyTempRate(effectiveTemp int) float64 {
161 1 : switch {
162 : case effectiveTemp <= -50:
163 1 : return -0.5 // 極寒。最も厳しい区分で、居座れば急速に凍える
164 : case effectiveTemp <= 0:
165 1 : return -0.2 // 非常に寒い
166 : case effectiveTemp <= 10:
167 1 : return -0.1 // 寒い
168 : default:
169 1 : return 0 // 適温以上
170 : }
171 : }
172 :
173 : // logTemperatureChange は状態変化をログ出力する
174 : func logTemperatureChange(world w.World, current, prev gc.Severity) {
175 1 : var msg i18n.Msgid
176 1 : if current > prev {
177 1 : msg = getWorseningMessage(current)
178 1 : } else {
179 1 : msg = getRecoveryMessage(current)
180 1 : }
181 :
182 1 : if msg != "" {
183 1 : gamelog.New(query.GetGameLog(world)).
184 1 : Markup(gamelog.Tag("warning", query.T(world, msg))).
185 1 : Log()
186 1 : }
187 : }
188 :
189 : // getWorseningMessage は寒さによる不調が悪化したときのメッセージを返す
190 : func getWorseningMessage(severity gc.Severity) i18n.Msgid {
191 1 : switch severity {
192 : case gc.SeverityNone:
193 1 : return ""
194 : case gc.SeverityMinor:
195 1 : return "The cold is setting in"
196 : case gc.SeverityMedium:
197 1 : return "You are quite cold"
198 : case gc.SeveritySevere:
199 1 : return "The cold is dangerous"
200 : }
201 0 : return ""
202 : }
203 :
204 : // getRecoveryMessage は寒さによる不調が回復したときのメッセージを返す
205 : func getRecoveryMessage(severity gc.Severity) i18n.Msgid {
206 1 : switch severity {
207 : case gc.SeverityNone:
208 1 : return "You have warmed up"
209 : case gc.SeverityMinor:
210 1 : return "You are warming up a little"
211 : case gc.SeverityMedium:
212 1 : return "Still cold, but a little better"
213 : case gc.SeveritySevere:
214 1 : return ""
215 : }
216 0 : return ""
217 : }
|