use of spacesettlers.objects.AbstractObject in project spacesettlers by amymcgovern.
the class PacifistHeuristicAsteroidCollectorTeamClient 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>();
// loop through each ship
for (AbstractObject actionable : actionableObjects) {
if (actionable instanceof Ship) {
Ship ship = (Ship) actionable;
AbstractAction action;
action = getAsteroidCollectorAction(space, ship);
actions.put(ship.getId(), action);
} else {
// it is a base. Heuristically decide when to use the shield (TODO)
actions.put(actionable.getId(), new DoNothingAction());
}
}
return actions;
}
use of spacesettlers.objects.AbstractObject in project spacesettlers by amymcgovern.
the class AggressiveHeuristicAsteroidCollectorTeamClient 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>();
// loop through each ship
for (AbstractObject actionable : actionableObjects) {
if (actionable instanceof Ship) {
Ship ship = (Ship) actionable;
// the first time we initialize, decide which ship is the asteroid collector
if (asteroidCollectorID == null) {
asteroidCollectorID = ship.getId();
}
AbstractAction action;
if (ship.getId().equals(asteroidCollectorID)) {
// get the asteroids
action = getAsteroidCollectorAction(space, ship);
} else {
// this ship will try to shoot other ships so its movements take it towards the nearest other ship not on our team
action = getWeaponShipAction(space, ship);
}
actions.put(ship.getId(), action);
} else {
// it is a base. Heuristically decide when to use the shield (TODO)
actions.put(actionable.getId(), new DoNothingAction());
}
}
return actions;
}
use of spacesettlers.objects.AbstractObject 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());
// }
// }
}
use of spacesettlers.objects.AbstractObject in project spacesettlers by amymcgovern.
the class Toroidal2DPhysics method respawnDeadObjects.
/**
* Respawns any dead objects in new random locations. Ships
* have a delay before they can respawn.
*/
public void respawnDeadObjects(Random random, double asteroidMaxVelocity) {
for (AbstractObject object : allObjects) {
if (!object.isAlive() && object.canRespawn()) {
Position newPosition = null;
// flags should re-spawn at a randomly chosen starting location
if (object instanceof Flag) {
Flag flag = (Flag) object;
newPosition = flag.getNewStartingPosition(random);
// ensure their starting location is free (to handle the thought bug the class
// introduced of putting a ship or a base where the flag should spawn)
newPosition = getRandomFreeLocationInRegion(random, flag.getRadius() * 2, (int) newPosition.getX(), (int) newPosition.getY(), 75);
} else {
// note this is time 2 in order to ensure objects don't spawn touching (and just to get
// them a bit farther apart
newPosition = getRandomFreeLocation(random, object.getRadius() * 2);
}
object.setPosition(newPosition);
object.setAlive(true);
object.setDrawable(true);
// reset the UUID if it is a asteroid or beacon
if (object instanceof Asteroid || object instanceof Beacon) {
object.resetId();
}
// make moveable asteroids move again when they respawn
if (object.isMoveable() && !(object instanceof Flag)) {
Vector2D randomMotion = Vector2D.getRandom(random, asteroidMaxVelocity);
object.getPosition().setTranslationalVelocity(randomMotion);
}
}
}
}
use of spacesettlers.objects.AbstractObject in project spacesettlers by amymcgovern.
the class JSpaceSettlersComponent method paintComponent.
/**
* Draw the space background and all the sub components
*/
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
final Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// handle a race condition in the GUI
if (scaleTransform == null) {
return;
}
graphics.transform(scaleTransform);
// draw space
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.width, this.height);
// handle an annoying race condition in the GUI
if (simulator == null) {
return;
}
// draw graphic for all the objects
Set<AbstractObject> allObjects = new LinkedHashSet<AbstractObject>(simulator.getAllObjects());
for (AbstractObject object : allObjects) {
SpacewarGraphics graphic = object.getGraphic();
if (graphic != null) {
if (graphic.isDrawable()) {
drawShadow(graphic, graphics);
}
}
}
// and draw any team graphics from this round
for (Team team : simulator.getTeams()) {
Set<SpacewarGraphics> teamShadows = team.getGraphics();
if (teamShadows != null) {
for (SpacewarGraphics graphic : teamShadows) {
if (graphic.isDrawable()) {
drawShadow(graphic, graphics);
}
}
}
}
}
Aggregations