Search in sources :

Example 6 with Ship

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

the class AggressiveHeuristicAsteroidCollectorSingletonTeamClient 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 = getAggressiveAsteroidCollectorAction(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 7 with Ship

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

the class AggressiveHeuristicAsteroidCollectorSingletonTeamClient method getAggressiveAsteroidCollectorAction.

/**
 * Gets the action for the asteroid collecting ship (while being aggressive towards the other ships)
 * @param space
 * @param ship
 * @return
 */
private AbstractAction getAggressiveAsteroidCollectorAction(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);
        shouldShoot = 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);
        shouldShoot = 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);
        shouldShoot = 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);
        return newAction;
    }
    // otherwise either for an asteroid or an enemy ship (depending on who is closer and what we need)
    if (current == null || current.isMovementFinished(space)) {
        aimingForBase.put(ship.getId(), false);
        // see if there is an enemy ship nearby
        Ship enemy = pickNearestEnemyShip(space, ship);
        // find the highest valued nearby asteroid
        Asteroid asteroid = pickHighestValueNearestFreeAsteroid(space, ship);
        AbstractAction newAction = null;
        // if there is no enemy nearby, go for an asteroid
        if (enemy == null) {
            if (asteroid != null) {
                newAction = new MoveToObjectAction(space, currentPosition, asteroid, asteroid.getPosition().getTranslationalVelocity());
                shouldShoot = false;
                return newAction;
            } else {
                // no enemy and no asteroid, just skip this turn (shouldn't happen often)
                shouldShoot = true;
                newAction = new DoNothingAction();
                return newAction;
            }
        }
        // now decide which one to aim for
        if (asteroid != null) {
            double enemyDistance = space.findShortestDistance(ship.getPosition(), enemy.getPosition());
            double asteroidDistance = space.findShortestDistance(ship.getPosition(), asteroid.getPosition());
            // we are aggressive, so aim for enemies if they are nearby
            if (enemyDistance < asteroidDistance) {
                shouldShoot = true;
                newAction = new MoveToObjectAction(space, currentPosition, enemy, enemy.getPosition().getTranslationalVelocity());
            } else {
                shouldShoot = false;
                newAction = new MoveToObjectAction(space, currentPosition, asteroid, asteroid.getPosition().getTranslationalVelocity());
            }
            return newAction;
        } else {
            newAction = new MoveToObjectAction(space, currentPosition, enemy, enemy.getPosition().getTranslationalVelocity());
        }
        return newAction;
    }
    // return the current if new goals haven't formed
    return ship.getCurrentAction();
}
Also used : Asteroid(spacesettlers.objects.Asteroid) 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 8 with Ship

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

the class AggressiveHeuristicAsteroidCollectorSingletonTeamClient method pickNearestEnemyShip.

/**
 * Find the nearest ship on another team and aim for it
 * @param space
 * @param ship
 * @return
 */
private Ship pickNearestEnemyShip(Toroidal2DPhysics space, Ship ship) {
    double minDistance = Double.POSITIVE_INFINITY;
    Ship nearestShip = null;
    for (Ship otherShip : space.getShips()) {
        // don't aim for our own team (or ourself)
        if (otherShip.getTeamName().equals(ship.getTeamName())) {
            continue;
        }
        double distance = space.findShortestDistance(ship.getPosition(), otherShip.getPosition());
        if (distance < minDistance) {
            minDistance = distance;
            nearestShip = otherShip;
        }
    }
    return nearestShip;
}
Also used : Ship(spacesettlers.objects.Ship)

Example 9 with Ship

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

the class AggressiveHeuristicAsteroidCollectorTeamClient method pickNearestEnemyShip.

/**
 * Find the nearest ship on another team and aim for it
 * @param space
 * @param ship
 * @return
 */
private Ship pickNearestEnemyShip(Toroidal2DPhysics space, Ship ship) {
    double minDistance = Double.POSITIVE_INFINITY;
    Ship nearestShip = null;
    for (Ship otherShip : space.getShips()) {
        // don't aim for our own team (or ourself)
        if (otherShip.getTeamName().equals(ship.getTeamName())) {
            continue;
        }
        double distance = space.findShortestDistance(ship.getPosition(), otherShip.getPosition());
        if (distance < minDistance) {
            minDistance = distance;
            nearestShip = otherShip;
        }
    }
    return nearestShip;
}
Also used : Ship(spacesettlers.objects.Ship)

Example 10 with Ship

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

the class AggressiveHeuristicAsteroidCollectorTeamClient 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)

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