Files
OpenFrontIO/src/core/execution/NukeExecution.ts
T
Léo Kosman 87fd9c3219 Feat : focus unit when clicking on warnings in chat (#699)
## Description:

Enables you to click on the `Naval invasion incoming from X` or `X -
atom bomb inbound` messages to focus the camera on the incoming unit
(boat or nuke). Works for boats, atom bombs, hydrogen bombs and MIRVs.

Nothing changes in looks, only the fact that the messages are clickable.

closes #641 

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

leo21_
2025-05-14 17:18:46 -04:00

233 lines
6.2 KiB
TypeScript

import { consolex } from "../Consolex";
import {
Execution,
Game,
MessageType,
NukeType,
Player,
PlayerID,
TerraNullius,
Unit,
UnitType,
} from "../game/Game";
import { TileRef } from "../game/GameMap";
import { ParabolaPathFinder } from "../pathfinding/PathFinding";
import { PseudoRandom } from "../PseudoRandom";
export class NukeExecution implements Execution {
private player: Player;
private active = true;
private mg: Game;
private nuke: Unit;
private random: PseudoRandom;
private pathFinder: ParabolaPathFinder;
constructor(
private type: NukeType,
private senderID: PlayerID,
private dst: TileRef,
private src?: TileRef,
private speed: number = -1,
private waitTicks = 0,
) {}
init(mg: Game, ticks: number): void {
if (!mg.hasPlayer(this.senderID)) {
console.warn(`NukeExecution: sender ${this.senderID} not found`);
this.active = false;
return;
}
this.mg = mg;
this.player = mg.player(this.senderID);
this.random = new PseudoRandom(ticks);
if (this.speed == -1) {
this.speed = this.mg.config().defaultNukeSpeed();
}
this.pathFinder = new ParabolaPathFinder(mg);
}
public target(): Player | TerraNullius {
return this.mg.owner(this.dst);
}
private tilesToDestroy(): Set<TileRef> {
const magnitude = this.mg.config().nukeMagnitudes(this.nuke.type());
const rand = new PseudoRandom(this.mg.ticks());
const inner2 = magnitude.inner * magnitude.inner;
const outer2 = magnitude.outer * magnitude.outer;
return this.mg.bfs(this.dst, (_, n: TileRef) => {
const d2 = this.mg.euclideanDistSquared(this.dst, n);
return d2 <= outer2 && (d2 <= inner2 || rand.chance(2));
});
}
private breakAlliances(toDestroy: Set<TileRef>) {
const attacked = new Map<Player, number>();
for (const tile of toDestroy) {
const owner = this.mg.owner(tile);
if (owner.isPlayer()) {
const prev = attacked.get(owner) ?? 0;
attacked.set(owner, prev + 1);
}
}
for (const [other, tilesDestroyed] of attacked) {
if (tilesDestroyed > 100 && this.nuke.type() != UnitType.MIRVWarhead) {
// Mirv warheads shouldn't break alliances
const alliance = this.player.allianceWith(other);
if (alliance != null) {
this.player.breakAlliance(alliance);
}
if (other != this.player) {
other.updateRelation(this.player, -100);
}
}
}
}
tick(ticks: number): void {
if (this.nuke == null) {
const spawn = this.src ?? this.player.canBuild(this.type, this.dst);
if (spawn == false) {
consolex.warn(`cannot build Nuke`);
this.active = false;
return;
}
this.pathFinder.computeControlPoints(
spawn,
this.dst,
this.type != UnitType.MIRVWarhead,
);
this.nuke = this.player.buildUnit(this.type, spawn, {
detonationDst: this.dst,
});
if (this.mg.hasOwner(this.dst)) {
const target = this.mg.owner(this.dst) as Player;
if (this.type == UnitType.AtomBomb) {
this.mg.displayIncomingUnit(
this.nuke.id(),
`${this.player.name()} - atom bomb inbound`,
MessageType.ERROR,
target.id(),
);
}
if (this.type == UnitType.HydrogenBomb) {
this.mg.displayIncomingUnit(
this.nuke.id(),
`${this.player.name()} - hydrogen bomb inbound`,
MessageType.ERROR,
target.id(),
);
}
this.mg
.stats()
.increaseNukeCount(
this.senderID,
target.id(),
this.nuke.type() as NukeType,
);
}
// after sending a nuke set the missilesilo on cooldown
const silo = this.player
.units(UnitType.MissileSilo)
.find((silo) => silo.tile() === spawn);
if (silo) {
silo.setCooldown(true);
}
return;
}
// make the nuke unactive if it was intercepted
if (!this.nuke.isActive()) {
consolex.log(`Nuke destroyed before reaching target`);
this.active = false;
return;
}
if (this.waitTicks > 0) {
this.waitTicks--;
return;
}
// Move to next tile
const nextTile = this.pathFinder.nextTile(this.speed);
if (nextTile === true) {
this.detonate();
return;
} else {
this.nuke.move(nextTile);
}
}
private detonate() {
const magnitude = this.mg.config().nukeMagnitudes(this.nuke.type());
const toDestroy = this.tilesToDestroy();
this.breakAlliances(toDestroy);
for (const tile of toDestroy) {
const owner = this.mg.owner(tile);
if (owner.isPlayer()) {
owner.relinquish(tile);
owner.removeTroops(
this.mg
.config()
.nukeDeathFactor(owner.troops(), owner.numTilesOwned()),
);
owner.removeWorkers(
this.mg
.config()
.nukeDeathFactor(owner.workers(), owner.numTilesOwned()),
);
owner.outgoingAttacks().forEach((attack) => {
const deaths = this.mg
.config()
.nukeDeathFactor(attack.troops(), owner.numTilesOwned());
attack.setTroops(attack.troops() - deaths);
});
owner.units(UnitType.TransportShip).forEach((attack) => {
const deaths = this.mg
.config()
.nukeDeathFactor(attack.troops(), owner.numTilesOwned());
attack.setTroops(attack.troops() - deaths);
});
}
if (this.mg.isLand(tile)) {
this.mg.setFallout(tile, true);
}
}
const outer2 = magnitude.outer * magnitude.outer;
for (const unit of this.mg.units()) {
if (
unit.type() != UnitType.AtomBomb &&
unit.type() != UnitType.HydrogenBomb &&
unit.type() != UnitType.MIRVWarhead &&
unit.type() != UnitType.MIRV
) {
if (this.mg.euclideanDistSquared(this.dst, unit.tile()) < outer2) {
unit.delete();
}
}
}
this.active = false;
this.nuke.delete(false);
}
owner(): Player {
return this.player;
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}