--- a/client/webapp/js/engine/tile_grid_loader.js +++ b/client/webapp/js/engine/tile_grid_loader.js @@ -3,2 +3,7 @@ // Exposes window.currentMapGrid, window.currentMapWidth, window.currentMapHeight, and window.getTileAt(tx, ty) +const root = typeof window !== "undefined" ? window : (typeof globalThis !== "undefined" ? globalThis : global); +if (typeof window === "undefined") { + root.window = root; +} + @@ -40,2 +45,9 @@ + const gridLength = width * height; + const minExpectedLength = 16 + poiCount * 3 + encounterCount * 5 + gridLength; + if (byteBuf.length < minExpectedLength) { + console.error("[TileGridLoader] Buffer truncated:", byteBuf.length, "needed:", minExpectedLength); + return null; + } + @@ -64,7 +76,2 @@ - const gridLength = width * height; - if (offset + gridLength > byteBuf.length) { - console.error("[TileGridLoader] Buffer truncated for grid:", byteBuf.length, "needed:", offset + gridLength); - return null; - } - const grid = byteBuf.subarray(offset, offset + gridLength); - window.currentMapGrid = grid; - window.currentMapWidth = width; - window.currentMapHeight = height; - window.currentMapMetadata = { + root.currentMapGrid = grid; + root.currentMapWidth = width; + root.currentMapHeight = height; + root.currentMapMetadata = { @@ -88,4 +95,4 @@ const grid = new Uint8Array(width * height); grid.fill(defaultTile); - window.currentMapGrid = grid; - window.currentMapWidth = width; - window.currentMapHeight = height; - window.currentMapMetadata = { + root.currentMapGrid = grid; + root.currentMapWidth = width; + root.currentMapHeight = height; + root.currentMapMetadata = { @@ -106,4 +113,4 @@ - const w = window.currentMapWidth || 0; - const h = window.currentMapHeight || 0; - const grid = window.currentMapGrid; + const w = root.currentMapWidth || 0; + const h = root.currentMapHeight || 0; + const grid = root.currentMapGrid; const ix = Math.floor(tx); const iy = Math.floor(ty); - if (!grid || ix < 0 || ix >= w || iy < 0 || iy >= h) { + if (!grid || !(ix >= 0 && ix < w && iy >= 0 && iy < h)) { return 2; // TileType.WALL } @@ -118,3 +125,3 @@ - const w = window.currentMapWidth || 0; - const h = window.currentMapHeight || 0; - const grid = window.currentMapGrid; + const w = root.currentMapWidth || 0; + const h = root.currentMapHeight || 0; + const grid = root.currentMapGrid; @@ -131,3 +138,3 @@ -window.TileGridLoader = TileGridLoader; -window.getTileAt = TileGridLoader.getTileAt; -window.setTileAt = TileGridLoader.setTileAt; +root.TileGridLoader = TileGridLoader; +root.getTileAt = TileGridLoader.getTileAt; +root.setTileAt = TileGridLoader.setTileAt;