BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

fix campfires
timfa Tim Falken · pushed by timfa · 2 months ago
55946701 History
tiles.js 21.7 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 /** Lit/expires at runtime — per-tile draw, not chunk atlas (see chunk-delta). */
276 perTileBlock: true,
277 outdoor: true
278 }),
279 [BlockType.oven]: Object.freeze({
280 id: BlockType.oven,
281 key: "oven",
282 displayName: "Oven",
283 passable: false,
284 blocksLight: false,
285 lightRangeTiles: 2,
286 frames: 1,
287 framesPerRow: 1,
288 animationSpeed: 0,
289 craftingStationId: "oven",
290 outdoor: false
291 }),
292 [BlockType.fishingSpot]: Object.freeze({
293 id: BlockType.fishingSpot,
294 key: "fishing-spot",
295 displayName: "Fishing spot",
296 passable: true,
297 blocksLight: false,
298 blocksRangedAttacks: false,
299 lightRangeTiles: 0,
300 /** Ripple / splash strip (temp art; 3 frames). */
301 frames: 3,
302 framesPerRow: 3,
303 animationSpeed: 0.18,
304 outdoor: true,
305 /** Requires water / deepWater under the tile (editor + server). */
306 requiresWater: true
307 }),
308 [BlockType.anvil]: Object.freeze({
309 id: BlockType.anvil,
310 key: "anvil",
311 displayName: "Anvil",
312 passable: false,
313 blocksLight: false,
314 lightRangeTiles: 0,
315 frames: 1,
316 framesPerRow: 1,
317 animationSpeed: 0,
318 craftingStationId: "anvil",
319 outdoor: false
320 }),
321 [BlockType.woodworkingBench]: Object.freeze({
322 id: BlockType.woodworkingBench,
323 key: "woodworking-bench",
324 displayName: "Woodworking bench",
325 passable: false,
326 blocksLight: false,
327 lightRangeTiles: 0,
328 frames: 1,
329 framesPerRow: 1,
330 animationSpeed: 0,
331 craftingStationId: "woodworking-bench",
332 outdoor: false
333 }),
334 [BlockType.craftingBench]: Object.freeze({
335 id: BlockType.craftingBench,
336 key: "crafting-bench",
337 displayName: "Crafting bench",
338 passable: false,
339 blocksLight: false,
340 lightRangeTiles: 0,
341 frames: 1,
342 framesPerRow: 1,
343 animationSpeed: 0,
344 craftingStationId: "crafting-bench",
345 outdoor: false
346 }),
347 [BlockType.pineTree]: treeBlockDef(BlockType.pineTree, WoodTier.pine),
348 [BlockType.birchTree]: treeBlockDef(BlockType.birchTree, WoodTier.birch),
349 [BlockType.oakTree]: treeBlockDef(BlockType.oakTree, WoodTier.oak),
350 [BlockType.ebonyTree]: treeBlockDef(BlockType.ebonyTree, WoodTier.ebony),
351 [BlockType.lignumTree]: treeBlockDef(BlockType.lignumTree, WoodTier.lignum),
352 [BlockType.heartwoodTree]: treeBlockDef(BlockType.heartwoodTree, WoodTier.heartwood),
353 [BlockType.elderTree]: treeBlockDef(BlockType.elderTree, WoodTier.elder),
354 [BlockType.marketplace]: Object.freeze({
355 id: BlockType.marketplace,
356 key: "marketplace",
357 displayName: "Central Exchange counter",
358 passable: false,
359 /** Furniture: blocks movement but not baked/dynamic light rays. */
360 blocksLight: false,
361 lightRangeTiles: 0,
362 frames: 1,
363 framesPerRow: 1,
364 animationSpeed: 0,
365 outdoor: false,
366 /** Autotile via N/E/S/W bitmask states `"0"`…`"15"`. */
367 neighborConnect: true
368 }),
369 [BlockType.table]: Object.freeze({
370 id: BlockType.table,
371 key: "table",
372 displayName: "Table",
373 passable: false,
374 blocksLight: false,
375 blocksRangedAttacks: false,
376 lightRangeTiles: 0,
377 frames: 1,
378 framesPerRow: 1,
379 animationSpeed: 0,
380 outdoor: false,
381 /** Magenta base `0`–`15` (strip-union line tables); catalog expands `${wood}-${mask}`.
382 * Filled-rectangle corners can later add diagonal-aware sprites without replacing these. */
383 neighborConnect: true,
384 neighborConnectByMaterial: true,
385 defaultNeighborMaterial: WoodTier.pine
386 }),
387 [BlockType.flower]: Object.freeze({
388 id: BlockType.flower,
389 key: "flower",
390 displayName: "Flower",
391 passable: true,
392 blocksLight: false,
393 lightRangeTiles: 0,
394 frames: 1,
395 framesPerRow: 1,
396 animationSpeed: 0,
397 outdoor: true,
398 gatherable: true,
399 plantGather: Object.freeze({ kind: "flower", requiresClippers: false, skillId: "herbalism" })
400 }),
401 [BlockType.cottonPlant]: Object.freeze({
402 id: BlockType.cottonPlant,
403 key: "cotton-plant",
404 displayName: "Cotton plant",
405 passable: true,
406 blocksLight: false,
407 lightRangeTiles: 0,
408 frames: 1,
409 framesPerRow: 1,
410 animationSpeed: 0,
411 outdoor: true,
412 gatherable: true,
413 plantGather: Object.freeze({ kind: "cotton", requiresClippers: false, skillId: "herbalism" })
414 }),
415 [BlockType.spinningWheel]: Object.freeze({
416 id: BlockType.spinningWheel,
417 key: "spinning-wheel",
418 displayName: "Spinning wheel",
419 passable: false,
420 blocksLight: false,
421 lightRangeTiles: 0,
422 frames: 1,
423 framesPerRow: 1,
424 animationSpeed: 0,
425 craftingStationId: "spinning-wheel",
426 outdoor: false
427 }),
428 [BlockType.loom]: Object.freeze({
429 id: BlockType.loom,
430 key: "loom",
431 displayName: "Loom",
432 passable: false,
433 blocksLight: false,
434 lightRangeTiles: 0,
435 frames: 1,
436 framesPerRow: 1,
437 animationSpeed: 0,
438 craftingStationId: "loom",
439 outdoor: false
440 }),
441 [BlockType.tailorBench]: Object.freeze({
442 id: BlockType.tailorBench,
443 key: "tailor-bench",
444 displayName: "Tailor's bench",
445 passable: false,
446 blocksLight: false,
447 lightRangeTiles: 0,
448 frames: 1,
449 framesPerRow: 1,
450 animationSpeed: 0,
451 craftingStationId: "tailor-bench",
452 outdoor: false
453 }),
454 [BlockType.leatherworkingTable]: Object.freeze({
455 id: BlockType.leatherworkingTable,
456 key: "leatherworking-table",
457 displayName: "Leatherworking table",
458 passable: false,
459 blocksLight: false,
460 lightRangeTiles: 0,
461 frames: 1,
462 framesPerRow: 1,
463 animationSpeed: 0,
464 craftingStationId: "leatherworking-station",
465 outdoor: false
466 }),
467 [BlockType.bountyOffice]: Object.freeze({
468 id: BlockType.bountyOffice,
469 key: "bounty-office",
470 displayName: "Bounty office",
471 passable: false,
472 blocksLight: false,
473 lightRangeTiles: 0,
474 frames: 1,
475 framesPerRow: 1,
476 animationSpeed: 0,
477 outdoor: false
478 }),
479 [BlockType.barber]: Object.freeze({
480 id: BlockType.barber,
481 key: "barber",
482 displayName: "Barber",
483 passable: false,
484 blocksLight: false,
485 lightRangeTiles: 0,
486 frames: 1,
487 framesPerRow: 1,
488 animationSpeed: 0,
489 outdoor: false
490 }),
491 [BlockType.doorway]: Object.freeze({
492 id: BlockType.doorway,
493 key: "doorway",
494 displayName: "Doorway",
495 passable: true,
496 blocksLight: false,
497 lightRangeTiles: 0,
498 frames: 1,
499 framesPerRow: 1,
500 animationSpeed: 0,
501 outdoor: false,
502 /** Visual variants under `blocks/doorway/`. */
503 doorway: true,
504 defaultState: "door"
505 }),
506 [BlockType.herbPlant]: Object.freeze({
507 id: BlockType.herbPlant,
508 key: "herb",
509 displayName: "Herb",
510 passable: true,
511 blocksLight: false,
512 lightRangeTiles: 0,
513 frames: 1,
514 framesPerRow: 1,
515 animationSpeed: 0,
516 outdoor: true,
517 gatherable: true,
518 /** Sprites live under `assets/herbs/` (shared with items), not `blocks/herb/`. */
519 assetFolder: "herbs",
520 plantGather: Object.freeze({
521 kind: "herb",
522 requiresClippers: true,
523 skillId: "herbalism"
524 })
525 })
526});
527
528/** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */
529export function listPlaceableGrounds ()
530{
531 return Object.values(GROUND_DEFINITIONS);
532}
533
534export function listPlaceableBlocks ()
535{
536 return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden);
537}
538
539/** Display name for an ore boulder state (`copper-ore` → "Copper ore boulder"). */
540export function oreBoulderDisplayName (state)
541{
542 const material = materialFromOreBoulderState(state);
543 if (!material)
544 { return null; }
545 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
546 return material === "coal" ? "Coal boulder" : `${label} ore boulder`;
547}
548
549/**
550 * Hover / UI label for a gatherable resource node.
551 * Ore boulders and chopable trees; fishing / herbs can extend this later.
552 * Returns null when the block is not an active resource node.
553 */
554export function resourceNodeDisplayName (block)
555{
556 if (!block)
557 { return null; }
558 if (block.typeId === BlockType.fishingSpot && block.state !== "exhausted")
559 { return "Fishing spot"; }
560 if (block.typeId === BlockType.boulder)
561 { return oreBoulderDisplayName(block.state); }
562 if (isChopableTreeBlock(block))
563 {
564 const def = BLOCK_DEFINITIONS[block.typeId];
565 return def?.displayName ?? "Tree";
566 }
567 if (block.typeId === BlockType.flower)
568 {
569 const color = FLOWER_COLOR_DISPLAY[block.state];
570 return color ? `${color} flower` : "Flower";
571 }
572 if (block.typeId === BlockType.cottonPlant)
573 { return "Cotton plant"; }
574 if (block.typeId === BlockType.herbPlant)
575 {
576 const herb = herbDefinition(block.state);
577 return herb ? herb.displayName : "Herb";
578 }
579 return null;
580}
581
582/** Wood tier for a tree block type, or null. */
583export function woodTierForTreeBlock (typeId)
584{
585 const def = BLOCK_DEFINITIONS[typeId];
586 return def?.woodTier ?? null;
587}
588
589/** True when this block is a standing (non-stump) gatherable tree. */
590export function isChopableTreeBlock (block)
591{
592 if (!block || woodTierForTreeBlock(block.typeId) == null)
593 { return false; }
594 return !isSeasonalStumpState(block.state);
595}
596
597/** Plant gather config from block def, or null. */
598export function plantGatherForBlock (block)
599{
600 if (!block)
601 { return null; }
602 const def = BLOCK_DEFINITIONS[block.typeId];
603 return def?.plantGather ?? null;
604}
605
606export function isPickablePlantBlock (block)
607{
608 return !!plantGatherForBlock(block);
609}
610
611/**
612 * Blocks baked into chunk atlas rows. Gather nodes and pickables stay as per-tile
613 * sprites so chunkDelta can hide or restyle them without rebaking the row.
614 */
615export function isChunkAtlasBakedBlock (block)
616{
617 if (!block)
618 { return false; }
619 const entry = normalizeBlock(block);
620 if (!entry)
621 { return false; }
622 const def = BLOCK_DEFINITIONS[entry.typeId];
623 if (!def || def.editorHidden)
624 { return false; }
625 if (def.perTileBlock)
626 { return false; }
627 if (resourceNodeDisplayName(entry))
628 { return false; }
629 if (isPickablePlantBlock(entry))
630 { return false; }
631 return true;
632}
633
634/**
635 * Editor palette cards: one entry per placeable, with boulder expanded into
636 * plain rock + each ore/coal variant (shared BlockType, distinct state).
637 * Flowers are a single card; colour is rolled when painting (and on respawn).
638 */
639export function listPlaceableBlockPaletteEntries (catalog = null)
640{
641 const entries = [];
642 for (const def of listPlaceableBlocks())
643 {
644 if (def.id === BlockType.boulder)
645 {
646 entries.push({
647 typeId: def.id,
648 state: "boulder",
649 key: def.key,
650 displayName: "Boulder",
651 spriteName: spriteImageName("block", def.key, "boulder")
652 });
653 for (const material of ORE_BEARING_MATERIALS)
654 {
655 const state = oreBoulderState(material);
656 entries.push({
657 typeId: def.id,
658 state,
659 key: def.key,
660 displayName: oreBoulderDisplayName(state) ?? state,
661 spriteName: spriteImageName("block", def.key, state)
662 });
663 }
664 continue;
665 }
666 if (def.id === BlockType.flower)
667 {
668 const state = flowerBlockState(FLOWER_COLOR_ORDER[0]);
669 entries.push({
670 typeId: def.id,
671 state,
672 key: def.key,
673 displayName: "Flower",
674 spriteName: spriteImageName("block", def.key, state)
675 });
676 continue;
677 }
678 if (def.id === BlockType.herbPlant)
679 {
680 for (const herbId of HERB_ORDER)
681 {
682 const herb = HERB_DEFINITIONS[herbId];
683 entries.push({
684 typeId: def.id,
685 state: herbId,
686 key: def.key,
687 displayName: herb?.displayName ?? herbId,
688 spriteName: spriteImageName("block", def.key, herbId)
689 });
690 }
691 continue;
692 }
693 if (def.neighborConnect)
694 {
695 if (definitionUsesMaterialNeighborConnect(def))
696 {
697 for (const material of WOOD_TIER_ORDER)
698 {
699 const state = composeNeighborConnectState(def, material, 0);
700 const label = MATERIAL_DISPLAY_NAME[material] ?? material;
701 entries.push({
702 typeId: def.id,
703 state,
704 key: def.key,
705 displayName: `${label} table`,
706 spriteName: spriteImageName("block", def.key, state)
707 });
708 }
709 continue;
710 }
711 entries.push({
712 typeId: def.id,
713 state: "0",
714 key: def.key,
715 displayName: def.displayName,
716 spriteName: spriteImageName("block", def.key, "0")
717 });
718 continue;
719 }
720 const state = defaultStateForType(catalog, "block", def.key);
721 entries.push({
722 typeId: def.id,
723 state,
724 key: def.key,
725 displayName: def.displayName,
726 spriteName: state
727 ? spriteImageName("block", def.key, state)
728 : spriteImageName("block", def.key, def.key)
729 });
730 }
731 return entries;
732}
733
734export function groundDefinition (typeId)
735{
736 return GROUND_DEFINITIONS[typeId] ?? null;
737}
738
739export function blockDefinition (typeId)
740{
741 return BLOCK_DEFINITIONS[typeId] ?? null;
742}
743
744export function isGroundPassable (typeId)
745{
746 const def = groundDefinition(typeId);
747 if (!def)
748 { return true; }
749 return def.passable !== false;
750}
751
752export function isWaterGround (typeId)
753{
754 return !!groundDefinition(typeId)?.water;
755}
756
757export function isWadeGround (typeId)
758{
759 return !!groundDefinition(typeId)?.wades;
760}
761
762/**
763 * Liquid water for foam / wade (shallow water freezes in winter and is solid ice).
764 * Deep water stays liquid year-round.
765 */
766export function isLiquidWaterGround (typeId, seasonId = null)
767{
768 if (!isWaterGround(typeId))
769 { return false; }
770 if (typeId === GroundType.water && seasonId === SeasonId.winter)
771 { return false; }
772 return true;
773}
774
775/** Wade clip/foam only on liquid shallow water (not winter ice). */
776export function isWadeGroundInSeason (typeId, seasonId = null)
777{
778 return isWadeGround(typeId) && isLiquidWaterGround(typeId, seasonId);
779}
780
781export function isBlockPassable (typeId)
782{
783 if (typeId == null)
784 { return true; }
785
786 const def = blockDefinition(typeId);
787 return def ? def.passable : false;
788}
789
790/**
791 * Whether a block stops ranged attack / spell line of sight.
792 * `blocksRangedAttacks`: explicit override; `craftingStationId` defaults to clear;
793 * otherwise impassable blocks block and passable blocks do not.
794 */
795export function blockBlocksRangedAttacks (typeId)
796{
797 if (typeId == null)
798 { return false; }
799 const def = blockDefinition(typeId);
800 if (!def)
801 { return true; }
802 if (def.blocksRangedAttacks === false)
803 { return false; }
804 if (def.blocksRangedAttacks === true)
805 { return true; }
806 if (def.craftingStationId)
807 { return false; }
808 return def.passable === false;
809}
810
811/**
812 * Light ray occlusion for a block tile.
813 * - `clear` (0): no effect (default for passable; set `blocksLight: false` for furniture)
814 * - `partial` (1): ray continues; each such tile adds +1 to falloff distance (2× cost)
815 * - `solid` (2): ray stops (default for impassable; set `blocksLight: true` explicitly)
816 */
817export const LightOcclusion = Object.freeze({
818 clear: 0,
819 partial: 1,
820 solid: 2
821});
822
823/**
824 * Resolve light occlusion for a block tile (type + visual state).
825 * Stumps are a runtime/editor state override: no occlusion even on tree types.
826 */
827export function blockLightOcclusionForBlock (block)
828{
829 if (!block)
830 { return LightOcclusion.clear; }
831 if (isSeasonalStumpState(block.state))
832 { return LightOcclusion.clear; }
833 return blockLightOcclusion(block.typeId ?? null);
834}
835
836/**
837 * Resolve light occlusion for a block type id.
838 * `blocksLight`: `true` | `false` | `"partial"`; omitted → solid iff `!passable`.
839 * `blocksRangedAttacks`: `true` | `false`; omitted → false when `craftingStationId` is set,
840 * otherwise blocks iff `!passable` (furniture/stations may block movement but not magic/ranged).
841 * Prefer {@link blockLightOcclusionForBlock} when block state is known.
842 */
843export function blockLightOcclusion (typeId)
844{
845 if (typeId == null)
846 { return LightOcclusion.clear; }
847 const def = blockDefinition(typeId);
848 if (!def)
849 { return LightOcclusion.solid; }
850 if (def.blocksLight === "partial")
851 { return LightOcclusion.partial; }
852 if (def.blocksLight === false)
853 { return LightOcclusion.clear; }
854 if (def.blocksLight === true)
855 { return LightOcclusion.solid; }
856 return def.passable === false ? LightOcclusion.solid : LightOcclusion.clear;
857}
858
859/** True when a block fully stops light rays (not partial canopy). */
860export function isBlockOccludingLight (typeId)
861{
862 return blockLightOcclusion(typeId) === LightOcclusion.solid;
863}
864
865export function blockLightRange (typeId)
866{
867 const def = blockDefinition(typeId);
868 return def ? def.lightRangeTiles : 0;
869}
870
871export function normalizeGround (value, fallbackTypeId = GroundType.grass)
872{
873 if (typeof value === "number")
874 { return { typeId: value, state: null }; }
875 if (value && Number.isInteger(value.typeId))
876 { return { typeId: value.typeId, state: value.state ?? null }; }
877 return { typeId: fallbackTypeId, state: null };
878}
879
880export function normalizeBlock (value)
881{
882 if (!value || !Number.isInteger(value.typeId))
883 { return null; }
884 return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null };
885}
886
887export function setBlockVisualState (block, state)
888{
889 if (!block)
890 { return null; }
891 block.state = state ?? null;
892 return block;
893}