import { consolex } from "../Consolex"; import { Execution, Game, MessageType, Player, PlayerID, Unit, UnitType, } from "../game/Game"; import { TileRef } from "../game/GameMap"; import { PseudoRandom } from "../PseudoRandom"; import { SAMMissileExecution } from "./SAMMissileExecution"; export class SAMLauncherExecution implements Execution { private player: Player; private mg: Game; private sam: Unit; private active: boolean = true; private target: Unit | null = null; private searchRangeRadius = 75; private pseudoRandom: PseudoRandom; constructor( private ownerId: PlayerID, private tile: TileRef, ) {} init(mg: Game, ticks: number): void { this.mg = mg; if (!mg.hasPlayer(this.ownerId)) { console.warn(`SAMLauncherExecution: owner ${this.ownerId} not found`); this.active = false; return; } this.player = mg.player(this.ownerId); } tick(ticks: number): void { if (this.sam == null) { const spawnTile = this.player.canBuild(UnitType.SAMLauncher, this.tile); if (spawnTile == false) { consolex.warn("cannot build SAM Launcher"); this.active = false; return; } this.sam = this.player.buildUnit(UnitType.SAMLauncher, 0, spawnTile, { cooldownDuration: this.mg.config().SAMCooldown(), }); } if (!this.sam.isActive()) { this.active = false; return; } if (this.player != this.sam.owner()) { this.player = this.sam.owner(); } if (!this.pseudoRandom) { this.pseudoRandom = new PseudoRandom(this.sam.id()); } const nukes = this.mg .nearbyUnits(this.sam.tile(), this.searchRangeRadius, [ UnitType.AtomBomb, UnitType.HydrogenBomb, ]) .filter( ({ unit }) => unit.owner() !== this.player && !this.player.isFriendly(unit.owner()), ); this.target = 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; if ( this.sam.isCooldown() && this.sam.ticksLeftInCooldown(this.mg.config().SAMCooldown()) == 0 ) { this.sam.setCooldown(false); } if (this.target && !this.sam.isCooldown() && !this.target.targetedBySAM()) { this.sam.setCooldown(true); const random = this.pseudoRandom.next(); const hit = random < this.mg.config().samHittingChance(); if (!hit) { this.mg.displayMessage( `Missile failed to intercept ${this.target.type()}`, MessageType.ERROR, this.sam.owner().id(), ); } else { this.target.setTargetedBySAM(true); this.mg.addExecution( new SAMMissileExecution( this.sam.tile(), this.sam.owner(), this.sam, this.target, ), ); } } } isActive(): boolean { return this.active; } activeDuringSpawnPhase(): boolean { return false; } }