diff --git a/src/client/graphics/layers/TerritoryLayer.ts b/src/client/graphics/layers/TerritoryLayer.ts index 55a57e61f..f6906ffb1 100644 --- a/src/client/graphics/layers/TerritoryLayer.ts +++ b/src/client/graphics/layers/TerritoryLayer.ts @@ -3,11 +3,7 @@ import { Colord } from "colord"; import { Theme } from "../../../core/configuration/Config"; import { EventBus } from "../../../core/EventBus"; import { Cell, PlayerType, UnitType } from "../../../core/game/Game"; -import { - euclDistFN, - manhattanDistFN, - TileRef, -} from "../../../core/game/GameMap"; +import { euclDistFN, TileRef } from "../../../core/game/GameMap"; import { GameUpdateType, UnitUpdate } from "../../../core/game/GameUpdates"; import { GameView, PlayerView } from "../../../core/game/GameView"; import { PseudoRandom } from "../../../core/PseudoRandom"; @@ -64,17 +60,15 @@ export class TerritoryLayer implements Layer { this.game.recentlyUpdatedTiles().forEach((t) => this.enqueueTile(t)); this.game.updatesSinceLastTick()[GameUpdateType.Unit].forEach((u) => { const update = u as UnitUpdate; - if (update.unitType == UnitType.DefensePost && update.isActive) { + if (update.unitType == UnitType.DefensePost) { const tile = update.pos; this.game - .bfs( - tile, - manhattanDistFN(tile, this.game.config().defensePostRange()), - ) + .bfs(tile, euclDistFN(tile, this.game.config().defensePostRange())) .forEach((t) => { if ( this.game.isBorder(t) && - this.game.ownerID(t) == update.ownerID + (this.game.ownerID(t) == update.ownerID || + this.game.ownerID(t) == update.lastOwnerID) ) { this.enqueueTile(t); } diff --git a/src/core/game/Game.ts b/src/core/game/Game.ts index b4ccf7ba7..0edcf0e47 100644 --- a/src/core/game/Game.ts +++ b/src/core/game/Game.ts @@ -344,6 +344,7 @@ export interface Unit { setWarshipTarget(target: Unit): void; // warship only warshipTarget(): Unit; + setOwner(owner: Player): void; setCooldown(triggerCooldown: boolean): void; ticksLeftInCooldown(cooldownDuration: number): Tick; isCooldown(): boolean; diff --git a/src/core/game/GameUpdates.ts b/src/core/game/GameUpdates.ts index f9b3087d9..cf83f3161 100644 --- a/src/core/game/GameUpdates.ts +++ b/src/core/game/GameUpdates.ts @@ -68,6 +68,7 @@ export interface UnitUpdate { troops: number; id: number; ownerID: number; + lastOwnerID?: number; // TODO: make these tilerefs pos: TileRef; lastPos: TileRef; diff --git a/src/core/game/PlayerImpl.ts b/src/core/game/PlayerImpl.ts index ed202b9cf..321d937af 100644 --- a/src/core/game/PlayerImpl.ts +++ b/src/core/game/PlayerImpl.ts @@ -703,23 +703,7 @@ export class PlayerImpl implements Player { if (unit.owner() == this) { throw new Error(`Cannot capture unit, ${this} already owns ${unit}`); } - const prev = unit.owner(); - (prev as PlayerImpl)._units = (prev as PlayerImpl)._units.filter( - (u) => u != unit, - ); - (unit as UnitImpl)._owner = this; - this._units.push(unit as UnitImpl); - this.mg.addUpdate(unit.toUpdate()); - this.mg.displayMessage( - `${unit.type()} captured by ${this.displayName()}`, - MessageType.ERROR, - prev.id(), - ); - this.mg.displayMessage( - `Captured ${unit.type()} from ${prev.displayName()}`, - MessageType.SUCCESS, - this.id(), - ); + unit.setOwner(this); } buildUnit( diff --git a/src/core/game/UnitImpl.ts b/src/core/game/UnitImpl.ts index 9c1458f51..f3ec02696 100644 --- a/src/core/game/UnitImpl.ts +++ b/src/core/game/UnitImpl.ts @@ -2,7 +2,6 @@ import { simpleHash, toInt, withinInt } from "../Util"; import { AllUnitParams, MessageType, - Player, Tick, Unit, UnitInfo, @@ -23,7 +22,7 @@ export class UnitImpl implements Unit { private _safeFromPiratesCooldown: number; // Only for trade ships private _lastSetSafeFromPirates: number; // Only for trade ships private _constructionType: UnitType = undefined; - + private _lastOwner: PlayerImpl | null = null; private _troops: number; private _cooldownTick: Tick | null = null; private _dstPort: Unit | null = null; // Only for trade ships @@ -66,6 +65,7 @@ export class UnitImpl implements Unit { id: this._id, troops: this._troops, ownerID: this._owner.smallID(), + lastOwnerID: this._lastOwner?.smallID(), isActive: this._active, pos: this._tile, lastPos: this._lastTile, @@ -119,15 +119,21 @@ export class UnitImpl implements Unit { return this.mg.unitInfo(this._type); } - setOwner(newOwner: Player): void { - const oldOwner = this._owner; - oldOwner._units = oldOwner._units.filter((u) => u != this); - this._owner = newOwner as PlayerImpl; + setOwner(newOwner: PlayerImpl): void { + this._lastOwner = this._owner; + this._lastOwner._units = this._lastOwner._units.filter((u) => u != this); + this._owner = newOwner; + this._owner._units.push(this); this.mg.addUpdate(this.toUpdate()); this.mg.displayMessage( `Your ${this.type()} was captured by ${newOwner.displayName()}`, MessageType.ERROR, - oldOwner.id(), + this._lastOwner.id(), + ); + this.mg.displayMessage( + `Captured ${this.type()} from ${this._lastOwner.displayName()}`, + MessageType.SUCCESS, + newOwner.id(), ); }