Nations target structures with nukes (#393)

## Description:

Nations will prefer to target their nukes at structures. This updates
the existing logic, which attempts ten times to randomly select a
coordinate 15 tiles inside the border, to instead generate a list of ten
random tiles in addition to a list of all of the tiles of relevant
structures. The two lists are concatenated to create a set of tile
candidates. These candidates are scored, and the tile with the highest
score, if one is found, is nuked.

The scoring function considers three factors:
1. Damage potential: values of structures with 25 tiles of the target
tile.
2. Distance from silo to target (hang time).
3. Recently nuked locations.


![image](https://github.com/user-attachments/assets/10c7bd96-ad51-404c-84c9-c0840dc8250c)

Fixes #471 

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

fake.neo

---------

Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
This commit is contained in:
Scott Anderson
2025-04-17 22:22:35 -04:00
committed by GitHub
parent 8925f48ba7
commit ccadd7f6a8
+91 -12
View File
@@ -13,9 +13,10 @@ import {
TerrainType,
TerraNullius,
Tick,
Unit,
UnitType,
} from "../game/Game";
import { manhattanDistFN, TileRef } from "../game/GameMap";
import { euclDistFN, manhattanDistFN, TileRef } from "../game/GameMap";
import { PseudoRandom } from "../PseudoRandom";
import { GameID } from "../Schemas";
import { calculateBoundingBox, simpleHash } from "../Util";
@@ -40,6 +41,7 @@ export class FakeHumanExecution implements Execution {
private lastEnemyUpdateTick: number = 0;
private lastEmojiSent = new Map<Player, Tick>();
private lastNukeSent: [Tick, TileRef][] = [];
private embargoMalusApplied = new Set<PlayerID>();
constructor(
@@ -288,32 +290,109 @@ export class FakeHumanExecution implements Execution {
}
private maybeSendNuke(other: Player) {
const silos = this.player.units(UnitType.MissileSilo);
if (
this.player.units(UnitType.MissileSilo).length == 0 ||
silos.length == 0 ||
this.player.gold() <
this.mg.config().unitInfo(UnitType.AtomBomb).cost(this.player) ||
this.player.isOnSameTeam(other)
) {
return;
}
outer: for (let i = 0; i < 10; i++) {
const tile = this.randTerritoryTile(other);
if (tile == null) {
return;
}
const structures = other.units(
UnitType.City,
UnitType.DefensePost,
UnitType.MissileSilo,
UnitType.Port,
UnitType.SAMLauncher,
);
const structureTiles = structures.map((u) => u.tile());
const randomTiles: TileRef[] = new Array(10);
for (let i = 0; i < randomTiles.length; i++) {
randomTiles[i] = this.randTerritoryTile(other);
}
const allTiles = randomTiles.concat(structureTiles);
let bestTile = null;
let bestValue = 0;
this.removeOldNukeEvents();
outer: for (const tile of new Set(allTiles)) {
if (tile == null) continue;
for (const t of this.mg.bfs(tile, manhattanDistFN(tile, 15))) {
// Make sure we nuke at least 15 tiles in border
if (this.mg.owner(t) != other) {
continue outer;
}
}
if (this.player.canBuild(UnitType.AtomBomb, tile)) {
this.mg.addExecution(
new NukeExecution(UnitType.AtomBomb, this.player.id(), tile),
);
return;
if (!this.player.canBuild(UnitType.AtomBomb, tile)) continue;
const value = this.nukeTileScore(tile, silos, structures);
if (value > bestTile) {
bestTile = tile;
bestValue = value;
}
}
if (bestTile != null) {
this.sendNuke(bestTile);
}
}
private removeOldNukeEvents() {
const maxAge = 500;
const tick = this.mg.ticks();
while (
this.lastNukeSent.length > 0 &&
this.lastNukeSent[0][0] + maxAge < tick
) {
this.lastNukeSent.shift();
}
}
private sendNuke(tile: TileRef) {
const tick = this.mg.ticks();
this.lastNukeSent.push([tick, tile]);
this.mg.addExecution(
new NukeExecution(UnitType.AtomBomb, this.player.id(), tile),
);
}
private nukeTileScore(tile: TileRef, silos: Unit[], targets: Unit[]): number {
// Potential damage in a 25-tile radius
const dist = euclDistFN(tile, 25, false);
let tileValue = targets
.filter((unit) => dist(this.mg, unit.tile()))
.map((unit) => {
switch (unit.type()) {
case UnitType.City:
return 25_000;
case UnitType.DefensePost:
return 5_000;
case UnitType.MissileSilo:
return 50_000;
case UnitType.Port:
return 10_000;
case UnitType.SAMLauncher:
return 5_000;
default:
return 0;
}
})
.reduce((prev, cur) => prev + cur, 0);
// Prefer tiles that are closer to a silo
const siloTiles = silos.map((u) => u.tile());
const { x: closestSilo } = closestTwoTiles(this.mg, siloTiles, [tile]);
const distanceSquared = this.mg.euclideanDistSquared(tile, closestSilo);
const distanceToClosestSilo = Math.sqrt(distanceSquared);
tileValue -= distanceToClosestSilo * 30;
// Don't target near recent targets
tileValue -= this.lastNukeSent
.filter(([_tick, tile]) => dist(this.mg, tile))
.map((_) => 1_000_000)
.reduce((prev, cur) => prev + cur, 0);
return tileValue;
}
private maybeSendBoatAttack(other: Player) {