Files
OpenFrontIO/src/core/game/TerrainMapLoader.ts
T
evanpelle 5d9b62da4d add compact map option (#2095)
## Description:

Create mini map option
<img width="741" height="234" alt="Screenshot 2025-09-25 at 4 47 47 PM"
src="https://github.com/user-attachments/assets/6c442698-8e3b-44d5-b07e-c4f0a916c3bc"
/>

## 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

## Please put your Discord username so you can be contacted if a bug or
regression is found:

evan
2025-09-25 16:51:25 -07:00

92 lines
2.3 KiB
TypeScript

import { GameMapSize, GameMapType } from "./Game";
import { GameMap, GameMapImpl } from "./GameMap";
import { GameMapLoader } from "./GameMapLoader";
export type TerrainMapData = {
nations: Nation[];
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;
map4x: MapMetadata;
map16x: MapMetadata;
nations: Nation[];
}
export interface Nation {
coordinates: [number, number];
flag: string;
name: string;
strength: number;
}
export async function loadTerrainMap(
map: GameMapType,
mapSize: GameMapSize,
terrainMapFileLoader: GameMapLoader,
): Promise<TerrainMapData> {
const cached = loadedMaps.get(map);
if (cached !== undefined) return cached;
const mapFiles = terrainMapFileLoader.getMapData(map);
const manifest = await mapFiles.manifest();
const gameMap =
mapSize === GameMapSize.Normal
? await genTerrainFromBin(manifest.map, await mapFiles.mapBin())
: await genTerrainFromBin(manifest.map4x, await mapFiles.map4xBin());
const miniMap =
mapSize === GameMapSize.Normal
? await genTerrainFromBin(
mapSize === GameMapSize.Normal ? manifest.map4x : manifest.map16x,
await mapFiles.map4xBin(),
)
: await genTerrainFromBin(manifest.map16x, await mapFiles.map16xBin());
if (mapSize === GameMapSize.Compact) {
manifest.nations.forEach((nation) => {
nation.coordinates = [
Math.floor(nation.coordinates[0] / 2),
Math.floor(nation.coordinates[1] / 2),
];
});
}
const result = {
nations: manifest.nations,
gameMap: gameMap,
miniGameMap: miniMap,
};
loadedMaps.set(map, result);
return result;
}
export async function genTerrainFromBin(
mapData: MapMetadata,
data: Uint8Array,
): 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.`,
);
}
return new GameMapImpl(
mapData.width,
mapData.height,
data,
mapData.num_land_tiles,
);
}