BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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