Search in sources :

Example 11 with Position

use of spacesettlers.utilities.Position 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 12 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class SpaceSettlersSimulator method createNewFixedAsteroid.

/**
 * Create a new fixed location asteroid following all rules of the config files
 *
 * Fixed asteroids specify a x, y location and a radius.  They are always non-mineable.
 *
 * @param asteroidConfig
 * @return
 */
private Asteroid createNewFixedAsteroid(FixedAsteroidConfig asteroidConfig) {
    boolean mineable = false;
    boolean moveable = false;
    int radius = asteroidConfig.getRadius();
    // create the asteroid (no fuels in it either)
    Asteroid asteroid = new Asteroid(new Position(asteroidConfig.getX(), asteroidConfig.getY()), mineable, radius, moveable, 0, 0, 0);
    return asteroid;
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Position(spacesettlers.utilities.Position)

Example 13 with Position

use of spacesettlers.utilities.Position 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)

Example 14 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class Toroidal2DPhysics method applyMovement.

/**
 * Takes an acceleration and a simulation time step and moves the object
 *
 * @param actionMovement
 * @param timeStep
 * @return
 */
public Position applyMovement(Position position, Movement movement, double timeStep) {
    double translationalAccelX = movement.getTranslationalAcceleration().getXValue();
    double translationalAccelY = movement.getTranslationalAcceleration().getYValue();
    double angularAccel = movement.getAngularAccleration();
    // velocity is acceleration times time
    double translationalVelocityX = position.getTranslationalVelocityX() + (translationalAccelX * timeStep);
    double translationalVelocityY = position.getTranslationalVelocityY() + (translationalAccelY * timeStep);
    double angularVelocity = position.getAngularVelocity() + (angularAccel * timeStep);
    // ensure the max/mins are respected
    translationalVelocityX = checkTranslationalVelocity(translationalVelocityX);
    translationalVelocityY = checkTranslationalVelocity(translationalVelocityY);
    angularVelocity = checkAngularVelocity(angularVelocity);
    Position newPosition = new Position(position.getX(), position.getY(), position.getOrientation());
    newPosition.setTranslationalVelocity(new Vector2D(translationalVelocityX, translationalVelocityY));
    newPosition.setAngularVelocity(angularVelocity);
    return moveOneTimestep(newPosition);
}
Also used : Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position)

Example 15 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class Toroidal2DPhysics method moveOneTimestep.

/**
 * Advances one time step using the set velocities
 * @param currentPosition
 * @return
 */
private Position moveOneTimestep(Position position) {
    double angularVelocity = position.getAngularVelocity();
    double orientation = position.getOrientation() + (angularVelocity * timeStep);
    // make sure orientation wraps correctly (-pi to pi)
    if (orientation > Math.PI) {
        orientation -= (2 * Math.PI);
    } else if (orientation < -Math.PI) {
        orientation += (2 * Math.PI);
    }
    // new x,y coordinates
    double newX = position.getX() + (position.getTranslationalVelocityX() * timeStep);
    double newY = position.getY() + (position.getTranslationalVelocityY() * timeStep);
    Position newPosition = new Position(newX, newY, orientation);
    newPosition.setAngularVelocity(angularVelocity);
    newPosition.setTranslationalVelocity(position.getTranslationalVelocity());
    toroidalWrap(newPosition);
    return newPosition;
}
Also used : Position(spacesettlers.utilities.Position)

Aggregations

Position (spacesettlers.utilities.Position)54 Test (org.junit.Test)28 Vector2D (spacesettlers.utilities.Vector2D)26 Ship (spacesettlers.objects.Ship)18 Movement (spacesettlers.utilities.Movement)16 Asteroid (spacesettlers.objects.Asteroid)14 AbstractAction (spacesettlers.actions.AbstractAction)12 DoNothingAction (spacesettlers.actions.DoNothingAction)12 Base (spacesettlers.objects.Base)11 Beacon (spacesettlers.objects.Beacon)10 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)9 UUID (java.util.UUID)6 MoveAction (spacesettlers.actions.MoveAction)6 AbstractObject (spacesettlers.objects.AbstractObject)6 AiCore (spacesettlers.objects.AiCore)5 HashMap (java.util.HashMap)4 Team (spacesettlers.clients.Team)2 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)2 Flag (spacesettlers.objects.Flag)2 ResourcePile (spacesettlers.objects.resources.ResourcePile)2