mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-04 22:46:06 +00:00
tiles now have lake or ocean method
This commit is contained in:
+4
-2
@@ -23,8 +23,8 @@ export interface ExecutionView {
|
||||
}
|
||||
|
||||
export interface Execution extends ExecutionView {
|
||||
init(mg: MutableGame, ticks: number)
|
||||
tick(ticks: number)
|
||||
init(mg: MutableGame, ticks: number): void
|
||||
tick(ticks: number): void
|
||||
owner(): MutablePlayer
|
||||
}
|
||||
|
||||
@@ -42,6 +42,8 @@ export interface Tile {
|
||||
isShore(): boolean
|
||||
isWater(): boolean
|
||||
isShorelineWater(): boolean
|
||||
isOcean(): boolean
|
||||
isLake(): boolean
|
||||
magnitude(): number
|
||||
owner(): Player | TerraNullius
|
||||
hasOwner(): boolean
|
||||
|
||||
@@ -19,6 +19,12 @@ class TileImpl implements Tile {
|
||||
private readonly _cell: Cell,
|
||||
private readonly _terrain: Terrain
|
||||
) { }
|
||||
isLake(): boolean {
|
||||
return !this.isLand() && !this.isOcean()
|
||||
}
|
||||
isOcean(): boolean {
|
||||
return this._terrain.ocean
|
||||
}
|
||||
magnitude(): number {
|
||||
return this._terrain.magnitude
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ export enum TerrainType {
|
||||
|
||||
export class Terrain {
|
||||
public shoreline: boolean = false
|
||||
public ocean: boolean = false
|
||||
public magnitude: number = 0
|
||||
constructor(public type: TerrainType) { }
|
||||
}
|
||||
@@ -55,10 +56,12 @@ export function loadTerrainMap(): TerrainMap {
|
||||
const packedByte = fileData.charCodeAt(4 + y * width + x); // +4 to skip dimension bytes
|
||||
const type = (packedByte & 0b10000000) ? TerrainType.Land : TerrainType.Water;
|
||||
const shoreline = !!(packedByte & 0b01000000);
|
||||
const magnitude = packedByte & 0b00111111;
|
||||
const ocean = !!(packedByte & 0b00100000);
|
||||
const magnitude = packedByte & 0b00011111;
|
||||
|
||||
terrain[x][y] = new Terrain(type);
|
||||
terrain[x][y].shoreline = shoreline;
|
||||
terrain[x][y].ocean = ocean;
|
||||
terrain[x][y].magnitude = magnitude;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
import {EventBus, GameEvent} from "./EventBus";
|
||||
import {Config} from "./configuration/Config";
|
||||
|
||||
export class TickEvent implements GameEvent {
|
||||
constructor(public readonly tickCount: number) { }
|
||||
}
|
||||
|
||||
export class Ticker {
|
||||
private ticker: NodeJS.Timeout;
|
||||
private tickCount: number;
|
||||
|
||||
constructor(private tickInterval: number, private eventBus: EventBus) {
|
||||
|
||||
}
|
||||
|
||||
start() {
|
||||
this.tickCount = 0;
|
||||
this.ticker = setInterval(() => this.tick(), this.tickInterval);
|
||||
}
|
||||
|
||||
stop() {
|
||||
clearInterval(this.ticker);
|
||||
}
|
||||
|
||||
private tick() {
|
||||
this.eventBus.emit(new TickEvent(this.tickCount))
|
||||
this.tickCount++;
|
||||
}
|
||||
|
||||
getTickCount(): number {
|
||||
return this.tickCount;
|
||||
}
|
||||
}
|
||||
+17
-1
@@ -1,4 +1,4 @@
|
||||
import {Cell} from "./Game";
|
||||
import {Cell, Tile} from "./Game";
|
||||
|
||||
export function manhattanDist(c1: Cell, c2: Cell): number {
|
||||
return Math.abs(c1.x - c2.x) + Math.abs(c1.y - c2.y);
|
||||
@@ -6,4 +6,20 @@ export function manhattanDist(c1: Cell, c2: Cell): number {
|
||||
|
||||
export function within(value: number, min: number, max: number): number {
|
||||
return Math.min(Math.max(value, min), max);
|
||||
}
|
||||
|
||||
export function bfs(tile: Tile, dist: number): Set<Tile> {
|
||||
const seen = new Set<Tile>
|
||||
const q: Tile[] = []
|
||||
q.push(tile)
|
||||
while (q.length > 0) {
|
||||
const curr = q.pop()
|
||||
seen.add(curr)
|
||||
for (const n of curr.neighbors()) {
|
||||
if (!seen.has(n) && manhattanDist(tile.cell(), n.cell()) <= dist) {
|
||||
q.push(n)
|
||||
}
|
||||
}
|
||||
}
|
||||
return seen
|
||||
}
|
||||
@@ -117,7 +117,7 @@ export class BoatAttackExecution implements Execution {
|
||||
}
|
||||
|
||||
private closestShoreTileToTarget(player: Player, target: Cell): Tile | null {
|
||||
const shoreTiles = Array.from(player.borderTiles()).filter(t => t.onShore())
|
||||
const shoreTiles = Array.from(player.borderTiles()).filter(t => t.onShore() && t.neighbors().filter(n => n.isOcean()).length > 0)
|
||||
if (shoreTiles.length == 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user