| 1 | /** |
| 2 | * Level pack format for raytracer + editor. |
| 3 | * v1: walls/floors/ceilings/npcs/lights[] (empty reserved) |
| 4 | * v2: lights fully specified + per-level ambient; migrate v1→v2. |
| 5 | */ |
| 6 | var LevelFormat = |
| 7 | { |
| 8 | CURRENT_VERSION: 2, |
| 9 | MAX_SIZE: 128, |
| 10 | MAX_LEVELS: 64, |
| 11 | MAX_LIGHTS: 32, |
| 12 | KNOWN_TEXTURES: ["stone", "wood"], |
| 13 | DEFAULT_AMBIENT: 0.38, |
| 14 | |
| 15 | emptyLight: function (x, y) |
| 16 | { |
| 17 | return { |
| 18 | id: "light-" + Date.now().toString(36), |
| 19 | x: x == null ? 48 : x, |
| 20 | y: y == null ? 48 : y, |
| 21 | z: 20, |
| 22 | color: [1, 0.9, 0.7], |
| 23 | intensity: 1.2, |
| 24 | range: 96, |
| 25 | falloff: 2 |
| 26 | }; |
| 27 | }, |
| 28 | |
| 29 | emptyLevel: function (name, width, height) |
| 30 | { |
| 31 | width = Math.max(3, Math.min(this.MAX_SIZE, width || 10)); |
| 32 | height = Math.max(3, Math.min(this.MAX_SIZE, height || 10)); |
| 33 | var n = width * height; |
| 34 | var walls = []; |
| 35 | var floors = []; |
| 36 | var ceilings = []; |
| 37 | for (var i = 0; i < n; i++) |
| 38 | { |
| 39 | var x = i % width; |
| 40 | var y = Math.floor(i / width); |
| 41 | var border = x === 0 || y === 0 || x === width - 1 || y === height - 1; |
| 42 | walls.push(border ? 1 : 0); |
| 43 | floors.push(border ? null : "stone"); |
| 44 | ceilings.push(border ? null : "stone"); |
| 45 | } |
| 46 | return { |
| 47 | id: "level-" + Date.now().toString(36), |
| 48 | name: name || "Nieuw level", |
| 49 | width: width, |
| 50 | height: height, |
| 51 | cellSize: 32, |
| 52 | ambient: this.DEFAULT_AMBIENT, |
| 53 | spawn: { tileX: 1, tileY: 1, yawRad: 0 }, |
| 54 | walls: walls, |
| 55 | floors: floors, |
| 56 | ceilings: ceilings, |
| 57 | npcs: [], |
| 58 | lights: [] |
| 59 | }; |
| 60 | }, |
| 61 | |
| 62 | defaultPack: function () |
| 63 | { |
| 64 | return { |
| 65 | format_version: this.CURRENT_VERSION, |
| 66 | textures: [ |
| 67 | { id: "stone", url: "images/stone.png" }, |
| 68 | { id: "wood", url: "images/wood.png" } |
| 69 | ], |
| 70 | levels: [] |
| 71 | }; |
| 72 | }, |
| 73 | |
| 74 | fromLegacyLevels: function (legacyArr, npcsByLevel) |
| 75 | { |
| 76 | var pack = this.defaultPack(); |
| 77 | for (var i = 0; i < (legacyArr || []).length; i++) |
| 78 | { |
| 79 | var L = legacyArr[i]; |
| 80 | var n = L.width * L.height; |
| 81 | var floors = []; |
| 82 | var ceilings = []; |
| 83 | for (var t = 0; t < n; t++) |
| 84 | { |
| 85 | var solid = L.tiles[t] === 1 || L.tiles[t] === 3; |
| 86 | floors.push(solid ? null : "stone"); |
| 87 | ceilings.push(solid ? null : "stone"); |
| 88 | } |
| 89 | pack.levels.push({ |
| 90 | id: "level-" + (i + 1), |
| 91 | name: L.name || ("Level " + (i + 1)), |
| 92 | width: L.width, |
| 93 | height: L.height, |
| 94 | cellSize: 32, |
| 95 | ambient: this.DEFAULT_AMBIENT, |
| 96 | spawn: { tileX: L.spawnX, tileY: L.spawnY, yawRad: 0 }, |
| 97 | walls: L.tiles.slice(), |
| 98 | floors: floors, |
| 99 | ceilings: ceilings, |
| 100 | npcs: (npcsByLevel && npcsByLevel[i]) || [], |
| 101 | lights: [] |
| 102 | }); |
| 103 | } |
| 104 | return pack; |
| 105 | }, |
| 106 | |
| 107 | migratePack: function (pack) |
| 108 | { |
| 109 | if (!pack || typeof pack !== "object") |
| 110 | { throw new Error("pack: root moet een object zijn"); } |
| 111 | var ver = pack.format_version; |
| 112 | if (ver === this.CURRENT_VERSION) |
| 113 | { return pack; } |
| 114 | if (ver === 1) |
| 115 | { |
| 116 | pack.format_version = 2; |
| 117 | for (var i = 0; i < (pack.levels || []).length; i++) |
| 118 | { |
| 119 | var L = pack.levels[i]; |
| 120 | if (typeof L.ambient !== "number") |
| 121 | { L.ambient = this.DEFAULT_AMBIENT; } |
| 122 | if (!Array.isArray(L.lights)) |
| 123 | { L.lights = []; } |
| 124 | for (var li = 0; li < L.lights.length; li++) |
| 125 | { this.normalizeLight(L.lights[li]); } |
| 126 | } |
| 127 | return pack; |
| 128 | } |
| 129 | throw new Error("pack: format_version " + ver + " niet ondersteund (wel: 1..2)"); |
| 130 | }, |
| 131 | |
| 132 | normalizeLight: function (light) |
| 133 | { |
| 134 | if (!light || typeof light !== "object") |
| 135 | { throw new Error("light: moet een object zijn"); } |
| 136 | if (typeof light.x !== "number" || typeof light.y !== "number") |
| 137 | { throw new Error("light: x/y verplicht"); } |
| 138 | if (typeof light.z !== "number") |
| 139 | { light.z = 20; } |
| 140 | if (!Array.isArray(light.color) || light.color.length < 3) |
| 141 | { light.color = [1, 0.9, 0.7]; } |
| 142 | for (var c = 0; c < 3; c++) |
| 143 | { |
| 144 | var v = Number(light.color[c]); |
| 145 | if (!(v >= 0)) |
| 146 | { v = 0; } |
| 147 | // allow 0–255 UI values |
| 148 | if (v > 1) |
| 149 | { v = v / 255; } |
| 150 | light.color[c] = v; |
| 151 | } |
| 152 | if (typeof light.intensity !== "number") |
| 153 | { light.intensity = 1; } |
| 154 | if (typeof light.range !== "number" || light.range <= 0) |
| 155 | { light.range = 96; } |
| 156 | if (typeof light.falloff !== "number") |
| 157 | { light.falloff = 2; } |
| 158 | if (!light.id || typeof light.id !== "string") |
| 159 | { light.id = "light-" + Math.floor(light.x) + "-" + Math.floor(light.y); } |
| 160 | return light; |
| 161 | }, |
| 162 | |
| 163 | validateTextureId: function (tex, allowNull) |
| 164 | { |
| 165 | if (tex == null || tex === "") |
| 166 | { return !!allowNull; } |
| 167 | return this.KNOWN_TEXTURES.indexOf(tex) >= 0; |
| 168 | }, |
| 169 | |
| 170 | validateLight: function (light, index, levelPrefix) |
| 171 | { |
| 172 | var prefix = (levelPrefix || "level") + ".lights[" + (index == null ? "?" : index) + "]"; |
| 173 | try |
| 174 | { this.normalizeLight(light); } |
| 175 | catch (e) |
| 176 | { throw new Error(prefix + ": " + e.message); } |
| 177 | if (light.intensity < 0 || light.intensity > 20) |
| 178 | { throw new Error(prefix + ": intensity 0..20"); } |
| 179 | if (light.range > 2048) |
| 180 | { throw new Error(prefix + ": range te groot"); } |
| 181 | if (light.falloff < 0.5 || light.falloff > 8) |
| 182 | { throw new Error(prefix + ": falloff 0.5..8"); } |
| 183 | return light; |
| 184 | }, |
| 185 | |
| 186 | validateLevel: function (level, index) |
| 187 | { |
| 188 | var prefix = "level[" + (index == null ? "?" : index) + "]"; |
| 189 | if (!level || typeof level !== "object") |
| 190 | { throw new Error(prefix + ": moet een object zijn"); } |
| 191 | if (!level.id || typeof level.id !== "string") |
| 192 | { throw new Error(prefix + ": id verplicht"); } |
| 193 | if (!level.name || typeof level.name !== "string") |
| 194 | { throw new Error(prefix + ": name verplicht"); } |
| 195 | var w = level.width | 0; |
| 196 | var h = level.height | 0; |
| 197 | if (w < 3 || h < 3 || w > this.MAX_SIZE || h > this.MAX_SIZE) |
| 198 | { throw new Error(prefix + ": width/height 3.." + this.MAX_SIZE); } |
| 199 | var n = w * h; |
| 200 | if (!Array.isArray(level.walls) || level.walls.length !== n) |
| 201 | { throw new Error(prefix + ": walls lengte ≠ width*height"); } |
| 202 | if (!Array.isArray(level.floors) || level.floors.length !== n) |
| 203 | { throw new Error(prefix + ": floors lengte ≠ width*height"); } |
| 204 | if (!Array.isArray(level.ceilings) || level.ceilings.length !== n) |
| 205 | { throw new Error(prefix + ": ceilings lengte ≠ width*height"); } |
| 206 | for (var i = 0; i < n; i++) |
| 207 | { |
| 208 | var wall = level.walls[i]; |
| 209 | if (typeof wall !== "number" || wall !== (wall | 0) || wall < 0 || wall > 3) |
| 210 | { throw new Error(prefix + ": walls[" + i + "] ongeldig"); } |
| 211 | if (!this.validateTextureId(level.floors[i], true)) |
| 212 | { throw new Error(prefix + ": floors[" + i + "] onbekende textuur"); } |
| 213 | if (!this.validateTextureId(level.ceilings[i], true)) |
| 214 | { throw new Error(prefix + ": ceilings[" + i + "] onbekende textuur"); } |
| 215 | } |
| 216 | if (!level.spawn || typeof level.spawn.tileX !== "number" || typeof level.spawn.tileY !== "number") |
| 217 | { throw new Error(prefix + ": spawn.tileX/tileY verplicht"); } |
| 218 | if (level.spawn.tileX < 0 || level.spawn.tileY < 0 || level.spawn.tileX >= w || level.spawn.tileY >= h) |
| 219 | { throw new Error(prefix + ": spawn buiten de map"); } |
| 220 | if (typeof level.spawn.yawRad !== "number") |
| 221 | { level.spawn.yawRad = 0; } |
| 222 | if (!Array.isArray(level.npcs)) |
| 223 | { throw new Error(prefix + ": npcs moet een array zijn"); } |
| 224 | if (!Array.isArray(level.lights)) |
| 225 | { level.lights = []; } |
| 226 | if (level.lights.length > this.MAX_LIGHTS) |
| 227 | { throw new Error(prefix + ": te veel lights"); } |
| 228 | for (var li = 0; li < level.lights.length; li++) |
| 229 | { this.validateLight(level.lights[li], li, prefix); } |
| 230 | if (typeof level.ambient !== "number") |
| 231 | { level.ambient = this.DEFAULT_AMBIENT; } |
| 232 | if (level.ambient < 0 || level.ambient > 1) |
| 233 | { throw new Error(prefix + ": ambient 0..1"); } |
| 234 | if (level.cellSize == null) |
| 235 | { level.cellSize = 32; } |
| 236 | return level; |
| 237 | }, |
| 238 | |
| 239 | validatePack: function (pack) |
| 240 | { |
| 241 | pack = this.migratePack(pack); |
| 242 | if (pack.format_version !== this.CURRENT_VERSION) |
| 243 | { throw new Error("pack: format_version " + pack.format_version + " niet ondersteund (wel: " + this.CURRENT_VERSION + ")"); } |
| 244 | if (!Array.isArray(pack.levels)) |
| 245 | { throw new Error("pack: levels moet een array zijn"); } |
| 246 | if (pack.levels.length > this.MAX_LEVELS) |
| 247 | { throw new Error("pack: te veel levels"); } |
| 248 | if (pack.levels.length < 1) |
| 249 | { throw new Error("pack: minstens één level"); } |
| 250 | var ids = {}; |
| 251 | for (var i = 0; i < pack.levels.length; i++) |
| 252 | { |
| 253 | this.validateLevel(pack.levels[i], i); |
| 254 | if (ids[pack.levels[i].id]) |
| 255 | { throw new Error("pack: dubbele level-id " + pack.levels[i].id); } |
| 256 | ids[pack.levels[i].id] = true; |
| 257 | } |
| 258 | if (!Array.isArray(pack.textures)) |
| 259 | { pack.textures = this.defaultPack().textures; } |
| 260 | return pack; |
| 261 | }, |
| 262 | |
| 263 | parse: function (jsonText) |
| 264 | { |
| 265 | var doc; |
| 266 | try |
| 267 | { doc = typeof jsonText === "string" ? JSON.parse(jsonText) : jsonText; } |
| 268 | catch (e) |
| 269 | { throw new Error("levels.json: ongeldige JSON (" + e.message + ")"); } |
| 270 | return this.validatePack(doc); |
| 271 | }, |
| 272 | |
| 273 | toRuntimeLevel: function (level) |
| 274 | { |
| 275 | return { |
| 276 | id: level.id, |
| 277 | name: level.name, |
| 278 | width: level.width, |
| 279 | height: level.height, |
| 280 | cellSize: level.cellSize || 32, |
| 281 | ambient: typeof level.ambient === "number" ? level.ambient : this.DEFAULT_AMBIENT, |
| 282 | spawnX: level.spawn.tileX, |
| 283 | spawnY: level.spawn.tileY, |
| 284 | spawnYaw: level.spawn.yawRad || 0, |
| 285 | tiles: level.walls, |
| 286 | floors: level.floors, |
| 287 | ceilings: level.ceilings, |
| 288 | npcs: level.npcs || [], |
| 289 | lights: level.lights || [] |
| 290 | }; |
| 291 | }, |
| 292 | |
| 293 | findLevelIndex: function (pack, query) |
| 294 | { |
| 295 | if (query == null || query === "") |
| 296 | { return 0; } |
| 297 | var asNum = Number(query); |
| 298 | if (!isNaN(asNum) && asNum === (asNum | 0) && asNum >= 0 && asNum < pack.levels.length) |
| 299 | { return asNum; } |
| 300 | for (var i = 0; i < pack.levels.length; i++) |
| 301 | { |
| 302 | if (pack.levels[i].id === query || pack.levels[i].name === query) |
| 303 | { return i; } |
| 304 | } |
| 305 | return -1; |
| 306 | }, |
| 307 | |
| 308 | cloneLevel: function (level) |
| 309 | { |
| 310 | return JSON.parse(JSON.stringify(level)); |
| 311 | } |
| 312 | }; |
| 313 | |
| 314 | if (typeof module !== "undefined" && module.exports) |
| 315 | { module.exports = LevelFormat; } |