Search in sources :

Example 1 with Ship

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

the class AggressiveFlagCollectorTeamClient 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>();
    Ship flagShip;
    int numShips = 0;
    // get the flag carrier, if we have one
    flagShip = getFlagCarrier(space, actionableObjects);
    // we don't have a ship carrying a flag, so find the best choice (if it exists)
    if (flagShip == null) {
        flagShip = findHealthiestShipNearFlag(space, actionableObjects);
    }
    // count ships so we know if we can have a weapons ship
    for (AbstractObject actionable : actionableObjects) {
        if (actionable instanceof Ship) {
            numShips++;
        }
    }
    // resources (as long as it isn't the flagShip)
    for (AbstractObject actionable : actionableObjects) {
        if (actionable instanceof Ship) {
            Ship ship = (Ship) actionable;
            AbstractAction action = null;
            if (flagShip != null && ship.equals(flagShip)) {
                if (flagShip.isCarryingFlag()) {
                    // System.out.println("We have a flag carrier!");
                    Base base = findNearestBase(space, ship);
                    // System.out.println("Flag ship before computing action: " + flagShip);
                    action = new MoveToObjectAction(space, ship.getPosition(), base);
                    // System.out.println("Aiming for base with action " + action);
                    aimingForBase.put(ship.getId(), true);
                // System.out.println("Flag ship after computing action: " + flagShip);
                } else {
                    Flag enemyFlag = getEnemyFlag(space);
                    action = new MoveToObjectAction(space, ship.getPosition(), enemyFlag, enemyFlag.getPosition().getTranslationalVelocity());
                }
            } else {
                // a ship to go get resources and help buy more ships/bases)
                if (numShips >= 2) {
                    // see if we already have a hunting ship
                    if (huntingShip.isEmpty()) {
                        huntingShip.put(ship.getId(), true);
                        System.out.println("Creating a hunting ship");
                        // make one ship the hunter
                        action = getWeaponShipAction(space, ship);
                    } else {
                        // if this ship is the current hunter, have it keep hunting
                        if (huntingShip.containsKey(ship.getId())) {
                            // make one ship the hunter
                            action = getWeaponShipAction(space, ship);
                        // System.out.println("Getting action for hunter");
                        } else {
                            // extra ship but not the hunter (likely a 4th ship)
                            action = getAsteroidCollectorAction(space, ship);
                        }
                    }
                } else {
                // with 2 ships, we need to always collect resources so we can buy more ships for hunting
                // and resources later
                }
            }
            // save the action for this ship
            actions.put(ship.getId(), action);
        } else {
            // bases do nothing
            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) Flag(spacesettlers.objects.Flag) DoNothingAction(spacesettlers.actions.DoNothingAction) Base(spacesettlers.objects.Base) MoveToObjectAction(spacesettlers.actions.MoveToObjectAction)

Example 2 with Ship

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

the class AggressiveFlagCollectorTeamClient 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 3 with Ship

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

the class AggressiveFlagCollectorTeamClient 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);
        return newAction;
    }
    // if the ship has enough resourcesAvailable, take it back to base
    if (ship.getResources().getTotal() > 500) {
        Base base = findNearestBase(space, ship);
        AbstractAction newAction = new MoveToObjectAction(space, currentPosition, base);
        aimingForBase.put(ship.getId(), true);
        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);
    }
    // otherwise aim for the nearest enemy ship
    if (current == null || current.isMovementFinished(space)) {
        aimingForBase.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) 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 4 with Ship

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

the class AggressiveFlagCollectorTeamClient method findHealthiestShipNearFlag.

/**
 * Finds the ship with the highest health and nearest the flag
 *
 * @param space
 * @param actionableObjects
 * @return
 */
private Ship findHealthiestShipNearFlag(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
    double minDistance = Double.MAX_VALUE;
    double maxHealth = Double.MIN_VALUE;
    int minHealth = 2000;
    Ship bestShip = null;
    // first find the enemy flag
    Flag enemyFlag = getEnemyFlag(space);
    // if no ships meet that criteria, return null
    for (AbstractObject actionable : actionableObjects) {
        if (actionable instanceof Ship) {
            Ship ship = (Ship) actionable;
            double dist = space.findShortestDistance(ship.getPosition(), enemyFlag.getPosition());
            if (dist < minDistance && ship.getEnergy() > minHealth) {
                if (ship.getEnergy() > maxHealth) {
                    minDistance = dist;
                    maxHealth = ship.getEnergy();
                    bestShip = ship;
                }
            }
        }
    }
    return bestShip;
}
Also used : AbstractObject(spacesettlers.objects.AbstractObject) Ship(spacesettlers.objects.Ship) Flag(spacesettlers.objects.Flag)

Example 5 with Ship

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

the class AggressiveFlagCollectorTeamClient 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;
    int numBases, numShips;
    // count the number of ships for the base/ship buying algorithm
    numShips = 0;
    for (AbstractActionableObject actionableObject : actionableObjects) {
        if (actionableObject instanceof Ship) {
            numShips++;
        }
    }
    // try to balance
    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?
                boolean buyBase = true;
                numBases = 0;
                for (Base base : bases) {
                    if (base.getTeamName().equalsIgnoreCase(getTeamName())) {
                        numBases++;
                        double distance = space.findShortestDistance(ship.getPosition(), base.getPosition());
                        if (distance < BASE_BUYING_DISTANCE) {
                            buyBase = false;
                        }
                    }
                }
                if (buyBase && numBases < numShips) {
                    purchases.put(ship.getId(), PurchaseTypes.BASE);
                    bought_base = true;
                    System.out.println("Aggressive Flag Collector is buying a base!");
                    break;
                }
            }
        }
    }
    // 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);
                System.out.println("Aggressive Flag Collector is buying a 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