Search in sources :

Example 1 with MoveToObjectAction

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

use of spacesettlers.actions.MoveToObjectAction 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 3 with MoveToObjectAction

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

the class AggressiveFlagCollectorTeamClient 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);
        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 asteroid
    if (current == null || current.isMovementFinished(space)) {
        aimingForBase.put(ship.getId(), false);
        Asteroid asteroid = pickHighestValueNearestFreeAsteroid(space, ship);
        AbstractAction newAction = null;
        if (asteroid != null) {
            asteroidToShipMap.put(asteroid.getId(), ship);
            newAction = new MoveToObjectAction(space, currentPosition, asteroid, asteroid.getPosition().getTranslationalVelocity());
        }
        return newAction;
    }
    return ship.getCurrentAction();
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Position(spacesettlers.utilities.Position) Beacon(spacesettlers.objects.Beacon) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction) Base(spacesettlers.objects.Base) MoveToObjectAction(spacesettlers.actions.MoveToObjectAction)

Example 4 with MoveToObjectAction

use of spacesettlers.actions.MoveToObjectAction 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 5 with MoveToObjectAction

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

the class BeaconCollectorTeamClient method getMovementStart.

/**
 * Send each ship to a beacon
 */
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;
            AbstractAction current = ship.getCurrentAction();
            // does the ship have a beacon it is aiming for?
            if (current == null || current.isMovementFinished(space) || !shipToBeaconMap.containsKey(ship)) {
                Position currentPosition = ship.getPosition();
                Beacon beacon = pickNearestFreeBeacon(space, ship);
                AbstractAction newAction = null;
                if (beacon == null) {
                    // there is no beacon available so do nothing
                    newAction = new DoNothingAction();
                } else {
                    beaconToShipMap.put(beacon, ship);
                    shipToBeaconMap.put(ship, beacon);
                    Position newGoal = beacon.getPosition();
                    newAction = new MoveToObjectAction(space, currentPosition, beacon);
                }
                actions.put(ship.getId(), newAction);
            } else {
                actions.put(ship.getId(), ship.getCurrentAction());
            }
        } else {
            // it is a base and Beacon collector doesn't do anything to bases
            actions.put(actionable.getId(), new DoNothingAction());
        }
    }
    return actions;
}
Also used : HashMap(java.util.HashMap) Position(spacesettlers.utilities.Position) AbstractObject(spacesettlers.objects.AbstractObject) Beacon(spacesettlers.objects.Beacon) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction) MoveToObjectAction(spacesettlers.actions.MoveToObjectAction)

Aggregations

AbstractAction (spacesettlers.actions.AbstractAction)11 DoNothingAction (spacesettlers.actions.DoNothingAction)11 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)11 Base (spacesettlers.objects.Base)10 Position (spacesettlers.utilities.Position)9 Beacon (spacesettlers.objects.Beacon)8 Ship (spacesettlers.objects.Ship)7 Asteroid (spacesettlers.objects.Asteroid)5 HashMap (java.util.HashMap)4 UUID (java.util.UUID)4 AbstractObject (spacesettlers.objects.AbstractObject)4 AiCore (spacesettlers.objects.AiCore)4 Flag (spacesettlers.objects.Flag)2