|
|
|
|
@ -27,12 +27,10 @@ public abstract class Race implements Runnable {
|
|
|
|
|
protected RaceController controller;
|
|
|
|
|
protected int boatsFinished = 0;
|
|
|
|
|
protected long totalTimeElapsed;
|
|
|
|
|
private int dnfChance = 1; //%percentage chance a boat fails at each checkpoint
|
|
|
|
|
private int lastFPS = 20;
|
|
|
|
|
|
|
|
|
|
protected int scaleFactor;
|
|
|
|
|
|
|
|
|
|
private int SLEEP_TIME = 100; //time in milliseconds to pause in a paced loop
|
|
|
|
|
protected int PRERACE_TIME = 120000; //time in milliseconds to pause during pre-race
|
|
|
|
|
private boolean timerEnabled = true; //boolean to determine if timer is ran
|
|
|
|
|
|
|
|
|
|
@ -64,40 +62,30 @@ public abstract class Race implements Runnable {
|
|
|
|
|
this(raceData.getBoats(), raceData.getLegs(), controller, scaleFactor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void initialiseBoats();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the chance each boat has of failing at a gate or marker
|
|
|
|
|
* @param chance percentage chance a boat has of failing per checkpoint.
|
|
|
|
|
* Randomly generate number to see if boat fails
|
|
|
|
|
* @return True if number lower than dnfChance else false
|
|
|
|
|
*/
|
|
|
|
|
protected void setDnfChance(int chance) {
|
|
|
|
|
if (chance >= 0 && chance <= 100) {
|
|
|
|
|
dnfChance = chance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void initialiseBoats() {
|
|
|
|
|
|
|
|
|
|
Leg officialStart = legs.get(0);
|
|
|
|
|
String name = officialStart.getName();
|
|
|
|
|
Marker endMarker = officialStart.getEndMarker();
|
|
|
|
|
protected abstract boolean doNotFinish();
|
|
|
|
|
|
|
|
|
|
BoatInRace.setTrackPointTimeInterval(BoatInRace.getBaseTrackPointTimeInterval() / scaleFactor);
|
|
|
|
|
|
|
|
|
|
ArrayList<Marker> startMarkers = getSpreadStartingPositions();
|
|
|
|
|
for (int i = 0; i < startingBoats.size(); i++) {
|
|
|
|
|
BoatInRace 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);
|
|
|
|
|
/**
|
|
|
|
|
* Checks the position of the boat, this updates the boats current position.
|
|
|
|
|
*
|
|
|
|
|
* @param boat Boat that the postion is to be updated for.
|
|
|
|
|
* @param timeElapsed Time that has elapse since the start of the the race.
|
|
|
|
|
* @see BoatInRace
|
|
|
|
|
*/
|
|
|
|
|
protected abstract void checkPosition(BoatInRace boat, long timeElapsed);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Updates the boat's gps coordinates depending on time elapsed
|
|
|
|
|
*
|
|
|
|
|
* @param boat to be updated
|
|
|
|
|
* @param millisecondsElapsed time since last update
|
|
|
|
|
*/
|
|
|
|
|
protected abstract void updatePosition(BoatInRace boat, int millisecondsElapsed);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Runnable for the thread.
|
|
|
|
|
@ -193,15 +181,6 @@ public abstract class Race implements Runnable {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Randomly generate number to see if boat fails
|
|
|
|
|
* @return True if number lower than dnfChance else false
|
|
|
|
|
*/
|
|
|
|
|
private boolean doNotFinish() {
|
|
|
|
|
Random rand = new Random();
|
|
|
|
|
return rand.nextInt(100) < dnfChance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
@ -233,18 +212,10 @@ public abstract class Race implements Runnable {
|
|
|
|
|
checkPosition(boat, totalTimeElapsed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (controller != null) controller.updateMap(startingBoats);
|
|
|
|
|
if (timerEnabled)
|
|
|
|
|
updateTime(calcTimer());
|
|
|
|
|
}
|
|
|
|
|
controller.updateMap(startingBoats);
|
|
|
|
|
// } else {
|
|
|
|
|
// //Exit animation timer
|
|
|
|
|
// updateTime(calcTimer());
|
|
|
|
|
// updateFPS(0); //race ended so fps = 0
|
|
|
|
|
// stop(); //exit animation timer
|
|
|
|
|
// }
|
|
|
|
|
fps++;
|
|
|
|
|
if ((System.currentTimeMillis() - timeCurrent) > 1000) {
|
|
|
|
|
updateFPS(fps);
|
|
|
|
|
@ -258,46 +229,10 @@ public abstract class Race implements Runnable {
|
|
|
|
|
}.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks the position of the boat, this updates the boats current position.
|
|
|
|
|
*
|
|
|
|
|
* @param boat Boat that the postion is to be updated for.
|
|
|
|
|
* @param timeElapsed Time that has elapse since the start of the the race.
|
|
|
|
|
* @see BoatInRace
|
|
|
|
|
*/
|
|
|
|
|
protected void checkPosition(BoatInRace boat, long timeElapsed) {
|
|
|
|
|
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
|
|
|
|
|
//boat has passed onto new leg
|
|
|
|
|
if (boat.getCurrentLeg().getName().equals("Finish")) {
|
|
|
|
|
//boat has finished
|
|
|
|
|
boatsFinished++;
|
|
|
|
|
boat.setFinished(true);
|
|
|
|
|
boat.setTimeFinished(timeElapsed);
|
|
|
|
|
} else if (doNotFinish()) {
|
|
|
|
|
boatsFinished++;
|
|
|
|
|
boat.setFinished(true);
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
//Update the boat display table in the GUI to reflect the leg change
|
|
|
|
|
updatePositions();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update position of boats in race, no position if on starting leg or DNF.
|
|
|
|
|
*/
|
|
|
|
|
private void updatePositions() {
|
|
|
|
|
protected void updatePositions() {
|
|
|
|
|
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
|
|
|
|
|
for(BoatInRace boat: startingBoats) {
|
|
|
|
|
if(boat != null) {
|
|
|
|
|
@ -325,45 +260,4 @@ public abstract class Race implements Runnable {
|
|
|
|
|
public ObservableList<BoatInRace> getStartingBoats() {
|
|
|
|
|
return startingBoats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the boat's gps coordinates depending on time elapsed
|
|
|
|
|
*
|
|
|
|
|
* @param boat to be updated
|
|
|
|
|
* @param millisecondsElapsed time since last update
|
|
|
|
|
*/
|
|
|
|
|
protected abstract void updatePosition(BoatInRace boat, int millisecondsElapsed);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|