From efd5f65787b9e16565b3692f2066dc08d23c9894 Mon Sep 17 00:00:00 2001 From: evanpelle Date: Tue, 13 Aug 2024 08:16:23 -0700 Subject: [PATCH] attacks cancel out --- TODO.txt | 1 + src/core/execution/AttackExecution.ts | 31 ++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/TODO.txt b/TODO.txt index 747447650..f37945945 100644 --- a/TODO.txt +++ b/TODO.txt @@ -13,6 +13,7 @@ * fix boat bugs DONE 8/12/2024 * add username in front page DONE 8/12/2024 * improve front page DONE 8/12/2024 +* attacks cancel out DONE 8/13/2024 * upload and start server * fix enemy islands when attacking * better algorithm for name render placement diff --git a/src/core/execution/AttackExecution.ts b/src/core/execution/AttackExecution.ts index 5fd3f4079..67d0e1350 100644 --- a/src/core/execution/AttackExecution.ts +++ b/src/core/execution/AttackExecution.ts @@ -23,12 +23,33 @@ export class AttackExecution implements Execution { private targetCell: Cell | null ) { } - init(gs: MutableGame, ticks: number) { - this._owner = gs.player(this._ownerID) - this.target = this.targetID == null ? gs.terraNullius() : gs.player(this.targetID) + init(mg: MutableGame, ticks: number) { + + this._owner = mg.player(this._ownerID) + this.target = this.targetID == null ? mg.terraNullius() : mg.player(this.targetID) this.troops = Math.min(this._owner.troops(), this.troops) this._owner.setTroops(this._owner.troops() - this.troops) - this.mg = gs + this.mg = mg + + if (this.target.isPlayer()) { + for (const exec of mg.executions()) { + if (exec instanceof AttackExecution) { + const otherAttack = exec as AttackExecution + if (otherAttack.target == this._owner && this.target == otherAttack._owner) { + if (otherAttack.troops > this.troops) { + otherAttack.troops -= this.troops + otherAttack.calculateToConquer() + this.active = false + return + } else { + this.troops -= otherAttack.troops + otherAttack.active = false + } + } + } + } + } + this.calculateToConquer() } @@ -92,7 +113,7 @@ export class AttackExecution implements Execution { } newBorder.add(neighbor) this.numTilesWithEnemy += 1 - let numOwnedByMe = tile.neighbors() + let numOwnedByMe = neighbor.neighbors() .filter(t => t.terrain() == TerrainTypes.Land) .filter(t => t.owner() == this._owner) .length