# 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.javamain
commit
3d417c3a40
@ -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;
|
||||
|
||||
public class RaceLogic {
|
||||
private RaceState raceState;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
import java.util.Stack;
|
||||
|
||||
public class RaceLogic implements Observer, Runnable {
|
||||
/**
|
||||
* State of current race modified by this object
|
||||
*/
|
||||
private MockRace race;
|
||||
/**
|
||||
* The scale factor of the race.
|
||||
* See {@link Constants#RaceTimeScale}.
|
||||
* High-level interface to AC35 protocol server
|
||||
*/
|
||||
private int scaleFactor;
|
||||
private RaceServer server;
|
||||
|
||||
private CompositeCommand commands;
|
||||
|
||||
/**
|
||||
* Object used to generate changes in wind speed/direction.
|
||||
* Initialises race loop with state and server message queue
|
||||
* @param race state of race to modify
|
||||
* @param messages to send to server
|
||||
*/
|
||||
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());
|
||||
public RaceLogic(MockRace race, LatestMessages messages) {
|
||||
this.race = race;
|
||||
this.server = new RaceServer(race, messages);
|
||||
this.commands = new CompositeCommand();
|
||||
}
|
||||
|
||||
private void changeWindDirection() {
|
||||
Wind nextWind = windGenerator.generateNextWind(raceState.getWind());
|
||||
|
||||
raceState.setWind(nextWind);
|
||||
/**
|
||||
* Initialise boats and start countdown timer
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
race.initialiseBoats();
|
||||
this.countdownTimer.start();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Countdown timer until race starts.
|
||||
*/
|
||||
protected int getNumberOfActiveBoats() {
|
||||
protected AnimationTimer countdownTimer = new AnimationTimer() {
|
||||
|
||||
|
||||
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();
|
||||
|
||||
int numberOfActiveBoats = 0;
|
||||
//Provide boat's with an estimated time at next mark until the race starts.
|
||||
race.setBoatsTimeNextMark(race.getRaceClock().getCurrentTime());
|
||||
|
||||
for (MockBoat boat : raceState.getBoats()) {
|
||||
//Parse the boat locations.
|
||||
server.parseBoatLocations();
|
||||
|
||||
//If the boat is currently racing, count it.
|
||||
if (boat.getStatus() == BoatStatusEnum.RACING) {
|
||||
numberOfActiveBoats++;
|
||||
//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();
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package mock.model.commandFactory;
|
||||
|
||||
import mock.model.MockRace;
|
||||
import network.Messages.Enums.BoatActionEnum;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import shared.model.Boat;
|
||||
import shared.model.Race;
|
||||
import visualiser.model.VisualiserRace;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* Created by connortaylorbrown on 4/08/17.
|
||||
*/
|
||||
public class WindCommandTest {
|
||||
private Race race;
|
||||
private Boat boat;
|
||||
private Command upwind;
|
||||
private Command downwind;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
boat = new Boat(0, "Bob", "NZ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upwindCommandDecreasesAngle() {
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue