BlueGit

timfa questward

Files

questward / shared / world / neighborConnect.js

neighborConnect.js

rweaks
timfa Tim Falken · pushed by timfa · 2 months ago
2b34e2dd History
neighborConnect.js 3.7 KiB Download Raw
1/**
2 * Generic neighbor-connect autotile masks (N/E/S/W).
3 * Stored as block/ground `state` string `"0"`…`"15"` for bakeable sprite variants.
4 * Same pattern can later apply to walls and selected grounds.
5 */
6
7export const NeighborBit = Object.freeze({
8 n: 1,
9 e: 2,
10 s: 4,
11 w: 8
12});
13
14export const NEIGHBOR_CONNECT_MASK_COUNT = 16;
15
16export function neighborConnectMask ({ n = false, e = false, s = false, w = false } = {})
17{
18 return (n ? NeighborBit.n : 0)
19 | (e ? NeighborBit.e : 0)
20 | (s ? NeighborBit.s : 0)
21 | (w ? NeighborBit.w : 0);
22}
23
24export function neighborConnectStateName (mask)
25{
26 const m = Math.max(0, Math.min(15, mask | 0));
27 return String(m);
28}
29
30export function parseNeighborConnectMask (state)
31{
32 if (state == null || state === "")
33 { return 0; }
34 const n = Number(state);
35 if (!Number.isFinite(n))
36 { return 0; }
37 return Math.max(0, Math.min(15, n | 0));
38}
39
40export function hasNeighborBit (mask, bit)
41{
42 return ((mask | 0) & bit) !== 0;
43}
44
45/** Cardinal offsets in tile space (y increases south in this project). */
46export const NEIGHBOR_CARDINALS = Object.freeze([
47 Object.freeze({ bit: NeighborBit.n, dx: 0, dy: -1, key: "n" }),
48 Object.freeze({ bit: NeighborBit.e, dx: 1, dy: 0, key: "e" }),
49 Object.freeze({ bit: NeighborBit.s, dx: 0, dy: 1, key: "s" }),
50 Object.freeze({ bit: NeighborBit.w, dx: -1, dy: 0, key: "w" })
51]);
52
53/**
54 * @param {(dx: number, dy: number) => boolean} isConnectedNeighbor
55 * true when the adjacent tile should join (same connectable type).
56 */
57export function computeNeighborConnectMask (isConnectedNeighbor)
58{
59 let mask = 0;
60 for (const dir of NEIGHBOR_CARDINALS)
61 {
62 if (isConnectedNeighbor(dir.dx, dir.dy))
63 { mask |= dir.bit; }
64 }
65 return mask;
66}
67
68export function definitionUsesNeighborConnect (def)
69{
70 return !!def?.neighborConnect;
71}
72
73export function definitionUsesMaterialNeighborConnect (def)
74{
75 return !!def?.neighborConnect && !!def?.neighborConnectByMaterial;
76}
77
78function clampMask (value)
79{
80 const n = Number(value);
81 if (!Number.isFinite(n))
82 { return 0; }
83 return Math.max(0, Math.min(15, n | 0));
84}
85
86/**
87 * Tables (and similar) store `${material}-${mask}` e.g. `pine-3`.
88 * Plain masks (`"7"`) stay valid for counters.
89 */
90export function parseMaterialNeighborConnectState (state, def = null)
91{
92 const fallback = def?.defaultNeighborMaterial ?? "pine";
93 const s = String(state ?? "").trim();
94 const paired = /^([a-z][a-z0-9]*)-(\d+)$/i.exec(s);
95 if (paired)
96 {
97 return {
98 material: paired[1].toLowerCase(),
99 mask: clampMask(paired[2])
100 };
101 }
102 if (/^\d+$/.test(s))
103 {
104 return { material: fallback, mask: clampMask(s) };
105 }
106 if (/^[a-z][a-z0-9]*$/i.test(s))
107 {
108 return { material: s.toLowerCase(), mask: 0 };
109 }
110 return { material: fallback, mask: 0 };
111}
112
113export function composeNeighborConnectState (def, currentState, mask)
114{
115 const m = clampMask(mask);
116 if (definitionUsesMaterialNeighborConnect(def))
117 {
118 const { material } = parseMaterialNeighborConnectState(currentState, def);
119 return `${material}-${m}`;
120 }
121 return neighborConnectStateName(m);
122}
123
124/** True when two blocks should join for neighbor-connect (same type; same material if required). */
125export function neighborConnectBlocksMatch (self, neighbor, def)
126{
127 if (!self || !neighbor || (self.typeId | 0) !== (neighbor.typeId | 0))
128 { return false; }
129 if (!definitionUsesMaterialNeighborConnect(def))
130 { return true; }
131 const a = parseMaterialNeighborConnectState(self.state, def).material;
132 const b = parseMaterialNeighborConnectState(neighbor.state, def).material;
133 return a === b;
134}
135
136/** Resolve the visual mask from any neighbor-connect state string. */
137export function parseNeighborConnectMaskFromState (state, def = null)
138{
139 if (definitionUsesMaterialNeighborConnect(def))
140 { return parseMaterialNeighborConnectState(state, def).mask; }
141 return parseNeighborConnectMask(state);
142}