From 2216c34c41a85d1f8dd8b09e73768cdab6372fc4 Mon Sep 17 00:00:00 2001 From: Evan Date: Thu, 28 Nov 2024 08:55:57 -0800 Subject: [PATCH] create terrainsearchmap --- TODO.txt | 3 ++ src/core/configuration/DevConfig.ts | 2 +- src/core/game/Game.ts | 1 + src/core/game/GameImpl.ts | 6 +++ src/core/game/TerrainMapLoader.ts | 10 ++++- src/core/game/TerrainSearchMap.ts | 64 +++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/core/game/TerrainSearchMap.ts diff --git a/TODO.txt b/TODO.txt index 1816332c4..8f0921ab7 100644 --- a/TODO.txt +++ b/TODO.txt @@ -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 diff --git a/src/core/configuration/DevConfig.ts b/src/core/configuration/DevConfig.ts index ce09d8320..f7557411d 100644 --- a/src/core/configuration/DevConfig.ts +++ b/src/core/configuration/DevConfig.ts @@ -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 } diff --git a/src/core/game/Game.ts b/src/core/game/Game.ts index c8105a327..39d2e23b7 100644 --- a/src/core/game/Game.ts +++ b/src/core/game/Game.ts @@ -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 { diff --git a/src/core/game/GameImpl.ts b/src/core/game/GameImpl.ts index a93d2c61c..b26d45655 100644 --- a/src/core/game/GameImpl.ts +++ b/src/core/game/GameImpl.ts @@ -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)) } diff --git a/src/core/game/TerrainMapLoader.ts b/src/core/game/TerrainMapLoader.ts index 85bde19be..0b0fb794d 100644 --- a/src/core/game/TerrainMapLoader.ts +++ b/src/core/game/TerrainMapLoader.ts @@ -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 { 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 } diff --git a/src/core/game/TerrainSearchMap.ts b/src/core/game/TerrainSearchMap.ts new file mode 100644 index 000000000..4be01d053 --- /dev/null +++ b/src/core/game/TerrainSearchMap.ts @@ -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; + } +} \ No newline at end of file