Search in sources :

Example 1 with Flag

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

use of spacesettlers.objects.Flag 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 3 with Flag

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

the class PacifistFlagCollectorTeamClient 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 4 with Flag

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

the class PacifistFlagCollectorTeamClient 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;
    // 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);
    }
    // resources (as long as it isn't the flagShip)
    for (AbstractObject actionable : actionableObjects) {
        if (actionable instanceof Ship) {
            Ship ship = (Ship) actionable;
            AbstractAction action;
            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 {
                action = getAsteroidCollectorAction(space, ship);
            }
            // 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 5 with Flag

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

the class SpaceSettlersSimulator method initializeSimulation.

/**
 * Initialize the simulation given a configuration file.  Creates all the objects.
 * @throws SimulatorException
 */
void initializeSimulation(JSAPResult parserConfig) throws SimulatorException {
    simulatedSpace = new Toroidal2DPhysics(simConfig);
    // place the beacons
    for (int b = 0; b < simConfig.getNumBeacons(); b++) {
        Beacon beacon = new Beacon(simulatedSpace.getRandomFreeLocation(random, Beacon.BEACON_RADIUS * 2));
        // System.out.println("New beacon at " + beacon.getPosition());
        simulatedSpace.addObject(beacon);
    }
    // place any fixed location asteroids
    FixedAsteroidConfig[] fixedAsteroidConfigs = simConfig.getFixedAsteroids();
    if (fixedAsteroidConfigs != null) {
        for (FixedAsteroidConfig fixedAsteroidConfig : fixedAsteroidConfigs) {
            Asteroid asteroid = createNewFixedAsteroid(fixedAsteroidConfig);
            simulatedSpace.addObject(asteroid);
        }
    }
    // place the asteroids
    RandomAsteroidConfig randomAsteroidConfig = simConfig.getRandomAsteroids();
    for (int a = 0; a < randomAsteroidConfig.getNumberInitialAsteroids(); a++) {
        Asteroid asteroid = createNewRandomAsteroid(randomAsteroidConfig);
        simulatedSpace.addObject(asteroid);
    }
    // create the clients
    for (HighLevelTeamConfig teamConfig : simConfig.getTeams()) {
        // ensure this team isn't a duplicate
        if (clientMap.containsKey(teamConfig.getTeamName())) {
            throw new SimulatorException("Error: duplicate team name " + teamConfig.getTeamName());
        }
        TeamClientConfig teamClientConfig = getTeamClientConfig(teamConfig, parserConfig.getString("configPath"));
        // grab the home base config for this team (to get starting locations as needed)
        BaseConfig thisBaseConfig = null;
        for (BaseConfig baseConfig : simConfig.getBases()) {
            String teamName = baseConfig.getTeamName();
            if (teamName.equalsIgnoreCase(teamConfig.getTeamName())) {
                thisBaseConfig = baseConfig;
                break;
            }
        }
        // now either use the base config for the default region radius or the teamConfig file
        if (thisBaseConfig != null && thisBaseConfig.isFixedLocation()) {
            teamConfig.setInitialRegionULX(thisBaseConfig.getBoundingBoxULX());
            teamConfig.setInitialRegionULY(thisBaseConfig.getBoundingBoxULY());
            teamConfig.setInitialRegionLRX(thisBaseConfig.getBoundingBoxLRX());
            teamConfig.setInitialRegionLRY(thisBaseConfig.getBoundingBoxLRY());
            System.out.println("Initial provided for team " + teamConfig.getTeamName() + "UL (x,y) = " + teamConfig.getInitialRegionULX() + ", " + teamConfig.getInitialRegionULY() + " LR (x,y) = " + teamConfig.getInitialRegionLRX() + ", " + teamConfig.getInitialRegionLRY());
        } else {
            // if the team doesn't provide default radiii and bases, create one
            if (teamConfig.getInitialRegionULX() == 0 && teamConfig.getInitialRegionLRX() == 0) {
                teamConfig.setInitialRegionULX(random.nextInt(simConfig.getWidth()));
                teamConfig.setInitialRegionLRX(teamConfig.getInitialRegionULX() + simConfig.getWidth() / 4);
                teamConfig.setInitialRegionULY(random.nextInt(simConfig.getHeight()));
                teamConfig.setInitialRegionLRY(teamConfig.getInitialRegionULX() + simConfig.getHeight() / 4);
                System.out.println("Initial location not provided for team " + teamConfig.getTeamName() + "...generating: UL (x,y) = " + teamConfig.getInitialRegionULX() + ", " + teamConfig.getInitialRegionULY() + " LR (x,y) = " + teamConfig.getInitialRegionLRX() + ", " + teamConfig.getInitialRegionLRY());
            }
        }
        TeamClient teamClient = createTeamClient(teamConfig, teamClientConfig);
        // make the team inside the simulator for this team
        Team team = createTeam(teamConfig, teamClient, teamClientConfig);
        for (Ship ship : team.getShips()) {
            simulatedSpace.addObject(ship);
        }
        clientMap.put(teamConfig.getTeamName(), teamClient);
    }
    // make sure the base count matches the team count
    if (simConfig.getTeams().length != simConfig.getBases().length) {
        throw new SimulatorException("Error: You specified " + simConfig.getTeams().length + " teams and " + simConfig.getBases().length + " bases.  They must match.");
    }
    // create the bases and ensure there is a base for each team
    for (BaseConfig baseConfig : simConfig.getBases()) {
        String teamName = baseConfig.getTeamName();
        if (!clientMap.containsKey(teamName)) {
            throw new SimulatorException("Error: base is listed as team " + teamName + " but there is no corresponding team");
        }
        TeamClient teamClient = clientMap.get(teamName);
        // find the team config for this team
        HighLevelTeamConfig thisTeamConfig = null;
        for (HighLevelTeamConfig teamConfig : simConfig.getTeams()) {
            if (teamConfig.getTeamName().equalsIgnoreCase(teamName)) {
                thisTeamConfig = teamConfig;
                break;
            }
        }
        // make the location based on fixed or random
        Position baseLocation;
        if (baseConfig.isFixedLocation()) {
            baseLocation = new Position(baseConfig.getX(), baseConfig.getY());
        } else {
            // make the base in the region specified for this team
            // ensure bases are not created right next to asteroids (free by 4 * base_radius for now)
            baseLocation = simulatedSpace.getRandomFreeLocationInRegion(random, 4 * Base.BASE_RADIUS, thisTeamConfig.getInitialRegionULX(), thisTeamConfig.getInitialRegionULY(), thisTeamConfig.getInitialRegionLRX(), thisTeamConfig.getInitialRegionLRY());
        }
        // get this team as well as the client
        Team thisTeam = null;
        for (Team team : teams) {
            if (team.getTeamName().equalsIgnoreCase(teamName)) {
                thisTeam = team;
                break;
            }
        }
        Base base = new Base(baseLocation, baseConfig.getTeamName(), thisTeam, true);
        simulatedSpace.addObject(base);
        thisTeam.addBase(base);
    }
    /**
     * If there are flags specified (presumably for capture the flag games), create them
     * and match their color to their team.  Randomly choose their starting location
     * from the specified set of starting locations.
     */
    if (simConfig.getFlags() != null) {
        for (FlagConfig flagConfig : simConfig.getFlags()) {
            // get the right team to match the flag
            Team thisTeam = null;
            for (Team team : teams) {
                if (team.getTeamName().equalsIgnoreCase(flagConfig.getTeamName())) {
                    thisTeam = team;
                    break;
                }
            }
            int[] startX = flagConfig.getStartX();
            int[] startY = flagConfig.getStartY();
            Position[] startingPositions = new Position[startX.length];
            for (int i = 0; i < startX.length; i++) {
                startingPositions[i] = new Position(startX[i], startY[i]);
            }
            // System.out.println("Starting Locations are " + startingPositions);
            Position flagPosition = startingPositions[random.nextInt(startingPositions.length)];
            // System.out.println("Chosen location is " + flagPosition);
            Flag flag = new Flag(flagPosition, flagConfig.getTeamName(), thisTeam, startingPositions);
            simulatedSpace.addObject(flag);
        }
    }
}
Also used : TeamClient(spacesettlers.clients.TeamClient) Position(spacesettlers.utilities.Position) Flag(spacesettlers.objects.Flag) Base(spacesettlers.objects.Base) Asteroid(spacesettlers.objects.Asteroid) Beacon(spacesettlers.objects.Beacon) Team(spacesettlers.clients.Team) Ship(spacesettlers.objects.Ship)

Aggregations

Flag (spacesettlers.objects.Flag)6 AbstractObject (spacesettlers.objects.AbstractObject)5 Ship (spacesettlers.objects.Ship)5 Base (spacesettlers.objects.Base)3 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 AbstractAction (spacesettlers.actions.AbstractAction)2 DoNothingAction (spacesettlers.actions.DoNothingAction)2 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)2 Asteroid (spacesettlers.objects.Asteroid)2 Beacon (spacesettlers.objects.Beacon)2 Position (spacesettlers.utilities.Position)2 Team (spacesettlers.clients.Team)1 TeamClient (spacesettlers.clients.TeamClient)1 Vector2D (spacesettlers.utilities.Vector2D)1