Line data Source code
1 : package activity
2 :
3 : import (
4 : "fmt"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/gamelog"
9 : w "github.com/kijimaD/ruins/internal/world"
10 : "github.com/kijimaD/ruins/internal/world/lifecycle"
11 : "github.com/kijimaD/ruins/internal/world/query"
12 : "github.com/mlange-42/ark/ecs"
13 : )
14 :
15 : // DisassembleBehavior は所持品のアイテムを材質の素材に戻すアクティビティの実装
16 : type DisassembleBehavior struct{}
17 :
18 : // Info はBehaviorの実装
19 : func (b *DisassembleBehavior) Info() Info {
20 0 : return Info{
21 0 : Name: "Disassemble",
22 0 : Description: "Break an item down into materials",
23 0 : Interruptible: true,
24 0 : Resumable: false,
25 0 : ActionPointCost: consts.StandardActionCost,
26 0 : }
27 : }
28 :
29 : // Name はBehaviorの実装
30 : func (b *DisassembleBehavior) Name() gc.BehaviorName {
31 0 : return gc.BehaviorDisassemble
32 0 : }
33 :
34 : // NewDisassembleActivity は分解するアイテムを指定して分解アクティビティを組む。
35 : // 重さ1kgごとに1ターンかかり、最低1ターンにする
36 : func NewDisassembleActivity(target ecs.Entity, world w.World) *gc.Activity {
37 1 : turns := 1
38 1 : if world.ECS.Alive(target) && world.Components.Weight.Has(target) {
39 1 : turns = max(1, int(world.Components.Weight.Get(target).Milligram/consts.MilligramPerKg))
40 1 : }
41 1 : comp := NewActivity(gc.BehaviorDisassemble, turns)
42 1 : comp.Params = &gc.DisassembleParams{Target: target}
43 1 : return comp
44 : }
45 :
46 : // inBackpackOf は item が actor の所持品にあるかを返す
47 : func inBackpackOf(world w.World, item, actor ecs.Entity) bool {
48 1 : return world.ECS.Alive(item) &&
49 1 : world.Components.LocationInBackpack.Has(item) &&
50 1 : world.Components.LocationInBackpack.Get(item).Owner == actor
51 1 : }
52 :
53 : // Validate は分解の検証を行う
54 : func (b *DisassembleBehavior) Validate(comp *gc.Activity, actor ecs.Entity, world w.World) error {
55 1 : p, ok := comp.Params.(*gc.DisassembleParams)
56 1 : if !ok {
57 0 : return ErrParamsTypeMismatch
58 0 : }
59 1 : if !inBackpackOf(world, p.Target, actor) {
60 1 : return &UserError{Msg: query.T(world, "the item is not in your inventory")}
61 1 : }
62 1 : if _, _, ok := query.ItemDisassemblyYield(world, p.Target); !ok {
63 1 : return &UserError{Msg: query.T(world, "this item cannot be disassembled")}
64 1 : }
65 1 : if !query.IsAreaSafe(world, actor) {
66 0 : return &UserError{Msg: query.T(world, "cannot disassemble because enemies are nearby")}
67 0 : }
68 1 : return nil
69 : }
70 :
71 : // Start は分解開始時の処理を実行する
72 : func (b *DisassembleBehavior) Start(comp *gc.Activity, _ ecs.Entity, world w.World) error {
73 1 : p, ok := comp.Params.(*gc.DisassembleParams)
74 1 : if !ok {
75 0 : return ErrParamsTypeMismatch
76 0 : }
77 1 : gamelog.New(query.GetGameLog(world)).
78 1 : Markup(query.T(world, "started disassembling %s", query.GetEntityName(p.Target, world))).
79 1 : Log()
80 1 : return nil
81 : }
82 :
83 : // DoTurn は分解の1ターン分の処理を実行する
84 : func (b *DisassembleBehavior) DoTurn(comp *gc.Activity, actor ecs.Entity, world w.World) error {
85 1 : p, ok := comp.Params.(*gc.DisassembleParams)
86 1 : if !ok {
87 0 : return ErrParamsTypeMismatch
88 0 : }
89 1 : if !inBackpackOf(world, p.Target, actor) {
90 1 : Cancel(comp, "interrupted because the item is gone")
91 1 : return nil
92 1 : }
93 1 : if !query.IsAreaSafe(world, actor) {
94 1 : Cancel(comp, "disassembly interrupted because enemies are nearby")
95 1 : return nil
96 1 : }
97 1 : comp.Progress.Current++
98 1 : if comp.Progress.Current >= comp.Progress.Max {
99 1 : Complete(comp)
100 1 : }
101 1 : return nil
102 : }
103 :
104 : // Finish はアイテムを消し、戻る素材を所持品に足す
105 : func (b *DisassembleBehavior) Finish(comp *gc.Activity, actor ecs.Entity, world w.World) error {
106 1 : p, ok := comp.Params.(*gc.DisassembleParams)
107 1 : if !ok {
108 0 : return ErrParamsTypeMismatch
109 0 : }
110 1 : if !inBackpackOf(world, p.Target, actor) {
111 0 : return nil
112 0 : }
113 1 : id, count, ok := query.ItemDisassemblyYield(world, p.Target)
114 1 : if !ok {
115 0 : return nil
116 0 : }
117 1 : if err := lifecycle.ChangeItemCount(world, p.Target, -1); err != nil {
118 0 : return fmt.Errorf("failed to consume disassembled item: %w", err)
119 0 : }
120 1 : material, err := lifecycle.SpawnBackpackItem(world, id, count)
121 1 : if err != nil {
122 0 : return fmt.Errorf("failed to spawn disassembled material: %w", err)
123 0 : }
124 1 : gamelog.New(query.GetGameLog(world)).
125 1 : Markup(query.T(world, "disassembled into %s x%d", query.GetEntityName(material, world), count)).
126 1 : Log()
127 1 : return nil
128 : }
129 :
130 : // Canceled は分解キャンセル時の処理を実行する。対象が消えたことによる中断もあるため、名前は対象が残っている場合だけ出す
131 : func (b *DisassembleBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, world w.World) error {
132 1 : if !world.Components.Player.Has(actor) {
133 0 : return nil
134 0 : }
135 1 : message := query.T(world, "interrupted disassembling")
136 1 : if p, ok := comp.Params.(*gc.DisassembleParams); ok && world.ECS.Alive(p.Target) {
137 1 : message = query.T(world, "interrupted disassembling %s", query.GetEntityName(p.Target, world))
138 1 : }
139 1 : gamelog.New(query.GetGameLog(world)).
140 1 : Markup(message).
141 1 : Log()
142 1 : return nil
143 : }
|