use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.
the class Team method deepCopy.
/**
* Make a deep copy for security
*
* @return
*/
public Team deepCopy() {
Team newTeam = new Team(teamClient, ladderName, maxNumberShips);
for (Ship ship : teamShips) {
newTeam.addShip(ship.deepClone());
}
newTeam.teamBaseIDs.addAll(this.teamBaseIDs);
newTeam.costToPurchase = costToPurchase.deepCopy();
newTeam.totalResources = new ResourcePile(totalResources);
newTeam.availableResources = new ResourcePile(availableResources);
newTeam.totalHitsInflicted = this.totalHitsInflicted;
newTeam.totalKillsInflicted = this.totalKillsInflicted;
newTeam.totalKillsReceived = this.totalKillsReceived;
newTeam.totalDamageInflicted = this.totalDamageInflicted;
newTeam.totalDamageReceived = this.totalDamageReceived;
newTeam.totalFlagsCollected = this.totalFlagsCollected;
newTeam.totalCoresCollected = this.totalCoresCollected;
return newTeam;
}
use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.
the class CollisionHandler method missileCollision.
/**
* Collide with a missile
* @param object1
* @param object2
*/
private void missileCollision(Missile missile, AbstractObject object2) {
// get the ship that fired this
Ship firingShip = missile.getFiringShip();
firingShip.decrementWeaponCount();
// did it hit a ship?
if (object2.getClass() == Ship.class) {
Ship ship = (Ship) object2;
// only take damageInflicted if not shielded
if (!ship.isShielded()) {
double initialEnergy = ship.getEnergy();
ship.updateEnergy(missile.getDamage());
if (ship.getEnergy() <= 0) {
// if you killed the ship, only count the final amount of damage needed to kill it
firingShip.incrementDamageInflicted((int) initialEnergy);
ship.incrementDamageReceived(-(int) initialEnergy);
} else {
// otherwise a missile is a fixed amount of damage
firingShip.incrementDamageInflicted(-missile.getDamage());
ship.incrementDamageReceived(missile.getDamage());
}
// it hit a ship
firingShip.incrementHitsInflicted();
}
// if the bullet killed the ship, credit the ship that hit it
if (ship.getEnergy() <= 0) {
// System.out.println("ship " + firingShip.getTeamName() + " stealing resourcesAvailable " + shipMoney + " from " + ship.getTeamName() + ship.getId());
// it killed a ship
firingShip.incrementKillsInflicted();
ship.incrementKillsReceived();
}
}
// did it hit a base?
if (object2.getClass() == Base.class) {
Base base = (Base) object2;
// only take damageInflicted if not shielded
if (!base.isShielded()) {
double initialEnergy = base.getEnergy();
base.updateEnergy(missile.getDamage());
if (base.getEnergy() <= 0) {
// if the base is dead, you can only count the energy it had prior to being dead
firingShip.incrementDamageInflicted((int) initialEnergy);
base.incrementDamageReceived(-(int) initialEnergy);
// System.out.println("Firing at a dead base - should give only " + (int) -initialEnergy + " in damage");
} else {
// otherwise the missles count constant
firingShip.incrementDamageInflicted(-missile.getDamage());
base.incrementDamageReceived(missile.getDamage());
}
// it hit a base
firingShip.incrementHitsInflicted();
}
}
// Handle a bullet hitting a bullet
if (object2.getClass() == Missile.class) {
object2.setAlive(false);
Ship otherFiringShip = ((Missile) object2).getFiringShip();
otherFiringShip.decrementWeaponCount();
}
// Did the missile hit an AiCore? If so, damage the AiCore.
if (object2.getClass() == AiCore.class) {
AiCore core = (AiCore) object2;
core.updateEnergy(-missile.getDamage());
}
// make the missile die
missile.setAlive(false);
}
use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.
the class CollisionHandler method beaconCollision.
/**
* Collide with a beacon
* @param beacon
* @param object
*/
public void beaconCollision(Beacon beacon, AbstractObject object) {
// beacons die when they are touched (respawned elsewhere)
beacon.setAlive(false);
if (object.getClass() == Ship.class) {
Ship ship = (Ship) object;
ship.incrementBeaconCount();
ship.updateEnergy(Beacon.BEACON_ENERGY_BOOST);
}
}
use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.
the class CollisionHandler method EMPCollision.
/**
* Collide with a EMP
* @param object1
* @param object2
*/
private void EMPCollision(EMP emp, AbstractObject object2) {
// get the ship that fired
Ship firingShip = emp.getFiringShip();
firingShip.decrementWeaponCount();
if (object2.getClass() == Ship.class) {
Ship ship = (Ship) object2;
// only take a hit if not shielded
if (!ship.isShielded()) {
ship.updateEnergy(emp.getDamage());
ship.incrementDamageReceived(emp.getDamage());
ship.setFreezeCount(emp.getFreezeCount());
// it hit a ship
firingShip.incrementHitsInflicted();
}
}
if (object2.getClass() == Base.class) {
Base base = (Base) object2;
// only take a hit if not shielded
if (!base.isShielded()) {
base.updateEnergy(emp.getDamage());
base.incrementDamageReceived(emp.getDamage());
base.setFreezeCount(emp.getFreezeCount());
// it hit a base
firingShip.incrementHitsInflicted();
}
}
// Handle a emp hitting a emp (no damageInflicted but both weapons die)
if (object2.getClass() == EMP.class) {
object2.setAlive(false);
Ship otherFiringShip = ((EMP) object2).getFiringShip();
otherFiringShip.decrementWeaponCount();
}
// Did the EMP hit an AiCore? If so, destroy the AiCore.
if (object2.getClass() == AiCore.class) {
AiCore core = (AiCore) object2;
// Kill the core!
core.setAlive(false);
}
emp.setAlive(false);
}
use of spacesettlers.objects.Ship 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