mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-28 12:54:32 +00:00
implement battleship
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import { Cell, Execution, MutableGame, MutablePlayer, MutableUnit, PlayerID, Tile, UnitType } from "../game/Game";
|
||||
import { AStar, PathFinder, PathFindResultType } from "../PathFinding";
|
||||
import { PseudoRandom } from "../PseudoRandom";
|
||||
import { distSort, distSortUnit, manhattanDist } from "../Util";
|
||||
import { ShellExecution } from "./ShellExecution";
|
||||
|
||||
export class BattleshipExecution implements Execution {
|
||||
private random: PseudoRandom
|
||||
|
||||
private _owner: MutablePlayer
|
||||
private active = true
|
||||
private battleship: MutableUnit = null
|
||||
private mg: MutableGame = null
|
||||
|
||||
private pathfinder = new PathFinder(5000, t => t.isWater())
|
||||
|
||||
private patrolTile: Tile;
|
||||
private patrolCenterTile: Tile
|
||||
|
||||
// TODO: put in config
|
||||
private searchRange = 100
|
||||
private attackRate = 20
|
||||
private lastAttack = 0
|
||||
|
||||
constructor(
|
||||
private playerID: PlayerID,
|
||||
private cell: Cell,
|
||||
) { }
|
||||
|
||||
|
||||
init(mg: MutableGame, ticks: number): void {
|
||||
this._owner = mg.player(this.playerID)
|
||||
this.mg = mg
|
||||
this.patrolCenterTile = mg.tile(this.cell)
|
||||
this.patrolTile = this.patrolCenterTile
|
||||
this.random = new PseudoRandom(mg.ticks())
|
||||
}
|
||||
|
||||
tick(ticks: number): void {
|
||||
if (this.battleship == null) {
|
||||
const spawn = this._owner.canBuild(UnitType.Battleship, this.patrolTile)
|
||||
if (spawn == false) {
|
||||
this.active = false
|
||||
return
|
||||
}
|
||||
this.battleship = this._owner.buildUnit(UnitType.Battleship, 0, spawn)
|
||||
return
|
||||
}
|
||||
if (!this.battleship.isActive()) {
|
||||
this.active = false
|
||||
return
|
||||
}
|
||||
|
||||
if (this.mg.ticks() % 2 == 0) {
|
||||
const result = this.pathfinder.nextTile(this.battleship.tile(), this.patrolTile)
|
||||
switch (result.type) {
|
||||
case PathFindResultType.Completed:
|
||||
this.patrolTile = this.randomTile()
|
||||
break
|
||||
case PathFindResultType.NextTile:
|
||||
this.battleship.move(result.tile)
|
||||
break
|
||||
case PathFindResultType.Pending:
|
||||
return
|
||||
case PathFindResultType.PathNotFound:
|
||||
console.log(`path not found to patrol tile`)
|
||||
this.patrolTile = this.randomTile()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (this.mg.ticks() - this.lastAttack < this.attackRate) {
|
||||
return
|
||||
}
|
||||
|
||||
const ships = this.mg.units(UnitType.TransportShip, UnitType.Destroyer, UnitType.TradeShip, UnitType.Battleship)
|
||||
.filter(u => manhattanDist(u.tile().cell(), this.battleship.tile().cell()) < 100)
|
||||
.filter(u => u.owner() != this.battleship.owner())
|
||||
.filter(u => u != this.battleship)
|
||||
.filter(u => !u.owner().isAlliedWith(this.battleship.owner()))
|
||||
.sort(distSortUnit(this.battleship));
|
||||
|
||||
if (ships.length > 0) {
|
||||
this.lastAttack = this.mg.ticks()
|
||||
this.mg.addExecution(new ShellExecution(this.battleship.tile(), this.battleship.owner(), ships[0]))
|
||||
}
|
||||
}
|
||||
|
||||
owner(): MutablePlayer {
|
||||
return this._owner
|
||||
}
|
||||
|
||||
isActive(): boolean {
|
||||
return this.active
|
||||
}
|
||||
|
||||
activeDuringSpawnPhase(): boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
randomTile(): Tile {
|
||||
while (true) {
|
||||
const x = this.patrolCenterTile.cell().x + this.random.nextInt(-this.searchRange / 2, this.searchRange / 2)
|
||||
const y = this.patrolCenterTile.cell().y + this.random.nextInt(-this.searchRange / 2, this.searchRange / 2)
|
||||
const cell = new Cell(x, y)
|
||||
if (!this.mg.isOnMap(cell)) {
|
||||
continue
|
||||
}
|
||||
const tile = this.mg.tile(cell)
|
||||
if (!tile.isOcean()) {
|
||||
continue
|
||||
}
|
||||
return tile
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -52,7 +52,7 @@ export class DestroyerExecution implements Execution {
|
||||
this.target = null
|
||||
}
|
||||
if (this.target == null) {
|
||||
const ships = this.mg.units(UnitType.TransportShip, UnitType.Destroyer, UnitType.TradeShip)
|
||||
const ships = this.mg.units(UnitType.TransportShip, UnitType.Destroyer, UnitType.TradeShip, UnitType.Battleship)
|
||||
.filter(u => manhattanDist(u.tile().cell(), this.destroyer.tile().cell()) < 100)
|
||||
.filter(u => u.owner() != this.destroyer.owner())
|
||||
.filter(u => u != this.destroyer)
|
||||
|
||||
@@ -19,6 +19,7 @@ import { SetTargetTroopRatioExecution } from "./SetTargetTroopRatioExecution";
|
||||
import { DestroyerExecution } from "./DestroyerExecution";
|
||||
import { PortExecution } from "./PortExecution";
|
||||
import { MissileSiloExecution } from "./MissileSiloExecution";
|
||||
import { BattleshipExecution } from "./BattleshipExecution";
|
||||
|
||||
|
||||
|
||||
@@ -88,6 +89,8 @@ export class Executor {
|
||||
return new NukeExecution(intent.unit, intent.player, new Cell(intent.x, intent.y))
|
||||
case UnitType.Destroyer:
|
||||
return new DestroyerExecution(intent.player, new Cell(intent.x, intent.y))
|
||||
case UnitType.Battleship:
|
||||
return new BattleshipExecution(intent.player, new Cell(intent.x, intent.y))
|
||||
case UnitType.Port:
|
||||
return new PortExecution(intent.player, new Cell(intent.x, intent.y))
|
||||
case UnitType.MissileSilo:
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { Execution, MutableGame, MutablePlayer, MutableUnit, Tile, Unit, UnitType } from "../game/Game";
|
||||
import { PathFinder, PathFindResultType } from "../PathFinding";
|
||||
|
||||
export class ShellExecution implements Execution {
|
||||
|
||||
private active = true
|
||||
private pathFinder = new PathFinder(2000, () => true, 10)
|
||||
private shell: MutableUnit
|
||||
|
||||
constructor(private spawn: Tile, private _owner: MutablePlayer, private target: MutableUnit) {
|
||||
|
||||
}
|
||||
|
||||
init(mg: MutableGame, ticks: number): void {
|
||||
}
|
||||
|
||||
tick(ticks: number): void {
|
||||
if (this.shell == null) {
|
||||
this.shell = this._owner.buildUnit(UnitType.Shell, 0, this.spawn)
|
||||
}
|
||||
if (!this.target.isActive()) {
|
||||
this.shell.delete()
|
||||
this.active = false
|
||||
return
|
||||
}
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const result = this.pathFinder.nextTile(this.shell.tile(), this.target.tile())
|
||||
switch (result.type) {
|
||||
case PathFindResultType.Completed:
|
||||
this.active = false
|
||||
this.target.delete()
|
||||
this.shell.delete()
|
||||
return
|
||||
case PathFindResultType.NextTile:
|
||||
this.shell.move(result.tile)
|
||||
break
|
||||
case PathFindResultType.Pending:
|
||||
return
|
||||
case PathFindResultType.PathNotFound:
|
||||
console.log(`Shell ${this.shell} could not find target`)
|
||||
this.active = false
|
||||
this.shell.delete()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
owner(): MutablePlayer {
|
||||
return null
|
||||
}
|
||||
isActive(): boolean {
|
||||
return this.active
|
||||
}
|
||||
activeDuringSpawnPhase(): boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user