mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-27 18:14:57 +00:00
remove BuildValidator
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
import { bfs, dist, manhattanDist } from "../Util";
|
||||
import { Game, Player, Tile, UnitType } from "./Game";
|
||||
|
||||
export class BuildValidator {
|
||||
constructor(private game: Game) { }
|
||||
|
||||
canBuild(player: Player, tile: Tile, unitType: UnitType): boolean {
|
||||
const cost = this.game.unitInfo(unitType).cost
|
||||
if (!player.isAlive() || player.gold() < cost) {
|
||||
return false
|
||||
}
|
||||
switch (unitType) {
|
||||
case UnitType.Nuke:
|
||||
return player.units(UnitType.MissileSilo).length > 0
|
||||
case UnitType.Port:
|
||||
return this.canBuildPort(player, tile)
|
||||
case UnitType.Destroyer:
|
||||
return this.canBuildDestroyer(player, tile)
|
||||
case UnitType.MissileSilo:
|
||||
return tile.owner() == player
|
||||
}
|
||||
}
|
||||
|
||||
canBuildPort(player: Player, tile: Tile): boolean {
|
||||
return Array.from(bfs(tile, dist(tile, 20)))
|
||||
.filter(t => t.owner() == player && t.isOceanShore()).length > 0
|
||||
|
||||
}
|
||||
|
||||
canBuildDestroyer(player: Player, tile: Tile): boolean {
|
||||
return player.units(UnitType.Port)
|
||||
.filter(u => manhattanDist(u.tile().cell(), tile.cell()) < this.game.config().boatMaxDistance()).length > 0
|
||||
}
|
||||
}
|
||||
@@ -213,6 +213,8 @@ export interface Player {
|
||||
// Number between 0, 1
|
||||
targetTroopRatio(): number
|
||||
troops(): number
|
||||
|
||||
canBuild(type: UnitType, tile: Tile): boolean
|
||||
}
|
||||
|
||||
export interface MutablePlayer extends Player {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MutablePlayer, Tile, PlayerInfo, PlayerID, PlayerType, Player, TerraNullius, Cell, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance, Tick, TargetPlayerEvent, EmojiMessage, EmojiMessageEvent, AllPlayers, Gold, UnitType } from "./Game";
|
||||
import { ClientID } from "../Schemas";
|
||||
import { processName, simpleHash } from "../Util";
|
||||
import { bfs, dist, manhattanDist, processName, simpleHash } from "../Util";
|
||||
import { CellString, GameImpl } from "./GameImpl";
|
||||
import { UnitImpl } from "./UnitImpl";
|
||||
import { TileImpl } from "./TileImpl";
|
||||
@@ -324,6 +324,32 @@ export class PlayerImpl implements MutablePlayer {
|
||||
return toRemove
|
||||
}
|
||||
|
||||
canBuild(unitType: UnitType, tile: Tile): boolean {
|
||||
const cost = this.gs.unitInfo(unitType).cost
|
||||
if (!this.isAlive() || this.gold() < cost) {
|
||||
return false
|
||||
}
|
||||
switch (unitType) {
|
||||
case UnitType.Nuke:
|
||||
return this.units(UnitType.MissileSilo).length > 0
|
||||
case UnitType.Port:
|
||||
return this.canBuildPort(tile)
|
||||
case UnitType.Destroyer:
|
||||
return this.canBuildDestroyer(tile)
|
||||
case UnitType.MissileSilo:
|
||||
return tile.owner() == this
|
||||
}
|
||||
}
|
||||
|
||||
canBuildPort(tile: Tile): boolean {
|
||||
return Array.from(bfs(tile, dist(tile, 20)))
|
||||
.filter(t => t.owner() == this && t.isOceanShore()).length > 0
|
||||
}
|
||||
|
||||
canBuildDestroyer(tile: Tile): boolean {
|
||||
return this.units(UnitType.Port)
|
||||
.filter(u => manhattanDist(u.tile().cell(), tile.cell()) < this.gs.config().boatMaxDistance()).length > 0
|
||||
}
|
||||
|
||||
hash(): number {
|
||||
return simpleHash(this.id()) * (this.population() + this.numTilesOwned());
|
||||
|
||||
Reference in New Issue
Block a user