Search in sources :

Example 1 with Toroidal2DPhysics

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;
}
Also used : ResourcePile(spacesettlers.objects.resources.ResourcePile) AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Toroidal2DPhysics(spacesettlers.simulator.Toroidal2DPhysics) PurchaseCosts(spacesettlers.actions.PurchaseCosts) 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 2 with Toroidal2DPhysics

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

Example 3 with Toroidal2DPhysics

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;
}
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 4 with Toroidal2DPhysics

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;
}
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 5 with Toroidal2DPhysics

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();
}
Also used : Vector2D(spacesettlers.utilities.Vector2D) Toroidal2DPhysics(spacesettlers.simulator.Toroidal2DPhysics) Before(org.junit.Before)

Aggregations

Toroidal2DPhysics (spacesettlers.simulator.Toroidal2DPhysics)7 UUID (java.util.UUID)4 ExecutionException (java.util.concurrent.ExecutionException)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Before (org.junit.Before)3 Vector2D (spacesettlers.utilities.Vector2D)2 AbstractAction (spacesettlers.actions.AbstractAction)1 PurchaseCosts (spacesettlers.actions.PurchaseCosts)1 PurchaseTypes (spacesettlers.actions.PurchaseTypes)1 Base (spacesettlers.objects.Base)1 Ship (spacesettlers.objects.Ship)1 SpaceSettlersPowerupEnum (spacesettlers.objects.powerups.SpaceSettlersPowerupEnum)1 ResourcePile (spacesettlers.objects.resources.ResourcePile)1 Position (spacesettlers.utilities.Position)1