|
|
|
@ -5,9 +5,9 @@ import javafx.animation.AnimationTimer;
|
|
|
|
import javafx.collections.FXCollections;
|
|
|
|
import javafx.collections.FXCollections;
|
|
|
|
import javafx.collections.ObservableList;
|
|
|
|
import javafx.collections.ObservableList;
|
|
|
|
import org.geotools.referencing.GeodeticCalculator;
|
|
|
|
import org.geotools.referencing.GeodeticCalculator;
|
|
|
|
import seng302.DataInput.RaceDataSource;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import SharedModel.*;
|
|
|
|
|
|
|
|
|
|
|
|
import seng302.DataInput.RaceDataSource;
|
|
|
|
import seng302.Networking.MockOutput;
|
|
|
|
import seng302.Networking.MockOutput;
|
|
|
|
import seng302.Networking.Utils.BoatStatusMessage;
|
|
|
|
import seng302.Networking.Utils.BoatStatusMessage;
|
|
|
|
import seng302.Networking.Utils.Enums.BoatStatus;
|
|
|
|
import seng302.Networking.Utils.Enums.BoatStatus;
|
|
|
|
@ -24,9 +24,9 @@ import java.util.Random;
|
|
|
|
* Created by fwy13 on 3/03/17.
|
|
|
|
* Created by fwy13 on 3/03/17.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class Race implements Runnable {
|
|
|
|
public class Race implements Runnable {
|
|
|
|
//protected BoatInRace[] startingBoats;
|
|
|
|
//protected SharedModel.BoatInRace[] startingBoats;
|
|
|
|
protected ObservableList<BoatInRace> startingBoats;
|
|
|
|
protected ObservableList<SharedModel.BoatInRace> startingBoats;
|
|
|
|
protected List<Leg> legs;
|
|
|
|
protected List<SharedModel.Leg> legs;
|
|
|
|
protected int boatsFinished = 0;
|
|
|
|
protected int boatsFinished = 0;
|
|
|
|
protected long totalTimeElapsed;
|
|
|
|
protected long totalTimeElapsed;
|
|
|
|
protected int scaleFactor;
|
|
|
|
protected int scaleFactor;
|
|
|
|
@ -44,11 +44,11 @@ public class Race implements Runnable {
|
|
|
|
* @param legs Number of marks in order that the boats pass in order to complete the race.
|
|
|
|
* @param legs Number of marks in order that the boats pass in order to complete the race.
|
|
|
|
* @param scaleFactor for race
|
|
|
|
* @param scaleFactor for race
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public Race(List<BoatInRace> boats, List<Leg> legs, int scaleFactor, MockOutput mockOutput) {
|
|
|
|
public Race(List<SharedModel.BoatInRace> boats, List<SharedModel.Leg> legs, int scaleFactor, MockOutput mockOutput) {
|
|
|
|
|
|
|
|
|
|
|
|
this.startingBoats = FXCollections.observableArrayList(boats);
|
|
|
|
this.startingBoats = FXCollections.observableArrayList(boats);
|
|
|
|
this.legs = legs;
|
|
|
|
this.legs = legs;
|
|
|
|
this.legs.add(new Leg("Finish", this.legs.size()));
|
|
|
|
this.legs.add(new SharedModel.Leg("Finish", this.legs.size()));
|
|
|
|
this.scaleFactor = scaleFactor;
|
|
|
|
this.scaleFactor = scaleFactor;
|
|
|
|
this.mockOutput = mockOutput;
|
|
|
|
this.mockOutput = mockOutput;
|
|
|
|
|
|
|
|
|
|
|
|
@ -68,14 +68,14 @@ public class Race implements Runnable {
|
|
|
|
this.raceId = raceId;
|
|
|
|
this.raceId = raceId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Calculates the boats next GPS position based on its distance travelled and heading
|
|
|
|
* Calculates the boats next SharedModel.GPS position based on its distance travelled and heading
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param oldCoordinates GPS coordinates of the boat's starting position
|
|
|
|
* @param oldCoordinates SharedModel.GPS coordinates of the boat's starting position
|
|
|
|
* @param distanceTravelled distance in nautical miles
|
|
|
|
* @param distanceTravelled distance in nautical miles
|
|
|
|
* @param azimuth boat's current direction. Value between -180 and 180
|
|
|
|
* @param azimuth boat's current direction. Value between -180 and 180
|
|
|
|
* @return The boat's new coordinate
|
|
|
|
* @return The boat's new coordinate
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static GPSCoordinate calculatePosition(GPSCoordinate oldCoordinates, double distanceTravelled, double azimuth) {
|
|
|
|
public static SharedModel.GPSCoordinate calculatePosition(SharedModel.GPSCoordinate oldCoordinates, double distanceTravelled, double azimuth) {
|
|
|
|
|
|
|
|
|
|
|
|
//Find new coordinate using current heading and distance
|
|
|
|
//Find new coordinate using current heading and distance
|
|
|
|
|
|
|
|
|
|
|
|
@ -84,11 +84,11 @@ public class Race implements Runnable {
|
|
|
|
Point2D startPoint = new Point2D.Double(oldCoordinates.getLongitude(), oldCoordinates.getLatitude());
|
|
|
|
Point2D startPoint = new Point2D.Double(oldCoordinates.getLongitude(), oldCoordinates.getLatitude());
|
|
|
|
geodeticCalculator.setStartingGeographicPoint(startPoint);
|
|
|
|
geodeticCalculator.setStartingGeographicPoint(startPoint);
|
|
|
|
//load direction and distance tranvelled into calculator
|
|
|
|
//load direction and distance tranvelled into calculator
|
|
|
|
geodeticCalculator.setDirection(azimuth, distanceTravelled * Constants.NMToMetersConversion);
|
|
|
|
geodeticCalculator.setDirection(azimuth, distanceTravelled * SharedModel.Constants.NMToMetersConversion);
|
|
|
|
//get new point
|
|
|
|
//get new point
|
|
|
|
Point2D endPoint = geodeticCalculator.getDestinationGeographicPoint();
|
|
|
|
Point2D endPoint = geodeticCalculator.getDestinationGeographicPoint();
|
|
|
|
|
|
|
|
|
|
|
|
return new GPSCoordinate(endPoint.getY(), endPoint.getX());
|
|
|
|
return new SharedModel.GPSCoordinate(endPoint.getY(), endPoint.getX());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
@ -142,7 +142,7 @@ public class Race implements Runnable {
|
|
|
|
|
|
|
|
|
|
|
|
System.setProperty("javafx.animation.fullspeed", "true");
|
|
|
|
System.setProperty("javafx.animation.fullspeed", "true");
|
|
|
|
|
|
|
|
|
|
|
|
for (BoatInRace boat : startingBoats) {
|
|
|
|
for (SharedModel.BoatInRace boat : startingBoats) {
|
|
|
|
boat.setStarted(true);
|
|
|
|
boat.setStarted(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -160,7 +160,7 @@ public class Race implements Runnable {
|
|
|
|
ArrayList<BoatStatusMessage> boatStatusMessages = new ArrayList<BoatStatusMessage>();
|
|
|
|
ArrayList<BoatStatusMessage> boatStatusMessages = new ArrayList<BoatStatusMessage>();
|
|
|
|
|
|
|
|
|
|
|
|
//For each boat, we update it's position, and generate a BoatLocationMessage.
|
|
|
|
//For each boat, we update it's position, and generate a BoatLocationMessage.
|
|
|
|
for (BoatInRace boat : startingBoats) {
|
|
|
|
for (SharedModel.BoatInRace boat : startingBoats) {
|
|
|
|
if (boat != null && !boat.isFinished()) {
|
|
|
|
if (boat != null && !boat.isFinished()) {
|
|
|
|
//Update position.
|
|
|
|
//Update position.
|
|
|
|
updatePosition(boat, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS));
|
|
|
|
updatePosition(boat, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS));
|
|
|
|
@ -184,7 +184,7 @@ public class Race implements Runnable {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected void updatePositions() {
|
|
|
|
protected void updatePositions() {
|
|
|
|
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
|
|
|
|
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
|
|
|
|
for (BoatInRace boat : startingBoats) {
|
|
|
|
for (SharedModel.BoatInRace boat : startingBoats) {
|
|
|
|
if (boat != null) {
|
|
|
|
if (boat != null) {
|
|
|
|
boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1));
|
|
|
|
boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1));
|
|
|
|
//System.out.println(boat.toString() + " " + boat.getPosition());//TEMP debug print
|
|
|
|
//System.out.println(boat.toString() + " " + boat.getPosition());//TEMP debug print
|
|
|
|
@ -196,16 +196,16 @@ public class Race implements Runnable {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void initialiseBoats() {
|
|
|
|
public void initialiseBoats() {
|
|
|
|
Leg officialStart = legs.get(0);
|
|
|
|
SharedModel.Leg officialStart = legs.get(0);
|
|
|
|
String name = officialStart.getName();
|
|
|
|
String name = officialStart.getName();
|
|
|
|
Marker endMarker = officialStart.getEndMarker();
|
|
|
|
SharedModel.Marker endMarker = officialStart.getEndMarker();
|
|
|
|
|
|
|
|
|
|
|
|
ArrayList<Marker> startMarkers = getSpreadStartingPositions();
|
|
|
|
ArrayList<SharedModel.Marker> startMarkers = getSpreadStartingPositions();
|
|
|
|
for (int i = 0; i < startingBoats.size(); i++) {
|
|
|
|
for (int i = 0; i < startingBoats.size(); i++) {
|
|
|
|
BoatInRace boat = startingBoats.get(i);
|
|
|
|
SharedModel.BoatInRace boat = startingBoats.get(i);
|
|
|
|
if (boat != null) {
|
|
|
|
if (boat != null) {
|
|
|
|
boat.setScaledVelocity(boat.getVelocity() * scaleFactor);
|
|
|
|
boat.setScaledVelocity(boat.getVelocity() * scaleFactor);
|
|
|
|
Leg startLeg = new Leg(name, 0);
|
|
|
|
SharedModel.Leg startLeg = new SharedModel.Leg(name, 0);
|
|
|
|
boat.setCurrentPosition(startMarkers.get(i).getAverageGPSCoordinate());
|
|
|
|
boat.setCurrentPosition(startMarkers.get(i).getAverageGPSCoordinate());
|
|
|
|
startLeg.setStartMarker(startMarkers.get(i));
|
|
|
|
startLeg.setStartMarker(startMarkers.get(i));
|
|
|
|
startLeg.setEndMarker(endMarker);
|
|
|
|
startLeg.setEndMarker(endMarker);
|
|
|
|
@ -221,10 +221,10 @@ public class Race implements Runnable {
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return list of starting positions
|
|
|
|
* @return list of starting positions
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public ArrayList<Marker> getSpreadStartingPositions() {
|
|
|
|
public ArrayList<SharedModel.Marker> getSpreadStartingPositions() {
|
|
|
|
|
|
|
|
|
|
|
|
int nBoats = startingBoats.size();
|
|
|
|
int nBoats = startingBoats.size();
|
|
|
|
Marker marker = legs.get(0).getStartMarker();
|
|
|
|
SharedModel.Marker marker = legs.get(0).getStartMarker();
|
|
|
|
|
|
|
|
|
|
|
|
GeodeticCalculator initialCalc = new GeodeticCalculator();
|
|
|
|
GeodeticCalculator initialCalc = new GeodeticCalculator();
|
|
|
|
initialCalc.setStartingGeographicPoint(marker.getMark1().getLongitude(), marker.getMark1().getLatitude());
|
|
|
|
initialCalc.setStartingGeographicPoint(marker.getMark1().getLongitude(), marker.getMark1().getLatitude());
|
|
|
|
@ -236,12 +236,12 @@ public class Race implements Runnable {
|
|
|
|
|
|
|
|
|
|
|
|
GeodeticCalculator positionCalc = new GeodeticCalculator();
|
|
|
|
GeodeticCalculator positionCalc = new GeodeticCalculator();
|
|
|
|
positionCalc.setStartingGeographicPoint(marker.getMark1().getLongitude(), marker.getMark1().getLatitude());
|
|
|
|
positionCalc.setStartingGeographicPoint(marker.getMark1().getLongitude(), marker.getMark1().getLatitude());
|
|
|
|
ArrayList<Marker> positions = new ArrayList<>();
|
|
|
|
ArrayList<SharedModel.Marker> positions = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < nBoats; i++) {
|
|
|
|
for (int i = 0; i < nBoats; i++) {
|
|
|
|
positionCalc.setDirection(azimuth, distanceBetweenBoats);
|
|
|
|
positionCalc.setDirection(azimuth, distanceBetweenBoats);
|
|
|
|
Point2D position = positionCalc.getDestinationGeographicPoint();
|
|
|
|
Point2D position = positionCalc.getDestinationGeographicPoint();
|
|
|
|
positions.add(new Marker(new GPSCoordinate(position.getY(), position.getX())));
|
|
|
|
positions.add(new SharedModel.Marker(new SharedModel.GPSCoordinate(position.getY(), position.getX())));
|
|
|
|
|
|
|
|
|
|
|
|
positionCalc = new GeodeticCalculator();
|
|
|
|
positionCalc = new GeodeticCalculator();
|
|
|
|
positionCalc.setStartingGeographicPoint(position);
|
|
|
|
positionCalc.setStartingGeographicPoint(position);
|
|
|
|
@ -271,7 +271,7 @@ public class Race implements Runnable {
|
|
|
|
* @param boat to be updated
|
|
|
|
* @param boat to be updated
|
|
|
|
* @param millisecondsElapsed since last update
|
|
|
|
* @param millisecondsElapsed since last update
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected void updatePosition(BoatInRace boat, int millisecondsElapsed) {
|
|
|
|
protected void updatePosition(SharedModel.BoatInRace boat, int millisecondsElapsed) {
|
|
|
|
|
|
|
|
|
|
|
|
//distanceTravelled = velocity (nm p hr) * time taken to update loop
|
|
|
|
//distanceTravelled = velocity (nm p hr) * time taken to update loop
|
|
|
|
double distanceTravelled = (boat.getScaledVelocity() * millisecondsElapsed) / 3600000;
|
|
|
|
double distanceTravelled = (boat.getScaledVelocity() * millisecondsElapsed) / 3600000;
|
|
|
|
@ -289,7 +289,7 @@ public class Race implements Runnable {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void checkPosition(BoatInRace boat, long timeElapsed) {
|
|
|
|
protected void checkPosition(SharedModel.BoatInRace boat, long timeElapsed) {
|
|
|
|
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
|
|
|
|
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
|
|
|
|
//boat has passed onto new leg
|
|
|
|
//boat has passed onto new leg
|
|
|
|
if (boat.getCurrentLeg().getName().equals("Finish")) {
|
|
|
|
if (boat.getCurrentLeg().getName().equals("Finish")) {
|
|
|
|
@ -300,14 +300,14 @@ public class Race implements Runnable {
|
|
|
|
} else if (doNotFinish()) {
|
|
|
|
} else if (doNotFinish()) {
|
|
|
|
boatsFinished++;
|
|
|
|
boatsFinished++;
|
|
|
|
boat.setFinished(true);
|
|
|
|
boat.setFinished(true);
|
|
|
|
boat.setCurrentLeg(new Leg("DNF", -1));
|
|
|
|
boat.setCurrentLeg(new SharedModel.Leg("DNF", -1));
|
|
|
|
boat.setVelocity(0);
|
|
|
|
boat.setVelocity(0);
|
|
|
|
boat.setScaledVelocity(0);
|
|
|
|
boat.setScaledVelocity(0);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
//Calculate how much the boat overshot the marker by
|
|
|
|
//Calculate how much the boat overshot the marker by
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
|
|
|
|
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
|
|
|
|
//Move boat on to next leg
|
|
|
|
//Move boat on to next leg
|
|
|
|
Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
|
|
|
|
SharedModel.Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
|
|
|
|
|
|
|
|
|
|
|
|
boat.setCurrentLeg(nextLeg);
|
|
|
|
boat.setCurrentLeg(nextLeg);
|
|
|
|
//Add overshoot distance into the distance travelled for the next leg
|
|
|
|
//Add overshoot distance into the distance travelled for the next leg
|
|
|
|
@ -321,11 +321,11 @@ public class Race implements Runnable {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Returns the boats that have started the race.
|
|
|
|
* Returns the boats that have started the race.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return ObservableList of BoatInRace class that participated in the race.
|
|
|
|
* @return ObservableList of SharedModel.BoatInRace class that participated in the race.
|
|
|
|
* @see ObservableList
|
|
|
|
* @see ObservableList
|
|
|
|
* @see BoatInRace
|
|
|
|
* @see SharedModel.BoatInRace
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public ObservableList<BoatInRace> getStartingBoats() {
|
|
|
|
public ObservableList<SharedModel.BoatInRace> getStartingBoats() {
|
|
|
|
return startingBoats;
|
|
|
|
return startingBoats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|