BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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