create terrainsearchmap

This commit is contained in:
Evan
2024-11-28 08:55:57 -08:00
parent 61515172c7
commit 2216c34c41
6 changed files with 83 additions and 3 deletions
+3
View File
@@ -198,6 +198,9 @@
* make ports cost more for more ports DONE 11/25/2024
* add battleship DONE 11/26/2024
* use drawRect() instead of putImageData DONE 11/26/2024
* run a* in a web worker
* have NPCs build destroyers and battleships
* spread out calculate clusters
* add radiation from nuke
* NPC has relations
* add defense post
+1 -1
View File
@@ -5,7 +5,7 @@ export const devConfig = new class extends DefaultConfig {
unitInfo(type: UnitType): UnitInfo {
const info = super.unitInfo(type)
const oldCost = info.cost
info.cost = (p: Player) => oldCost(p) / 10
info.cost = (p: Player) => oldCost(p) / 1000000
return info
}
+1
View File
@@ -279,6 +279,7 @@ export interface Game {
displayMessage(message: string, type: MessageType, playerID: PlayerID | null): void
units(...types: UnitType[]): Unit[]
unitInfo(type: UnitType): UnitInfo
searchMap(): SharedArrayBuffer
}
export interface MutableGame extends Game {
+6
View File
@@ -34,6 +34,7 @@ export class GameImpl implements MutableGame {
private _height: number
private _numLandTiles: number
_terraNullius: TerraNulliusImpl
private _searchMap: SharedArrayBuffer
allianceRequests: AllianceRequestImpl[] = []
alliances_: AllianceImpl[] = []
@@ -43,6 +44,7 @@ export class GameImpl implements MutableGame {
this._width = terrainMap.width();
this._height = terrainMap.height();
this._numLandTiles = terrainMap.numLandTiles
this._searchMap = terrainMap.searchBuffer
this.map = new Array(this._width);
for (let x = 0; x < this._width; x++) {
this.map[x] = new Array(this._height);
@@ -394,6 +396,10 @@ export class GameImpl implements MutableGame {
this.eventBus.emit(new AllianceExpiredEvent(alliance.requestor(), alliance.recipient()))
}
public searchMap(): SharedArrayBuffer {
return this._searchMap
}
displayMessage(message: string, type: MessageType, playerID: PlayerID | null): void {
this.eventBus.emit(new DisplayMessageEvent(message, type, playerID))
}
+8 -2
View File
@@ -33,7 +33,8 @@ export class TerrainMap {
constructor(
public readonly tiles: Terrain[][],
public readonly numLandTiles: number,
public readonly nationMap: NationMap
public readonly nationMap: NationMap,
public searchBuffer: SharedArrayBuffer
) { }
terrain(cell: Cell): Terrain {
@@ -121,7 +122,12 @@ export async function loadTerrainMap(map: GameMap): Promise<TerrainMap> {
terrain[x][y].land = land
}
}
const m = new TerrainMap(terrain, numLand, mapData.info);
const encoder = new TextEncoder();
const encoded = encoder.encode(fileData);
const buffer = new SharedArrayBuffer(encoded.length);
const view = new Uint8Array(buffer);
view.set(encoded)
const m = new TerrainMap(terrain, numLand, mapData.info, buffer);
loadedMaps.set(map, m)
return m
}
+64
View File
@@ -0,0 +1,64 @@
import { TerrainMap } from "./TerrainMapLoader";
export enum SearchMapTileType {
Land,
Shore,
Water,
}
export class TerrainSearchMap {
private width: number;
private height: number;
private mapData: Uint8Array;
constructor(buffer: SharedArrayBuffer) {
this.mapData = new Uint8Array(buffer);
this.width = (this.mapData[1] << 8) | this.mapData[0];
this.height = (this.mapData[3] << 8) | this.mapData[2];
}
node(x: number, y: number): SearchMapTileType {
const packedByte = this.mapData[4 + y * this.width + x];
const isLand = (packedByte & 0b10000000)
const shoreline = !!(packedByte & 0b01000000);
const ocean = !!(packedByte & 0b00100000);
const magnitude = packedByte & 0b00011111;
if (isLand) {
return SearchMapTileType.Land
}
if (magnitude < 10) {
return SearchMapTileType.Shore
}
return SearchMapTileType.Water
}
neighbors(x: number, y: number): Array<{ x: number; y: number }> {
const result: Array<{ x: number; y: number }> = [];
// Check all 8 adjacent tiles
const dirs = [
[-1, -1], [0, -1], [1, -1],
[-1, 0], [1, 0],
[-1, 1], [0, 1], [1, 1]
];
for (const [dx, dy] of dirs) {
const newX = x + dx;
const newY = y + dy;
// Check bounds
if (newX >= 0 && newX < this.width &&
newY >= 0 && newY < this.height) {
result.push({ x: newX, y: newY });
}
}
return result;
}
getWidth(): number {
return this.width;
}
getHeight(): number {
return this.height;
}
}