Line data Source code
1 : package components
2 :
3 : import (
4 : "github.com/kijimaD/ruins/internal/consts"
5 : "github.com/kijimaD/ruins/internal/i18n"
6 : "github.com/kijimaD/ruins/internal/rawschema"
7 : )
8 :
9 : // Severity は状態の重症度
10 : type Severity int
11 :
12 : // 重症度定数
13 : const (
14 : SeverityNone Severity = iota // なし
15 : SeverityMinor // 軽度
16 : SeverityMedium // 中度
17 : SeveritySevere // 重度
18 : )
19 :
20 : // Name は重症度の表示名を返す
21 : func (s Severity) Name() i18n.Msgid {
22 1 : switch s {
23 : case SeverityNone:
24 1 : return ""
25 : case SeverityMinor:
26 1 : return "Minor"
27 : case SeverityMedium:
28 1 : return "Medium"
29 : case SeveritySevere:
30 1 : return "Severe"
31 : default:
32 1 : panic("invalid Severity value")
33 : }
34 : }
35 :
36 : // StatType は影響を受けるステータスの種類
37 : type StatType string
38 :
39 : // ステータス種類定数
40 : const (
41 : StatVitality StatType = "Vitality"
42 : StatStrength StatType = "Strength"
43 : StatSensation StatType = "Sensation"
44 : StatDexterity StatType = "Dexterity"
45 : StatAgility StatType = "Agility"
46 : StatDefense StatType = "Defense"
47 : )
48 :
49 : // String はステータス種類の表示名を返す
50 : func (st StatType) String() string {
51 1 : switch st {
52 : case StatVitality:
53 1 : return "Vitality"
54 : case StatStrength:
55 1 : return "Strength"
56 : case StatSensation:
57 1 : return "Sensation"
58 : case StatDexterity:
59 1 : return "Dexterity"
60 : case StatAgility:
61 1 : return "Agility"
62 : case StatDefense:
63 1 : return "Defense"
64 : default:
65 1 : return string(st)
66 : }
67 : }
68 :
69 : // StatEffect はステータスへの1つの影響
70 : type StatEffect struct {
71 : Stat StatType // 影響を受けるステータス
72 : Value int // 修正値
73 : }
74 :
75 : // ConditionType は状態の種類を識別する
76 : type ConditionType string
77 :
78 : // BodyFuncs は身体機能の一式。すべて基準 100 の consts.Percent で、100 が正常、
79 : // 低いほど機能が落ちる。痛み Pain だけは 0 が無痛で、大きいほど痛い。
80 : // 不調から読み取り時に導出し、保存はしない
81 : type BodyFuncs struct {
82 : Pain consts.Percent // 0 が無痛。大きいほど痛い
83 : Blood consts.Percent // 100 が正常。失血や重い不調で下がる。危険域を下回ると HP が減る
84 : Consciousness consts.Percent // 100 が正常。痛みと全身性の不調で下がる。速度・命中へは消費側が掛ける
85 : Manipulation consts.Percent // 腕・手の局所低下に意識を全体乗数で掛けた値。近接命中・製作
86 : Moving consts.Percent // 脚・足の局所低下に意識を全体乗数で掛けた値。速度
87 : Sight consts.Percent // 頭の局所低下に意識を全体乗数で掛けた値。遠隔命中
88 : Metabolism consts.Percent // 100 が正常。空腹・疲労で下がる。回復の身体機能側。VIT・睡眠は熟練側で別に積む
89 : }
90 :
91 : // of は身体機能の種別 k の値を返す
92 : func (b BodyFuncs) of(k BodyFuncKind) consts.Percent {
93 1 : switch k {
94 : case BodyFuncConsciousness:
95 0 : return b.Consciousness
96 : case BodyFuncManipulation:
97 1 : return b.Manipulation
98 : case BodyFuncMoving:
99 0 : return b.Moving
100 : case BodyFuncSight:
101 1 : return b.Sight
102 : case BodyFuncMetabolism:
103 0 : return b.Metabolism
104 : }
105 0 : panic("unknown body function: " + string(k))
106 : }
107 :
108 : // painConsciousnessDivisor は痛みが意識を下げる割合。痛みをこれで割ったぶん意識が下がる。
109 : // 症状に依らない集約側の係数。値は実プレイで調整する
110 : const painConsciousnessDivisor = 2
111 :
112 : // BodyFuncKind は部位が下げる身体機能の区分。値は表示名を兼ねる
113 : type BodyFuncKind string
114 :
115 : // Name は身体機能の表示名を返す
116 0 : func (b BodyFuncKind) Name() i18n.Msgid { return i18n.Msgid(b) }
117 :
118 : // 身体機能の区分
119 : const (
120 : BodyFuncConsciousness BodyFuncKind = "Consciousness" // 胴・全身の全身性
121 : BodyFuncManipulation BodyFuncKind = "Manipulation" // 腕・手
122 : BodyFuncMoving BodyFuncKind = "Moving" // 脚・足
123 : BodyFuncSight BodyFuncKind = "Sight" // 頭
124 : BodyFuncMetabolism BodyFuncKind = "Metabolism" // 回復の身体機能側
125 : )
126 :
127 : // bodyPartMeta は部位ごとの静的情報
128 : type bodyPartMeta struct {
129 : id string // raw の bodyParts の id
130 : bodyFunc BodyFuncKind // この部位の不調が下げる身体機能
131 : }
132 :
133 : // bodyPartMetas は部位ごとの静的情報の唯一の表。部位階層は持たず平坦に持つ。
134 : var bodyPartMetas = [BodyPartCount]bodyPartMeta{
135 : BodyPartHead: {id: "head", bodyFunc: BodyFuncSight},
136 : BodyPartTorso: {id: "torso", bodyFunc: BodyFuncConsciousness},
137 : BodyPartArms: {id: "arm", bodyFunc: BodyFuncManipulation},
138 : BodyPartHands: {id: "hand", bodyFunc: BodyFuncManipulation},
139 : BodyPartLegs: {id: "leg", bodyFunc: BodyFuncMoving},
140 : BodyPartFeet: {id: "foot", bodyFunc: BodyFuncMoving},
141 : BodyPartWholeBody: {id: "whole_body", bodyFunc: BodyFuncConsciousness},
142 : }
143 :
144 : // bodyPartFunction は部位が下げる身体機能を返す
145 : func bodyPartFunction(part BodyPart) BodyFuncKind {
146 1 : return bodyPartMetas[part].bodyFunc
147 1 : }
148 :
149 : // HealthyBodyFuncs は不調が無いときの身体機能を返す。HealthStatus を持たない対象の既定
150 : func HealthyBodyFuncs() BodyFuncs {
151 1 : return (&HealthStatus{}).BodyFuncs(ConditionDefs{})
152 1 : }
153 :
154 : // treatedPenaltyRemain は応急処置後に残る痛みと機能低下の割合。全治まで軽減して残す。
155 : // 出血や HP 減少は治療で止まるが、機能低下はゼロにはならず全治まで引きずる
156 : const treatedPenaltyRemain consts.Percent = 50
157 :
158 : // BodyFuncImpact は不調1件が身体機能へ与える影響を返す。
159 : // 身体機能は部位で定まり重症度に依らない。drop が0なら影響なし
160 : func (d ConditionDefs) BodyFuncImpact(cond *HealthCondition, part BodyPart) (pain int, bodyFunc BodyFuncKind, drop int) {
161 1 : bodyFunc = bodyPartFunction(part)
162 1 : pain, drop = d.severityImpact(cond)
163 1 : return pain, bodyFunc, drop
164 1 : }
165 :
166 : // severityImpact は不調1件の痛みと機能低下を返す。症状ごとの反応率に重症度を掛ける。
167 : // 応急処置済みなら軽減して返す。表示と適用がこの1関数を共有するので両者がずれない
168 : func (d ConditionDefs) severityImpact(cond *HealthCondition) (pain, drop int) {
169 1 : m := conditionSeverityMultiplier(cond.Severity)
170 1 : if m == 0 {
171 1 : return 0, 0
172 1 : }
173 1 : def, ok := d.Def(cond.Type)
174 1 : if !ok {
175 1 : return 0, 0
176 1 : }
177 1 : pain, drop = def.PainPerSeverity*m, def.BodyFuncDropPerSeverity*m
178 1 : if cond.TendQuality > 0 {
179 1 : pain = treatedPenaltyRemain.ApplyInt(pain)
180 1 : drop = treatedPenaltyRemain.ApplyInt(drop)
181 1 : }
182 1 : return pain, drop
183 : }
184 :
185 : // conditionSeverityMultiplier は重症度から効果倍率を返す
186 : func conditionSeverityMultiplier(sev Severity) int {
187 1 : switch sev {
188 : case SeveritySevere:
189 1 : return 3
190 : case SeverityMedium:
191 1 : return 2
192 : case SeverityMinor:
193 1 : return 1
194 : default:
195 1 : return 0
196 : }
197 : }
198 :
199 : // HealthCondition は部位に付与される1つの状態
200 : type HealthCondition struct {
201 : Type ConditionType // 状態の種類
202 : Severity Severity // 重症度
203 : Timer float64 // 進行度タイマー (0-100)
204 : // TendQuality は治療の質。0 なら未治療、正なら治療済みで 100 が標準、150 なら回復1.5倍。
205 : // 適した薬の Potency が入り、回復速度を左右する
206 : TendQuality consts.Percent
207 : }
208 :
209 : // UpdateTimer はタイマーを更新し、Severityを再計算する
210 : // delta が正なら悪化、負なら回復
211 : // 戻り値: (前のSeverity, 新しいSeverity)
212 : func (hc *HealthCondition) UpdateTimer(delta float64) (Severity, Severity) {
213 1 : prevSeverity := hc.Severity
214 1 : hc.Timer = clamp(hc.Timer+delta, 0, 100)
215 1 : hc.Severity = TimerToSeverity(hc.Timer)
216 1 : return prevSeverity, hc.Severity
217 1 : }
218 :
219 : // IsActive はこの状態が発症しているかを返す
220 : func (hc *HealthCondition) IsActive() bool {
221 1 : return hc.Timer >= 25
222 1 : }
223 :
224 : // TimerToSeverity はタイマー値からSeverityを導出する
225 : func TimerToSeverity(timer float64) Severity {
226 1 : switch {
227 : case timer < 25:
228 1 : return SeverityNone
229 : case timer < 50:
230 1 : return SeverityMinor
231 : case timer < 75:
232 1 : return SeverityMedium
233 : default:
234 1 : return SeveritySevere
235 : }
236 : }
237 :
238 : // clamp は値を範囲内に収める
239 : func clamp[T ~int | ~float64](val, minVal, maxVal T) T {
240 1 : if val < minVal {
241 1 : return minVal
242 1 : }
243 1 : if val > maxVal {
244 1 : return maxVal
245 1 : }
246 1 : return val
247 : }
248 :
249 : // BodyPartHealth は1つの部位の健康状態
250 : type BodyPartHealth struct {
251 : Conditions []HealthCondition
252 : }
253 :
254 : // SetCondition は状態を設定する。既存の同種の状態は上書きする
255 : func (bph *BodyPartHealth) SetCondition(cond HealthCondition) {
256 1 : for i := range bph.Conditions {
257 1 : if bph.Conditions[i].Type == cond.Type {
258 1 : bph.Conditions[i] = cond
259 1 : return
260 1 : }
261 : }
262 1 : bph.Conditions = append(bph.Conditions, cond)
263 : }
264 :
265 : // AddCondition は状態を1つ積む。SetCondition と違い同種でも上書きせず別の傷として足す。
266 : // 戦闘の怪我のように独立した傷を重ねるのに使う
267 : func (bph *BodyPartHealth) AddCondition(cond HealthCondition) {
268 1 : bph.Conditions = append(bph.Conditions, cond)
269 1 : }
270 :
271 : // CountConditions は指定した種類の状態がこの部位にいくつあるかを返す。独立傷のソフト上限判定に使う
272 : func (bph *BodyPartHealth) CountConditions(condType ConditionType) int {
273 1 : n := 0
274 1 : for i := range bph.Conditions {
275 1 : if bph.Conditions[i].Type == condType {
276 1 : n++
277 1 : }
278 : }
279 1 : return n
280 : }
281 :
282 : // RemoveCondition は状態を削除する
283 : func (bph *BodyPartHealth) RemoveCondition(condType ConditionType) {
284 1 : for i := range bph.Conditions {
285 1 : if bph.Conditions[i].Type == condType {
286 1 : bph.Conditions = append(bph.Conditions[:i], bph.Conditions[i+1:]...)
287 1 : return
288 1 : }
289 : }
290 : }
291 :
292 : // GetCondition は指定した種類の状態を取得する。存在しない場合はnil
293 : func (bph *BodyPartHealth) GetCondition(condType ConditionType) *HealthCondition {
294 1 : for i := range bph.Conditions {
295 1 : if bph.Conditions[i].Type == condType {
296 1 : return &bph.Conditions[i]
297 1 : }
298 : }
299 1 : return nil
300 : }
301 :
302 : // GetOrCreateCondition は指定した種類の状態を取得または作成する
303 : func (bph *BodyPartHealth) GetOrCreateCondition(condType ConditionType) *HealthCondition {
304 1 : if cond := bph.GetCondition(condType); cond != nil {
305 1 : return cond
306 1 : }
307 1 : bph.Conditions = append(bph.Conditions, HealthCondition{
308 1 : Type: condType,
309 1 : Severity: SeverityNone,
310 1 : Timer: 0,
311 1 : })
312 1 : return &bph.Conditions[len(bph.Conditions)-1]
313 : }
314 :
315 : // SeverityChange はSeverityの変化を表す
316 : type SeverityChange struct {
317 : CondType ConditionType
318 : Prev Severity
319 : Current Severity
320 : }
321 :
322 : // UpdateConditionTimer は指定した状態のタイマーを更新する
323 : // 状態が存在しない場合は作成する
324 : // タイマーが0になった状態は削除する
325 : // 戻り値: Severityの変化情報
326 : func (bph *BodyPartHealth) UpdateConditionTimer(condType ConditionType, delta float64) SeverityChange {
327 1 : cond := bph.GetOrCreateCondition(condType)
328 1 : prev, current := cond.UpdateTimer(delta)
329 1 :
330 : // タイマーが0になったら状態を削除
331 1 : if cond.Timer == 0 {
332 1 : bph.RemoveCondition(condType)
333 1 : }
334 :
335 1 : return SeverityChange{CondType: condType, Prev: prev, Current: current}
336 : }
337 :
338 : // HealthStatus は部位ごとの健康状態を管理するコンポーネント
339 : type HealthStatus struct {
340 : Parts [BodyPartCount]BodyPartHealth
341 : // BodyTempOffset は平熱からの体温のずれ。摂氏。0 が平熱かつ上限で、寒さで負へ動く
342 : BodyTempOffset float64
343 : }
344 :
345 : // bloodCriticalThreshold は血液量がこれを下回ると失血で HP が減り始める割合。
346 : // bloodDrainStep は血液量の不足をこれで割った数だけ毎ターン削る。深く落ちるほど速く減る。
347 : // 外傷は出血で、重症の低体温や病気は循環低下で血液量を下げる。値は実プレイで調整する
348 : const (
349 : bloodCriticalThreshold = 40
350 : bloodDrainStep = 10
351 : )
352 :
353 : // BloodDrop は不調1件がいま下げている血液量を返す。0 なら下げない。
354 : // 外傷は発症中ずっと出血し、重症の全身性の不調は循環を落とす。治療済みは止まる
355 : func (d ConditionDefs) BloodDrop(cond *HealthCondition) int {
356 1 : def, ok := d.Def(cond.Type)
357 1 : if !ok || def.BloodDropPerSeverity == 0 || cond.TendQuality > 0 {
358 1 : return 0
359 1 : }
360 1 : m := conditionSeverityMultiplier(cond.Severity)
361 1 : if m == 0 {
362 1 : return 0
363 1 : }
364 : // 外傷は発症中ずっと血を失う。全身性の不調は重症でだけ循環が落ちる
365 1 : if def.Recovery == rawschema.RecoverAfterTend {
366 1 : if !cond.IsActive() {
367 0 : return 0
368 0 : }
369 1 : } else if cond.Severity != SeveritySevere {
370 1 : return 0
371 1 : }
372 1 : return def.BloodDropPerSeverity * m
373 : }
374 :
375 : // BloodLossHPDrain は血液量の低下による毎ターンの HP 減少量と死因を返す。
376 : // 血液量が危険域を下回ると、不足に応じてじわじわ HP を削る。失血・凍死・衰弱死はすべてここを通る
377 : func (hs *HealthStatus) BloodLossHPDrain(defs ConditionDefs) (int, DeathCause) {
378 1 : drain := BloodLossHPDrainRate(int(hs.BodyFuncs(defs).Blood))
379 1 : if drain == 0 {
380 1 : return 0, ""
381 1 : }
382 1 : return drain, hs.bloodLossCause(defs)
383 : }
384 :
385 : // BloodLossHPDrainRate は血液量から失血による1ターンのHP減少量を返す純関数。危険域 bloodCriticalThreshold
386 : // 以上なら0、下回るほど段階的に増える。バランス導出が血液→HPの連鎖を単一出典で評価できるよう公開する。
387 : func BloodLossHPDrainRate(blood int) int {
388 1 : if blood >= bloodCriticalThreshold {
389 1 : return 0
390 1 : }
391 1 : deficit := bloodCriticalThreshold - blood
392 1 : return (deficit + bloodDrainStep - 1) / bloodDrainStep
393 : }
394 :
395 : // bloodLossCause は血液量を最も下げている不調の死因を返す。該当が無ければ失血に落とす
396 : func (hs *HealthStatus) bloodLossCause(defs ConditionDefs) DeathCause {
397 1 : cause := CauseBloodLoss
398 1 : best := 0
399 1 : for i := range hs.Parts {
400 1 : for j := range hs.Parts[i].Conditions {
401 1 : c := &hs.Parts[i].Conditions[j]
402 1 : if d := defs.BloodDrop(c); d > best {
403 1 : best = d
404 1 : if def := defs.conds[c.Type]; def.Cause != "" {
405 1 : cause = def.Cause
406 1 : }
407 : }
408 : }
409 : }
410 1 : return cause
411 : }
412 :
413 : // IsHPDraining はいま HP を削る要因があるかを返す。血液量が危険域なら削っている。
414 : // 削っているあいだは自然回復を止め、減っているのを読み取れるようにする
415 : func (hs *HealthStatus) IsHPDraining(defs ConditionDefs) bool {
416 1 : drain, _ := hs.BloodLossHPDrain(defs)
417 1 : return drain > 0
418 1 : }
419 :
420 : // BodyFuncs は不調から身体機能の一式を導出する。保存済みの値でなく Timer と Severity から計算する。
421 : // 部位ごとの不調が対応機能を下げ、痛みと全身性の不調が意識を下げ、意識を全体乗数として局所機能へ掛ける。
422 : // needConds に全身性の不調を渡すと保存された不調と一緒に集約する。渡さなければ保存された不調だけで導出する
423 : func (hs *HealthStatus) BodyFuncs(defs ConditionDefs, needConds ...HealthCondition) BodyFuncs {
424 1 : pain := 0
425 1 : bloodDrop := 0
426 1 : var manip, moving, sight, systemic int // 各機能の低下量
427 1 : for i := range hs.Parts {
428 1 : for j := range hs.Parts[i].Conditions {
429 1 : cond := &hs.Parts[i].Conditions[j]
430 1 : p, drop := defs.severityImpact(cond)
431 1 : pain += p
432 1 : bloodDrop += defs.BloodDrop(cond)
433 1 : switch bodyPartFunction(BodyPart(i)) {
434 : case BodyFuncManipulation:
435 1 : manip += drop
436 : case BodyFuncMoving:
437 1 : moving += drop
438 : case BodyFuncSight:
439 1 : sight += drop
440 : case BodyFuncConsciousness:
441 1 : systemic += drop
442 0 : case BodyFuncMetabolism:
443 : // 代謝は部位に割り当てない。疲労・空腹の不調から下記で導く
444 : }
445 : }
446 : }
447 :
448 : // 疲労・空腹の不調は全身性。意識へ集約し、代謝を下げる量 needDrop としても集計する
449 1 : needDrop := 0
450 1 : for i := range needConds {
451 0 : p, drop := defs.severityImpact(&needConds[i])
452 0 : pain += p
453 0 : systemic += drop
454 0 : needDrop += drop
455 0 : }
456 :
457 1 : pain = clamp(pain, 0, 100)
458 1 : // 意識は全身性の低下と痛みで下がる。怪我・病気・過労・栄養失調がすべて systemic へ集約されている
459 1 : consciousness := clamp(100-systemic-pain/painConsciousnessDivisor, 0, 100)
460 1 : // 局所機能は低下を引いたうえで意識を全体乗数として掛ける
461 1 : withConsciousness := func(local int) consts.Percent {
462 1 : return consts.Percent(clamp(local, 0, 100) * consciousness / 100)
463 1 : }
464 1 : return BodyFuncs{
465 1 : Pain: consts.Percent(pain),
466 1 : Blood: consts.Percent(clamp(100-bloodDrop, 0, 100)),
467 1 : Consciousness: consts.Percent(consciousness),
468 1 : Manipulation: withConsciousness(100 - manip),
469 1 : Moving: withConsciousness(100 - moving),
470 1 : Sight: withConsciousness(100 - sight),
471 1 : // 代謝は回復の身体機能側。過労・栄養失調でだけ下がり、痛み・全身病は含めない
472 1 : Metabolism: consts.Percent(clamp(100-needDrop, 0, 100)),
473 1 : }
474 : }
475 :
476 : // ConsciousnessDrops は意識を下げる不調由来の内訳を返す。全身性の低下ぶんと痛みぶんに分けて返す。
477 : // 疲労・空腹は量由来で保存しないため含めない。呼び出し側が量から足す。内訳表示に使う
478 : func (hs *HealthStatus) ConsciousnessDrops(defs ConditionDefs) (systemic, pain int) {
479 1 : rawPain := 0
480 1 : for i := range hs.Parts {
481 1 : for j := range hs.Parts[i].Conditions {
482 1 : cond := &hs.Parts[i].Conditions[j]
483 1 : p, drop := defs.severityImpact(cond)
484 1 : rawPain += p
485 1 : if bodyPartFunction(BodyPart(i)) == BodyFuncConsciousness {
486 1 : systemic += drop
487 1 : }
488 : }
489 : }
490 1 : return systemic, clamp(rawPain, 0, 100) / painConsciousnessDivisor
491 : }
492 :
493 : // LocalDrop は指定した局所身体機能を下げる部位の不調ぶんを返す。意識の 全体乗数を掛ける前の局所低下で、
494 : // 局所機能の内訳表示に使う。全身性の Consciousness を渡すと 0 を返す
495 : func (hs *HealthStatus) LocalDrop(defs ConditionDefs, bf BodyFuncKind) int {
496 1 : drop := 0
497 1 : for i := range hs.Parts {
498 1 : if bodyPartFunction(BodyPart(i)) != bf {
499 1 : continue
500 : }
501 1 : for j := range hs.Parts[i].Conditions {
502 1 : _, d := defs.severityImpact(&hs.Parts[i].Conditions[j])
503 1 : drop += d
504 1 : }
505 : }
506 1 : return drop
507 : }
|