Boats now store whether autoVMG is on or off, boat speed scales off their bearing relative to the TWA. #story[1094]
This commit is contained in:
@@ -22,6 +22,11 @@ public class MockBoat extends Boat {
|
|||||||
*/
|
*/
|
||||||
private long timeSinceTackChange = 0;
|
private long timeSinceTackChange = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores whether the boat is on autoVMG or not
|
||||||
|
*/
|
||||||
|
private boolean autoVMG = true;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -191,4 +196,11 @@ public class MockBoat extends Boat {
|
|||||||
return distanceTravelledMeters;
|
return distanceTravelledMeters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAutoVMG() {
|
||||||
|
return autoVMG;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAutoVMG(boolean autoVMG) {
|
||||||
|
this.autoVMG = autoVMG;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package mock.model;
|
|||||||
|
|
||||||
import network.Messages.Enums.BoatStatusEnum;
|
import network.Messages.Enums.BoatStatusEnum;
|
||||||
import network.Messages.LatestMessages;
|
import network.Messages.LatestMessages;
|
||||||
|
import org.opengis.geometry.primitive.*;
|
||||||
import shared.dataInput.BoatDataSource;
|
import shared.dataInput.BoatDataSource;
|
||||||
import shared.dataInput.RaceDataSource;
|
import shared.dataInput.RaceDataSource;
|
||||||
import network.Messages.Enums.RaceStatusEnum;
|
import network.Messages.Enums.RaceStatusEnum;
|
||||||
import shared.dataInput.RegattaDataSource;
|
import shared.dataInput.RegattaDataSource;
|
||||||
import shared.model.*;
|
import shared.model.*;
|
||||||
|
import shared.model.Bearing;
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
@@ -209,8 +211,7 @@ public class MockRace extends Race {
|
|||||||
boat.setStatus(BoatStatusEnum.PRESTART);
|
boat.setStatus(BoatStatusEnum.PRESTART);
|
||||||
|
|
||||||
//We set a large time since tack change so that it calculates a new VMG when the simulation starts.
|
//We set a large time since tack change so that it calculates a new VMG when the simulation starts.
|
||||||
boat.setTimeSinceTackChange(999999);
|
boat.setTimeSinceTackChange(Long.MAX_VALUE);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -319,8 +320,14 @@ public class MockRace extends Race {
|
|||||||
//Checks if the current boat has finished the race or not.
|
//Checks if the current boat has finished the race or not.
|
||||||
boolean finish = this.isLastLeg(boat.getCurrentLeg());
|
boolean finish = this.isLastLeg(boat.getCurrentLeg());
|
||||||
|
|
||||||
if (!finish) {
|
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.
|
//Calculates the distance travelled, in meters, in the current timeslice.
|
||||||
double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds);
|
double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds);
|
||||||
@@ -331,25 +338,10 @@ 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);
|
boat.moveForwards(distanceTravelledMeters);
|
||||||
|
boat.setTimeSinceTackChange(boat.getTimeSinceTackChange() + updatePeriodMilliseconds);
|
||||||
|
|
||||||
|
if (boat.isAutoVMG()) {
|
||||||
long tackPeriod = 15000;
|
newOptimalVMG(boat);
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.updateEstimatedTime(boat);
|
this.updateEstimatedTime(boat);
|
||||||
@@ -357,6 +349,38 @@ public class MockRace extends Race {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.
|
* Returns the number of boats that are still active in the race.
|
||||||
* They become inactive by either finishing or withdrawing.
|
* They become inactive by either finishing or withdrawing.
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import javafx.animation.AnimationTimer;
|
|||||||
import network.Messages.Enums.BoatStatusEnum;
|
import network.Messages.Enums.BoatStatusEnum;
|
||||||
import network.Messages.Enums.RaceStatusEnum;
|
import network.Messages.Enums.RaceStatusEnum;
|
||||||
import network.Messages.LatestMessages;
|
import network.Messages.LatestMessages;
|
||||||
|
import shared.model.Race;
|
||||||
|
|
||||||
public class RaceLogic implements Runnable {
|
public class RaceLogic implements Runnable {
|
||||||
/**
|
/**
|
||||||
@@ -100,6 +101,8 @@ public class RaceLogic implements Runnable {
|
|||||||
*/
|
*/
|
||||||
long lastFrameTime = timeRaceStarted;
|
long lastFrameTime = timeRaceStarted;
|
||||||
|
|
||||||
|
long framePeriod = currentTime - lastFrameTime;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(long arg0) {
|
public void handle(long arg0) {
|
||||||
|
|
||||||
@@ -109,12 +112,11 @@ public class RaceLogic implements Runnable {
|
|||||||
//Update race time.
|
//Update race time.
|
||||||
race.updateRaceTime(currentTime);
|
race.updateRaceTime(currentTime);
|
||||||
|
|
||||||
|
|
||||||
//As long as there is at least one boat racing, we still simulate the race.
|
//As long as there is at least one boat racing, we still simulate the race.
|
||||||
if (race.getNumberOfActiveBoats() != 0) {
|
if (race.getNumberOfActiveBoats() != 0) {
|
||||||
|
|
||||||
//Get the time period of this frame.
|
//Get the time period of this frame.
|
||||||
long framePeriod = currentTime - lastFrameTime;
|
framePeriod = currentTime - lastFrameTime;
|
||||||
|
|
||||||
//For each boat, we update its position, and generate a BoatLocationMessage.
|
//For each boat, we update its position, and generate a BoatLocationMessage.
|
||||||
for (MockBoat boat : race.getBoats()) {
|
for (MockBoat boat : race.getBoats()) {
|
||||||
|
|||||||
@@ -1,4 +1,228 @@
|
|||||||
package mock.model;
|
package mock.model;
|
||||||
|
|
||||||
public class RaceState {
|
import network.Messages.Enums.BoatStatusEnum;
|
||||||
|
import network.Messages.LatestMessages;
|
||||||
|
import shared.dataInput.BoatDataSource;
|
||||||
|
import shared.dataInput.RaceDataSource;
|
||||||
|
import shared.dataInput.RegattaDataSource;
|
||||||
|
import shared.model.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class RaceState extends Race {
|
||||||
|
/**
|
||||||
|
* An observable list of boats in the race.
|
||||||
|
*/
|
||||||
|
private List<MockBoat> boats;
|
||||||
|
|
||||||
|
private Map<Integer, MockBoat> boatMap;
|
||||||
|
|
||||||
|
private Wind wind;
|
||||||
|
|
||||||
|
public RaceState(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, LatestMessages latestMessages, Polars polars) {
|
||||||
|
super(boatDataSource, raceDataSource, regattaDataSource, latestMessages);
|
||||||
|
this.boats = this.generateMockBoats(boatDataSource.getBoats(), raceDataSource.getParticipants(), polars);
|
||||||
|
this.boatMap = this.generateMockBoatMap(this.boats);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a map of the sourceID to its MockBoat.
|
||||||
|
* @param boats The list of MockBoats describing boats that are potentially in the race.
|
||||||
|
* @return A map of sourceID to MockBoats.
|
||||||
|
*/
|
||||||
|
private Map<Integer, MockBoat> generateMockBoatMap(List<MockBoat> boats) {
|
||||||
|
Map<Integer, MockBoat> boatMap = new HashMap<>();
|
||||||
|
|
||||||
|
for (MockBoat boat : boats) {
|
||||||
|
boatMap.put(boat.getSourceID(), boat);
|
||||||
|
}
|
||||||
|
|
||||||
|
return boatMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a boat has finished any legs, or has pulled out of race (DNF).
|
||||||
|
* @param boat The boat to check.
|
||||||
|
* @param timeElapsed The total time, in milliseconds, that has elapsed since the race started.
|
||||||
|
*/
|
||||||
|
public void checkPosition(MockBoat boat, long timeElapsed) {
|
||||||
|
|
||||||
|
//The distance, in nautical miles, within which the boat needs to get in order to consider that it has reached the marker.
|
||||||
|
double epsilonNauticalMiles = 100.0 / Constants.NMToMetersConversion; //100 meters. TODO should be more like 5-10.
|
||||||
|
|
||||||
|
if (boat.calculateDistanceToNextMarker() < epsilonNauticalMiles) {
|
||||||
|
//Boat has reached its target marker, and has moved on to a new leg.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Calculate how much the boat overshot the marker by.
|
||||||
|
double overshootMeters = boat.calculateDistanceToNextMarker();
|
||||||
|
|
||||||
|
|
||||||
|
//Move boat on to next leg.
|
||||||
|
Leg nextLeg = this.legs.get(boat.getCurrentLeg().getLegNumber() + 1);
|
||||||
|
boat.setCurrentLeg(nextLeg);
|
||||||
|
|
||||||
|
//Add overshoot distance into the distance travelled for the next leg.
|
||||||
|
boat.setDistanceTravelledInLeg(overshootMeters);
|
||||||
|
|
||||||
|
//Setting a high value for this allows the boat to immediately do a large turn, as it needs to in order to get to the next mark.
|
||||||
|
boat.setTimeSinceTackChange(999999);
|
||||||
|
|
||||||
|
|
||||||
|
//Check if the boat has finished or stopped racing.
|
||||||
|
|
||||||
|
if (this.isLastLeg(boat.getCurrentLeg())) {
|
||||||
|
//Boat has finished.
|
||||||
|
boat.setTimeFinished(timeElapsed);
|
||||||
|
boat.setCurrentSpeed(0);
|
||||||
|
boat.setStatus(BoatStatusEnum.FINISHED);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise the boats in the race.
|
||||||
|
* This sets their starting positions and current legs.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected 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(999999);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
public 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
initialiseBoats();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Wind getWind() {
|
||||||
|
return wind;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<MockBoat> getBoats() {
|
||||||
|
return boats;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MockBoat getBoatByid(int id) {
|
||||||
|
return boatMap.get(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user