BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

tiles.js 15.2 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 flower: 15,
40 cottonPlant: 16,
41 spinningWheel: 17,
42 loom: 18,
43 tailorBench: 19
44});
45
46export const GROUND_DEFINITIONS = Object.freeze({
47 [GroundType.grass]: Object.freeze({
48 id: GroundType.grass,
49 key: "grass",
50 displayName: "Grass",
51 passable: true,
52 wades: false,
53 water: false,
54 frames: 1,
55 framesPerRow: 1,
56 animationSpeed: 0,
57 /** Outdoor grounds should expose summer/fall/winter (legacy `grass` ok for summer). */
58 outdoor: true
59 }),
60 [GroundType.wood]: Object.freeze({
61 id: GroundType.wood,
62 key: "wood",
63 displayName: "Wood",
64 passable: true,
65 wades: false,
66 water: false,
67 frames: 1,
68 framesPerRow: 1,
69 animationSpeed: 0,
70 outdoor: false
71 }),
72 [GroundType.water]: Object.freeze({
73 id: GroundType.water,
74 key: "water",
75 displayName: "Water",
76 passable: true,
77 wades: true,
78 water: true,
79 frames: 4,
80 framesPerRow: 4,
81 animationSpeed: 0.12,
82 outdoor: true
83 }),
84 [GroundType.deepWater]: Object.freeze({
85 id: GroundType.deepWater,
86 key: "deep-water",
87 displayName: "Deep water",
88 passable: false,
89 wades: false,
90 water: true,
91 frames: 4,
92 framesPerRow: 4,
93 animationSpeed: 0.12,
94 outdoor: true
95 })
96});
97
98export const BLOCK_DEFINITIONS = Object.freeze({
99 [BlockType.stoneWall]: Object.freeze({
100 id: BlockType.stoneWall,
101 key: "stone-wall",
102 displayName: "Stone wall",
103 passable: false,
104 lightRangeTiles: 0,
105 frames: 1,
106 framesPerRow: 1,
107 animationSpeed: 0,
108 /** Building walls count as indoor (no seasonal art required). */
109 outdoor: false
110 }),
111 [BlockType.torch]: Object.freeze({
112 id: BlockType.torch,
113 key: "torch",
114 displayName: "Torch",
115 passable: true,
116 lightRangeTiles: 8,
117 frames: 1,
118 framesPerRow: 1,
119 animationSpeed: 0,
120 outdoor: false
121 }),
122 [BlockType.spawner]: Object.freeze({
123 id: BlockType.spawner,
124 key: "spawner",
125 displayName: "Spawner",
126 passable: true,
127 lightRangeTiles: 0,
128 frames: 1,
129 framesPerRow: 1,
130 animationSpeed: 0,
131 /** Not shown in the placeable block palette; use the Spawners tab. */
132 editorHidden: true,
133 outdoor: false
134 }),
135 [BlockType.bank]: Object.freeze({
136 id: BlockType.bank,
137 key: "bank",
138 displayName: "Bank",
139 passable: false,
140 lightRangeTiles: 0,
141 frames: 1,
142 framesPerRow: 1,
143 animationSpeed: 0,
144 outdoor: false
145 }),
146 [BlockType.churchAltar]: Object.freeze({
147 id: BlockType.churchAltar,
148 key: "church-altar",
149 displayName: "Church altar",
150 passable: false,
151 lightRangeTiles: 0,
152 frames: 1,
153 framesPerRow: 1,
154 animationSpeed: 0,
155 outdoor: false
156 }),
157 [BlockType.cauldron]: Object.freeze({
158 id: BlockType.cauldron,
159 key: "cauldron",
160 displayName: "Cauldron",
161 passable: false,
162 blocksLight: false,
163 lightRangeTiles: 0,
164 frames: 1,
165 framesPerRow: 1,
166 animationSpeed: 0,
167 outdoor: false
168 }),
169 [BlockType.boulder]: Object.freeze({
170 id: BlockType.boulder,
171 key: "boulder",
172 displayName: "Boulder",
173 passable: false,
174 blocksLight: false,
175 lightRangeTiles: 0,
176 frames: 1,
177 framesPerRow: 1,
178 animationSpeed: 0,
179 /** Ore states are synthesized in the sprite catalog (tinted boulder-ore). */
180 gatherable: true,
181 outdoor: true
182 }),
183 [BlockType.furnace]: Object.freeze({
184 id: BlockType.furnace,
185 key: "furnace",
186 displayName: "Furnace",
187 passable: false,
188 blocksLight: false,
189 lightRangeTiles: 4,
190 frames: 1,
191 framesPerRow: 1,
192 animationSpeed: 0,
193 craftingStationId: "furnace",
194 outdoor: false
195 }),
196 [BlockType.anvil]: Object.freeze({
197 id: BlockType.anvil,
198 key: "anvil",
199 displayName: "Anvil",
200 passable: false,
201 blocksLight: false,
202 lightRangeTiles: 0,
203 frames: 1,
204 framesPerRow: 1,
205 animationSpeed: 0,
206 craftingStationId: "anvil",
207 outdoor: false
208 }),
209 [BlockType.woodworkingBench]: Object.freeze({
210 id: BlockType.woodworkingBench,
211 key: "woodworking-bench",
212 displayName: "Woodworking bench",
213 passable: false,
214 blocksLight: false,
215 lightRangeTiles: 0,
216 frames: 1,
217 framesPerRow: 1,
218 animationSpeed: 0,
219 craftingStationId: "woodworking-bench",
220 outdoor: false
221 }),
222 [BlockType.craftingBench]: Object.freeze({
223 id: BlockType.craftingBench,
224 key: "crafting-bench",
225 displayName: "Crafting bench",
226 passable: false,
227 blocksLight: false,
228 lightRangeTiles: 0,
229 frames: 1,
230 framesPerRow: 1,
231 animationSpeed: 0,
232 craftingStationId: "crafting-bench",
233 outdoor: false
234 }),
235 [BlockType.pineTree]: Object.freeze({
236 id: BlockType.pineTree,
237 key: "pine-tree",
238 displayName: "Pine tree",
239 passable: false,
240 /** Canopy: light passes but each tree tile adds +1 falloff distance. */
241 blocksLight: "partial",
242 lightRangeTiles: 0,
243 frames: 1,
244 framesPerRow: 1,
245 animationSpeed: 0,
246 outdoor: true,
247 gatherable: true,
248 /** Wood tier yielded when chopped (axe). */
249 woodTier: WoodTier.pine
250 }),
251 [BlockType.marketplace]: Object.freeze({
252 id: BlockType.marketplace,
253 key: "marketplace",
254 displayName: "Central Exchange counter",
255 passable: false,
256 /** Furniture: blocks movement but not baked/dynamic light rays. */
257 blocksLight: false,
258 lightRangeTiles: 0,
259 frames: 1,
260 framesPerRow: 1,
261 animationSpeed: 0,
262 outdoor: false,
263 /** Autotile via N/E/S/W bitmask states `"0"`…`"15"`. */
264 neighborConnect: true
265 }),
266 [BlockType.table]: Object.freeze({
267 id: BlockType.table,
268 key: "table",
269 displayName: "Table",
270 passable: false,
271 blocksLight: false,
272 lightRangeTiles: 0,
273 frames: 1,
274 framesPerRow: 1,
275 animationSpeed: 0,
276 outdoor: false,
277 /** Magenta base `0`–`15` (strip-union line tables); catalog expands `${wood}-${mask}`.
278 * Filled-rectangle corners can later add diagonal-aware sprites without replacing these. */
279 neighborConnect: true,
280 neighborConnectByMaterial: true,
281 defaultNeighborMaterial: WoodTier.pine
282 }),
283 [BlockType.flower]: Object.freeze({
284 id: BlockType.flower,
285 key: "flower",
286 displayName: "Flower",
287 passable: true,
288 blocksLight: false,
289 lightRangeTiles: 0,
290 frames: 1,
291 framesPerRow: 1,
292 animationSpeed: 0,
293 outdoor: true,
294 gatherable: true,
295 plantGather: Object.freeze({ kind: "flower", requiresClippers: true, skillId: "herbalism" })
296 }),
297 [BlockType.cottonPlant]: Object.freeze({
298 id: BlockType.cottonPlant,
299 key: "cotton-plant",
300 displayName: "Cotton plant",
301 passable: true,
302 blocksLight: false,
303 lightRangeTiles: 0,
304 frames: 1,
305 framesPerRow: 1,
306 animationSpeed: 0,
307 outdoor: true,
308 gatherable: true,
309 plantGather: Object.freeze({ kind: "cotton", requiresClippers: false, skillId: "herbalism" })
310 }),
311 [BlockType.spinningWheel]: Object.freeze({
312 id: BlockType.spinningWheel,
313 key: "spinning-wheel",
314 displayName: "Spinning wheel",
315 passable: false,
316 blocksLight: false,
317 lightRangeTiles: 0,
318 frames: 1,
319 framesPerRow: 1,
320 animationSpeed: 0,
321 craftingStationId: "spinning-wheel",
322 outdoor: false
323 }),
324 [BlockType.loom]: Object.freeze({
325 id: BlockType.loom,
326 key: "loom",
327 displayName: "Loom",
328 passable: false,
329 blocksLight: false,
330 lightRangeTiles: 0,
331 frames: 1,
332 framesPerRow: 1,
333 animationSpeed: 0,
334 craftingStationId: "loom",
335 outdoor: false
336 }),
337 [BlockType.tailorBench]: Object.freeze({
338 id: BlockType.tailorBench,
339 key: "tailor-bench",
340 displayName: "Tailor's bench",
341 passable: false,
342 blocksLight: false,
343 lightRangeTiles: 0,
344 frames: 1,
345 framesPerRow: 1,
346 animationSpeed: 0,
347 craftingStationId: "tailor-bench",
348 outdoor: false
349 })
350});
351
352/** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */
353export function listPlaceableGrounds ()
354{
355 return Object.values(GROUND_DEFINITIONS);
356}
357
358export function listPlaceableBlocks ()
359{
360 return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden);
361}
362
363/** Display name for an ore boulder state (`copper-ore` → "Copper ore boulder"). */
364export function oreBoulderDisplayName (state)
365{
366 const material = materialFromOreBoulderState(state);
367 if (!material)
368 { return null; }
369 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
370 return material === "coal" ? "Coal boulder" : `${label} ore boulder`;
371}
372
373/**
374 * Hover / UI label for a gatherable resource node.
375 * Ore boulders and chopable trees; fishing / herbs can extend this later.
376 * Returns null when the block is not an active resource node.
377 */
378export function resourceNodeDisplayName (block)
379{
380 if (!block)
381 { return null; }
382 if (block.typeId === BlockType.boulder)
383 { return oreBoulderDisplayName(block.state); }
384 if (isChopableTreeBlock(block))
385 {
386 const def = BLOCK_DEFINITIONS[block.typeId];
387 return def?.displayName ?? "Tree";
388 }
389 if (block.typeId === BlockType.flower)
390 { return "Flower"; }
391 if (block.typeId === BlockType.cottonPlant)
392 { return "Cotton plant"; }
393 return null;
394}
395
396/** Wood tier for a tree block type, or null. */
397export function woodTierForTreeBlock (typeId)
398{
399 const def = BLOCK_DEFINITIONS[typeId];
400 return def?.woodTier ?? null;
401}
402
403/** True when this block is a standing (non-stump) gatherable tree. */
404export function isChopableTreeBlock (block)
405{
406 if (!block || woodTierForTreeBlock(block.typeId) == null)
407 { return false; }
408 return !isSeasonalStumpState(block.state);
409}
410
411/** Plant gather config from block def, or null. */
412export function plantGatherForBlock (block)
413{
414 if (!block)
415 { return null; }
416 const def = BLOCK_DEFINITIONS[block.typeId];
417 return def?.plantGather ?? null;
418}
419
420export function isPickablePlantBlock (block)
421{
422 return !!plantGatherForBlock(block);
423}
424
425/**
426 * Editor palette cards: one entry per placeable, with boulder expanded into
427 * plain rock + each ore/coal variant (shared BlockType, distinct state).
428 */
429export function listPlaceableBlockPaletteEntries (catalog = null)
430{
431 const entries = [];
432 for (const def of listPlaceableBlocks())
433 {
434 if (def.id === BlockType.boulder)
435 {
436 entries.push({
437 typeId: def.id,
438 state: "boulder",
439 key: def.key,
440 displayName: "Boulder",
441 spriteName: spriteImageName("block", def.key, "boulder")
442 });
443 for (const material of ORE_BEARING_MATERIALS)
444 {
445 const state = oreBoulderState(material);
446 entries.push({
447 typeId: def.id,
448 state,
449 key: def.key,
450 displayName: oreBoulderDisplayName(state) ?? state,
451 spriteName: spriteImageName("block", def.key, state)
452 });
453 }
454 continue;
455 }
456 if (def.neighborConnect)
457 {
458 if (definitionUsesMaterialNeighborConnect(def))
459 {
460 for (const material of WOOD_TIER_ORDER)
461 {
462 const state = composeNeighborConnectState(def, material, 0);
463 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
464 entries.push({
465 typeId: def.id,
466 state,
467 key: def.key,
468 displayName: `${label} table`,
469 spriteName: spriteImageName("block", def.key, state)
470 });
471 }
472 continue;
473 }
474 entries.push({
475 typeId: def.id,
476 state: "0",
477 key: def.key,
478 displayName: def.displayName,
479 spriteName: spriteImageName("block", def.key, "0")
480 });
481 continue;
482 }
483 const states = catalog?.blocks?.[def.key]?.states ?? [];
484 const state = resolveState(states, null);
485 entries.push({
486 typeId: def.id,
487 state,
488 key: def.key,
489 displayName: def.displayName,
490 spriteName: state
491 ? spriteImageName("block", def.key, state)
492 : spriteImageName("block", def.key, def.key)
493 });
494 }
495 return entries;
496}
497
498export function groundDefinition (typeId)
499{
500 return GROUND_DEFINITIONS[typeId] ?? null;
501}
502
503export function blockDefinition (typeId)
504{
505 return BLOCK_DEFINITIONS[typeId] ?? null;
506}
507
508export function isGroundPassable (typeId)
509{
510 const def = groundDefinition(typeId);
511 if (!def)
512 { return true; }
513 return def.passable !== false;
514}
515
516export function isWaterGround (typeId)
517{
518 return !!groundDefinition(typeId)?.water;
519}
520
521export function isWadeGround (typeId)
522{
523 return !!groundDefinition(typeId)?.wades;
524}
525
526/**
527 * Liquid water for foam / wade (shallow water freezes in winter and is solid ice).
528 * Deep water stays liquid year-round.
529 */
530export function isLiquidWaterGround (typeId, seasonId = null)
531{
532 if (!isWaterGround(typeId))
533 { return false; }
534 if (typeId === GroundType.water && seasonId === SeasonId.winter)
535 { return false; }
536 return true;
537}
538
539/** Wade clip/foam only on liquid shallow water (not winter ice). */
540export function isWadeGroundInSeason (typeId, seasonId = null)
541{
542 return isWadeGround(typeId) && isLiquidWaterGround(typeId, seasonId);
543}
544
545export function isBlockPassable (typeId)
546{
547 if (typeId == null)
548 { return true; }
549
550 const def = blockDefinition(typeId);
551 return def ? def.passable : false;
552}
553
554/**
555 * Light ray occlusion for a block tile.
556 * - `clear` (0): no effect (default for passable; set `blocksLight: false` for furniture)
557 * - `partial` (1): ray continues; each such tile adds +1 to falloff distance (2× cost)
558 * - `solid` (2): ray stops (default for impassable; set `blocksLight: true` explicitly)
559 */
560export const LightOcclusion = Object.freeze({
561 clear: 0,
562 partial: 1,
563 solid: 2
564});
565
566/**
567 * Resolve light occlusion for a block tile (type + visual state).
568 * Stumps are a runtime/editor state override: no occlusion even on tree types.
569 */
570export function blockLightOcclusionForBlock (block)
571{
572 if (!block)
573 { return LightOcclusion.clear; }
574 if (isSeasonalStumpState(block.state))
575 { return LightOcclusion.clear; }
576 return blockLightOcclusion(block.typeId ?? null);
577}
578
579/**
580 * Resolve light occlusion for a block type id.
581 * `blocksLight`: `true` | `false` | `"partial"`; omitted → solid iff `!passable`.
582 * Prefer {@link blockLightOcclusionForBlock} when block state is known.
583 */
584export function blockLightOcclusion (typeId)
585{
586 if (typeId == null)
587 { return LightOcclusion.clear; }
588 const def = blockDefinition(typeId);
589 if (!def)
590 { return LightOcclusion.solid; }
591 if (def.blocksLight === "partial")
592 { return LightOcclusion.partial; }
593 if (def.blocksLight === false)
594 { return LightOcclusion.clear; }
595 if (def.blocksLight === true)
596 { return LightOcclusion.solid; }
597 return def.passable === false ? LightOcclusion.solid : LightOcclusion.clear;
598}
599
600/** True when a block fully stops light rays (not partial canopy). */
601export function isBlockOccludingLight (typeId)
602{
603 return blockLightOcclusion(typeId) === LightOcclusion.solid;
604}
605
606export function blockLightRange (typeId)
607{
608 const def = blockDefinition(typeId);
609 return def ? def.lightRangeTiles : 0;
610}
611
612export function normalizeGround (value, fallbackTypeId = GroundType.grass)
613{
614 if (typeof value === "number")
615 { return { typeId: value, state: null }; }
616 if (value && Number.isInteger(value.typeId))
617 { return { typeId: value.typeId, state: value.state ?? null }; }
618 return { typeId: fallbackTypeId, state: null };
619}
620
621export function normalizeBlock (value)
622{
623 if (!value || !Number.isInteger(value.typeId))
624 { return null; }
625 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
626}
627
628export function setBlockVisualState (block, state)
629{
630 if (!block)
631 { return null; }
632 block.state = state ?? null;
633 return block;
634}