BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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