use of spacesettlers.objects.Asteroid 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.Asteroid in project spacesettlers by amymcgovern.
the class PacifistHeuristicAsteroidCollectorTeamClient method pickHighestValueNearestFreeAsteroid.
/**
* Returns the asteroid of highest value that isn't already being chased by this team
*
* @return
*/
private Asteroid pickHighestValueNearestFreeAsteroid(Toroidal2DPhysics space, Ship ship) {
Set<Asteroid> asteroids = space.getAsteroids();
int bestMoney = Integer.MIN_VALUE;
Asteroid bestAsteroid = null;
double minDistance = Double.MAX_VALUE;
for (Asteroid asteroid : asteroids) {
if (!asteroidToShipMap.containsKey(asteroid.getId())) {
if (asteroid.isMineable() && asteroid.getResources().getTotal() > bestMoney) {
double dist = space.findShortestDistance(asteroid.getPosition(), ship.getPosition());
if (dist < minDistance) {
bestMoney = asteroid.getResources().getTotal();
// System.out.println("Considering asteroid " + asteroid.getId() + " as a best one");
bestAsteroid = asteroid;
minDistance = dist;
}
}
}
}
// System.out.println("Best asteroid has " + bestMoney);
return bestAsteroid;
}
use of spacesettlers.objects.Asteroid 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.Asteroid in project spacesettlers by amymcgovern.
the class SpaceSettlersSimulator method createNewFixedAsteroid.
/**
* Create a new fixed location asteroid following all rules of the config files
*
* Fixed asteroids specify a x, y location and a radius. They are always non-mineable.
*
* @param asteroidConfig
* @return
*/
private Asteroid createNewFixedAsteroid(FixedAsteroidConfig asteroidConfig) {
boolean mineable = false;
boolean moveable = false;
int radius = asteroidConfig.getRadius();
// create the asteroid (no fuels in it either)
Asteroid asteroid = new Asteroid(new Position(asteroidConfig.getX(), asteroidConfig.getY()), mineable, radius, moveable, 0, 0, 0);
return asteroid;
}
use of spacesettlers.objects.Asteroid in project spacesettlers by amymcgovern.
the class SpaceSettlersSimulator method createNewRandomAsteroid.
/**
* Create a new asteroid following all rules of the config files
*
* Asteroids can either be fixed location or randomly generated. If they are
* fixed, they need x, y, and radius.
*
* @param asteroidConfig
* @return
*/
private Asteroid createNewRandomAsteroid(RandomAsteroidConfig asteroidConfig) {
// choose if the asteroid is mine-able
double prob = random.nextDouble();
boolean mineable = false;
if (prob < asteroidConfig.getProbabilityMineable()) {
mineable = true;
}
// asteroids
// choose the radius randomly for random asteroids
int radius = random.nextInt(Asteroid.MAX_ASTEROID_RADIUS - Asteroid.MIN_ASTEROID_RADIUS) + Asteroid.MIN_ASTEROID_RADIUS;
// choose if the asteroid is moving or stationary
prob = random.nextDouble();
boolean moveable = false;
if (prob < asteroidConfig.getProbabilityMoveable()) {
moveable = true;
}
// choose the asteroid mixture
double fuel = random.nextDouble() * asteroidConfig.getProbabilityFuelType();
double water = random.nextDouble() * asteroidConfig.getProbabilityWaterType();
double metals = random.nextDouble() * asteroidConfig.getProbabilityMetalsType();
// renormalize so it all adds to 1
double normalize = fuel + water + metals;
fuel = fuel / normalize;
water = water / normalize;
metals = metals / normalize;
// create the asteroid
Asteroid asteroid = new Asteroid(simulatedSpace.getRandomFreeLocation(random, radius * 2), mineable, radius, moveable, fuel, water, metals);
if (asteroid.isMoveable()) {
Vector2D randomMotion = Vector2D.getRandom(random, asteroidConfig.getMaxInitialVelocity());
asteroid.getPosition().setTranslationalVelocity(randomMotion);
}
return asteroid;
}
Aggregations