| 1 | import { |
| 2 | ORE_BEARING_MATERIALS, |
| 3 | oreBoulderState, |
| 4 | materialFromOreBoulderState, |
| 5 | MATERIAL_DISPLAY_NAME |
| 6 | } from "./materialTiers.js"; |
| 7 | import { spriteImageName, resolveState } from "./tileSprites.js"; |
| 8 | |
| 9 | export const GroundType = Object.freeze({ |
| 10 | grass: 1, |
| 11 | wood: 2, |
| 12 | water: 3, |
| 13 | deepWater: 4 |
| 14 | }); |
| 15 | |
| 16 | export const BlockType = Object.freeze({ |
| 17 | stoneWall: 1, |
| 18 | torch: 2, |
| 19 | /** @deprecated Legacy authored block; migrated into chunk `spawners[]` on load. */ |
| 20 | spawner: 3, |
| 21 | bank: 4, |
| 22 | churchAltar: 5, |
| 23 | cauldron: 6, |
| 24 | boulder: 7, |
| 25 | furnace: 8, |
| 26 | anvil: 9, |
| 27 | woodworkingBench: 10, |
| 28 | craftingBench: 11 |
| 29 | }); |
| 30 | |
| 31 | export const GROUND_DEFINITIONS = Object.freeze({ |
| 32 | [GroundType.grass]: Object.freeze({ |
| 33 | id: GroundType.grass, |
| 34 | key: "grass", |
| 35 | displayName: "Grass", |
| 36 | passable: true, |
| 37 | wades: false, |
| 38 | water: false, |
| 39 | frames: 1, |
| 40 | framesPerRow: 1, |
| 41 | animationSpeed: 0 |
| 42 | }), |
| 43 | [GroundType.wood]: Object.freeze({ |
| 44 | id: GroundType.wood, |
| 45 | key: "wood", |
| 46 | displayName: "Wood", |
| 47 | passable: true, |
| 48 | wades: false, |
| 49 | water: false, |
| 50 | frames: 1, |
| 51 | framesPerRow: 1, |
| 52 | animationSpeed: 0 |
| 53 | }), |
| 54 | [GroundType.water]: Object.freeze({ |
| 55 | id: GroundType.water, |
| 56 | key: "water", |
| 57 | displayName: "Water", |
| 58 | passable: true, |
| 59 | wades: true, |
| 60 | water: true, |
| 61 | frames: 4, |
| 62 | framesPerRow: 4, |
| 63 | animationSpeed: 0.12 |
| 64 | }), |
| 65 | [GroundType.deepWater]: Object.freeze({ |
| 66 | id: GroundType.deepWater, |
| 67 | key: "deep-water", |
| 68 | displayName: "Deep water", |
| 69 | passable: false, |
| 70 | wades: false, |
| 71 | water: true, |
| 72 | frames: 4, |
| 73 | framesPerRow: 4, |
| 74 | animationSpeed: 0.12 |
| 75 | }) |
| 76 | }); |
| 77 | |
| 78 | export const BLOCK_DEFINITIONS = Object.freeze({ |
| 79 | [BlockType.stoneWall]: Object.freeze({ |
| 80 | id: BlockType.stoneWall, |
| 81 | key: "stone-wall", |
| 82 | displayName: "Stone wall", |
| 83 | passable: false, |
| 84 | lightRangeTiles: 0, |
| 85 | frames: 1, |
| 86 | framesPerRow: 1, |
| 87 | animationSpeed: 0 |
| 88 | }), |
| 89 | [BlockType.torch]: Object.freeze({ |
| 90 | id: BlockType.torch, |
| 91 | key: "torch", |
| 92 | displayName: "Torch", |
| 93 | passable: true, |
| 94 | lightRangeTiles: 8, |
| 95 | frames: 1, |
| 96 | framesPerRow: 1, |
| 97 | animationSpeed: 0 |
| 98 | }), |
| 99 | [BlockType.spawner]: Object.freeze({ |
| 100 | id: BlockType.spawner, |
| 101 | key: "spawner", |
| 102 | displayName: "Spawner", |
| 103 | passable: true, |
| 104 | lightRangeTiles: 0, |
| 105 | frames: 1, |
| 106 | framesPerRow: 1, |
| 107 | animationSpeed: 0, |
| 108 | /** Not shown in the placeable block palette; use the Spawners tab. */ |
| 109 | editorHidden: true |
| 110 | }), |
| 111 | [BlockType.bank]: Object.freeze({ |
| 112 | id: BlockType.bank, |
| 113 | key: "bank", |
| 114 | displayName: "Bank", |
| 115 | passable: false, |
| 116 | lightRangeTiles: 0, |
| 117 | frames: 1, |
| 118 | framesPerRow: 1, |
| 119 | animationSpeed: 0 |
| 120 | }), |
| 121 | [BlockType.churchAltar]: Object.freeze({ |
| 122 | id: BlockType.churchAltar, |
| 123 | key: "church-altar", |
| 124 | displayName: "Church altar", |
| 125 | passable: false, |
| 126 | lightRangeTiles: 0, |
| 127 | frames: 1, |
| 128 | framesPerRow: 1, |
| 129 | animationSpeed: 0 |
| 130 | }), |
| 131 | [BlockType.cauldron]: Object.freeze({ |
| 132 | id: BlockType.cauldron, |
| 133 | key: "cauldron", |
| 134 | displayName: "Cauldron", |
| 135 | passable: false, |
| 136 | lightRangeTiles: 0, |
| 137 | frames: 1, |
| 138 | framesPerRow: 1, |
| 139 | animationSpeed: 0 |
| 140 | }), |
| 141 | [BlockType.boulder]: Object.freeze({ |
| 142 | id: BlockType.boulder, |
| 143 | key: "boulder", |
| 144 | displayName: "Boulder", |
| 145 | passable: false, |
| 146 | lightRangeTiles: 0, |
| 147 | frames: 1, |
| 148 | framesPerRow: 1, |
| 149 | animationSpeed: 0, |
| 150 | /** Ore states are synthesized in the sprite catalog (tinted boulder-ore). */ |
| 151 | gatherable: true |
| 152 | }), |
| 153 | [BlockType.furnace]: Object.freeze({ |
| 154 | id: BlockType.furnace, |
| 155 | key: "furnace", |
| 156 | displayName: "Furnace", |
| 157 | passable: false, |
| 158 | lightRangeTiles: 4, |
| 159 | frames: 1, |
| 160 | framesPerRow: 1, |
| 161 | animationSpeed: 0, |
| 162 | craftingStationId: "furnace" |
| 163 | }), |
| 164 | [BlockType.anvil]: Object.freeze({ |
| 165 | id: BlockType.anvil, |
| 166 | key: "anvil", |
| 167 | displayName: "Anvil", |
| 168 | passable: false, |
| 169 | lightRangeTiles: 0, |
| 170 | frames: 1, |
| 171 | framesPerRow: 1, |
| 172 | animationSpeed: 0, |
| 173 | craftingStationId: "anvil" |
| 174 | }), |
| 175 | [BlockType.woodworkingBench]: Object.freeze({ |
| 176 | id: BlockType.woodworkingBench, |
| 177 | key: "woodworking-bench", |
| 178 | displayName: "Woodworking bench", |
| 179 | passable: false, |
| 180 | lightRangeTiles: 0, |
| 181 | frames: 1, |
| 182 | framesPerRow: 1, |
| 183 | animationSpeed: 0, |
| 184 | craftingStationId: "woodworking-bench" |
| 185 | }), |
| 186 | [BlockType.craftingBench]: Object.freeze({ |
| 187 | id: BlockType.craftingBench, |
| 188 | key: "crafting-bench", |
| 189 | displayName: "Crafting bench", |
| 190 | passable: false, |
| 191 | lightRangeTiles: 0, |
| 192 | frames: 1, |
| 193 | framesPerRow: 1, |
| 194 | animationSpeed: 0, |
| 195 | craftingStationId: "crafting-bench" |
| 196 | }) |
| 197 | }); |
| 198 | |
| 199 | /** Grounds and real blocks the editor may place (excludes legacy/hidden entries). */ |
| 200 | export function listPlaceableGrounds () |
| 201 | { |
| 202 | return Object.values(GROUND_DEFINITIONS); |
| 203 | } |
| 204 | |
| 205 | export function listPlaceableBlocks () |
| 206 | { |
| 207 | return Object.values(BLOCK_DEFINITIONS).filter((def) => !def.editorHidden); |
| 208 | } |
| 209 | |
| 210 | /** Display name for an ore boulder state (`copper-ore` → "Copper ore boulder"). */ |
| 211 | export function oreBoulderDisplayName (state) |
| 212 | { |
| 213 | const material = materialFromOreBoulderState(state); |
| 214 | if (!material) |
| 215 | { return null; } |
| 216 | const label = MATERIAL_DISPLAY_NAME[material] ?? material; |
| 217 | return material === "coal" ? "Coal boulder" : `${label} ore boulder`; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Hover / UI label for a gatherable resource node. |
| 222 | * Ore boulders today; trees / fishing / herbs can extend this later. |
| 223 | * Returns null when the block is not an active resource node. |
| 224 | */ |
| 225 | export function resourceNodeDisplayName (block) |
| 226 | { |
| 227 | if (!block) |
| 228 | { return null; } |
| 229 | if (block.typeId === BlockType.boulder) |
| 230 | { return oreBoulderDisplayName(block.state); } |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Editor palette cards: one entry per placeable, with boulder expanded into |
| 236 | * plain rock + each ore/coal variant (shared BlockType, distinct state). |
| 237 | */ |
| 238 | export function listPlaceableBlockPaletteEntries (catalog = null) |
| 239 | { |
| 240 | const entries = []; |
| 241 | for (const def of listPlaceableBlocks()) |
| 242 | { |
| 243 | if (def.id === BlockType.boulder) |
| 244 | { |
| 245 | entries.push({ |
| 246 | typeId: def.id, |
| 247 | state: "boulder", |
| 248 | key: def.key, |
| 249 | displayName: "Boulder", |
| 250 | spriteName: spriteImageName("block", def.key, "boulder") |
| 251 | }); |
| 252 | for (const material of ORE_BEARING_MATERIALS) |
| 253 | { |
| 254 | const state = oreBoulderState(material); |
| 255 | entries.push({ |
| 256 | typeId: def.id, |
| 257 | state, |
| 258 | key: def.key, |
| 259 | displayName: oreBoulderDisplayName(state) ?? state, |
| 260 | spriteName: spriteImageName("block", def.key, state) |
| 261 | }); |
| 262 | } |
| 263 | continue; |
| 264 | } |
| 265 | const states = catalog?.blocks?.[def.key]?.states ?? []; |
| 266 | const state = resolveState(states, null); |
| 267 | entries.push({ |
| 268 | typeId: def.id, |
| 269 | state, |
| 270 | key: def.key, |
| 271 | displayName: def.displayName, |
| 272 | spriteName: state |
| 273 | ? spriteImageName("block", def.key, state) |
| 274 | : spriteImageName("block", def.key, def.key) |
| 275 | }); |
| 276 | } |
| 277 | return entries; |
| 278 | } |
| 279 | |
| 280 | export function groundDefinition (typeId) |
| 281 | { |
| 282 | return GROUND_DEFINITIONS[typeId] ?? null; |
| 283 | } |
| 284 | |
| 285 | export function blockDefinition (typeId) |
| 286 | { |
| 287 | return BLOCK_DEFINITIONS[typeId] ?? null; |
| 288 | } |
| 289 | |
| 290 | export function isGroundPassable (typeId) |
| 291 | { |
| 292 | const def = groundDefinition(typeId); |
| 293 | if (!def) |
| 294 | { return true; } |
| 295 | return def.passable !== false; |
| 296 | } |
| 297 | |
| 298 | export function isWaterGround (typeId) |
| 299 | { |
| 300 | return !!groundDefinition(typeId)?.water; |
| 301 | } |
| 302 | |
| 303 | export function isWadeGround (typeId) |
| 304 | { |
| 305 | return !!groundDefinition(typeId)?.wades; |
| 306 | } |
| 307 | |
| 308 | export function isBlockPassable (typeId) |
| 309 | { |
| 310 | if (typeId == null) |
| 311 | { return true; } |
| 312 | |
| 313 | const def = blockDefinition(typeId); |
| 314 | return def ? def.passable : false; |
| 315 | } |
| 316 | |
| 317 | export function blockLightRange (typeId) |
| 318 | { |
| 319 | const def = blockDefinition(typeId); |
| 320 | return def ? def.lightRangeTiles : 0; |
| 321 | } |
| 322 | |
| 323 | export function normalizeGround (value, fallbackTypeId = GroundType.grass) |
| 324 | { |
| 325 | if (typeof value === "number") |
| 326 | { return { typeId: value, state: null }; } |
| 327 | if (value && Number.isInteger(value.typeId)) |
| 328 | { return { typeId: value.typeId, state: value.state ?? null }; } |
| 329 | return { typeId: fallbackTypeId, state: null }; |
| 330 | } |
| 331 | |
| 332 | export function normalizeBlock (value) |
| 333 | { |
| 334 | if (!value || !Number.isInteger(value.typeId)) |
| 335 | { return null; } |
| 336 | return { typeId: value.typeId, state: value.state ?? null, config: value.config ?? null }; |
| 337 | } |
| 338 | |
| 339 | export function setBlockVisualState (block, state) |
| 340 | { |
| 341 | if (!block) |
| 342 | { return null; } |
| 343 | block.state = state ?? null; |
| 344 | return block; |
| 345 | } |