mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-23 03:25:40 +00:00
1d0732d3d9
## Description: By using a type union we get better type safety, enforcing each unit type have the appropriate params when initializing ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors ## Please put your Discord username so you can be contacted if a bug or regression is found: evan --------- Co-authored-by: evan <openfrontio@gmail.com>
137 lines
3.4 KiB
TypeScript
137 lines
3.4 KiB
TypeScript
import { consolex } from "../Consolex";
|
|
import {
|
|
Execution,
|
|
Game,
|
|
Player,
|
|
PlayerID,
|
|
Unit,
|
|
UnitType,
|
|
} from "../game/Game";
|
|
import { TileRef } from "../game/GameMap";
|
|
import { ShellExecution } from "./ShellExecution";
|
|
|
|
export class DefensePostExecution implements Execution {
|
|
private player: Player;
|
|
private mg: Game;
|
|
private post: Unit;
|
|
private active: boolean = true;
|
|
|
|
private target: Unit = null;
|
|
private lastShellAttack = 0;
|
|
|
|
private alreadySentShell = new Set<Unit>();
|
|
|
|
constructor(
|
|
private ownerId: PlayerID,
|
|
private tile: TileRef,
|
|
) {}
|
|
|
|
init(mg: Game, ticks: number): void {
|
|
this.mg = mg;
|
|
if (!mg.hasPlayer(this.ownerId)) {
|
|
console.warn(`DefensePostExectuion: owner ${this.ownerId} not found`);
|
|
this.active = false;
|
|
return;
|
|
}
|
|
this.player = mg.player(this.ownerId);
|
|
}
|
|
|
|
private shoot() {
|
|
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 == null) {
|
|
const spawnTile = this.player.canBuild(UnitType.DefensePost, this.tile);
|
|
if (spawnTile == false) {
|
|
consolex.warn("cannot build Defense Post");
|
|
this.active = false;
|
|
return;
|
|
}
|
|
this.post = this.player.buildUnit(UnitType.DefensePost, spawnTile, {});
|
|
}
|
|
if (!this.post.isActive()) {
|
|
this.active = false;
|
|
return;
|
|
}
|
|
|
|
if (this.player != this.post.owner()) {
|
|
this.player = this.post.owner();
|
|
}
|
|
|
|
if (this.target != null && !this.target.isActive()) {
|
|
this.target = null;
|
|
}
|
|
|
|
// TODO: Reconsider how/if defense posts target ships.
|
|
return;
|
|
|
|
const ships = this.mg
|
|
.nearbyUnits(
|
|
this.post.tile(),
|
|
this.mg.config().defensePostTargettingRange(),
|
|
[UnitType.TransportShip, UnitType.Warship],
|
|
)
|
|
.filter(
|
|
({ unit }) =>
|
|
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;
|
|
}
|
|
}
|