Files
OpenFrontIO/src/core/execution/DefensePostExecution.ts
T
CrackeRR11 8f53785a80 BUG FIX: Gold double deduction + Rmoval of UnitType.Construction (#2378)
## Description:

- Removed the temporary UnitType.Construction and embedded construction
state into real units via isUnderConstruction().
- Centralized non-structure spawning to perform a single validation
right before unit creation/launch.
- Updated UI layers to render construction state without relying on the
removed enum.
- Adjusted and created tests to match the new flow and to cover the
no-refundscenarios.

# Tests updated 
- tests/economy/ConstructionGold.test.ts: covers structure cost
deduction and income, tolerant of passive income; ensures no refunds
during construction.
- tests/nukes/HydrogenAndMirv.test.ts: accounts for single-check launch
flow; MIRV test targets a player-owned tile; ensures launch after
payment.
- tests/client/graphics/UILayer.test.ts: mocks now provide
isUnderConstruction and real type strings;

## Please complete the following:

- [X] I have added screenshots for all UI updates
- [X] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [X] I have added relevant tests to the test directory
- [X] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

CrackeRR1

---------

Co-authored-by: Evan <evanpelle@gmail.com>
2025-11-26 14:45:14 -08:00

109 lines
2.9 KiB
TypeScript

import { Execution, Game, Unit } from "../game/Game";
import { ShellExecution } from "./ShellExecution";
export class DefensePostExecution implements Execution {
private mg: Game;
private active: boolean = true;
private target: Unit | null = null;
private lastShellAttack = 0;
private alreadySentShell = new Set<Unit>();
constructor(private post: Unit) {}
init(mg: Game, ticks: number): void {
this.mg = mg;
}
private shoot() {
if (this.target === null) return;
const shellAttackRate = this.mg.config().defensePostShellAttackRate();
if (this.mg.ticks() - this.lastShellAttack > shellAttackRate) {
this.lastShellAttack = this.mg.ticks();
this.mg.addExecution(
new ShellExecution(
this.post.tile(),
this.post.owner(),
this.post,
this.target,
),
);
if (!this.target.hasHealth()) {
// Don't send multiple shells to target that can be oneshotted
this.alreadySentShell.add(this.target);
this.target = null;
return;
}
}
}
tick(ticks: number): void {
if (!this.post.isActive()) {
this.active = false;
return;
}
// Do nothing while the structure is under construction
if (this.post.isUnderConstruction()) {
return;
}
if (this.target !== null && !this.target.isActive()) {
this.target = null;
}
// TODO: Reconsider how/if defense posts target ships.
// const ships = this.mg
// .nearbyUnits(
// this.post.tile(),
// this.mg.config().defensePostTargettingRange(),
// [UnitType.TransportShip, UnitType.Warship],
// )
// .filter(
// ({ unit }) =>
// this.post !== null &&
// unit.owner() !== this.post.owner() &&
// !unit.owner().isFriendly(this.post.owner()) &&
// !this.alreadySentShell.has(unit),
// );
//
// this.target =
// ships.sort((a, b) => {
// const { unit: unitA, distSquared: distA } = a;
// const { unit: unitB, distSquared: distB } = b;
//
// // Prioritize TransportShip
// if (
// unitA.type() === UnitType.TransportShip &&
// unitB.type() !== UnitType.TransportShip
// )
// return -1;
// if (
// unitA.type() !== UnitType.TransportShip &&
// unitB.type() === UnitType.TransportShip
// )
// return 1;
//
// // If both are the same type, sort by distance (lower `distSquared` means closer)
// return distA - distB;
// })[0]?.unit ?? null;
//
// if (this.target === null || !this.target.isActive()) {
// this.target = null;
// return;
// } else {
// this.shoot();
// return;
// }
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}