Files
OpenFrontIO/src/core/execution/TradeShipExecution.ts
T
Readixyee 665a8c3823 Executions dont switch owner (#326)
when an building is taken over by another player the execution for it
doesnt change its owner this makes it so when a sam is captured it tries
to intercept your own nukes and doesnt intercept the ones by the
previous player

this change makes executions of buildings automaticly switch their owner
2025-03-27 16:03:15 -07:00

166 lines
4.2 KiB
TypeScript

import { MessageType } from "../game/Game";
import { renderNumber } from "../../client/Utils";
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";
import { consolex } from "../Consolex";
import { TileRef } from "../game/GameMap";
export class TradeShipExecution implements Execution {
private active = true;
private mg: Game;
private origOwner: Player;
private tradeShip: Unit;
private index = 0;
private wasCaptured = false;
constructor(
private _owner: PlayerID,
private srcPort: Unit,
private _dstPort: Unit,
private pathFinder: PathFinder,
) {}
init(mg: Game, ticks: number): void {
this.mg = mg;
this.origOwner = mg.player(this._owner);
}
tick(ticks: number): void {
if (this.tradeShip == null) {
const spawn = this.origOwner.canBuild(
UnitType.TradeShip,
this.srcPort.tile(),
);
if (spawn == false) {
consolex.warn(`cannot build trade ship`);
this.active = false;
return;
}
this.tradeShip = this.origOwner.buildUnit(UnitType.TradeShip, 0, spawn, {
dstPort: this._dstPort,
});
}
if (!this.tradeShip.isActive()) {
this.active = false;
return;
}
if (this.origOwner != this.tradeShip.owner()) {
// Store as vairable in case ship is recaptured by previous owner
this.wasCaptured = true;
}
// If a player captures an other player's port while trading we should delete
// the ship.
if (this._dstPort.owner().id() == this.srcPort.owner().id()) {
this.tradeShip.delete(false);
this.active = false;
return;
}
if (
!this.wasCaptured &&
(!this._dstPort.isActive() ||
!this.tradeShip.owner().canTrade(this._dstPort.owner()))
) {
this.tradeShip.delete(false);
this.active = false;
return;
}
if (this.wasCaptured) {
const ports = this.tradeShip
.owner()
.units(UnitType.Port)
.sort(distSortUnit(this.mg, this.tradeShip));
if (ports.length == 0) {
this.tradeShip.delete(false);
this.active = false;
return;
} else {
this._dstPort = ports[0];
this.tradeShip.setDstPort(this._dstPort);
}
}
const result = this.pathFinder.nextTile(
this.tradeShip.tile(),
this._dstPort.tile(),
);
switch (result.type) {
case PathFindResultType.Completed:
this.complete();
break;
case PathFindResultType.Pending:
// Fire unit event to rerender.
this.tradeShip.move(this.tradeShip.tile());
break;
case PathFindResultType.NextTile:
this.tradeShip.move(result.tile);
break;
case PathFindResultType.PathNotFound:
consolex.warn("captured trade ship cannot find route");
this.active = false;
break;
}
}
private complete() {
this.active = false;
this.tradeShip.delete(false);
const gold = this.mg
.config()
.tradeShipGold(
this.mg.manhattanDist(this.srcPort.tile(), this._dstPort.tile()),
);
if (this.wasCaptured) {
this.tradeShip.owner().addGold(gold);
this.mg.displayMessage(
`Received ${renderNumber(gold)} gold from ship captured from ${this.origOwner.displayName()}`,
MessageType.SUCCESS,
this.tradeShip.owner().id(),
);
} else {
this.srcPort.owner().addGold(gold);
this._dstPort.owner().addGold(gold);
this.mg.displayMessage(
`Received ${renderNumber(gold)} gold from trade with ${this.srcPort.owner().displayName()}`,
MessageType.SUCCESS,
this._dstPort.owner().id(),
);
this.mg.displayMessage(
`Received ${renderNumber(gold)} gold from trade with ${this._dstPort.owner().displayName()}`,
MessageType.SUCCESS,
this.srcPort.owner().id(),
);
}
return;
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
dstPort(): TileRef {
return this._dstPort.tile();
}
}