Line data Source code
1 : package activity
2 :
3 : import (
4 : "errors"
5 : "fmt"
6 :
7 : gc "github.com/kijimaD/ruins/internal/components"
8 : "github.com/kijimaD/ruins/internal/consts"
9 : "github.com/kijimaD/ruins/internal/gamelog"
10 : w "github.com/kijimaD/ruins/internal/world"
11 :
12 : "github.com/kijimaD/ruins/internal/world/lifecycle"
13 : "github.com/kijimaD/ruins/internal/world/query"
14 : "github.com/mlange-42/ark/ecs"
15 : )
16 :
17 : // リロードシステムの定数
18 : const (
19 : BaseReloadEffort = 10 // 1ターンあたりの基本装填工数
20 : )
21 :
22 : // ReloadBehavior はリロードアクティビティの実装。
23 : // 継続処理は GetBehavior が毎回作るゼロ値インスタンスで回るため、
24 : // 蓄積した装填工数などの進捗はフィールドに持たず gc.Activity 側に持たせる。
25 : type ReloadBehavior struct{}
26 :
27 : // Info はBehaviorの実装
28 : func (rb *ReloadBehavior) Info() Info {
29 1 : return Info{
30 1 : Name: "Reload",
31 1 : Description: "Load ammunition into a weapon",
32 1 : Interruptible: true,
33 1 : Resumable: false,
34 1 : ActionPointCost: consts.StandardActionCost,
35 1 : }
36 : }
37 :
38 : // Name はBehaviorの実装
39 : func (rb *ReloadBehavior) Name() gc.BehaviorName {
40 1 : return gc.BehaviorReload
41 1 : }
42 :
43 : // NewReloadActivity は装填アクティビティを組む。必要総工数は装備中の遠距離武器から求める。
44 : // 遠距離武器が無ければ工数0で組み、遠距離武器なしの判定は Validate に委ねる
45 : func NewReloadActivity(actor ecs.Entity, world w.World) *gc.Activity {
46 1 : effort := 0
47 1 : if fire, _, err := getEquippedFire(actor, world); err == nil {
48 1 : effort = fire.ReloadEffort
49 1 : }
50 1 : return NewActivity(gc.BehaviorReload, effort)
51 : }
52 :
53 : // Validate はリロードの検証を行う
54 : func (rb *ReloadBehavior) Validate(_ *gc.Activity, actor ecs.Entity, world w.World) error {
55 : // 継続アクティビティ共通の周囲安全チェック。敵が近ければ装填を始めさせない
56 1 : if !query.IsAreaSafe(world, actor) {
57 1 : return &UserError{Msg: query.T(world, "cannot reload because enemies are nearby")}
58 1 : }
59 :
60 1 : fire, _, err := getEquippedFire(actor, world)
61 1 : if err != nil {
62 1 : if errors.Is(err, ErrShootNoFireWeapon) {
63 1 : return &UserError{Msg: query.T(world, "no ranged weapon equipped")}
64 1 : }
65 0 : return err
66 : }
67 :
68 1 : if fire.Magazine >= fire.MagazineSize {
69 1 : return &UserError{Msg: query.T(world, "reload is not needed")}
70 1 : }
71 :
72 : // 弾薬の在庫チェック
73 1 : if _, found := query.FindAmmoInInventory(world, fire.AmmoTag); !found {
74 1 : return &UserError{Msg: query.T(world, "no ammo")}
75 1 : }
76 :
77 1 : return nil
78 : }
79 :
80 : // Start はリロード開始時の処理
81 : func (rb *ReloadBehavior) Start(_ *gc.Activity, _ ecs.Entity, world w.World) error {
82 1 : gamelog.New(query.GetGameLog(world)).
83 1 : Markup(query.T(world, "started reloading")).
84 1 : Log()
85 1 : return nil
86 1 : }
87 :
88 : // DoTurn はリロードの1ターン分の処理
89 : func (rb *ReloadBehavior) DoTurn(comp *gc.Activity, actor ecs.Entity, world w.World) error {
90 : // 装填中に敵が接近したら中断する。他の継続系と同じ契約
91 1 : if !query.IsAreaSafe(world, actor) {
92 1 : Cancel(comp, "reload interrupted because enemies are nearby")
93 1 : return nil
94 1 : }
95 :
96 1 : fire, _, err := getEquippedFire(actor, world)
97 1 : if err != nil {
98 0 : return err
99 0 : }
100 :
101 : // 1ターンあたりの工数を計算。能力が高いほど速く装填する
102 1 : comp.Progress.Current += rb.calcEffortPerTurn(actor, fire, world)
103 1 :
104 : // 工数が目標に達したら装填完了
105 1 : if comp.Progress.Current >= comp.Progress.Max {
106 : // 装填数を計算(マガジン容量と弾薬在庫の小さい方)
107 1 : needed := fire.MagazineSize - fire.Magazine
108 1 : ammoEntity, found := query.FindAmmoInInventory(world, fire.AmmoTag)
109 1 : if !found {
110 0 : Cancel(comp, "ran out of ammo")
111 0 : return nil
112 0 : }
113 1 : ammoCount := query.GetEntityCount(world, ammoEntity)
114 1 :
115 1 : loaded := min(ammoCount, needed)
116 1 :
117 : // 装填した弾薬の修正値を記録する
118 1 : ammoComp := world.Components.Ammo.Get(ammoEntity)
119 1 : fire.LoadedDamageBonus = ammoComp.DamageBonus
120 1 : fire.LoadedAccuracyBonus = ammoComp.AccuracyBonus
121 1 :
122 1 : fire.Magazine += loaded
123 1 : if err := lifecycle.ChangeItemCount(world, ammoEntity, -loaded); err != nil {
124 0 : return fmt.Errorf("failed to consume ammo: %w", err)
125 0 : }
126 :
127 1 : gamelog.New(query.GetGameLog(world)).
128 1 : Markup(query.T(world, "reload complete (%d/%d)", fire.Magazine, fire.MagazineSize)).
129 1 : Log()
130 1 :
131 1 : Complete(comp)
132 1 : return nil
133 : }
134 :
135 1 : return nil
136 : }
137 :
138 : // Finish はリロード完了時の処理
139 : func (rb *ReloadBehavior) Finish(_ *gc.Activity, actor ecs.Entity, _ w.World) error {
140 1 : log.Debug("reload finished", "actor", actor)
141 1 : return nil
142 1 : }
143 :
144 : // Canceled はリロードキャンセル時の処理
145 : func (rb *ReloadBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, world w.World) error {
146 1 : gamelog.New(query.GetGameLog(world)).
147 1 : Markup(query.T(world, "interrupted reloading")).
148 1 : Log()
149 1 : log.Debug("reload canceled", "actor", actor, "reason", comp.CancelReason)
150 1 : return nil
151 1 : }
152 :
153 : // calcEffortPerTurn は1ターンあたりの装填工数を計算する
154 : func (rb *ReloadBehavior) calcEffortPerTurn(actor ecs.Entity, fire *gc.Fire, world w.World) int {
155 1 : effort := BaseReloadEffort
156 1 :
157 : // DEXを加算
158 1 : if world.Components.Abilities.Has(actor) {
159 1 : effort += world.Components.Abilities.Get(actor).Dexterity.Total
160 1 : }
161 :
162 : // 武器スキルレベルを加算
163 1 : if weapon, ok := world.Resources.SkillDefs.Weapon(fire.AttackCategory); ok {
164 1 : if world.Components.Skills.Has(actor) {
165 1 : effort += world.Components.Skills.Get(actor).Level(weapon.Skill)
166 1 : }
167 : }
168 :
169 1 : return effort
170 : }
|