BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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