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