BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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