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.

372 lines
17 KiB

package seng302.Model;
import javafx.animation.AnimationTimer;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.geotools.referencing.GeodeticCalculator;
import seng302.Constants;
import seng302.DataInput.RaceDataSource;
import seng302.MockOutput;
import seng302.Networking.Messages.BoatLocation;
import seng302.Networking.Messages.BoatStatus;
import seng302.Networking.Messages.Enums.BoatStatusEnum;
import seng302.Networking.Messages.RaceStatus;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static java.lang.Math.cos;
import static java.lang.Math.max;
import static java.lang.Math.min;
/**
* Parent class for races
* Created by fwy13 on 3/03/17.
*/
public class Race implements Runnable {
protected ObservableList<Boat> startingBoats;
protected ObservableList<CompoundMark> compoundMarks;
protected List<Leg> legs;
protected int boatsFinished = 0;
protected long totalTimeElapsed;
protected int scaleFactor = 15;
private long startTime;
private int raceId;
private int dnfChance = 0; //percentage chance a boat fails at each checkpoint
private MockOutput mockOutput;
private List<GPSCoordinate> boundary;
/**
* Wind direction bearing.
*/
private double windDirection;
/**
* Wind speed (knots).
* Convert this to millimeters per second before passing to RaceStatus.
*/
private double windSpeed;
public Race(RaceDataSource raceData, MockOutput mockOutput) {
this.startingBoats = FXCollections.observableArrayList(raceData.getBoats());
this.legs = raceData.getLegs();
this.compoundMarks = FXCollections.observableArrayList(raceData.getCompoundMarks());
this.legs.add(new Leg("Finish", this.legs.size()));
this.raceId = raceData.getRaceId();
this.mockOutput = mockOutput;
this.boundary = raceData.getBoundary();
this.startTime = System.currentTimeMillis() + (Constants.PRE_RACE_WAIT_TIME / this.scaleFactor);
this.windSpeed = 12;//TODO could use input parameters for these. And should fluctuate during race.
this.windDirection = 180;
}
/**
* Runnable for the thread.
*/
public void run() {
initialiseBoats();
countdownTimer.start();
}
/**
* Parse the marker boats through mock output
*/
public void parseMarks() {
for (CompoundMark mark : compoundMarks){
mockOutput.parseBoatLocation(mark.getMark1Source().getSourceID(), mark.getMark1().getLatitude(), mark.getMark1().getLongitude(),0,0);
if (mark.getMark2Source()!=null){
mockOutput.parseBoatLocation(mark.getMark2Source().getSourceID(), mark.getMark2().getLatitude(), mark.getMark2().getLongitude(),0,0);
}
}
}
/**
* Countdown timer until race starts.
*/
protected AnimationTimer countdownTimer = new AnimationTimer() {
long currentTime = System.currentTimeMillis();
long timeLeft;
@Override
public void handle(long arg0) {
timeLeft = startTime - currentTime;
if (timeLeft <= 0) {
System.setProperty("javafx.animation.fullspeed", "true");
raceTimer.start();
stop();
}
ArrayList<BoatStatus> boatStatuses = new ArrayList<>();
for (Boat boat : startingBoats) {
mockOutput.parseBoatLocation(boat.getSourceID(), boat.getCurrentPosition().getLatitude(),
boat.getCurrentPosition().getLongitude(), boat.getHeading(), 0);
boatStatuses.add(new BoatStatus(boat.getSourceID(), BoatStatusEnum.PRESTART, 0));
}
parseMarks();
int raceStatusNumber = timeLeft <= 60000 / scaleFactor && timeLeft > 0? 2 : 1;
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, raceStatusNumber, startTime, 0, 2300, 1, boatStatuses);
mockOutput.parseRaceStatus(raceStatus);
currentTime = System.currentTimeMillis();
}
};
private AnimationTimer raceTimer = new AnimationTimer() {
//Start time of loop.
long timeRaceStarted = System.currentTimeMillis();
int boatOffset = 0;
@Override
public void handle(long arg0) {
if (boatsFinished < startingBoats.size()) {
//Get the current time.
long currentTime = System.currentTimeMillis();
//Update the total elapsed time.
totalTimeElapsed = currentTime - timeRaceStarted;
ArrayList<BoatStatus> boatStatuses = new ArrayList<>();
//For each boat, we update its position, and generate a BoatLocationMessage.
for (int i = 0; i < startingBoats.size(); i++) {
Boat boat = startingBoats.get((i + boatOffset) % startingBoats.size());
if (boat != null) {
//Update position.
if (boat.getTimeFinished() < 0) {
updatePosition(boat, 15);
checkPosition(boat, totalTimeElapsed);
}
if (boat.getTimeFinished() > 0) {
mockOutput.parseBoatLocation(boat.getSourceID(), boat.getCurrentPosition().getLatitude(), boat.getCurrentPosition().getLongitude(), boat.getHeading(), boat.getCurrentSpeed());
boatStatuses.add(new BoatStatus(boat.getSourceID(), BoatStatusEnum.FINISHED, boat.getCurrentLeg().getLegNumber()));
} else {
mockOutput.parseBoatLocation(boat.getSourceID(), boat.getCurrentPosition().getLatitude(), boat.getCurrentPosition().getLongitude(), boat.getHeading(), boat.getCurrentSpeed());
boatStatuses.add(new BoatStatus(boat.getSourceID(),
boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatusEnum.RACING : BoatStatusEnum.DNF, boat.getCurrentLeg().getLegNumber()));
}
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 4, startTime, BoatLocation.convertHeadingDoubleToInt(windDirection), (int) (windSpeed * Constants.KnotsToMMPerSecond), 2, boatStatuses);//TODO FIX replace magic values.
} else {
stop();
}
}
parseMarks();
boatOffset = (boatOffset + 1) % (startingBoats.size());
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 3, startTime, BoatLocation.convertHeadingDoubleToInt(windDirection), (int) (windSpeed * Constants.KnotsToMMPerSecond), 2, boatStatuses);//TODO FIX replace magic values.
mockOutput.parseRaceStatus(raceStatus);
}
}
};
public void initialiseBoats() {
Leg officialStart = legs.get(0);
String name = officialStart.getName();
Marker endMark = officialStart.getEndCompoundMark();
ArrayList<GPSCoordinate> startingPositions = getSpreadStartingPositions();
for (int i = 0; i < startingBoats.size(); i++) {
Boat boat = startingBoats.get(i);
if (boat != null) {
Leg newLeg = new Leg(name, new Marker(startingPositions.get(i)), endMark, 0);
boat.setCurrentLeg(newLeg);
boat.setCurrentSpeed(Constants.TEST_VELOCITIES[i]);//TODO we should get rid of TEST_VELOCITIES since speed is based off of wind speed/angle.
boat.setCurrentPosition(startingPositions.get(i));
boat.setHeading(boat.calculateHeading());
boat.setTimeSinceTackChange(999999);//We set a large time since tack change so that it calculates a new VMG when the simulation starts.
}
}
}
/**
* Creates a list of starting positions for the different boats, so they do not appear cramped at the start line
*
* @return list of starting positions
*/
public ArrayList<GPSCoordinate> getSpreadStartingPositions() {
int nBoats = startingBoats.size();
Marker compoundMark = legs.get(0).getStartCompoundMark();
GeodeticCalculator initialCalc = new GeodeticCalculator();
initialCalc.setStartingGeographicPoint(compoundMark.getMark1().getLongitude(), compoundMark.getMark1().getLatitude());
initialCalc.setDestinationGeographicPoint(compoundMark.getMark2().getLongitude(), compoundMark.getMark2().getLatitude());
double azimuth = initialCalc.getAzimuth();
double distanceBetweenMarkers = initialCalc.getOrthodromicDistance();
double distanceBetweenBoats = distanceBetweenMarkers / (nBoats + 1);
GeodeticCalculator positionCalc = new GeodeticCalculator();
positionCalc.setStartingGeographicPoint(compoundMark.getMark1().getLongitude(), compoundMark.getMark1().getLatitude());
ArrayList<GPSCoordinate> positions = new ArrayList<>();
for (int i = 0; i < nBoats; i++) {
positionCalc.setDirection(azimuth, distanceBetweenBoats);
Point2D position = positionCalc.getDestinationGeographicPoint();
positions.add(new GPSCoordinate(position.getY(), position.getX()));
positionCalc = new GeodeticCalculator();
positionCalc.setStartingGeographicPoint(position);
}
return positions;
}
/**
* Calculates the boats next GPS position based on its distance travelled and heading
*
* @param oldCoordinates GPS coordinates of the boat's starting position
* @param distanceTravelled distance in nautical miles
* @param azimuth boat's current direction. Value between -180 and 180
* @return The boat's new coordinate
*/
public static GPSCoordinate calculatePosition(GPSCoordinate oldCoordinates, double distanceTravelled, double azimuth) {
//Find new coordinate using current heading and distance
GeodeticCalculator geodeticCalculator = new GeodeticCalculator();
//Load start point into calculator
Point2D startPoint = new Point2D.Double(oldCoordinates.getLongitude(), oldCoordinates.getLatitude());
geodeticCalculator.setStartingGeographicPoint(startPoint);
//load direction and distance travelled into calculator
geodeticCalculator.setDirection(azimuth, distanceTravelled * Constants.NMToMetersConversion);
//get new point
Point2D endPoint = geodeticCalculator.getDestinationGeographicPoint();
return new GPSCoordinate(endPoint.getY(), endPoint.getX());
}
private VMG calculateHeading(Boat boat) {
//How fast a boat can turn, in degrees per millisecond.
double turnRate = 0.03;
//How much the boat is allowed to turn, considering how long since it last turned.
double turnAngle = turnRate * boat.getTimeSinceTackChange();
//Find the bounds on what angle the boat is allowed to travel at. The bounds cap out at [0, 360).
double bound1 = Math.max(boat.getHeading() - turnAngle, 0);
double bound2 = Math.min(boat.getHeading() + turnAngle, 360);
return boat.getPolars().calculateVMG(this.windDirection, this.windSpeed, boat.calculateBearingToDestination(), bound1, bound2);
}
private boolean improvesVelocity(Boat boat, VMG newHeading) {
double angleBetweenDestAndHeading = boat.getHeading() - boat.calculateBearingToDestination();
double angleBetweenDestAndNewVMG = newHeading.getBearing() - boat.calculateBearingToDestination();
double currentVelocity = cos(Math.toRadians(angleBetweenDestAndHeading)) * boat.getVelocity();
double vmgVelocity = cos(Math.toRadians(angleBetweenDestAndNewVMG)) * newHeading.getSpeed();
return vmgVelocity > currentVelocity;
}
/**
* Calculates the distance a boat has travelled and updates its current position according to this value.
*
* @param boat to be updated
* @param millisecondsElapsed since last update
*/
protected void updatePosition(Boat boat, int millisecondsElapsed) {
//distanceTravelled = velocity (nm p hr) * time taken to update loop
double distanceTravelled = (boat.getCurrentSpeed() * this.scaleFactor * millisecondsElapsed) / 3600000;
double totalDistanceTravelled;
boolean finish = boat.getCurrentLeg().getName().equals("Finish");
if (!finish) {
double totalDistanceTravelledInTack = distanceTravelled;//TODO FIX// + boat.getDistanceTravelledInTack();
boat.setTimeSinceTackChange(boat.getTimeSinceTackChange() + this.scaleFactor * millisecondsElapsed);
VMG newHeading = calculateHeading(boat);
//Calculate the new VMG.
if (improvesVelocity(boat, newHeading)) {
boat.setHeading(newHeading.getBearing());
boat.setCurrentSpeed(newHeading.getSpeed());
boat.setTimeSinceTackChange(0);
}
double azimuth = boat.getHeading();
if (azimuth > 180) {
azimuth = azimuth - 360;
}
//tests to see if a point in front of the boat is out of bounds, if so mirror heading in the wind
GPSCoordinate test = calculatePosition(boat.getCurrentPosition(), (100.0 / Constants.NMToMetersConversion), azimuth);
if (!GPSCoordinate.isInsideBoundary(test, boundary)) {
double tempHeading = (boat.getHeading() - this.windDirection + 90) % 360;
boat.setHeading(tempHeading);
}
//calc the distance travelled in a straight line to windward
//double angleBetweenDestAndHeading = boat.getHeading() - boat.calculateBearingToDestination();
totalDistanceTravelled = cos(Math.toRadians(boat.getHeading() - boat.calculateBearingToDestination()))*totalDistanceTravelledInTack;
boat.setDistanceTravelledInLeg(totalDistanceTravelled);
//Calculate boat's new position by adding the distance travelled onto the start point of the leg
azimuth = boat.getHeading();
azimuth = azimuth > 180? azimuth - 360 : azimuth;
boat.setCurrentPosition(calculatePosition(boat.getCurrentPosition(), totalDistanceTravelledInTack, azimuth));
}
}
protected void checkPosition(Boat boat, long timeElapsed) {
//System.out.println(boat.getDistanceTravelledInLeg());
//System.out.println(boat.getCurrentLeg().getDistance());
//System.out.println(" ");
//if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
//The distance (in nautical miles) within which the boat needs to get in order to consider that it has reached the marker.
double epsilon = 100.0 / Constants.NMToMetersConversion; //100 meters. TODO should be more like 5-10.
if (boat.calculateDistanceToNextMarker() < epsilon) {
//boat has passed onto new leg
if (boat.getCurrentLeg().getName().equals("Finish")) {
//boat has finished
boatsFinished++;
boat.setTimeFinished(timeElapsed);
boat.setTimeFinished(timeElapsed);
boat.setCurrentSpeed(0);
} else if (doNotFinish()) {
boatsFinished++;
boat.setTimeFinished(timeElapsed);
boat.setCurrentLeg(new Leg("DNF", -1));
boat.setCurrentSpeed(0);
} else {
//Calculate how much the boat overshot the marker by
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
//Move boat on to next leg
Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
boat.setCurrentLeg(nextLeg);
//Add overshoot distance into the distance travelled for the next leg
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
//Setting a high value for this allows the boat to immediately do a large turn, as it has needs to in order to get to the next mark.
boat.setTimeSinceTackChange(999999);
}
}
}
/**
* 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;
}
}
protected boolean doNotFinish() {
Random rand = new Random();
return rand.nextInt(100) < dnfChance;
}
}