| 1 | /** |
| 2 | * Generic neighbor-connect autotile masks (N/E/S/W). |
| 3 | * Stored as block/ground `state` for bakeable sprite variants. |
| 4 | * |
| 5 | * - Cardinal numeric (tables/fences/counters): `"0"`…`"15"` (or `pine-3` with material). |
| 6 | * - Named cardinal (stone walls): `"thick-stone-wall"`, `"thick-stone-wall-esw"`, … |
| 7 | */ |
| 8 | |
| 9 | export const NeighborBit = Object.freeze({ |
| 10 | n: 1, |
| 11 | e: 2, |
| 12 | s: 4, |
| 13 | w: 8 |
| 14 | }); |
| 15 | |
| 16 | export const NEIGHBOR_CONNECT_MASK_COUNT = 16; |
| 17 | |
| 18 | export function neighborConnectMask ({ n = false, e = false, s = false, w = false } = {}) |
| 19 | { |
| 20 | return (n ? NeighborBit.n : 0) |
| 21 | | (e ? NeighborBit.e : 0) |
| 22 | | (s ? NeighborBit.s : 0) |
| 23 | | (w ? NeighborBit.w : 0); |
| 24 | } |
| 25 | |
| 26 | export function neighborConnectStateName (mask) |
| 27 | { |
| 28 | return String((mask | 0) & 15); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Cardinal connection suffix for named wall sprites. |
| 33 | * Opposite runs: `ns`, `we`. Three-ways clockwise from the side after the gap |
| 34 | * (`esw` = T open north, `wne` = T open south). Full: `nesw`. Alone: `""`. |
| 35 | */ |
| 36 | export function cardinalConnectionSuffix (mask) |
| 37 | { |
| 38 | const m = (mask | 0) & 15; |
| 39 | if (m === 0) |
| 40 | { return ""; } |
| 41 | const n = (m & NeighborBit.n) !== 0; |
| 42 | const e = (m & NeighborBit.e) !== 0; |
| 43 | const s = (m & NeighborBit.s) !== 0; |
| 44 | const w = (m & NeighborBit.w) !== 0; |
| 45 | const count = (n ? 1 : 0) + (e ? 1 : 0) + (s ? 1 : 0) + (w ? 1 : 0); |
| 46 | if (count === 1) |
| 47 | { |
| 48 | if (n) |
| 49 | { return "n"; } |
| 50 | if (e) |
| 51 | { return "e"; } |
| 52 | if (s) |
| 53 | { return "s"; } |
| 54 | return "w"; |
| 55 | } |
| 56 | if (count === 2) |
| 57 | { |
| 58 | if (n && s) |
| 59 | { return "ns"; } |
| 60 | if (e && w) |
| 61 | { return "we"; } |
| 62 | if (n && e) |
| 63 | { return "ne"; } |
| 64 | if (e && s) |
| 65 | { return "es"; } |
| 66 | if (s && w) |
| 67 | { return "sw"; } |
| 68 | return "wn"; |
| 69 | } |
| 70 | if (count === 3) |
| 71 | { |
| 72 | const bits = [NeighborBit.n, NeighborBit.e, NeighborBit.s, NeighborBit.w]; |
| 73 | const letters = ["n", "e", "s", "w"]; |
| 74 | let miss = 0; |
| 75 | for (let i = 0; i < 4; i++) |
| 76 | { |
| 77 | if ((m & bits[i]) === 0) |
| 78 | { miss = i; } |
| 79 | } |
| 80 | return letters[(miss + 1) % 4] + letters[(miss + 2) % 4] + letters[(miss + 3) % 4]; |
| 81 | } |
| 82 | return "nesw"; |
| 83 | } |
| 84 | |
| 85 | export function composeNamedNeighborConnectState (def, mask) |
| 86 | { |
| 87 | const key = def?.key ?? "block"; |
| 88 | const suffix = cardinalConnectionSuffix(mask); |
| 89 | return suffix ? `${key}-${suffix}` : key; |
| 90 | } |
| 91 | |
| 92 | /** Parse named (`thick-stone-wall-esw`) or bare (`esw`) or legacy numeric states. */ |
| 93 | export function parseNamedNeighborConnectMask (state, def = null) |
| 94 | { |
| 95 | const s = String(state ?? "").trim().toLowerCase(); |
| 96 | if (!s) |
| 97 | { return 0; } |
| 98 | if (/^\d+$/.test(s)) |
| 99 | { |
| 100 | // Legacy numeric masks may include old diagonal bits (e.g. 19 = N|E|NE). |
| 101 | // Keep only cardinals — do not clamp with min(15,n) (that turns 19 into 15/nesw). |
| 102 | return (Number(s) | 0) & 15; |
| 103 | } |
| 104 | const key = String(def?.key ?? "").toLowerCase(); |
| 105 | if (key && s === key) |
| 106 | { return 0; } |
| 107 | let suffix = s; |
| 108 | if (key && s.startsWith(`${key}-`)) |
| 109 | { suffix = s.slice(key.length + 1); } |
| 110 | let mask = 0; |
| 111 | for (const ch of suffix) |
| 112 | { |
| 113 | if (ch === "n") |
| 114 | { mask |= NeighborBit.n; } |
| 115 | else if (ch === "e") |
| 116 | { mask |= NeighborBit.e; } |
| 117 | else if (ch === "s") |
| 118 | { mask |= NeighborBit.s; } |
| 119 | else if (ch === "w") |
| 120 | { mask |= NeighborBit.w; } |
| 121 | } |
| 122 | return mask; |
| 123 | } |
| 124 | |
| 125 | export function parseNeighborConnectMask (state) |
| 126 | { |
| 127 | if (state == null || state === "") |
| 128 | { return 0; } |
| 129 | const n = Number(state); |
| 130 | if (!Number.isFinite(n)) |
| 131 | { return 0; } |
| 132 | return (n | 0) & 15; |
| 133 | } |
| 134 | |
| 135 | export function hasNeighborBit (mask, bit) |
| 136 | { |
| 137 | return ((mask | 0) & bit) !== 0; |
| 138 | } |
| 139 | |
| 140 | /** Cardinal offsets in tile space (y increases south in this project). */ |
| 141 | export const NEIGHBOR_CARDINALS = Object.freeze([ |
| 142 | Object.freeze({ bit: NeighborBit.n, dx: 0, dy: -1, key: "n" }), |
| 143 | Object.freeze({ bit: NeighborBit.e, dx: 1, dy: 0, key: "e" }), |
| 144 | Object.freeze({ bit: NeighborBit.s, dx: 0, dy: 1, key: "s" }), |
| 145 | Object.freeze({ bit: NeighborBit.w, dx: -1, dy: 0, key: "w" }) |
| 146 | ]); |
| 147 | |
| 148 | /** |
| 149 | * @param {(dx: number, dy: number) => boolean} isConnectedNeighbor |
| 150 | */ |
| 151 | export function computeNeighborConnectMask (isConnectedNeighbor) |
| 152 | { |
| 153 | let mask = 0; |
| 154 | for (const dir of NEIGHBOR_CARDINALS) |
| 155 | { |
| 156 | if (isConnectedNeighbor(dir.dx, dir.dy)) |
| 157 | { mask |= dir.bit; } |
| 158 | } |
| 159 | return mask; |
| 160 | } |
| 161 | |
| 162 | export function definitionUsesNeighborConnect (def) |
| 163 | { |
| 164 | return !!def?.neighborConnect; |
| 165 | } |
| 166 | |
| 167 | export function definitionUsesNamedNeighborConnect (def) |
| 168 | { |
| 169 | return !!def?.neighborConnect && !!def?.neighborConnectNamed; |
| 170 | } |
| 171 | |
| 172 | export function definitionUsesMaterialNeighborConnect (def) |
| 173 | { |
| 174 | return !!def?.neighborConnect && !!def?.neighborConnectByMaterial; |
| 175 | } |
| 176 | |
| 177 | function clampCardinalMask (value) |
| 178 | { |
| 179 | const n = Number(value); |
| 180 | if (!Number.isFinite(n)) |
| 181 | { return 0; } |
| 182 | return (n | 0) & 15; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Tables (and similar) store `${material}-${mask}` e.g. `pine-3`. |
| 187 | * Plain masks (`"7"`) stay valid for counters. |
| 188 | */ |
| 189 | export function parseMaterialNeighborConnectState (state, def = null) |
| 190 | { |
| 191 | const fallback = def?.defaultNeighborMaterial ?? "pine"; |
| 192 | const s = String(state ?? "").trim(); |
| 193 | const paired = /^([a-z][a-z0-9]*)-(\d+)$/i.exec(s); |
| 194 | if (paired) |
| 195 | { |
| 196 | return { |
| 197 | material: paired[1].toLowerCase(), |
| 198 | mask: clampCardinalMask(paired[2]) |
| 199 | }; |
| 200 | } |
| 201 | if (/^\d+$/.test(s)) |
| 202 | { |
| 203 | return { material: fallback, mask: clampCardinalMask(s) }; |
| 204 | } |
| 205 | if (/^[a-z][a-z0-9]*$/i.test(s)) |
| 206 | { |
| 207 | return { material: s.toLowerCase(), mask: 0 }; |
| 208 | } |
| 209 | return { material: fallback, mask: 0 }; |
| 210 | } |
| 211 | |
| 212 | export function composeNeighborConnectState (def, currentState, mask) |
| 213 | { |
| 214 | const m = clampCardinalMask(mask); |
| 215 | if (definitionUsesMaterialNeighborConnect(def)) |
| 216 | { |
| 217 | const { material } = parseMaterialNeighborConnectState(currentState, def); |
| 218 | return `${material}-${m}`; |
| 219 | } |
| 220 | if (definitionUsesNamedNeighborConnect(def)) |
| 221 | { return composeNamedNeighborConnectState(def, m); } |
| 222 | return neighborConnectStateName(m); |
| 223 | } |
| 224 | |
| 225 | /** True when two blocks should join for neighbor-connect (same type; same material if required). */ |
| 226 | export function neighborConnectBlocksMatch (self, neighbor, def) |
| 227 | { |
| 228 | if (!self || !neighbor || (self.typeId | 0) !== (neighbor.typeId | 0)) |
| 229 | { return false; } |
| 230 | if (!definitionUsesMaterialNeighborConnect(def)) |
| 231 | { return true; } |
| 232 | const a = parseMaterialNeighborConnectState(self.state, def).material; |
| 233 | const b = parseMaterialNeighborConnectState(neighbor.state, def).material; |
| 234 | return a === b; |
| 235 | } |
| 236 | |
| 237 | /** Resolve the visual mask from any neighbor-connect state string. */ |
| 238 | export function parseNeighborConnectMaskFromState (state, def = null) |
| 239 | { |
| 240 | if (definitionUsesMaterialNeighborConnect(def)) |
| 241 | { return parseMaterialNeighborConnectState(state, def).mask; } |
| 242 | if (definitionUsesNamedNeighborConnect(def)) |
| 243 | { return parseNamedNeighborConnectMask(state, def); } |
| 244 | return parseNeighborConnectMask(state); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Map a world tile to chunk coords + local tile (handles negative world tiles). |
| 249 | */ |
| 250 | export function worldTileToChunkLocal (tileX, tileY, chunkSize = 16) |
| 251 | { |
| 252 | const size = Math.max(1, chunkSize | 0 || 16); |
| 253 | const chunkX = Math.floor(tileX / size); |
| 254 | const chunkY = Math.floor(tileY / size); |
| 255 | return { |
| 256 | chunkX, |
| 257 | chunkY, |
| 258 | localX: tileX - chunkX * size, |
| 259 | localY: tileY - chunkY * size |
| 260 | }; |
| 261 | } |
| 262 | |
| 263 | /** Self + cardinal neighbors in world tile space (for connect mask refresh). */ |
| 264 | export function neighborConnectRefreshTiles (tileX, tileY) |
| 265 | { |
| 266 | return [ |
| 267 | [tileX, tileY], |
| 268 | [tileX, tileY - 1], |
| 269 | [tileX + 1, tileY], |
| 270 | [tileX, tileY + 1], |
| 271 | [tileX - 1, tileY] |
| 272 | ]; |
| 273 | } |