mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Merge remote-tracking branch 'origin/develop' into Story66_Collision
# Conflicts: # src/main/java/seng302/gameServer/GameState.java # src/main/java/seng302/model/Yacht.java # src/main/java/seng302/model/mark/MarkOrder.java # src/main/java/seng302/visualiser/GameClient.java
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package seng302.gameServer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import java.util.*;
|
||||
|
||||
import javafx.application.Platform;
|
||||
@@ -9,10 +11,9 @@ import seng302.model.GeoPoint;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.model.mark.MarkOrder;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.model.mark.Mark;
|
||||
import seng302.model.mark.MarkOrder;
|
||||
import seng302.utilities.GeoUtility;
|
||||
|
||||
/**
|
||||
@@ -21,6 +22,8 @@ import seng302.utilities.GeoUtility;
|
||||
*/
|
||||
public class GameState implements Runnable {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(MarkOrder.class);
|
||||
|
||||
private static Integer STATE_UPDATES_PER_SECOND = 60;
|
||||
|
||||
private static Long previousUpdateTime;
|
||||
@@ -32,6 +35,7 @@ public class GameState implements Runnable {
|
||||
private static Map<Integer, Yacht> yachts;
|
||||
private static Boolean isRaceStarted;
|
||||
private static GameStages currentStage;
|
||||
private static MarkOrder markOrder;
|
||||
private static long startTime;
|
||||
private static Set<Mark> marks;
|
||||
|
||||
@@ -61,8 +65,9 @@ public class GameState implements Runnable {
|
||||
//set this when game stage changes to prerace
|
||||
previousUpdateTime = System.currentTimeMillis();
|
||||
yachts = new HashMap<>();
|
||||
markOrder = new MarkOrder(); //This could be instantiated at some point with a select map?
|
||||
|
||||
new Thread(this).start();
|
||||
new Thread(this).start(); //Run the auto updates on the game state
|
||||
|
||||
marks = new MarkOrder().getAllMarks();
|
||||
}
|
||||
@@ -114,7 +119,11 @@ public class GameState implements Runnable {
|
||||
GameState.currentStage = currentStage;
|
||||
}
|
||||
|
||||
public static long getStartTime() {
|
||||
public static MarkOrder getMarkOrder() {
|
||||
return markOrder;
|
||||
}
|
||||
|
||||
public static long getStartTime(){
|
||||
return startTime;
|
||||
}
|
||||
|
||||
|
||||
@@ -79,18 +79,19 @@ public class CourseParser extends FileParser {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element e = (Element) node;
|
||||
Integer markID = Integer.valueOf(e.getAttribute("CompoundMarkID"));
|
||||
|
||||
String name = e.getAttribute("Name");
|
||||
CompoundMark cMark = new CompoundMark(markID, name);
|
||||
|
||||
NodeList marks = e.getElementsByTagName("Mark");
|
||||
for (int i = 0; i < marks.getLength(); i++) {
|
||||
List<Mark> subMarks = new ArrayList<>();
|
||||
for (int i = 0; i < marks.getLength(); i++) {
|
||||
Mark mark = getMark(marks.item(i));
|
||||
if (mark != null)
|
||||
cMark.addSubMarks(mark);
|
||||
if (mark != null) {
|
||||
subMarks.add(mark);
|
||||
}
|
||||
}
|
||||
return cMark;
|
||||
}
|
||||
|
||||
return new CompoundMark(markID, name, subMarks);
|
||||
}
|
||||
System.out.println("Failed to create compound mark.");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import javafx.beans.property.ReadOnlyDoubleWrapper;
|
||||
import javafx.beans.property.ReadOnlyLongProperty;
|
||||
import javafx.beans.property.ReadOnlyLongWrapper;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import seng302.gameServer.GameState;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.model.mark.Mark;
|
||||
@@ -33,12 +35,16 @@ public class Yacht extends Observable {
|
||||
void notifyLocation(Yacht yacht, double lat, double lon, double heading, double velocity);
|
||||
}
|
||||
|
||||
private static final Double ROUNDING_DISTANCE = 15d; // TODO: 3/08/17 wmu16 - Look into this value further
|
||||
private Logger logger = LoggerFactory.getLogger(Yacht.class);
|
||||
|
||||
private static final Double ROUNDING_DISTANCE = 50d; // TODO: 3/08/17 wmu16 - Look into this value further
|
||||
public static final Double MARK_COLLISION_DISTANCE = 15d;
|
||||
public static final Double YACHT_COLLISION_DISTANCE = 25.0;
|
||||
private static final Double BOUNCE_DISTANCE = 20.0;
|
||||
private static final Integer COLLISION_UPDATE_INTERVAL = 100;
|
||||
|
||||
|
||||
|
||||
//BOTH AFAIK
|
||||
private String boatType;
|
||||
private Integer sourceId;
|
||||
@@ -48,11 +54,10 @@ public class Yacht extends Observable {
|
||||
private String country;
|
||||
|
||||
private Long estimateTimeAtFinish;
|
||||
private Long lastMark;
|
||||
private Integer currentMarkSeqID = 0;
|
||||
private Long markRoundTime;
|
||||
private Double distanceToNextMark;
|
||||
private Double distanceToCurrentMark;
|
||||
private Long timeTillNext;
|
||||
private CompoundMark nextMark;
|
||||
private Double heading;
|
||||
private Integer legNumber = 0;
|
||||
|
||||
@@ -63,11 +68,13 @@ public class Yacht extends Observable {
|
||||
private GeoPoint location;
|
||||
private Integer boatStatus;
|
||||
private Double velocity;
|
||||
|
||||
//MARK ROUNDING INFO
|
||||
private GeoPoint lastLocation; //For purposes of mark rounding calculations
|
||||
private Boolean hasEnteredRoundingZone; //The distance that the boat must be from the mark to round
|
||||
private Boolean hasPassedFirstLine; //The line extrapolated from the next mark to the current mark
|
||||
private Boolean hasPassedSecondLine; //The line extrapolated from the last mark to the current mark
|
||||
private Boolean hasPassedLine;
|
||||
private Boolean hasPassedThroughGate;
|
||||
private Boolean finishedRace;
|
||||
private Long lastCollisionUpdate;
|
||||
|
||||
//CLIENT SIDE
|
||||
@@ -94,8 +101,9 @@ public class Yacht extends Observable {
|
||||
this.velocity = 0d; //in mms-1
|
||||
|
||||
this.hasEnteredRoundingZone = false;
|
||||
this.hasPassedFirstLine = false;
|
||||
this.hasPassedSecondLine = false;
|
||||
this.hasPassedLine = false;
|
||||
this.hasPassedThroughGate = false;
|
||||
this.finishedRace = false;
|
||||
}
|
||||
|
||||
public Mark markCollidedWith(){
|
||||
@@ -169,9 +177,8 @@ public class Yacht extends Observable {
|
||||
}
|
||||
|
||||
//CHECK FOR MARK ROUNDING
|
||||
distanceToNextMark = calcDistanceToNextMark();
|
||||
if (distanceToNextMark < ROUNDING_DISTANCE) {
|
||||
hasEnteredRoundingZone = true;
|
||||
if (!finishedRace) {
|
||||
checkForLegProgression();
|
||||
}
|
||||
|
||||
// TODO: 3/08/17 wmu16 - Implement line cross check here
|
||||
@@ -204,14 +211,16 @@ public class Yacht extends Observable {
|
||||
|
||||
|
||||
/**
|
||||
* Calculates the distance to the next mark (closest of the two if a gate mark).
|
||||
*
|
||||
* Calculates the distance to the next mark (closest of the two if a gate mark). For purposes
|
||||
* of mark rounding
|
||||
* @return A distance in metres. Returns -1 if there is no next mark
|
||||
* @throws IndexOutOfBoundsException If the next mark is null (ie the last mark in the race)
|
||||
* Check first using {@link seng302.model.mark.MarkOrder#isLastMark(Integer)}
|
||||
*/
|
||||
public Double calcDistanceToNextMark() {
|
||||
if (nextMark == null) {
|
||||
return -1d;
|
||||
} else if (nextMark.isGate()) {
|
||||
public Double calcDistanceToCurrentMark() throws IndexOutOfBoundsException {
|
||||
CompoundMark nextMark = GameState.getMarkOrder().getCurrentMark(currentMarkSeqID);
|
||||
|
||||
if (nextMark.isGate()) {
|
||||
Mark sub1 = nextMark.getSubMark(1);
|
||||
Mark sub2 = nextMark.getSubMark(2);
|
||||
Double distance1 = GeoUtility.getDistance(location, sub1);
|
||||
@@ -222,6 +231,144 @@ public class Yacht extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 4 Different cases of progression in the race
|
||||
* 1 - Passing the start line
|
||||
* 2 - Passing any in-race Gate
|
||||
* 3 - Passing any in-race Mark
|
||||
* 4 - Passing the finish line
|
||||
*/
|
||||
private void checkForLegProgression() {
|
||||
CompoundMark currentMark = GameState.getMarkOrder().getCurrentMark(currentMarkSeqID);
|
||||
if (currentMarkSeqID == 0) {
|
||||
checkStartLineCrossing(currentMark);
|
||||
} else if (GameState.getMarkOrder().isLastMark(currentMarkSeqID)) {
|
||||
checkFinishLineCrossing(currentMark);
|
||||
} else if (currentMark.isGate()) {
|
||||
checkGateRounding(currentMark);
|
||||
} else {
|
||||
checkMarkRounding(currentMark);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If we pass the start line gate in the correct direction, progress
|
||||
*
|
||||
* @param currentMark The current gate
|
||||
*/
|
||||
private void checkStartLineCrossing(CompoundMark currentMark) {
|
||||
Mark mark1 = currentMark.getSubMark(1);
|
||||
Mark mark2 = currentMark.getSubMark(2);
|
||||
CompoundMark nextMark = GameState.getMarkOrder().getNextMark(currentMarkSeqID);
|
||||
|
||||
Integer crossedLine = GeoUtility.checkCrossedLine(mark1, mark2, lastLocation, location);
|
||||
if (crossedLine > 0) {
|
||||
Boolean isClockwiseCross = GeoUtility.isClockwise(mark1, mark2, nextMark.getMidPoint());
|
||||
if (crossedLine == 2 && isClockwiseCross || crossedLine == 1 && !isClockwiseCross) {
|
||||
currentMarkSeqID++;
|
||||
logMarkRounding(currentMark);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This algorithm checks for mark rounding. And increments the currentMarSeqID number attribute
|
||||
* of the yacht if so.
|
||||
* A visual representation of this algorithm can be seen on the Wiki under
|
||||
* 'mark passing algorithm'
|
||||
*/
|
||||
private void checkMarkRounding(CompoundMark currentMark) {
|
||||
distanceToCurrentMark = calcDistanceToCurrentMark();
|
||||
GeoPoint nextPoint = GameState.getMarkOrder().getNextMark(currentMarkSeqID).getMidPoint();
|
||||
GeoPoint prevPoint = GameState.getMarkOrder().getPreviousMark(currentMarkSeqID)
|
||||
.getMidPoint();
|
||||
GeoPoint midPoint = GeoUtility.getDirtyMidPoint(nextPoint, prevPoint);
|
||||
|
||||
//1 TEST FOR ENTERING THE ROUNDING DISTANCE
|
||||
if (distanceToCurrentMark < ROUNDING_DISTANCE) {
|
||||
hasEnteredRoundingZone = true;
|
||||
}
|
||||
|
||||
//In case current mark is a gate, loop through all marks just in case
|
||||
for (Mark thisCurrentMark : currentMark.getMarks()) {
|
||||
if (GeoUtility.isPointInTriangle(lastLocation, location, midPoint, thisCurrentMark)) {
|
||||
hasPassedLine = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasPassedLine && hasEnteredRoundingZone) {
|
||||
currentMarkSeqID++;
|
||||
hasPassedLine = false;
|
||||
hasEnteredRoundingZone = false;
|
||||
hasPassedThroughGate = false;
|
||||
logMarkRounding(currentMark);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if a gate line has been crossed and in the correct direction
|
||||
*
|
||||
* @param currentMark The current gate
|
||||
*/
|
||||
private void checkGateRounding(CompoundMark currentMark) {
|
||||
Mark mark1 = currentMark.getSubMark(1);
|
||||
Mark mark2 = currentMark.getSubMark(2);
|
||||
CompoundMark prevMark = GameState.getMarkOrder().getPreviousMark(currentMarkSeqID);
|
||||
CompoundMark nextMark = GameState.getMarkOrder().getNextMark(currentMarkSeqID);
|
||||
|
||||
Integer crossedLine = GeoUtility.checkCrossedLine(mark1, mark2, lastLocation, location);
|
||||
|
||||
//We have crossed the line
|
||||
if (crossedLine > 0) {
|
||||
Boolean isClockwiseCross = GeoUtility.isClockwise(mark1, mark2, prevMark.getMidPoint());
|
||||
|
||||
//Check we cross the line in the correct direction
|
||||
if (crossedLine == 1 && isClockwiseCross || crossedLine == 2 && !isClockwiseCross) {
|
||||
hasPassedThroughGate = true;
|
||||
}
|
||||
}
|
||||
|
||||
Boolean prevMarkSide = GeoUtility.isClockwise(mark1, mark2, prevMark.getMidPoint());
|
||||
Boolean nextMarkSide = GeoUtility.isClockwise(mark1, mark2, nextMark.getMidPoint());
|
||||
|
||||
if (hasPassedThroughGate) {
|
||||
//Check if we need to round this gate after passing through
|
||||
if (prevMarkSide == nextMarkSide) {
|
||||
checkMarkRounding(currentMark);
|
||||
} else {
|
||||
currentMarkSeqID++;
|
||||
logMarkRounding(currentMark);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If we pass the finish gate in the correct direction
|
||||
*
|
||||
* @param currentMark The current gate
|
||||
*/
|
||||
private void checkFinishLineCrossing(CompoundMark currentMark) {
|
||||
Mark mark1 = currentMark.getSubMark(1);
|
||||
Mark mark2 = currentMark.getSubMark(2);
|
||||
CompoundMark prevMark = GameState.getMarkOrder().getPreviousMark(currentMarkSeqID);
|
||||
|
||||
Integer crossedLine = GeoUtility.checkCrossedLine(mark1, mark2, lastLocation, location);
|
||||
if (crossedLine > 0) {
|
||||
Boolean isClockwiseCross = GeoUtility.isClockwise(mark1, mark2, prevMark.getMidPoint());
|
||||
if (crossedLine == 1 && isClockwiseCross || crossedLine == 2 && !isClockwiseCross) {
|
||||
currentMarkSeqID++;
|
||||
finishedRace = true;
|
||||
logMarkRounding(currentMark);
|
||||
logger.debug(sourceId + " finished");
|
||||
// TODO: 8/08/17 wmu16 - Do something!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void adjustHeading(Double amount) {
|
||||
Double newVal = heading + amount;
|
||||
lastHeading = heading;
|
||||
@@ -300,7 +447,6 @@ public class Yacht extends Observable {
|
||||
}
|
||||
|
||||
private void turnTowardsHeading(Double newHeading) {
|
||||
System.out.println(newHeading);
|
||||
if (heading < 90 && newHeading > 270) {
|
||||
adjustHeading(-TURN_STEP);
|
||||
} else {
|
||||
@@ -432,14 +578,6 @@ public class Yacht extends Observable {
|
||||
this.lastMarkRounded = lastMarkRounded;
|
||||
}
|
||||
|
||||
public void setNextMark(CompoundMark nextMark) {
|
||||
this.nextMark = nextMark;
|
||||
}
|
||||
|
||||
public CompoundMark getNextMark() {
|
||||
return nextMark;
|
||||
}
|
||||
|
||||
public GeoPoint getLocation() {
|
||||
return location;
|
||||
}
|
||||
@@ -504,8 +642,8 @@ public class Yacht extends Observable {
|
||||
this.velocity = velocity;
|
||||
}
|
||||
|
||||
public Double getDistanceToNextMark() {
|
||||
return distanceToNextMark;
|
||||
public Double getDistanceToCurrentMark() {
|
||||
return distanceToCurrentMark;
|
||||
}
|
||||
|
||||
public void updateLocation(double lat, double lng, double heading, double velocity) {
|
||||
@@ -518,6 +656,20 @@ public class Yacht extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
private void logMarkRounding(CompoundMark currentMark) {
|
||||
String typeString = "mark";
|
||||
if (currentMark.isGate()) {
|
||||
typeString = "gate";
|
||||
}
|
||||
logger.debug(
|
||||
String.format("BoatID %d passed %s %s with id %d. Now on leg %d",
|
||||
sourceId,
|
||||
typeString,
|
||||
currentMark.getMarks().get(0).getName(),
|
||||
currentMark.getId(),
|
||||
currentMarkSeqID));
|
||||
}
|
||||
|
||||
public void addLocationListener(YachtLocationListener listener) {
|
||||
locationListeners.add(listener);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package seng302.model.mark;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import seng302.model.GeoPoint;
|
||||
import seng302.utilities.GeoUtility;
|
||||
|
||||
public class CompoundMark {
|
||||
|
||||
@@ -10,18 +11,17 @@ public class CompoundMark {
|
||||
private String name;
|
||||
|
||||
private List<Mark> marks = new ArrayList<>();
|
||||
private GeoPoint midPoint;
|
||||
|
||||
public CompoundMark(int markID, String name) {
|
||||
this.compoundMarkId = markID;
|
||||
public CompoundMark(int markID, String name, List<Mark> marks) {
|
||||
this.compoundMarkId = markID;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void addSubMarks(Mark... marks) {
|
||||
this.marks.addAll(Arrays.asList(marks));
|
||||
}
|
||||
|
||||
public void addSubMarks(List<Mark> marks) {
|
||||
this.marks.addAll(marks);
|
||||
this.marks.addAll(marks);
|
||||
if (marks.size() > 1) {
|
||||
this.midPoint = GeoUtility.getDirtyMidPoint(marks.get(0), marks.get(1));
|
||||
} else {
|
||||
this.midPoint = marks.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,6 +68,16 @@ public class CompoundMark {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: This is a 'dirty' mid point as it is simply calculated as an xy point would be.
|
||||
* NO CHECKING FOR LAT / LNG WRAPPING IS DONE IN CREATION OF THIS MIDPOINT
|
||||
*
|
||||
* @return GeoPoint of the midpoint of the two marks, or the one mark if there is only one
|
||||
*/
|
||||
public GeoPoint getMidPoint() {
|
||||
return midPoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not this CompoundMark is a Gate. It is generally cleaner to program to a
|
||||
* specific singleMark or the list of marks.
|
||||
@@ -87,38 +97,6 @@ public class CompoundMark {
|
||||
return marks;
|
||||
}
|
||||
|
||||
|
||||
// @Override
|
||||
// public boolean equals(Object other) {
|
||||
// if (other == null) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (!(other instanceof Mark)){
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// Mark otherMark = (Mark) other;
|
||||
//
|
||||
// if (otherMark.getLat() != getLat() || otherMark.getLongitude() != getLongitude()) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (otherMark.getCompoundMarkID() != getCompoundMarkID()){
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (otherMark.getId() != getId()){
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (!otherMark.getName().equals(name)){
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 0;
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.*;
|
||||
* Class to hold the order of the marks in the race.
|
||||
*/
|
||||
public class MarkOrder {
|
||||
private List<Mark> raceMarkOrder;
|
||||
private List<CompoundMark> raceMarkOrder;
|
||||
private Logger logger = LoggerFactory.getLogger(MarkOrder.class);
|
||||
private Set<Mark> allMarks;
|
||||
|
||||
@@ -33,7 +33,7 @@ public class MarkOrder {
|
||||
* @return An ordered list of marks in the race
|
||||
* OR null if the mark order could not be loaded
|
||||
*/
|
||||
public List<Mark> getMarkOrder(){
|
||||
public List<CompoundMark> getMarkOrder(){
|
||||
if (raceMarkOrder == null){
|
||||
logger.warn("Race order accessed but not instantiated");
|
||||
return null;
|
||||
@@ -43,26 +43,35 @@ public class MarkOrder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mark in the race after the previous mark
|
||||
* @param position The current race position
|
||||
* @return the next race position
|
||||
* OR null if there is no position
|
||||
* @param seqID The seqID of the current mark the boat is heading to
|
||||
* @return A Boolean indicating if this coming mark is the last one (finish line)
|
||||
*/
|
||||
public RacePosition getNextPosition(RacePosition position){
|
||||
Mark previousMark = position.getNextMark();
|
||||
Mark nextMark;
|
||||
public Boolean isLastMark(Integer seqID) {
|
||||
return seqID == raceMarkOrder.size() - 1;
|
||||
}
|
||||
|
||||
if (position.getPositionIndex() + 1 >= raceMarkOrder.size() - 1){
|
||||
RacePosition nextRacePosition = new RacePosition(raceMarkOrder.size() - 1, null, previousMark);
|
||||
nextRacePosition.setFinishingLeg();
|
||||
/**
|
||||
* @param currentSeqID The seqID of the current mark the boat is heading to
|
||||
* @return The mark last passed
|
||||
* @throws IndexOutOfBoundsException if there is no next mark.
|
||||
* Check seqID != 0 first
|
||||
*/
|
||||
public CompoundMark getPreviousMark(Integer currentSeqID) throws IndexOutOfBoundsException{
|
||||
return raceMarkOrder.get(currentSeqID - 1);
|
||||
}
|
||||
|
||||
return nextRacePosition;
|
||||
}
|
||||
public CompoundMark getCurrentMark(Integer currentSeqID) {
|
||||
return raceMarkOrder.get(currentSeqID);
|
||||
}
|
||||
|
||||
Integer nextPositionIndex = position.getPositionIndex() + 1;
|
||||
RacePosition nextRacePosition = new RacePosition(nextPositionIndex, raceMarkOrder.get(nextPositionIndex), previousMark);
|
||||
|
||||
return nextRacePosition;
|
||||
/**
|
||||
* @param currentSeqID The seqID of the current mark the boat is heading to
|
||||
* @return The mark following the mark that the boat is heading to
|
||||
* @throws IndexOutOfBoundsException if there is no next mark.
|
||||
* Check using {@link #isLastMark(Integer)}
|
||||
*/
|
||||
public CompoundMark getNextMark(Integer currentSeqID) throws IndexOutOfBoundsException{
|
||||
return raceMarkOrder.get(currentSeqID + 1);
|
||||
}
|
||||
|
||||
public Set<Mark> getAllMarks(){
|
||||
@@ -74,7 +83,7 @@ public class MarkOrder {
|
||||
* @param xml An AC35 RaceXML
|
||||
* @return An ordered list of marks in the race
|
||||
*/
|
||||
private List<Mark> loadRaceOrderFromXML(String xml){
|
||||
private List<CompoundMark> loadRaceOrderFromXML(String xml){
|
||||
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db;
|
||||
@@ -95,11 +104,11 @@ public class MarkOrder {
|
||||
logger.debug("Loaded RaceXML for mark order");
|
||||
List<Corner> corners = data.getMarkSequence();
|
||||
Map<Integer, CompoundMark> marks = data.getCompoundMarks();
|
||||
List<Mark> course = new ArrayList<>();
|
||||
List<CompoundMark> course = new ArrayList<>();
|
||||
|
||||
for (Corner corner : corners){
|
||||
CompoundMark compoundMark = marks.get(corner.getCompoundMarkID());
|
||||
course.add(compoundMark.getMarks().get(0));
|
||||
course.add(compoundMark);
|
||||
allMarks.addAll(compoundMark.getMarks());
|
||||
}
|
||||
|
||||
@@ -109,17 +118,6 @@ public class MarkOrder {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The first position in the race
|
||||
*/
|
||||
public RacePosition getFirstPosition(){
|
||||
if (raceMarkOrder.size() > 0){
|
||||
return new RacePosition(-1, raceMarkOrder.get(0), null);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the raceXML and mark order
|
||||
*/
|
||||
@@ -136,4 +134,4 @@ public class MarkOrder {
|
||||
}
|
||||
raceMarkOrder = loadRaceOrderFromXML(raceXML);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package seng302.model.mark;
|
||||
|
||||
/**
|
||||
* Represents a boats position between two marks
|
||||
*/
|
||||
public class RacePosition {
|
||||
private Integer positionIndex;
|
||||
private Mark nextMark;
|
||||
private Mark previousMark;
|
||||
private Boolean isFinishingLeg;
|
||||
|
||||
public RacePosition(Integer positionIndex, Mark nextMark, Mark previousMark){
|
||||
this.positionIndex = positionIndex;
|
||||
this.nextMark = nextMark;
|
||||
this.previousMark = previousMark;
|
||||
isFinishingLeg = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The position of the boat (0...number of marks in race - 1)
|
||||
*/
|
||||
public Integer getPositionIndex(){
|
||||
return positionIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The mark the boat is heading to
|
||||
* will return NULL if this is the finishing legg
|
||||
*/
|
||||
public Mark getNextMark(){
|
||||
return nextMark;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The mark the boat is heading away from,
|
||||
* Will return NULL if this is the starting leg
|
||||
*/
|
||||
public Mark getPreviousMark(){
|
||||
return previousMark;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a flag that this is the last leg in the race
|
||||
*/
|
||||
public void setFinishingLeg(){
|
||||
isFinishingLeg = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this is the last leg in the race
|
||||
*/
|
||||
public boolean getIsFinishingLeg() {
|
||||
return isFinishingLeg;
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,19 @@ public class GeoUtility {
|
||||
return (Math.toDegrees(getBearingRad(p1, p2)) + 360.0) % 360.0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* WARNING: this function DOES NOT account for wrapping around on lats / longs etc.
|
||||
* SO BE CAREFUL IN USING THIS FUNCTION
|
||||
*
|
||||
* @param p1 GeoPoint 1
|
||||
* @param p2 GeoPoint 2
|
||||
* @return GeoPoint midPoint
|
||||
*/
|
||||
public static GeoPoint getDirtyMidPoint(GeoPoint p1, GeoPoint p2) {
|
||||
return new GeoPoint((p1.getLat() + p2.getLat()) / 2, (p1.getLng() + p2.getLng()) / 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the angle between to angular co-ordinates on a sphere in radians.
|
||||
*
|
||||
@@ -93,7 +106,6 @@ public class GeoUtility {
|
||||
return new GeoPoint(Math.toDegrees(endLat), Math.toDegrees(endLng));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Performs the line function on two points of a line and a test point to test which side of the
|
||||
* line that point is on. If the return value is return 1, then the point is on one side of the
|
||||
@@ -125,6 +137,31 @@ public class GeoUtility {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the line formed by lastLocation and location doesn't intersect the line segment
|
||||
* formed by mark1 and mark2 See the wiki Mark Rounding algorithm for more info
|
||||
*
|
||||
* @param mark1 One mark of the line
|
||||
* @param mark2 The second mark of the line
|
||||
* @param lastLocation The last location of the point crossing this line
|
||||
* @param location The current location of the point crossing this line
|
||||
* @return 0 if two line segment doesn't intersect, otherwise 1 if they intersect and
|
||||
* lastLocation is on RHS of the line segment (mark1 to mark2) or 2 if lastLocation on LHS of
|
||||
* the line segment (mark1 to mark2)
|
||||
*/
|
||||
public static Integer checkCrossedLine(GeoPoint mark1, GeoPoint mark2, GeoPoint lastLocation,
|
||||
GeoPoint location) {
|
||||
boolean enteredDirection = isClockwise(mark1, mark2, lastLocation);
|
||||
boolean exitedDirection = isClockwise(mark1, mark2, location);
|
||||
if (enteredDirection != exitedDirection) {
|
||||
if (!isPointInTriangle(mark1, lastLocation, location, mark2)
|
||||
&& !isPointInTriangle(mark2, lastLocation, location, mark1)) {
|
||||
|
||||
return enteredDirection ? 1 : 2;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a point and a vector (angle and vector length) Will create a new point, that vector
|
||||
@@ -155,10 +192,24 @@ public class GeoUtility {
|
||||
* @param bearing2 the bearing of v2
|
||||
* @return the difference of bearing from v1 to v2
|
||||
*/
|
||||
private static double getBearingDiff(double bearing1, double bearing2) {
|
||||
private static Double getBearingDiff(double bearing1, double bearing2) {
|
||||
return ((360 - bearing1) + bearing2) % 360;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a geo point ins on the right hand side of the line segment, which
|
||||
* formed by two geo points v1 to v2. (Algorithm: point is clockwise to the
|
||||
* line if the bearing difference is less than 180 deg.)
|
||||
*
|
||||
* @param v1 one end of the line segment
|
||||
* @param v2 another end of the line segment
|
||||
* @param point the point to be tested
|
||||
* @return true if the point is on the RHS of the line
|
||||
*/
|
||||
public static Boolean isClockwise(GeoPoint v1, GeoPoint v2, GeoPoint point) {
|
||||
return getBearingDiff(getBearing(v1, v2), getBearing(v1, point)) < 180;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given three geo points to form a triangle, the method returns true if the fourth point is
|
||||
* inside the triangle
|
||||
@@ -169,15 +220,15 @@ public class GeoUtility {
|
||||
* @param point the point to be tested
|
||||
* @return true if the fourth point is inside the triangle
|
||||
*/
|
||||
public static boolean isPointInTriangle(GeoPoint v1, GeoPoint v2, GeoPoint v3, GeoPoint point) {
|
||||
// true, if diff of bearing from (v1->v2) to (v1->p) is less than 180 deg
|
||||
boolean sideFlag = getBearingDiff(getBearing(v1, v2), getBearing(v1, point)) < 180;
|
||||
public static Boolean isPointInTriangle(GeoPoint v1, GeoPoint v2, GeoPoint v3, GeoPoint point) {
|
||||
// true, if diff of bearing from (v1 to v2) to (v1 to p) is less than 180 deg
|
||||
boolean isCW = isClockwise(v1, v2, point);
|
||||
|
||||
if ((getBearingDiff(getBearing(v2, v3), getBearing(v2, point)) < 180) != sideFlag) {
|
||||
if (isClockwise(v2, v3, point) != isCW) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((getBearingDiff(getBearing(v3, v1), getBearing(v3, point)) < 180) != sideFlag) {
|
||||
if (isClockwise(v3, v1, point) != isCW) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -256,9 +256,9 @@ public class XMLParser {
|
||||
if (cMarkNode.getNodeName().equals("CompoundMark")) {
|
||||
cMark = new CompoundMark(
|
||||
XMLParser.getNodeAttributeInt(cMarkNode, "CompoundMarkID"),
|
||||
XMLParser.getNodeAttributeString(cMarkNode, "Name")
|
||||
XMLParser.getNodeAttributeString(cMarkNode, "Name"),
|
||||
createMarks(cMarkNode)
|
||||
);
|
||||
cMark.addSubMarks(createMarks(cMarkNode));
|
||||
allMarks.add(cMark);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,7 +178,6 @@ public class GameClient {
|
||||
break;
|
||||
|
||||
case BOAT_XML:
|
||||
// System.out.println("GOT SUM BOATS YAY :)");
|
||||
allBoatsMap = XMLParser.parseBoats(
|
||||
StreamParser.extractXmlMessage(packet)
|
||||
);
|
||||
|
||||
@@ -316,7 +316,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
|
||||
updateRaceTime();
|
||||
updateWindDirection();
|
||||
updateOrder();
|
||||
updateSparkLine();
|
||||
// updateSparkLine();
|
||||
}
|
||||
}, 0, 1000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user