LCOV - code coverage report
Current view: top level - components - material.go Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 11 11
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/i18n"
       6              :         "github.com/kijimaD/ruins/internal/rawschema"
       7              : )
       8              : 
       9              : // Material はアイテムの材質を表す。可燃性と燃焼熱量の算出根拠で、生成時に raw から付く。
      10              : // Kind は raw の Material enum の値。金属や石など不燃の材質も保持し、
      11              : // 観察メニューで材質を見せて、なぜ燃える/燃えないかを読み取れるようにする。
      12              : type Material struct {
      13              :         Kind rawschema.Material // 材質の種類。WOOD・METAL・FOOD など。enum で比較のタイプミスを型が弾く
      14              : }
      15              : 
      16              : // MaterialDef は材質1つの定義。raw の materials の1行から組み立てる
      17              : type MaterialDef struct {
      18              :         Name        i18n.Msgid           // 表示名
      19              :         HeatPerKg   int                  // kgあたり燃焼熱量。0 なら不燃
      20              :         Disassembly *MaterialDisassembly // 分解で戻る素材。nil なら分解できない
      21              : }
      22              : 
      23              : // MaterialDisassembly は材質のアイテムを分解したときに戻る素材
      24              : type MaterialDisassembly struct {
      25              :         Item      string           // 戻る素材の id
      26              :         PerWeight consts.Milligram // 素材1個に要るアイテムの重さ
      27              : }
      28              : 
      29              : // MaterialDefs は材質ごとの表示名と燃焼熱量の表。読み込み時に raw から1度だけ組み立て、以後は変えない
      30              : type MaterialDefs map[rawschema.Material]MaterialDef
      31              : 
      32              : // HeatOf は材質と重量から燃焼熱量を導く。不燃の材質は0を返す。
      33              : // 整数で閉じるため mg 基準で計算し 1kg=1_000_000mg で割って kg 換算する。切り捨てで軽い物は0になる
      34              : func (d MaterialDefs) HeatOf(material rawschema.Material, weight consts.Milligram) consts.Heat {
      35            1 :         return consts.Heat(d[material].HeatPerKg * int(weight) / consts.MilligramPerKg)
      36            1 : }
      37              : 
      38              : // DisassemblyOf は材質と重さのアイテム itemID を分解したときに戻る素材の id と個数を返す。
      39              : // 個数は重さを PerWeight で割って切り捨てる。分解できない材質、戻し先の素材そのもの、1個に満たない軽い物は ok が false
      40              : func (d MaterialDefs) DisassemblyOf(itemID string, material rawschema.Material, weight consts.Milligram) (id string, count int, ok bool) {
      41            1 :         dis := d[material].Disassembly
      42            1 :         if dis == nil || dis.Item == itemID {
      43            1 :                 return "", 0, false
      44            1 :         }
      45            1 :         count = int(weight / dis.PerWeight)
      46            1 :         if count < 1 {
      47            1 :                 return "", 0, false
      48            1 :         }
      49            1 :         return dis.Item, count, true
      50              : }
        

Generated by: LCOV version 2.0-1