mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-23 05:39:38 +00:00
1ce282d41b
## Description: To simply the map binary data, remove height & width data to the manifest ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors ## Please put your Discord username so you can be contacted if a bug or regression is found: evan
84 lines
2.0 KiB
TypeScript
84 lines
2.0 KiB
TypeScript
import { GameMapType } from "./Game";
|
|
import { GameMap, GameMapImpl } from "./GameMap";
|
|
import { terrainMapFileLoader } from "./TerrainMapFileLoader";
|
|
|
|
export type TerrainMapData = {
|
|
manifest: MapManifest;
|
|
gameMap: GameMap;
|
|
miniGameMap: GameMap;
|
|
};
|
|
|
|
const loadedMaps = new Map<GameMapType, TerrainMapData>();
|
|
|
|
export interface MapMetadata {
|
|
width: number;
|
|
height: number;
|
|
num_land_tiles: number;
|
|
}
|
|
|
|
export interface MapManifest {
|
|
name: string;
|
|
map: MapMetadata;
|
|
mini_map: MapMetadata;
|
|
nations: Nation[];
|
|
}
|
|
|
|
export interface Nation {
|
|
coordinates: [number, number];
|
|
flag: string;
|
|
name: string;
|
|
strength: number;
|
|
}
|
|
|
|
export async function loadTerrainMap(
|
|
map: GameMapType,
|
|
): Promise<TerrainMapData> {
|
|
const cached = loadedMaps.get(map);
|
|
if (cached !== undefined) return cached;
|
|
const mapFiles = terrainMapFileLoader.getMapData(map);
|
|
|
|
const manifest = await mapFiles.manifest();
|
|
const gameMap = await genTerrainFromBin(
|
|
manifest.map,
|
|
await mapFiles.mapBin(),
|
|
);
|
|
const miniGameMap = await genTerrainFromBin(
|
|
manifest.mini_map,
|
|
await mapFiles.miniMapBin(),
|
|
);
|
|
const result = {
|
|
manifest: await mapFiles.manifest(),
|
|
gameMap: gameMap,
|
|
miniGameMap: miniGameMap,
|
|
};
|
|
loadedMaps.set(map, result);
|
|
return result;
|
|
}
|
|
|
|
export async function genTerrainFromBin(
|
|
mapData: MapMetadata,
|
|
data: string,
|
|
): Promise<GameMap> {
|
|
if (data.length !== mapData.width * mapData.height) {
|
|
throw new Error(
|
|
`Invalid data: buffer size ${data.length} incorrect for ${mapData.width}x${mapData.height} terrain plus 4 bytes for dimensions.`,
|
|
);
|
|
}
|
|
|
|
// Store raw data in Uint8Array
|
|
const rawData = new Uint8Array(mapData.width * mapData.height);
|
|
|
|
// Copy data starting after the header
|
|
for (let i = 0; i < mapData.width * mapData.height; i++) {
|
|
const packedByte = data.charCodeAt(i);
|
|
rawData[i] = packedByte;
|
|
}
|
|
|
|
return new GameMapImpl(
|
|
mapData.width,
|
|
mapData.height,
|
|
rawData,
|
|
mapData.num_land_tiles,
|
|
);
|
|
}
|