BlueGit

timfa questward

Files

questward / shared / definitions / tiles.js

tiles.js

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