Search in sources :

Example 6 with SpaceSettlersPowerupEnum

use of spacesettlers.objects.powerups.SpaceSettlersPowerupEnum 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

SpaceSettlersPowerupEnum (spacesettlers.objects.powerups.SpaceSettlersPowerupEnum)6 HashMap (java.util.HashMap)4 UUID (java.util.UUID)4 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)4 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 ResourcePile (spacesettlers.objects.resources.ResourcePile)2 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AbstractAction (spacesettlers.actions.AbstractAction)1 DoNothingAction (spacesettlers.actions.DoNothingAction)1 PurchaseTypes (spacesettlers.actions.PurchaseTypes)1 Team (spacesettlers.clients.Team)1 ShipGraphics (spacesettlers.graphics.ShipGraphics)1 AbstractObject (spacesettlers.objects.AbstractObject)1 Asteroid (spacesettlers.objects.Asteroid)1 Ship (spacesettlers.objects.Ship)1 Toroidal2DPhysics (spacesettlers.simulator.Toroidal2DPhysics)1