LCOV - code coverage report
Current view: top level - world/gameaction - craft.go Coverage Total Hit
Test: coverage.lcov Lines: 86.8 % 76 66
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package gameaction
       2              : 
       3              : import (
       4              :         "errors"
       5              :         "fmt"
       6              :         "math/rand/v2"
       7              : 
       8              :         gc "github.com/kijimaD/ruins/internal/components"
       9              :         "github.com/kijimaD/ruins/internal/consts"
      10              :         "github.com/kijimaD/ruins/internal/raw"
      11              :         "github.com/kijimaD/ruins/internal/rawschema"
      12              :         w "github.com/kijimaD/ruins/internal/world"
      13              :         "github.com/kijimaD/ruins/internal/world/lifecycle"
      14              :         "github.com/kijimaD/ruins/internal/world/query"
      15              :         "github.com/mlange-42/ark/ecs"
      16              : )
      17              : 
      18              : // クラフトのエラー。呼び出し側とテストが errors.Is で同定できるよう sentinel にする。
      19              : var (
      20              :         errRecipeNotFound        = errors.New("recipe not found")
      21              :         errInsufficientMaterials = errors.New("insufficient materials")
      22              : )
      23              : 
      24              : // Craft はアイテムをクラフトする
      25              : func Craft(world w.World, name string) (ecs.Entity, error) {
      26            1 :         canCraft, err := CanCraft(world, name)
      27            1 :         if err != nil {
      28            1 :                 return gc.InvalidEntity, err
      29            1 :         }
      30            1 :         if !canCraft {
      31            1 :                 return gc.InvalidEntity, errInsufficientMaterials
      32            1 :         }
      33              : 
      34            1 :         craftCostPct, smithQualityPct := playerCraftMods(world)
      35            1 : 
      36              :         // 完成品を生成する前に素材を消費する。生成が先だと、CraftCost が100%を超えて消費が
      37              :         // 失敗したとき完成品だけが手元に残る。CanCraft と同じ実消費量を先に引くことで整合させる
      38            1 :         if err := consumeMaterials(world, name, craftCostPct); err != nil {
      39            0 :                 return gc.InvalidEntity, fmt.Errorf("failed to consume materials: %w", err)
      40            0 :         }
      41              : 
      42            1 :         resultEntity, err := lifecycle.SpawnBackpackItem(world, name, 1)
      43            1 :         if err != nil {
      44            0 :                 return gc.InvalidEntity, fmt.Errorf("failed to generate item: %w", err)
      45            0 :         }
      46            1 :         randomize(world, resultEntity, smithQualityPct)
      47            1 : 
      48            1 :         return resultEntity, nil
      49              : }
      50              : 
      51              : // CanCraft は所持数と実消費量を比較してクラフト可能か判定する。実消費量は CraftCost 倍率込みで、
      52              : // consumeMaterials と同じ式にして判定と消費のズレをなくす
      53              : func CanCraft(world w.World, name string) (bool, error) {
      54            1 :         required := requiredMaterials(world, name)
      55            1 :         if len(required) == 0 {
      56            1 :                 return false, fmt.Errorf("%w: %s", errRecipeNotFound, name)
      57            1 :         }
      58              : 
      59            1 :         craftCostPct, _ := playerCraftMods(world)
      60            1 :         for _, recipeInput := range required {
      61            1 :                 entity, found := query.FindStackInInventory(world, recipeInput.ID)
      62            1 :                 if !found {
      63            1 :                         return false, nil
      64            1 :                 }
      65            1 :                 count := query.GetEntityCount(world, entity)
      66            1 :                 if count < craftConsumedAmount(craftCostPct, recipeInput.Amount) {
      67            0 :                         return false, nil
      68            0 :                 }
      69              :         }
      70              : 
      71            1 :         return true, nil
      72              : }
      73              : 
      74              : // playerCraftMods はプレイヤーのクラフト倍率を返す。プレイヤーが無ければ基準値
      75              : func playerCraftMods(world w.World) (craftCost, smithQuality consts.Percent) {
      76            1 :         craftCost, smithQuality = consts.PercentBase, consts.PercentBase
      77            1 :         player, err := query.GetPlayerEntity(world)
      78            1 :         if err == nil {
      79            1 :                 craftCost = query.ProficiencyValue(world, player, rawschema.ProficiencyIdCraftCost)
      80            1 :                 smithQuality = query.ProficiencyValue(world, player, rawschema.ProficiencyIdSmithQuality)
      81            1 :         }
      82            1 :         return craftCost, smithQuality
      83              : }
      84              : 
      85              : // craftConsumedAmount は CraftCost 倍率を掛けた実消費量。最低でも1は消費する
      86              : func craftConsumedAmount(craftCostPct consts.Percent, amount int) int {
      87            1 :         return max(craftCostPct.ApplyInt(amount), 1)
      88            1 : }
      89              : 
      90              : // consumeMaterials はアイテムクラフトに必要な素材を消費する。
      91              : // craftCostPctは素材消費量の倍率%で、100が基準。低いほど素材が節約できる。
      92              : func consumeMaterials(world w.World, goal string, craftCostPct consts.Percent) error {
      93            1 :         for _, recipeInput := range requiredMaterials(world, goal) {
      94            1 :                 consumed := craftConsumedAmount(craftCostPct, recipeInput.Amount)
      95            1 :                 err := lifecycle.ChangeStackCount(world, recipeInput.ID, -consumed)
      96            1 :                 if err != nil {
      97            0 :                         return err
      98            0 :                 }
      99              :         }
     100            1 :         return nil
     101              : }
     102              : 
     103              : // requiredMaterials は指定したレシピに必要な素材一覧
     104              : func requiredMaterials(world w.World, need string) []gc.RecipeInput {
     105            1 :         rawMaster := world.Resources.RawMaster
     106            1 : 
     107            1 :         spec, err := raw.NewRecipeSpec(rawMaster, need)
     108            1 :         if err != nil {
     109            1 :                 return []gc.RecipeInput{}
     110            1 :         }
     111              : 
     112            1 :         if spec.Recipe == nil {
     113            0 :                 return []gc.RecipeInput{}
     114            0 :         }
     115              : 
     116            1 :         return spec.Recipe.Inputs
     117              : }
     118              : 
     119              : // randomize はアイテムにランダム値を設定する。
     120              : // smithQualityPctは品質倍率%で、100が基準。高いほどボーナスが大きくなる。
     121              : func randomize(world w.World, entity ecs.Entity, smithQualityPct consts.Percent) {
     122              :         // 基準からの乖離を10%刻みでボーナス段階に換算する。倍率そのものでなく段数なので int で扱う
     123            1 :         qualityBonus := (int(smithQualityPct) - int(consts.PercentBase)) / 10
     124            1 : 
     125            1 :         if world.Components.Melee.Has(entity) {
     126            1 :                 melee := world.Components.Melee.Get(entity)
     127            1 :                 melee.Accuracy += (-10 + rand.IntN(20)) + qualityBonus
     128            1 :                 melee.Damage += (-5 + rand.IntN(15)) + qualityBonus
     129            1 :         }
     130            1 :         if world.Components.Fire.Has(entity) {
     131            1 :                 fire := world.Components.Fire.Get(entity)
     132            1 :                 fire.Accuracy += (-10 + rand.IntN(20)) + qualityBonus
     133            1 :                 fire.Damage += (-5 + rand.IntN(15)) + qualityBonus
     134            1 :         }
     135            1 :         if world.Components.Wearable.Has(entity) {
     136            1 :                 wearable := world.Components.Wearable.Get(entity)
     137            1 :                 wearable.Defense += (-4 + rand.IntN(20)) + qualityBonus
     138            1 :         }
     139              : }
        

Generated by: LCOV version 2.0-1