BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

rweaks
timfa Tim Falken · pushed by timfa · 2 months ago
2b34e2dd History
tiles.js 11.7 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`; catalog expands `${wood}-${mask}` with wood tints. */
265 neighborConnect: true,
266 neighborConnectByMaterial: true,
267 defaultNeighborMaterial: WoodTier.pine
268 })
269});
270
271/** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */
272export function listPlaceableGrounds ()
273{
274 return Object.values(GROUND_DEFINITIONS);
275}
276
277export function listPlaceableBlocks ()
278{
279 return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden);
280}
281
282/** Display name for an ore boulder state (`copper-ore` → "Copper ore boulder"). */
283export function oreBoulderDisplayName (state)
284{
285 const material = materialFromOreBoulderState(state);
286 if (!material)
287 { return null; }
288 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
289 return material === "coal" ? "Coal boulder" : `${label} ore boulder`;
290}
291
292/**
293 * Hover / UI label for a gatherable resource node.
294 * Ore boulders and chopable trees; fishing / herbs can extend this later.
295 * Returns null when the block is not an active resource node.
296 */
297export function resourceNodeDisplayName (block)
298{
299 if (!block)
300 { return null; }
301 if (block.typeId === BlockType.boulder)
302 { return oreBoulderDisplayName(block.state); }
303 if (isChopableTreeBlock(block))
304 {
305 const def = BLOCK_DEFINITIONS[block.typeId];
306 return def?.displayName ?? "Tree";
307 }
308 return null;
309}
310
311/** Wood tier for a tree block type, or null. */
312export function woodTierForTreeBlock (typeId)
313{
314 const def = BLOCK_DEFINITIONS[typeId];
315 return def?.woodTier ?? null;
316}
317
318/** True when this block is a standing (non-stump) gatherable tree. */
319export function isChopableTreeBlock (block)
320{
321 if (!block || woodTierForTreeBlock(block.typeId) == null)
322 { return false; }
323 return !isSeasonalStumpState(block.state);
324}
325
326/**
327 * Editor palette cards: one entry per placeable, with boulder expanded into
328 * plain rock + each ore/coal variant (shared BlockType, distinct state).
329 */
330export function listPlaceableBlockPaletteEntries (catalog = null)
331{
332 const entries = [];
333 for (const def of listPlaceableBlocks())
334 {
335 if (def.id === BlockType.boulder)
336 {
337 entries.push({
338 typeId: def.id,
339 state: "boulder",
340 key: def.key,
341 displayName: "Boulder",
342 spriteName: spriteImageName("block", def.key, "boulder")
343 });
344 for (const material of ORE_BEARING_MATERIALS)
345 {
346 const state = oreBoulderState(material);
347 entries.push({
348 typeId: def.id,
349 state,
350 key: def.key,
351 displayName: oreBoulderDisplayName(state) ?? state,
352 spriteName: spriteImageName("block", def.key, state)
353 });
354 }
355 continue;
356 }
357 if (def.neighborConnect)
358 {
359 if (definitionUsesMaterialNeighborConnect(def))
360 {
361 for (const material of WOOD_TIER_ORDER)
362 {
363 const state = composeNeighborConnectState(def, material, 0);
364 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
365 entries.push({
366 typeId: def.id,
367 state,
368 key: def.key,
369 displayName: `${label} table`,
370 spriteName: spriteImageName("block", def.key, state)
371 });
372 }
373 continue;
374 }
375 entries.push({
376 typeId: def.id,
377 state: "0",
378 key: def.key,
379 displayName: def.displayName,
380 spriteName: spriteImageName("block", def.key, "0")
381 });
382 continue;
383 }
384 const states = catalog?.blocks?.[def.key]?.states ?? [];
385 const state = resolveState(states, null);
386 entries.push({
387 typeId: def.id,
388 state,
389 key: def.key,
390 displayName: def.displayName,
391 spriteName: state
392 ? spriteImageName("block", def.key, state)
393 : spriteImageName("block", def.key, def.key)
394 });
395 }
396 return entries;
397}
398
399export function groundDefinition (typeId)
400{
401 return GROUND_DEFINITIONS[typeId] ?? null;
402}
403
404export function blockDefinition (typeId)
405{
406 return BLOCK_DEFINITIONS[typeId] ?? null;
407}
408
409export function isGroundPassable (typeId)
410{
411 const def = groundDefinition(typeId);
412 if (!def)
413 { return true; }
414 return def.passable !== false;
415}
416
417export function isWaterGround (typeId)
418{
419 return !!groundDefinition(typeId)?.water;
420}
421
422export function isWadeGround (typeId)
423{
424 return !!groundDefinition(typeId)?.wades;
425}
426
427/**
428 * Liquid water for foam / wade (shallow water freezes in winter and is solid ice).
429 * Deep water stays liquid year-round.
430 */
431export function isLiquidWaterGround (typeId, seasonId = null)
432{
433 if (!isWaterGround(typeId))
434 { return false; }
435 if (typeId === GroundType.water && seasonId === SeasonId.winter)
436 { return false; }
437 return true;
438}
439
440/** Wade clip/foam only on liquid shallow water (not winter ice). */
441export function isWadeGroundInSeason (typeId, seasonId = null)
442{
443 return isWadeGround(typeId) && isLiquidWaterGround(typeId, seasonId);
444}
445
446export function isBlockPassable (typeId)
447{
448 if (typeId == null)
449 { return true; }
450
451 const def = blockDefinition(typeId);
452 return def ? def.passable : false;
453}
454
455/**
456 * Whether a block occludes baked/dynamic light rays through its tile.
457 * Defaults to `!passable` when `blocksLight` is omitted (walls block, open floor does not).
458 * Set `blocksLight: false` on furniture (table, counter, anvil, …) to let light through
459 * while keeping `passable: false` for movement.
460 */
461export function isBlockOccludingLight (typeId)
462{
463 if (typeId == null)
464 { return false; }
465 const def = blockDefinition(typeId);
466 if (!def)
467 { return true; }
468 if (def.blocksLight != null)
469 { return !!def.blocksLight; }
470 return def.passable === false;
471}
472
473export function blockLightRange (typeId)
474{
475 const def = blockDefinition(typeId);
476 return def ? def.lightRangeTiles : 0;
477}
478
479export function normalizeGround (value, fallbackTypeId = GroundType.grass)
480{
481 if (typeof value === "number")
482 { return { typeId: value, state: null }; }
483 if (value && Number.isInteger(value.typeId))
484 { return { typeId: value.typeId, state: value.state ?? null }; }
485 return { typeId: fallbackTypeId, state: null };
486}
487
488export function normalizeBlock (value)
489{
490 if (!value || !Number.isInteger(value.typeId))
491 { return null; }
492 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
493}
494
495export function setBlockVisualState (block, state)
496{
497 if (!block)
498 { return null; }
499 block.state = state ?? null;
500 return block;
501}