use of spacesettlers.simulator.Toroidal2DPhysics in project spacesettlers by amymcgovern.
the class Team method getTeamPurchases.
/**
* Ask the team if they want to purchase anything this turn. You can only
* purchase one item per turn and only if you have enough resourcesAvailable.
*
* @param space
* @return
*/
public Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space) {
Map<UUID, PurchaseTypes> purchase = new HashMap<UUID, PurchaseTypes>();
final Toroidal2DPhysics clonedSpace = space.deepClone();
final Set<AbstractActionableObject> clonedActionableObjects = getTeamActionableObjectsClone(space);
final PurchaseCosts clonedPurchaseCost = getPurchaseCostClone();
final ResourcePile clonedResources = new ResourcePile(availableResources);
// if the previous thread call hasn't finished, then just return default
if (executor == null || executor.isTerminated()) {
executor = Executors.newSingleThreadExecutor();
} else {
return purchase;
}
// System.out.println("exec " + executor.isTerminated());
Future<Map<UUID, PurchaseTypes>> future = executor.submit(new Callable<Map<UUID, PurchaseTypes>>() {
public Map<UUID, PurchaseTypes> call() throws Exception {
return teamClient.getTeamPurchases(clonedSpace, clonedActionableObjects, clonedResources, clonedPurchaseCost);
}
});
try {
// start
purchase = 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 getTeamPurchases");
purchase = new HashMap<UUID, PurchaseTypes>();
} catch (InterruptedException e) {
// we were interrupted (should not happen but lets be good programmers)
// return empty map, don't buy anything
purchase = new HashMap<UUID, PurchaseTypes>();
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
purchase = new HashMap<UUID, PurchaseTypes>();
e.printStackTrace();
} catch (RejectedExecutionException e) {
System.err.println("exec" + executor.isTerminated());
e.printStackTrace();
} catch (Exception e) {
purchase = new HashMap<UUID, PurchaseTypes>();
e.printStackTrace();
}
executor.shutdownNow();
return purchase;
}
use of spacesettlers.simulator.Toroidal2DPhysics in project spacesettlers by amymcgovern.
the class Team method getTeamMovementEnd.
/**
* Allows the client to do cleanup after an action and before
* the next one (if needed)
*
* @param simulatedSpace
* @return
*/
public void getTeamMovementEnd(Toroidal2DPhysics space) {
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;
}
// System.out.println("exec " + executor.isTerminated());
Future<Boolean> future = executor.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
teamClient.getMovementEnd(clonedSpace, clonedActionableObjects);
return true;
}
});
Boolean didReturn = false;
try {
// start
didReturn = future.get(SpaceSettlersSimulator.TEAM_END_ACTION_TIMEOUT, TimeUnit.MILLISECONDS);
// finished in time
} catch (TimeoutException e) {
// was terminated
// set didReturn false
System.out.println(getTeamName() + " timed out in getTeamMovementEnd");
didReturn = false;
} catch (InterruptedException e) {
// we were interrupted (should not happen but lets be good programmers)
// set didReturn false
didReturn = false;
e.printStackTrace();
} catch (ExecutionException e) {
// the executor threw and exception (should not happen but lets be good programmers)
// set didReturn false
didReturn = false;
e.printStackTrace();
} catch (RejectedExecutionException e) {
System.err.println("exec" + executor.isTerminated());
e.printStackTrace();
} catch (Exception e) {
// we shouldn't do this but it seems necessary to make
// the agent behave (do nothing) if it crashes
didReturn = false;
System.err.println("Error in agent. Printing stack trace.");
e.printStackTrace();
}
executor.shutdownNow();
// figure out how many beacons the team has collected
// figure out how many hitsInflicted and killsInflicted the team has
int beacons = 0;
int hits = 0;
int killsInflicted = 0;
int killsReceived = 0;
int damageInflicted = 0;
int damagedReceived = 0;
for (Ship ship : teamShips) {
beacons += ship.getNumBeacons();
hits += ship.getHitsInflicted();
killsInflicted += ship.getKillsInflicted();
killsReceived += ship.getKillsReceived();
damageInflicted += ship.getDamageInflicted();
// check team ships for how much damageInflicted they have received
damagedReceived += ship.getDamageReceived();
}
// check the bases for how much damageInflicted they have received
for (UUID baseID : teamBaseIDs) {
Base base = (Base) space.getObjectById(baseID);
damagedReceived += base.getDamageReceived();
killsReceived += base.getKillsReceived();
}
setTotalBeacons(beacons);
this.totalKillsInflicted = killsInflicted;
this.totalKillsReceived = killsReceived;
this.totalHitsInflicted = hits;
this.totalDamageInflicted = damageInflicted;
this.totalDamageReceived = damagedReceived;
}
use of spacesettlers.simulator.Toroidal2DPhysics 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.simulator.Toroidal2DPhysics 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.simulator.Toroidal2DPhysics in project spacesettlers by amymcgovern.
the class TestMoveAction method setUp.
@Before
public void setUp() throws Exception {
space = new Toroidal2DPhysics(480, 640, timestep);
targetVelocity = new Vector2D();
}
Aggregations