1
0
Fork 0

Fixed approach simulator moving because of float rounding errors

This commit is contained in:
Michaël Lemaire 2017-06-25 19:48:34 +02:00
parent b6f80caf70
commit 1649096209
4 changed files with 17 additions and 7 deletions

View file

@ -155,5 +155,16 @@ module TS.SpaceTac.Specs {
expect(result.need_fire).toBe(false);
expect(result.parts).toEqual([]);
});
it("does not move if already in range, even if in the safety margin", function () {
let [ship, simulator, action] = simpleWeaponCase(100);
let result = simulator.simulateAction(action, new Target(ship.arena_x + 97, ship.arena_y, null), 5);
expect(result.success).toBe(true);
expect(result.need_move).toBe(false);
result = simulator.simulateAction(action, new Target(ship.arena_x + 101, ship.arena_y, null), 5);
expect(result.success).toBe(true);
expect(result.need_move).toBe(true);
expect(result.move_location).toEqual(new Target(ship.arena_x + 6, ship.arena_y));
});
});
}

View file

@ -90,7 +90,7 @@ module TS.SpaceTac {
*
* Return null if no approach vector was found.
*/
getApproach(action: MoveAction, target: Target, radius: number): Target | ApproachSimulationError {
getApproach(action: MoveAction, target: Target, radius: number, margin = 0): Target | ApproachSimulationError {
let dx = target.x - this.ship.arena_x;
let dy = target.y - this.ship.arena_y;
let distance = Math.sqrt(dx * dx + dy * dy);
@ -98,6 +98,9 @@ module TS.SpaceTac {
if (distance <= radius) {
return ApproachSimulationError.NO_MOVE_NEEDED;
} else {
if (margin && radius > margin) {
radius -= margin;
}
let factor = (distance - radius) / distance;
let candidate = new Target(this.ship.arena_x + dx * factor, this.ship.arena_y + dy * factor);
if (this.canMoveTo(action, candidate)) {
@ -140,10 +143,7 @@ module TS.SpaceTac {
let engine = this.findBestEngine();
if (engine && engine.action instanceof MoveAction) {
let approach_radius = action.getRangeRadius(this.ship);
if (move_margin && approach_radius > move_margin) {
approach_radius -= move_margin;
}
let approach = this.getApproach(engine.action, target, approach_radius);
let approach = this.getApproach(engine.action, target, approach_radius, move_margin);
if (approach instanceof Target) {
result.need_move = true;
move_target = approach;

View file

@ -23,7 +23,7 @@ module TS.SpaceTac {
// List of guessed effects of this maneuver
effects: [Ship, BaseEffect][]
constructor(ship: Ship, action: BaseAction, target: Target, move_margin = 0.1) {
constructor(ship: Ship, action: BaseAction, target: Target, move_margin = 1) {
this.ship = ship;
this.battle = nn(ship.getBattle());
this.action = action;

View file

@ -17,7 +17,6 @@ module TS.SpaceTac {
expect(ship.getAttribute("shield_capacity")).toBe(30);
expect(ship.getValue("shield")).toBe(30);
console.log(battle.log.events);
expect(battle.log.events).toEqual([
new ActiveEffectsEvent(ship, [new AttributeEffect("hull_capacity", 100), new AttributeEffect("shield_capacity", 50)], [effect]),
new ValueChangeEvent(ship, new ShipValue("shield", 30, 50), -10),