Search in sources :

Example 1 with TeamClient

use of spacesettlers.clients.TeamClient 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 2 with TeamClient

use of spacesettlers.clients.TeamClient in project spacesettlers by amymcgovern.

the class SpaceSettlersSimulator method createTeamClient.

/**
 * Make the team client from the configuration file
 *
 * @param teamConfig
 * @return
 * @throws SimulatorException
 */
@SuppressWarnings("unchecked")
public TeamClient createTeamClient(HighLevelTeamConfig teamConfig, TeamClientConfig teamClientConfig) throws SimulatorException {
    try {
        // make a team client of the class specified in the config file
        Class<TeamClient> newTeamClass = (Class<TeamClient>) Class.forName(teamClientConfig.getClassname());
        TeamClient newTeamClient = (TeamClient) newTeamClass.newInstance();
        Color teamColor = new Color(teamClientConfig.getTeamColorRed(), teamClientConfig.getTeamColorGreen(), teamClientConfig.getTeamColorBlue());
        newTeamClient.setTeamColor(teamColor);
        newTeamClient.setTeamName(teamConfig.getTeamName());
        newTeamClient.setKnowledgeFile(teamClientConfig.getKnowledgeFile());
        newTeamClient.setRandom(random);
        newTeamClient.setMaxNumberShips(simConfig.getMaximumShipsPerTeam());
        newTeamClient.initialize(simulatedSpace.deepClone());
        return newTeamClient;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new SimulatorException("Unable to make a new team client " + teamClientConfig.getClassname());
    } catch (InstantiationException e) {
        e.printStackTrace();
        throw new SimulatorException("Unable to create a new instance of class " + teamClientConfig.getClassname());
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        throw new SimulatorException("Unable to create a new instance of class " + teamClientConfig.getClassname());
    }
}
Also used : TeamClient(spacesettlers.clients.TeamClient) Color(java.awt.Color)

Aggregations

TeamClient (spacesettlers.clients.TeamClient)2 Color (java.awt.Color)1 Team (spacesettlers.clients.Team)1 Asteroid (spacesettlers.objects.Asteroid)1 Base (spacesettlers.objects.Base)1 Beacon (spacesettlers.objects.Beacon)1 Flag (spacesettlers.objects.Flag)1 Ship (spacesettlers.objects.Ship)1 Position (spacesettlers.utilities.Position)1