Line data Source code
1 : package query
2 :
3 : import (
4 : "fmt"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : "github.com/kijimaD/ruins/internal/rawschema"
8 : w "github.com/kijimaD/ruins/internal/world"
9 : "github.com/mlange-42/ark/ecs"
10 : )
11 :
12 : // StackKey はアイテムのスタック同一性キー。キーが等しいアイテムだけが同一スタックに束ねられる。
13 : // スタック数はこのキーで束ねた同値クラスの大きさとして導出し、保存しない。
14 : // 個体差を足すときはキーに項を1つ加える。鮮度のような連続量は段階へ量子化すれば束ね、
15 : // 生値のまま持てば個体ごとに分かれる。
16 : type StackKey struct {
17 : RawID string // 生成元の同定キー。まず品種が一致すること
18 : FreshnessStage rawschema.FreshnessStageId // 腐敗品の鮮度段階。非腐敗品は空文字
19 : Equip string // 装備の個体差の指紋。性能の違う同名装備を束ねない。非装備は空文字
20 : }
21 :
22 : // StackKeyOf は entity のスタック同一性キーを返す。スタック同一判定の唯一の権威。
23 : // RawID に加え、腐敗品なら現在の鮮度段階を、装備なら性能の指紋を含める。
24 : // RawID を持たない実体はスタックになりえないので ok=false を返す。raw 定義を経ない
25 : // 実行時生成の実体、死亡フェードアウトのエフェクト等がこれにあたる
26 : func StackKeyOf(world w.World, entity ecs.Entity) (StackKey, bool) {
27 1 : if !world.Components.RawID.Has(entity) {
28 1 : return StackKey{}, false
29 1 : }
30 1 : key := StackKey{RawID: world.Components.RawID.Get(entity).ID}
31 1 : if stage, ok := FreshnessStageOf(world, entity); ok {
32 1 : key.FreshnessStage = stage
33 1 : }
34 1 : key.Equip = equipFingerprint(world, entity)
35 1 : return key, true
36 : }
37 :
38 : // equipFingerprint は装備の性能を1つの文字列に畳む。クラフトの乱数化で同名でも性能が
39 : // ばらつくため、生値をそのまま指紋にして性能の違う個体を別スタックに分ける。
40 : // 完全一致の個体だけが束ねられる。非装備は空文字で、指紋は同一性に影響しない
41 : func equipFingerprint(world w.World, entity ecs.Entity) string {
42 1 : var fp string
43 1 : if world.Components.Melee.Has(entity) {
44 1 : fp += fmt.Sprintf("m%+v", *world.Components.Melee.Get(entity))
45 1 : }
46 1 : if world.Components.Fire.Has(entity) {
47 1 : fp += fmt.Sprintf("f%+v", *world.Components.Fire.Get(entity))
48 1 : }
49 1 : if world.Components.Wearable.Has(entity) {
50 1 : fp += fmt.Sprintf("w%+v", *world.Components.Wearable.Get(entity))
51 1 : }
52 1 : return fp
53 : }
54 :
55 : // SameStack は a と b が同一スタックに束ねられるかを返す。StackKey の等価そのもの。
56 : // スタックになりえない実体は自分自身とも束ねられない。
57 : func SameStack(world w.World, a ecs.Entity, b ecs.Entity) bool {
58 1 : ka, oka := StackKeyOf(world, a)
59 1 : kb, okb := StackKeyOf(world, b)
60 1 : return oka && okb && ka == kb
61 1 : }
62 :
63 : // StackCountOf は candidates のうち entity と同一スタックのものを数える。スタック数は保存せず
64 : // この数え上げで導出する。数える範囲は呼び出し側が候補集合として渡す。リロードやクラフトは
65 : // バックパックの中を、総重量は装備や収納も含む所有全体を候補にする、というように範囲を選ぶ。
66 : // スタックになりえない実体は常に1になる。
67 : func StackCountOf(world w.World, entity ecs.Entity, candidates []ecs.Entity) int {
68 1 : key, ok := StackKeyOf(world, entity)
69 1 : if !ok {
70 1 : return 1
71 1 : }
72 1 : n := 0
73 1 : for _, c := range candidates {
74 1 : if ck, cok := StackKeyOf(world, c); cok && ck == key {
75 1 : n++
76 1 : }
77 : }
78 1 : return n
79 : }
80 :
81 : // Stack は同一スタックに束ねたアイテム群。表示や一括操作の単位に使う。
82 : type Stack struct {
83 : Rep ecs.Entity // 束の代表。入力での初出エンティティ
84 : Count int // 束に属する個数
85 : Members []ecs.Entity // 束に属する全エンティティ。初出順
86 : }
87 :
88 : // BackpackStacks は owner のバックパックの中身を、表示順に並べたスタックの列で返す。
89 : // 収集・並べ替え・束ねをこの1呼び出しに集約し、一覧はこの結果をそのまま行にする。
90 : // 生のエンティティ列挙から一覧を組むと、束ね忘れや並べ替え忘れが呼び出し側ごとに起きる
91 : func BackpackStacks(world w.World, owner ecs.Entity) []Stack {
92 1 : var items []ecs.Entity
93 1 : q := ecs.NewFilter1[gc.LocationInBackpack](world.ECS).Query()
94 1 : for q.Next() {
95 1 : entity := q.Entity()
96 1 : if world.Components.LocationInBackpack.Get(entity).Owner == owner {
97 1 : items = append(items, entity)
98 1 : }
99 : }
100 1 : return GroupStacks(world, SortEntities(world, items))
101 : }
102 :
103 : // StorageStacks は storage の中身を、表示順に並べたスタックの列で返す。BackpackStacks と対
104 : func StorageStacks(world w.World, storage ecs.Entity) []Stack {
105 1 : return GroupStacks(world, SortEntities(world, GetStorageItems(world, storage)))
106 1 : }
107 :
108 : // StackMembers は entity と同じ所有者かつ同じ位置種別にある、同一スタックのエンティティを返す。
109 : // 個数の数え上げ、一括消費、表示の束ねの範囲をこの1関数に集約する。バックパックと収納は
110 : // 所有者一致で絞る。装備やフィールド、位置未設定、スタックになりえない実体は entity 単独を返す。
111 : func StackMembers(world w.World, entity ecs.Entity) []ecs.Entity {
112 1 : key, ok := StackKeyOf(world, entity)
113 1 : if !ok {
114 1 : return []ecs.Entity{entity}
115 1 : }
116 1 : switch {
117 : case world.Components.LocationInBackpack.Has(entity):
118 1 : owner := world.Components.LocationInBackpack.Get(entity).Owner
119 1 : return sameStackInBackpack(world, owner, key)
120 : case world.Components.LocationInStorage.Has(entity):
121 1 : owner := world.Components.LocationInStorage.Get(entity).Owner
122 1 : return sameStackInStorage(world, owner, key)
123 : case world.Components.LocationOnField.Has(entity):
124 1 : if ge := world.Components.GridElement.Get(entity); ge != nil {
125 1 : return sameStackOnField(world, ge, key)
126 1 : }
127 1 : return []ecs.Entity{entity}
128 : default:
129 1 : return []ecs.Entity{entity}
130 : }
131 : }
132 :
133 : // sameStackOnField は床の同じタイルにある同一スタックのエンティティを返す。床アイテムは
134 : // 所有者を持たず位置で束ねるため、同座標かつ同キーのものを数える。
135 : func sameStackOnField(world w.World, grid *gc.GridElement, key StackKey) []ecs.Entity {
136 1 : var out []ecs.Entity
137 1 : q := ActiveFilter2[gc.LocationOnField, gc.GridElement](world).Query()
138 1 : for q.Next() {
139 1 : e := q.Entity()
140 1 : ge := world.Components.GridElement.Get(e)
141 1 : if ge.X != grid.X || ge.Y != grid.Y {
142 1 : continue
143 : }
144 1 : if ck, ok := StackKeyOf(world, e); ok && ck == key {
145 1 : out = append(out, e)
146 1 : }
147 : }
148 1 : return out
149 : }
150 :
151 : func sameStackInBackpack(world w.World, owner ecs.Entity, key StackKey) []ecs.Entity {
152 1 : var out []ecs.Entity
153 1 : q := ecs.NewFilter1[gc.LocationInBackpack](world.ECS).Query()
154 1 : for q.Next() {
155 1 : e := q.Entity()
156 1 : if world.Components.LocationInBackpack.Get(e).Owner != owner {
157 1 : continue
158 : }
159 1 : if ck, ok := StackKeyOf(world, e); ok && ck == key {
160 1 : out = append(out, e)
161 1 : }
162 : }
163 1 : return out
164 : }
165 :
166 : func sameStackInStorage(world w.World, owner ecs.Entity, key StackKey) []ecs.Entity {
167 1 : var out []ecs.Entity
168 1 : q := ecs.NewFilter1[gc.LocationInStorage](world.ECS).Query()
169 1 : for q.Next() {
170 1 : e := q.Entity()
171 1 : if world.Components.LocationInStorage.Get(e).Owner != owner {
172 1 : continue
173 : }
174 1 : if ck, ok := StackKeyOf(world, e); ok && ck == key {
175 1 : out = append(out, e)
176 1 : }
177 : }
178 1 : return out
179 : }
180 :
181 : // GroupStacks は entities を StackKey で束ね、各束の代表と個数を返す。
182 : // 束の並びは入力での初出順にして決定的にする。表示の一覧はこの束1つを1行にする。
183 : // スタックになりえない実体は束ねず、1個だけの束として並ぶ。
184 : func GroupStacks(world w.World, entities []ecs.Entity) []Stack {
185 1 : index := make(map[StackKey]int, len(entities))
186 1 : stacks := make([]Stack, 0, len(entities))
187 1 : for _, e := range entities {
188 1 : key, ok := StackKeyOf(world, e)
189 1 : if !ok {
190 1 : stacks = append(stacks, Stack{Rep: e, Count: 1, Members: []ecs.Entity{e}})
191 1 : continue
192 : }
193 1 : if i, ok := index[key]; ok {
194 1 : stacks[i].Count++
195 1 : stacks[i].Members = append(stacks[i].Members, e)
196 1 : continue
197 : }
198 1 : index[key] = len(stacks)
199 1 : stacks = append(stacks, Stack{Rep: e, Count: 1, Members: []ecs.Entity{e}})
200 : }
201 1 : return stacks
202 : }
|