Files
OpenFrontIO/src/core/execution/ShellExecution.ts
T
Kipstz Avenger 86a329f7cb Add random shell damage (#1665)
Closes #555 

## Description:

Added random damage for warship shells and defence posts.
Damage mapping: roll 1-5 → 200, 225, 250, 275, 300 damage respectively.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I have read and accepted the CLA agreement (only required once).

## Please put your Discord username so you can be contacted if a bug or
regression is found:

kipstzz
2025-08-02 22:08:17 +00:00

85 lines
2.3 KiB
TypeScript

import { Execution, Game, Player, Unit, UnitType } from "../game/Game";
import { TileRef } from "../game/GameMap";
import { AirPathFinder } from "../pathfinding/PathFinding";
import { PseudoRandom } from "../PseudoRandom";
export class ShellExecution implements Execution {
private active = true;
private pathFinder: AirPathFinder;
private shell: Unit | undefined;
private mg: Game;
private destroyAtTick: number = -1;
private random: PseudoRandom;
constructor(
private spawn: TileRef,
private _owner: Player,
private ownerUnit: Unit,
private target: Unit,
) {}
init(mg: Game, ticks: number): void {
this.pathFinder = new AirPathFinder(mg, new PseudoRandom(mg.ticks()));
this.mg = mg;
this.random = new PseudoRandom(mg.ticks());
}
tick(ticks: number): void {
this.shell ??= this._owner.buildUnit(UnitType.Shell, this.spawn, {});
if (!this.shell.isActive()) {
this.active = false;
return;
}
if (
!this.target.isActive() ||
this.target.owner() === this.shell.owner() ||
(this.destroyAtTick !== -1 && this.mg.ticks() >= this.destroyAtTick)
) {
this.shell.delete(false);
this.active = false;
return;
}
if (this.destroyAtTick === -1 && !this.ownerUnit.isActive()) {
this.destroyAtTick = this.mg.ticks() + this.mg.config().shellLifetime();
}
for (let i = 0; i < 3; i++) {
const result = this.pathFinder.nextTile(
this.shell.tile(),
this.target.tile(),
);
if (result === true) {
this.active = false;
this.target.modifyHealth(-this.effectOnTarget(), this._owner);
this.shell.setReachedTarget();
this.shell.delete(false);
return;
} else {
this.shell.move(result);
}
}
}
private effectOnTarget(): number {
const { damage } = this.mg.config().unitInfo(UnitType.Shell);
const baseDamage = damage ?? 250;
const roll = this.random.nextInt(1, 6);
const damageMultiplier = (roll - 1) * 25 + 200;
return Math.round((baseDamage / 250) * damageMultiplier);
}
public getEffectOnTargetForTesting(): number {
return this.effectOnTarget();
}
isActive(): boolean {
return this.active;
}
activeDuringSpawnPhase(): boolean {
return false;
}
}