BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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