BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

tiles.js 24.2 KiB Download Raw
1import
2{
3 ORE_BEARING_MATERIALS,
4 oreBoulderState,
5 materialFromOreBoulderState,
6 MATERIAL_DISPLAY_NAME,
7 isGemstoneMaterial,
8 WoodTier,
9 WOOD_TIER_ORDER
10} from "./materialTiers.js";
11import
12{
13 FLOWER_COLOR_ORDER,
14 FLOWER_COLOR_DISPLAY,
15 flowerBlockState
16} from "./dyes.js";
17import
18{
19 HERB_ORDER,
20 HERB_DEFINITIONS,
21 herbDefinition
22} from "./herbs.js";
23import { spriteImageName, defaultStateForType } from "./tileSprites.js";
24import { isSeasonalStumpState, SeasonId } from "../world/season.js";
25import
26{
27 composeNeighborConnectState,
28 definitionUsesMaterialNeighborConnect
29} from "../world/neighborConnect.js";
30
31export const GroundType = Object.freeze({
32 grass: 1,
33 wood: 2,
34 water: 3,
35 deepWater: 4,
36 dirt: 5
37});
38
39export const BlockType = Object.freeze({
40 stoneWall: 1,
41 torch: 2,
42 /** @deprecated Legacy authored block; migrated into chunk `spawners[]` on load. */
43 spawner: 3,
44 bank: 4,
45 churchAltar: 5,
46 cauldron: 6,
47 boulder: 7,
48 furnace: 8,
49 anvil: 9,
50 woodworkingBench: 10,
51 craftingBench: 11,
52 pineTree: 12,
53 marketplace: 13,
54 table: 14,
55 flower: 15,
56 cottonPlant: 16,
57 spinningWheel: 17,
58 loom: 18,
59 tailorBench: 19,
60 bountyOffice: 20,
61 barber: 23,
62 /** Folklore herbs — state is herbId (`sage`, `mint`, …); sprites in `assets/herbs/`. */
63 herbPlant: 24,
64 birchTree: 21,
65 oakTree: 22,
66 ebonyTree: 25,
67 lignumTree: 26,
68 heartwoodTree: 27,
69 elderTree: 28,
70 leatherworkingTable: 29,
71 doorway: 30,
72 campfire: 31,
73 oven: 32,
74 fishingSpot: 33,
75 jeweleringStation: 34,
76 flaxPlant: 35,
77 silkCocoon: 36,
78 fence: 37,
79 stoneWallThin: 38
80});
81
82export const GROUND_DEFINITIONS = Object.freeze({
83 [GroundType.grass]: Object.freeze({
84 id: GroundType.grass,
85 key: "grass",
86 displayName: "Grass",
87 passable: true,
88 wades: false,
89 water: false,
90 frames: 1,
91 framesPerRow: 1,
92 animationSpeed: 0,
93 /** Outdoor grounds should expose summer/fall/winter (legacy `grass` ok for summer). */
94 outdoor: true,
95 /** Soft-terrain edge blend when the player enables Soft terrain. */
96 blendable: true
97 }),
98 [GroundType.wood]: Object.freeze({
99 id: GroundType.wood,
100 key: "wood",
101 displayName: "Wood",
102 passable: true,
103 wades: false,
104 water: false,
105 frames: 1,
106 framesPerRow: 1,
107 animationSpeed: 0,
108 outdoor: false
109 }),
110 [GroundType.water]: Object.freeze({
111 id: GroundType.water,
112 key: "water",
113 displayName: "Water",
114 passable: true,
115 wades: true,
116 water: true,
117 frames: 4,
118 framesPerRow: 4,
119 animationSpeed: 0.12,
120 outdoor: true
121 }),
122 [GroundType.deepWater]: Object.freeze({
123 id: GroundType.deepWater,
124 key: "deep-water",
125 displayName: "Deep water",
126 passable: false,
127 wades: false,
128 water: true,
129 frames: 4,
130 framesPerRow: 4,
131 animationSpeed: 0.12,
132 outdoor: true
133 }),
134 [GroundType.dirt]: Object.freeze({
135 id: GroundType.dirt,
136 key: "dirt",
137 displayName: "Dirt",
138 passable: true,
139 wades: false,
140 water: false,
141 frames: 1,
142 framesPerRow: 1,
143 animationSpeed: 0,
144 outdoor: true,
145 blendable: true
146 })
147});
148
149function treeBlockDef (id, woodTier)
150{
151 const name = MATERIAL_DISPLAY_NAME[woodTier] ?? woodTier;
152 return Object.freeze({
153 id,
154 key: `${woodTier}-tree`,
155 displayName: `${name} tree`,
156 passable: false,
157 /** Canopy: light passes but each tree tile adds +1 falloff distance. */
158 blocksLight: "partial",
159 lightRangeTiles: 0,
160 frames: 1,
161 framesPerRow: 1,
162 animationSpeed: 0,
163 outdoor: true,
164 gatherable: true,
165 woodTier
166 });
167}
168
169export const BLOCK_DEFINITIONS = Object.freeze({
170 [BlockType.stoneWall]: Object.freeze({
171 id: BlockType.stoneWall,
172 key: "stone-wall",
173 displayName: "Thick stone wall",
174 passable: false,
175 lightRangeTiles: 0,
176 frames: 1,
177 framesPerRow: 1,
178 animationSpeed: 0,
179 /** Building walls count as indoor (no seasonal art required). */
180 outdoor: false,
181 /**
182 * Autotile via N/E/S/W + diagonal corner bits (blob masks).
183 * Diagonals distinguish L-strip joins from solid 2×2 fills.
184 */
185 neighborConnect: true,
186 neighborConnectCorners: true
187 }),
188 [BlockType.stoneWallThin]: Object.freeze({
189 id: BlockType.stoneWallThin,
190 key: "stone-wall-thin",
191 displayName: "Thin stone wall",
192 passable: false,
193 lightRangeTiles: 0,
194 frames: 1,
195 framesPerRow: 1,
196 animationSpeed: 0,
197 outdoor: false,
198 neighborConnect: true,
199 neighborConnectCorners: true
200 }),
201 [BlockType.torch]: Object.freeze({
202 id: BlockType.torch,
203 key: "torch",
204 displayName: "Torch",
205 passable: true,
206 lightRangeTiles: 8,
207 frames: 1,
208 framesPerRow: 1,
209 animationSpeed: 0,
210 outdoor: false
211 }),
212 [BlockType.spawner]: Object.freeze({
213 id: BlockType.spawner,
214 key: "spawner",
215 displayName: "Spawner",
216 passable: true,
217 lightRangeTiles: 0,
218 frames: 1,
219 framesPerRow: 1,
220 animationSpeed: 0,
221 /** Not shown in the placeable block palette; use the Spawners tab. */
222 editorHidden: true,
223 outdoor: false
224 }),
225 [BlockType.bank]: Object.freeze({
226 id: BlockType.bank,
227 key: "bank",
228 displayName: "Bank",
229 passable: false,
230 lightRangeTiles: 0,
231 frames: 1,
232 framesPerRow: 1,
233 animationSpeed: 0,
234 outdoor: false
235 }),
236 [BlockType.churchAltar]: Object.freeze({
237 id: BlockType.churchAltar,
238 key: "church-altar",
239 displayName: "Church altar",
240 passable: false,
241 lightRangeTiles: 0,
242 frames: 1,
243 framesPerRow: 1,
244 animationSpeed: 0,
245 outdoor: false
246 }),
247 [BlockType.cauldron]: Object.freeze({
248 id: BlockType.cauldron,
249 key: "cauldron",
250 displayName: "Cauldron",
251 passable: false,
252 blocksLight: false,
253 lightRangeTiles: 0,
254 frames: 1,
255 framesPerRow: 1,
256 animationSpeed: 0,
257 outdoor: false
258 }),
259 [BlockType.boulder]: Object.freeze({
260 id: BlockType.boulder,
261 key: "boulder",
262 displayName: "Boulder",
263 passable: false,
264 blocksLight: false,
265 lightRangeTiles: 0,
266 frames: 1,
267 framesPerRow: 1,
268 animationSpeed: 0,
269 /** Ore states are synthesized in the sprite catalog (tinted boulder-ore). */
270 gatherable: true,
271 outdoor: true
272 }),
273 [BlockType.furnace]: Object.freeze({
274 id: BlockType.furnace,
275 key: "furnace",
276 displayName: "Furnace",
277 passable: false,
278 blocksLight: false,
279 lightRangeTiles: 4,
280 frames: 1,
281 framesPerRow: 1,
282 animationSpeed: 0,
283 craftingStationId: "furnace",
284 outdoor: false
285 }),
286 [BlockType.campfire]: Object.freeze({
287 id: BlockType.campfire,
288 key: "campfire",
289 displayName: "Campfire",
290 passable: true,
291 blocksLight: false,
292 blocksRangedAttacks: false,
293 lightRangeTiles: 4,
294 frames: 1,
295 framesPerRow: 1,
296 animationSpeed: 0,
297 craftingStationId: "fireplace",
298 /** State is wood tier id; tinted like logs. Bloom companion is fire only. */
299 tintFromTier: true,
300 /** Lit/expires at runtime — per-tile draw, not chunk atlas (see chunk-delta). */
301 perTileBlock: true,
302 outdoor: true
303 }),
304 [BlockType.oven]: Object.freeze({
305 id: BlockType.oven,
306 key: "oven",
307 displayName: "Oven",
308 passable: false,
309 blocksLight: false,
310 lightRangeTiles: 2,
311 frames: 1,
312 framesPerRow: 1,
313 animationSpeed: 0,
314 craftingStationId: "oven",
315 outdoor: false
316 }),
317 [BlockType.fishingSpot]: Object.freeze({
318 id: BlockType.fishingSpot,
319 key: "fishing-spot",
320 displayName: "Fishing spot",
321 passable: true,
322 blocksLight: false,
323 blocksRangedAttacks: false,
324 lightRangeTiles: 0,
325 /** Ripple / splash strip (temp art; 3 frames). */
326 frames: 3,
327 framesPerRow: 3,
328 animationSpeed: 0.18,
329 outdoor: true,
330 /** Requires water / deepWater under the tile (editor + server). */
331 requiresWater: true
332 }),
333 [BlockType.anvil]: Object.freeze({
334 id: BlockType.anvil,
335 key: "anvil",
336 displayName: "Anvil",
337 passable: false,
338 blocksLight: false,
339 lightRangeTiles: 0,
340 frames: 1,
341 framesPerRow: 1,
342 animationSpeed: 0,
343 craftingStationId: "anvil",
344 outdoor: false
345 }),
346 [BlockType.jeweleringStation]: Object.freeze({
347 id: BlockType.jeweleringStation,
348 key: "jewelering-station",
349 displayName: "Jewelering station",
350 passable: false,
351 blocksLight: false,
352 lightRangeTiles: 0,
353 frames: 1,
354 framesPerRow: 1,
355 animationSpeed: 0,
356 craftingStationId: "jewelering-station",
357 outdoor: false
358 }),
359 [BlockType.woodworkingBench]: Object.freeze({
360 id: BlockType.woodworkingBench,
361 key: "woodworking-bench",
362 displayName: "Woodworking bench",
363 passable: false,
364 blocksLight: false,
365 lightRangeTiles: 0,
366 frames: 1,
367 framesPerRow: 1,
368 animationSpeed: 0,
369 craftingStationId: "woodworking-bench",
370 outdoor: false
371 }),
372 [BlockType.craftingBench]: Object.freeze({
373 id: BlockType.craftingBench,
374 key: "crafting-bench",
375 displayName: "Crafting bench",
376 passable: false,
377 blocksLight: false,
378 lightRangeTiles: 0,
379 frames: 1,
380 framesPerRow: 1,
381 animationSpeed: 0,
382 craftingStationId: "crafting-bench",
383 outdoor: false
384 }),
385 [BlockType.pineTree]: treeBlockDef(BlockType.pineTree, WoodTier.pine),
386 [BlockType.birchTree]: treeBlockDef(BlockType.birchTree, WoodTier.birch),
387 [BlockType.oakTree]: treeBlockDef(BlockType.oakTree, WoodTier.oak),
388 [BlockType.ebonyTree]: treeBlockDef(BlockType.ebonyTree, WoodTier.ebony),
389 [BlockType.lignumTree]: treeBlockDef(BlockType.lignumTree, WoodTier.lignum),
390 [BlockType.heartwoodTree]: treeBlockDef(BlockType.heartwoodTree, WoodTier.heartwood),
391 [BlockType.elderTree]: treeBlockDef(BlockType.elderTree, WoodTier.elder),
392 [BlockType.marketplace]: Object.freeze({
393 id: BlockType.marketplace,
394 key: "marketplace",
395 displayName: "Central Exchange counter",
396 passable: false,
397 /** Furniture: blocks movement but not baked/dynamic light rays. */
398 blocksLight: false,
399 lightRangeTiles: 0,
400 frames: 1,
401 framesPerRow: 1,
402 animationSpeed: 0,
403 outdoor: false,
404 /** Autotile via N/E/S/W bitmask states `"0"`…`"15"`. */
405 neighborConnect: true
406 }),
407 [BlockType.table]: Object.freeze({
408 id: BlockType.table,
409 key: "table",
410 displayName: "Table",
411 passable: false,
412 blocksLight: false,
413 blocksRangedAttacks: false,
414 lightRangeTiles: 0,
415 frames: 1,
416 framesPerRow: 1,
417 animationSpeed: 0,
418 outdoor: false,
419 /** Magenta base `0`–`15` (strip-union line tables); catalog expands `${wood}-${mask}`.
420 * Filled-rectangle corners can later add diagonal-aware sprites without replacing these. */
421 neighborConnect: true,
422 neighborConnectByMaterial: true,
423 defaultNeighborMaterial: WoodTier.pine
424 }),
425 [BlockType.fence]: Object.freeze({
426 id: BlockType.fence,
427 key: "fence",
428 displayName: "Fence",
429 passable: false,
430 blocksLight: false,
431 blocksRangedAttacks: false,
432 lightRangeTiles: 0,
433 frames: 1,
434 framesPerRow: 1,
435 animationSpeed: 0,
436 outdoor: true,
437 /** Magenta base `0`–`15`; catalog expands `${wood}-${mask}` like tables. */
438 neighborConnect: true,
439 neighborConnectByMaterial: true,
440 defaultNeighborMaterial: WoodTier.pine
441 }),
442 [BlockType.flower]: Object.freeze({
443 id: BlockType.flower,
444 key: "flower",
445 displayName: "Flower",
446 passable: true,
447 blocksLight: false,
448 lightRangeTiles: 0,
449 frames: 1,
450 framesPerRow: 1,
451 animationSpeed: 0,
452 outdoor: true,
453 gatherable: true,
454 plantGather: Object.freeze({ kind: "flower", requiresClippers: false, skillId: "herbalism" })
455 }),
456 [BlockType.cottonPlant]: Object.freeze({
457 id: BlockType.cottonPlant,
458 key: "cotton-plant",
459 displayName: "Cotton plant",
460 passable: true,
461 blocksLight: false,
462 lightRangeTiles: 0,
463 frames: 1,
464 framesPerRow: 1,
465 animationSpeed: 0,
466 outdoor: true,
467 gatherable: true,
468 plantGather: Object.freeze({ kind: "cotton", requiresClippers: false, skillId: "herbalism" })
469 }),
470 [BlockType.flaxPlant]: Object.freeze({
471 id: BlockType.flaxPlant,
472 key: "flax-plant",
473 displayName: "Flax plant",
474 passable: true,
475 blocksLight: false,
476 lightRangeTiles: 0,
477 frames: 1,
478 framesPerRow: 1,
479 animationSpeed: 0,
480 outdoor: true,
481 gatherable: true,
482 plantGather: Object.freeze({
483 kind: "flax",
484 requiresClippers: true,
485 /** Linen-rank clippers or better (steel↔linen). */
486 minClothTier: "linen",
487 skillId: "herbalism"
488 })
489 }),
490 [BlockType.silkCocoon]: Object.freeze({
491 id: BlockType.silkCocoon,
492 key: "silk-cocoon",
493 displayName: "Silk cocoon",
494 passable: true,
495 blocksLight: false,
496 lightRangeTiles: 0,
497 frames: 1,
498 framesPerRow: 1,
499 animationSpeed: 0,
500 outdoor: true,
501 gatherable: true,
502 plantGather: Object.freeze({
503 kind: "silk",
504 requiresClippers: true,
505 /** Silk-rank clippers or better (adamantium↔silk). */
506 minClothTier: "silk",
507 skillId: "herbalism"
508 })
509 }),
510 [BlockType.spinningWheel]: Object.freeze({
511 id: BlockType.spinningWheel,
512 key: "spinning-wheel",
513 displayName: "Spinning wheel",
514 passable: false,
515 blocksLight: false,
516 lightRangeTiles: 0,
517 frames: 1,
518 framesPerRow: 1,
519 animationSpeed: 0,
520 craftingStationId: "spinning-wheel",
521 outdoor: false
522 }),
523 [BlockType.loom]: Object.freeze({
524 id: BlockType.loom,
525 key: "loom",
526 displayName: "Loom",
527 passable: false,
528 blocksLight: false,
529 lightRangeTiles: 0,
530 frames: 1,
531 framesPerRow: 1,
532 animationSpeed: 0,
533 craftingStationId: "loom",
534 outdoor: false
535 }),
536 [BlockType.tailorBench]: Object.freeze({
537 id: BlockType.tailorBench,
538 key: "tailor-bench",
539 displayName: "Tailor's bench",
540 passable: false,
541 blocksLight: false,
542 lightRangeTiles: 0,
543 frames: 1,
544 framesPerRow: 1,
545 animationSpeed: 0,
546 craftingStationId: "tailor-bench",
547 outdoor: false
548 }),
549 [BlockType.leatherworkingTable]: Object.freeze({
550 id: BlockType.leatherworkingTable,
551 key: "leatherworking-table",
552 displayName: "Leatherworking table",
553 passable: false,
554 blocksLight: false,
555 lightRangeTiles: 0,
556 frames: 1,
557 framesPerRow: 1,
558 animationSpeed: 0,
559 craftingStationId: "leatherworking-station",
560 outdoor: false
561 }),
562 [BlockType.bountyOffice]: Object.freeze({
563 id: BlockType.bountyOffice,
564 key: "bounty-office",
565 displayName: "Bounty office",
566 passable: false,
567 blocksLight: false,
568 lightRangeTiles: 0,
569 frames: 1,
570 framesPerRow: 1,
571 animationSpeed: 0,
572 outdoor: false
573 }),
574 [BlockType.barber]: Object.freeze({
575 id: BlockType.barber,
576 key: "barber",
577 displayName: "Barber",
578 passable: false,
579 blocksLight: false,
580 lightRangeTiles: 0,
581 frames: 1,
582 framesPerRow: 1,
583 animationSpeed: 0,
584 outdoor: false
585 }),
586 [BlockType.doorway]: Object.freeze({
587 id: BlockType.doorway,
588 key: "doorway",
589 displayName: "Doorway",
590 passable: true,
591 blocksLight: false,
592 lightRangeTiles: 0,
593 frames: 1,
594 framesPerRow: 1,
595 animationSpeed: 0,
596 outdoor: false,
597 /** Visual variants under `blocks/doorway/`. */
598 doorway: true,
599 defaultState: "door"
600 }),
601 [BlockType.herbPlant]: Object.freeze({
602 id: BlockType.herbPlant,
603 key: "herb",
604 displayName: "Herb",
605 passable: true,
606 blocksLight: false,
607 lightRangeTiles: 0,
608 frames: 1,
609 framesPerRow: 1,
610 animationSpeed: 0,
611 outdoor: true,
612 gatherable: true,
613 /** Sprites live under `assets/herbs/` (shared with items), not `blocks/herb/`. */
614 assetFolder: "herbs",
615 plantGather: Object.freeze({
616 kind: "herb",
617 requiresClippers: true,
618 skillId: "herbalism"
619 })
620 })
621});
622
623/** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */
624export function listPlaceableGrounds ()
625{
626 return Object.values(GROUND_DEFINITIONS);
627}
628
629export function listPlaceableBlocks ()
630{
631 return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden);
632}
633
634/** Display name for an ore boulder state (`copper-ore` → "Copper ore boulder"). */
635export function oreBoulderDisplayName (state)
636{
637 const material = materialFromOreBoulderState(state);
638 if (!material)
639 { return null; }
640 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
641 if (material === "coal")
642 { return "Coal boulder"; }
643 if (isGemstoneMaterial(material))
644 { return `${label} boulder`; }
645 return `${label} ore boulder`;
646}
647
648/**
649 * Hover / UI label for a gatherable resource node.
650 * Ore boulders and chopable trees; fishing / herbs can extend this later.
651 * Returns null when the block is not an active resource node.
652 */
653export function resourceNodeDisplayName (block)
654{
655 if (!block)
656 { return null; }
657 if (block.typeId === BlockType.fishingSpot && block.state !== "exhausted")
658 { return "Fishing spot"; }
659 if (block.typeId === BlockType.boulder)
660 { return oreBoulderDisplayName(block.state); }
661 if (isChopableTreeBlock(block))
662 {
663 const def = BLOCK_DEFINITIONS[block.typeId];
664 return def?.displayName ?? "Tree";
665 }
666 if (block.typeId === BlockType.flower)
667 {
668 const color = FLOWER_COLOR_DISPLAY[block.state];
669 return color ? `${color} flower` : "Flower";
670 }
671 if (block.typeId === BlockType.cottonPlant)
672 { return "Cotton plant"; }
673 if (block.typeId === BlockType.flaxPlant)
674 { return "Flax plant"; }
675 if (block.typeId === BlockType.silkCocoon)
676 { return "Silk cocoon"; }
677 if (block.typeId === BlockType.herbPlant)
678 {
679 const herb = herbDefinition(block.state);
680 return herb ? herb.displayName : "Herb";
681 }
682 return null;
683}
684
685/** Wood tier for a tree block type, or null. */
686export function woodTierForTreeBlock (typeId)
687{
688 const def = BLOCK_DEFINITIONS[typeId];
689 return def?.woodTier ?? null;
690}
691
692/** True when this block is a standing (non-stump) gatherable tree. */
693export function isChopableTreeBlock (block)
694{
695 if (!block || woodTierForTreeBlock(block.typeId) == null)
696 { return false; }
697 return !isSeasonalStumpState(block.state);
698}
699
700/** Plant gather config from block def, or null. */
701export function plantGatherForBlock (block)
702{
703 if (!block)
704 { return null; }
705 const def = BLOCK_DEFINITIONS[block.typeId];
706 return def?.plantGather ?? null;
707}
708
709export function isPickablePlantBlock (block)
710{
711 return !!plantGatherForBlock(block);
712}
713
714/**
715 * Blocks baked into chunk atlas rows. Gather nodes and pickables stay as per-tile
716 * sprites so chunkDelta can hide or restyle them without rebaking the row.
717 */
718export function isChunkAtlasBakedBlock (block)
719{
720 if (!block)
721 { return false; }
722 const entry = normalizeBlock(block);
723 if (!entry)
724 { return false; }
725 const def = BLOCK_DEFINITIONS[entry.typeId];
726 if (!def || def.editorHidden)
727 { return false; }
728 if (def.perTileBlock)
729 { return false; }
730 if (resourceNodeDisplayName(entry))
731 { return false; }
732 if (isPickablePlantBlock(entry))
733 { return false; }
734 return true;
735}
736
737/**
738 * Editor palette cards: one entry per placeable, with boulder expanded into
739 * plain rock + each ore/coal variant (shared BlockType, distinct state).
740 * Flowers are a single card; colour is rolled when painting (and on respawn).
741 */
742export function listPlaceableBlockPaletteEntries (catalog = null)
743{
744 const entries = [];
745 for (const def of listPlaceableBlocks())
746 {
747 if (def.id === BlockType.boulder)
748 {
749 entries.push({
750 typeId: def.id,
751 state: "boulder",
752 key: def.key,
753 displayName: "Boulder",
754 spriteName: spriteImageName("block", def.key, "boulder")
755 });
756 for (const material of ORE_BEARING_MATERIALS)
757 {
758 const state = oreBoulderState(material);
759 entries.push({
760 typeId: def.id,
761 state,
762 key: def.key,
763 displayName: oreBoulderDisplayName(state) ?? state,
764 spriteName: spriteImageName("block", def.key, state)
765 });
766 }
767 continue;
768 }
769 if (def.id === BlockType.flower)
770 {
771 const state = flowerBlockState(FLOWER_COLOR_ORDER[0]);
772 entries.push({
773 typeId: def.id,
774 state,
775 key: def.key,
776 displayName: "Flower",
777 spriteName: spriteImageName("block", def.key, state)
778 });
779 continue;
780 }
781 if (def.id === BlockType.herbPlant)
782 {
783 for (const herbId of HERB_ORDER)
784 {
785 const herb = HERB_DEFINITIONS[herbId];
786 entries.push({
787 typeId: def.id,
788 state: herbId,
789 key: def.key,
790 displayName: herb?.displayName ?? herbId,
791 spriteName: spriteImageName("block", def.key, herbId)
792 });
793 }
794 continue;
795 }
796 if (def.neighborConnect)
797 {
798 if (definitionUsesMaterialNeighborConnect(def))
799 {
800 for (const material of WOOD_TIER_ORDER)
801 {
802 const state = composeNeighborConnectState(def, material, 0);
803 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
804 const noun = String(def.displayName ?? def.key).toLowerCase();
805 entries.push({
806 typeId: def.id,
807 state,
808 key: def.key,
809 displayName: `${label} ${noun}`,
810 spriteName: spriteImageName("block", def.key, state)
811 });
812 }
813 continue;
814 }
815 entries.push({
816 typeId: def.id,
817 state: "0",
818 key: def.key,
819 displayName: def.displayName,
820 spriteName: spriteImageName("block", def.key, "0")
821 });
822 continue;
823 }
824 const state = defaultStateForType(catalog, "block", def.key);
825 entries.push({
826 typeId: def.id,
827 state,
828 key: def.key,
829 displayName: def.displayName,
830 spriteName: state
831 ? spriteImageName("block", def.key, state)
832 : spriteImageName("block", def.key, def.key)
833 });
834 }
835 return entries;
836}
837
838export function groundDefinition (typeId)
839{
840 return GROUND_DEFINITIONS[typeId] ?? null;
841}
842
843export function blockDefinition (typeId)
844{
845 return BLOCK_DEFINITIONS[typeId] ?? null;
846}
847
848export function isGroundPassable (typeId)
849{
850 const def = groundDefinition(typeId);
851 if (!def)
852 { return true; }
853 return def.passable !== false;
854}
855
856export function isWaterGround (typeId)
857{
858 return !!groundDefinition(typeId)?.water;
859}
860
861export function isWadeGround (typeId)
862{
863 return !!groundDefinition(typeId)?.wades;
864}
865
866/**
867 * Liquid water for foam / wade (shallow water freezes in winter and is solid ice).
868 * Deep water stays liquid year-round.
869 */
870export function isLiquidWaterGround (typeId, seasonId = null)
871{
872 if (!isWaterGround(typeId))
873 { return false; }
874 if (typeId === GroundType.water && seasonId === SeasonId.winter)
875 { return false; }
876 return true;
877}
878
879/** Wade clip/foam only on liquid shallow water (not winter ice). */
880export function isWadeGroundInSeason (typeId, seasonId = null)
881{
882 return isWadeGround(typeId) && isLiquidWaterGround(typeId, seasonId);
883}
884
885export function isBlockPassable (typeId)
886{
887 if (typeId == null)
888 { return true; }
889
890 const def = blockDefinition(typeId);
891 return def ? def.passable : false;
892}
893
894/**
895 * Whether a block stops ranged attack / spell line of sight.
896 * `blocksRangedAttacks`: explicit override; `craftingStationId` defaults to clear;
897 * otherwise impassable blocks block and passable blocks do not.
898 */
899export function blockBlocksRangedAttacks (typeId)
900{
901 if (typeId == null)
902 { return false; }
903 const def = blockDefinition(typeId);
904 if (!def)
905 { return true; }
906 if (def.blocksRangedAttacks === false)
907 { return false; }
908 if (def.blocksRangedAttacks === true)
909 { return true; }
910 if (def.craftingStationId)
911 { return false; }
912 return def.passable === false;
913}
914
915/**
916 * Light ray occlusion for a block tile.
917 * - `clear` (0): no effect (default for passable; set `blocksLight: false` for furniture)
918 * - `partial` (1): ray continues; each such tile adds +1 to falloff distance (2× cost)
919 * - `solid` (2): ray stops (default for impassable; set `blocksLight: true` explicitly)
920 */
921export const LightOcclusion = Object.freeze({
922 clear: 0,
923 partial: 1,
924 solid: 2
925});
926
927/**
928 * Resolve light occlusion for a block tile (type + visual state).
929 * Stumps are a runtime/editor state override: no occlusion even on tree types.
930 */
931export function blockLightOcclusionForBlock (block)
932{
933 if (!block)
934 { return LightOcclusion.clear; }
935 if (isSeasonalStumpState(block.state))
936 { return LightOcclusion.clear; }
937 return blockLightOcclusion(block.typeId ?? null);
938}
939
940/**
941 * Resolve light occlusion for a block type id.
942 * `blocksLight`: `true` | `false` | `"partial"`; omitted → solid iff `!passable`.
943 * `blocksRangedAttacks`: `true` | `false`; omitted → false when `craftingStationId` is set,
944 * otherwise blocks iff `!passable` (furniture/stations may block movement but not magic/ranged).
945 * Prefer {@link blockLightOcclusionForBlock} when block state is known.
946 */
947export function blockLightOcclusion (typeId)
948{
949 if (typeId == null)
950 { return LightOcclusion.clear; }
951 const def = blockDefinition(typeId);
952 if (!def)
953 { return LightOcclusion.solid; }
954 if (def.blocksLight === "partial")
955 { return LightOcclusion.partial; }
956 if (def.blocksLight === false)
957 { return LightOcclusion.clear; }
958 if (def.blocksLight === true)
959 { return LightOcclusion.solid; }
960 return def.passable === false ? LightOcclusion.solid : LightOcclusion.clear;
961}
962
963/** True when a block fully stops light rays (not partial canopy). */
964export function isBlockOccludingLight (typeId)
965{
966 return blockLightOcclusion(typeId) === LightOcclusion.solid;
967}
968
969export function blockLightRange (typeId)
970{
971 const def = blockDefinition(typeId);
972 return def ? def.lightRangeTiles : 0;
973}
974
975export function normalizeGround (value, fallbackTypeId = GroundType.grass)
976{
977 if (typeof value === "number")
978 { return { typeId: value, state: null }; }
979 if (value && Number.isInteger(value.typeId))
980 { return { typeId: value.typeId, state: value.state ?? null }; }
981 return { typeId: fallbackTypeId, state: null };
982}
983
984export function normalizeBlock (value)
985{
986 if (!value || !Number.isInteger(value.typeId))
987 { return null; }
988 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
989}
990
991export function setBlockVisualState (block, state)
992{
993 if (!block)
994 { return null; }
995 block.state = state ?? null;
996 return block;
997}