implement battleship

This commit is contained in:
Evan
2024-11-26 10:15:54 -08:00
parent 45e7170c2a
commit 8abc5e4aed
12 changed files with 304 additions and 25 deletions
+14
View File
@@ -78,6 +78,12 @@ export class UnitLayer implements Layer {
case UnitType.Destroyer:
this.handleDestroyerEvent(event);
break;
case UnitType.Battleship:
this.handleBattleshipEvent(event);
break;
case UnitType.Shell:
this.handleShellEvent(event)
break;
case UnitType.TradeShip:
this.handleTradeShipEvent(event)
break;
@@ -116,6 +122,14 @@ export class UnitLayer implements Layer {
.forEach(t => this.paintCell(t.cell(), this.theme.territoryColor(event.unit.owner().info()), 255));
}
private handleShellEvent(event: UnitEvent) {
this.clearCell(event.oldTile.cell())
if (!event.unit.isActive()) {
return
}
this.paintCell(event.unit.tile().cell(), this.theme.borderColor(event.unit.owner().info()), 255)
}
private handleNuke(event: UnitEvent) {
bfs(event.oldTile, euclDist(event.oldTile, 2)).forEach(t => {
+3 -17
View File
@@ -6,6 +6,7 @@ import { BuildUnitIntentEvent } from '../../../Transport';
import atomBombIcon from '../../../../../resources/images/NukeIconWhite.svg';
import hydrogenBombIcon from '../../../../../resources/images/MushroomCloudIconWhite.svg';
import destroyerIcon from '../../../../../resources/images/DestroyerIconWhite.svg';
import battleshipIcon from '../../../../../resources/images/BattleshipIconWhite.svg';
import missileSiloIcon from '../../../../../resources/images/MissileSiloIconWhite.svg';
import goldCoinIcon from '../../../../../resources/images/GoldCoinIcon.svg';
import portIcon from '../../../../../resources/images/PortIcon.svg';
@@ -22,6 +23,7 @@ const buildTable: BuildItemDisplay[][] = [
{ unitType: UnitType.AtomBomb, icon: atomBombIcon },
{ unitType: UnitType.HydrogenBomb, icon: hydrogenBombIcon },
{ unitType: UnitType.Destroyer, icon: destroyerIcon },
{ unitType: UnitType.Battleship, icon: battleshipIcon },
{ unitType: UnitType.Port, icon: portIcon },
{ unitType: UnitType.MissileSilo, icon: missileSiloIcon }
]
@@ -156,23 +158,7 @@ export class BuildMenu extends LitElement {
}
public onBuildSelected = (item: BuildItemDisplay) => {
switch (item.unitType) {
case UnitType.AtomBomb:
this.eventBus.emit(new BuildUnitIntentEvent(UnitType.AtomBomb, this.clickedCell))
break
case UnitType.HydrogenBomb:
this.eventBus.emit(new BuildUnitIntentEvent(UnitType.HydrogenBomb, this.clickedCell))
break
case UnitType.Destroyer:
this.eventBus.emit(new BuildUnitIntentEvent(UnitType.Destroyer, this.clickedCell))
break
case UnitType.Port:
this.eventBus.emit(new BuildUnitIntentEvent(UnitType.Port, this.clickedCell))
break
case UnitType.MissileSilo:
this.eventBus.emit(new BuildUnitIntentEvent(UnitType.MissileSilo, this.clickedCell))
break
}
this.eventBus.emit(new BuildUnitIntentEvent(item.unitType, this.clickedCell))
this.hideMenu()
};
+11 -1
View File
@@ -29,9 +29,19 @@ export class DefaultConfig implements Config {
cost: (p: Player) => (p.units(UnitType.Destroyer).length + 1) * 250_000,
territoryBound: false
}
case UnitType.Battleship:
return {
cost: (p: Player) => (p.units(UnitType.Battleship).length + 1) * 500_000,
territoryBound: false
}
case UnitType.Shell:
return {
cost: (p: Player) => (p.units(UnitType.Destroyer).length + 1) * 500_000,
territoryBound: false
}
case UnitType.Port:
return {
cost: (p: Player) => (p.units(UnitType.Port).length + 1) * 250_000,
cost: (p: Player) => Math.pow(2, p.units(UnitType.Port).length) * 250_000,
territoryBound: true
}
case UnitType.AtomBomb:
+1 -1
View File
@@ -13,7 +13,7 @@ export const devConfig = new class extends DefaultConfig {
return 95
}
numSpawnPhaseTurns(): number {
return 40
return 80
}
gameCreationRate(): number {
return 20 * 1000
+117
View File
@@ -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
}
}
}
+1 -1
View File
@@ -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)
+3
View File
@@ -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:
+58
View File
@@ -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
}
}
+2
View File
@@ -31,6 +31,8 @@ export interface UnitInfo {
export enum UnitType {
TransportShip = "Transport",
Destroyer = "Destroyer",
Battleship = "Battleship",
Shell = "Shell",
Port = "Port",
AtomBomb = "Atom Bomb",
HydrogenBomb = "Hydrogen Bomb",
+5 -2
View File
@@ -357,7 +357,10 @@ export class PlayerImpl implements MutablePlayer {
case UnitType.Port:
return this.portSpawn(targetTile)
case UnitType.Destroyer:
return this.destroyerSpawn(targetTile)
case UnitType.Battleship:
return this.warshipSpawn(targetTile)
case UnitType.Shell:
return targetTile
case UnitType.MissileSilo:
return this.missileSiloSpawn(targetTile)
case UnitType.TransportShip:
@@ -387,7 +390,7 @@ export class PlayerImpl implements MutablePlayer {
return spawns[0]
}
destroyerSpawn(tile: Tile): Tile | false {
warshipSpawn(tile: Tile): Tile | false {
if (!tile.isOcean()) {
return false
}