BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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