Files
OpenFrontIO/src/core/execution/MIRVExecution.ts
T
Sam Bokai af86a9222f Feature: Enable FakeHumans ("Nation Bots") to Launch MIRVs Strategically (#2225)
## Description:

> [!IMPORTANT]
> Try here: https://mirv-test.openfront.dev/ 

> [!NOTE]
> Blocks PRs:
> - #2244 
> - #2263

### Summary
Implements intelligent MIRV usage for fakehuman players, enabling them
to make strategic nuclear strikes based on game state analysis.
 
### Changes

#### Core FakeHuman Strategy (`FakeHumanExecution.ts`)
- **MIRV Decision System**: Added `considerMIRV()` method that evaluates
game state and determines optimal MIRV usage
- **Three Strategic Targeting Modes**:
1. **Counter-MIRV**: Retaliatory strikes against players actively
launching MIRVs at the fakehuman
2. **Victory Denial**: Preemptive strikes against players approaching
win conditions
     - Team threshold: n% of total land (configurable)
     - Individual threshold: n% of total land (configurable)
3. **Steamroll Prevention**: Strikes against players with dominant city
counts (n% ahead of next competitor)

#### FakeHuman Behavior Tuning
- **Cooldown System**: n-minute cooldown between MIRV attempts
(configurable)
- **Failure Rate**: ~n% chance of cooldown trigger without launch
(simulates human hesitation/resource management; configurable)
- **Territory Targeting**: Centers MIRV strikes on enemy territory
center-of-mass for maximum impact

#### Technical Improvements
- **Type Safety**: Updated `UnitParamsMap` to include `targetTile`
parameter for MIRV units
- **Execution Flow**: Integrated MIRV consideration into fakehuman tick
cycle outside of standard attack logic, due to its holistic strategic
nature

### Game Balance Impact
- **FakeHuman Threat Level**: Increases late-game fakehuman
competitiveness
- **Endgame Dynamics**: Prevents runaway victories, extends game tension

### Breaking Changes
None - purely additive feature

### Related GitHub Issues:
-  #2205 

------
## Other

- [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

Discord Username: samsammiliah

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Evan <evanpelle@gmail.com>
2025-10-29 16:39:31 -07:00

193 lines
4.9 KiB
TypeScript

import {
Execution,
Game,
MessageType,
Player,
TerraNullius,
Unit,
UnitType,
} from "../game/Game";
import { TileRef } from "../game/GameMap";
import { ParabolaPathFinder } from "../pathfinding/PathFinding";
import { PseudoRandom } from "../PseudoRandom";
import { simpleHash } from "../Util";
import { NukeExecution } from "./NukeExecution";
export class MirvExecution implements Execution {
private active = true;
private mg: Game;
private nuke: Unit | null = null;
private mirvRange = 1500;
private warheadCount = 350;
private random: PseudoRandom;
private pathFinder: ParabolaPathFinder;
private targetPlayer: Player | TerraNullius;
private separateDst: TileRef;
private speed: number = -1;
constructor(
private player: Player,
private dst: TileRef,
) {}
init(mg: Game, ticks: number): void {
this.random = new PseudoRandom(mg.ticks() + simpleHash(this.player.id()));
this.mg = mg;
this.pathFinder = new ParabolaPathFinder(mg);
this.targetPlayer = this.mg.owner(this.dst);
this.speed = this.mg.config().defaultNukeSpeed();
// Record stats
this.mg.stats().bombLaunch(this.player, this.targetPlayer, UnitType.MIRV);
// Betrayal on launch
if (this.targetPlayer.isPlayer()) {
const alliance = this.player.allianceWith(this.targetPlayer);
if (alliance !== null) {
this.player.breakAlliance(alliance);
}
if (this.targetPlayer !== this.player) {
this.targetPlayer.updateRelation(this.player, -100);
}
}
}
tick(ticks: number): void {
if (this.nuke === null) {
const spawn = this.player.canBuild(UnitType.MIRV, this.dst);
if (spawn === false) {
console.warn(`cannot build MIRV`);
this.active = false;
return;
}
this.nuke = this.player.buildUnit(UnitType.MIRV, spawn, {
targetTile: this.dst,
});
const x = Math.floor(
(this.mg.x(this.dst) + this.mg.x(this.mg.x(this.nuke.tile()))) / 2,
);
const y = Math.max(0, this.mg.y(this.dst) - 500) + 50;
this.separateDst = this.mg.ref(x, y);
this.pathFinder.computeControlPoints(spawn, this.separateDst);
this.mg.displayIncomingUnit(
this.nuke.id(),
// TODO TranslateText
`⚠️⚠️⚠️ ${this.player.name()} - MIRV INBOUND ⚠️⚠️⚠️`,
MessageType.MIRV_INBOUND,
this.targetPlayer.id(),
);
}
const result = this.pathFinder.nextTile(this.speed);
if (result === true) {
this.separate();
this.active = false;
// Record stats
this.mg.stats().bombLand(this.player, this.targetPlayer, UnitType.MIRV);
return;
} else {
this.nuke.move(result);
}
}
private separate() {
if (this.nuke === null) throw new Error("uninitialized");
const dsts: TileRef[] = [this.dst];
let attempts = 1000;
while (attempts > 0 && dsts.length < this.warheadCount) {
attempts--;
const potential = this.randomLand(this.dst, dsts);
if (potential === null) {
continue;
}
dsts.push(potential);
}
console.log(`dsts: ${dsts.length}`);
dsts.sort(
(a, b) =>
this.mg.manhattanDist(b, this.dst) - this.mg.manhattanDist(a, this.dst),
);
console.log(`got ${dsts.length} dsts!!`);
for (const [i, dst] of dsts.entries()) {
this.mg.addExecution(
new NukeExecution(
UnitType.MIRVWarhead,
this.player,
dst,
this.nuke.tile(),
15 + Math.floor((i / this.warheadCount) * 5),
// this.random.nextInt(5, 9),
this.random.nextInt(0, 15),
),
);
}
this.nuke.delete(false);
}
randomLand(ref: TileRef, taken: TileRef[]): TileRef | null {
let tries = 0;
const mirvRange2 = this.mirvRange * this.mirvRange;
while (tries < 100) {
tries++;
const x = this.random.nextInt(
this.mg.x(ref) - this.mirvRange,
this.mg.x(ref) + this.mirvRange,
);
const y = this.random.nextInt(
this.mg.y(ref) - this.mirvRange,
this.mg.y(ref) + this.mirvRange,
);
if (!this.mg.isValidCoord(x, y)) {
continue;
}
const tile = this.mg.ref(x, y);
if (!this.mg.isLand(tile)) {
continue;
}
if (this.mg.euclideanDistSquared(tile, ref) > mirvRange2) {
continue;
}
if (this.mg.owner(tile) !== this.targetPlayer) {
continue;
}
if (this.proximityCheck(tile, taken)) {
continue;
}
return tile;
}
console.log("couldn't find place, giving up");
return null;
}
private proximityCheck(tile: TileRef, taken: TileRef[]): boolean {
for (const t of taken) {
if (this.mg.manhattanDist(tile, t) < 55) {
return true;
}
}
return false;
}
owner(): Player {
return this.player;
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}