use of spacesettlers.objects.Beacon in project spacesettlers by amymcgovern.
the class PacifistFlagCollectorTeamClient method pickNearestBeacon.
/**
* Find the nearest beacon to this ship
* @param space
* @param ship
* @return
*/
private Beacon pickNearestBeacon(Toroidal2DPhysics space, Ship ship) {
// get the current beacons
Set<Beacon> beacons = space.getBeacons();
Beacon closestBeacon = null;
double bestDistance = Double.POSITIVE_INFINITY;
for (Beacon beacon : beacons) {
double dist = space.findShortestDistance(ship.getPosition(), beacon.getPosition());
if (dist < bestDistance) {
bestDistance = dist;
closestBeacon = beacon;
}
}
return closestBeacon;
}
use of spacesettlers.objects.Beacon in project spacesettlers by amymcgovern.
the class PacifistFlagCollectorTeamClient method getAsteroidCollectorAction.
/**
* Gets the action for the asteroid collecting ship
* @param space
* @param ship
* @return
*/
private AbstractAction getAsteroidCollectorAction(Toroidal2DPhysics space, Ship ship) {
AbstractAction current = ship.getCurrentAction();
Position currentPosition = ship.getPosition();
// aim for a beacon if there isn't enough energy
if (ship.getEnergy() < 2000) {
Beacon beacon = pickNearestBeacon(space, ship);
AbstractAction newAction = null;
// if there is no beacon, then just skip a turn
if (beacon == null) {
newAction = new DoNothingAction();
} else {
newAction = new MoveToObjectAction(space, currentPosition, beacon);
}
aimingForBase.put(ship.getId(), false);
return newAction;
}
// if the ship has enough resourcesAvailable, take it back to base
if (ship.getResources().getTotal() > 500) {
Base base = findNearestBase(space, ship);
AbstractAction newAction = new MoveToObjectAction(space, currentPosition, base);
aimingForBase.put(ship.getId(), true);
return newAction;
}
// did we bounce off the base?
if (ship.getResources().getTotal() == 0 && ship.getEnergy() > 2000 && aimingForBase.containsKey(ship.getId()) && aimingForBase.get(ship.getId())) {
current = null;
aimingForBase.put(ship.getId(), false);
}
// otherwise aim for the asteroid
if (current == null || current.isMovementFinished(space)) {
aimingForBase.put(ship.getId(), false);
Asteroid asteroid = pickHighestValueNearestFreeAsteroid(space, ship);
AbstractAction newAction = null;
if (asteroid != null) {
asteroidToShipMap.put(asteroid.getId(), ship);
newAction = new MoveToObjectAction(space, currentPosition, asteroid, asteroid.getPosition().getTranslationalVelocity());
}
return newAction;
}
return ship.getCurrentAction();
}
use of spacesettlers.objects.Beacon in project spacesettlers by amymcgovern.
the class SpaceSettlersSimulator method initializeSimulation.
/**
* Initialize the simulation given a configuration file. Creates all the objects.
* @throws SimulatorException
*/
void initializeSimulation(JSAPResult parserConfig) throws SimulatorException {
simulatedSpace = new Toroidal2DPhysics(simConfig);
// place the beacons
for (int b = 0; b < simConfig.getNumBeacons(); b++) {
Beacon beacon = new Beacon(simulatedSpace.getRandomFreeLocation(random, Beacon.BEACON_RADIUS * 2));
// System.out.println("New beacon at " + beacon.getPosition());
simulatedSpace.addObject(beacon);
}
// place any fixed location asteroids
FixedAsteroidConfig[] fixedAsteroidConfigs = simConfig.getFixedAsteroids();
if (fixedAsteroidConfigs != null) {
for (FixedAsteroidConfig fixedAsteroidConfig : fixedAsteroidConfigs) {
Asteroid asteroid = createNewFixedAsteroid(fixedAsteroidConfig);
simulatedSpace.addObject(asteroid);
}
}
// place the asteroids
RandomAsteroidConfig randomAsteroidConfig = simConfig.getRandomAsteroids();
for (int a = 0; a < randomAsteroidConfig.getNumberInitialAsteroids(); a++) {
Asteroid asteroid = createNewRandomAsteroid(randomAsteroidConfig);
simulatedSpace.addObject(asteroid);
}
// create the clients
for (HighLevelTeamConfig teamConfig : simConfig.getTeams()) {
// ensure this team isn't a duplicate
if (clientMap.containsKey(teamConfig.getTeamName())) {
throw new SimulatorException("Error: duplicate team name " + teamConfig.getTeamName());
}
TeamClientConfig teamClientConfig = getTeamClientConfig(teamConfig, parserConfig.getString("configPath"));
// grab the home base config for this team (to get starting locations as needed)
BaseConfig thisBaseConfig = null;
for (BaseConfig baseConfig : simConfig.getBases()) {
String teamName = baseConfig.getTeamName();
if (teamName.equalsIgnoreCase(teamConfig.getTeamName())) {
thisBaseConfig = baseConfig;
break;
}
}
// now either use the base config for the default region radius or the teamConfig file
if (thisBaseConfig != null && thisBaseConfig.isFixedLocation()) {
teamConfig.setInitialRegionULX(thisBaseConfig.getBoundingBoxULX());
teamConfig.setInitialRegionULY(thisBaseConfig.getBoundingBoxULY());
teamConfig.setInitialRegionLRX(thisBaseConfig.getBoundingBoxLRX());
teamConfig.setInitialRegionLRY(thisBaseConfig.getBoundingBoxLRY());
System.out.println("Initial provided for team " + teamConfig.getTeamName() + "UL (x,y) = " + teamConfig.getInitialRegionULX() + ", " + teamConfig.getInitialRegionULY() + " LR (x,y) = " + teamConfig.getInitialRegionLRX() + ", " + teamConfig.getInitialRegionLRY());
} else {
// if the team doesn't provide default radiii and bases, create one
if (teamConfig.getInitialRegionULX() == 0 && teamConfig.getInitialRegionLRX() == 0) {
teamConfig.setInitialRegionULX(random.nextInt(simConfig.getWidth()));
teamConfig.setInitialRegionLRX(teamConfig.getInitialRegionULX() + simConfig.getWidth() / 4);
teamConfig.setInitialRegionULY(random.nextInt(simConfig.getHeight()));
teamConfig.setInitialRegionLRY(teamConfig.getInitialRegionULX() + simConfig.getHeight() / 4);
System.out.println("Initial location not provided for team " + teamConfig.getTeamName() + "...generating: UL (x,y) = " + teamConfig.getInitialRegionULX() + ", " + teamConfig.getInitialRegionULY() + " LR (x,y) = " + teamConfig.getInitialRegionLRX() + ", " + teamConfig.getInitialRegionLRY());
}
}
TeamClient teamClient = createTeamClient(teamConfig, teamClientConfig);
// make the team inside the simulator for this team
Team team = createTeam(teamConfig, teamClient, teamClientConfig);
for (Ship ship : team.getShips()) {
simulatedSpace.addObject(ship);
}
clientMap.put(teamConfig.getTeamName(), teamClient);
}
// make sure the base count matches the team count
if (simConfig.getTeams().length != simConfig.getBases().length) {
throw new SimulatorException("Error: You specified " + simConfig.getTeams().length + " teams and " + simConfig.getBases().length + " bases. They must match.");
}
// create the bases and ensure there is a base for each team
for (BaseConfig baseConfig : simConfig.getBases()) {
String teamName = baseConfig.getTeamName();
if (!clientMap.containsKey(teamName)) {
throw new SimulatorException("Error: base is listed as team " + teamName + " but there is no corresponding team");
}
TeamClient teamClient = clientMap.get(teamName);
// find the team config for this team
HighLevelTeamConfig thisTeamConfig = null;
for (HighLevelTeamConfig teamConfig : simConfig.getTeams()) {
if (teamConfig.getTeamName().equalsIgnoreCase(teamName)) {
thisTeamConfig = teamConfig;
break;
}
}
// make the location based on fixed or random
Position baseLocation;
if (baseConfig.isFixedLocation()) {
baseLocation = new Position(baseConfig.getX(), baseConfig.getY());
} else {
// make the base in the region specified for this team
// ensure bases are not created right next to asteroids (free by 4 * base_radius for now)
baseLocation = simulatedSpace.getRandomFreeLocationInRegion(random, 4 * Base.BASE_RADIUS, thisTeamConfig.getInitialRegionULX(), thisTeamConfig.getInitialRegionULY(), thisTeamConfig.getInitialRegionLRX(), thisTeamConfig.getInitialRegionLRY());
}
// get this team as well as the client
Team thisTeam = null;
for (Team team : teams) {
if (team.getTeamName().equalsIgnoreCase(teamName)) {
thisTeam = team;
break;
}
}
Base base = new Base(baseLocation, baseConfig.getTeamName(), thisTeam, true);
simulatedSpace.addObject(base);
thisTeam.addBase(base);
}
/**
* If there are flags specified (presumably for capture the flag games), create them
* and match their color to their team. Randomly choose their starting location
* from the specified set of starting locations.
*/
if (simConfig.getFlags() != null) {
for (FlagConfig flagConfig : simConfig.getFlags()) {
// get the right team to match the flag
Team thisTeam = null;
for (Team team : teams) {
if (team.getTeamName().equalsIgnoreCase(flagConfig.getTeamName())) {
thisTeam = team;
break;
}
}
int[] startX = flagConfig.getStartX();
int[] startY = flagConfig.getStartY();
Position[] startingPositions = new Position[startX.length];
for (int i = 0; i < startX.length; i++) {
startingPositions[i] = new Position(startX[i], startY[i]);
}
// System.out.println("Starting Locations are " + startingPositions);
Position flagPosition = startingPositions[random.nextInt(startingPositions.length)];
// System.out.println("Chosen location is " + flagPosition);
Flag flag = new Flag(flagPosition, flagConfig.getTeamName(), thisTeam, startingPositions);
simulatedSpace.addObject(flag);
}
}
}
use of spacesettlers.objects.Beacon in project spacesettlers by amymcgovern.
the class ResourcesPanel method updateData.
public void updateData(AbstractObject object) {
ResourcePile avail = null;
ResourcePile total = null;
if (object.getClass() == Asteroid.class) {
Asteroid asteroid = (Asteroid) object;
avail = asteroid.getResources();
total = avail;
} else if (object.getClass() == Ship.class) {
Ship ship = (Ship) object;
avail = ship.getResources();
total = avail;
} else if (object.getClass() == Base.class) {
Base base = (Base) object;
avail = base.getResources();
total = base.getResources();
} else if (object.getClass() == Beacon.class) {
Beacon beacon = (Beacon) object;
}
if (avail != null) {
waterAvail.setText("" + avail.getResourceQuantity(ResourceTypes.WATER));
fuelAvail.setText("" + avail.getResourceQuantity(ResourceTypes.FUEL));
metalsAvail.setText("" + avail.getResourceQuantity(ResourceTypes.METALS));
waterTotal.setText("" + total.getResourceQuantity(ResourceTypes.WATER));
fuelTotal.setText("" + total.getResourceQuantity(ResourceTypes.FUEL));
metalsTotal.setText("" + total.getResourceQuantity(ResourceTypes.METALS));
} else {
waterAvail.setText("0");
fuelAvail.setText("0");
metalsAvail.setText("0");
waterTotal.setText("0");
fuelTotal.setText("0");
metalsTotal.setText("0");
}
}
use of spacesettlers.objects.Beacon in project spacesettlers by amymcgovern.
the class BeaconCollectorTeamClient method pickNearestFreeBeacon.
/**
* Find the nearest free beacon to this ship
* @param space
* @param ship
* @return
*/
private Beacon pickNearestFreeBeacon(Toroidal2DPhysics space, Ship ship) {
// get the current beacons
Set<Beacon> beacons = space.getBeacons();
Beacon closestBeacon = null;
double bestDistance = Double.POSITIVE_INFINITY;
for (Beacon beacon : beacons) {
if (beaconToShipMap.containsKey(beacon)) {
continue;
}
double dist = space.findShortestDistance(ship.getPosition(), beacon.getPosition());
if (dist < bestDistance) {
bestDistance = dist;
closestBeacon = beacon;
}
}
return closestBeacon;
}
Aggregations