Sails command now applies acceleration to boat
- Race only has control over speed while Sails command is not in action - This is governed by a default velocity property on the boat - Sails command returns control when sails are out and boat is up to speed - Speed never falls below 0 - Speed never goes above VMG velocity to prevent exploitation #story[1196]
This commit is contained in:
@@ -34,7 +34,10 @@ public class MockBoat extends Boat {
|
||||
*/
|
||||
private boolean autoVMG = false;
|
||||
|
||||
|
||||
/**
|
||||
* Indicates whether boat velocity is determined by wind
|
||||
*/
|
||||
private boolean velocityDefault = true;
|
||||
|
||||
/**
|
||||
* Constructs a boat object with a given sourceID, name, country/team abbreviation, and polars table.
|
||||
@@ -300,4 +303,12 @@ public class MockBoat extends Boat {
|
||||
public void setAutoVMG(boolean autoVMG) {
|
||||
this.autoVMG = autoVMG;
|
||||
}
|
||||
|
||||
public boolean isVelocityDefault() {
|
||||
return velocityDefault;
|
||||
}
|
||||
|
||||
public void setVelocityDefault(boolean velocityDefault) {
|
||||
this.velocityDefault = velocityDefault;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,11 +355,11 @@ public class MockRace extends RaceState {
|
||||
//Checks if the current boat has finished the race or not.
|
||||
boolean finish = this.isLastLeg(boat.getCurrentLeg());
|
||||
|
||||
if (!finish && totalElapsedMilliseconds >= updatePeriodMilliseconds && boat.isSailsOut()) {
|
||||
if (!finish && totalElapsedMilliseconds >= updatePeriodMilliseconds) {
|
||||
|
||||
checkPosition(boat, totalElapsedMilliseconds);
|
||||
|
||||
setBoatSpeed(boat);
|
||||
if(boat.isVelocityDefault()) setBoatSpeed(boat);
|
||||
|
||||
//Calculates the distance travelled, in meters, in the current timeslice.
|
||||
double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds) * this.scaleFactor;
|
||||
@@ -373,8 +373,6 @@ public class MockRace extends RaceState {
|
||||
boat.setAutoVMG(false);
|
||||
}
|
||||
|
||||
} else {
|
||||
boat.setCurrentSpeed(0);
|
||||
}
|
||||
|
||||
this.updateEstimatedTime(boat);
|
||||
|
||||
@@ -10,7 +10,6 @@ import java.util.Observable;
|
||||
public class SailsCommand extends ObserverCommand {
|
||||
private boolean sailsOut;
|
||||
private double goalVelocity;
|
||||
private double acceleration = 1;
|
||||
|
||||
public SailsCommand(MockRace race, MockBoat boat, boolean sailsOut) {
|
||||
super(race, boat);
|
||||
@@ -20,6 +19,8 @@ public class SailsCommand extends ObserverCommand {
|
||||
@Override
|
||||
public void execute() {
|
||||
this.boat.setSailsOut(this.sailsOut);
|
||||
boat.setVelocityDefault(false);
|
||||
|
||||
if(sailsOut) {
|
||||
// Accelerate to VMG speed
|
||||
double polarSpeed = NewPolars.calculateSpeed(race.getWindDirection(), race.getWindSpeed(), boat.getBearing());
|
||||
@@ -33,12 +34,18 @@ public class SailsCommand extends ObserverCommand {
|
||||
|
||||
@Override
|
||||
public void update(Observable o, Object arg) {
|
||||
double acceleration = 0.5;
|
||||
|
||||
if(sailsOut && boat.getCurrentSpeed() < goalVelocity) {
|
||||
// Apply acceleration
|
||||
boat.setCurrentSpeed(Math.min(goalVelocity, boat.getCurrentSpeed() + acceleration));
|
||||
} else if (!sailsOut && boat.getCurrentSpeed() > goalVelocity) {
|
||||
// Apply deceleration
|
||||
// Apply deceleration to strictly 0 speed
|
||||
boat.setCurrentSpeed(Math.max(0, boat.getCurrentSpeed() - acceleration));
|
||||
} else {
|
||||
System.out.println(goalVelocity + " " + boat.getCurrentSpeed());
|
||||
|
||||
// Release boat from SailsCommand control
|
||||
if(sailsOut) boat.setVelocityDefault(true);
|
||||
race.deleteObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user