MissileSilo cooldown (#309)

changed the sam cooldown to be a general cooldown function and added a
missilesilo cooldown

The function either adds the current tick as the starting point of the
cooldown or sets the cooldown to null and updates the unit. That way
getcooldown function can be used to get back the tick the cooldown was
started and can be compared to the cooldown set in the config.

changed the sam test / added a missilesilo test

---------

Co-authored-by: evanpelle <evanpelle@gmail.com>
This commit is contained in:
Readixyee
2025-03-29 15:19:07 -07:00
committed by GitHub
co-authored by evanpelle
parent 72016f3dd4
commit 0891637eb2
14 changed files with 204 additions and 59 deletions
+10 -1
View File
@@ -41,12 +41,21 @@ export class MissileSiloExecution implements Execution {
this.active = false;
return;
}
this.silo = this.player.buildUnit(UnitType.MissileSilo, 0, this.tile);
this.silo = this.player.buildUnit(UnitType.MissileSilo, 0, this.tile, {
cooldownDuration: this.mg.config().SiloCooldown(),
});
if (this.player != this.silo.owner()) {
this.player = this.silo.owner();
}
}
if (
this.silo.isCooldown() &&
this.silo.ticksLeftInCooldown(this.mg.config().SiloCooldown()) == 0
) {
this.silo.setCooldown(false);
}
}
isActive(): boolean {
+6
View File
@@ -83,6 +83,12 @@ export class NukeExecution implements Execution {
this.nuke.type() as NukeType,
);
}
// after sending an nuke set the missilesilo on cooldown
const silo = this.player
.units(UnitType.MissileSilo)
.find((silo) => silo.tile() === spawn);
silo.setCooldown(true);
}
// make the nuke unactive if it was intercepted
+21 -30
View File
@@ -16,15 +16,13 @@ import { PseudoRandom } from "../PseudoRandom";
export class SAMLauncherExecution implements Execution {
private player: Player;
private mg: Game;
private post: Unit;
private sam: Unit;
private active: boolean = true;
private target: Unit = null;
private searchRangeRadius = 75;
private lastMissileAttack = 0;
private pseudoRandom: PseudoRandom;
constructor(
@@ -43,30 +41,32 @@ export class SAMLauncherExecution implements Execution {
}
tick(ticks: number): void {
if (this.post == null) {
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.post = this.player.buildUnit(UnitType.SAMLauncher, 0, spawnTile);
this.sam = this.player.buildUnit(UnitType.SAMLauncher, 0, spawnTile, {
cooldownDuration: this.mg.config().SAMCooldown(),
});
}
if (!this.post.isActive()) {
if (!this.sam.isActive()) {
this.active = false;
return;
}
if (this.player != this.post.owner()) {
this.player = this.post.owner();
if (this.player != this.sam.owner()) {
this.player = this.sam.owner();
}
if (!this.pseudoRandom) {
this.pseudoRandom = new PseudoRandom(this.post.id());
this.pseudoRandom = new PseudoRandom(this.sam.id());
}
const nukes = this.mg
.nearbyUnits(this.post.tile(), this.searchRangeRadius, [
.nearbyUnits(this.sam.tile(), this.searchRangeRadius, [
UnitType.AtomBomb,
UnitType.HydrogenBomb,
])
@@ -96,39 +96,30 @@ export class SAMLauncherExecution implements Execution {
return distA - distB;
})[0]?.unit ?? null;
const cooldown =
this.lastMissileAttack != 0 &&
this.mg.ticks() - this.lastMissileAttack <=
this.mg.config().samCooldown();
if (this.post.isSamCooldown() && !cooldown) {
this.post.setSamCooldown(false);
if (
this.sam.isCooldown() &&
this.sam.ticksLeftInCooldown(this.mg.config().SAMCooldown()) == 0
) {
this.sam.setCooldown(false);
}
if (
this.target &&
!this.post.isSamCooldown() &&
!this.target.targetedBySAM()
) {
this.lastMissileAttack = this.mg.ticks();
this.post.setSamCooldown(true);
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();
this.lastMissileAttack = this.mg.ticks();
if (!hit) {
this.mg.displayMessage(
`Missile failed to intercept ${this.target.type()}`,
MessageType.ERROR,
this.post.owner().id(),
this.sam.owner().id(),
);
} else {
this.target.setTargetedBySAM(true);
this.mg.addExecution(
new SAMMissileExecution(
this.post.tile(),
this.post.owner(),
this.post,
this.sam.tile(),
this.sam.owner(),
this.sam,
this.target,
),
);