use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class Toroidal2DPhysics method advanceTime.
/**
* Move all moveable objects and handle power ups.
*/
public void advanceTime(int currentTimeStep, Map<UUID, SpaceSettlersPowerupEnum> powerups) {
this.currentTimeStep = currentTimeStep;
// heal any base injuries
for (Base base : bases) {
base.updateEnergy(base.getHealingIncrement());
}
// detect collisions across all objects
detectCollisions();
// get the power ups and create any objects (weapons) as necessary
for (UUID key : powerups.keySet()) {
AbstractObject swobject = getObjectById(key);
// if the object is not alive or it is not actionable, then ignore this
if (!swobject.isAlive() || (!(swobject instanceof AbstractActionableObject))) {
continue;
}
// otherwise, handle the power up
handlePowerup((AbstractActionableObject) swobject, powerups.get(key));
}
// now move all objects that are moveable (which may include weapons)
for (AbstractObject object : allObjects) {
// skip non-moveable objects or dead object
if (!object.isMoveable() || !object.isAlive()) {
continue;
}
Position currentPosition = object.getPosition();
// is it a ship that can be controlled?
if (object.isControllable()) {
Ship ship = (Ship) object;
AbstractAction action = ship.getCurrentAction();
// handle a null action
if (action == null) {
action = new DoNothingAction();
}
// need to clone the ship and space because otherwise the ship can affect
// itself inside AbstractAction
Movement actionMovement = action.getMovement(this.deepClone(), ship.deepClone());
Position newPosition = applyMovement(currentPosition, actionMovement, timeStep);
if (newPosition.isValid()) {
ship.setPosition(newPosition);
} else {
ship.setPosition(currentPosition);
}
// spend ship energy proportional to its acceleration (old formula used velocity) and mass (new for space settlers
// since resources cost mass)
// double penalty = ENERGY_PENALTY * -Math.abs(ship.getPosition().getTotalTranslationalVelocity());
double angularAccel = Math.abs(actionMovement.getAngularAccleration());
double angularInertia = (3.0 * ship.getMass() * ship.getRadius() * angularAccel) / 2.0;
double linearAccel = actionMovement.getTranslationalAcceleration().getMagnitude();
double linearInertia = ship.getMass() * linearAccel;
int penalty = (int) Math.floor(ENERGY_PENALTY * (angularInertia + linearInertia));
ship.updateEnergy(-penalty);
// this isn't the most general fix but it will work for now (also has to be done for bases)
if (ship.isShielded()) {
ship.updateEnergy(-PowerupToggleShield.SHIELD_STEP_COST);
}
// if (!ship.isAlive()) {
// System.out.println("Ship " + ship.getTeamName() + ship.getId() + " is dead");
// }
} else {
// move all other types of objects
Position newPosition = moveOneTimestep(currentPosition);
object.setPosition(newPosition);
}
// if any ships or bases are frozen, decrement their frozen count
if (object instanceof AbstractActionableObject && !object.isControllable()) {
AbstractActionableObject actionable = (AbstractActionableObject) object;
actionable.decrementFreezeCount();
}
}
// go through and see if any bases have died
Set<Base> basesClone = new LinkedHashSet<Base>(bases);
for (Base base : basesClone) {
// this isn't the most general fix but it will work for now (also has to be done for bases)
if (base.isShielded()) {
base.updateEnergy(-PowerupToggleShield.SHIELD_STEP_COST);
}
if (!base.isAlive()) {
base.setAlive(false);
removeObject(base);
base.getTeam().removeBase(base);
}
}
// from when it was called inside updateEnergy
for (Ship ship : ships) {
if (ship.getEnergy() <= 0 && ship.isAlive() == true) {
// drop any resources that the ship was carrying
ResourcePile resources = ship.getResources();
// Spawn a new AiCore with the same velocity magnitude and direction as its parent ship.
// handle dropping the core if the ship died
Position corePosition = ship.getPosition();
corePosition.setTranslationalVelocity(ship.getPosition().getTranslationalVelocity());
corePosition.setAngularVelocity(ship.getPosition().getAngularVelocity());
AiCore shipCore = new AiCore(corePosition, ship.getTeamName(), ship.getTeamColor());
this.addObject(shipCore);
if (resources.getTotal() > 0) {
// Position newPosition = ship.getPosition();
// newPosition.setTranslationalVelocity(new Vector2D(0,0));
// newPosition.setAngularVelocity(0.0);
// Asteroid newAsteroid = new Asteroid(newPosition, true, ship.getRadius(), true, resources);
// this.addObject(newAsteroid);
// distributeResourcesToNearbyAsteroids(ship.getPosition(), resources);
// System.out.println("Adding a new asteroid with resources " + newAsteroid.getResources().getTotal() +
// " due to death, total is " + asteroids.size());
// System.out.println("Ship died and " + resources.getTotal() + " has been added to an asteroid");
}
// set the ship to dead last (so we can grab its resources first)
ship.setAlive(false);
}
}
}
use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class ResourcesPanel method updateData.
public void updateData(AbstractObject object) {
ResourcePile avail = null;
ResourcePile total = null;
if (object.getClass() == Asteroid.class) {
Asteroid asteroid = (Asteroid) object;
avail = asteroid.getResources();
total = avail;
} else if (object.getClass() == Ship.class) {
Ship ship = (Ship) object;
avail = ship.getResources();
total = avail;
} else if (object.getClass() == Base.class) {
Base base = (Base) object;
avail = base.getResources();
total = base.getResources();
} else if (object.getClass() == Beacon.class) {
Beacon beacon = (Beacon) object;
}
if (avail != null) {
waterAvail.setText("" + avail.getResourceQuantity(ResourceTypes.WATER));
fuelAvail.setText("" + avail.getResourceQuantity(ResourceTypes.FUEL));
metalsAvail.setText("" + avail.getResourceQuantity(ResourceTypes.METALS));
waterTotal.setText("" + total.getResourceQuantity(ResourceTypes.WATER));
fuelTotal.setText("" + total.getResourceQuantity(ResourceTypes.FUEL));
metalsTotal.setText("" + total.getResourceQuantity(ResourceTypes.METALS));
} else {
waterAvail.setText("0");
fuelAvail.setText("0");
metalsAvail.setText("0");
waterTotal.setText("0");
fuelTotal.setText("0");
metalsTotal.setText("0");
}
}
use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class PowerupDoubleHealingBaseEnergy method applyPowerup.
/**
* Double the healing increment
*/
@Override
public void applyPowerup(AbstractActionableObject actionableObject) {
Base base = (Base) actionableObject;
base.setHealingIncrement(base.getHealingIncrement() * 2);
base.removePowerup(getType());
}
use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class PacifistFlagCollectorTeamClient method getTeamPurchases.
@Override
public /**
* If there is enough resourcesAvailable, buy a base. Place it by finding a ship that is sufficiently
* far away from the existing bases
*/
Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects, ResourcePile resourcesAvailable, PurchaseCosts purchaseCosts) {
HashMap<UUID, PurchaseTypes> purchases = new HashMap<UUID, PurchaseTypes>();
double BASE_BUYING_DISTANCE = 200;
boolean bought_base = false;
int numBases, numShips;
// count the number of ships for the base/ship buying algorithm
numShips = 0;
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
numShips++;
}
}
// try to balance
if (purchaseCosts.canAfford(PurchaseTypes.BASE, resourcesAvailable)) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
Ship ship = (Ship) actionableObject;
Set<Base> bases = space.getBases();
// how far away is this ship to a base of my team?
boolean buyBase = true;
numBases = 0;
for (Base base : bases) {
if (base.getTeamName().equalsIgnoreCase(getTeamName())) {
numBases++;
double distance = space.findShortestDistance(ship.getPosition(), base.getPosition());
if (distance < BASE_BUYING_DISTANCE) {
buyBase = false;
}
}
}
if (buyBase && numBases < numShips) {
purchases.put(ship.getId(), PurchaseTypes.BASE);
bought_base = true;
System.out.println("Pacifist Flag Collector is buying a base!");
break;
}
}
}
}
// can I buy a ship?
if (purchaseCosts.canAfford(PurchaseTypes.SHIP, resourcesAvailable) && bought_base == false) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Base) {
Base base = (Base) actionableObject;
purchases.put(base.getId(), PurchaseTypes.SHIP);
System.out.println("Pacifist Flag Collector is buying a ship!");
break;
}
}
}
return purchases;
}
use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class PacifistHeuristicAsteroidCollectorTeamClient method getTeamPurchases.
@Override
public /**
* If there is enough resourcesAvailable, buy a base. Place it by finding a ship that is sufficiently
* far away from the existing bases
*/
Map<UUID, PurchaseTypes> getTeamPurchases(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects, ResourcePile resourcesAvailable, PurchaseCosts purchaseCosts) {
HashMap<UUID, PurchaseTypes> purchases = new HashMap<UUID, PurchaseTypes>();
double BASE_BUYING_DISTANCE = 200;
boolean bought_base = false;
if (purchaseCosts.canAfford(PurchaseTypes.BASE, resourcesAvailable)) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
Ship ship = (Ship) actionableObject;
Set<Base> bases = space.getBases();
// how far away is this ship to a base of my team?
boolean buyBase = true;
for (Base base : bases) {
if (base.getTeamName().equalsIgnoreCase(getTeamName())) {
double distance = space.findShortestDistance(ship.getPosition(), base.getPosition());
if (distance < BASE_BUYING_DISTANCE) {
buyBase = false;
}
}
}
if (buyBase) {
purchases.put(ship.getId(), PurchaseTypes.BASE);
bought_base = true;
// System.out.println("Buying a base!!");
break;
}
}
}
}
// can I buy a ship?
if (purchaseCosts.canAfford(PurchaseTypes.SHIP, resourcesAvailable) && bought_base == false) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Base) {
Base base = (Base) actionableObject;
purchases.put(base.getId(), PurchaseTypes.SHIP);
break;
}
}
}
return purchases;
}
Aggregations