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