Search in sources :

Example 31 with Ship

use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.

the class PacifistHeuristicAsteroidCollectorTeamClient method initialize.

/**
 * Demonstrates one way to read in knowledge from a file
 */
@Override
public void initialize(Toroidal2DPhysics space) {
    asteroidToShipMap = new HashMap<UUID, Ship>();
    aimingForBase = new HashMap<UUID, Boolean>();
    XStream xstream = new XStream();
    xstream.alias("ExampleKnowledge", ExampleKnowledge.class);
    try {
        myKnowledge = (ExampleKnowledge) xstream.fromXML(new File(knowledgeFile));
    } catch (XStreamException e) {
        // if you get an error, handle it other than a null pointer because
        // the error will happen the first time you run
        myKnowledge = new ExampleKnowledge();
    }
}
Also used : XStreamException(com.thoughtworks.xstream.XStreamException) XStream(com.thoughtworks.xstream.XStream) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) File(java.io.File)

Example 32 with Ship

use of spacesettlers.objects.Ship 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 33 with Ship

use of spacesettlers.objects.Ship 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;
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) Base(spacesettlers.objects.Base)

Example 34 with Ship

use of spacesettlers.objects.Ship 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 35 with Ship

use of spacesettlers.objects.Ship 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)

Aggregations

Ship (spacesettlers.objects.Ship)49 UUID (java.util.UUID)20 Base (spacesettlers.objects.Base)19 Position (spacesettlers.utilities.Position)18 HashMap (java.util.HashMap)16 AbstractAction (spacesettlers.actions.AbstractAction)15 DoNothingAction (spacesettlers.actions.DoNothingAction)15 AbstractObject (spacesettlers.objects.AbstractObject)15 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)9 Asteroid (spacesettlers.objects.Asteroid)9 Vector2D (spacesettlers.utilities.Vector2D)8 Test (org.junit.Test)7 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)7 PurchaseTypes (spacesettlers.actions.PurchaseTypes)7 Beacon (spacesettlers.objects.Beacon)7 AiCore (spacesettlers.objects.AiCore)6 Flag (spacesettlers.objects.Flag)5 Team (spacesettlers.clients.Team)4 ResourcePile (spacesettlers.objects.resources.ResourcePile)4 ExecutionException (java.util.concurrent.ExecutionException)2