Search in sources :

Example 16 with Ship

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

the class ExampleGAClient method getMovementStart.

@Override
public Map<UUID, AbstractAction> getMovementStart(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
    // make a hash map of actions to return
    HashMap<UUID, AbstractAction> actions = new HashMap<UUID, AbstractAction>();
    // this agent choses only between doNothing and moveToNearestAsteroid)
    for (AbstractObject actionable : actionableObjects) {
        if (actionable instanceof Ship) {
            Ship ship = (Ship) actionable;
            AbstractAction action;
            if (ship.getCurrentAction() == null || ship.getCurrentAction().isMovementFinished(space)) {
                ExampleGAState currentState = new ExampleGAState(space, ship);
                action = currentPolicy.getCurrentAction(space, ship, currentState, random);
            // System.out.println("New random action is " + action);
            } else {
                action = ship.getCurrentAction();
            }
            actions.put(ship.getId(), action);
        } else {
            // it is a base.  Heuristically decide when to use the shield (TODO)
            actions.put(actionable.getId(), new DoNothingAction());
        }
    }
    // System.out.println("actions are " + actions);
    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 17 with Ship

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

the class CollisionHandler method baseCollision.

/**
 * Collide into the base
 *
 * @param base
 * @param object
 */
public void baseCollision(Base base, AbstractObject object) {
    if (object.getClass() == Ship.class) {
        Ship ship = (Ship) object;
        if (ship.getTeamName().equalsIgnoreCase(base.getTeamName())) {
            // deposit the resources
            base.addResources(ship.getResources());
            ship.resetResources();
            // deposit the flag (if there is one)
            if (ship.isCarryingFlag()) {
                base.addFlag(ship.getFlag());
                ship.depositFlag();
            }
            // deposit any AI Cores
            base.incrementCores(ship.getNumCores());
            ship.resetAiCores();
            // heal the ship
            double origEnergy = ship.getEnergy();
            ship.updateEnergy(base.getHealingEnergy());
            double energyChange = ship.getEnergy() - origEnergy;
            base.updateEnergy(-(int) energyChange);
        // System.out.println("ship " + ship.getTeamName() + ship.getId() + " left resourcesAvailable at base and now has resourcesAvailable " + ship.getMoney());
        }
    }
}
Also used : Ship(spacesettlers.objects.Ship)

Example 18 with Ship

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

the class SpaceSettlersSimulator method createTeam.

/**
 * Make the actual team (holds a pointer to the client)
 *
 * @param teamConfig
 * @param teamClient
 * @return
 */
public Team createTeam(HighLevelTeamConfig teamConfig, TeamClient teamClient, TeamClientConfig teamClientConfig) {
    // it succeeded!  Now make the team ships
    int numShips = Math.min(simConfig.getMaximumInitialShipsPerTeam(), teamClientConfig.getNumberInitialShipsInTeam());
    Team team = new Team(teamClient, teamClientConfig.getLadderName(), simConfig.getMaximumShipsPerTeam());
    for (int s = 0; s < numShips; s++) {
        // put the ships in the initial region for the team
        // moved this to ship radius * 4 to keep ships away from each other initially
        Position freeLocation = simulatedSpace.getRandomFreeLocationInRegion(random, Ship.SHIP_RADIUS * 4, teamConfig.getInitialRegionULX(), teamConfig.getInitialRegionULY(), teamConfig.getInitialRegionLRX(), teamConfig.getInitialRegionLRY());
        System.out.println("Starting ship for team " + team.getTeamName() + " in location " + freeLocation);
        Ship ship = new Ship(teamConfig.getTeamName(), team.getTeamColor(), freeLocation);
        team.addShip(ship);
    }
    teams.add(team);
    return team;
}
Also used : Position(spacesettlers.utilities.Position) Team(spacesettlers.clients.Team) Ship(spacesettlers.objects.Ship)

Example 19 with Ship

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

Example 20 with Ship

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

the class SpaceSettlersSimulator method handlePurchases.

/**
 * Handle purchases for a team
 *
 * @param team
 * @param purchases
 */
private void handlePurchases(Team team, Map<UUID, PurchaseTypes> purchases) {
    // handle teams that don't purchase
    if (purchases == null) {
        return;
    }
    for (UUID key : purchases.keySet()) {
        PurchaseTypes purchase = purchases.get(key);
        // skip the purchase if there isn't enough resourcesAvailable
        if (!team.canAfford(purchase)) {
            continue;
        }
        // get the object where the item is to be purchased (on on whom it is to be purchased)
        AbstractActionableObject purchasingObject = (AbstractActionableObject) simulatedSpace.getObjectById(key);
        // can only make purchases for your team
        if (!purchasingObject.getTeamName().equalsIgnoreCase(team.getTeamName())) {
            continue;
        }
        switch(purchase) {
            case BASE:
                // only purchase if this is a ship (can't buy a base next to a base)
                if (purchasingObject instanceof Ship) {
                    Ship ship = (Ship) purchasingObject;
                    // set the base just away from the ship (to avoid a collision)
                    Position newPosition = simulatedSpace.getRandomFreeLocationInRegion(random, Base.BASE_RADIUS, (int) ship.getPosition().getX(), (int) ship.getPosition().getY(), (6 * (ship.getRadius() + Base.BASE_RADIUS)));
                    // make the new base and add it to the lists
                    Base base = new Base(newPosition, team.getTeamName(), team, false);
                    simulatedSpace.addObject(base);
                    team.addBase(base);
                    // charge the team for the purchase
                    team.decrementAvailableResources(team.getCurrentCost(purchase));
                    team.updateCost(purchase);
                }
                break;
            case SHIP:
                // can only buy if there are enough ships
                if (team.getShips().size() >= team.getMaxNumberShips())
                    break;
                // Ships can only be purchased near a base (which launches them)
                if (purchasingObject instanceof Base) {
                    Base base = (Base) purchasingObject;
                    // set the new ship just away from the base (to avoid a collision)
                    Position newPosition = simulatedSpace.getRandomFreeLocationInRegion(random, Ship.SHIP_RADIUS, (int) base.getPosition().getX(), (int) base.getPosition().getY(), (10 * (base.getRadius() + Ship.SHIP_RADIUS)));
                    // make the new ship and add it to the lists
                    Ship ship = new Ship(team.getTeamName(), team.getTeamColor(), newPosition);
                    simulatedSpace.addObject(ship);
                    team.addShip(ship);
                    // charge the team for the purchase
                    team.decrementAvailableResources(team.getCurrentCost(purchase));
                    team.updateCost(purchase);
                }
                break;
            case POWERUP_SHIELD:
                purchasingObject.addPowerup(SpaceSettlersPowerupEnum.TOGGLE_SHIELD);
                // charge the team for the purchase
                team.decrementAvailableResources(team.getCurrentCost(purchase));
                team.updateCost(purchase);
                System.out.println("Buying a shield");
                break;
            case POWERUP_EMP_LAUNCHER:
                // only purchase if this is a ship (can't buy a base next to a base)
                if (purchasingObject instanceof Ship) {
                    purchasingObject.addPowerup(SpaceSettlersPowerupEnum.FIRE_EMP);
                    // charge the team for the purchase
                    team.decrementAvailableResources(team.getCurrentCost(purchase));
                    team.updateCost(purchase);
                    System.out.println("Buying a emp launcher");
                }
                break;
            case POWERUP_DOUBLE_BASE_HEALING_SPEED:
                // this can only be purchased on bases
                if (purchasingObject instanceof Base) {
                    purchasingObject.addPowerup(SpaceSettlersPowerupEnum.DOUBLE_BASE_HEALING_SPEED);
                    // charge the team for the purchase
                    team.decrementAvailableResources(team.getCurrentCost(purchase));
                    team.updateCost(purchase);
                    System.out.println("Buying a healing doubler for a base");
                }
                break;
            case POWERUP_DOUBLE_MAX_ENERGY:
                purchasingObject.addPowerup(SpaceSettlersPowerupEnum.DOUBLE_MAX_ENERGY);
                // charge the team for the purchase
                team.decrementAvailableResources(team.getCurrentCost(purchase));
                team.updateCost(purchase);
                System.out.println("Buying a energy doubler");
                break;
            case POWERUP_DOUBLE_WEAPON_CAPACITY:
                purchasingObject.addPowerup(SpaceSettlersPowerupEnum.DOUBLE_WEAPON_CAPACITY);
                // charge the team for the purchase
                team.decrementAvailableResources(team.getCurrentCost(purchase));
                team.updateCost(purchase);
                System.out.println("Buying a weapons doubler");
                break;
            case NOTHING:
                break;
            default:
                break;
        }
    }
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) Position(spacesettlers.utilities.Position) 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