LCOV - code coverage report
Current view: top level - components - condition_defs.go Coverage Total Hit
Test: coverage.lcov Lines: 93.3 % 30 28
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package components
       2              : 
       3              : import (
       4              :         "fmt"
       5              :         "maps"
       6              :         "slices"
       7              : 
       8              :         "github.com/kijimaD/ruins/internal/i18n"
       9              : 
      10              :         "github.com/kijimaD/ruins/internal/rawschema"
      11              : )
      12              : 
      13              : // ConditionDef は状態種類ごとの定義。raw の conditions の1行から組み立てる
      14              : type ConditionDef struct {
      15              :         Name                    i18n.Msgid               // 表示名
      16              :         Description             i18n.Msgid               // 概要説明
      17              :         PainPerSeverity         int                      // 重症度1段あたりに加える痛み
      18              :         BodyFuncDropPerSeverity int                      // 重症度1段あたりに下げる該当機能の量
      19              :         Recovery                rawschema.RecoveryMode   // 未治療の振る舞いと治し方。空なら ConditionSystem 管轄外
      20              :         WorsenPer               int                      // rawschema.ProgressUntilTend で未治療のとき1ターン Timer を増やす量
      21              :         RecoverPer              int                      // 治療済みで1ターン Timer を減らす基準量。質と代謝で増減する
      22              :         BloodDropPerSeverity    int                      // 重症度1段あたりに下げる血液量。外傷は出血、重い全身性は循環低下
      23              :         Cause                   DeathCause               // 血液量が尽きて倒したときの死因
      24              :         Parts                   []BodyPart               // 起こりうる部位。空なら全部位
      25              :         Onset                   rawschema.ConditionOnset // 不調を起こす仕組み。攻撃で負う怪我ときっかけの無い不調は空
      26              : }
      27              : 
      28              : // BodyPartDef は部位ごとの定義。raw の bodyParts の1行から組み立てる
      29              : type BodyPartDef struct {
      30              :         Name      i18n.Msgid // 表示名
      31              :         HitWeight float64    // 戦闘の命中抽選での重み。0 なら命中先にならない
      32              : }
      33              : 
      34              : // ConditionDefs は状態種類ごとの定義、部位ごとの命中の重み、攻撃種別ごとの怪我の表。
      35              : // 読み込み時に raw から1度だけ組み立て、以後は変えない。ゼロ値は定義を持たない表として振る舞う
      36              : type ConditionDefs struct {
      37              :         conds    map[ConditionType]ConditionDef
      38              :         parts    [BodyPartCount]BodyPartDef
      39              :         injuries map[rawschema.AttackCategory]ConditionType
      40              :         onsets   map[rawschema.ConditionOnset]ConditionType
      41              : }
      42              : 
      43              : // NewConditionDefs は状態種類ごとの定義、部位ごとの命中の重み、攻撃種別の Type ごとの怪我から表を作る。
      44              : // 渡した map は表が所有する
      45              : func NewConditionDefs(conds map[ConditionType]ConditionDef, parts [BodyPartCount]BodyPartDef, injuries map[rawschema.AttackCategory]ConditionType) ConditionDefs {
      46            1 :         onsets := make(map[rawschema.ConditionOnset]ConditionType)
      47            1 :         for ct := range conds {
      48            1 :                 if o := conds[ct].Onset; o != "" {
      49            1 :                         onsets[o] = ct
      50            1 :                 }
      51              :         }
      52            1 :         return ConditionDefs{conds: conds, parts: parts, injuries: injuries, onsets: onsets}
      53              : }
      54              : 
      55              : // Def は状態種類の定義を返す。未登録なら ok=false
      56              : func (d ConditionDefs) Def(ct ConditionType) (ConditionDef, bool) {
      57            1 :         def, ok := d.conds[ct]
      58            1 :         return def, ok
      59            1 : }
      60              : 
      61              : // Types は表にある状態種類を id の順に返す
      62              : func (d ConditionDefs) Types() []ConditionType {
      63            1 :         return slices.Sorted(maps.Keys(d.conds))
      64            1 : }
      65              : 
      66              : // CanAffect は状態種類 ct が部位 part に起こりうるかを返す
      67              : func (d ConditionDefs) CanAffect(ct ConditionType, part BodyPart) bool {
      68            1 :         def, ok := d.Def(ct)
      69            1 :         if !ok {
      70            1 :                 return false
      71            1 :         }
      72            1 :         return len(def.Parts) == 0 || slices.Contains(def.Parts, part)
      73              : }
      74              : 
      75              : // PartName は部位の表示名 msgid を返す
      76              : func (d ConditionDefs) PartName(part BodyPart) i18n.Msgid {
      77            0 :         return d.parts[part].Name
      78            0 : }
      79              : 
      80              : // HitWeight は戦闘の命中抽選での部位の重みを返す。0 なら命中先にならない
      81              : func (d ConditionDefs) HitWeight(part BodyPart) float64 {
      82            1 :         return d.parts[part].HitWeight
      83            1 : }
      84              : 
      85              : // Injury は攻撃種別が命中で負わせる怪我を返す。未登録なら ok=false
      86              : func (d ConditionDefs) Injury(at rawschema.AttackCategory) (ConditionType, bool) {
      87            1 :         ct, ok := d.injuries[at]
      88            1 :         return ct, ok
      89            1 : }
      90              : 
      91              : // ByOnset は仕組み o が起こす不調を返す。仕組みごとに不調があることは読み込み時に確かめるので、無ければ panic する
      92              : func (d ConditionDefs) ByOnset(o rawschema.ConditionOnset) ConditionType {
      93            1 :         ct, ok := d.onsets[o]
      94            1 :         if !ok {
      95            1 :                 panic(fmt.Sprintf("no condition for onset %q", o))
      96              :         }
      97            1 :         return ct
      98              : }
      99              : 
     100              : // IsDerived は不調が疲労・空腹の量から読み取り時に導出されるものかを返す。
     101              : // 導出される不調は保存されず、Timer も治療状態も持たない
     102              : func (d ConditionDefs) IsDerived(ct ConditionType) bool {
     103            1 :         def, ok := d.Def(ct)
     104            1 :         return ok && (def.Onset == rawschema.Fatigue || def.Onset == rawschema.Hunger)
     105            1 : }
        

Generated by: LCOV version 2.0-1