mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 09:10:42 +00:00
21291b9fa3
## What Notify a player when one of their trade ships is captured. The alert appears in the **less-important (top) events tier** and is gated behind a new in-game setting (on by default). ## Why Previously there was no notification to the player who *lost* a trade ship — only the capturer got a transient +gold pip on the ship's arrival. This surfaces the loss to the victim, while letting players opt out if they find it noisy. ## Changes - **`src/core/execution/TradeShipExecution.ts`** — On capture detection, emit a display message (`events_display.trade_ship_captured`, type `UNIT_DESTROYED`) to the original owner. Fires once, guarded by the existing `wasCaptured` flag. `UNIT_DESTROYED` is not a Tier-1 type, so it lands in the top/less-important tier. - **`src/client/hud/layers/EventsDisplay.ts`** — Suppress the message when the setting is off, following the existing key-based filter pattern. - **`src/core/game/UserSettings.ts`** — New `tradeShipCapturedEvents()` getter (default `true`) + `toggleTradeShipCapturedEvents()`. - **`src/client/hud/layers/SettingsModal.ts`** — New toggle in the in-game settings modal. - **`resources/lang/en.json`** — New `events_display.trade_ship_captured` and `user_setting.trade_ship_captured_label`/`_desc` keys. - **`tests/core/executions/TradeShipExecution.test.ts`** — Tests that the notification is sent to the original owner with the right args and only once across ticks. ## Notes - The setting is gated client-side (in `EventsDisplay`), keeping `src/core` free of client-local localStorage settings — consistent with how display events are already filtered there. - Reused `MessageType.UNIT_DESTROYED` (red/"loss" styling) rather than adding a new message type, to keep the change minimal. Happy to add a dedicated type/color if preferred. ## Testing - `npx vitest tests/core/executions/TradeShipExecution.test.ts --run` — 7 passed - lint clean, no type errors 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
222 lines
6.1 KiB
TypeScript
222 lines
6.1 KiB
TypeScript
import { renderNumber } from "../../client/Utils";
|
|
import {
|
|
Execution,
|
|
Game,
|
|
MessageType,
|
|
Player,
|
|
Unit,
|
|
UnitType,
|
|
} from "../game/Game";
|
|
import { TileRef } from "../game/GameMap";
|
|
import { WaterPathFinder } from "../pathfinding/PathFinder";
|
|
import { PathStatus } from "../pathfinding/types";
|
|
import { findClosestBy } from "../Util";
|
|
|
|
export class TradeShipExecution implements Execution {
|
|
private active = true;
|
|
private mg: Game;
|
|
private tradeShip: Unit | undefined;
|
|
private wasCaptured = false;
|
|
private pathFinder: WaterPathFinder;
|
|
private tilesTraveled = 0;
|
|
private motionPlanId = 1;
|
|
private motionPlanDst: TileRef | null = null;
|
|
|
|
private static _staggerCounter = 0;
|
|
|
|
constructor(
|
|
private origOwner: Player,
|
|
private srcPort: Unit,
|
|
private _dstPort: Unit,
|
|
) {}
|
|
|
|
init(mg: Game, ticks: number): void {
|
|
this.mg = mg;
|
|
const stagger =
|
|
TradeShipExecution._staggerCounter++ % WaterPathFinder.STAGGER_SPREAD;
|
|
this.pathFinder = new WaterPathFinder(mg, stagger);
|
|
}
|
|
|
|
tick(ticks: number): void {
|
|
if (this.pathFinder.rebuilt) {
|
|
this.motionPlanDst = null; // Force motion plan re-recording
|
|
}
|
|
|
|
if (this.tradeShip === undefined) {
|
|
const spawn = this.origOwner.canBuild(
|
|
UnitType.TradeShip,
|
|
this.srcPort.tile(),
|
|
);
|
|
if (spawn === false) {
|
|
console.warn(`cannot build trade ship`);
|
|
this.active = false;
|
|
return;
|
|
}
|
|
this.tradeShip = this.origOwner.buildUnit(UnitType.TradeShip, spawn, {
|
|
targetUnit: this._dstPort,
|
|
lastSetSafeFromPirates: ticks,
|
|
});
|
|
this.mg.stats().boatSendTrade(this.origOwner, this._dstPort.owner());
|
|
}
|
|
|
|
if (!this.tradeShip.isActive()) {
|
|
this.active = false;
|
|
return;
|
|
}
|
|
|
|
const tradeShipOwner = this.tradeShip.owner();
|
|
const dstPortOwner = this._dstPort.owner();
|
|
if (this.wasCaptured !== true && this.origOwner !== tradeShipOwner) {
|
|
// Store as variable in case ship is recaptured by previous owner
|
|
this.wasCaptured = true;
|
|
this.mg.displayMessage(
|
|
"events_display.trade_ship_captured",
|
|
MessageType.UNIT_DESTROYED,
|
|
this.origOwner.id(),
|
|
undefined,
|
|
{ name: tradeShipOwner.displayName() },
|
|
this.tradeShip.id(),
|
|
);
|
|
}
|
|
|
|
// If a player captures another player's port while trading we should delete
|
|
// the ship.
|
|
if (dstPortOwner.id() === this.srcPort.owner().id()) {
|
|
this.tradeShip.delete(false);
|
|
this.active = false;
|
|
return;
|
|
}
|
|
|
|
if (
|
|
!this.wasCaptured &&
|
|
(!this._dstPort.isActive() || !tradeShipOwner.canTrade(dstPortOwner))
|
|
) {
|
|
this.tradeShip.delete(false);
|
|
this.active = false;
|
|
return;
|
|
}
|
|
|
|
const curTile = this.tradeShip.tile();
|
|
|
|
if (
|
|
this.wasCaptured &&
|
|
(tradeShipOwner !== dstPortOwner || !this._dstPort.isActive())
|
|
) {
|
|
const myComponent = this.mg.getWaterComponent(curTile);
|
|
const nearestPort = findClosestBy(
|
|
tradeShipOwner.units(UnitType.Port),
|
|
(port) => this.mg.manhattanDist(port.tile(), curTile),
|
|
(port) =>
|
|
port.isActive() &&
|
|
!port.isMarkedForDeletion() &&
|
|
!port.isUnderConstruction() &&
|
|
myComponent !== null &&
|
|
this.mg.hasWaterComponent(port.tile(), myComponent),
|
|
);
|
|
if (nearestPort === null) {
|
|
this.tradeShip.delete(false);
|
|
this.active = false;
|
|
return;
|
|
} else {
|
|
this._dstPort = nearestPort;
|
|
this.tradeShip.setTargetUnit(this._dstPort);
|
|
// Plan-driven units don't emit per-tick unit updates, so force a sync for the new target.
|
|
this.tradeShip.touch();
|
|
}
|
|
}
|
|
|
|
if (curTile === this.dstPort()) {
|
|
this.complete();
|
|
return;
|
|
}
|
|
|
|
const dst = this._dstPort.tile();
|
|
const result = this.pathFinder.next(curTile, dst);
|
|
|
|
switch (result.status) {
|
|
case PathStatus.NEXT:
|
|
if (dst !== this.motionPlanDst) {
|
|
this.motionPlanId++;
|
|
const from = result.node;
|
|
const path = this.pathFinder.findPath(from, dst) ?? [from];
|
|
if (path.length === 0 || path[0] !== from) {
|
|
path.unshift(from);
|
|
}
|
|
|
|
this.mg.recordMotionPlan({
|
|
kind: "grid",
|
|
unitId: this.tradeShip.id(),
|
|
planId: this.motionPlanId,
|
|
startTick: ticks + 1,
|
|
ticksPerStep: 1,
|
|
path,
|
|
});
|
|
this.motionPlanDst = dst;
|
|
}
|
|
// Update safeFromPirates status
|
|
if (this.mg.isWater(result.node) && this.mg.isShoreline(result.node)) {
|
|
this.tradeShip.setSafeFromPirates();
|
|
}
|
|
this.tradeShip.move(result.node);
|
|
this.tilesTraveled++;
|
|
break;
|
|
case PathStatus.COMPLETE:
|
|
this.complete();
|
|
return;
|
|
case PathStatus.NOT_FOUND:
|
|
console.warn("captured trade ship cannot find route");
|
|
if (this.tradeShip.isActive()) {
|
|
this.tradeShip.delete(false);
|
|
}
|
|
this.active = false;
|
|
return;
|
|
}
|
|
}
|
|
|
|
private complete() {
|
|
this.active = false;
|
|
this.tradeShip!.delete(false);
|
|
const gold = this.mg
|
|
.config()
|
|
.tradeShipGold(this.tilesTraveled, this.tradeShip!.owner());
|
|
|
|
if (this.wasCaptured) {
|
|
this.tradeShip!.owner().addGold(gold, this._dstPort.tile());
|
|
this.mg.displayMessage(
|
|
"events_display.received_gold_from_captured_ship",
|
|
MessageType.CAPTURED_ENEMY_UNIT,
|
|
this.tradeShip!.owner().id(),
|
|
gold,
|
|
{
|
|
gold: renderNumber(gold),
|
|
name: this.origOwner.displayName(),
|
|
},
|
|
);
|
|
// Record stats
|
|
this.mg
|
|
.stats()
|
|
.boatCapturedTrade(this.tradeShip!.owner(), this.origOwner, gold);
|
|
} else {
|
|
this.srcPort.owner().addGold(gold, this.srcPort.tile());
|
|
this._dstPort.owner().addGold(gold, this._dstPort.tile());
|
|
// Record stats
|
|
this.mg
|
|
.stats()
|
|
.boatArriveTrade(this.srcPort.owner(), this._dstPort.owner(), gold);
|
|
}
|
|
return;
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.active;
|
|
}
|
|
|
|
activeDuringSpawnPhase(): boolean {
|
|
return false;
|
|
}
|
|
|
|
dstPort(): TileRef {
|
|
return this._dstPort.tile();
|
|
}
|
|
}
|