Files
OpenFrontIO/src/core/execution/SAMLauncherExecution.ts
T
FloPinguin 3013133d08 Embrace the aftergame! 😄 (#3410)
## Description:

In v30 we have the following change to prevent teammates from destroying
your structures:

**Block nuking teammate structures** - Nukes blocked if they'd hit a
teammate's structure (that was possible by nuking oceans / rivers) (by
@FloPinguin)

Original idea was from Wonder.
I think it makes sense, but it has a side effect: The aftergame, which
many players love, will be dead because of this change.

<img width="835" height="103" alt="image"
src="https://github.com/user-attachments/assets/521b7915-be28-4d83-8d45-65835e7385ab"
/>

<img width="1101" height="105" alt="image"
src="https://github.com/user-attachments/assets/db74a9c6-da12-44a2-aa06-f042b8e58b8a"
/>

I think a lot of complaints will follow after v30 is live. 

So why not add a little bit of logic for the aftergame?
After a team wins/loses, players can nuke their teammates. No longer
need to aim for water. SAMs also intercept teammate nukes in this phase.

## 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:

FloPinguin
2026-03-11 20:39:39 -07:00

370 lines
10 KiB
TypeScript

import {
Execution,
Game,
isUnit,
MessageType,
Player,
Unit,
UnitType,
} from "../game/Game";
import { TileRef } from "../game/GameMap";
import { PseudoRandom } from "../PseudoRandom";
import { SAMMissileExecution } from "./SAMMissileExecution";
type Target = {
unit: Unit;
tile: TileRef;
};
type InterceptionTile = {
tile: TileRef;
tick: number;
};
/**
* Smart SAM targeting system preshoting nukes so its range is strictly enforced
*/
class SAMTargetingSystem {
// Interception tiles are computed a single time, but it may not be reachable yet.
// Store the result so it can be intercepted at the proper time, rather than recomputing each tick.
// Null interception tile means there are no interception tiles in range. Store it to avoid recomputing.
private readonly precomputedNukes: Map<number, InterceptionTile | null> =
new Map();
private readonly missileSpeed: number;
constructor(
private readonly mg: Game,
private readonly sam: Unit,
) {
this.missileSpeed = this.mg.config().defaultSamMissileSpeed();
}
updateUnreachableNukes(nearbyUnits: { unit: Unit; distSquared: number }[]) {
if (this.precomputedNukes.size === 0) {
return;
}
// Avoid per-tick allocations for the common case where only a few nukes are tracked.
if (this.precomputedNukes.size <= 16) {
for (const nukeId of this.precomputedNukes.keys()) {
let found = false;
for (const u of nearbyUnits) {
if (u.unit.id() === nukeId) {
found = true;
break;
}
}
if (!found) {
this.precomputedNukes.delete(nukeId);
}
}
return;
}
const nearbyUnitSet = new Set<number>();
for (const u of nearbyUnits) {
nearbyUnitSet.add(u.unit.id());
}
for (const nukeId of this.precomputedNukes.keys()) {
if (!nearbyUnitSet.has(nukeId)) {
this.precomputedNukes.delete(nukeId);
}
}
}
private tickToReach(currentTile: TileRef, tile: TileRef): number {
return Math.ceil(
this.mg.manhattanDist(currentTile, tile) / this.missileSpeed,
);
}
private computeInterceptionTile(
unit: Unit,
samTile: TileRef,
rangeSquared: number,
): InterceptionTile | undefined {
const trajectory = unit.trajectory();
const currentIndex = unit.trajectoryIndex();
const explosionTick: number = trajectory.length - currentIndex;
for (let i = currentIndex; i < trajectory.length; i++) {
const trajectoryTile = trajectory[i];
if (
trajectoryTile.targetable &&
this.mg.euclideanDistSquared(samTile, trajectoryTile.tile) <=
rangeSquared
) {
const nukeTickToReach = i - currentIndex;
const samTickToReach = this.tickToReach(samTile, trajectoryTile.tile);
const tickBeforeShooting = nukeTickToReach - samTickToReach;
if (samTickToReach < explosionTick && tickBeforeShooting >= 0) {
return { tick: tickBeforeShooting, tile: trajectoryTile.tile };
}
}
}
return undefined;
}
public getSingleTarget(ticks: number): Target | null {
const samTile = this.sam.tile();
const range = this.mg.config().samRange(this.sam.level());
const rangeSquared = range * range;
// Look beyond the SAM range so it can preshot nukes
const detectionRange = this.mg.config().maxSamRange() * 2;
const nukes = this.mg.nearbyUnits(
samTile,
detectionRange,
[UnitType.AtomBomb, UnitType.HydrogenBomb],
({ unit }) => {
if (!isUnit(unit) || unit.targetedBySAM()) return false;
if (unit.owner() === this.sam.owner()) return false;
const samOwner = this.sam.owner();
const nukeOwner = unit.owner();
// After game-over in team games, SAMs also target teammate nukes (aftergame fun)
if (samOwner.isFriendly(nukeOwner)) {
return (
this.mg.getWinner() !== null && samOwner.isOnSameTeam(nukeOwner)
);
}
return true;
},
);
// Clear unreachable nukes that went out of range
this.updateUnreachableNukes(nukes);
let best: Target | null = null;
for (const nuke of nukes) {
const nukeId = nuke.unit.id();
const cached = this.precomputedNukes.get(nukeId);
if (cached !== undefined) {
if (cached === null) {
// Already computed as unreachable, skip
continue;
}
if (cached.tick === ticks) {
// Time to shoot!
const target = { tile: cached.tile, unit: nuke.unit };
if (
best === null ||
(target.unit.type() === UnitType.HydrogenBomb &&
best.unit.type() !== UnitType.HydrogenBomb)
) {
best = target;
}
this.precomputedNukes.delete(nukeId);
continue;
}
if (cached.tick > ticks) {
// Not due yet, skip for now.
continue;
}
// Missed the planned tick (e.g was on cooldown), recompute a new interception tile if possible
this.precomputedNukes.delete(nukeId);
}
const interceptionTile = this.computeInterceptionTile(
nuke.unit,
samTile,
rangeSquared,
);
if (interceptionTile !== undefined) {
if (interceptionTile.tick <= 1) {
// Shoot instantly
const target = { unit: nuke.unit, tile: interceptionTile.tile };
if (
best === null ||
(target.unit.type() === UnitType.HydrogenBomb &&
best.unit.type() !== UnitType.HydrogenBomb)
) {
best = target;
}
} else {
// Nuke will be reachable but not yet. Store the result.
this.precomputedNukes.set(nukeId, {
tick: interceptionTile.tick + ticks,
tile: interceptionTile.tile,
});
}
} else {
// Store unreachable nukes in order to prevent useless interception computation
this.precomputedNukes.set(nukeId, null);
}
}
return best;
}
}
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 targetingSystem: SAMTargetingSystem;
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;
}
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, {});
}
this.targetingSystem ??= new SAMTargetingSystem(this.mg, this.sam);
if (this.sam.isUnderConstruction()) {
return;
}
if (this.sam.isInCooldown()) {
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();
}
return;
}
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 (!isUnit(unit)) return false;
if (unit.owner() === this.player) return false;
// After game-over in team games, SAMs also target teammate MIRVs (aftergame fun)
const nukeOwner = unit.owner();
if (this.player.isFriendly(nukeOwner)) {
if (
this.mg.getWinner() === null ||
!this.player.isOnSameTeam(nukeOwner)
) {
return false;
}
}
const dst = unit.targetTile();
return (
this.sam !== null &&
dst !== undefined &&
this.mg.manhattanDist(dst, this.sam.tile()) <
this.MIRVWarheadProtectionRadius
);
},
);
let target: Target | null = null;
if (mirvWarheadTargets.length === 0) {
target = this.targetingSystem.getSingleTarget(ticks);
if (target !== null) {
console.log("Target acquired");
}
}
// target is already filtered to exclude nukes targeted by other SAMs
if (target || mirvWarheadTargets.length > 0) {
this.sam.launch();
const type =
mirvWarheadTargets.length > 0
? UnitType.MIRVWarhead
: target?.unit.type();
if (type === undefined) throw new Error("Unknown unit type");
if (mirvWarheadTargets.length > 0) {
const samOwner = this.sam.owner();
// Message
this.mg.displayMessage(
"events_display.mirv_warheads_intercepted",
MessageType.SAM_HIT,
samOwner.id(),
undefined,
{ count: mirvWarheadTargets.length },
);
mirvWarheadTargets.forEach(({ unit: u }) => {
// Delete warheads
u.delete();
});
// Record stats
this.mg
.stats()
.bombIntercept(
samOwner,
UnitType.MIRVWarhead,
mirvWarheadTargets.length,
);
} else if (target !== null) {
target.unit.setTargetedBySAM(true);
this.mg.addExecution(
new SAMMissileExecution(
this.sam.tile(),
this.sam.owner(),
this.sam,
target.unit,
target.tile,
),
);
} else {
throw new Error("target is null");
}
}
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}