BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

tiles.js 9.8 KiB Download Raw
1import {
2 ORE_BEARING_MATERIALS,
3 oreBoulderState,
4 materialFromOreBoulderState,
5 MATERIAL_DISPLAY_NAME,
6 WoodTier
7} from "./materialTiers.js";
8import { spriteImageName, resolveState } from "./tileSprites.js";
9import { isSeasonalStumpState, SeasonId } from "../world/season.js";
10
11export const GroundType = Object.freeze({
12 grass: 1,
13 wood: 2,
14 water: 3,
15 deepWater: 4
16});
17
18export const BlockType = Object.freeze({
19 stoneWall: 1,
20 torch: 2,
21 /** @deprecated Legacy authored block; migrated into chunk `spawners[]` on load. */
22 spawner: 3,
23 bank: 4,
24 churchAltar: 5,
25 cauldron: 6,
26 boulder: 7,
27 furnace: 8,
28 anvil: 9,
29 woodworkingBench: 10,
30 craftingBench: 11,
31 pineTree: 12,
32 marketplace: 13
33});
34
35export const GROUND_DEFINITIONS = Object.freeze({
36 [GroundType.grass]: Object.freeze({
37 id: GroundType.grass,
38 key: "grass",
39 displayName: "Grass",
40 passable: true,
41 wades: false,
42 water: false,
43 frames: 1,
44 framesPerRow: 1,
45 animationSpeed: 0,
46 /** Outdoor grounds should expose summer/fall/winter (legacy `grass` ok for summer). */
47 outdoor: true
48 }),
49 [GroundType.wood]: Object.freeze({
50 id: GroundType.wood,
51 key: "wood",
52 displayName: "Wood",
53 passable: true,
54 wades: false,
55 water: false,
56 frames: 1,
57 framesPerRow: 1,
58 animationSpeed: 0,
59 outdoor: false
60 }),
61 [GroundType.water]: Object.freeze({
62 id: GroundType.water,
63 key: "water",
64 displayName: "Water",
65 passable: true,
66 wades: true,
67 water: true,
68 frames: 4,
69 framesPerRow: 4,
70 animationSpeed: 0.12,
71 outdoor: true
72 }),
73 [GroundType.deepWater]: Object.freeze({
74 id: GroundType.deepWater,
75 key: "deep-water",
76 displayName: "Deep water",
77 passable: false,
78 wades: false,
79 water: true,
80 frames: 4,
81 framesPerRow: 4,
82 animationSpeed: 0.12,
83 outdoor: true
84 })
85});
86
87export const BLOCK_DEFINITIONS = Object.freeze({
88 [BlockType.stoneWall]: Object.freeze({
89 id: BlockType.stoneWall,
90 key: "stone-wall",
91 displayName: "Stone wall",
92 passable: false,
93 lightRangeTiles: 0,
94 frames: 1,
95 framesPerRow: 1,
96 animationSpeed: 0,
97 /** Building walls count as indoor (no seasonal art required). */
98 outdoor: false
99 }),
100 [BlockType.torch]: Object.freeze({
101 id: BlockType.torch,
102 key: "torch",
103 displayName: "Torch",
104 passable: true,
105 lightRangeTiles: 8,
106 frames: 1,
107 framesPerRow: 1,
108 animationSpeed: 0,
109 outdoor: false
110 }),
111 [BlockType.spawner]: Object.freeze({
112 id: BlockType.spawner,
113 key: "spawner",
114 displayName: "Spawner",
115 passable: true,
116 lightRangeTiles: 0,
117 frames: 1,
118 framesPerRow: 1,
119 animationSpeed: 0,
120 /** Not shown in the placeable block palette; use the Spawners tab. */
121 editorHidden: true,
122 outdoor: false
123 }),
124 [BlockType.bank]: Object.freeze({
125 id: BlockType.bank,
126 key: "bank",
127 displayName: "Bank",
128 passable: false,
129 lightRangeTiles: 0,
130 frames: 1,
131 framesPerRow: 1,
132 animationSpeed: 0,
133 outdoor: false
134 }),
135 [BlockType.churchAltar]: Object.freeze({
136 id: BlockType.churchAltar,
137 key: "church-altar",
138 displayName: "Church altar",
139 passable: false,
140 lightRangeTiles: 0,
141 frames: 1,
142 framesPerRow: 1,
143 animationSpeed: 0,
144 outdoor: false
145 }),
146 [BlockType.cauldron]: Object.freeze({
147 id: BlockType.cauldron,
148 key: "cauldron",
149 displayName: "Cauldron",
150 passable: false,
151 lightRangeTiles: 0,
152 frames: 1,
153 framesPerRow: 1,
154 animationSpeed: 0,
155 outdoor: false
156 }),
157 [BlockType.boulder]: Object.freeze({
158 id: BlockType.boulder,
159 key: "boulder",
160 displayName: "Boulder",
161 passable: false,
162 lightRangeTiles: 0,
163 frames: 1,
164 framesPerRow: 1,
165 animationSpeed: 0,
166 /** Ore states are synthesized in the sprite catalog (tinted boulder-ore). */
167 gatherable: true,
168 outdoor: true
169 }),
170 [BlockType.furnace]: Object.freeze({
171 id: BlockType.furnace,
172 key: "furnace",
173 displayName: "Furnace",
174 passable: false,
175 lightRangeTiles: 4,
176 frames: 1,
177 framesPerRow: 1,
178 animationSpeed: 0,
179 craftingStationId: "furnace",
180 outdoor: false
181 }),
182 [BlockType.anvil]: Object.freeze({
183 id: BlockType.anvil,
184 key: "anvil",
185 displayName: "Anvil",
186 passable: false,
187 lightRangeTiles: 0,
188 frames: 1,
189 framesPerRow: 1,
190 animationSpeed: 0,
191 craftingStationId: "anvil",
192 outdoor: false
193 }),
194 [BlockType.woodworkingBench]: Object.freeze({
195 id: BlockType.woodworkingBench,
196 key: "woodworking-bench",
197 displayName: "Woodworking bench",
198 passable: false,
199 lightRangeTiles: 0,
200 frames: 1,
201 framesPerRow: 1,
202 animationSpeed: 0,
203 craftingStationId: "woodworking-bench",
204 outdoor: false
205 }),
206 [BlockType.craftingBench]: Object.freeze({
207 id: BlockType.craftingBench,
208 key: "crafting-bench",
209 displayName: "Crafting bench",
210 passable: false,
211 lightRangeTiles: 0,
212 frames: 1,
213 framesPerRow: 1,
214 animationSpeed: 0,
215 craftingStationId: "crafting-bench",
216 outdoor: false
217 }),
218 [BlockType.pineTree]: Object.freeze({
219 id: BlockType.pineTree,
220 key: "pine-tree",
221 displayName: "Pine tree",
222 passable: false,
223 lightRangeTiles: 0,
224 frames: 1,
225 framesPerRow: 1,
226 animationSpeed: 0,
227 outdoor: true,
228 gatherable: true,
229 /** Wood tier yielded when chopped (axe). */
230 woodTier: WoodTier.pine
231 }),
232 [BlockType.marketplace]: Object.freeze({
233 id: BlockType.marketplace,
234 key: "marketplace",
235 displayName: "Central Exchange",
236 passable: false,
237 lightRangeTiles: 0,
238 frames: 1,
239 framesPerRow: 1,
240 animationSpeed: 0,
241 outdoor: false
242 })
243});
244
245/** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */
246export function listPlaceableGrounds ()
247{
248 return Object.values(GROUND_DEFINITIONS);
249}
250
251export function listPlaceableBlocks ()
252{
253 return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden);
254}
255
256/** Display name for an ore boulder state (`copper-ore` → "Copper ore boulder"). */
257export function oreBoulderDisplayName (state)
258{
259 const material = materialFromOreBoulderState(state);
260 if (!material)
261 { return null; }
262 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
263 return material === "coal" ? "Coal boulder" : `${label} ore boulder`;
264}
265
266/**
267 * Hover / UI label for a gatherable resource node.
268 * Ore boulders and chopable trees; fishing / herbs can extend this later.
269 * Returns null when the block is not an active resource node.
270 */
271export function resourceNodeDisplayName (block)
272{
273 if (!block)
274 { return null; }
275 if (block.typeId === BlockType.boulder)
276 { return oreBoulderDisplayName(block.state); }
277 if (isChopableTreeBlock(block))
278 {
279 const def = BLOCK_DEFINITIONS[block.typeId];
280 return def?.displayName ?? "Tree";
281 }
282 return null;
283}
284
285/** Wood tier for a tree block type, or null. */
286export function woodTierForTreeBlock (typeId)
287{
288 const def = BLOCK_DEFINITIONS[typeId];
289 return def?.woodTier ?? null;
290}
291
292/** True when this block is a standing (non-stump) gatherable tree. */
293export function isChopableTreeBlock (block)
294{
295 if (!block || woodTierForTreeBlock(block.typeId) == null)
296 { return false; }
297 return !isSeasonalStumpState(block.state);
298}
299
300/**
301 * Editor palette cards: one entry per placeable, with boulder expanded into
302 * plain rock + each ore/coal variant (shared BlockType, distinct state).
303 */
304export function listPlaceableBlockPaletteEntries (catalog = null)
305{
306 const entries = [];
307 for (const def of listPlaceableBlocks())
308 {
309 if (def.id === BlockType.boulder)
310 {
311 entries.push({
312 typeId: def.id,
313 state: "boulder",
314 key: def.key,
315 displayName: "Boulder",
316 spriteName: spriteImageName("block", def.key, "boulder")
317 });
318 for (const material of ORE_BEARING_MATERIALS)
319 {
320 const state = oreBoulderState(material);
321 entries.push({
322 typeId: def.id,
323 state,
324 key: def.key,
325 displayName: oreBoulderDisplayName(state) ?? state,
326 spriteName: spriteImageName("block", def.key, state)
327 });
328 }
329 continue;
330 }
331 const states = catalog?.blocks?.[def.key]?.states ?? [];
332 const state = resolveState(states, null);
333 entries.push({
334 typeId: def.id,
335 state,
336 key: def.key,
337 displayName: def.displayName,
338 spriteName: state
339 ? spriteImageName("block", def.key, state)
340 : spriteImageName("block", def.key, def.key)
341 });
342 }
343 return entries;
344}
345
346export function groundDefinition (typeId)
347{
348 return GROUND_DEFINITIONS[typeId] ?? null;
349}
350
351export function blockDefinition (typeId)
352{
353 return BLOCK_DEFINITIONS[typeId] ?? null;
354}
355
356export function isGroundPassable (typeId)
357{
358 const def = groundDefinition(typeId);
359 if (!def)
360 { return true; }
361 return def.passable !== false;
362}
363
364export function isWaterGround (typeId)
365{
366 return !!groundDefinition(typeId)?.water;
367}
368
369export function isWadeGround (typeId)
370{
371 return !!groundDefinition(typeId)?.wades;
372}
373
374/**
375 * Liquid water for foam / wade (shallow water freezes in winter and is solid ice).
376 * Deep water stays liquid year-round.
377 */
378export function isLiquidWaterGround (typeId, seasonId = null)
379{
380 if (!isWaterGround(typeId))
381 { return false; }
382 if (typeId === GroundType.water && seasonId === SeasonId.winter)
383 { return false; }
384 return true;
385}
386
387/** Wade clip/foam only on liquid shallow water (not winter ice). */
388export function isWadeGroundInSeason (typeId, seasonId = null)
389{
390 return isWadeGround(typeId) && isLiquidWaterGround(typeId, seasonId);
391}
392
393export function isBlockPassable (typeId)
394{
395 if (typeId == null)
396 { return true; }
397
398 const def = blockDefinition(typeId);
399 return def ? def.passable : false;
400}
401
402export function blockLightRange (typeId)
403{
404 const def = blockDefinition(typeId);
405 return def ? def.lightRangeTiles : 0;
406}
407
408export function normalizeGround (value, fallbackTypeId = GroundType.grass)
409{
410 if (typeof value === "number")
411 { return { typeId: value, state: null }; }
412 if (value && Number.isInteger(value.typeId))
413 { return { typeId: value.typeId, state: value.state ?? null }; }
414 return { typeId: fallbackTypeId, state: null };
415}
416
417export function normalizeBlock (value)
418{
419 if (!value || !Number.isInteger(value.typeId))
420 { return null; }
421 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
422}
423
424export function setBlockVisualState (block, state)
425{
426 if (!block)
427 { return null; }
428 block.state = state ?? null;
429 return block;
430}