From c8a96dcce9844c6d194f32a1f598637606ca99e1 Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Tue, 25 Jul 2017 20:14:50 +1200 Subject: [PATCH] Fixed XML Loading error, used VMG to calculate boat velocity - XML read from stream instead of file - Started implementing VMG to calculate boat velocity dynamically Tags: #pair[wmu16, mra106] #story[986] --- .../seng302/client/ClientPacketParser.java | 2 +- .../seng302/gameServer/MainServerThread.java | 2 +- src/main/java/seng302/models/PolarTable.java | 46 +++++++++++++------ src/main/java/seng302/models/Yacht.java | 34 ++++---------- .../java/seng302/models/xml/XMLGenerator.java | 7 +-- src/main/resources/views/MainView.fxml | 2 +- src/test/java/seng302/models/YachtTest.java | 9 ---- 7 files changed, 45 insertions(+), 57 deletions(-) diff --git a/src/main/java/seng302/client/ClientPacketParser.java b/src/main/java/seng302/client/ClientPacketParser.java index 4f36e9ea..4198dc55 100644 --- a/src/main/java/seng302/client/ClientPacketParser.java +++ b/src/main/java/seng302/client/ClientPacketParser.java @@ -52,7 +52,7 @@ public class ClientPacketParser { private static Map clientStateBoats = new ConcurrentHashMap<>(); //CONVERSION CONSTANTS - private static final Double MS_TO_KNOTS = 1.94384; + public static final Double MS_TO_KNOTS = 1.94384; /** * Used to initialise the thread name and stream parser object so a thread can be executed diff --git a/src/main/java/seng302/gameServer/MainServerThread.java b/src/main/java/seng302/gameServer/MainServerThread.java index ca3f07ae..d2cc3473 100644 --- a/src/main/java/seng302/gameServer/MainServerThread.java +++ b/src/main/java/seng302/gameServer/MainServerThread.java @@ -21,7 +21,7 @@ public class MainServerThread extends Observable implements Runnable, PacketBuff private static final int PORT = 4942; private static final Integer MAX_NUM_PLAYERS = 3; - private static final Integer UPDATES_PER_SECOND = 5; + private static final Integer UPDATES_PER_SECOND = 2; private static final int LOG_LEVEL = 1; private Thread thread; diff --git a/src/main/java/seng302/models/PolarTable.java b/src/main/java/seng302/models/PolarTable.java index 83df7711..997b0356 100644 --- a/src/main/java/seng302/models/PolarTable.java +++ b/src/main/java/seng302/models/PolarTable.java @@ -4,6 +4,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; @@ -126,7 +127,7 @@ public final class PolarTable { */ public static HashMap getOptimalUpwindVMG(Double thisWindSpeed) { - Double polarWindSpeed = getClosestMatch(thisWindSpeed); + Double polarWindSpeed = getClosestWindSpeedInPolar(thisWindSpeed); return upwindOptimal.get(polarWindSpeed); } @@ -138,30 +139,47 @@ public final class PolarTable { */ public static HashMap getOptimalDownwindVMG(Double thisWindSpeed) { - Double polarWindSpeed = getClosestMatch(thisWindSpeed); + Double polarWindSpeed = getClosestWindSpeedInPolar(thisWindSpeed); return downwindOptimal.get(polarWindSpeed); } - public static Double getClosestMatch(Double thisWindSpeed) { + public static Double getBoatSpeed(Double thisWindSpeed, Double thisHeading) { - ArrayList windValues = new ArrayList<>(polarTable.keySet()); + Double polarWindSpeed = getClosestWindSpeedInPolar(thisWindSpeed); + Double polarAngle = getClosestAngleInPolar(polarTable.get(polarWindSpeed), thisHeading); - Double lowerVal = windValues.get(0); - Double upperVal = windValues.get(1); + return polarTable.get(polarWindSpeed).get(polarAngle); + } - for(int i = 0; i < windValues.size() - 1; i++) { - lowerVal = windValues.get(i); - upperVal = windValues.get(i+1); - if (thisWindSpeed <= upperVal) { - break; + + public static Double getClosestWindSpeedInPolar(Double thisWindSpeed) { + Double smallestDif = Double.POSITIVE_INFINITY; + Double closestWind = 0d; + + for (Double polarWindSpeed : polarTable.keySet()) { + Double difference = Math.abs(polarWindSpeed - thisWindSpeed); + if (difference < smallestDif) { + smallestDif = difference; + closestWind = polarWindSpeed; } } + return closestWind; + } - Double lowerDiff = Math.abs(lowerVal - thisWindSpeed); - Double upperDiff = Math.abs(upperVal - thisWindSpeed); - return (lowerDiff <= upperDiff) ? lowerVal : upperVal; + public static Double getClosestAngleInPolar(HashMap thisWindSpeedPolar, Double thisHeading) { + Double smallestDif = Double.POSITIVE_INFINITY; + Double closestAngle = 0d; + + for (Double polarAngle : thisWindSpeedPolar.keySet()) { + Double difference = Math.abs(polarAngle - thisHeading); + if (difference < smallestDif) { + smallestDif = difference; + closestAngle = polarAngle; + } + } + return closestAngle; } } \ No newline at end of file diff --git a/src/main/java/seng302/models/Yacht.java b/src/main/java/seng302/models/Yacht.java index 8072d5d0..443c9e55 100644 --- a/src/main/java/seng302/models/Yacht.java +++ b/src/main/java/seng302/models/Yacht.java @@ -4,8 +4,9 @@ import static seng302.utilities.GeoUtility.getGeoCoordinate; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.Map; + import javafx.scene.paint.Color; +import seng302.client.ClientPacketParser; import seng302.controllers.RaceViewController; import seng302.gameServer.GameState; import seng302.models.mark.Mark; @@ -19,7 +20,7 @@ import seng302.utilities.GeoPoint; */ public class Yacht { - private final Double TURN_STEP = 2.0; + private final Double TURN_STEP = 5.0; private Double lastHeading; private Boolean sailIn; @@ -119,35 +120,16 @@ public class Yacht { public void update(Long timeInterval) { if (sailIn) { Double secondsElapsed = timeInterval / 1000000.0; + Double thisHeading = ((double) Math.floorMod(heading.longValue(), 360L)); + Double windSpeedKnots = 0d; + Double boatSpeedInKnots = PolarTable.getBoatSpeed(windSpeedKnots, thisHeading); + velocity = boatSpeedInKnots / ClientPacketParser.MS_TO_KNOTS * 3000; + //System.out.println("velocity = " + velocity); Double metersCovered = velocity * secondsElapsed; 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 Double getHeading() { return heading; diff --git a/src/main/java/seng302/models/xml/XMLGenerator.java b/src/main/java/seng302/models/xml/XMLGenerator.java index d74dfb31..04a5b5fb 100644 --- a/src/main/java/seng302/models/xml/XMLGenerator.java +++ b/src/main/java/seng302/models/xml/XMLGenerator.java @@ -3,6 +3,7 @@ package seng302.models.xml; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; +import org.apache.commons.io.IOUtils; import seng302.server.messages.XMLMessageSubType; import java.io.*; @@ -27,11 +28,7 @@ public class XMLGenerator { configuration = new Configuration(Configuration.VERSION_2_3_26); try { - configuration.setDirectoryForTemplateLoading(new File(getClass().getResource(XML_TEMPLATE_DIR).toURI())); - } catch (IOException e){ - System.out.println("[FATAL] Server could not read XML templates"); - } catch (URISyntaxException e) { - System.out.println("[FATAL] Xml template directory URI is invalid"); + configuration.setClassForTemplateLoading(getClass(), XML_TEMPLATE_DIR); } catch (NullPointerException e){ System.out.println("[FATAL] Server could not load XML Template directory, ensure this directory isn't empty"); } diff --git a/src/main/resources/views/MainView.fxml b/src/main/resources/views/MainView.fxml index 84348137..87cc7443 100644 --- a/src/main/resources/views/MainView.fxml +++ b/src/main/resources/views/MainView.fxml @@ -8,4 +8,4 @@ - + diff --git a/src/test/java/seng302/models/YachtTest.java b/src/test/java/seng302/models/YachtTest.java index 257e5f59..ab467522 100644 --- a/src/test/java/seng302/models/YachtTest.java +++ b/src/test/java/seng302/models/YachtTest.java @@ -24,13 +24,4 @@ public class YachtTest { 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. - } - } - }