From 7bdad3577d7c1312e0c5f71dc98c87bc05875662 Mon Sep 17 00:00:00 2001 From: evanpelle Date: Fri, 23 Aug 2024 20:33:09 -0700 Subject: [PATCH] can attack terra nullius with boats --- TODO.txt | 2 ++ src/client/ClientGame.ts | 17 ++++++++++------- src/core/execution/BoatAttackExecution.ts | 21 +++++++++++++-------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/TODO.txt b/TODO.txt index c0b6f9937..41a240429 100644 --- a/TODO.txt +++ b/TODO.txt @@ -46,7 +46,9 @@ * try vintage theme * add shader to dim border * remove player.info() +* give terranullius an ID, game.player() returns terranullius * give time to (re) spawn at start of game +* store & delay tile updates for lag compensation * BUG: ocean is considered TerraNullius ? * BUG: fix hotreload (priority queue breaks it) * PERF: use hierarchical a* search for boats diff --git a/src/client/ClientGame.ts b/src/client/ClientGame.ts index df192cc47..3ca4902c1 100644 --- a/src/client/ClientGame.ts +++ b/src/client/ClientGame.ts @@ -190,13 +190,16 @@ export class ClientGame { const owner = tile.owner() const targetID = owner.isPlayer() ? owner.id() : null if (tile.owner() != this.myPlayer && tile.isLand()) { - // const ocean = Array.from(bfs(tile, 4)) - // .filter(t => t.isOcean) - // .sort((a, b) => manhattanDist(tile.cell(), a.cell()) - manhattanDist(tile.cell(), b.cell())) - // if (ocean.length > 0) { - // this.sendBoatAttackIntent(targetID, cell, this.config.player().boatAttackAmount(this.myPlayer, owner)) - // return - // } + const tn = Array.from(bfs(tile, 6)) + .filter(t => t.isOcean()) + .filter(t => !t.hasOwner()) + .sort((a, b) => manhattanDist(tile.cell(), a.cell()) - manhattanDist(tile.cell(), b.cell())) + .flatMap(t => t.neighbors()) + .filter(n => n.isShore()) + if (tn.length > 0) { + this.sendBoatAttackIntent(targetID, tn[0].cell(), this.config.player().boatAttackAmount(this.myPlayer, owner)) + return + } if (this.myPlayer.sharesBorderWith(tile.owner())) { this.sendAttackIntent(targetID, cell, this.config.player().attackAmount(this.myPlayer, owner)) diff --git a/src/core/execution/BoatAttackExecution.ts b/src/core/execution/BoatAttackExecution.ts index 6d617b223..d942751b0 100644 --- a/src/core/execution/BoatAttackExecution.ts +++ b/src/core/execution/BoatAttackExecution.ts @@ -1,5 +1,5 @@ import PriorityQueue from "priority-queue-typescript"; -import {Boat, Cell, Execution, MutableBoat, MutableGame, MutablePlayer, Player, PlayerID, Tile, TileEvent} from "../Game"; +import {Boat, Cell, Execution, MutableBoat, MutableGame, MutablePlayer, Player, PlayerID, TerraNullius, Tile, TileEvent} from "../Game"; import {manhattanDist} from "../Util"; import {AttackExecution} from "./AttackExecution"; import {Config, PlayerConfig} from "../configuration/Config"; @@ -15,7 +15,7 @@ export class BoatAttackExecution implements Execution { private mg: MutableGame private attacker: MutablePlayer - private target: MutablePlayer + private target: MutablePlayer | TerraNullius // TODO make private public path: Tile[] @@ -40,20 +40,25 @@ export class BoatAttackExecution implements Execution { ) { } init(mg: MutableGame, ticks: number) { - if (this.targetID == null) { - throw new Error("attacking terranullius not supported") - } this.lastMove = ticks this.mg = mg this.attacker = mg.player(this.attackerID) - this.target = mg.player(this.targetID) + if (this.targetID == null) { + this.target = mg.terraNullius() + } else { + this.target = mg.player(this.targetID) + } this.troops = Math.min(this.troops, this.attacker.troops()) this.attacker.removeTroops(this.troops) this.src = this.closestShoreTileToTarget(this.attacker, this.cell) - this.dst = this.closestShoreTileToTarget(this.target, this.cell) + if (this.target.isPlayer()) { + this.dst = this.closestShoreTileToTarget(this.target, this.cell) + } else { + this.dst = this.mg.tile(this.cell) + } if (this.src == null || this.dst == null) { this.active = false @@ -64,7 +69,7 @@ export class BoatAttackExecution implements Execution { this.path = this.aStarPre.reconstructPath() if (this.path != null) { console.log(`got path ${this.path.map(t => t.cell().toString())}`) - this.boat = this.attacker.addBoat(1000, this.src, this.target) + this.boat = this.attacker.addBoat(this.troops, this.src, this.target) } else { console.log('got null path') this.active = false