1
0
Fork 0

Fixed Xander not being able to use Disengage

This commit is contained in:
Michaël Lemaire 2018-03-05 22:28:06 +01:00
parent 851c59bac1
commit 41f9b1ad6f
12 changed files with 46 additions and 47 deletions

View file

@ -128,6 +128,7 @@ module TK.SpaceTac {
// Move or approach needed ? // Move or approach needed ?
let move_target: Target | null = null; let move_target: Target | null = null;
let move_action: MoveAction | null = null;
result.move_location = Target.newFromShip(this.ship); result.move_location = Target.newFromShip(this.ship);
if (action instanceof MoveAction) { if (action instanceof MoveAction) {
let corrected_target = action.applyReachableRange(this.ship, target, move_margin); let corrected_target = action.applyReachableRange(this.ship, target, move_margin);
@ -136,11 +137,12 @@ module TK.SpaceTac {
result.need_move = target.getDistanceTo(this.ship.location) > 0; result.need_move = target.getDistanceTo(this.ship.location) > 0;
move_target = corrected_target; move_target = corrected_target;
} }
move_action = action;
} else { } else {
let engine = this.findEngine(); move_action = this.findEngine();
if (engine) { if (move_action) {
let approach_radius = action.getRangeRadius(this.ship); let approach_radius = action.getRangeRadius(this.ship);
let approach = this.getApproach(engine, target, approach_radius, move_margin); let approach = this.getApproach(move_action, target, approach_radius, move_margin);
if (approach instanceof Target) { if (approach instanceof Target) {
result.need_move = true; result.need_move = true;
move_target = approach; move_target = approach;
@ -157,18 +159,15 @@ module TK.SpaceTac {
} }
// Check move AP // Check move AP
if (result.need_move && move_target) { if (result.need_move && move_target && move_action) {
let engine = this.findEngine(); result.total_move_ap = move_action.getPowerUsage(this.ship, move_target);
if (engine) { result.can_move = ap > 0;
result.total_move_ap = engine.getActionPointsUsage(this.ship, move_target); result.can_end_move = result.total_move_ap <= ap;
result.can_move = ap > 0; result.move_location = move_target;
result.can_end_move = result.total_move_ap <= ap; // TODO Split in "this turn" part and "next turn" part if needed
result.move_location = move_target; result.parts.push({ action: move_action, target: move_target, ap: result.total_move_ap, possible: result.can_move });
// TODO Split in "this turn" part and "next turn" part if needed
result.parts.push({ action: engine, target: move_target, ap: result.total_move_ap, possible: result.can_move });
ap -= result.total_move_ap; ap -= result.total_move_ap;
}
} }
// Check action AP // Check action AP
@ -176,7 +175,7 @@ module TK.SpaceTac {
result.success = result.need_move && result.can_move; result.success = result.need_move && result.can_move;
} else { } else {
result.need_fire = true; result.need_fire = true;
result.total_fire_ap = action.getActionPointsUsage(this.ship, target); result.total_fire_ap = action.getPowerUsage(this.ship, target);
result.can_fire = result.total_fire_ap <= ap; result.can_fire = result.total_fire_ap <= ap;
result.fire_location = target; result.fire_location = target;
result.parts.push({ action: action, target: target, ap: result.total_fire_ap, possible: (!result.need_move || result.can_end_move) && result.can_fire }); result.parts.push({ action: action, target: target, ap: result.total_fire_ap, possible: (!result.need_move || result.can_end_move) && result.can_fire });

View file

@ -39,9 +39,9 @@ module TK.SpaceTac.Specs {
]); ]);
}) })
test.case("checks against remaining AP", check => { test.case("checks against remaining power", check => {
let action = new BaseAction("test"); let action = new BaseAction("test");
check.patch(action, "getActionPointsUsage", () => 3); check.patch(action, "getPowerUsage", () => 3);
let ship = new Ship(); let ship = new Ship();
check.equals(action.checkCannotBeApplied(ship), "action not available"); check.equals(action.checkCannotBeApplied(ship), "action not available");

View file

@ -105,7 +105,7 @@ module TK.SpaceTac {
if (remaining_ap === null) { if (remaining_ap === null) {
remaining_ap = ship.getValue("power"); remaining_ap = ship.getValue("power");
} }
var ap_usage = this.getActionPointsUsage(ship, null); var ap_usage = this.getPowerUsage(ship, null);
if (remaining_ap < ap_usage) { if (remaining_ap < ap_usage) {
return "not enough power"; return "not enough power";
} }
@ -119,11 +119,11 @@ module TK.SpaceTac {
} }
/** /**
* Get the number of action points the action applied to a target would use * Get the power usage, for applying this action on an hypothetical target
* *
* If target is null, an estimated cost is returned. * If target is null, an estimated cost is returned.
*/ */
getActionPointsUsage(ship: Ship, target: Target | null): number { getPowerUsage(ship: Ship, target: Target | null): number {
return 0; return 0;
} }
@ -196,7 +196,7 @@ module TK.SpaceTac {
result.push(new ShipActionUsedDiff(ship, this, target)); result.push(new ShipActionUsedDiff(ship, this, target));
// Power usage // Power usage
let cost = this.getActionPointsUsage(ship, target); let cost = this.getPowerUsage(ship, target);
if (cost) { if (cost) {
result = result.concat(ship.getValueDiffs("power", -cost, true)); result = result.concat(ship.getValueDiffs("power", -cost, true));
} }
@ -232,7 +232,7 @@ module TK.SpaceTac {
return false; return false;
} }
let cost = this.getActionPointsUsage(ship, checked_target); let cost = this.getPowerUsage(ship, checked_target);
if (ship.getValue("power") < cost) { if (ship.getValue("power") < cost) {
console.warn("Action rejected - not enough power", ship, this, checked_target); console.warn("Action rejected - not enough power", ship, this, checked_target);
return false; return false;

View file

@ -22,7 +22,7 @@ module TK.SpaceTac {
return this.name; return this.name;
} }
getActionPointsUsage(ship: Ship, target: Target | null): number { getPowerUsage(ship: Ship, target: Target | null): number {
let toggled_cost = isum(imap(ship.iToggleActions(true), action => action.power)); let toggled_cost = isum(imap(ship.iToggleActions(true), action => action.power));
return ship.getValue("power") + toggled_cost - ship.getAttribute("power_capacity"); return ship.getValue("power") + toggled_cost - ship.getAttribute("power_capacity");
} }

View file

@ -10,7 +10,7 @@ module TK.SpaceTac.Specs {
var action = new MoveAction("Engine", { distance_per_power: 10 }); var action = new MoveAction("Engine", { distance_per_power: 10 });
ship.actions.addCustom(action); ship.actions.addCustom(action);
check.equals(action.getDistanceByActionPoint(ship), 10); check.equals(action.getDistanceByPower(ship), 10);
var result = action.checkTarget(ship, Target.newFromLocation(0, 20)); var result = action.checkTarget(ship, Target.newFromLocation(0, 20));
check.equals(result, Target.newFromLocation(0, 20)); check.equals(result, Target.newFromLocation(0, 20));
@ -125,19 +125,19 @@ module TK.SpaceTac.Specs {
let action = new MoveAction("Engine", { distance_per_power: 100, maneuvrability_factor: 60 }); let action = new MoveAction("Engine", { distance_per_power: 100, maneuvrability_factor: 60 });
TestTools.setAttribute(ship, "maneuvrability", 0); TestTools.setAttribute(ship, "maneuvrability", 0);
check.nears(action.getDistanceByActionPoint(ship), 40); check.nears(action.getDistanceByPower(ship), 40);
TestTools.setAttribute(ship, "maneuvrability", 1); TestTools.setAttribute(ship, "maneuvrability", 1);
check.nears(action.getDistanceByActionPoint(ship), 60); check.nears(action.getDistanceByPower(ship), 60);
TestTools.setAttribute(ship, "maneuvrability", 2); TestTools.setAttribute(ship, "maneuvrability", 2);
check.nears(action.getDistanceByActionPoint(ship), 70); check.nears(action.getDistanceByPower(ship), 70);
TestTools.setAttribute(ship, "maneuvrability", 10); TestTools.setAttribute(ship, "maneuvrability", 10);
check.nears(action.getDistanceByActionPoint(ship), 90); check.nears(action.getDistanceByPower(ship), 90);
action = new MoveAction("Engine", { distance_per_power: 100, maneuvrability_factor: 0 }); action = new MoveAction("Engine", { distance_per_power: 100, maneuvrability_factor: 0 });
TestTools.setAttribute(ship, "maneuvrability", 0); TestTools.setAttribute(ship, "maneuvrability", 0);
check.nears(action.getDistanceByActionPoint(ship), 100); check.nears(action.getDistanceByPower(ship), 100);
TestTools.setAttribute(ship, "maneuvrability", 10); TestTools.setAttribute(ship, "maneuvrability", 10);
check.nears(action.getDistanceByActionPoint(ship), 100); check.nears(action.getDistanceByPower(ship), 100);
}); });
test.case("builds a textual description", check => { test.case("builds a textual description", check => {

View file

@ -67,10 +67,10 @@ module TK.SpaceTac {
} }
} }
getActionPointsUsage(ship: Ship, target: Target | null): number { getPowerUsage(ship: Ship, target: Target | null): number {
if (target) { if (target) {
let distance = Target.newFromShip(ship).getDistanceTo(target); let distance = Target.newFromShip(ship).getDistanceTo(target);
return Math.ceil(distance / this.getDistanceByActionPoint(ship)); return Math.ceil(distance / this.getDistanceByPower(ship));
} else { } else {
return 0; return 0;
} }
@ -84,26 +84,26 @@ module TK.SpaceTac {
* Get the distance reachable with a given power * Get the distance reachable with a given power
*/ */
getRangeRadiusForPower(ship: Ship, power = ship.getValue("power")): number { getRangeRadiusForPower(ship: Ship, power = ship.getValue("power")): number {
return power * this.getDistanceByActionPoint(ship); return power * this.getDistanceByPower(ship);
} }
/** /**
* Get the distance range that may be traveled with 1 action point * Get the distance range that may be traveled with 1 power point
* *
* The actual range will then depend on the ship maneuvrability * The actual range will then depend on the ship maneuvrability
*/ */
getDistanceRangeByActionPoint(): IntegerRange { getDistanceRangeByPower(): IntegerRange {
let min_distance = Math.ceil(this.distance_per_power * (1 - this.maneuvrability_factor * 0.01)); let min_distance = Math.ceil(this.distance_per_power * (1 - this.maneuvrability_factor * 0.01));
return new IntegerRange(min_distance, this.distance_per_power); return new IntegerRange(min_distance, this.distance_per_power);
} }
/** /**
* Get the distance that may be traveled with 1 action point * Get the distance that may be traveled with 1 power point
*/ */
getDistanceByActionPoint(ship: Ship): number { getDistanceByPower(ship: Ship): number {
let maneuvrability = Math.max(ship.getAttribute("maneuvrability"), 0); let maneuvrability = Math.max(ship.getAttribute("maneuvrability"), 0);
let factor = maneuvrability / (maneuvrability + 2); let factor = maneuvrability / (maneuvrability + 2);
let range = this.getDistanceRangeByActionPoint(); let range = this.getDistanceRangeByPower();
return range.getProportional(factor); return range.getProportional(factor);
} }
@ -147,7 +147,7 @@ module TK.SpaceTac {
} }
getEffectsDescription(): string { getEffectsDescription(): string {
let range = this.getDistanceRangeByActionPoint(); let range = this.getDistanceRangeByPower();
let rangeinfo = (range.max == range.min) ? `${range.min}` : `${range.min}-${range.max}`; let rangeinfo = (range.max == range.min) ? `${range.min}` : `${range.min}-${range.max}`;
let result = `Move: ${rangeinfo}km per power point`; let result = `Move: ${rangeinfo}km per power point`;

View file

@ -50,7 +50,7 @@ module TK.SpaceTac {
} }
} }
getActionPointsUsage(ship: Ship, target: Target | null): number { getPowerUsage(ship: Ship, target: Target | null): number {
return ship.actions.isToggled(this) ? -this.power : this.power; return ship.actions.isToggled(this) ? -this.power : this.power;
} }

View file

@ -91,7 +91,7 @@ module TK.SpaceTac {
} }
} }
getActionPointsUsage(ship: Ship, target: Target | null): number { getPowerUsage(ship: Ship, target: Target | null): number {
return this.power; return this.power;
} }

View file

@ -15,7 +15,7 @@ module TK.SpaceTac.UI.Specs {
check.same(icon.img_power.visible, false, "no change"); check.same(icon.img_power.visible, false, "no change");
let cost = 3; let cost = 3;
check.patch(action, "getActionPointsUsage", () => cost); check.patch(action, "getPowerUsage", () => cost);
icon.refresh(); icon.refresh();
check.in("power cost = 3", check => { check.in("power cost = 3", check => {
check.equals(icon.img_power.visible, true); check.equals(icon.img_power.visible, true);

View file

@ -225,7 +225,7 @@ module TK.SpaceTac.UI {
} }
// left // left
let cost = this.action.getActionPointsUsage(this.ship, null); let cost = this.action.getPowerUsage(this.ship, null);
this.img_power.visible = bool(cost); this.img_power.visible = bool(cost);
this.text_power.text = `${Math.abs(cost)}\n${cost < 0 ? "+" : "-"}`; this.text_power.text = `${Math.abs(cost)}\n${cost < 0 ? "+" : "-"}`;
this.text_power.fill = (cost > 0) ? "#ffdd4b" : "#dbe748"; this.text_power.fill = (cost > 0) ? "#ffdd4b" : "#dbe748";

View file

@ -19,10 +19,10 @@ module TK.SpaceTac.UI {
if (ship.getValue("power") == 0) { if (ship.getValue("power") == 0) {
cost = "Not enough power"; cost = "Not enough power";
} else { } else {
cost = `Cost: 1 power per ${action.getDistanceByActionPoint(ship)}km`; cost = `Cost: 1 power per ${action.getDistanceByPower(ship)}km`;
} }
} else { } else {
let power_usage = action.getActionPointsUsage(ship, null); let power_usage = action.getPowerUsage(ship, null);
if (power_usage) { if (power_usage) {
if (ship.getValue("power") < power_usage) { if (ship.getValue("power") < power_usage) {
cost = "Not enough power"; cost = "Not enough power";

View file

@ -112,7 +112,7 @@ module TK.SpaceTac.UI {
let move = part.action instanceof MoveAction; let move = part.action instanceof MoveAction;
let color = (enabled && part.possible) ? (move ? 0xe09c47 : 0xdc6441) : 0x8e8e8e; let color = (enabled && part.possible) ? (move ? 0xe09c47 : 0xdc6441) : 0x8e8e8e;
let src = previous ? previous.target : this.ship.location; let src = previous ? previous.target : this.ship.location;
let gradation = (part.action instanceof MoveAction) ? part.action.getDistanceByActionPoint(this.ship) : 0; let gradation = (part.action instanceof MoveAction) ? part.action.getDistanceByPower(this.ship) : 0;
this.drawVector(color, src.x, src.y, part.target.x, part.target.y, gradation); this.drawVector(color, src.x, src.y, part.target.x, part.target.y, gradation);
} }
@ -265,7 +265,7 @@ module TK.SpaceTac.UI {
if (move_action) { if (move_action) {
let power = this.ship.getValue("power"); let power = this.ship.getValue("power");
if (this.action !== move_action) { if (this.action !== move_action) {
power = Math.max(power - this.action.getActionPointsUsage(this.ship, this.target), 0); power = Math.max(power - this.action.getPowerUsage(this.ship, this.target), 0);
} }
let radius = move_action.getRangeRadiusForPower(this.ship, power); let radius = move_action.getRangeRadiusForPower(this.ship, power);
this.range_hint.update(this.ship, move_action, radius); this.range_hint.update(this.ship, move_action, radius);