BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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