LCOV - code coverage report
Current view: top level - activity - salvage.go Coverage Total Hit
Test: coverage.lcov Lines: 76.3 % 76 58
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package activity
       2              : 
       3              : import (
       4              :         "fmt"
       5              :         "slices"
       6              : 
       7              :         gc "github.com/kijimaD/ruins/internal/components"
       8              :         "github.com/kijimaD/ruins/internal/gamelog"
       9              :         "github.com/kijimaD/ruins/internal/raw"
      10              :         "github.com/kijimaD/ruins/internal/rawschema"
      11              :         "github.com/kijimaD/ruins/internal/skill"
      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              : // wieldedTool は actor が構えている武器が分解定義 def に合う道具ならそのグレードを返す
      19              : func wieldedTool(world w.World, actor ecs.Entity, def *rawschema.Disassembly) (int, bool) {
      20            1 :         if def.ToolCategory == nil || !world.Components.Player.Has(actor) {
      21            1 :                 return 0, false
      22            1 :         }
      23            1 :         weapon, ok, err := selectedWeapon(world, actor)
      24            1 :         if err != nil || !ok {
      25            0 :                 return 0, false
      26            0 :         }
      27            1 :         tool, ok := raw.FindTool(world.Resources.RawMaster, query.GetEntityID(weapon, world))
      28            1 :         if !ok {
      29            1 :                 return 0, false
      30            1 :         }
      31            1 :         if slices.Contains(tool.Categories, *def.ToolCategory) {
      32            1 :                 return tool.Grade, true
      33            1 :         }
      34            1 :         return 0, false
      35              : }
      36              : 
      37              : // toolDamageMultiplier は prop を殴るときの道具のダメージ倍率を返す。合う道具で 1+グレード、道具が必須で無ければ0、他は1
      38              : func toolDamageMultiplier(world w.World, actor, target ecs.Entity) int {
      39            1 :         if !world.Components.Prop.Has(target) {
      40            1 :                 return 1
      41            1 :         }
      42            1 :         def, ok := raw.FindDisassembly(world.Resources.RawMaster, query.GetEntityID(target, world))
      43            1 :         if !ok {
      44            1 :                 return 1
      45            1 :         }
      46            1 :         grade, ok := wieldedTool(world, actor, def)
      47            1 :         switch {
      48              :         case ok:
      49            1 :                 return 1 + grade
      50              :         case def.ToolRequired != nil && *def.ToolRequired:
      51            1 :                 return 0
      52              :         }
      53            1 :         return 1
      54              : }
      55              : 
      56              : // salvageRoll は壊したときの抽選を決める。合う道具を構えているか、道具の分類が無い定義なら全枠を出す
      57              : func salvageRoll(world w.World, actor ecs.Entity, def *rawschema.Disassembly) (grade int, full, withTool bool) {
      58            1 :         grade, withTool = wieldedTool(world, actor, def)
      59            1 :         return grade, withTool || def.ToolCategory == nil, withTool
      60            1 : }
      61              : 
      62              : // dropSalvage は actor が壊した prop の産出を足元へ落とす
      63              : func dropSalvage(world w.World, actor, target ecs.Entity) error {
      64            1 :         def, ok := raw.FindDisassembly(world.Resources.RawMaster, query.GetEntityID(target, world))
      65            1 :         if !ok || !world.Components.GridElement.Has(target) {
      66            0 :                 return nil
      67            0 :         }
      68            1 :         grade, full, withTool := salvageRoll(world, actor, def)
      69            1 :         var skillID rawschema.SkillId
      70            1 :         hasSkill := false
      71            1 :         if withTool {
      72            1 :                 skillID, hasSkill = world.Resources.SkillDefs.ToolSkills[*def.ToolCategory]
      73            1 :         }
      74            1 :         skillValue := 0
      75            1 :         if hasSkill {
      76            1 :                 skillValue = skillValueOf(actor, world, skillID)
      77            1 :         }
      78            1 :         stacks, err := lifecycle.RollDisassemblyYields(world.Resources.Config.RNG, def, skillValue, grade, full)
      79            1 :         if err != nil {
      80            0 :                 return fmt.Errorf("failed to roll salvage: %w", err)
      81            0 :         }
      82            1 :         coord := world.Components.GridElement.Get(target).Coord
      83            1 :         if err := lifecycle.SpawnDisassemblyYields(world, stacks, coord.X, coord.Y); err != nil {
      84            0 :                 return fmt.Errorf("failed to spawn salvage: %w", err)
      85            0 :         }
      86            1 :         if hasSkill {
      87            1 :                 gainSkillExp(actor, world, skillID)
      88            1 :         }
      89            1 :         return nil
      90              : }
      91              : 
      92              : // gainSkillExp は道具で prop を壊したときに、その道具の分類のスキルの経験値を与える
      93              : func gainSkillExp(actor ecs.Entity, world w.World, skillID rawschema.SkillId) {
      94            1 :         if !world.Components.Skills.Has(actor) {
      95            0 :                 return
      96            0 :         }
      97            1 :         s := world.Components.Skills.Get(actor).Get(skillID)
      98            1 : 
      99            1 :         abilityValue := 0
     100            1 :         if world.Components.Abilities.Has(actor) {
     101            1 :                 abilityValue = world.Components.Abilities.Get(actor).ValueOf(world.Resources.SkillDefs.Ability(skillID))
     102            1 :         }
     103              : 
     104            1 :         if skill.GainExp(s, abilityValue) {
     105              :                 // 同一ターン内の別処理が既にマーカーを付けていることがあるため、二重付与を避ける
     106            0 :                 if !world.Components.StatsChanged.Has(actor) {
     107            0 :                         world.Components.StatsChanged.Add(actor, &gc.StatsChanged{})
     108            0 :                 }
     109            0 :                 gamelog.New(query.GetGameLog(world)).
     110            0 :                         Markup(query.T(world, "%s skill rose to %d", world.Resources.SkillDefs.Name(skillID), s.Value)).
     111            0 :                         Log()
     112              :         }
     113              : }
     114              : 
     115              : // skillValueOf は actor のスキル skillID の値を返す。スキルを持たなければ0
     116              : func skillValueOf(actor ecs.Entity, world w.World, skillID rawschema.SkillId) int {
     117            1 :         if !world.Components.Skills.Has(actor) {
     118            0 :                 return 0
     119            0 :         }
     120            1 :         return world.Components.Skills.Get(actor).Level(skillID)
     121              : }
        

Generated by: LCOV version 2.0-1