Search in sources :

Example 26 with Ship

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;
}
Also used : HashMap(java.util.HashMap) Position(spacesettlers.utilities.Position) RawAction(spacesettlers.actions.RawAction) LineGraphics(spacesettlers.graphics.LineGraphics) MoveAction(spacesettlers.actions.MoveAction) Vector2D(spacesettlers.utilities.Vector2D) AbstractObject(spacesettlers.objects.AbstractObject) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) StarGraphics(spacesettlers.graphics.StarGraphics) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction)

Example 27 with Ship

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;
            }
        }
    }
}
Also used : AbstractObject(spacesettlers.objects.AbstractObject) Ship(spacesettlers.objects.Ship)

Example 28 with Ship

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;
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) Base(spacesettlers.objects.Base)

Example 29 with Ship

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;
}
Also used : AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) HashMap(java.util.HashMap) PurchaseTypes(spacesettlers.actions.PurchaseTypes) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) Base(spacesettlers.objects.Base)

Example 30 with Ship

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;
}
Also used : HashMap(java.util.HashMap) AbstractObject(spacesettlers.objects.AbstractObject) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction)

Aggregations

Ship (spacesettlers.objects.Ship)49 UUID (java.util.UUID)20 Base (spacesettlers.objects.Base)19 Position (spacesettlers.utilities.Position)18 HashMap (java.util.HashMap)16 AbstractAction (spacesettlers.actions.AbstractAction)15 DoNothingAction (spacesettlers.actions.DoNothingAction)15 AbstractObject (spacesettlers.objects.AbstractObject)15 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)9 Asteroid (spacesettlers.objects.Asteroid)9 Vector2D (spacesettlers.utilities.Vector2D)8 Test (org.junit.Test)7 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)7 PurchaseTypes (spacesettlers.actions.PurchaseTypes)7 Beacon (spacesettlers.objects.Beacon)7 AiCore (spacesettlers.objects.AiCore)6 Flag (spacesettlers.objects.Flag)5 Team (spacesettlers.clients.Team)4 ResourcePile (spacesettlers.objects.resources.ResourcePile)4 ExecutionException (java.util.concurrent.ExecutionException)2