From 75fe718f160d33d6bbdc384470bb81f1bfd9e452 Mon Sep 17 00:00:00 2001 From: evanpelle Date: Thu, 23 Jan 2025 11:14:08 -0800 Subject: [PATCH] Combine Unit & MutableUnit --- src/core/execution/BattleshipExecution.ts | 4 +-- src/core/execution/CityExecution.ts | 4 +-- src/core/execution/DefensePostExecution.ts | 4 +-- src/core/execution/DestroyerExecution.ts | 6 ++-- src/core/execution/MissileSiloExecution.ts | 4 +-- src/core/execution/NukeExecution.ts | 4 +-- src/core/execution/PortExecution.ts | 8 ++--- src/core/execution/ShellExecution.ts | 6 ++-- src/core/execution/TradeShipExecution.ts | 8 ++--- src/core/execution/TransportShipExecution.ts | 4 +-- src/core/game/Game.ts | 33 +++++++++++--------- src/core/game/PlayerImpl.ts | 4 +-- src/core/game/UnitImpl.ts | 4 +-- 13 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/core/execution/BattleshipExecution.ts b/src/core/execution/BattleshipExecution.ts index 818a246d1..41885f8be 100644 --- a/src/core/execution/BattleshipExecution.ts +++ b/src/core/execution/BattleshipExecution.ts @@ -1,4 +1,4 @@ -import { Cell, Execution, Game, Player, MutableUnit, PlayerID, TerrainType, Unit, UnitType } from "../game/Game"; +import { Cell, Execution, Game, Player, Unit, PlayerID, TerrainType, UnitType } from "../game/Game"; import { PathFinder } from "../pathfinding/PathFinding"; import { PathFindResultType } from "../pathfinding/AStar"; import { PseudoRandom } from "../PseudoRandom"; @@ -12,7 +12,7 @@ export class BattleshipExecution implements Execution { private _owner: Player private active = true - private battleship: MutableUnit = null + private battleship: Unit = null private mg: Game = null private pathfinder: PathFinder diff --git a/src/core/execution/CityExecution.ts b/src/core/execution/CityExecution.ts index 26d6b2e20..807939536 100644 --- a/src/core/execution/CityExecution.ts +++ b/src/core/execution/CityExecution.ts @@ -1,12 +1,12 @@ import { consolex } from "../Consolex"; -import { Execution, Game, Player, MutableUnit, PlayerID, UnitType } from "../game/Game"; +import { Execution, Game, Player, Unit, PlayerID, UnitType } from "../game/Game"; import { TileRef } from "../game/GameMap"; export class CityExecution implements Execution { private player: Player private mg: Game - private city: MutableUnit + private city: Unit private active: boolean = true constructor(private ownerId: PlayerID, private tile: TileRef) { } diff --git a/src/core/execution/DefensePostExecution.ts b/src/core/execution/DefensePostExecution.ts index 0754f9fb1..f91d28a36 100644 --- a/src/core/execution/DefensePostExecution.ts +++ b/src/core/execution/DefensePostExecution.ts @@ -1,12 +1,12 @@ import { consolex } from "../Consolex"; -import { Cell, DefenseBonus, Execution, Game, Player, MutableUnit, PlayerID, UnitType } from "../game/Game"; +import { Cell, DefenseBonus, Execution, Game, Player, Unit, PlayerID, UnitType } from "../game/Game"; import { manhattanDistFN, TileRef } from "../game/GameMap"; export class DefensePostExecution implements Execution { private player: Player private mg: Game - private post: MutableUnit + private post: Unit private active: boolean = true private defenseBonuses: DefenseBonus[] = [] diff --git a/src/core/execution/DestroyerExecution.ts b/src/core/execution/DestroyerExecution.ts index 774b3cd57..20d065535 100644 --- a/src/core/execution/DestroyerExecution.ts +++ b/src/core/execution/DestroyerExecution.ts @@ -1,4 +1,4 @@ -import { Cell, Execution, Game, Player, MutableUnit, PlayerID, TerrainType, UnitType } from "../game/Game"; +import { Cell, Execution, Game, Player, Unit, PlayerID, TerrainType, UnitType } from "../game/Game"; import { PathFinder } from "../pathfinding/PathFinding"; import { PathFindResultType } from "../pathfinding/AStar"; import { PseudoRandom } from "../PseudoRandom"; @@ -11,10 +11,10 @@ export class DestroyerExecution implements Execution { private _owner: Player private active = true - private destroyer: MutableUnit = null + private destroyer: Unit = null private mg: Game = null - private target: MutableUnit = null + private target: Unit = null private pathfinder: PathFinder private patrolTile: TileRef; diff --git a/src/core/execution/MissileSiloExecution.ts b/src/core/execution/MissileSiloExecution.ts index 51661c0b5..f6da22eb1 100644 --- a/src/core/execution/MissileSiloExecution.ts +++ b/src/core/execution/MissileSiloExecution.ts @@ -1,5 +1,5 @@ import { consolex } from "../Consolex"; -import { Cell, Execution, Game, Player, MutableUnit, PlayerID, UnitType } from "../game/Game"; +import { Cell, Execution, Game, Player, Unit, PlayerID, UnitType } from "../game/Game"; import { TileRef } from "../game/GameMap"; export class MissileSiloExecution implements Execution { @@ -7,7 +7,7 @@ export class MissileSiloExecution implements Execution { private active = true private mg: Game private player: Player - private silo: MutableUnit + private silo: Unit constructor( private _owner: PlayerID, diff --git a/src/core/execution/NukeExecution.ts b/src/core/execution/NukeExecution.ts index e6e56c5d0..f601ae1c8 100644 --- a/src/core/execution/NukeExecution.ts +++ b/src/core/execution/NukeExecution.ts @@ -1,5 +1,5 @@ import { nextTick } from "process"; -import { Cell, Execution, Game, Player, PlayerID, MutableUnit, UnitType, TerraNullius } from "../game/Game"; +import { Cell, Execution, Game, Player, PlayerID, Unit, UnitType, TerraNullius } from "../game/Game"; import { PathFinder } from "../pathfinding/PathFinding"; import { PathFindResultType } from "../pathfinding/AStar"; import { PseudoRandom } from "../PseudoRandom"; @@ -14,7 +14,7 @@ export class NukeExecution implements Execution { private mg: Game - private nuke: MutableUnit + private nuke: Unit private pathFinder: PathFinder constructor( diff --git a/src/core/execution/PortExecution.ts b/src/core/execution/PortExecution.ts index 63806c3bf..2585c5fd9 100644 --- a/src/core/execution/PortExecution.ts +++ b/src/core/execution/PortExecution.ts @@ -1,4 +1,4 @@ -import { AllPlayers, Cell, Execution, Game, Player, MutableUnit, PlayerID, TerrainType, UnitType } from "../game/Game"; +import { AllPlayers, Cell, Execution, Game, Player, Unit, PlayerID, TerrainType, UnitType } from "../game/Game"; import { PathFinder } from "../pathfinding/PathFinding"; import { PathFindResultType } from "../pathfinding/AStar"; import { PseudoRandom } from "../PseudoRandom"; @@ -11,10 +11,10 @@ export class PortExecution implements Execution { private active = true private mg: Game - private port: MutableUnit + private port: Unit private random: PseudoRandom - private portPaths = new Map() - private computingPaths = new Map() + private portPaths = new Map() + private computingPaths = new Map() constructor( private _owner: PlayerID, diff --git a/src/core/execution/ShellExecution.ts b/src/core/execution/ShellExecution.ts index 1552b7be4..c5c7f46f4 100644 --- a/src/core/execution/ShellExecution.ts +++ b/src/core/execution/ShellExecution.ts @@ -1,4 +1,4 @@ -import { Execution, Game, Player, MutableUnit, Unit, UnitType } from "../game/Game"; +import { Execution, Game, Player, Unit, UnitType } from "../game/Game"; import { PathFinder } from "../pathfinding/PathFinding"; import { PathFindResultType } from "../pathfinding/AStar"; import { consolex } from "../Consolex"; @@ -8,9 +8,9 @@ export class ShellExecution implements Execution { private active = true private pathFinder: PathFinder - private shell: MutableUnit + private shell: Unit - constructor(private spawn: TileRef, private _owner: Player, private ownerUnit: Unit, private target: MutableUnit) { + constructor(private spawn: TileRef, private _owner: Player, private ownerUnit: Unit, private target: Unit) { } diff --git a/src/core/execution/TradeShipExecution.ts b/src/core/execution/TradeShipExecution.ts index 95841ecb4..dc20e92f8 100644 --- a/src/core/execution/TradeShipExecution.ts +++ b/src/core/execution/TradeShipExecution.ts @@ -1,6 +1,6 @@ import { MessageType } from '../game/Game'; import { renderNumber } from "../../client/Utils"; -import { AllPlayers, Cell, Execution, Game, MutableUnit, Player, PlayerID, UnitType } from "../game/Game"; +import { AllPlayers, Cell, Execution, Game, Unit, Player, PlayerID, UnitType } from "../game/Game"; import { PathFinder } from "../pathfinding/PathFinding"; import { PathFindResultType } from "../pathfinding/AStar"; import { distSortUnit } from "../Util"; @@ -12,14 +12,14 @@ export class TradeShipExecution implements Execution { private active = true private mg: Game private origOwner: Player - private tradeShip: MutableUnit + private tradeShip: Unit private index = 0 private wasCaptured = false constructor( private _owner: PlayerID, - private srcPort: MutableUnit, - private dstPort: MutableUnit, + private srcPort: Unit, + private dstPort: Unit, private pathFinder: PathFinder, // don't modify private path: TileRef[] diff --git a/src/core/execution/TransportShipExecution.ts b/src/core/execution/TransportShipExecution.ts index 9936c6567..4032633da 100644 --- a/src/core/execution/TransportShipExecution.ts +++ b/src/core/execution/TransportShipExecution.ts @@ -1,4 +1,4 @@ -import { Unit, Cell, Execution, MutableUnit, Game, Player, PlayerID, TerraNullius, UnitType, TerrainType } from "../game/Game"; +import { Unit, Cell, Execution, Game, Player, PlayerID, TerraNullius, UnitType, TerrainType } from "../game/Game"; import { AttackExecution } from "./AttackExecution"; import { MessageType } from '../game/Game'; import { PathFinder } from "../pathfinding/PathFinding"; @@ -26,7 +26,7 @@ export class TransportShipExecution implements Execution { private dst: TileRef | null - private boat: MutableUnit + private boat: Unit private pathFinder: PathFinder diff --git a/src/core/game/Game.ts b/src/core/game/Game.ts index 1620247f1..e54d50a5e 100644 --- a/src/core/game/Game.ts +++ b/src/core/game/Game.ts @@ -177,23 +177,28 @@ export interface DefenseBonus { } export interface Unit { + // Properties type(): UnitType troops(): number - tile(): TileRef owner(): Player + info(): UnitInfo + + // Location + tile(): TileRef + lastTile(): TileRef + move(tile: TileRef): void + + // State isActive(): boolean hasHealth(): boolean health(): number - lastTile(): TileRef -} - -export interface MutableUnit extends Unit { - move(tile: TileRef): void - owner(): Player - setTroops(troops: number): void - info(): UnitInfo - delete(displayerMessage?: boolean): void modifyHealth(delta: number): void + + // Mutations + setTroops(troops: number): void + delete(displayerMessage?: boolean): void + + // Updates toUpdate(): UnitUpdate } @@ -245,10 +250,10 @@ export interface Player { removeTroops(troops: number): number // Units - units(...types: UnitType[]): MutableUnit[] + units(...types: UnitType[]): Unit[] canBuild(type: UnitType, targetTile: TileRef): TileRef | false - buildUnit(type: UnitType, troops: number, tile: TileRef): MutableUnit - captureUnit(unit: MutableUnit): void + buildUnit(type: UnitType, troops: number, tile: TileRef): Unit + captureUnit(unit: Unit): void // Relations & Diplomacy neighbors(): (Player | TerraNullius)[] @@ -317,7 +322,7 @@ export interface Game extends GameMap { config(): Config // Units - units(...types: UnitType[]): MutableUnit[] + units(...types: UnitType[]): Unit[] unitInfo(type: UnitType): UnitInfo addTileDefenseBonus(tile: TileRef, unit: Unit, amount: number): DefenseBonus removeTileDefenseBonus(bonus: DefenseBonus): void diff --git a/src/core/game/PlayerImpl.ts b/src/core/game/PlayerImpl.ts index def8b093c..406160dfc 100644 --- a/src/core/game/PlayerImpl.ts +++ b/src/core/game/PlayerImpl.ts @@ -1,4 +1,4 @@ -import { Player, PlayerInfo, PlayerID, PlayerType, TerraNullius, Cell, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance, Tick, AllPlayers, Gold, UnitType, Unit, MutableUnit, Relation, PlayerUpdate, GameUpdateType, EmojiMessage } from "./Game"; +import { Player, PlayerInfo, PlayerID, PlayerType, TerraNullius, Cell, Execution, AllianceRequest, MutableAllianceRequest, MutableAlliance, Alliance, Tick, AllPlayers, Gold, UnitType, Unit, Relation, PlayerUpdate, GameUpdateType, EmojiMessage } from "./Game"; import { ClientID } from "../Schemas"; import { assertNever, closestOceanShoreFromPlayer, distSortUnit, simpleHash, sourceDstOceanShore, within } from "../Util"; import { CellString, GameImpl } from "./GameImpl"; @@ -425,7 +425,7 @@ export class PlayerImpl implements Player { return toRemove } - captureUnit(unit: MutableUnit): void { + captureUnit(unit: Unit): void { if (unit.owner() == this) { throw new Error(`Cannot capture unit, ${this} already owns ${unit}`) } diff --git a/src/core/game/UnitImpl.ts b/src/core/game/UnitImpl.ts index 15e833f56..3e70155de 100644 --- a/src/core/game/UnitImpl.ts +++ b/src/core/game/UnitImpl.ts @@ -1,12 +1,12 @@ import { GameUpdateType, MessageType, UnitUpdate } from './Game'; import { simpleHash, within } from "../Util"; -import { MutableUnit, TerraNullius, UnitType, Player, UnitInfo } from "./Game"; +import { Unit, TerraNullius, UnitType, Player, UnitInfo } from "./Game"; import { GameImpl } from "./GameImpl"; import { PlayerImpl } from "./PlayerImpl"; import { TileRef } from './GameMap'; -export class UnitImpl implements MutableUnit { +export class UnitImpl implements Unit { private _active = true; private _health: number private _lastTile: TileRef = null