mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-24 05:56:41 +00:00
put methods onto terraintile
This commit is contained in:
+10
-12
@@ -162,7 +162,15 @@ export interface TerrainMap {
|
||||
}
|
||||
|
||||
export interface TerrainTile extends SearchNode {
|
||||
terrainType(): TerrainType
|
||||
isLand(): boolean
|
||||
isShore(): boolean
|
||||
isOceanShore(): boolean
|
||||
isWater(): boolean
|
||||
isShorelineWater(): boolean
|
||||
isOcean(): boolean
|
||||
isLake(): boolean
|
||||
type(): TerrainType
|
||||
magnitude(): number
|
||||
}
|
||||
|
||||
export interface DefenseBonus {
|
||||
@@ -173,15 +181,6 @@ export interface DefenseBonus {
|
||||
}
|
||||
|
||||
export interface Tile extends SearchNode {
|
||||
isLand(): boolean
|
||||
isShore(): boolean
|
||||
isOceanShore(): boolean
|
||||
isWater(): boolean
|
||||
isShorelineWater(): boolean
|
||||
isOcean(): boolean
|
||||
isLake(): boolean
|
||||
terrain(): TerrainType
|
||||
magnitude(): number
|
||||
owner(): Player | TerraNullius
|
||||
hasOwner(): boolean
|
||||
isBorder(): boolean
|
||||
@@ -190,12 +189,11 @@ export interface Tile extends SearchNode {
|
||||
cell(): Cell
|
||||
neighbors(): Tile[]
|
||||
neighborsWrapped(): Tile[]
|
||||
onShore(): boolean
|
||||
|
||||
defenseBonuses(): DefenseBonus[]
|
||||
// defense bonus against this player
|
||||
defenseBonus(player: Player): number
|
||||
hasFallout(): boolean
|
||||
terrain(): TerrainTile
|
||||
}
|
||||
|
||||
export interface Unit {
|
||||
|
||||
@@ -342,7 +342,7 @@ export class GameImpl implements MutableGame {
|
||||
if (!tile.hasOwner()) {
|
||||
throw new Error(`Cannot relinquish tile because it is unowned: cell ${tile.cell().toString()}`)
|
||||
}
|
||||
if (tile.isWater()) {
|
||||
if (tile.terrain().isWater()) {
|
||||
throw new Error("Cannot relinquish water")
|
||||
}
|
||||
|
||||
|
||||
@@ -110,7 +110,7 @@ export class PlayerImpl implements MutablePlayer {
|
||||
const ns: Set<(MutablePlayer | TerraNullius)> = new Set();
|
||||
for (const border of this.borderTiles()) {
|
||||
for (const neighbor of border.neighbors()) {
|
||||
if (neighbor.isLand() && neighbor.owner() != this) {
|
||||
if (neighbor.terrain().isLand() && neighbor.owner() != this) {
|
||||
ns.add((neighbor as TileImpl)._owner);
|
||||
}
|
||||
}
|
||||
@@ -450,7 +450,7 @@ export class PlayerImpl implements MutablePlayer {
|
||||
|
||||
portSpawn(tile: Tile): Tile | false {
|
||||
const spawns = Array.from(bfs(tile, dist(tile, 20)))
|
||||
.filter(t => t.owner() == this && t.isOceanShore())
|
||||
.filter(t => t.owner() == this && t.terrain().isOceanShore())
|
||||
.sort((a, b) => manhattanDist(a.cell(), tile.cell()) - manhattanDist(b.cell(), tile.cell()))
|
||||
if (spawns.length == 0) {
|
||||
return false
|
||||
@@ -459,7 +459,7 @@ export class PlayerImpl implements MutablePlayer {
|
||||
}
|
||||
|
||||
warshipSpawn(tile: Tile): Tile | false {
|
||||
if (!tile.isOcean()) {
|
||||
if (!tile.terrain().isOcean()) {
|
||||
return false
|
||||
}
|
||||
const spawns = this.units(UnitType.Port)
|
||||
@@ -479,7 +479,7 @@ export class PlayerImpl implements MutablePlayer {
|
||||
}
|
||||
|
||||
transportShipSpawn(targetTile: Tile): Tile | false {
|
||||
if (!targetTile.isOceanShore()) {
|
||||
if (!targetTile.terrain().isOceanShore()) {
|
||||
return false
|
||||
}
|
||||
const spawn = closestOceanShoreFromPlayer(this, targetTile, this.gs.width())
|
||||
|
||||
@@ -20,19 +20,41 @@ export interface Nation {
|
||||
|
||||
export class TerrainTileImpl implements TerrainTile {
|
||||
public shoreline: boolean = false
|
||||
public magnitude: number = 0
|
||||
public _magnitude: number = 0
|
||||
public ocean = false
|
||||
public land = false
|
||||
private _neighbors: TerrainTile[] | null = null
|
||||
|
||||
constructor(private map: TerrainMap, public type: TerrainType, private _cell: Cell) { }
|
||||
|
||||
terrainType(): TerrainType {
|
||||
return this.type
|
||||
constructor(private map: TerrainMap, public _type: TerrainType, private _cell: Cell) { }
|
||||
type(): TerrainType {
|
||||
return this._type
|
||||
}
|
||||
isLake(): boolean {
|
||||
return !this.isLand() && !this.isOcean();
|
||||
}
|
||||
isOcean(): boolean {
|
||||
return this.ocean;
|
||||
}
|
||||
magnitude(): number {
|
||||
return this._magnitude;
|
||||
}
|
||||
isShore(): boolean {
|
||||
return this.isLand() && this.shoreline;
|
||||
}
|
||||
isOceanShore(): boolean {
|
||||
return this.isShore() && this.neighbors().filter(n => n.isOcean()).length > -1;
|
||||
}
|
||||
isShorelineWater(): boolean {
|
||||
return this.isWater() && this.shoreline;
|
||||
}
|
||||
isLand(): boolean {
|
||||
return this.land;
|
||||
}
|
||||
isWater(): boolean {
|
||||
return !this.land;
|
||||
}
|
||||
|
||||
cost(): number {
|
||||
return this.magnitude < 10 ? 2 : 1
|
||||
return this._magnitude < 10 ? 2 : 1
|
||||
}
|
||||
|
||||
cell(): Cell {
|
||||
@@ -146,7 +168,7 @@ export async function loadTerrainFromFile(fileData: string): Promise<TerrainMapI
|
||||
|
||||
terrain[x][y] = new TerrainTileImpl(m, type, new Cell(x, y));
|
||||
terrain[x][y].shoreline = shoreline;
|
||||
terrain[x][y].magnitude = magnitude;
|
||||
terrain[x][y]._magnitude = magnitude;
|
||||
terrain[x][y].ocean = ocean
|
||||
terrain[x][y].land = land
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ export class TileImpl implements Tile {
|
||||
return this._hasFallout
|
||||
}
|
||||
|
||||
terrainType(): TerrainType {
|
||||
return this._terrain.type
|
||||
type(): TerrainType {
|
||||
return this._terrain._type
|
||||
}
|
||||
|
||||
defenseBonus(player: Player): number {
|
||||
@@ -77,32 +77,8 @@ export class TileImpl implements Tile {
|
||||
}
|
||||
return ns;
|
||||
}
|
||||
isLake(): boolean {
|
||||
return !this.isLand() && !this.isOcean();
|
||||
}
|
||||
isOcean(): boolean {
|
||||
return this._terrain.ocean;
|
||||
}
|
||||
magnitude(): number {
|
||||
return this._terrain.magnitude;
|
||||
}
|
||||
isShore(): boolean {
|
||||
return this.isLand() && this._terrain.shoreline;
|
||||
}
|
||||
isOceanShore(): boolean {
|
||||
return this.isShore() && this.neighbors().filter(n => n.isOcean()).length > 0;
|
||||
}
|
||||
isShorelineWater(): boolean {
|
||||
return this.isWater() && this._terrain.shoreline;
|
||||
}
|
||||
isLand(): boolean {
|
||||
return this._terrain.land;
|
||||
}
|
||||
isWater(): boolean {
|
||||
return !this._terrain.land;
|
||||
}
|
||||
terrain(): TerrainType {
|
||||
return this._terrain.type;
|
||||
terrain(): TerrainTile {
|
||||
return this._terrain
|
||||
}
|
||||
|
||||
borders(other: Player | TerraNullius): boolean {
|
||||
@@ -114,12 +90,6 @@ export class TileImpl implements Tile {
|
||||
return false;
|
||||
}
|
||||
|
||||
onShore(): boolean {
|
||||
return this.neighbors()
|
||||
.filter(t => t.isWater())
|
||||
.length > 0;
|
||||
}
|
||||
|
||||
hasOwner(): boolean { return this._owner != this.gs._terraNullius; }
|
||||
owner(): MutablePlayer | TerraNullius { return this._owner; }
|
||||
isBorder(): boolean { return this._isBorder; }
|
||||
@@ -140,6 +110,6 @@ export class TileImpl implements Tile {
|
||||
}
|
||||
|
||||
cost(): number {
|
||||
return this.magnitude() < 10 ? 2 : 1
|
||||
return this.terrain().magnitude() < 10 ? 2 : 1
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user