BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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