Search in sources :

Example 11 with AbstractActionableObject

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;
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) SpaceSettlersPowerupEnum(spacesettlers.objects.powerups.SpaceSettlersPowerupEnum) UUID(java.util.UUID)

Example 12 with AbstractActionableObject

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;
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) SpaceSettlersPowerupEnum(spacesettlers.objects.powerups.SpaceSettlersPowerupEnum) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Toroidal2DPhysics(spacesettlers.simulator.Toroidal2DPhysics) UUID(java.util.UUID) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

Example 13 with AbstractActionableObject

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;
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Toroidal2DPhysics(spacesettlers.simulator.Toroidal2DPhysics) UUID(java.util.UUID) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) AbstractAction(spacesettlers.actions.AbstractAction) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

Example 14 with AbstractActionableObject

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());
// }
// }
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) SpaceSettlersPowerupEnum(spacesettlers.objects.powerups.SpaceSettlersPowerupEnum) Asteroid(spacesettlers.objects.Asteroid) AbstractObject(spacesettlers.objects.AbstractObject) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Team(spacesettlers.clients.Team) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) AbstractAction(spacesettlers.actions.AbstractAction) HashMap(java.util.HashMap) Map(java.util.Map) DoNothingAction(spacesettlers.actions.DoNothingAction)

Aggregations

UUID (java.util.UUID)14 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)14 HashMap (java.util.HashMap)11 Ship (spacesettlers.objects.Ship)9 PurchaseTypes (spacesettlers.actions.PurchaseTypes)8 Base (spacesettlers.objects.Base)8 ExecutionException (java.util.concurrent.ExecutionException)5 Map (java.util.Map)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 SpaceSettlersPowerupEnum (spacesettlers.objects.powerups.SpaceSettlersPowerupEnum)4 Toroidal2DPhysics (spacesettlers.simulator.Toroidal2DPhysics)4 AbstractAction (spacesettlers.actions.AbstractAction)3 DoNothingAction (spacesettlers.actions.DoNothingAction)2 AbstractObject (spacesettlers.objects.AbstractObject)2 ResourcePile (spacesettlers.objects.resources.ResourcePile)2 Position (spacesettlers.utilities.Position)2 LinkedHashSet (java.util.LinkedHashSet)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1