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.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; /** * Parent class for races * Created by fwy13 on 3/03/17. */ public class Race implements Runnable { protected ObservableList startingBoats; protected List legs; protected int boatsFinished = 0; protected long totalTimeElapsed; protected int scaleFactor = 3; private long startTime; private int raceId; private int dnfChance = 0; //percentage chance a boat fails at each checkpoint private MockOutput mockOutput; public Race(RaceDataSource raceData, MockOutput mockOutput) { this.startingBoats = FXCollections.observableArrayList(raceData.getBoats()); this.legs = raceData.getLegs(); this.legs.add(new Leg("Finish", this.legs.size())); this.raceId = raceData.getRaceId(); this.mockOutput = mockOutput; this.startTime = System.currentTimeMillis() + (Constants.PRE_RACE_WAIT_TIME / this.scaleFactor); } /** * Runnable for the thread. */ public void run() { initialiseBoats(); countdownTimer.start(); } /** * 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 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)); } 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 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.getVelocity()); 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.getVelocity()); boatStatuses.add(new BoatStatus(boat.getSourceID(), boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatusEnum.RACING : BoatStatusEnum.DNF, boat.getCurrentLeg().getLegNumber())); } } else { stop(); } } boatOffset = (boatOffset + 1) % (startingBoats.size()); RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 3, startTime, 0, 2300, 2, boatStatuses); mockOutput.parseRaceStatus(raceStatus); } } }; public void initialiseBoats() { Leg officialStart = legs.get(0); String name = officialStart.getName(); Marker endMark = officialStart.getEndCompoundMark(); ArrayList 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.setVelocity(Constants.TEST_VELOCITIES[i]); boat.setScaledVelocity(boat.getVelocity() * scaleFactor); boat.setCurrentPosition(startingPositions.get(i)); boat.setHeading(boat.calculateHeading()); } } } /** * 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 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 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()); } /** * 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.getScaledVelocity() * millisecondsElapsed) / 3600000; double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg(); boolean finish = boat.getCurrentLeg().getName().equals("Finish"); if (!finish) { boat.setHeading(boat.calculateHeading()); //update boat's distance travelled boat.setDistanceTravelledInLeg(totalDistanceTravelled); //Calculate boat's new position by adding the distance travelled onto the start point of the leg boat.setCurrentPosition(calculatePosition(boat.getCurrentLeg().getStartCompoundMark().getAverageGPSCoordinate(), totalDistanceTravelled, boat.calculateAzimuth())); } } protected void checkPosition(Boat 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.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()); } } } /** * 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; } }