mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 14:10:45 +00:00
create terrainsearchmap
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user