Merged wind generator into this branch. #story[1094]
This commit is contained in:
@@ -52,7 +52,6 @@ public class MockRace extends Race {
|
|||||||
*/
|
*/
|
||||||
private WindGenerator windGenerator;
|
private WindGenerator windGenerator;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a race object with a given RaceDataSource, BoatDataSource, and RegattaDataSource and sends events to the given mockOutput.
|
* Constructs a race object with a given RaceDataSource, BoatDataSource, and RegattaDataSource and sends events to the given mockOutput.
|
||||||
* @param boatDataSource Data source for boat related data (yachts and marker boats).
|
* @param boatDataSource Data source for boat related data (yachts and marker boats).
|
||||||
@@ -72,7 +71,6 @@ public class MockRace extends Race {
|
|||||||
|
|
||||||
this.shrinkBoundary = GPSCoordinate.getShrinkBoundary(this.boundary);
|
this.shrinkBoundary = GPSCoordinate.getShrinkBoundary(this.boundary);
|
||||||
|
|
||||||
|
|
||||||
//Set up wind generator. It may be tidier to create this outside the race (with the values sourced from a data file maybe?) and pass it in.
|
//Set up wind generator. It may be tidier to create this outside the race (with the values sourced from a data file maybe?) and pass it in.
|
||||||
this.windGenerator = new WindGenerator(
|
this.windGenerator = new WindGenerator(
|
||||||
Bearing.fromDegrees(225),
|
Bearing.fromDegrees(225),
|
||||||
@@ -120,7 +118,6 @@ public class MockRace extends Race {
|
|||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
initialiseBoats();
|
initialiseBoats();
|
||||||
initialiseWindDirection();
|
|
||||||
this.countdownTimer.start();
|
this.countdownTimer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,23 +542,13 @@ public class MockRace extends Race {
|
|||||||
/**
|
/**
|
||||||
* Calculates a boat's VMG.
|
* Calculates a boat's VMG.
|
||||||
* @param boat The boat to calculate VMG for.
|
* @param boat The boat to calculate VMG for.
|
||||||
* @param bearingBounds An array containing the lower and upper acceptable bearing bounds to keep the boat in the course.
|
|
||||||
* @return VMG for the specified boat.
|
* @return VMG for the specified boat.
|
||||||
*/
|
*/
|
||||||
private VMG calculateVMG(MockBoat boat, Bearing[] bearingBounds) {
|
private VMG calculateVMG(MockBoat boat) {
|
||||||
|
|
||||||
//Get the lower and upper acceptable bounds.
|
|
||||||
Bearing lowerAcceptableBound = bearingBounds[0];
|
|
||||||
Bearing upperAcceptableBound = bearingBounds[1];
|
|
||||||
|
|
||||||
|
|
||||||
//Find the VMG inside these bounds.
|
//Find the VMG inside these bounds.
|
||||||
VMG bestVMG = boat.getPolars().calculateVMG(
|
VMG bestVMG = boat.getPolars().calculateVMG(this.getWindDirection(), this.getWindSpeed(), boat.calculateBearingToNextMarker(), Bearing.fromDegrees(0d), Bearing.fromDegrees(359.99999d));
|
||||||
this.getWindDirection(),
|
|
||||||
this.getWindSpeed(),
|
|
||||||
boat.calculateBearingToNextMarker(),
|
|
||||||
lowerAcceptableBound,
|
|
||||||
upperAcceptableBound);
|
|
||||||
|
|
||||||
|
|
||||||
return bestVMG;
|
return bestVMG;
|
||||||
@@ -638,29 +625,16 @@ public class MockRace extends Race {
|
|||||||
//Move the boat forwards that many meters, and advances its time counters by enough milliseconds.
|
//Move the boat forwards that many meters, and advances its time counters by enough milliseconds.
|
||||||
boat.moveForwards(distanceTravelledMeters, updatePeriodMilliseconds * this.scaleFactor);
|
boat.moveForwards(distanceTravelledMeters, updatePeriodMilliseconds * this.scaleFactor);
|
||||||
|
|
||||||
|
|
||||||
//Only get a new VMG if the boat will go outside the course, or X seconds have elapsed.
|
|
||||||
boolean willStayInsideCourse = this.checkBearingInsideCourse(boat.getBearing(), boat.getCurrentPosition());
|
|
||||||
long tackPeriod = 15000;
|
long tackPeriod = 15000;
|
||||||
if (!willStayInsideCourse || (boat.getTimeSinceTackChange() > tackPeriod)) {
|
if (boat.getTimeSinceTackChange() > tackPeriod) {
|
||||||
|
|
||||||
//Calculate the boat's bearing bounds, to ensure that it doesn't go out of the course.
|
|
||||||
Bearing[] bearingBounds = this.calculateBearingBounds(boat);
|
|
||||||
|
|
||||||
|
|
||||||
//Calculate the new VMG.
|
//Calculate the new VMG.
|
||||||
VMG newVMG = this.calculateVMG(boat, bearingBounds);
|
VMG newVMG = this.calculateVMG(boat);
|
||||||
|
|
||||||
|
|
||||||
//If the new vmg improves velocity, use it.
|
//If the new vmg improves velocity, use it.
|
||||||
if (improvesVelocity(boat, newVMG)) {
|
if (improvesVelocity(boat, newVMG)) {
|
||||||
boat.setVMG(newVMG);
|
boat.setVMG(newVMG);
|
||||||
|
|
||||||
} else {
|
|
||||||
//We also need to use the new VMG if our current bearing will take us out of the course.
|
|
||||||
if (!willStayInsideCourse) {
|
|
||||||
boat.setVMG(newVMG);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -674,88 +648,6 @@ public class MockRace extends Race {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates the upper and lower bounds that the boat may have in order to not go outside of the course.
|
|
||||||
* @param boat The boat to check.
|
|
||||||
* @return An array of bearings. The first is the lower bound, the second is the upper bound.
|
|
||||||
*/
|
|
||||||
private Bearing[] calculateBearingBounds(MockBoat boat) {
|
|
||||||
|
|
||||||
Bearing[] bearings = new Bearing[2];
|
|
||||||
|
|
||||||
Bearing lowerBearing = Bearing.fromDegrees(0.001);
|
|
||||||
Bearing upperBearing = Bearing.fromDegrees(359.999);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double lastAngle = -1;
|
|
||||||
boolean lastAngleWasGood = false;
|
|
||||||
|
|
||||||
//Check all bearings between [0, 360).
|
|
||||||
for (double angle = 0; angle < 360; angle += 1) {
|
|
||||||
|
|
||||||
//Create bearing from angle.
|
|
||||||
Bearing bearing = Bearing.fromDegrees(angle);
|
|
||||||
|
|
||||||
//Check that if it is acceptable.
|
|
||||||
boolean bearingIsGood = this.checkBearingInsideCourse(bearing, boat.getCurrentPosition());
|
|
||||||
|
|
||||||
|
|
||||||
if (lastAngle != -1) {
|
|
||||||
|
|
||||||
if (lastAngleWasGood && !bearingIsGood) {
|
|
||||||
//We have flipped over from good bearings to bad bearings. So the last good bearing is the upper bearing.
|
|
||||||
upperBearing = Bearing.fromDegrees(lastAngle);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!lastAngleWasGood && bearingIsGood) {
|
|
||||||
//We have flipped over from bad bearings to good bearings. So the current bearing is the lower bearing.
|
|
||||||
lowerBearing = Bearing.fromDegrees(angle);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
lastAngle = angle;
|
|
||||||
lastAngleWasGood = bearingIsGood;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//TODO BUG if it can't find either upper or lower, it returns (0, 359.999). Should return (boatbearing, boatbearing+0.0001)
|
|
||||||
bearings[0] = lowerBearing;
|
|
||||||
bearings[1] = upperBearing;
|
|
||||||
|
|
||||||
return bearings;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a given bearing, starting at a given position, would put a boat out of the course boundaries.
|
|
||||||
* @param bearing The bearing to check.
|
|
||||||
* @param position The position to start from.
|
|
||||||
* @return True if the bearing would keep the boat in the course, false if it would take it out of the course.
|
|
||||||
*/
|
|
||||||
private boolean checkBearingInsideCourse(Bearing bearing, GPSCoordinate position) {
|
|
||||||
|
|
||||||
//Get azimuth from bearing.
|
|
||||||
Azimuth azimuth = Azimuth.fromBearing(bearing);
|
|
||||||
|
|
||||||
|
|
||||||
//Tests to see if a point in front of the boat is out of bounds.
|
|
||||||
double epsilonMeters = 50d;
|
|
||||||
GPSCoordinate testCoord = GPSCoordinate.calculateNewPosition(position, epsilonMeters, azimuth);
|
|
||||||
|
|
||||||
//If it isn't inside the boundary, calculate new bearing.
|
|
||||||
if (GPSCoordinate.isInsideBoundary(testCoord, this.shrinkBoundary)) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a boat has finished any legs, or has pulled out of race (DNF).
|
* Checks if a boat has finished any legs, or has pulled out of race (DNF).
|
||||||
@@ -831,7 +723,6 @@ public class MockRace extends Race {
|
|||||||
return boats;
|
return boats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialises the wind bearing with the value of the windBaselineBearing.
|
* Initialises the wind bearing with the value of the windBaselineBearing.
|
||||||
*/
|
*/
|
||||||
@@ -840,7 +731,6 @@ public class MockRace extends Race {
|
|||||||
this.setWind(windGenerator.generateBaselineWind());
|
this.setWind(windGenerator.generateBaselineWind());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Changes the wind direction randomly, while keeping it within [windLowerBound, windUpperBound].
|
* Changes the wind direction randomly, while keeping it within [windLowerBound, windUpperBound].
|
||||||
*/
|
*/
|
||||||
@@ -872,4 +762,4 @@ public class MockRace extends Race {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user