use of spacesettlers.clients.Team in project spacesettlers by amymcgovern.
the class DamagePanel method updateData.
public void updateData(SpaceSettlersSimulator simulator, String teamName) {
Team team = null;
for (Team curTeam : simulator.getTeams()) {
if (curTeam.getLadderName().equalsIgnoreCase(teamName)) {
team = curTeam;
break;
}
}
damageInflicted.setText(team.getTotalDamageInflicted() + "");
damageReceived.setText(team.getTotalDamageReceived() + "");
killsInflicted.setText(team.getTotalKillsInflicted() + "");
killsReceived.setText(team.getTotalKillsReceived() + "");
}
use of spacesettlers.clients.Team 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;
}
use of spacesettlers.clients.Team 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);
}
}
}
use of spacesettlers.clients.Team in project spacesettlers by amymcgovern.
the class SpaceSettlersSimulator method run.
/**
* Main control loop of the simulator
* @throws SimulatorException
*/
public void run() throws SimulatorException {
if (gui != null) {
gui.redraw();
}
// if the pause is activated, just wait
for (timestep = 0; timestep < simConfig.getSimulationSteps(); timestep++) {
while (isPaused()) {
mySleep(50);
}
advanceTime();
if (gui != null) {
gui.redraw();
mySleep(graphicsSleep);
}
if (timestep % 5000 == 0) {
System.out.println("On time step " + timestep);
// print out the score every 5000 steps for debugging
for (Team team : teams) {
String str = "Team: " + team.getLadderName() + " scored " + team.getScore();
System.out.println(str);
}
}
}
// update the team info (to send into the space for use by other teams)
updateTeamInfo();
// shutdown all the teams
shutdownTeams();
}
use of spacesettlers.clients.Team in project spacesettlers by amymcgovern.
the class SpaceSettlersSimulator method advanceTime.
/**
* Advance time one step
*/
void advanceTime() {
// update the team info (to send into the space for use by other teams)
updateTeamInfo();
ExecutorService teamExecutor;
if (debug) {
teamExecutor = Executors.newSingleThreadExecutor();
} else {
teamExecutor = Executors.newCachedThreadPool();
}
Map<Team, Future<Map<UUID, AbstractAction>>> clientActionFutures = new HashMap<Team, Future<Map<UUID, AbstractAction>>>();
// get the actions from each team
for (Team team : teams) {
clientActionFutures.put(team, teamExecutor.submit(new AdvanceTimeCallable(team)));
}
for (Team team : teams) {
Map<UUID, AbstractAction> teamActions;
try {
teamActions = clientActionFutures.get(team).get();
} catch (InterruptedException e) {
// something went wrong...return empty map
teamActions = new HashMap<UUID, AbstractAction>();
} catch (ExecutionException e) {
// something went wrong...return empty map
teamActions = new HashMap<UUID, AbstractAction>();
}
// get the actions for each ship
for (Ship ship : team.getShips()) {
// if the client forgets to set an action, set it to DoNothing
if (teamActions == null || !teamActions.containsKey(ship.getId())) {
teamActions.put(ship.getId(), new DoNothingAction());
}
ship.setCurrentAction(teamActions.get(ship.getId()));
}
}
teamExecutor.shutdown();
// get the power ups being used on this turn
Map<UUID, SpaceSettlersPowerupEnum> allPowerups = new HashMap<UUID, SpaceSettlersPowerupEnum>();
for (Team team : teams) {
Map<UUID, SpaceSettlersPowerupEnum> powerups = team.getTeamPowerups(simulatedSpace);
if (powerups != null) {
for (UUID key : powerups.keySet()) {
// verify power ups belong to this team
if (!team.isValidTeamID(key)) {
continue;
}
// get the object and ensure it can have a power up on it
AbstractObject swObject = simulatedSpace.getObjectById(key);
if (!(swObject instanceof AbstractActionableObject)) {
continue;
}
// verify that the object has the power up associated with it
AbstractActionableObject actionableObject = (AbstractActionableObject) swObject;
if (actionableObject.isValidPowerup(powerups.get(key))) {
allPowerups.put(key, powerups.get(key));
}
}
}
}
// now update the physics on all objects
simulatedSpace.advanceTime(this.getTimestep(), allPowerups);
// and end any actions inside the team
for (Team team : teams) {
team.getTeamMovementEnd(simulatedSpace);
}
// handle purchases at the end of a turn (so ships will have movements next turn)
for (Team team : teams) {
// now get purchases for the team
Map<UUID, PurchaseTypes> purchases = team.getTeamPurchases(simulatedSpace);
handlePurchases(team, purchases);
}
// cleanup and remove dead weapons
simulatedSpace.cleanupDeadWeapons();
// cleanup and remove dead cores
simulatedSpace.cleanupDeadCores();
// respawn any objects that died (and that should respawn - this includes Flags)
final double asteroidMaxVelocity = simConfig.getRandomAsteroids().getMaxInitialVelocity();
simulatedSpace.respawnDeadObjects(random, asteroidMaxVelocity);
// spawn new asteroids with a small probability (up to the maximum number allowed)
int maxAsteroids = simConfig.getRandomAsteroids().getMaximumNumberAsteroids();
int numAsteroids = simulatedSpace.getAsteroids().size();
if (numAsteroids < maxAsteroids) {
if (random.nextDouble() < ASTEROID_SPAWN_PROBABILITY) {
// System.out.println("Spawning a new asteroid");
Asteroid asteroid = createNewRandomAsteroid(simConfig.getRandomAsteroids());
simulatedSpace.addObject(asteroid);
}
}
updateScores();
// for (Team team : teams) {
// for (Ship ship : team.getShips()) {
// System.out.println("Ship " + ship.getTeamName() + ship.getId() + " has resourcesAvailable " + ship.getMoney());
// }
// }
}
Aggregations