BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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