Line data Source code
1 : package query
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : "github.com/kijimaD/ruins/internal/consts"
6 : w "github.com/kijimaD/ruins/internal/world"
7 : "github.com/mlange-42/ark/ecs"
8 : )
9 :
10 : // CubeTiles はキューブのタイル座標を集めて返す。マクロ地図のマーカー用。
11 : // 位置は GridElement から常に導けるので保持せず、読み取り時に集める。
12 : func CubeTiles(world w.World) []consts.Coord[consts.Tile] {
13 1 : var out []consts.Coord[consts.Tile]
14 1 : q := ActiveFilter2[gc.GridElement, gc.Cube](world).Query()
15 1 : for q.Next() {
16 1 : out = append(out, world.Components.GridElement.Get(q.Entity()).Coord)
17 1 : }
18 1 : return out
19 : }
20 :
21 : // DiscoveredChunks は探索済みタイルを含むチャンクの集合を絶対チャンク座標で返す。タイル単位の
22 : // フォグ ExploredTiles をチャンク粒度へ畳み込み、マクロ地図のフォグにする。StageField が無ければ nil。
23 : // 全画面の地形俯瞰図と HUD の右上地図が同じフォグを共有する。
24 : func DiscoveredChunks(world w.World, sb *gc.SeamlessBand) map[consts.Coord[consts.Chunk]]bool {
25 1 : field := GetCurrentStageField(world)
26 1 : if field == nil {
27 1 : return nil
28 1 : }
29 1 : out := make(map[consts.Coord[consts.Chunk]]bool)
30 1 : // ChunkW は帯が有効なら正なのでゼロ除算しない。X は有界なので絶対チャンク列はタイルを幅で割るだけ。
31 : // Y は AbsChunkRow が帯ローカルの非負タイル行を絶対チャンク行へ移す。単一出どころを共有する
32 1 : for tile := range field.ExploredTiles {
33 1 : col := consts.Chunk(int(tile.X) / int(sb.ChunkW))
34 1 : out[consts.Coord[consts.Chunk]{X: col, Y: sb.AbsChunkRow(tile.Y)}] = true
35 1 : }
36 1 : return out
37 : }
38 :
39 : // PlayerBandTile はプレイヤーの帯ローカルなタイル座標を返す。プレイヤーが居なければ ok=false。
40 : // マクロ地図のプレイヤーマーカーを置くのに使う。
41 : func PlayerBandTile(world w.World) (consts.Coord[consts.Tile], bool) {
42 1 : player, err := GetPlayerEntity(world)
43 1 : if err != nil || !world.Components.GridElement.Has(player) {
44 1 : return consts.Coord[consts.Tile]{}, false
45 1 : }
46 1 : return world.Components.GridElement.Get(player).Coord, true
47 : }
48 :
49 : // StowedCargo はキューブに畳み込んだ貨物の一覧を返す。LocationStowed で燃料タンクと別管理する。
50 : // 反復中に return するとロックが残るので、対象を集めてから返す。
51 : func StowedCargo(world w.World, cube ecs.Entity) []ecs.Entity {
52 1 : var items []ecs.Entity
53 1 : q := ecs.NewFilter1[gc.LocationStowed](world.ECS).Query()
54 1 : for q.Next() {
55 1 : e := q.Entity()
56 1 : if world.Components.LocationStowed.Get(e).Owner == cube {
57 1 : items = append(items, e)
58 1 : }
59 : }
60 1 : return items
61 : }
62 :
63 : // GetCubeModules はキューブに装着したモジュールの一覧を返す。LocationInstalled で Owner を辿り、
64 : // CubeModule も併せて絞るので、返る実体は性能コンポーネントを必ず持つ。読み取り側の Has チェックが要らない。
65 : // 反復中に return するとロックが残るので、対象を集めてから返す。
66 : func GetCubeModules(world w.World, cube ecs.Entity) []ecs.Entity {
67 1 : var items []ecs.Entity
68 1 : q := ecs.NewFilter2[gc.LocationInstalled, gc.CubeModule](world.ECS).Query()
69 1 : for q.Next() {
70 1 : e := q.Entity()
71 1 : if world.Components.LocationInstalled.Get(e).Owner == cube {
72 1 : items = append(items, e)
73 1 : }
74 : }
75 1 : return items
76 : }
77 :
78 : // BackpackCubeModules はプレイヤーのバックパックにあるキューブモジュールを返す。装着候補の一覧に使う。
79 : // 反復中に return するとロックが残るので、対象を集めてから返す。
80 : func BackpackCubeModules(world w.World, player ecs.Entity) []ecs.Entity {
81 1 : var items []ecs.Entity
82 1 : q := ecs.NewFilter2[gc.LocationInBackpack, gc.CubeModule](world.ECS).Query()
83 1 : for q.Next() {
84 1 : e := q.Entity()
85 1 : if world.Components.LocationInBackpack.Get(e).Owner == player {
86 1 : items = append(items, e)
87 1 : }
88 : }
89 1 : return items
90 : }
91 :
92 : // BackpackDeployables はプレイヤーのバックパックにある装着アイテムを返す。設備画面の装着候補に使う。
93 : // 反復中に return するとロックが残るので、対象を集めてから返す。
94 : func BackpackDeployables(world w.World, player ecs.Entity) []ecs.Entity {
95 1 : var items []ecs.Entity
96 1 : q := ecs.NewFilter2[gc.LocationInBackpack, gc.Deployable](world.ECS).Query()
97 1 : for q.Next() {
98 1 : e := q.Entity()
99 1 : if world.Components.LocationInBackpack.Get(e).Owner == player {
100 1 : items = append(items, e)
101 1 : }
102 : }
103 1 : return items
104 : }
105 :
106 : // FacilityAt は指定タイルのフィールドにある装着設備を返す。設備画面で撤去対象を引くのに使う。
107 : // フィールドに装着されている Deployable だけを対象にするので、grass など装着でない prop は撤去できない。
108 : // LocationOnField を明示して確認し、座標だけ残った別ロケーションの実体を誤って引かない。
109 : func FacilityAt(world w.World, coord consts.Coord[consts.Tile]) (ecs.Entity, bool) {
110 1 : for _, e := range GetEntitiesAt(world, coord.X, coord.Y) {
111 1 : if world.Components.Deployable.Has(e) && world.Components.LocationOnField.Has(e) {
112 1 : return e, true
113 1 : }
114 : }
115 1 : return gc.InvalidEntity, false
116 : }
117 :
118 : // CubeDeployRange はキューブの展開野営の縦横別の半径を返す。基準 consts.CubeDeployBaseRange に、
119 : // 装着した各モジュールの RangeBonus を縦横一律に加算する。範囲は保持せず読み取り時に導く。
120 : // 展開判定・畳み込み・レーザー壁描画がこの単一出典を参照する。
121 : func CubeDeployRange(world w.World, cube ecs.Entity) consts.Coord[consts.Tile] {
122 1 : r := consts.CubeDeployBaseRange()
123 1 : for _, m := range GetCubeModules(world, cube) {
124 1 : bonus := world.Components.CubeModule.Get(m).RangeBonus
125 1 : r.X += bonus
126 1 : r.Y += bonus
127 1 : }
128 1 : return r
129 : }
130 :
131 : // CubeFuelCapacity は基準容量に装着中モジュールの FuelCapacityBonus を足した燃料タンクの容量を返す。
132 : func CubeFuelCapacity(world w.World, cube ecs.Entity) consts.Heat {
133 1 : c := consts.CubeFuelCapacityBase
134 1 : for _, m := range GetCubeModules(world, cube) {
135 1 : c += world.Components.CubeModule.Get(m).FuelCapacityBonus
136 1 : }
137 1 : return c
138 : }
139 :
140 : // CubeWeight はキューブが積む物の総重量を返す。運転1タイルの燃料コスト算出に使う。
141 : // 燃料タンクの中身・畳み込んだ貨物・装着モジュールが機動に効くので合算する。値は保持せず読み取り時に導く。
142 : func CubeWeight(world w.World, cube ecs.Entity) consts.Milligram {
143 1 : var total consts.Milligram
144 1 : for _, item := range GetStorageItems(world, cube) {
145 1 : total += GetEntityWeight(world, item)
146 1 : }
147 1 : for _, item := range StowedCargo(world, cube) {
148 1 : total += GetEntityWeight(world, item)
149 1 : }
150 1 : for _, item := range GetCubeModules(world, cube) {
151 1 : total += GetEntityWeight(world, item)
152 1 : }
153 1 : return total
154 : }
155 :
156 : // FollowFuelCost は総重量から1歩随伴するのに要する燃料ポイントを返す。空でも基準量がかかり、
157 : // 総重量に比例して増える。重いほど燃費が悪化する。
158 : func FollowFuelCost(total consts.Milligram) consts.Heat {
159 1 : kg := int(total / consts.MilligramPerKg)
160 1 : return consts.Heat(consts.FollowFuelBase + consts.FollowFuelPerKg*kg)
161 1 : }
162 :
163 : // CubeFuelTotal はキューブに貯めた燃料ポイントを返す。FuelTank を持たなければ0。
164 : func CubeFuelTotal(world w.World, cube ecs.Entity) consts.Heat {
165 1 : if !world.Components.FuelTank.Has(cube) {
166 1 : return 0
167 1 : }
168 1 : return world.Components.FuelTank.Get(cube).Current
169 : }
170 :
171 : // FuelGaugeRatio は燃料ゲージの充填率を現在値と上限の比で返す。上限が0以下なら0。
172 : func FuelGaugeRatio(tank gc.FuelTank) float64 {
173 1 : if tank.Max <= 0 {
174 1 : return 0
175 1 : }
176 1 : return float64(tank.Current) / float64(tank.Max)
177 : }
178 :
179 : // ActiveCube は現ステージにいるキューブを返す。ダンジョンなどキューブのいないステージでは false。
180 : func ActiveCube(world w.World) (ecs.Entity, bool) {
181 1 : q := ActiveFilter1[gc.Cube](world).Query()
182 1 : if !q.Next() {
183 1 : return gc.InvalidEntity, false
184 1 : }
185 1 : cube := q.Entity()
186 1 : q.Close()
187 1 : return cube, true
188 : }
|