use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.
the class HumanTeamClient method getMovementStart.
/**
* Look at the last key pressed by the human and do its movement
*/
@Override
public Map<UUID, AbstractAction> getMovementStart(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
HashMap<UUID, AbstractAction> actions = new HashMap<UUID, AbstractAction>();
for (AbstractObject actionable : actionableObjects) {
if (actionable instanceof Ship) {
// get the current position
Ship ship = (Ship) actionable;
Position myPosition = ship.getPosition();
Vector2D currentVelocity = myPosition.getTranslationalVelocity();
RawAction action = null;
double angularVel = myPosition.getAngularVelocity();
// if the key was up or down, accelerate along the current line
if (lastKeyPressed == HumanKeyPressed.UP) {
Vector2D newVel = new Vector2D(HUMAN_ACCEL * Math.cos(myPosition.getOrientation()), HUMAN_ACCEL * Math.sin(myPosition.getOrientation()));
newVel.add(currentVelocity);
action = new RawAction(newVel, 0);
} else if (lastKeyPressed == HumanKeyPressed.DOWN) {
Vector2D newVel = new Vector2D(-HUMAN_ACCEL * Math.cos(myPosition.getOrientation()), -HUMAN_ACCEL * Math.sin(myPosition.getOrientation()));
newVel.add(currentVelocity);
action = new RawAction(newVel, 0);
}
// if the key was right or left, turn
if (lastKeyPressed == HumanKeyPressed.RIGHT) {
action = new RawAction(0, HUMAN_TURN_ACCEL);
} else if (lastKeyPressed == HumanKeyPressed.LEFT) {
action = new RawAction(0, -HUMAN_TURN_ACCEL);
}
// was the mouse clicked?
if (lastMouseClick != null) {
if (mouseClickMove == null || mouseClickMove.isMovementFinished(space) || space.findShortestDistance(lastMouseClick, myPosition) > CLICK_DISTANCE) {
mouseClickMove = new MoveAction(space, myPosition, lastMouseClick);
graphicsToAdd.add(new StarGraphics(3, super.teamColor, lastMouseClick));
LineGraphics line = new LineGraphics(myPosition, lastMouseClick, space.findShortestDistanceVector(myPosition, lastMouseClick));
line.setLineColor(super.teamColor);
graphicsToAdd.add(line);
}
actions.put(actionable.getId(), mouseClickMove);
} else {
actions.put(actionable.getId(), action);
}
} else {
// can't really control anything but the ship
actions.put(actionable.getId(), new DoNothingAction());
}
}
return actions;
}
use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.
the class HumanTeamClient method getMovementEnd.
@Override
public void getMovementEnd(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
// reset so the human has to press again to move again (otherwise
// it does strange things like fly when you don't tell it anything on
// acceleration!)
lastKeyPressed = HumanKeyPressed.NONE;
for (AbstractObject actionable : actionableObjects) {
if (actionable instanceof Ship) {
Ship ship = (Ship) actionable;
if (!ship.isAlive()) {
lastMouseClick = null;
mouseClickMove = null;
}
}
}
}
use of spacesettlers.objects.Ship 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.Ship 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;
}
use of spacesettlers.objects.Ship 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;
}
Aggregations