use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class PacifistHeuristicAsteroidCollectorTeamClient method getAsteroidCollectorAction.
/**
* Gets the action for the asteroid collecting ship
* @param space
* @param ship
* @return
*/
private AbstractAction getAsteroidCollectorAction(Toroidal2DPhysics space, Ship ship) {
AbstractAction current = ship.getCurrentAction();
Position currentPosition = ship.getPosition();
// aim for a beacon if there isn't enough energy
if (ship.getEnergy() < 2000) {
Beacon beacon = pickNearestBeacon(space, ship);
AbstractAction newAction = null;
// if there is no beacon, then just skip a turn
if (beacon == null) {
newAction = new DoNothingAction();
} else {
newAction = new MoveToObjectAction(space, currentPosition, beacon);
}
aimingForBase.put(ship.getId(), false);
return newAction;
}
// if the ship has enough resourcesAvailable, take it back to base
if (ship.getResources().getTotal() > 500) {
Base base = findNearestBase(space, ship);
AbstractAction newAction = new MoveToObjectAction(space, currentPosition, base);
aimingForBase.put(ship.getId(), true);
return newAction;
}
// did we bounce off the base?
if (ship.getResources().getTotal() == 0 && ship.getEnergy() > 2000 && aimingForBase.containsKey(ship.getId()) && aimingForBase.get(ship.getId())) {
current = null;
aimingForBase.put(ship.getId(), false);
}
// otherwise aim for the asteroid
if (current == null || current.isMovementFinished(space)) {
aimingForBase.put(ship.getId(), false);
Asteroid asteroid = pickHighestValueNearestFreeAsteroid(space, ship);
AbstractAction newAction = null;
/*if (asteroid == null) {
// there is no asteroid available so collect a beacon
Beacon beacon = pickNearestBeacon(space, ship);
// if there is no beacon, then just skip a turn
if (beacon == null) {
newAction = new DoNothingAction();
} else {
newAction = new MoveToObjectAction(space, currentPosition, beacon);
}
} else {
asteroidToShipMap.put(asteroid.getId(), ship);
newAction = new MoveToObjectAction(space, currentPosition, asteroid);
}*/
if (asteroid != null) {
asteroidToShipMap.put(asteroid.getId(), ship);
newAction = new MoveToObjectAction(space, currentPosition, asteroid, asteroid.getPosition().getTranslationalVelocity());
}
return newAction;
}
return ship.getCurrentAction();
}
use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class Team method getTeamMovementEnd.
/**
* Allows the client to do cleanup after an action and before
* the next one (if needed)
*
* @param simulatedSpace
* @return
*/
public void getTeamMovementEnd(Toroidal2DPhysics space) {
final Toroidal2DPhysics clonedSpace = space.deepClone();
final Set<AbstractActionableObject> clonedActionableObjects = getTeamActionableObjectsClone(space);
// if the previous thread call hasn't finished, then just return default
if (executor == null || executor.isTerminated()) {
executor = Executors.newSingleThreadExecutor();
} else {
return;
}
// System.out.println("exec " + executor.isTerminated());
Future<Boolean> future = executor.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
teamClient.getMovementEnd(clonedSpace, clonedActionableObjects);
return true;
}
});
Boolean didReturn = false;
try {
// start
didReturn = future.get(SpaceSettlersSimulator.TEAM_END_ACTION_TIMEOUT, TimeUnit.MILLISECONDS);
// finished in time
} catch (TimeoutException e) {
// was terminated
// set didReturn false
System.out.println(getTeamName() + " timed out in getTeamMovementEnd");
didReturn = false;
} catch (InterruptedException e) {
// we were interrupted (should not happen but lets be good programmers)
// set didReturn false
didReturn = false;
e.printStackTrace();
} catch (ExecutionException e) {
// the executor threw and exception (should not happen but lets be good programmers)
// set didReturn false
didReturn = false;
e.printStackTrace();
} catch (RejectedExecutionException e) {
System.err.println("exec" + executor.isTerminated());
e.printStackTrace();
} catch (Exception e) {
// we shouldn't do this but it seems necessary to make
// the agent behave (do nothing) if it crashes
didReturn = false;
System.err.println("Error in agent. Printing stack trace.");
e.printStackTrace();
}
executor.shutdownNow();
// figure out how many beacons the team has collected
// figure out how many hitsInflicted and killsInflicted the team has
int beacons = 0;
int hits = 0;
int killsInflicted = 0;
int killsReceived = 0;
int damageInflicted = 0;
int damagedReceived = 0;
for (Ship ship : teamShips) {
beacons += ship.getNumBeacons();
hits += ship.getHitsInflicted();
killsInflicted += ship.getKillsInflicted();
killsReceived += ship.getKillsReceived();
damageInflicted += ship.getDamageInflicted();
// check team ships for how much damageInflicted they have received
damagedReceived += ship.getDamageReceived();
}
// check the bases for how much damageInflicted they have received
for (UUID baseID : teamBaseIDs) {
Base base = (Base) space.getObjectById(baseID);
damagedReceived += base.getDamageReceived();
killsReceived += base.getKillsReceived();
}
setTotalBeacons(beacons);
this.totalKillsInflicted = killsInflicted;
this.totalKillsReceived = killsReceived;
this.totalHitsInflicted = hits;
this.totalDamageInflicted = damageInflicted;
this.totalDamageReceived = damagedReceived;
}
use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class AggressiveHeuristicAsteroidCollectorSingletonTeamClient method findNearestBase.
/**
* Find the base for this team nearest to this ship
*
* @param space
* @param ship
* @return
*/
private Base findNearestBase(Toroidal2DPhysics space, Ship ship) {
double minDistance = Double.MAX_VALUE;
Base nearestBase = null;
for (Base base : space.getBases()) {
if (base.getTeamName().equalsIgnoreCase(ship.getTeamName())) {
double dist = space.findShortestDistance(ship.getPosition(), base.getPosition());
if (dist < minDistance) {
minDistance = dist;
nearestBase = base;
}
}
}
return nearestBase;
}
use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class AggressiveHeuristicAsteroidCollectorSingletonTeamClient 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?
double maxDistance = Double.MIN_VALUE;
for (Base base : bases) {
if (base.getTeamName().equalsIgnoreCase(getTeamName())) {
double distance = space.findShortestDistance(ship.getPosition(), base.getPosition());
if (distance > maxDistance) {
maxDistance = distance;
}
}
}
if (maxDistance > BASE_BUYING_DISTANCE) {
purchases.put(ship.getId(), PurchaseTypes.BASE);
bought_base = true;
// System.out.println("Buying a base!!");
break;
}
}
}
}
// see if you can buy EMPs
if (purchaseCosts.canAfford(PurchaseTypes.POWERUP_EMP_LAUNCHER, resourcesAvailable)) {
for (AbstractActionableObject actionableObject : actionableObjects) {
if (actionableObject instanceof Ship) {
Ship ship = (Ship) actionableObject;
if (!ship.getId().equals(asteroidCollectorID) && !ship.isValidPowerup(PurchaseTypes.POWERUP_EMP_LAUNCHER.getPowerupMap())) {
purchases.put(ship.getId(), PurchaseTypes.POWERUP_EMP_LAUNCHER);
}
}
}
}
// 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;
}
use of spacesettlers.objects.Base in project spacesettlers by amymcgovern.
the class AggressiveHeuristicAsteroidCollectorTeamClient method getAsteroidCollectorAction.
/**
* Gets the action for the asteroid collecting ship
* @param space
* @param ship
* @return
*/
private AbstractAction getAsteroidCollectorAction(Toroidal2DPhysics space, Ship ship) {
AbstractAction current = ship.getCurrentAction();
Position currentPosition = ship.getPosition();
// aim for a beacon if there isn't enough energy
if (ship.getEnergy() < 2000) {
Beacon beacon = pickNearestBeacon(space, ship);
AbstractAction newAction = null;
// if there is no beacon, then just skip a turn
if (beacon == null) {
newAction = new DoNothingAction();
} else {
newAction = new MoveToObjectAction(space, currentPosition, beacon);
}
aimingForBase.put(ship.getId(), false);
goingForCore.put(ship.getId(), false);
return newAction;
}
// if the ship has enough resourcesAvailable, take it back to base
if (ship.getResources().getTotal() > 500 || ship.getNumCores() > 0) {
Base base = findNearestBase(space, ship);
AbstractAction newAction = new MoveToObjectAction(space, currentPosition, base);
aimingForBase.put(ship.getId(), true);
goingForCore.put(ship.getId(), false);
return newAction;
}
// did we bounce off the base?
if (ship.getResources().getTotal() == 0 && ship.getEnergy() > 2000 && aimingForBase.containsKey(ship.getId()) && aimingForBase.get(ship.getId())) {
current = null;
aimingForBase.put(ship.getId(), false);
goingForCore.put(ship.getId(), false);
}
// if there is a nearby core, go get it
AiCore nearbyCore = pickNearestCore(space, ship, 100);
if (nearbyCore != null) {
Position newGoal = nearbyCore.getPosition();
AbstractAction newAction = new MoveToObjectAction(space, currentPosition, nearbyCore);
aimingForBase.put(ship.getId(), false);
goingForCore.put(ship.getId(), true);
return newAction;
}
// otherwise aim for the asteroid
if (current == null || current.isMovementFinished(space)) {
aimingForBase.put(ship.getId(), false);
goingForCore.put(ship.getId(), false);
Asteroid asteroid = pickHighestValueNearestFreeAsteroid(space, ship);
AbstractAction newAction = null;
if (asteroid == null) {
// there is no asteroid available so collect a beacon
Beacon beacon = pickNearestBeacon(space, ship);
// if there is no beacon, then just skip a turn
if (beacon == null) {
newAction = new DoNothingAction();
} else {
newAction = new MoveToObjectAction(space, currentPosition, beacon);
}
} else {
asteroidToShipMap.put(asteroid.getId(), ship);
newAction = new MoveToObjectAction(space, currentPosition, asteroid, asteroid.getPosition().getTranslationalVelocity());
}
return newAction;
} else {
return ship.getCurrentAction();
}
}
Aggregations