Merge branch 'story63' into CommandFactory
# Conflicts: # racevisionGame/src/main/java/mock/model/MockRace.java # racevisionGame/src/main/java/mock/model/RaceLogic.java # racevisionGame/src/main/java/mock/model/RaceState.java # racevisionGame/src/main/java/mock/model/commandFactory/CommandFactory.java # racevisionGame/src/main/java/mock/model/commandFactory/VMGCommand.java # racevisionGame/src/main/java/visualiser/gameController/ControllerServer.java
This commit is contained in:
@@ -3,6 +3,7 @@ package mock.app;
|
||||
import mock.dataInput.PolarParser;
|
||||
import mock.model.MockRace;
|
||||
import mock.model.Polars;
|
||||
import mock.model.RaceLogic;
|
||||
import network.Messages.LatestMessages;
|
||||
import shared.dataInput.*;
|
||||
import shared.enums.XMLFileType;
|
||||
@@ -91,7 +92,7 @@ public class Event {
|
||||
RegattaDataSource regattaDataSource = new RegattaXMLReader(this.regattaXML, this.xmlFileType);
|
||||
|
||||
//Create and start race.
|
||||
MockRace newRace = new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale);
|
||||
RaceLogic newRace = new RaceLogic(new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale), this.latestMessages);
|
||||
|
||||
new Thread(newRace).start();
|
||||
}
|
||||
|
||||
@@ -22,6 +22,11 @@ public class MockBoat extends Boat {
|
||||
*/
|
||||
private long timeSinceTackChange = 0;
|
||||
|
||||
/**
|
||||
* Stores whether the boat is on autoVMG or not
|
||||
*/
|
||||
private boolean autoVMG = true;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@@ -191,4 +196,11 @@ public class MockBoat extends Boat {
|
||||
return distanceTravelledMeters;
|
||||
}
|
||||
|
||||
public boolean isAutoVMG() {
|
||||
return autoVMG;
|
||||
}
|
||||
|
||||
public void setAutoVMG(boolean autoVMG) {
|
||||
this.autoVMG = autoVMG;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
package mock.model;
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import network.Messages.BoatLocation;
|
||||
import network.Messages.BoatStatus;
|
||||
import network.Messages.Enums.BoatStatusEnum;
|
||||
import network.Messages.LatestMessages;
|
||||
import network.Messages.RaceStatus;
|
||||
import network.Utils.AC35UnitConverter;
|
||||
import org.opengis.geometry.primitive.*;
|
||||
import shared.dataInput.BoatDataSource;
|
||||
import shared.dataInput.RaceDataSource;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import shared.dataInput.RegattaDataSource;
|
||||
import shared.model.*;
|
||||
import shared.model.Bearing;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
@@ -27,13 +24,20 @@ import static java.lang.Math.cos;
|
||||
* Is responsible for simulating the race, and sending messages to a MockOutput instance.
|
||||
*/
|
||||
public class MockRace extends Race {
|
||||
private RaceServer server;
|
||||
|
||||
/**
|
||||
* An observable list of boats in the race.
|
||||
*/
|
||||
private List<MockBoat> boats;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A copy of the boundary list, except "shrunk" inwards by 50m.
|
||||
*/
|
||||
private List<GPSCoordinate> shrinkBoundary;
|
||||
|
||||
|
||||
/**
|
||||
* The scale factor of the race.
|
||||
* See {@link Constants#RaceTimeScale}.
|
||||
@@ -62,6 +66,8 @@ public class MockRace extends Race {
|
||||
|
||||
this.boats = this.generateMockBoats(boatDataSource.getBoats(), raceDataSource.getParticipants(), polars);
|
||||
|
||||
this.shrinkBoundary = GPSCoordinate.getShrinkBoundary(this.boundary);
|
||||
|
||||
//Set up wind generator. It may be tidier to create this outside the race (with the values sourced from a data file maybe?) and pass it in.
|
||||
this.windGenerator = new WindGenerator(
|
||||
Bearing.fromDegrees(225),
|
||||
@@ -73,8 +79,6 @@ public class MockRace extends Race {
|
||||
|
||||
//Wind.
|
||||
this.setWind(windGenerator.generateBaselineWind());
|
||||
|
||||
this.server = new RaceServer(this, latestMessages);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,20 +110,11 @@ public class MockRace extends Race {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Runnable for the thread.
|
||||
*/
|
||||
public void run() {
|
||||
initialiseBoats();
|
||||
this.countdownTimer.start();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates the race time to a specified value, in milliseconds since the unix epoch.
|
||||
* @param currentTime Milliseconds since unix epoch.
|
||||
*/
|
||||
private void updateRaceTime(long currentTime) {
|
||||
public void updateRaceTime(long currentTime) {
|
||||
this.raceClock.setUTCTime(currentTime);
|
||||
}
|
||||
|
||||
@@ -127,7 +122,7 @@ public class MockRace extends Race {
|
||||
/**
|
||||
* Updates the race status enumeration based on the current time.
|
||||
*/
|
||||
private void updateRaceStatusEnum() {
|
||||
public void updateRaceStatusEnum() {
|
||||
|
||||
//The millisecond duration of the race. Negative means it hasn't started, so we flip sign.
|
||||
long timeToStart = - this.raceClock.getDurationMilli();
|
||||
@@ -158,7 +153,7 @@ public class MockRace extends Race {
|
||||
/**
|
||||
* Sets the status of all boats in the race to RACING.
|
||||
*/
|
||||
private void setBoatsStatusToRacing() {
|
||||
public void setBoatsStatusToRacing() {
|
||||
|
||||
for (MockBoat boat : this.boats) {
|
||||
boat.setStatus(BoatStatusEnum.RACING);
|
||||
@@ -170,7 +165,7 @@ public class MockRace extends Race {
|
||||
* Sets the estimated time at next mark for each boat to a specified time. This is used during the countdown timer to provide this value to boat before the race starts.
|
||||
* @param time The time to provide to each boat.
|
||||
*/
|
||||
private void setBoatsTimeNextMark(ZonedDateTime time) {
|
||||
public void setBoatsTimeNextMark(ZonedDateTime time) {
|
||||
|
||||
for (MockBoat boat : this.boats) {
|
||||
boat.setEstimatedTimeAtNextMark(time);
|
||||
@@ -178,143 +173,6 @@ public class MockRace extends Race {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Countdown timer until race starts.
|
||||
*/
|
||||
protected AnimationTimer countdownTimer = new AnimationTimer() {
|
||||
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
@Override
|
||||
public void handle(long arg0) {
|
||||
|
||||
//Update race time.
|
||||
updateRaceTime(currentTime);
|
||||
|
||||
//Update the race status based on the current time.
|
||||
updateRaceStatusEnum();
|
||||
|
||||
//Provide boat's with an estimated time at next mark until the race starts.
|
||||
setBoatsTimeNextMark(raceClock.getCurrentTime());
|
||||
|
||||
//Parse the boat locations.
|
||||
server.parseBoatLocations();
|
||||
|
||||
//Parse the marks.
|
||||
server.parseMarks();
|
||||
|
||||
// Change wind direction
|
||||
changeWindDirection();
|
||||
|
||||
//Parse the race status.
|
||||
server.parseRaceStatus();
|
||||
|
||||
|
||||
if (getRaceStatusEnum() == RaceStatusEnum.STARTED) {
|
||||
setBoatsStatusToRacing();
|
||||
raceTimer.start();
|
||||
this.stop();
|
||||
}
|
||||
|
||||
//Update the animations timer's time.
|
||||
currentTime = System.currentTimeMillis();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Timer that runs for the duration of the race, until all boats finish.
|
||||
*/
|
||||
private AnimationTimer raceTimer = new AnimationTimer() {
|
||||
|
||||
/**
|
||||
* Start time of loop, in milliseconds.
|
||||
*/
|
||||
long timeRaceStarted = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* Current time during a loop iteration.
|
||||
*/
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* The time of the previous frame, in milliseconds.
|
||||
*/
|
||||
long lastFrameTime = timeRaceStarted;
|
||||
|
||||
@Override
|
||||
public void handle(long arg0) {
|
||||
|
||||
//Get the current time.
|
||||
currentTime = System.currentTimeMillis();
|
||||
|
||||
//Update race time.
|
||||
updateRaceTime(currentTime);
|
||||
|
||||
|
||||
//As long as there is at least one boat racing, we still simulate the race.
|
||||
if (getNumberOfActiveBoats() != 0) {
|
||||
|
||||
//Get the time period of this frame.
|
||||
long framePeriod = currentTime - lastFrameTime;
|
||||
|
||||
//For each boat, we update its position, and generate a BoatLocationMessage.
|
||||
for (MockBoat boat : boats) {
|
||||
//If it is still racing, update its position.
|
||||
if (boat.getStatus() == BoatStatusEnum.RACING) {
|
||||
|
||||
updatePosition(boat, framePeriod, raceClock.getDurationMilli());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
//Otherwise, the race is over!
|
||||
raceFinished.start();
|
||||
setRaceStatusEnum(RaceStatusEnum.FINISHED);
|
||||
this.stop();
|
||||
}
|
||||
|
||||
if (getNumberOfActiveBoats() != 0) {
|
||||
// Change wind direction
|
||||
changeWindDirection();
|
||||
|
||||
//Parse the boat locations.
|
||||
server.parseBoatLocations();
|
||||
|
||||
//Parse the marks.
|
||||
server.parseMarks();
|
||||
|
||||
//Parse the race status.
|
||||
server.parseRaceStatus();
|
||||
|
||||
|
||||
//Update the last frame time.
|
||||
this.lastFrameTime = currentTime;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Broadcast that the race has finished.
|
||||
*/
|
||||
protected AnimationTimer raceFinished = new AnimationTimer(){
|
||||
int iters = 0;
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
|
||||
server.parseRaceStatus();
|
||||
|
||||
if (iters > 500) {
|
||||
stop();
|
||||
}
|
||||
iters++;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Initialise the boats in the race.
|
||||
* This sets their starting positions and current legs.
|
||||
@@ -353,8 +211,7 @@ public class MockRace extends Race {
|
||||
boat.setStatus(BoatStatusEnum.PRESTART);
|
||||
|
||||
//We set a large time since tack change so that it calculates a new VMG when the simulation starts.
|
||||
boat.setTimeSinceTackChange(999999);
|
||||
|
||||
boat.setTimeSinceTackChange(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -416,7 +273,6 @@ public class MockRace extends Race {
|
||||
*/
|
||||
public boolean improvesVelocity(VMG currentVMG, VMG potentialVMG, Bearing bearingToDestination) {
|
||||
|
||||
|
||||
//Calculates the angle between the boat and its destination.
|
||||
Angle angleBetweenDestAndHeading = Angle.fromDegrees(currentVMG.getBearing().degrees() - bearingToDestination.degrees());
|
||||
|
||||
@@ -464,8 +320,14 @@ public class MockRace extends Race {
|
||||
//Checks if the current boat has finished the race or not.
|
||||
boolean finish = this.isLastLeg(boat.getCurrentLeg());
|
||||
|
||||
if (!finish) {
|
||||
if (!finish && totalElapsedMilliseconds >= updatePeriodMilliseconds) {
|
||||
|
||||
if (boat.getCurrentSpeed() == 0) {
|
||||
newOptimalVMG(boat);
|
||||
boat.setBearing(boat.calculateBearingToNextMarker());
|
||||
}
|
||||
|
||||
setBoatSpeed(boat);
|
||||
|
||||
//Calculates the distance travelled, in meters, in the current timeslice.
|
||||
double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds);
|
||||
@@ -476,25 +338,10 @@ public class MockRace extends Race {
|
||||
|
||||
//Move the boat forwards that many meters, and advances its time counters by enough milliseconds.
|
||||
boat.moveForwards(distanceTravelledMeters);
|
||||
boat.setTimeSinceTackChange(boat.getTimeSinceTackChange() + updatePeriodMilliseconds);
|
||||
|
||||
|
||||
long tackPeriod = 1000;
|
||||
|
||||
if (boat.getTimeSinceTackChange() > tackPeriod) {
|
||||
//Calculate the new VMG.
|
||||
VMG newVMG = boat.getPolars().calculateVMG(
|
||||
this.getWindDirection(),
|
||||
this.getWindSpeed(),
|
||||
boat.calculateBearingToNextMarker(),
|
||||
Bearing.fromDegrees(0d),
|
||||
Bearing.fromDegrees(359.99999d));
|
||||
|
||||
|
||||
//If the new vmg improves velocity, use it.
|
||||
if (improvesVelocity(boat, newVMG)) {
|
||||
boat.setVMG(newVMG);
|
||||
|
||||
}
|
||||
if (boat.isAutoVMG()) {
|
||||
newOptimalVMG(boat);
|
||||
}
|
||||
|
||||
this.updateEstimatedTime(boat);
|
||||
@@ -502,6 +349,38 @@ public class MockRace extends Race {
|
||||
|
||||
}
|
||||
|
||||
private void newOptimalVMG(MockBoat boat) {
|
||||
long tackPeriod = 15000;
|
||||
|
||||
if (boat.getTimeSinceTackChange() > tackPeriod) {
|
||||
//Calculate the new VMG.
|
||||
VMG newVMG = boat.getPolars().calculateVMG(
|
||||
this.getWindDirection(),
|
||||
this.getWindSpeed(),
|
||||
boat.calculateBearingToNextMarker(),
|
||||
Bearing.fromDegrees(0d),
|
||||
Bearing.fromDegrees(359.99999d));
|
||||
|
||||
|
||||
//If the new vmg improves velocity, use it.
|
||||
if (improvesVelocity(boat, newVMG)) {
|
||||
boat.setVMG(newVMG);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setBoatSpeed(MockBoat boat) {
|
||||
VMG vmg = boat.getPolars().calculateVMG(
|
||||
this.getWindDirection(),
|
||||
this.getWindSpeed(),
|
||||
boat.getBearing(),
|
||||
boat.getBearing(),
|
||||
boat.getBearing());
|
||||
if (vmg.getSpeed() > 0) {
|
||||
boat.setCurrentSpeed(vmg.getSpeed());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of boats that are still active in the race.
|
||||
* They become inactive by either finishing or withdrawing.
|
||||
|
||||
@@ -1,77 +1,196 @@
|
||||
package mock.model;
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import mock.model.commandFactory.Command;
|
||||
import mock.model.commandFactory.CommandFactory;
|
||||
import mock.model.commandFactory.CompositeCommand;
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
import network.Messages.Enums.BoatStatusEnum;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import network.Messages.LatestMessages;
|
||||
import shared.dataInput.BoatDataSource;
|
||||
import shared.dataInput.RaceDataSource;
|
||||
import shared.dataInput.RegattaDataSource;
|
||||
import shared.model.*;
|
||||
import visualiser.gameController.ControllerServer;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
import java.util.Stack;
|
||||
|
||||
public class RaceLogic {
|
||||
private RaceState raceState;
|
||||
public class RaceLogic implements Observer, Runnable {
|
||||
/**
|
||||
* State of current race modified by this object
|
||||
*/
|
||||
private MockRace race;
|
||||
/**
|
||||
* High-level interface to AC35 protocol server
|
||||
*/
|
||||
private RaceServer server;
|
||||
|
||||
private CompositeCommand commands;
|
||||
|
||||
/**
|
||||
* The scale factor of the race.
|
||||
* See {@link Constants#RaceTimeScale}.
|
||||
* Initialises race loop with state and server message queue
|
||||
* @param race state of race to modify
|
||||
* @param messages to send to server
|
||||
*/
|
||||
private int scaleFactor;
|
||||
|
||||
/**
|
||||
* Object used to generate changes in wind speed/direction.
|
||||
*/
|
||||
private WindGenerator windGenerator;
|
||||
|
||||
public RaceLogic(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, LatestMessages latestMessages, Polars polars, int timeScale) {
|
||||
this.raceState = new RaceState(boatDataSource, raceDataSource, regattaDataSource, latestMessages, polars);
|
||||
this.raceState.run();
|
||||
|
||||
//Set up wind generator. It may be tidier to create this outside the race (with the values sourced from a data file maybe?) and pass it in.
|
||||
this.windGenerator = new WindGenerator(
|
||||
Bearing.fromDegrees(225),
|
||||
Bearing.fromDegrees(215),
|
||||
Bearing.fromDegrees(235),
|
||||
12d,
|
||||
8d,
|
||||
16d );
|
||||
raceState.setWind(windGenerator.generateBaselineWind());
|
||||
}
|
||||
|
||||
private void changeWindDirection() {
|
||||
Wind nextWind = windGenerator.generateNextWind(raceState.getWind());
|
||||
|
||||
raceState.setWind(nextWind);
|
||||
public RaceLogic(MockRace race, LatestMessages messages) {
|
||||
this.race = race;
|
||||
this.server = new RaceServer(race, messages);
|
||||
this.commands = new CompositeCommand();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of boats that are still active in the race.
|
||||
* They become inactive by either finishing or withdrawing.
|
||||
* @return The number of boats still active in the race.
|
||||
* Initialise boats and start countdown timer
|
||||
*/
|
||||
protected int getNumberOfActiveBoats() {
|
||||
@Override
|
||||
public void run() {
|
||||
race.initialiseBoats();
|
||||
this.countdownTimer.start();
|
||||
}
|
||||
|
||||
int numberOfActiveBoats = 0;
|
||||
|
||||
for (MockBoat boat : raceState.getBoats()) {
|
||||
/**
|
||||
* Countdown timer until race starts.
|
||||
*/
|
||||
protected AnimationTimer countdownTimer = new AnimationTimer() {
|
||||
|
||||
//If the boat is currently racing, count it.
|
||||
if (boat.getStatus() == BoatStatusEnum.RACING) {
|
||||
numberOfActiveBoats++;
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
@Override
|
||||
public void handle(long arg0) {
|
||||
|
||||
//Update race time.
|
||||
race.updateRaceTime(currentTime);
|
||||
|
||||
//Update the race status based on the current time.
|
||||
race.updateRaceStatusEnum();
|
||||
|
||||
//Provide boat's with an estimated time at next mark until the race starts.
|
||||
race.setBoatsTimeNextMark(race.getRaceClock().getCurrentTime());
|
||||
|
||||
//Parse the boat locations.
|
||||
server.parseBoatLocations();
|
||||
|
||||
//Parse the marks.
|
||||
server.parseMarks();
|
||||
|
||||
// Change wind direction
|
||||
race.changeWindDirection();
|
||||
|
||||
//Parse the race status.
|
||||
server.parseRaceStatus();
|
||||
|
||||
|
||||
if (race.getRaceStatusEnum() == RaceStatusEnum.STARTED) {
|
||||
race.setBoatsStatusToRacing();
|
||||
raceTimer.start();
|
||||
this.stop();
|
||||
}
|
||||
|
||||
//Update the animations timer's time.
|
||||
currentTime = System.currentTimeMillis();
|
||||
}
|
||||
};
|
||||
|
||||
return numberOfActiveBoats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of boats in the race.
|
||||
* @return List of boats in the race.
|
||||
* Timer that runs for the duration of the race, until all boats finish.
|
||||
*/
|
||||
public List<MockBoat> getBoats() {
|
||||
return raceState.getBoats();
|
||||
}
|
||||
private AnimationTimer raceTimer = new AnimationTimer() {
|
||||
|
||||
/**
|
||||
* Start time of loop, in milliseconds.
|
||||
*/
|
||||
long timeRaceStarted = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* Current time during a loop iteration.
|
||||
*/
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* The time of the previous frame, in milliseconds.
|
||||
*/
|
||||
long lastFrameTime = timeRaceStarted;
|
||||
|
||||
long framePeriod = currentTime - lastFrameTime;
|
||||
|
||||
@Override
|
||||
public void handle(long arg0) {
|
||||
|
||||
//Get the current time.
|
||||
currentTime = System.currentTimeMillis();
|
||||
|
||||
//Update race time.
|
||||
race.updateRaceTime(currentTime);
|
||||
|
||||
//As long as there is at least one boat racing, we still simulate the race.
|
||||
if (race.getNumberOfActiveBoats() != 0) {
|
||||
|
||||
//Get the time period of this frame.
|
||||
framePeriod = currentTime - lastFrameTime;
|
||||
|
||||
//For each boat, we update its position, and generate a BoatLocationMessage.
|
||||
for (MockBoat boat : race.getBoats()) {
|
||||
|
||||
//If it is still racing, update its position.
|
||||
if (boat.getStatus() == BoatStatusEnum.RACING) {
|
||||
commands.execute();
|
||||
race.updatePosition(boat, framePeriod, race.getRaceClock().getDurationMilli());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
//Otherwise, the race is over!
|
||||
raceFinished.start();
|
||||
race.setRaceStatusEnum(RaceStatusEnum.FINISHED);
|
||||
this.stop();
|
||||
}
|
||||
|
||||
if (race.getNumberOfActiveBoats() != 0) {
|
||||
// Change wind direction
|
||||
race.changeWindDirection();
|
||||
|
||||
//Parse the boat locations.
|
||||
server.parseBoatLocations();
|
||||
|
||||
//Parse the marks.
|
||||
server.parseMarks();
|
||||
|
||||
//Parse the race status.
|
||||
server.parseRaceStatus();
|
||||
|
||||
|
||||
//Update the last frame time.
|
||||
this.lastFrameTime = currentTime;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Broadcast that the race has finished.
|
||||
*/
|
||||
protected AnimationTimer raceFinished = new AnimationTimer(){
|
||||
int iters = 0;
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
|
||||
server.parseRaceStatus();
|
||||
|
||||
if (iters > 500) {
|
||||
stop();
|
||||
}
|
||||
iters++;
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void update(Observable o, Object arg) {
|
||||
ControllerServer server = (ControllerServer)o;
|
||||
BoatActionEnum action = server.getAction();
|
||||
MockBoat boat = race.getBoats().get(0);
|
||||
|
||||
commands.addCommand(CommandFactory.createCommand(race, boat, action));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
package mock.model;
|
||||
|
||||
import network.Messages.Enums.BoatStatusEnum;
|
||||
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.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class RaceState extends Race {
|
||||
|
||||
/**
|
||||
* An observable list of boats in the race.
|
||||
*/
|
||||
private List<MockBoat> boats;
|
||||
|
||||
private Wind wind;
|
||||
|
||||
public RaceState(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, LatestMessages latestMessages, Polars polars) {
|
||||
super(boatDataSource, raceDataSource, regattaDataSource, latestMessages);
|
||||
this.boats = this.generateMockBoats(boatDataSource.getBoats(), raceDataSource.getParticipants(), polars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a list of MockBoats given a list of Boats, and a list of participating boats.
|
||||
* @param boats The map of Boats describing boats that are potentially in the race. Maps boat sourceID to boat.
|
||||
* @param sourceIDs The list of boat sourceIDs describing which specific boats are actually participating.
|
||||
* @param polars The polars table to be used for boat simulation.
|
||||
* @return A list of MockBoats that are participating in the race.
|
||||
*/
|
||||
private List<MockBoat> generateMockBoats(Map<Integer, Boat> boats, List<Integer> sourceIDs, Polars polars) {
|
||||
|
||||
List<MockBoat> mockBoats = new ArrayList<>(sourceIDs.size());
|
||||
|
||||
//For each sourceID participating...
|
||||
for (int sourceID : sourceIDs) {
|
||||
|
||||
//Get the boat associated with the sourceID.
|
||||
Boat boat = boats.get(sourceID);
|
||||
|
||||
//Construct a MockBoat using the Boat and Polars.
|
||||
MockBoat mockBoat = new MockBoat(boat, polars);
|
||||
|
||||
mockBoats.add(mockBoat);
|
||||
|
||||
}
|
||||
|
||||
return mockBoats;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise the boats in the race.
|
||||
* This sets their starting positions and current legs.
|
||||
*/
|
||||
@Override
|
||||
protected void initialiseBoats() {
|
||||
|
||||
//Gets the starting positions of the boats.
|
||||
List<GPSCoordinate> startingPositions = getSpreadStartingPositions();
|
||||
|
||||
//Get iterators for our boat and position lists.
|
||||
Iterator<MockBoat> boatIt = this.boats.iterator();
|
||||
Iterator<GPSCoordinate> startPositionIt = startingPositions.iterator();
|
||||
|
||||
//Iterate over the pair of lists.
|
||||
while (boatIt.hasNext() && startPositionIt.hasNext()) {
|
||||
|
||||
//Get the next boat and position.
|
||||
MockBoat boat = boatIt.next();
|
||||
GPSCoordinate startPosition = startPositionIt.next();
|
||||
|
||||
|
||||
//The boat starts on the first leg of the race.
|
||||
boat.setCurrentLeg(this.legs.get(0));
|
||||
|
||||
//Boats start with 0 knots speed.
|
||||
boat.setCurrentSpeed(0d);
|
||||
|
||||
//Place the boat at its starting position.
|
||||
boat.setCurrentPosition(startPosition);
|
||||
|
||||
//Boats start facing their next marker.
|
||||
boat.setBearing(boat.calculateBearingToNextMarker());
|
||||
|
||||
//Sets the boats status to prestart - it changes to racing when the race starts.
|
||||
boat.setStatus(BoatStatusEnum.PRESTART);
|
||||
|
||||
//We set a large time since tack change so that it calculates a new VMG when the simulation starts.
|
||||
boat.setTimeSinceTackChange(999999);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a list of starting positions for the different boats, so they do not appear cramped at the start line.
|
||||
*
|
||||
* @return A list of starting positions.
|
||||
*/
|
||||
public List<GPSCoordinate> getSpreadStartingPositions() {
|
||||
|
||||
//The first compound marker of the race - the starting gate.
|
||||
CompoundMark compoundMark = this.legs.get(0).getStartCompoundMark();
|
||||
|
||||
//The position of the two markers from the compound marker.
|
||||
GPSCoordinate mark1Position = compoundMark.getMark1Position();
|
||||
GPSCoordinate mark2Position = compoundMark.getMark2Position();
|
||||
|
||||
|
||||
//Calculates the azimuth between the two points.
|
||||
Azimuth azimuth = GPSCoordinate.calculateAzimuth(mark1Position, mark2Position);
|
||||
|
||||
//Calculates the distance between the two points.
|
||||
double distanceMeters = GPSCoordinate.calculateDistanceMeters(mark1Position, mark2Position);
|
||||
|
||||
//The number of boats in the race.
|
||||
int numberOfBoats = this.boats.size();
|
||||
|
||||
//Calculates the distance between each boat. We divide by numberOfBoats + 1 to ensure that no boat is placed on one of the starting gate's marks.
|
||||
double distanceBetweenBoatsMeters = distanceMeters / (numberOfBoats + 1);
|
||||
|
||||
|
||||
//List to store coordinates in.
|
||||
List<GPSCoordinate> positions = new ArrayList<>();
|
||||
|
||||
//We start spacing boats out from mark 1.
|
||||
GPSCoordinate position = mark1Position;
|
||||
|
||||
//For each boat, displace position, and store it.
|
||||
for (int i = 0; i < numberOfBoats; i++) {
|
||||
|
||||
position = GPSCoordinate.calculateNewPosition(position, distanceBetweenBoatsMeters, azimuth);
|
||||
|
||||
positions.add(position);
|
||||
|
||||
}
|
||||
|
||||
return positions;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
initialiseBoats();
|
||||
}
|
||||
|
||||
public Wind getWind() {
|
||||
return wind;
|
||||
}
|
||||
|
||||
public List<MockBoat> getBoats() {
|
||||
return boats;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package mock.model.commandFactory;
|
||||
|
||||
import mock.model.MockBoat;
|
||||
import mock.model.MockRace;
|
||||
|
||||
/**
|
||||
* Allows RaceLogic to control MockRace state according to the Command pattern
|
||||
*/
|
||||
public interface Command {
|
||||
/**
|
||||
* Execute command - standard method name in pattern
|
||||
*/
|
||||
void execute();
|
||||
}
|
||||
@@ -2,8 +2,26 @@ package mock.model.commandFactory;
|
||||
|
||||
import mock.model.MockBoat;
|
||||
import mock.model.MockRace;
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
|
||||
public interface CommandFactory {
|
||||
|
||||
void execute();
|
||||
/**
|
||||
* Factory class for Command objects
|
||||
*/
|
||||
public class CommandFactory {
|
||||
/**
|
||||
* Generates a command on a race and boat corresponding to the protocol action number.
|
||||
* @param race to receive command
|
||||
* @param boat to receive command in race
|
||||
* @param action number to select command
|
||||
* @return
|
||||
*/
|
||||
public static Command createCommand(MockRace race, MockBoat boat, BoatActionEnum action) {
|
||||
switch(action) {
|
||||
case AUTO_PILOT: return new VMGCommand(race, boat);
|
||||
case TACK_GYBE: return new TackGybeCommand(race, boat);
|
||||
case UPWIND: return new WindCommand(race, boat, true);
|
||||
case DOWNWIND: return new WindCommand(race, boat, false);
|
||||
default: return null; // TODO - please please have discussion over what to default to
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package mock.model.commandFactory;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Wraps multiple commands into a composite to execute queued commands during a frame.
|
||||
*/
|
||||
public class CompositeCommand implements Command {
|
||||
private Stack<Command> commands;
|
||||
|
||||
public CompositeCommand() {
|
||||
this.commands = new Stack<>();
|
||||
}
|
||||
|
||||
public void addCommand(Command command) {
|
||||
commands.push(command);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
while(!commands.isEmpty()) commands.pop().execute();
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,18 @@ package mock.model.commandFactory;
|
||||
|
||||
import mock.model.MockBoat;
|
||||
import mock.model.MockRace;
|
||||
import mock.model.VMG;
|
||||
import shared.model.Bearing;
|
||||
|
||||
/**
|
||||
* Created by David on 2/08/2017.
|
||||
*/
|
||||
public class TackGybeCommand implements CommandFactory {
|
||||
public class TackGybeCommand implements Command {
|
||||
private MockRace race;
|
||||
private MockBoat boat;
|
||||
|
||||
public TackGybeCommand(MockRace race, MockBoat boat) {
|
||||
this.race = race;
|
||||
this.boat = boat;
|
||||
}
|
||||
|
||||
//The refactoring of MockRace will require changes to be made
|
||||
|
||||
|
||||
@@ -2,24 +2,28 @@ package mock.model.commandFactory;
|
||||
|
||||
import mock.model.MockBoat;
|
||||
import mock.model.MockRace;
|
||||
import mock.model.VMG;
|
||||
import shared.model.Bearing;
|
||||
|
||||
/**
|
||||
* Created by David on 2/08/2017.
|
||||
*/
|
||||
public class VMGCommand implements CommandFactory {
|
||||
|
||||
public class VMGCommand implements Command {
|
||||
private MockRace race;
|
||||
private MockBoat boat;
|
||||
|
||||
public VMGCommand(final MockBoat boat){
|
||||
public VMGCommand(MockRace race, MockBoat boat) {
|
||||
this.race = race;
|
||||
this.boat = boat;
|
||||
}
|
||||
|
||||
|
||||
//The refactoring of MockRace will require changes to be made
|
||||
@Override
|
||||
public void execute() {
|
||||
//MOCKBOAT SHOULD HAVE PARAMETER TO TOGGLE AUTO-VMG ON AND OFF
|
||||
/*VMG newVMG = boat.getPolars().calculateVMG(
|
||||
race.getWindDirection(),
|
||||
race.getWindSpeed(),
|
||||
boat.calculateBearingToNextMarker(),
|
||||
Bearing.fromDegrees(0d),
|
||||
Bearing.fromDegrees(359.99999d));
|
||||
boat.setVMG(newVMG);*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package mock.model.commandFactory;
|
||||
|
||||
import mock.model.MockBoat;
|
||||
import mock.model.MockRace;
|
||||
import shared.model.Bearing;
|
||||
|
||||
/**
|
||||
* Created by connortaylorbrown on 4/08/17.
|
||||
*/
|
||||
public class WindCommand implements Command {
|
||||
private MockRace race;
|
||||
private MockBoat boat;
|
||||
private int direction;
|
||||
|
||||
public WindCommand(MockRace race, MockBoat boat, boolean upwind) {
|
||||
this.race = race;
|
||||
this.boat = boat;
|
||||
this.direction = upwind? 1 : -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
double wind = race.getWindDirection().degrees();
|
||||
double heading = boat.getBearing().degrees();
|
||||
|
||||
double offset = 3;
|
||||
if(wind - heading < 0) offset *= -1 * direction;
|
||||
boat.setBearing(Bearing.fromDegrees(heading + offset));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user