diff --git a/racevisionGame/src/main/java/mock/model/MockRace.java b/racevisionGame/src/main/java/mock/model/MockRace.java index e7ee2dec..5ca68865 100644 --- a/racevisionGame/src/main/java/mock/model/MockRace.java +++ b/racevisionGame/src/main/java/mock/model/MockRace.java @@ -48,35 +48,6 @@ public class MockRace extends Race { private int scaleFactor; - /** - * The percent chance that a boat fails the race, and enters a DNF state, at each checkpoint. - * 0 = 0%, 100 = 100%. - */ - private int dnfChance = 0; - - - - /** - * Used to generate random numbers when changing the wind direction. - */ - private int changeWind = 4; - - /** - * The bearing the wind direction starts at. - */ - private static final Bearing windBaselineBearing = Bearing.fromDegrees(225); - - /** - * The lower bearing angle that the wind may have. - */ - private static final Bearing windLowerBound = Bearing.fromDegrees(215); - - /** - * The upper bearing angle that the wind may have. - */ - private static final Bearing windUpperBound = Bearing.fromDegrees(235); - - /** @@ -98,12 +69,7 @@ public class MockRace extends Race { this.shrinkBoundary = GPSCoordinate.getShrinkBoundary(this.boundary); - - - this.windSpeed = 12; - this.windDirection = Bearing.fromDegrees(180); - - + this.wind = new Wind(); } /** @@ -140,16 +106,8 @@ public class MockRace extends Race { */ public void run() { initialiseBoats(); - initialiseWindDirection(); this.countdownTimer.start(); } - /** - * Sets the current race status. - * @param raceStatusEnum The new status of the race. - */ - protected void setRaceStatusEnum(RaceStatusEnum raceStatusEnum) { - this.raceStatusEnum = raceStatusEnum; - } /** @@ -295,8 +253,8 @@ public class MockRace extends Race { //Convert wind direction and speed to ints. //TODO this conversion should be done inside the racestatus class. - int windDirectionInt = AC35UnitConverter.encodeHeading(this.windDirection.degrees()); - int windSpeedInt = (int) (this.windSpeed * Constants.KnotsToMMPerSecond); + int windDirectionInt = AC35UnitConverter.encodeHeading(wind.getWindDirection().degrees()); + int windSpeedInt = (int) (wind.getWindSpeed() * Constants.KnotsToMMPerSecond); //Create race status object, and send it. RaceStatus raceStatus = new RaceStatus( @@ -578,7 +536,7 @@ public class MockRace extends Race { //Find the VMG inside these bounds. - VMG bestVMG = boat.getPolars().calculateVMG(this.windDirection, this.windSpeed, boat.calculateBearingToNextMarker(), Bearing.fromDegrees(0d), Bearing.fromDegrees(359.99999d)); + VMG bestVMG = boat.getPolars().calculateVMG(wind.getWindDirection(), wind.getWindSpeed(), boat.calculateBearingToNextMarker(), Bearing.fromDegrees(0d), Bearing.fromDegrees(359.99999d)); return bestVMG; @@ -717,44 +675,12 @@ public class MockRace extends Race { boat.setCurrentSpeed(0); boat.setStatus(BoatStatusEnum.FINISHED); - } else if (doNotFinish()) { - //Boat has pulled out of race. - boat.setTimeFinished(timeElapsed); - boat.setCurrentLeg(new Leg("DNF", -1)); - boat.setCurrentSpeed(0); - boat.setStatus(BoatStatusEnum.DNF); - } } } - - - - /** - * 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; - } - } - - /** - * Decides if a boat should received a DNF status. - * @return True means it should DNF, false means it shouldn't. - */ - protected boolean doNotFinish() { - Random rand = new Random(); - return rand.nextInt(100) < dnfChance; - } - - - /** * Returns the number of boats that are still active in the race. * They become inactive by either finishing or withdrawing. @@ -785,42 +711,11 @@ public class MockRace extends Race { return boats; } - - /** - * Initialises the wind bearing with the value of the windBaselineBearing. - */ - protected void initialiseWindDirection() { - //Set the starting bearing. - this.windDirection = Bearing.fromDegrees(MockRace.windBaselineBearing.degrees()); - } - - /** * Changes the wind direction randomly, while keeping it within [windLowerBound, windUpperBound]. */ protected void changeWindDirection() { - - //Randomly add or remove 0.5 degrees. - int r = new Random().nextInt(changeWind) + 1; - - if (r == 1) { - //Add 0.5 degrees to the wind bearing. - this.windDirection.setDegrees(this.windDirection.degrees() + 0.5); - - } else if (r == 2) { - //Minus 0.5 degrees from the wind bearing. - this.windDirection.setDegrees(this.windDirection.degrees() - 0.5); - - } - - //Ensure that the wind is in the correct bounds. - if (this.windDirection.degrees() > MockRace.windUpperBound.degrees()) { - this.windDirection.setBearing(MockRace.windUpperBound); - - } else if (this.windDirection.degrees() < MockRace.windLowerBound.degrees()) { - this.windDirection.setBearing(MockRace.windLowerBound); - - } + this.wind.changeWindDirection(); } diff --git a/racevisionGame/src/main/java/mock/model/RaceState.java b/racevisionGame/src/main/java/mock/model/RaceState.java index 9e01deda..4b13cbb4 100644 --- a/racevisionGame/src/main/java/mock/model/RaceState.java +++ b/racevisionGame/src/main/java/mock/model/RaceState.java @@ -1,18 +1,4 @@ package mock.model; -import javafx.beans.property.IntegerProperty; -import javafx.beans.property.SimpleIntegerProperty; -import network.Messages.Enums.RaceStatusEnum; -import network.Messages.Enums.RaceTypeEnum; -import network.Messages.LatestMessages; -import shared.dataInput.BoatDataSource; -import shared.dataInput.RaceDataSource; -import shared.dataInput.RegattaDataSource; -import shared.model.*; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - public class RaceState { } diff --git a/racevisionGame/src/main/java/mock/model/Wind.java b/racevisionGame/src/main/java/mock/model/Wind.java new file mode 100644 index 00000000..199cb98b --- /dev/null +++ b/racevisionGame/src/main/java/mock/model/Wind.java @@ -0,0 +1,92 @@ +package mock.model; + +import shared.model.Bearing; + +import java.util.Random; + +public class Wind { + + /** + * Used to generate random numbers when changing the wind direction. + */ + private int changeWind = 4; + + /** + * The bearing the wind direction starts at. + */ + private Bearing windBearing; + + /** + * The lower bearing angle that the wind may have. + */ + private Bearing windLowerBound; + + /** + * The upper bearing angle that the wind may have. + */ + private Bearing windUpperBound; + + double windSpeed; + + public Wind() { + this.windBearing = Bearing.fromDegrees(225); + this.windSpeed = 12; + this.windLowerBound = Bearing.fromDegrees(215); + this.windUpperBound = Bearing.fromDegrees(235); + } + + public Wind(Bearing windBearing, double windSpeed, Bearing windLowerBound, Bearing windUpperBound) { + this.windBearing = windBearing; + this.windSpeed = windSpeed; + this.windLowerBound = windLowerBound; + this.windUpperBound = windUpperBound; + } + + /** + * Changes the wind direction randomly, while keeping it within [windLowerBound, windUpperBound]. + */ + public void changeWindDirection() { + + //Randomly add or remove 0.5 degrees. + int r = new Random().nextInt(changeWind) + 1; + + if (r == 1) { + //Add 0.5 degrees to the wind bearing. + this.windBearing.setDegrees(this.windBearing.degrees() + 0.5); + + } else if (r == 2) { + //Minus 0.5 degrees from the wind bearing. + this.windBearing.setDegrees(this.windBearing.degrees() - 0.5); + + } + + //Ensure that the wind is in the correct bounds. + if (this.windBearing.degrees() > this.windUpperBound.degrees()) { + this.windBearing.setBearing(this.windUpperBound); + + } else if (this.windBearing.degrees() < this.windLowerBound.degrees()) { + this.windBearing.setBearing(this.windLowerBound); + + } + } + + public Bearing getWindDirection() { + return this.windBearing; + } + + public double getWindSpeed() { + return this.windSpeed; + } + + public void setWindDirection(Bearing windBearing) { + this.windBearing = windBearing; + } + + public void setWindSpeed(double windSpeed) { + this.windSpeed = windSpeed; + } + + public void setDegrees(double degrees) { + this.windBearing.setDegrees(degrees); + } +} diff --git a/racevisionGame/src/main/java/shared/model/Race.java b/racevisionGame/src/main/java/shared/model/Race.java index 415e9f77..96bdf82a 100644 --- a/racevisionGame/src/main/java/shared/model/Race.java +++ b/racevisionGame/src/main/java/shared/model/Race.java @@ -2,6 +2,7 @@ package shared.model; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; +import mock.model.Wind; import network.Messages.Enums.RaceStatusEnum; import network.Messages.Enums.RaceTypeEnum; import network.Messages.LatestMessages; @@ -100,13 +101,7 @@ public abstract class Race implements Runnable { /** * The current wind direction bearing. */ - protected Bearing windDirection; - - /** - * Wind speed (knots). - * Convert this to millimeters per second before passing to RaceStatus. - */ - protected double windSpeed; + protected Wind wind; /** @@ -169,12 +164,7 @@ public abstract class Race implements Runnable { //Race type. this.raceType = raceDataSource.getRaceType(); - //Wind speed. - this.windSpeed = 0; - //Wind direction. - this.windDirection = Bearing.fromDegrees(0); - - + this.wind = new Wind(); } @@ -259,7 +249,7 @@ public abstract class Race implements Runnable { * @return The wind bearing. */ public Bearing getWindDirection() { - return windDirection; + return wind.getWindDirection(); } /** @@ -268,7 +258,7 @@ public abstract class Race implements Runnable { * @return The wind speed. */ public double getWindSpeed() { - return windSpeed; + return wind.getWindSpeed(); } /** diff --git a/racevisionGame/src/main/java/visualiser/model/VisualiserRace.java b/racevisionGame/src/main/java/visualiser/model/VisualiserRace.java index 3a76631d..a98a8345 100644 --- a/racevisionGame/src/main/java/visualiser/model/VisualiserRace.java +++ b/racevisionGame/src/main/java/visualiser/model/VisualiserRace.java @@ -311,11 +311,11 @@ public class VisualiserRace extends Race { //Race status enum. this.raceStatusEnum = RaceStatusEnum.fromByte(raceStatus.getRaceStatus()); - //Wind bearing. - this.windDirection.setDegrees(raceStatus.getScaledWindDirection()); + // Wind direction + wind.setDegrees(raceStatus.getScaledWindDirection()); - //Wind speed. - this.windSpeed = raceStatus.getWindSpeedKnots(); + // Wind speed + wind.setWindSpeed(raceStatus.getWindSpeedKnots()); //Current race time. this.raceClock.setUTCTime(raceStatus.getCurrentTime());