Search in sources :

Example 16 with AbstractAction

use of spacesettlers.actions.AbstractAction in project spacesettlers by amymcgovern.

the class AggressiveHeuristicAsteroidCollectorTeamClient method getAsteroidCollectorAction.

/**
 * Gets the action for the asteroid collecting ship
 * @param space
 * @param ship
 * @return
 */
private AbstractAction getAsteroidCollectorAction(Toroidal2DPhysics space, Ship ship) {
    AbstractAction current = ship.getCurrentAction();
    Position currentPosition = ship.getPosition();
    // aim for a beacon if there isn't enough energy
    if (ship.getEnergy() < 2000) {
        Beacon beacon = pickNearestBeacon(space, ship);
        AbstractAction newAction = null;
        // if there is no beacon, then just skip a turn
        if (beacon == null) {
            newAction = new DoNothingAction();
        } else {
            newAction = new MoveToObjectAction(space, currentPosition, beacon);
        }
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
        return newAction;
    }
    // if the ship has enough resourcesAvailable, take it back to base
    if (ship.getResources().getTotal() > 500 || ship.getNumCores() > 0) {
        Base base = findNearestBase(space, ship);
        AbstractAction newAction = new MoveToObjectAction(space, currentPosition, base);
        aimingForBase.put(ship.getId(), true);
        goingForCore.put(ship.getId(), false);
        return newAction;
    }
    // did we bounce off the base?
    if (ship.getResources().getTotal() == 0 && ship.getEnergy() > 2000 && aimingForBase.containsKey(ship.getId()) && aimingForBase.get(ship.getId())) {
        current = null;
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
    }
    // if there is a nearby core, go get it
    AiCore nearbyCore = pickNearestCore(space, ship, 100);
    if (nearbyCore != null) {
        Position newGoal = nearbyCore.getPosition();
        AbstractAction newAction = new MoveToObjectAction(space, currentPosition, nearbyCore);
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), true);
        return newAction;
    }
    // otherwise aim for the asteroid
    if (current == null || current.isMovementFinished(space)) {
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
        Asteroid asteroid = pickHighestValueNearestFreeAsteroid(space, ship);
        AbstractAction newAction = null;
        if (asteroid == null) {
            // there is no asteroid available so collect a beacon
            Beacon beacon = pickNearestBeacon(space, ship);
            // if there is no beacon, then just skip a turn
            if (beacon == null) {
                newAction = new DoNothingAction();
            } else {
                newAction = new MoveToObjectAction(space, currentPosition, beacon);
            }
        } else {
            asteroidToShipMap.put(asteroid.getId(), ship);
            newAction = new MoveToObjectAction(space, currentPosition, asteroid, asteroid.getPosition().getTranslationalVelocity());
        }
        return newAction;
    } else {
        return ship.getCurrentAction();
    }
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Position(spacesettlers.utilities.Position) AiCore(spacesettlers.objects.AiCore) Beacon(spacesettlers.objects.Beacon) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction) Base(spacesettlers.objects.Base) MoveToObjectAction(spacesettlers.actions.MoveToObjectAction)

Example 17 with AbstractAction

use of spacesettlers.actions.AbstractAction in project spacesettlers by amymcgovern.

the class AggressiveHeuristicAsteroidCollectorTeamClient method getWeaponShipAction.

/**
 * Gets the action for the weapons based ship
 * @param space
 * @param ship
 * @return
 */
