diff --git a/src/main/java/seng302/gameServer/GameState.java b/src/main/java/seng302/gameServer/GameState.java index 0b1debb4..20aaaf4f 100644 --- a/src/main/java/seng302/gameServer/GameState.java +++ b/src/main/java/seng302/gameServer/GameState.java @@ -66,6 +66,14 @@ public class GameState { GameState.currentStage = currentStage; } + public static Double getWindDirection() { + return windDirection; + } + + public static Double getWindSpeed() { + return windSpeed; + } + public static void updateBoat(Integer sourceId, BoatActionType actionType) { switch (actionType) { case VMG: diff --git a/src/main/java/seng302/models/PolarTable.java b/src/main/java/seng302/models/PolarTable.java index b40b54dd..83df7711 100644 --- a/src/main/java/seng302/models/PolarTable.java +++ b/src/main/java/seng302/models/PolarTable.java @@ -1,6 +1,9 @@ package seng302.models; -import java.io.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; @@ -140,7 +143,7 @@ public final class PolarTable { } - private static Double getClosestMatch(Double thisWindSpeed) { + public static Double getClosestMatch(Double thisWindSpeed) { ArrayList windValues = new ArrayList<>(polarTable.keySet()); diff --git a/src/main/java/seng302/models/Yacht.java b/src/main/java/seng302/models/Yacht.java index c6c88944..7e45549d 100644 --- a/src/main/java/seng302/models/Yacht.java +++ b/src/main/java/seng302/models/Yacht.java @@ -2,12 +2,12 @@ package seng302.models; import static seng302.utilities.GeoUtility.getGeoCoordinate; -import javafx.scene.paint.Color; -import seng302.models.mark.Mark; -import seng302.controllers.RaceViewController; - import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Map; +import javafx.scene.paint.Color; +import seng302.controllers.RaceViewController; +import seng302.models.mark.Mark; import seng302.utilities.GeoPoint; /** @@ -96,6 +96,32 @@ public class Yacht { location = getGeoCoordinate(location, heading, metersCovered); } + /** + * Adjusts the yachts velocity based on the wind direction and speed from the polar table. + * + * @param windDir current wind Direction TODO: 20/07/17 ajm412: (TWA or AWA, not 100% sure?) + * @param windSpd current wind Speed + */ + public void updateYachtVelocity(Double windDir, Double windSpd) { + Double closestSpd = PolarTable.getClosestMatch(windSpd); + Map polarsFromClosestSpd = PolarTable.getPolarTable().get(closestSpd); + + Double closest = 0d; + Double closest_key = 0d; + + for (Double key : polarsFromClosestSpd.keySet()) { + Double difference = Math.abs(key - windDir); + if (difference <= closest) { + closest = difference; + closest_key = key; + } + } +// System.out.println("Closest angle " + closest_key); +// System.out.println("WindDir " + windDir); + velocity = polarsFromClosestSpd.get(closest_key); + } + + public String getBoatType() { return boatType; } diff --git a/src/test/java/seng302/models/YachtTest.java b/src/test/java/seng302/models/YachtTest.java new file mode 100644 index 00000000..257e5f59 --- /dev/null +++ b/src/test/java/seng302/models/YachtTest.java @@ -0,0 +1,36 @@ +package seng302.models; + + +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import seng302.utilities.GeoPoint; + +public class YachtTest { + + Double windDir; + Double windSpd; + List yachts = new ArrayList(); + + @Before + public void setUp() { + PolarTable.parsePolarFile(getClass().getResourceAsStream("/config/acc_polars.csv")); + windDir = 90d; + windSpd = 10d; + + yachts.add(new Yacht("Yacht 1", "Y1", new GeoPoint(-30.0, 20.0), 160.0)); + yachts.add(new Yacht("Yacht 2", "Y2", new GeoPoint(-40.0, -20.0), 100.0)); + yachts.add(new Yacht("Yacht 3", "Y3", new GeoPoint(-35.0, -15.5), 20.0)); + } + + @Test + public void testVelocityUpdate() { + for (Yacht yacht : yachts) { + yacht.updateYachtVelocity(windDir, windSpd); + System.out.println(yacht.getVelocity()); + // TODO: 20/07/17 ajm412: add assertions. + } + } + +}