mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 10:54:16 +00:00
70745faac4
## Description: Improve type safety and runtime correctness by: 1. Enabling TypeScript's [strictNullChecks](https://www.typescriptlang.org/tsconfig/#strictNullChecks) compiler option. 2. Replacing all loose equality operators (`==` and `!=`) with strict equality operators (`===` and `!==`). 3. Cleaning up of type declarations, null handling logic, and equality expressions throughout the project. Currently, the code allows implicit assumptions that `null` and `undefined` are interchangeable, and relies on type-coercing equality checks that can introduce subtle bugs. These practices make it difficult to reason about when values may be absent and hinder the effectiveness of static analysis. Migrating to strict null checks and enforcing strict equality comparisons will clarify intent, reduce bugs, and make the codebase safer and easier to maintain. Fixes #466 ## 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 --------- Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com> Co-authored-by: evanpelle <openfrontio@gmail.com>
84 lines
2.0 KiB
TypeScript
84 lines
2.0 KiB
TypeScript
import {
|
|
Execution,
|
|
Game,
|
|
MessageType,
|
|
Player,
|
|
Unit,
|
|
UnitType,
|
|
} from "../game/Game";
|
|
import { TileRef } from "../game/GameMap";
|
|
import { AirPathFinder } from "../pathfinding/PathFinding";
|
|
import { PseudoRandom } from "../PseudoRandom";
|
|
|
|
export class SAMMissileExecution implements Execution {
|
|
private active = true;
|
|
private pathFinder: AirPathFinder;
|
|
private SAMMissile: Unit | undefined;
|
|
private mg: Game;
|
|
|
|
constructor(
|
|
private spawn: TileRef,
|
|
private _owner: Player,
|
|
private ownerUnit: Unit,
|
|
private target: Unit,
|
|
private speed: number = 12,
|
|
) {}
|
|
|
|
init(mg: Game, ticks: number): void {
|
|
this.pathFinder = new AirPathFinder(mg, new PseudoRandom(mg.ticks()));
|
|
this.mg = mg;
|
|
}
|
|
|
|
tick(ticks: number): void {
|
|
if (this.SAMMissile === undefined) {
|
|
this.SAMMissile = this._owner.buildUnit(
|
|
UnitType.SAMMissile,
|
|
this.spawn,
|
|
{},
|
|
);
|
|
}
|
|
if (!this.SAMMissile.isActive()) {
|
|
this.active = false;
|
|
return;
|
|
}
|
|
// Mirv warheads are too fast, and mirv shouldn't be stopped ever
|
|
const nukesWhitelist = [UnitType.AtomBomb, UnitType.HydrogenBomb];
|
|
if (
|
|
!this.target.isActive() ||
|
|
!this.ownerUnit.isActive() ||
|
|
this.target.owner() === this.SAMMissile.owner() ||
|
|
!nukesWhitelist.includes(this.target.type())
|
|
) {
|
|
this.SAMMissile.delete(false);
|
|
this.active = false;
|
|
return;
|
|
}
|
|
for (let i = 0; i < this.speed; i++) {
|
|
const result = this.pathFinder.nextTile(
|
|
this.SAMMissile.tile(),
|
|
this.target.tile(),
|
|
);
|
|
if (result === true) {
|
|
this.mg.displayMessage(
|
|
`Missile intercepted ${this.target.type()}`,
|
|
MessageType.SUCCESS,
|
|
this._owner.id(),
|
|
);
|
|
this.active = false;
|
|
this.target.delete();
|
|
this.SAMMissile.delete(false);
|
|
return;
|
|
} else {
|
|
this.SAMMissile.move(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.active;
|
|
}
|
|
activeDuringSpawnPhase(): boolean {
|
|
return false;
|
|
}
|
|
}
|