Split wind off from Race and made it its own class. Deleted dnfChance as it is no longer used.

main
Joseph Gardner 8 years ago
parent 775c32ca92
commit 81eeca3533

@ -48,35 +48,6 @@ public class MockRace extends Race {
private int scaleFactor;
/**
* The percent chance that a boat fails the race, and enters a DNF state, at each checkpoint.
* 0 = 0%, 100 = 100%.
*/
private int dnfChance = 0;
/**
* Used to generate random numbers when changing the wind direction.
*/
private int changeWind = 4;
/**
* The bearing the wind direction starts at.
*/
private static final Bearing windBaselineBearing = Bearing.fromDegrees(225);
/**
* The lower bearing angle that the wind may have.
*/
private static final Bearing windLowerBound = Bearing.fromDegrees(215);
/**
* The upper bearing angle that the wind may have.
*/
private static final Bearing windUpperBound = Bearing.fromDegrees(235);
/**
@ -98,12 +69,7 @@ public class MockRace extends Race {
this.shrinkBoundary = GPSCoordinate.getShrinkBoundary(this.boundary);
this.windSpeed = 12;
this.windDirection = Bearing.fromDegrees(180);
this.wind = new Wind();
}
/**
@ -140,7 +106,6 @@ public class MockRace extends Race {
*/
public void run() {
initialiseBoats();
initialiseWindDirection();
this.countdownTimer.start();
}
@ -288,8 +253,8 @@ public class MockRace extends Race {
//Convert wind direction and speed to ints. //TODO this conversion should be done inside the racestatus class.
int windDirectionInt = AC35UnitConverter.encodeHeading(this.windDirection.degrees());
int windSpeedInt = (int) (this.windSpeed * Constants.KnotsToMMPerSecond);
int windDirectionInt = AC35UnitConverter.encodeHeading(wind.getWindDirection().degrees());
int windSpeedInt = (int) (wind.getWindSpeed() * Constants.KnotsToMMPerSecond);
//Create race status object, and send it.
RaceStatus raceStatus = new RaceStatus(
@ -576,7 +541,7 @@ public class MockRace extends Race {
//Find the VMG inside these bounds.
VMG bestVMG = boat.getPolars().calculateVMG(this.windDirection, this.windSpeed, boat.calculateBearingToNextMarker(), lowerAcceptableBound, upperAcceptableBound);
VMG bestVMG = boat.getPolars().calculateVMG(wind.getWindDirection(), wind.getWindSpeed(), boat.calculateBearingToNextMarker(), lowerAcceptableBound, upperAcceptableBound);
return bestVMG;
@ -810,44 +775,12 @@ public class MockRace extends Race {
boat.setCurrentSpeed(0);
boat.setStatus(BoatStatusEnum.FINISHED);
} else if (doNotFinish()) {
//Boat has pulled out of race.
boat.setTimeFinished(timeElapsed);
boat.setCurrentLeg(new Leg("DNF", -1));
boat.setCurrentSpeed(0);
boat.setStatus(BoatStatusEnum.DNF);
}
}
}
/**
* Sets the chance each boat has of failing at a gate or marker
*
* @param chance percentage chance a boat has of failing per checkpoint.
*/
protected void setDnfChance(int chance) {
if (chance >= 0 && chance <= 100) {
dnfChance = chance;
}
}
/**
* Decides if a boat should received a DNF status.
* @return True means it should DNF, false means it shouldn't.
*/
protected boolean doNotFinish() {
Random rand = new Random();
return rand.nextInt(100) < dnfChance;
}
/**
* Returns the number of boats that are still active in the race.
* They become inactive by either finishing or withdrawing.
@ -878,42 +811,11 @@ public class MockRace extends Race {
return boats;
}
/**
* Initialises the wind bearing with the value of the windBaselineBearing.
*/
protected void initialiseWindDirection() {
//Set the starting bearing.
this.windDirection = Bearing.fromDegrees(MockRace.windBaselineBearing.degrees());
}
/**
* Changes the wind direction randomly, while keeping it within [windLowerBound, windUpperBound].
*/
protected void changeWindDirection() {
//Randomly add or remove 0.5 degrees.
int r = new Random().nextInt(changeWind) + 1;
if (r == 1) {
//Add 0.5 degrees to the wind bearing.
this.windDirection.setDegrees(this.windDirection.degrees() + 0.5);
} else if (r == 2) {
//Minus 0.5 degrees from the wind bearing.
this.windDirection.setDegrees(this.windDirection.degrees() - 0.5);
}
//Ensure that the wind is in the correct bounds.
if (this.windDirection.degrees() > MockRace.windUpperBound.degrees()) {
this.windDirection.setBearing(MockRace.windUpperBound);
} else if (this.windDirection.degrees() < MockRace.windLowerBound.degrees()) {
this.windDirection.setBearing(MockRace.windLowerBound);
}
this.wind.changeWindDirection();
}

@ -0,0 +1,4 @@
package mock.model;
public class RaceState {
}

@ -1,4 +0,0 @@
package mock.model;
public class RaceStatus {
}

@ -0,0 +1,92 @@
package mock.model;
import shared.model.Bearing;
import java.util.Random;
public class Wind {
/**
* Used to generate random numbers when changing the wind direction.
*/
private int changeWind = 4;
/**
* The bearing the wind direction starts at.
*/
private Bearing windBearing;
/**
* The lower bearing angle that the wind may have.
*/
private Bearing windLowerBound;
/**
* The upper bearing angle that the wind may have.
*/
private Bearing windUpperBound;
double windSpeed;
public Wind() {
this.windBearing = Bearing.fromDegrees(225);
this.windSpeed = 12;
this.windLowerBound = Bearing.fromDegrees(215);
this.windUpperBound = Bearing.fromDegrees(235);
}
public Wind(Bearing windBearing, double windSpeed, Bearing windLowerBound, Bearing windUpperBound) {
this.windBearing = windBearing;
this.windSpeed = windSpeed;
this.windLowerBound = windLowerBound;
this.windUpperBound = windUpperBound;
}
/**
* Changes the wind direction randomly, while keeping it within [windLowerBound, windUpperBound].
*/
public void changeWindDirection() {
//Randomly add or remove 0.5 degrees.
int r = new Random().nextInt(changeWind) + 1;
if (r == 1) {
//Add 0.5 degrees to the wind bearing.
this.windBearing.setDegrees(this.windBearing.degrees() + 0.5);
} else if (r == 2) {
//Minus 0.5 degrees from the wind bearing.
this.windBearing.setDegrees(this.windBearing.degrees() - 0.5);
}
//Ensure that the wind is in the correct bounds.
if (this.windBearing.degrees() > this.windUpperBound.degrees()) {
this.windBearing.setBearing(this.windUpperBound);
} else if (this.windBearing.degrees() < this.windLowerBound.degrees()) {
this.windBearing.setBearing(this.windLowerBound);
}
}
public Bearing getWindDirection() {
return this.windBearing;
}
public double getWindSpeed() {
return this.windSpeed;
}
public void setWindDirection(Bearing windBearing) {
this.windBearing = windBearing;
}
public void setWindSpeed(double windSpeed) {
this.windSpeed = windSpeed;
}
public void setDegrees(double degrees) {
this.windBearing.setDegrees(degrees);
}
}

@ -2,6 +2,7 @@ package shared.model;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import mock.model.Wind;
import network.Messages.Enums.RaceStatusEnum;
import network.Messages.Enums.RaceTypeEnum;
import network.Messages.LatestMessages;
@ -100,13 +101,7 @@ public abstract class Race implements Runnable {
/**
* The current wind direction bearing.
*/
protected Bearing windDirection;
/**
* Wind speed (knots).
* Convert this to millimeters per second before passing to RaceStatus.
*/
protected double windSpeed;
protected Wind wind;
/**
@ -169,12 +164,7 @@ public abstract class Race implements Runnable {
//Race type.
this.raceType = raceDataSource.getRaceType();
//Wind speed.
this.windSpeed = 0;
//Wind direction.
this.windDirection = Bearing.fromDegrees(0);
this.wind = new Wind();
}
@ -259,7 +249,7 @@ public abstract class Race implements Runnable {
* @return The wind bearing.
*/
public Bearing getWindDirection() {
return windDirection;
return wind.getWindDirection();
}
/**
@ -268,7 +258,7 @@ public abstract class Race implements Runnable {
* @return The wind speed.
*/
public double getWindSpeed() {
return windSpeed;
return wind.getWindSpeed();
}
/**

@ -311,11 +311,11 @@ public class VisualiserRace extends Race {
//Race status enum.
this.raceStatusEnum = RaceStatusEnum.fromByte(raceStatus.getRaceStatus());
//Wind bearing.
this.windDirection.setDegrees(raceStatus.getScaledWindDirection());
// Wind direction
wind.setDegrees(raceStatus.getScaledWindDirection());
//Wind speed.
this.windSpeed = raceStatus.getWindSpeedKnots();
// Wind speed
wind.setWindSpeed(raceStatus.getWindSpeedKnots());
//Current race time.
this.raceClock.setUTCTime(raceStatus.getCurrentTime());

Loading…
Cancel
Save