LCOV - code coverage report
Current view: top level - components - proficiency.go Coverage Total Hit
Test: coverage.lcov Lines: 94.7 % 38 36
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package components
       2              : 
       3              : import (
       4              :         "github.com/kijimaD/ruins/internal/consts"
       5              :         "github.com/kijimaD/ruins/internal/rawschema"
       6              : )
       7              : 
       8              : // ProficiencySourceKind は内訳1件の由来の種別
       9              : type ProficiencySourceKind string
      10              : 
      11              : // 内訳の由来種別
      12              : const (
      13              :         SourceSkill    ProficiencySourceKind = "skill"         // スキルによる補正
      14              :         SourceAbility  ProficiencySourceKind = "ability"       // 能力値による補正
      15              :         SourceBodyFunc ProficiencySourceKind = "body_function" // 身体機能の畳み込み
      16              :         SourceFatigue  ProficiencySourceKind = "fatigue"       // 疲労段階の畳み込み
      17              :         SourceHunger   ProficiencySourceKind = "hunger"        // 空腹段階の畳み込み
      18              :         SourceSleeping ProficiencySourceKind = "sleeping"      // 睡眠中の畳み込み
      19              : )
      20              : 
      21              : // ProficiencySource は効果倍率の算出元1件を表す。整形済みの文字列でなく事実を持ち、
      22              : // 表示側が現在言語へ訳して整形する。Kind に応じて Skill / Ability / BodyFunc のどれかが有効
      23              : type ProficiencySource struct {
      24              :         Kind     ProficiencySourceKind
      25              :         Skill    rawschema.SkillId        // Kind が skill のときのスキル
      26              :         Ability  rawschema.AbilityId      // Kind が ability のときの能力値
      27              :         BodyFunc BodyFuncKind             // Kind が body function のときの身体機能
      28              :         Fatigue  rawschema.FatigueStageId // Kind が fatigue のときの疲労段階
      29              :         Hunger   rawschema.HungerStageId  // Kind が hunger のときの空腹段階
      30              :         Amount   int                      // 要因の量。スキルLv・能力値・身体機能%
      31              :         Value    int                      // この要因による変化量。例: +10, -15
      32              : }
      33              : 
      34              : // forEachProficiencySource は key の内訳を計算順に fn へ渡す。値も内訳もこの1関数から導く。
      35              : // 最終値 = 基準 + Σ内訳 の不変条件はこの構造そのものが保証する。未定義キーは何も渡さない。
      36              : // skills / abils は不在なら nil でよく、その由来のソースは飛ばす。bodyFuncs は実効身体機能で、
      37              : // 疲労・空腹の低下を畳んだものを呼び出し側が渡す
      38              : func (d SkillDefs) forEachProficiencySource(skills *Skills, abils *Abilities, bodyFuncs BodyFuncs, key rawschema.ProficiencyId, fn func(ProficiencySource)) {
      39            1 :         spec, ok := d.Proficiencies[key]
      40            1 :         if !ok {
      41            1 :                 return
      42            1 :         }
      43            1 :         if skills == nil {
      44            0 :                 return
      45            0 :         }
      46            1 :         v := skills.Level(spec.Skill)
      47            1 :         bonus := v * spec.PerLevel
      48            1 :         fn(ProficiencySource{Kind: SourceSkill, Skill: spec.Skill, Amount: v, Value: bonus})
      49            1 : 
      50              :         // 対応する能力値による補正。能力値1ポイントにつきスキル係数と同じ方向に±1%
      51            1 :         if abils != nil {
      52            1 :                 ablID := d.Ability(spec.Skill)
      53            1 :                 ablVal := abils.ValueOf(ablID)
      54            1 :                 ablCoeff := 1
      55            1 :                 if spec.PerLevel < 0 {
      56            1 :                         ablCoeff = -1
      57            1 :                 }
      58            1 :                 ablBonus := ablVal * ablCoeff
      59            1 :                 fn(ProficiencySource{Kind: SourceAbility, Ability: ablID, Amount: ablVal, Value: ablBonus})
      60            1 :                 bonus += ablBonus
      61              :         }
      62              : 
      63              :         // 命中へ効く身体機能を乗算で畳み、内訳には加法差分で載せる。bodyFuncs は実効身体機能なので、
      64              :         // 部位の怪我に加え全身性の不調・疲労・空腹が既に畳まれている。ここは該当機能を掛けるだけ
      65            1 :         if spec.BodyFunc != "" {
      66            1 :                 limbVal := bodyFuncs.of(spec.BodyFunc)
      67            1 :                 acc := int(consts.PercentBase) + bonus
      68            1 :                 withCap := limbVal.ApplyInt(acc)
      69            1 :                 if withCap != acc {
      70            1 :                         fn(ProficiencySource{Kind: SourceBodyFunc, BodyFunc: spec.BodyFunc, Amount: int(limbVal), Value: withCap - acc})
      71            1 :                 }
      72              :         }
      73              : }
      74              : 
      75              : // ProficiencyValue は key の効果倍率を導出する。内訳の加法差分を積むだけで
      76              : // アロケーションが無い。表示の%も適用もこの関数を読むので両者は一致する
      77              : func (d SkillDefs) ProficiencyValue(skills *Skills, abils *Abilities, bodyFuncs BodyFuncs, key rawschema.ProficiencyId) consts.Percent {
      78            1 :         total := int(consts.PercentBase)
      79            1 :         d.forEachProficiencySource(skills, abils, bodyFuncs, key, func(s ProficiencySource) {
      80            1 :                 total += s.Value
      81            1 :         })
      82            1 :         return consts.Percent(total)
      83              : }
      84              : 
      85              : // ProficiencySources は key の内訳を返す。詳細モーダルの表示側だけが読む。
      86              : // 未定義キーは空を返す
      87              : func (d SkillDefs) ProficiencySources(skills *Skills, abils *Abilities, bodyFuncs BodyFuncs, key rawschema.ProficiencyId) []ProficiencySource {
      88            1 :         var srcs []ProficiencySource
      89            1 :         d.forEachProficiencySource(skills, abils, bodyFuncs, key, func(s ProficiencySource) {
      90            1 :                 srcs = append(srcs, s)
      91            1 :         })
      92            1 :         return srcs
      93              : }
        

Generated by: LCOV version 2.0-1