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 : // PickupBehavior はBehaviorの実装
18 : type PickupBehavior struct{}
19 :
20 : // Info はBehaviorの実装
21 : func (pb *PickupBehavior) Info() Info {
22 1 : return Info{
23 1 : Name: "Pick Up",
24 1 : Description: "Pick up an item",
25 1 : Interruptible: false,
26 1 : Resumable: false,
27 1 : ActionPointCost: consts.MinorActionCost,
28 1 : TotalRequiredAP: 0,
29 1 : }
30 : }
31 :
32 : // Name はBehaviorの実装
33 : func (pb *PickupBehavior) Name() gc.BehaviorName {
34 1 : return gc.BehaviorPickup
35 1 : }
36 :
37 : // NewPickupTileActivity は指定タイル上の拾得可能なエンティティを全部拾う拾得アクティビティを組む。
38 : // 何を拾うかはここで解決して Targets に確定させる。behavior はタイル走査を持たない。
39 : func NewPickupTileActivity(world w.World, tile consts.Coord[consts.Tile]) *gc.Activity {
40 1 : comp := NewActivity(gc.BehaviorPickup, 0)
41 1 : comp.Params = &gc.PickupParams{Targets: query.PickablesAt(world, tile)}
42 1 : return comp
43 1 : }
44 :
45 : // NewPickupStackActivity は target と同一スタックのエンティティをまとめて拾う拾得アクティビティを組む。
46 : // 一覧の1行はスタック代表なので、代表と束ねられた全個体を Targets に確定させ、表示の個数と拾う個数を揃える。
47 : func NewPickupStackActivity(world w.World, target ecs.Entity) *gc.Activity {
48 1 : comp := NewActivity(gc.BehaviorPickup, 0)
49 1 : comp.Params = &gc.PickupParams{Targets: query.StackMembers(world, target)}
50 1 : return comp
51 1 : }
52 :
53 : // Validate はアイテム拾得アクティビティの検証を行う。1つでも拾えるものがあれば有効。
54 : func (pb *PickupBehavior) Validate(comp *gc.Activity, _ ecs.Entity, world w.World) error {
55 1 : p, ok := comp.Params.(*gc.PickupParams)
56 1 : if !ok {
57 1 : return ErrParamsTypeMismatch
58 1 : }
59 1 : for _, entity := range p.Targets {
60 1 : if query.IsPickable(entity, world) {
61 1 : return nil
62 1 : }
63 : }
64 1 : return &UserError{Msg: query.T(world, "nothing to pick up")}
65 : }
66 :
67 : // Start はアイテム拾得開始時の処理を実行する
68 : func (pb *PickupBehavior) Start(_ *gc.Activity, actor ecs.Entity, _ w.World) error {
69 1 : log.Debug("item pickup started", "actor", actor)
70 1 : return nil
71 1 : }
72 :
73 : // DoTurn はアイテム拾得アクティビティの1ターン分の処理を実行する
74 : func (pb *PickupBehavior) DoTurn(comp *gc.Activity, actor ecs.Entity, world w.World) error {
75 : // アイテム拾得処理を実行
76 1 : if err := pb.performPickup(comp, actor, world); err != nil {
77 1 : return err
78 1 : }
79 :
80 : // 拾得処理完了
81 1 : Complete(comp)
82 1 :
83 1 : return nil
84 : }
85 :
86 : // Finish はアイテム拾得完了時の処理を実行する
87 : func (pb *PickupBehavior) Finish(_ *gc.Activity, actor ecs.Entity, _ w.World) error {
88 1 : log.Debug("item pickup activity finished", "actor", actor)
89 1 : return nil
90 1 : }
91 :
92 : // Canceled はアイテム拾得キャンセル時の処理を実行する
93 : func (pb *PickupBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
94 1 : log.Debug("item pickup canceled", "actor", actor, "reason", comp.CancelReason)
95 1 : return nil
96 1 : }
97 :
98 : // performPickup は Params に確定済みの Targets を拾う。同一スタックは1行にまとめてログし、
99 : // 表示の個数と実際に拾った個数を揃える。拾得不能になったものは飛ばす。
100 : func (pb *PickupBehavior) performPickup(comp *gc.Activity, actor ecs.Entity, world w.World) error {
101 1 : p, ok := comp.Params.(*gc.PickupParams)
102 1 : if !ok {
103 1 : return ErrParamsTypeMismatch
104 1 : }
105 :
106 : // 構築後に拾えなくなったものは除く。着手時に他の拾得や消滅で状態が変わりうる
107 1 : var pickable []ecs.Entity
108 1 : for _, entity := range p.Targets {
109 1 : if query.IsPickable(entity, world) {
110 1 : pickable = append(pickable, entity)
111 1 : }
112 : }
113 1 : if len(pickable) == 0 {
114 1 : return fmt.Errorf("nothing to pick up")
115 1 : }
116 :
117 1 : actorName := query.GetEntityName(actor, world)
118 1 : total := 0
119 1 : var errs []error
120 1 : for _, stack := range query.GroupStacks(world, pickable) {
121 : // 表示名と個数は移動前に確定する。移動後はスタックが割れて個数が変わるため先に数えておく
122 1 : formattedName := query.FormatItemName(world, stack.Rep)
123 1 : moved, err := lifecycle.MoveMembersToBackpack(world, stack.Members, actor)
124 1 : if err != nil {
125 0 : errs = append(errs, err)
126 0 : }
127 1 : if moved == 0 {
128 0 : continue
129 : }
130 1 : gamelog.New(query.GetGameLog(world)).
131 1 : Markup(query.T(world, "%s picked up %s.",
132 1 : query.NameMarkup(actor, actorName, world),
133 1 : gamelog.Tag("item", formattedName))).
134 1 : Log()
135 1 : total += moved
136 : }
137 :
138 1 : if total == 0 {
139 0 : if len(errs) > 0 {
140 0 : return fmt.Errorf("some pickups failed: %w", errors.Join(errs...))
141 0 : }
142 0 : return fmt.Errorf("nothing to pick up")
143 : }
144 :
145 1 : log.Debug("pickup finished", "count", total)
146 1 :
147 : // 漁り統計: 拾えた個数を run 統計へ加算する
148 1 : if s := query.GetRunStats(world); s != nil {
149 1 : s.ItemsScavenged += total
150 1 : }
151 :
152 1 : if len(errs) > 0 {
153 0 : return fmt.Errorf("some pickups failed: %w", errors.Join(errs...))
154 0 : }
155 :
156 1 : return nil
157 : }
|