115 lines
3.7 KiB
TypeScript
115 lines
3.7 KiB
TypeScript
module TK.SpaceTac {
|
|
/**
|
|
* Configuration of a trigger action
|
|
*/
|
|
export interface MoveActionConfig {
|
|
// Distance allowed for each power point
|
|
distance_per_power: number
|
|
}
|
|
|
|
/**
|
|
* Action to move the ship to a specific location
|
|
*/
|
|
export class MoveAction extends BaseAction implements MoveActionConfig {
|
|
distance_per_power = 0
|
|
|
|
constructor(name = "Engine", config?: Partial<MoveActionConfig>, code = "move") {
|
|
super(name, code);
|
|
|
|
if (config) {
|
|
this.configureEngine(config);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure the engine
|
|
*/
|
|
configureEngine(config: Partial<MoveActionConfig>): void {
|
|
copyfields(config, this);
|
|
}
|
|
|
|
getVerb(ship: Ship): string {
|
|
return "Move";
|
|
}
|
|
|
|
getTitle(ship: Ship): string {
|
|
return `Use ${this.name}`;
|
|
}
|
|
|
|
getTargettingMode(ship: Ship): ActionTargettingMode {
|
|
return ActionTargettingMode.SPACE;
|
|
}
|
|
|
|
getDefaultTarget(ship: Ship): Target {
|
|
return Target.newFromLocation(ship.arena_x + Math.cos(ship.arena_angle) * 100, ship.arena_y + Math.sin(ship.arena_angle) * 100);
|
|
}
|
|
|
|
checkCannotBeApplied(ship: Ship, remaining_ap: number | null = null): ActionUnavailability | null {
|
|
let base = super.checkCannotBeApplied(ship, Infinity);
|
|
if (base) {
|
|
return base;
|
|
}
|
|
|
|
// Check AP usage
|
|
if (remaining_ap === null) {
|
|
remaining_ap = ship.getValue("power");
|
|
}
|
|
if (remaining_ap < 0.0001) {
|
|
return ActionUnavailability.POWER;
|
|
}
|
|
|
|
// Check vigilance actions
|
|
if (any(ship.getToggleActions(true), action => action instanceof VigilanceAction)) {
|
|
return ActionUnavailability.VIGILANCE;
|
|
}
|
|
|
|
// Check pinned status
|
|
if (any(ship.getEffects(), effect => effect instanceof PinnedEffect)) {
|
|
return ActionUnavailability.PINNED;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
getPowerUsage(ship: Ship, target: Target | null): number {
|
|
if (this.distance_per_power == 0) {
|
|
return Infinity;
|
|
} else if (target) {
|
|
let distance = ship.grid.measure(ship.location, target);
|
|
return Math.ceil(distance / this.distance_per_power);
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
getRangeRadius(ship: Ship): number {
|
|
return ship.getValue("power") * this.getDistancePerPower(ship);
|
|
}
|
|
|
|
getDistancePerPower(ship: Ship): number {
|
|
return this.distance_per_power * ship.grid.getUnit();
|
|
}
|
|
|
|
checkLocationTarget(ship: Ship, target: Target): boolean {
|
|
if (!ship.grid.check(target) || ship.grid.measure(ship.location, target) < 1e-8) {
|
|
return false;
|
|
}
|
|
|
|
// TODO Check the space is not occupied
|
|
|
|
return ship.getValue("power") >= this.getPowerUsage(ship, target);
|
|
}
|
|
|
|
protected getSpecificDiffs(ship: Ship, battle: Battle, target: Target): BaseBattleDiff[] {
|
|
let angle = (arenaDistance(target, ship.location) < 1e-8) ? ship.arena_angle : arenaAngle(ship.location, target);
|
|
let destination = new ArenaLocationAngle(target.x, target.y, angle);
|
|
return [new ShipMoveDiff(ship, ship.location, destination, this)];
|
|
}
|
|
|
|
getEffectsDescription(): string {
|
|
let result = `Move: ${this.distance_per_power}km per power point`;
|
|
return result;
|
|
}
|
|
}
|
|
}
|