mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-16 15:22:38 +00:00
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:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user