Merged branch "RaceFactory" with master

main
Connor Taylor-Brown 8 years ago
parent 148108a658
commit 851bbb4fde

@ -1,228 +0,0 @@
package mock.model;
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);
}
}
Loading…
Cancel
Save