private AbstractAction getWeaponShipAction(Toroidal2DPhysics space, Ship ship) {
    AbstractAction current = ship.getCurrentAction();
    Position currentPosition = ship.getPosition();
    // aim for a beacon if there isn't enough energy
    if (ship.getEnergy() < 2000) {
        Beacon beacon = pickNearestBeacon(space, ship);
        AbstractAction newAction = null;
        // if there is no beacon, then just skip a turn
        if (beacon == null) {
            newAction = new DoNothingAction();
        } else {
            newAction = new MoveToObjectAction(space, currentPosition, beacon);
        }
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
        return newAction;
    }
    // if the ship has enough resourcesAvailable, take it back to base
    if (ship.getResources().getTotal() > 500 || ship.getNumCores() > 0) {
        Base base = findNearestBase(space, ship);
        AbstractAction newAction = new MoveToObjectAction(space, currentPosition, base);
        aimingForBase.put(ship.getId(), true);
        goingForCore.put(ship.getId(), false);
        return newAction;
    }
    // did we bounce off the base?
    if (ship.getResources().getTotal() == 0 && ship.getEnergy() > 2000 && aimingForBase.containsKey(ship.getId()) && aimingForBase.get(ship.getId())) {
        current = null;
        goingForCore.put(ship.getId(), false);
        aimingForBase.put(ship.getId(), false);
    }
    // if there is a nearby core, go get it
    AiCore nearbyCore = pickNearestCore(space, ship, 100);
    if (nearbyCore != null) {
        Position newGoal = nearbyCore.getPosition();
        AbstractAction newAction = new MoveToObjectAction(space, currentPosition, nearbyCore);
        goingForCore.put(ship.getId(), true);
        aimingForBase.put(ship.getId(), false);
        return newAction;
    }
    // otherwise aim for the nearest enemy ship
    if (current == null || current.isMovementFinished(space)) {
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
        Ship enemy = pickNearestEnemyShip(space, ship);
        AbstractAction newAction = null;
        if (enemy == null) {
            // there is no enemy available so collect a beacon
            Beacon beacon = pickNearestBeacon(space, ship);
            // if there is no beacon, then just skip a turn
            if (beacon == null) {
                newAction = new DoNothingAction();
            } else {
                newAction = new MoveToObjectAction(space, currentPosition, beacon);
            }
        } else {
            newAction = new MoveToObjectAction(space, currentPosition, enemy, enemy.getPosition().getTranslationalVelocity());
        }
        return newAction;
    } else {
        return ship.getCurrentAction();
    }
}
Also used : Position(spacesettlers.utilities.Position) AiCore(spacesettlers.objects.AiCore) Beacon(spacesettlers.objects.Beacon) Ship(spacesettlers.objects.Ship) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction) Base(spacesettlers.objects.Base) MoveToObjectAction(spacesettlers.actions.MoveToObjectAction)

Example 18 with AbstractAction

use of spacesettlers.actions.AbstractAction in project spacesettlers by amymcgovern.

the class AggressiveHeuristicAsteroidCollectorTeamClient method getMovementStart.

/**
 * Assigns ships to asteroids and beacons, as described above
 */
public Map<UUID, AbstractAction> getMovementStart(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
    HashMap<UUID, AbstractAction> actions = new HashMap<UUID, AbstractAction>();
    // loop through each ship
    for (AbstractObject actionable : actionableObjects) {
        if (actionable instanceof Ship) {
            Ship ship = (Ship) actionable;
            // the first time we initialize, decide which ship is the asteroid collector
            if (asteroidCollectorID == null) {
                asteroidCollectorID = ship.getId();
            }
            AbstractAction action;
            if (ship.getId().equals(asteroidCollectorID)) {
                // get the asteroids
                action = getAsteroidCollectorAction(space, ship);
            } else {
                // this ship will try to shoot other ships so its movements take it towards the nearest other ship not on our team
                action = getWeaponShipAction(space, ship);
            }
            actions.put(ship.getId(), action);
        } else {
            // it is a base.  Heuristically decide when to use the shield (TODO)
            actions.put(actionable.getId(), new DoNothingAction());
        }
    }
    return actions;
}
Also used : HashMap(java.util.HashMap) AbstractObject(spacesettlers.objects.AbstractObject) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction)

Example 19 with AbstractAction

use of spacesettlers.actions.AbstractAction 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 20 with AbstractAction

use of spacesettlers.actions.AbstractAction 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

AbstractAction (spacesettlers.actions.AbstractAction)20 DoNothingAction (spacesettlers.actions.DoNothingAction)19 Ship (spacesettlers.objects.Ship)15 UUID (java.util.UUID)13 HashMap (java.util.HashMap)12 AbstractObject (spacesettlers.objects.AbstractObject)12 Position (spacesettlers.utilities.Position)12 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)11 Base (spacesettlers.objects.Base)11 Beacon (spacesettlers.objects.Beacon)8 Asteroid (spacesettlers.objects.Asteroid)6 AiCore (spacesettlers.objects.AiCore)5 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)3 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 MoveAction (spacesettlers.actions.MoveAction)2 Flag (spacesettlers.objects.Flag)2 LinkedHashSet (java.util.LinkedHashSet)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1