You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

450 lines
15 KiB

package mock.model;
import network.Messages.Enums.BoatStatusEnum;
import network.Messages.LatestMessages;
import org.opengis.geometry.primitive.*;
import shared.dataInput.BoatDataSource;
import shared.dataInput.RaceDataSource;
import network.Messages.Enums.RaceStatusEnum;
import shared.dataInput.RegattaDataSource;
import shared.model.*;
import shared.model.Bearing;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.*;
import static java.lang.Math.cos;
/**
* Represents a yacht race.
* Has a course, boats, boundaries, etc...
* Is responsible for simulating the race, and sending messages to a MockOutput instance.
*/
public class MockRace extends Race {
/**
* An observable list of boats in the race.
*/
private List<MockBoat> boats;
/**
* A copy of the boundary list, except "shrunk" inwards by 50m.
*/
private List<GPSCoordinate> shrinkBoundary;
/**
* The scale factor of the race.
* See {@link Constants#RaceTimeScale}.
*/
private int scaleFactor;
/**
* Object used to generate changes in wind speed/direction.
*/
private WindGenerator windGenerator;
/**
* 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 raceDataSource Data source for race related data (participating boats, legs, etc...).
* @param regattaDataSource Data source for race related data (course name, location, timezone, etc...).
* @param latestMessages The LatestMessages to send events to.
* @param polars The polars table to be used for boat simulation.
* @param timeScale The timeScale for the race. See {@link Constants#RaceTimeScale}.
*/
public MockRace(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, LatestMessages latestMessages, Polars polars, int timeScale) {
super(boatDataSource, raceDataSource, regattaDataSource, latestMessages);
this.scaleFactor = timeScale;
this.boats = this.generateMockBoats(boatDataSource.getBoats(), raceDataSource.getParticipants(), polars);
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.
this.windGenerator = new WindGenerator(
Bearing.fromDegrees(225),
Bearing.fromDegrees(215),
Bearing.fromDegrees(235),
12d,
8d,
16d );
//Wind.
this.setWind(windGenerator.generateBaselineWind());
}
/**
* Generates a list of MockBoats given a list of Boats, and a list of participating boats.
* @param boats The map of Boats describing boats that are potentially in the race. Maps boat sourceID to boat.
* @param sourceIDs The list of boat sourceIDs describing which specific boats are actually participating.
* @param polars The polars table to be used for boat simulation.
* @return A list of MockBoats that are participating in the race.
*/
private List<MockBoat> generateMockBoats(Map<Integer, Boat> boats, List<Integer> sourceIDs, Polars polars) {
List<MockBoat> mockBoats = new ArrayList<>(sourceIDs.size());
//For each sourceID participating...
for (int sourceID : sourceIDs) {
//Get the boat associated with the sourceID.
Boat boat = boats.get(sourceID);
//Construct a MockBoat using the Boat and Polars.
MockBoat mockBoat = new MockBoat(boat, polars);
mockBoats.add(mockBoat);
}
return mockBoats;
}
/**
* Updates the race time to a specified value, in milliseconds since the unix epoch.
* @param currentTime Milliseconds since unix epoch.
*/
public void updateRaceTime(long currentTime) {
this.raceClock.setUTCTime(currentTime);
}
/**
* Updates the race status enumeration based on the current time.
*/
public void updateRaceStatusEnum() {
//The millisecond duration of the race. Negative means it hasn't started, so we flip sign.
long timeToStart = - this.raceClock.getDurationMilli();
if (timeToStart > Constants.RacePreStartTime) {
//Time > 3 minutes is the prestart period.
this.setRaceStatusEnum(RaceStatusEnum.PRESTART);
} else if ((timeToStart <= Constants.RacePreStartTime) && (timeToStart >= Constants.RacePreparatoryTime)) {
//Time between [1, 3] minutes is the warning period.
this.setRaceStatusEnum(RaceStatusEnum.WARNING);
} else if ((timeToStart <= Constants.RacePreparatoryTime) && (timeToStart > 0)) {
//Time between (0, 1] minutes is the preparatory period.
this.setRaceStatusEnum(RaceStatusEnum.PREPARATORY);
} else {
//Otherwise, the race has started!
this.setRaceStatusEnum(RaceStatusEnum.STARTED);
}
}
/**
* Sets the status of all boats in the race to RACING.
*/
public void setBoatsStatusToRacing() {
for (MockBoat boat : this.boats) {
boat.setStatus(BoatStatusEnum.RACING);
}
}
/**
* Sets the estimated time at next mark for each boat to a specified time. This is used during the countdown timer to provide this value to boat before the race starts.
* @param time The time to provide to each boat.
*/
public void setBoatsTimeNextMark(ZonedDateTime time) {
for (MockBoat boat : this.boats) {
boat.setEstimatedTimeAtNextMark(time);
}
}
/**
* Initialise the boats in the race.
* This sets their starting positions and current legs.
*/
@Override
public void initialiseBoats() {
//Gets the starting positions of the boats.
List<GPSCoordinate> startingPositions = getSpreadStartingPositions();
//Get iterators for our boat and position lists.
Iterator<MockBoat> boatIt = this.boats.iterator();
Iterator<GPSCoordinate> startPositionIt = startingPositions.iterator();
//Iterate over the pair of lists.
while (boatIt.hasNext() && startPositionIt.hasNext()) {
//Get the next boat and position.
MockBoat boat = boatIt.next();
GPSCoordinate startPosition = startPositionIt.next();
//The boat starts on the first leg of the race.
boat.setCurrentLeg(this.legs.get(0));
//Boats start with 0 knots speed.
boat.setCurrentSpeed(0d);
//Place the boat at its starting position.
boat.setCurrentPosition(startPosition);
//Boats start facing their next marker.
boat.setBearing(boat.calculateBearingToNextMarker());
//Sets the boats status to prestart - it changes to racing when the race starts.
boat.setStatus(BoatStatusEnum.PRESTART);
//We set a large time since tack change so that it calculates a new VMG when the simulation starts.
boat.setTimeSinceTackChange(Long.MAX_VALUE);
}
}
/**
* Creates a list of starting positions for the different boats, so they do not appear cramped at the start line.
*
* @return A list of starting positions.
*/
private List<GPSCoordinate> getSpreadStartingPositions() {
//The first compound marker of the race - the starting gate.
CompoundMark compoundMark = this.legs.get(0).getStartCompoundMark();
//The position of the two markers from the compound marker.
GPSCoordinate mark1Position = compoundMark.getMark1Position();
GPSCoordinate mark2Position = compoundMark.getMark2Position();
//Calculates the azimuth between the two points.
Azimuth azimuth = GPSCoordinate.calculateAzimuth(mark1Position, mark2Position);
//Calculates the distance between the two points.
double distanceMeters = GPSCoordinate.calculateDistanceMeters(mark1Position, mark2Position);
//The number of boats in the race.
int numberOfBoats = this.boats.size();
//Calculates the distance between each boat. We divide by numberOfBoats + 1 to ensure that no boat is placed on one of the starting gate's marks.
double distanceBetweenBoatsMeters = distanceMeters / (numberOfBoats + 1);
//List to store coordinates in.
List<GPSCoordinate> positions = new ArrayList<>();
//We start spacing boats out from mark 1.
GPSCoordinate position = mark1Position;
//For each boat, displace position, and store it.
for (int i = 0; i < numberOfBoats; i++) {
position = GPSCoordinate.calculateNewPosition(position, distanceBetweenBoatsMeters, azimuth);
positions.add(position);
}
return positions;
}
/**
* Determines whether or not a given VMG improves the velocity of a boat, if it were currently using currentVMG.
* @param currentVMG The current VMG of the boat.
* @param potentialVMG The new VMG to test.
* @param bearingToDestination The bearing between the boat and its destination.
* @return True if the new VMG is improves velocity, false otherwise.
*/
public boolean improvesVelocity(VMG currentVMG, VMG potentialVMG, Bearing bearingToDestination) {
//Calculates the angle between the boat and its destination.
Angle angleBetweenDestAndHeading = Angle.fromDegrees(currentVMG.getBearing().degrees() - bearingToDestination.degrees());
//Calculates the angle between the new VMG and the boat's destination.
Angle angleBetweenDestAndNewVMG = Angle.fromDegrees(potentialVMG.getBearing().degrees() - bearingToDestination.degrees());
//Calculate the boat's current velocity.
double currentVelocity = Math.cos(angleBetweenDestAndHeading.radians()) * currentVMG.getSpeed();
//Calculate the potential velocity with the new VMG.
double vmgVelocity = Math.cos(angleBetweenDestAndNewVMG.radians()) * potentialVMG.getSpeed();
//Return whether or not the new VMG gives better velocity.
return vmgVelocity > currentVelocity;
}
/**
* Determines whether or not a given VMG improves the velocity of a boat.
* @param boat The boat to test.
* @param vmg The new VMG to test.
* @return True if the new VMG is improves velocity, false otherwise.
*/
private boolean improvesVelocity(MockBoat boat, VMG vmg) {
//Get the boats "current" VMG.
VMG boatVMG = new VMG(boat.getCurrentSpeed(), boat.getBearing());
//Check if the new VMG is better than the boat's current VMG.
return this.improvesVelocity(boatVMG, vmg, boat.calculateBearingToNextMarker());
}
/**
* Calculates the distance a boat has travelled and updates its current position according to this value.
*
* @param boat The boat to be updated.
* @param updatePeriodMilliseconds The time, in milliseconds, since the last update.
* @param totalElapsedMilliseconds The total number of milliseconds that have elapsed since the start of the race.
*/
public void updatePosition(MockBoat boat, long updatePeriodMilliseconds, long totalElapsedMilliseconds) {
//Checks if the current boat has finished the race or not.
boolean finish = this.isLastLeg(boat.getCurrentLeg());
if (!finish && totalElapsedMilliseconds >= updatePeriodMilliseconds) {
if (boat.getCurrentSpeed() == 0) {
newOptimalVMG(boat);
boat.setBearing(boat.calculateBearingToNextMarker());
}
setBoatSpeed(boat);
//Calculates the distance travelled, in meters, in the current timeslice.
double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds);
//Scale it.
distanceTravelledMeters = distanceTravelledMeters * this.scaleFactor;
//Move the boat forwards that many meters, and advances its time counters by enough milliseconds.
boat.moveForwards(distanceTravelledMeters);
boat.setTimeSinceTackChange(boat.getTimeSinceTackChange() + updatePeriodMilliseconds);
if (boat.isAutoVMG()) {
newOptimalVMG(boat);
}
this.updateEstimatedTime(boat);
}
}
private void newOptimalVMG(MockBoat boat) {
long tackPeriod = 15000;
if (boat.getTimeSinceTackChange() > tackPeriod) {
//Calculate the new VMG.
VMG newVMG = boat.getPolars().calculateVMG(
this.getWindDirection(),
this.getWindSpeed(),
boat.calculateBearingToNextMarker(),
Bearing.fromDegrees(0d),
Bearing.fromDegrees(359.99999d));
//If the new vmg improves velocity, use it.
if (improvesVelocity(boat, newVMG)) {
boat.setVMG(newVMG);
}
}
}
private void setBoatSpeed(MockBoat boat) {
VMG vmg = boat.getPolars().calculateVMG(
this.getWindDirection(),
this.getWindSpeed(),
boat.getBearing(),
boat.getBearing(),
boat.getBearing());
if (vmg.getSpeed() > 0) {
boat.setCurrentSpeed(vmg.getSpeed());
}
}
/**
* Returns the number of boats that are still active in the race.
* They become inactive by either finishing or withdrawing.
* @return The number of boats still active in the race.
*/
protected int getNumberOfActiveBoats() {
int numberOfActiveBoats = 0;
for (MockBoat boat : this.boats) {
//If the boat is currently racing, count it.
if (boat.getStatus() == BoatStatusEnum.RACING) {
numberOfActiveBoats++;
}
}
return numberOfActiveBoats;
}
/**
* Returns a list of boats in the race.
* @return List of boats in the race.
*/
public List<MockBoat> getBoats() {
return boats;
}
/**
* Changes the wind direction randomly, while keeping it within [windLowerBound, windUpperBound].
*/
public void changeWindDirection() {
Wind nextWind = windGenerator.generateNextWind(raceWind.getValue());
setWind(nextWind);
}
/**
* Updates the boat's estimated time to next mark if positive
* @param boat to estimate time given its velocity
*/
private void updateEstimatedTime(MockBoat boat) {
double velocityToMark = boat.getCurrentSpeed() * cos(boat.getBearing().radians() - boat.calculateBearingToNextMarker().radians()) / Constants.KnotsToMMPerSecond;
if (velocityToMark > 0) {
//Calculate milliseconds until boat reaches mark.
long timeFromNow = (long) (1000 * boat.calculateDistanceToNextMarker() / velocityToMark);
//Calculate time at which it will reach mark.
ZonedDateTime timeAtMark = this.raceClock.getCurrentTime().plus(timeFromNow, ChronoUnit.MILLIS);
boat.setEstimatedTimeAtNextMark(timeAtMark);
}
}
public List<CompoundMark> getCompoundMarks() {
return compoundMarks;
}
}