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 : // FindStackInInventory は RawID でバックパック内のスタックアイテムを1つ返す。
13 : // 個数照会や消費の起点に使う。鮮度による束の分割は合流側の query.SameStack が担うため、
14 : // ここは鮮度を見ず RawID だけで引く。腐敗食は鮮度違いの別束が複数ありうる点に注意する。
15 : func FindStackInInventory(world w.World, id string) (ecs.Entity, bool) {
16 1 : q := ecs.NewFilter2[gc.LocationInBackpack, gc.RawID](world.ECS).Query()
17 1 : for q.Next() {
18 1 : entity := q.Entity()
19 1 : if world.Components.RawID.Get(entity).ID == id {
20 1 : q.Close()
21 1 : return entity, true
22 1 : }
23 : }
24 1 : return gc.InvalidEntity, false
25 : }
26 :
27 : // FindAmmoInInventory は口径タグでバックパック内の弾薬アイテムを検索する
28 : func FindAmmoInInventory(world w.World, ammoTag rawschema.AmmoTag) (ecs.Entity, bool) {
29 1 : q := ecs.NewFilter2[gc.LocationInBackpack, gc.Ammo](world.ECS).Query()
30 1 : for q.Next() {
31 1 : entity := q.Entity()
32 1 : if world.Components.Ammo.Get(entity).AmmoTag == ammoTag {
33 1 : q.Close()
34 1 : return entity, true
35 1 : }
36 : }
37 1 : return gc.InvalidEntity, false
38 : }
39 :
40 : // HoldsFireStarter は owner が火種の道具を所持しているかを返す。
41 : // バックパックと装備の両方を見る。着火の可否を所持の有無だけで判定する。消費はこの関数の関心ではない
42 : func HoldsFireStarter(world w.World, owner ecs.Entity) bool {
43 1 : backpack := ecs.NewFilter2[gc.LocationInBackpack, gc.FireStarter](world.ECS).Query()
44 1 : for backpack.Next() {
45 1 : if world.Components.LocationInBackpack.Get(backpack.Entity()).Owner == owner {
46 1 : backpack.Close()
47 1 : return true
48 1 : }
49 : }
50 1 : equipped := ecs.NewFilter2[gc.LocationEquipped, gc.FireStarter](world.ECS).Query()
51 1 : for equipped.Next() {
52 1 : if world.Components.LocationEquipped.Get(equipped.Entity()).Owner == owner {
53 1 : equipped.Close()
54 1 : return true
55 1 : }
56 : }
57 1 : return false
58 : }
59 :
60 : // GetEntityCount は entity が属するスタックの個数を返す。個数は保存せず、同じ所有者かつ
61 : // 同じ位置にある同一スタックのエンティティを数えて導出する。位置が無ければ1になる。
62 : func GetEntityCount(world w.World, entity ecs.Entity) int {
63 1 : return len(StackMembers(world, entity))
64 1 : }
65 :
66 : // FormatNameCount は個数が2以上のとき名前の前に個数を置く。1個や非スタックは名前だけを返す。
67 : // メニュー行とログの両方がこの1関数を通し、"個数 名前" の表記を揃える
68 : func FormatNameCount(name string, count int) string {
69 1 : if count > 1 {
70 1 : return fmt.Sprintf("%d %s", count, name)
71 1 : }
72 1 : return name
73 : }
74 :
75 : // FormatItemName はアイテムエンティティから名前と個数を取得してフォーマットする。
76 : // 名前はNameコンポーネントから取得し、見つからない場合は "Unknown Item" を返す。
77 : // 表記は FormatNameCount に委ね、個数が2以上のとき "個数 名前" になる
78 : func FormatItemName(world w.World, itemEntity ecs.Entity) string {
79 1 : name := T(world, "Unknown Item")
80 1 : if nameComp := world.Components.Name.Get(itemEntity); nameComp != nil {
81 1 : name = T(world, nameComp.Name)
82 1 : }
83 1 : return FormatNameCount(name, GetEntityCount(world, itemEntity))
84 : }
|