use of spacesettlers.actions.PurchaseTypes in project spacesettlers by amymcgovern.
the class PacifistHeuristicAsteroidCollectorTeamClient method getTeamPurchases.
@Override
public /**
* If there is enough resourcesAvailable, buy a base. Place it by finding a ship that is sufficiently
* far away from the existing bases
*/
Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects, ResourcePile resourcesAvailable, PurchaseCosts purchaseCosts) {
HashMap<UUID, PurchaseTypes> purchases = new HashMap<UUID, PurchaseTypes>();
double BASE_BUYING_DISTANCE = 200;
boolean bought_base = false;
if (purchaseCosts.canAfford(PurchaseTypes.BASE, resourcesAvailable)) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
Ship ship = (Ship) actionableObject;
Set<Base> bases = space.getBases();
// how far away is this ship to a base of my team?
boolean buyBase = true;
for (Base base : bases) {
if (base.getTeamName().equalsIgnoreCase(getTeamName())) {
double distance = space.findShortestDistance(ship.getPosition(), base.getPosition());
if (distance < BASE_BUYING_DISTANCE) {
buyBase = false;
}
}
}
if (buyBase) {
purchases.put(ship.getId(), PurchaseTypes.BASE);
bought_base = true;
// System.out.println("Buying a base!!");
break;
}
}
}
}
// can I buy a ship?
if (purchaseCosts.canAfford(PurchaseTypes.SHIP, resourcesAvailable) && bought_base == false) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Base) {
Base base = (Base) actionableObject;
purchases.put(base.getId(), PurchaseTypes.SHIP);
break;
}
}
}
return purchases;
}
use of spacesettlers.actions.PurchaseTypes in project spacesettlers by amymcgovern.
the class AggressiveHeuristicAsteroidCollectorSingletonTeamClient method getTeamPurchases.
@Override
public /**
* If there is enough resourcesAvailable, buy a base. Place it by finding a ship that is sufficiently
* far away from the existing bases
*/
Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects, ResourcePile resourcesAvailable, PurchaseCosts purchaseCosts) {
HashMap<UUID, PurchaseTypes> purchases = new HashMap<UUID, PurchaseTypes>();
double BASE_BUYING_DISTANCE = 200;
boolean bought_base = false;
if (purchaseCosts.canAfford(PurchaseTypes.BASE, resourcesAvailable)) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
Ship ship = (Ship) actionableObject;
Set<Base> bases = space.getBases();
// how far away is this ship to a base of my team?
double maxDistance = Double.MIN_VALUE;
for (Base base : bases) {
if (base.getTeamName().equalsIgnoreCase(getTeamName())) {
double distance = space.findShortestDistance(ship.getPosition(), base.getPosition());
if (distance > maxDistance) {
maxDistance = distance;
}
}
}
if (maxDistance > BASE_BUYING_DISTANCE) {
purchases.put(ship.getId(), PurchaseTypes.BASE);
bought_base = true;
// System.out.println("Buying a base!!");
break;
}
}
}
}
// see if you can buy EMPs
if (purchaseCosts.canAfford(PurchaseTypes.POWERUP_EMP_LAUNCHER, resourcesAvailable)) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
Ship ship = (Ship) actionableObject;
if (!ship.getId().equals(asteroidCollectorID) && !ship.isValidPowerup(PurchaseTypes.POWERUP_EMP_LAUNCHER.getPowerupMap())) {
purchases.put(ship.getId(), PurchaseTypes.POWERUP_EMP_LAUNCHER);
}
}
}
}
// can I buy a ship?
if (purchaseCosts.canAfford(PurchaseTypes.SHIP, resourcesAvailable) && bought_base == false) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Base) {
Base base = (Base) actionableObject;
purchases.put(base.getId(), PurchaseTypes.SHIP);
break;
}
}
}
return purchases;
}
use of spacesettlers.actions.PurchaseTypes 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