mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 18:06:39 +00:00
be01b90b25
## Description: Enable a few eslint rules: - `@typescript-eslint/no-empty-object-type` - `@typescript-eslint/no-require-imports` - `no-useless-escape` ## 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 - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors --------- Co-authored-by: Scott Anderson <scottanderson@users.noreply.github.com>
210 lines
5.4 KiB
TypeScript
210 lines
5.4 KiB
TypeScript
import {
|
|
Execution,
|
|
Game,
|
|
MessageType,
|
|
Player,
|
|
Unit,
|
|
UnitType,
|
|
} from "../game/Game";
|
|
import { TileRef } from "../game/GameMap";
|
|
import { PseudoRandom } from "../PseudoRandom";
|
|
import { SAMMissileExecution } from "./SAMMissileExecution";
|
|
|
|
export class SAMLauncherExecution implements Execution {
|
|
private mg: Game;
|
|
private active: boolean = true;
|
|
|
|
// As MIRV go very fast we have to detect them very early but we only
|
|
// shoot the one targeting very close (MIRVWarheadProtectionRadius)
|
|
private MIRVWarheadSearchRadius = 400;
|
|
private MIRVWarheadProtectionRadius = 50;
|
|
|
|
private pseudoRandom: PseudoRandom | undefined;
|
|
|
|
constructor(
|
|
private player: Player,
|
|
private tile: TileRef | null,
|
|
private sam: Unit | null = null,
|
|
) {
|
|
if (sam !== null) {
|
|
this.tile = sam.tile();
|
|
}
|
|
}
|
|
|
|
init(mg: Game, ticks: number): void {
|
|
this.mg = mg;
|
|
}
|
|
|
|
private getSingleTarget(): Unit | null {
|
|
if (this.sam === null) return null;
|
|
const nukes = this.mg.nearbyUnits(
|
|
this.sam.tile(),
|
|
this.mg.config().defaultSamRange(),
|
|
[UnitType.AtomBomb, UnitType.HydrogenBomb],
|
|
({ unit }) =>
|
|
unit.owner() !== this.player &&
|
|
!this.player.isFriendly(unit.owner()) &&
|
|
unit.isTargetable(),
|
|
);
|
|
|
|
return (
|
|
nukes.sort((a, b) => {
|
|
const { unit: unitA, distSquared: distA } = a;
|
|
const { unit: unitB, distSquared: distB } = b;
|
|
|
|
// Prioritize Hydrogen Bombs
|
|
if (
|
|
unitA.type() === UnitType.HydrogenBomb &&
|
|
unitB.type() !== UnitType.HydrogenBomb
|
|
)
|
|
return -1;
|
|
if (
|
|
unitA.type() !== UnitType.HydrogenBomb &&
|
|
unitB.type() === UnitType.HydrogenBomb
|
|
)
|
|
return 1;
|
|
|
|
// If both are the same type, sort by distance (lower `distSquared` means closer)
|
|
return distA - distB;
|
|
})[0]?.unit ?? null
|
|
);
|
|
}
|
|
|
|
private isHit(type: UnitType, random: number): boolean {
|
|
if (type === UnitType.AtomBomb) {
|
|
return true;
|
|
}
|
|
|
|
if (type === UnitType.MIRVWarhead) {
|
|
return random < this.mg.config().samWarheadHittingChance();
|
|
}
|
|
|
|
return random < this.mg.config().samHittingChance();
|
|
}
|
|
|
|
tick(ticks: number): void {
|
|
if (this.mg === null || this.player === null) {
|
|
throw new Error("Not initialized");
|
|
}
|
|
if (this.sam === null) {
|
|
if (this.tile === null) {
|
|
throw new Error("tile is null");
|
|
}
|
|
const spawnTile = this.player.canBuild(UnitType.SAMLauncher, this.tile);
|
|
if (spawnTile === false) {
|
|
console.warn("cannot build SAM Launcher");
|
|
this.active = false;
|
|
return;
|
|
}
|
|
this.sam = this.player.buildUnit(UnitType.SAMLauncher, spawnTile, {});
|
|
}
|
|
if (!this.sam.isActive()) {
|
|
this.active = false;
|
|
return;
|
|
}
|
|
|
|
if (this.player !== this.sam.owner()) {
|
|
this.player = this.sam.owner();
|
|
}
|
|
|
|
this.pseudoRandom ??= new PseudoRandom(this.sam.id());
|
|
|
|
const mirvWarheadTargets = this.mg.nearbyUnits(
|
|
this.sam.tile(),
|
|
this.MIRVWarheadSearchRadius,
|
|
UnitType.MIRVWarhead,
|
|
({ unit }) => {
|
|
if (unit.owner() === this.player) return false;
|
|
if (this.player.isFriendly(unit.owner())) return false;
|
|
const dst = unit.targetTile();
|
|
return (
|
|
this.sam !== null &&
|
|
dst !== undefined &&
|
|
this.mg.manhattanDist(dst, this.sam.tile()) <
|
|
this.MIRVWarheadProtectionRadius
|
|
);
|
|
},
|
|
);
|
|
|
|
let target: Unit | null = null;
|
|
if (mirvWarheadTargets.length === 0) {
|
|
target = this.getSingleTarget();
|
|
}
|
|
|
|
const isSingleTarget = target && !target.targetedBySAM();
|
|
if (
|
|
(isSingleTarget || mirvWarheadTargets.length > 0) &&
|
|
!this.sam.isInCooldown()
|
|
) {
|
|
this.sam.launch();
|
|
const type =
|
|
mirvWarheadTargets.length > 0 ? UnitType.MIRVWarhead : target?.type();
|
|
if (type === undefined) throw new Error("Unknown unit type");
|
|
const random = this.pseudoRandom.next();
|
|
const hit = this.isHit(type, random);
|
|
if (!hit) {
|
|
this.mg.displayMessage(
|
|
`Missile failed to intercept ${type}`,
|
|
MessageType.SAM_MISS,
|
|
this.sam.owner().id(),
|
|
);
|
|
} else if (mirvWarheadTargets.length > 0) {
|
|
const samOwner = this.sam.owner();
|
|
|
|
// Message
|
|
this.mg.displayMessage(
|
|
`${mirvWarheadTargets.length} MIRV warheads intercepted`,
|
|
MessageType.SAM_HIT,
|
|
samOwner.id(),
|
|
);
|
|
|
|
mirvWarheadTargets.forEach(({ unit: u }) => {
|
|
// Delete warheads
|
|
u.delete();
|
|
});
|
|
|
|
// Record stats
|
|
this.mg
|
|
.stats()
|
|
.bombIntercept(
|
|
samOwner,
|
|
UnitType.MIRVWarhead,
|
|
mirvWarheadTargets.length,
|
|
);
|
|
} else if (target !== null) {
|
|
target.setTargetedBySAM(true);
|
|
this.mg.addExecution(
|
|
new SAMMissileExecution(
|
|
this.sam.tile(),
|
|
this.sam.owner(),
|
|
this.sam,
|
|
target,
|
|
),
|
|
);
|
|
} else {
|
|
throw new Error("target is null");
|
|
}
|
|
}
|
|
|
|
const frontTime = this.sam.missileTimerQueue()[0];
|
|
if (frontTime === undefined) {
|
|
return;
|
|
}
|
|
|
|
const cooldown =
|
|
this.mg.config().SAMCooldown() - (this.mg.ticks() - frontTime);
|
|
|
|
if (cooldown <= 0) {
|
|
this.sam.reloadMissile();
|
|
}
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.active;
|
|
}
|
|
|
|
activeDuringSpawnPhase(): boolean {
|
|
return false;
|
|
}
|
|
}
|