BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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