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.
440 lines
20 KiB
440 lines
20 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 Boat[] startingBoats;
|
|
protected ObservableList<Boat> startingBoats;
|
|
protected List<Leg> legs;
|
|
protected int boatsFinished = 0;
|
|
protected long totalTimeElapsed;
|
|
protected int scaleFactor=25;
|
|
protected int PRERACE_TIME = 180000; //time in milliseconds to pause during pre-race. At the moment, 3 minutes
|
|
private long startTime;
|
|
protected boolean countdownFinish = false;
|
|
protected boolean runRace = true;
|
|
private int lastFPS = 20;
|
|
private int raceId;
|
|
private int dnfChance = 0; //percentage chance a boat fails at each checkpoint
|
|
private MockOutput mockOutput;
|
|
private static int boatOffset = 0;
|
|
private int finished = 0;
|
|
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;
|
|
|
|
|
|
/**
|
|
* Initailiser for Race
|
|
*
|
|
* @param boats Takes in an array of boats that are participating in the race.
|
|
* @param legs Number of marks in order that the boats pass in order to complete the race.
|
|
* @param scaleFactor for race
|
|
*/
|
|
public Race(List<Boat> boats, List<Leg> legs, int raceID, int scaleFactor, MockOutput mockOutput, List<GPSCoordinate> boundary) {
|
|
|
|
this.startingBoats = FXCollections.observableArrayList(boats);
|
|
this.legs = legs;
|
|
this.legs.add(new Leg("Finish", this.legs.size()));
|
|
this.raceId = raceID;
|
|
this.scaleFactor = scaleFactor;
|
|
this.mockOutput = mockOutput;
|
|
this.boundary = boundary;
|
|
|
|
this.windSpeed = 12;//TODO could use input parameters for these. And should fluctuate during race.
|
|
this.windDirection = 180;
|
|
|
|
//TODO refactor
|
|
this.startTime = System.currentTimeMillis() + (this.PRERACE_TIME / this.scaleFactor);
|
|
|
|
if (startingBoats != null && startingBoats.size() > 0) {
|
|
initialiseBoats();
|
|
}
|
|
|
|
}
|
|
|
|
public Race(RaceDataSource raceData, int scaleFactor, MockOutput mockOutput) {
|
|
this(raceData.getBoats(), raceData.getLegs(), raceData.getRaceId(), scaleFactor, mockOutput, raceData.getBoundary());
|
|
}
|
|
|
|
/**
|
|
* 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 tranvelled into calculator
|
|
geodeticCalculator.setDirection(azimuth, distanceTravelled * Constants.NMToMetersConversion);
|
|
//get new point
|
|
Point2D endPoint = geodeticCalculator.getDestinationGeographicPoint();
|
|
|
|
return new GPSCoordinate(endPoint.getY(), endPoint.getX());
|
|
}
|
|
|
|
/**
|
|
* Runnable for the thread.
|
|
*/
|
|
public void run() {
|
|
initialiseBoats();
|
|
countdownTimer();
|
|
}
|
|
|
|
/**
|
|
* Starts the heartbeat timer, which sends a heartbeat message every so often (i.e., 5 seconds).
|
|
*/
|
|
/**
|
|
* Countdown timer until race starts. Use PRERACE_TIME to set countdown duration.
|
|
*/
|
|
protected void countdownTimer() {
|
|
AnimationTimer timer = new AnimationTimer() {
|
|
long currentTime = System.currentTimeMillis();
|
|
//long startTime = currentTime + (PRERACE_TIME / scaleFactor);
|
|
//long minutes;
|
|
//long hours;
|
|
long timeLeft;
|
|
|
|
@Override
|
|
public void handle(long arg0) {
|
|
timeLeft = startTime - currentTime;
|
|
ArrayList<BoatStatus> boatStatuses = new ArrayList<>();
|
|
//For each boat, we update it's position, and generate a BoatLocationMessage.
|
|
for (int i = 0; i < startingBoats.size(); i++) {
|
|
Boat boat = startingBoats.get((i + boatOffset) % startingBoats.size());
|
|
if (boat != null) {
|
|
mockOutput.parseBoatLocation(boat.getSourceID(), boat.getCurrentPosition().getLatitude(), boat.getCurrentPosition().getLongitude(), boat.getHeading(), 0);
|
|
boatStatuses.add(new BoatStatus(boat.getSourceID(),
|
|
boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatusEnum.RACING : BoatStatusEnum.DNF, boat.getCurrentLeg().getLegNumber()));
|
|
}
|
|
}
|
|
boatOffset = (boatOffset + 1) % (startingBoats.size());
|
|
|
|
if (timeLeft <= 60000/scaleFactor && timeLeft > 0) {
|
|
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 2, startTime, BoatLocation.convertHeadingDoubleToInt(windDirection), (int) (windSpeed * Constants.KnotsToMMPerSecond), 1, boatStatuses);
|
|
mockOutput.parseRaceStatus(raceStatus);
|
|
}
|
|
else if (timeLeft <= 0) {
|
|
countdownFinish = true;
|
|
if (runRace) {
|
|
simulateRace();
|
|
}
|
|
stop();
|
|
}
|
|
else {
|
|
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 1, startTime, BoatLocation.convertHeadingDoubleToInt(windDirection), (int) (windSpeed * Constants.KnotsToMMPerSecond),1, boatStatuses);
|
|
mockOutput.parseRaceStatus(raceStatus);
|
|
}
|
|
currentTime = System.currentTimeMillis();
|
|
}
|
|
};
|
|
timer.start();
|
|
//countdownFinish = true;
|
|
}
|
|
|
|
/**
|
|
* Starts the Race Simulation, playing the race start to finish with the timescale.
|
|
* This prints the boats participating, the order that the events occur in time order, and the respective information of the events.
|
|
*/
|
|
private void simulateRace() {
|
|
|
|
System.setProperty("javafx.animation.fullspeed", "true");
|
|
|
|
for (Boat boat : startingBoats) {
|
|
boat.setStarted(true);
|
|
}
|
|
|
|
new AnimationTimer() {
|
|
//Start time of loop.
|
|
long timeRaceStarted = System.currentTimeMillis();
|
|
|
|
@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<BoatStatus>();
|
|
finished = 0;
|
|
//For each boat, we update it's 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, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS));
|
|
checkPosition(boat, totalTimeElapsed);
|
|
}
|
|
if (boat.getTimeFinished() > 0) {
|
|
mockOutput.parseBoatLocation(boat.getSourceID(), boat.getCurrentPosition().getLatitude(), boat.getCurrentPosition().getLongitude(), boat.getHeading(), boat.getVelocity());
|
|
boatStatuses.add(new BoatStatus(boat.getSourceID(), BoatStatusEnum.FINISHED, boat.getCurrentLeg().getLegNumber()));
|
|
finished++;
|
|
} else {
|
|
mockOutput.parseBoatLocation(boat.getSourceID(), boat.getCurrentPosition().getLatitude(), boat.getCurrentPosition().getLongitude(), boat.getHeading(), boat.getVelocity());
|
|
boatStatuses.add(new BoatStatus(boat.getSourceID(),
|
|
boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatusEnum.RACING : BoatStatusEnum.DNF, boat.getCurrentLeg().getLegNumber()));
|
|
}
|
|
if (startingBoats.size()==finished){
|
|
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 4, startTime, BoatLocation.convertHeadingDoubleToInt(windDirection), (int) (windSpeed * Constants.KnotsToMMPerSecond), 2, boatStatuses);//TODO FIX replace magic values.
|
|
mockOutput.parseRaceStatus(raceStatus);
|
|
}
|
|
} else {
|
|
stop();
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}.start();
|
|
}
|
|
public void initialiseBoats() {
|
|
Leg officialStart = legs.get(0);
|
|
String name = officialStart.getName();
|
|
Marker endMarker = officialStart.getEndMarker();
|
|
|
|
ArrayList<Marker> startMarkers = getSpreadStartingPositions();
|
|
for (int i = 0; i < startingBoats.size(); i++) {
|
|
Boat boat = startingBoats.get(i);
|
|
if (boat != null) {
|
|
boat.setScaledVelocity(boat.getVelocity() * scaleFactor);
|
|
Leg startLeg = new Leg(name, 0);
|
|
boat.setCurrentPosition(startMarkers.get(i).getAverageGPSCoordinate());
|
|
startLeg.setStartMarker(startMarkers.get(i));
|
|
startLeg.setEndMarker(endMarker);
|
|
startLeg.calculateDistance();
|
|
boat.setCurrentLeg(startLeg);
|
|
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<Marker> getSpreadStartingPositions() {
|
|
|
|
int nBoats = startingBoats.size();
|
|
Marker marker = legs.get(0).getStartMarker();
|
|
|
|
GeodeticCalculator initialCalc = new GeodeticCalculator();
|
|
initialCalc.setStartingGeographicPoint(marker.getMark1().getLongitude(), marker.getMark1().getLatitude());
|
|
initialCalc.setDestinationGeographicPoint(marker.getMark2().getLongitude(), marker.getMark2().getLatitude());
|
|
|
|
double azimuth = initialCalc.getAzimuth();
|
|
double distanceBetweenMarkers = initialCalc.getOrthodromicDistance();
|
|
double distanceBetweenBoats = distanceBetweenMarkers / (nBoats + 1);
|
|
|
|
GeodeticCalculator positionCalc = new GeodeticCalculator();
|
|
positionCalc.setStartingGeographicPoint(marker.getMark1().getLongitude(), marker.getMark1().getLatitude());
|
|
ArrayList<Marker> positions = new ArrayList<>();
|
|
|
|
for (int i = 0; i < nBoats; i++) {
|
|
positionCalc.setDirection(azimuth, distanceBetweenBoats);
|
|
Point2D position = positionCalc.getDestinationGeographicPoint();
|
|
positions.add(new Marker(new GPSCoordinate(position.getY(), position.getX())));
|
|
|
|
positionCalc = new GeodeticCalculator();
|
|
positionCalc.setStartingGeographicPoint(position);
|
|
}
|
|
return positions;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* 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.getVelocity() * this.scaleFactor * millisecondsElapsed) / 3600000;
|
|
|
|
double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg();
|
|
|
|
boolean finish = boat.getCurrentLeg().getName().equals("Finish");
|
|
if (!finish) {
|
|
|
|
|
|
double totalDistanceTravelledInTack = distanceTravelled + boat.getDistanceTravelledInTack();
|
|
|
|
double bound1 = (boat.calculateBearingToDestination() - 90) % 360;
|
|
double bound2 = (boat.calculateBearingToDestination() + 90) % 360;
|
|
//TODO the actual bearing bounds need to be the interval in which the boat won't go out of bounds.
|
|
bound1 = 0;
|
|
bound2 = 360;
|
|
|
|
boat.setTimeSinceTackChange(boat.getTimeSinceTackChange() + this.scaleFactor * millisecondsElapsed);
|
|
|
|
|
|
//How fast a boat can turn, in degrees per millisecond.
|
|
double turnRate = 0.03; //Roughly 30 per second, or 12 seconds per revolution.
|
|
|
|
//How much the boat is allowed to turn, considering how long since it last turned.
|
|
double turnAngle = turnRate * boat.getTimeSinceTackChange();
|
|
|
|
|
|
//System.out.println("boat " + boat.getAbbrev() + " turn angle is " + turnAngle + ".");//TEMP DEBUG REMOVE
|
|
|
|
//Find the bounds on what angle the boat is allowed to travel at.
|
|
bound1 = boat.getHeading() - turnAngle;
|
|
bound2 = boat.getHeading() + turnAngle;
|
|
|
|
|
|
VMG newHeading = boat.getPolars().calculateVMG(this.windDirection, this.windSpeed,
|
|
boat.calculateBearingToDestination(), bound1, bound2);
|
|
|
|
if (!GPSCoordinate.isInsideBoundary(boat.getCurrentPosition(), boundary)){
|
|
double tempHeading = (newHeading.getBearing() - this.windDirection +90)%360;
|
|
newHeading.setBearing(tempHeading);
|
|
}
|
|
|
|
//Is this new VMG better than the current VMG?
|
|
|
|
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();
|
|
//System.out.println("boat " + boat.getAbbrev() + " current velocity is " + currentVelocity + " knots, possible VMG is " + vmgVelocity + " knots.");//TEMP DEBUG REMOVE
|
|
if (vmgVelocity > currentVelocity) {
|
|
boat.setHeading(newHeading.getBearing());
|
|
boat.setVelocity(newHeading.getSpeed());
|
|
boat.setTimeSinceTackChange(0);
|
|
|
|
//System.out.println("boat " + boat.getAbbrev() + " has a new bearing " + boat.getHeading() + " degrees, and is " + boat.calculateDistanceToNextMarker() + " nautical miles to the next marker. Velocity to next marker is " + boat.getVelocity() + " knots.");//TEMP DEBUG REMOVE
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO one way to fix the boat's rapid turning it to only update the velocity/heading every X seconds (e.g., every 5 seconds).
|
|
//TODO may need a lower tack period
|
|
//TODO another way would be to allow boats use a better VMG if it is within turnRate * timeSinceTackChange. E.g., after 100ms a boat can select a more optimal VMG within 5deg of their current bearing. After 500ms VMG can be within 25deg of current bearing. After 2sec it can be 100deg of bearing, etc...
|
|
|
|
//calc the distance travelled in a straight line to windward
|
|
//double angleBetweenDestAndHeading = boat.getHeading() - boat.calculateBearingToDestination();
|
|
totalDistanceTravelled = cos(Math.toRadians(angleBetweenDestAndHeading))*totalDistanceTravelledInTack;
|
|
boat.setDistanceTravelledInLeg(totalDistanceTravelled);
|
|
|
|
|
|
//Calculate boat's new position by adding the distance travelled onto the start point of the leg
|
|
double azimuth = boat.getHeading();
|
|
if (azimuth > 180) {
|
|
azimuth = azimuth - 360;
|
|
}
|
|
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);
|
|
} else if (doNotFinish()) {
|
|
boatsFinished++;
|
|
boat.setTimeFinished(timeElapsed);
|
|
boat.setCurrentLeg(new Leg("DNF", -1));
|
|
boat.setVelocity(0);
|
|
boat.setScaledVelocity(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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the boats that have started the race.
|
|
*
|
|
* @return ObservableList of Boat class that participated in the race.
|
|
* @see ObservableList
|
|
* @see Boat
|
|
*/
|
|
public ObservableList<Boat> getStartingBoats() {
|
|
return startingBoats;
|
|
}
|
|
}
|