use of spacesettlers.objects.AbstractActionableObject in project spacesettlers by amymcgovern.
the class AggressiveHeuristicAsteroidCollectorTeamClient method getPowerups.
/**
* The asteroid collector doesn't use power ups but the weapons one does (at random)
* @param space
* @param actionableObjects
* @return
*/
@Override
public Map<UUID, SpaceSettlersPowerupEnum> getPowerups(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
HashMap<UUID, SpaceSettlersPowerupEnum> powerUps = new HashMap<UUID, SpaceSettlersPowerupEnum>();
for (AbstractActionableObject actionableObject : actionableObjects) {
SpaceSettlersPowerupEnum powerup = SpaceSettlersPowerupEnum.values()[random.nextInt(SpaceSettlersPowerupEnum.values().length)];
Boolean gettingCore = false;
if (goingForCore.containsKey(actionableObject.getId())) {
gettingCore = goingForCore.get(actionableObject.getId());
}
if (!actionableObject.getId().equals(asteroidCollectorID) && !gettingCore && actionableObject.isValidPowerup(powerup) && random.nextDouble() < weaponsProbability) {
powerUps.put(actionableObject.getId(), powerup);
}
}
return powerUps;
}
use of spacesettlers.objects.AbstractActionableObject in project spacesettlers by amymcgovern.
the class Team method getTeamPowerups.
/**
* Get the weapons or power ups for the team this turn
*
* @param space
* @return
*/
public Map<UUID, SpaceSettlersPowerupEnum> getTeamPowerups(Toroidal2DPhysics space) {
Map<UUID, SpaceSettlersPowerupEnum> powerups = new HashMap<UUID, SpaceSettlersPowerupEnum>();
final Toroidal2DPhysics clonedSpace = space.deepClone();
final Set<AbstractActionableObject> clonedActionableObjects = getTeamActionableObjectsClone(space);
// if the previous thread call hasn't finished, then just return default
if (executor == null || executor.isTerminated()) {
executor = Executors.newSingleThreadExecutor();
} else {
return powerups;
}
// System.out.println("exec " + executor.isTerminated());
Future<Map<UUID, SpaceSettlersPowerupEnum>> future = executor.submit(new Callable<Map<UUID, SpaceSettlersPowerupEnum>>() {
public Map<UUID, SpaceSettlersPowerupEnum> call() throws Exception {
return teamClient.getPowerups(clonedSpace, clonedActionableObjects);
}
});
try {
// start
powerups = future.get(SpaceSettlersSimulator.TEAM_ACTION_TIMEOUT, TimeUnit.MILLISECONDS);
// finished in time
} catch (TimeoutException e) {
// was terminated
// return empty map, don't buy anything
System.out.println(getTeamName() + " timed out in getTeamPowerups");
powerups = new HashMap<UUID, SpaceSettlersPowerupEnum>();
} catch (InterruptedException e) {
// we were interrupted (should not happen but lets be good programmers)
// return empty map, don't buy anything
powerups = new HashMap<UUID, SpaceSettlersPowerupEnum>();
e.printStackTrace();
} catch (ExecutionException e) {
// the executor threw and exception (should not happen but lets be good programmers)
// return empty map, don't buy anything
powerups = new HashMap<UUID, SpaceSettlersPowerupEnum>();
e.printStackTrace();
} catch (Exception e) {
System.err.println("Error in agent. Printing stack trace");
powerups = new HashMap<UUID, SpaceSettlersPowerupEnum>();
e.printStackTrace();
}
executor.shutdownNow();
return powerups;
}
use of spacesettlers.objects.AbstractActionableObject in project spacesettlers by amymcgovern.
the class Team method getTeamMovementStart.
/**
* Ask the team client for actions
*
* @param simulator
* @param random
* @return
*/
public Map<UUID, AbstractAction> getTeamMovementStart(Toroidal2DPhysics space) {
Map<UUID, AbstractAction> teamActions = new HashMap<UUID, AbstractAction>();
// ask the client for its movement
final Toroidal2DPhysics clonedSpace = space.deepClone();
final Set<AbstractActionableObject> clonedActionableObjects = getTeamActionableObjectsClone(space);
// if the previous thread call hasn't finished, then just return default
if (executor == null || executor.isTerminated()) {
executor = Executors.newSingleThreadExecutor();
} else {
return teamActions;
}
Future<Map<UUID, AbstractAction>> future = executor.submit(new Callable<Map<UUID, AbstractAction>>() {
public Map<UUID, AbstractAction> call() {
Map<UUID, AbstractAction> teamActions = null;
teamActions = teamClient.getMovementStart(clonedSpace, clonedActionableObjects);
return teamActions;
}
});
try {
// start
teamActions = future.get(SpaceSettlersSimulator.TEAM_ACTION_TIMEOUT, TimeUnit.MILLISECONDS);
// finished in time
} catch (TimeoutException e) {
// was terminated
// return empty map, this will invoke default behavior of using DoNothingAction
teamActions = new HashMap<UUID, AbstractAction>();
System.err.println(getTeamName() + " timed out in getTeamMovementStart");
} catch (InterruptedException e) {
// we were interrupted (should not happen but lets be good programmers)
// return empty map, this will invoke default behavior of using DoNothingAction
teamActions = new HashMap<UUID, AbstractAction>();
e.printStackTrace();
} catch (ExecutionException e) {
// the executor threw and exception (should not happen but lets be good programmers)
// return empty map, this will invoke default behavior of using DoNothingAction
teamActions = new HashMap<UUID, AbstractAction>();
e.printStackTrace();
} catch (Exception e) {
// we shouldn't do this but it seems necessary to make
// the agent behave (do nothing) if it crashes
System.err.println("Error in agent, stack trace to follow");
e.printStackTrace();
teamActions = new HashMap<UUID, AbstractAction>();
}
executor.shutdownNow();
return teamActions;
// HashMap<UUID, AbstractAction> teamActions = teamClient.getAction(clonedSpace, clonedTeamShips);
// return teamActions;
}
use of spacesettlers.objects.AbstractActionableObject in project spacesettlers by amymcgovern.
the class SpaceSettlersSimulator method advanceTime.
/**
* Advance time one step
*/
void advanceTime() {
// update the team info (to send into the space for use by other teams)
updateTeamInfo();
ExecutorService teamExecutor;
if (debug) {
teamExecutor = Executors.newSingleThreadExecutor();
} else {
teamExecutor = Executors.newCachedThreadPool();
}
Map<Team, Future<Map<UUID, AbstractAction>>> clientActionFutures = new HashMap<Team, Future<Map<UUID, AbstractAction>>>();
// get the actions from each team
for (Team team : teams) {
clientActionFutures.put(team, teamExecutor.submit(new AdvanceTimeCallable(team)));
}
for (Team team : teams) {
Map<UUID, AbstractAction> teamActions;
try {
teamActions = clientActionFutures.get(team).get();
} catch (InterruptedException e) {
// something went wrong...return empty map
teamActions = new HashMap<UUID, AbstractAction>();
} catch (ExecutionException e) {
// something went wrong...return empty map
teamActions = new HashMap<UUID, AbstractAction>();
}
// get the actions for each ship
for (Ship ship : team.getShips()) {
// if the client forgets to set an action, set it to DoNothing
if (teamActions == null || !teamActions.containsKey(ship.getId())) {
teamActions.put(ship.getId(), new DoNothingAction());
}
ship.setCurrentAction(teamActions.get(ship.getId()));
}
}
teamExecutor.shutdown();
// get the power ups being used on this turn
Map<UUID, SpaceSettlersPowerupEnum> allPowerups = new HashMap<UUID, SpaceSettlersPowerupEnum>();
for (Team team : teams) {
Map<UUID, SpaceSettlersPowerupEnum> powerups = team.getTeamPowerups(simulatedSpace);
if (powerups != null) {
for (UUID key : powerups.keySet()) {
// verify power ups belong to this team
if (!team.isValidTeamID(key)) {
continue;
}
// get the object and ensure it can have a power up on it
AbstractObject swObject = simulatedSpace.getObjectById(key);
if (!(swObject instanceof AbstractActionableObject)) {
continue;
}
// verify that the object has the power up associated with it
AbstractActionableObject actionableObject = (AbstractActionableObject) swObject;
if (actionableObject.isValidPowerup(powerups.get(key))) {
allPowerups.put(key, powerups.get(key));
}
}
}
}
// now update the physics on all objects
simulatedSpace.advanceTime(this.getTimestep(), allPowerups);
// and end any actions inside the team
for (Team team : teams) {
team.getTeamMovementEnd(simulatedSpace);
}
// handle purchases at the end of a turn (so ships will have movements next turn)
for (Team team : teams) {
// now get purchases for the team
Map<UUID, PurchaseTypes> purchases = team.getTeamPurchases(simulatedSpace);
handlePurchases(team, purchases);
}
// cleanup and remove dead weapons
simulatedSpace.cleanupDeadWeapons();
// cleanup and remove dead cores
simulatedSpace.cleanupDeadCores();
// respawn any objects that died (and that should respawn - this includes Flags)
final double asteroidMaxVelocity = simConfig.getRandomAsteroids().getMaxInitialVelocity();
simulatedSpace.respawnDeadObjects(random, asteroidMaxVelocity);
// spawn new asteroids with a small probability (up to the maximum number allowed)
int maxAsteroids = simConfig.getRandomAsteroids().getMaximumNumberAsteroids();
int numAsteroids = simulatedSpace.getAsteroids().size();
if (numAsteroids < maxAsteroids) {
if (random.nextDouble() < ASTEROID_SPAWN_PROBABILITY) {
// System.out.println("Spawning a new asteroid");
Asteroid asteroid = createNewRandomAsteroid(simConfig.getRandomAsteroids());
simulatedSpace.addObject(asteroid);
}
}
updateScores();
// for (Team team : teams) {
// for (Ship ship : team.getShips()) {
// System.out.println("Ship " + ship.getTeamName() + ship.getId() + " has resourcesAvailable " + ship.getMoney());
// }
// }
}
Aggregations