Search in sources :

Example 11 with AbstractAction

use of spacesettlers.actions.AbstractAction in project spacesettlers by amymcgovern.

the class ExampleGAClient method getMovementStart.

@Override
public Map<UUID, AbstractAction> getMovementStart(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
    // make a hash map of actions to return
    HashMap<UUID, AbstractAction> actions = new HashMap<UUID, AbstractAction>();
    // this agent choses only between doNothing and moveToNearestAsteroid)
    for (AbstractObject actionable : actionableObjects) {
        if (actionable instanceof Ship) {
            Ship ship = (Ship) actionable;
            AbstractAction action;
            if (ship.getCurrentAction() == null || ship.getCurrentAction().isMovementFinished(space)) {
                ExampleGAState currentState = new ExampleGAState(space, ship);
                action = currentPolicy.getCurrentAction(space, ship, currentState, random);
            // System.out.println("New random action is " + action);
            } else {
                action = ship.getCurrentAction();
            }
            actions.put(ship.getId(), action);
        } else {
            // it is a base.  Heuristically decide when to use the shield (TODO)
            actions.put(actionable.getId(), new DoNothingAction());
        }
    }
    // System.out.println("actions are " + actions);
    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)

Example 12 with AbstractAction

use of spacesettlers.actions.AbstractAction 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);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResourcePile(spacesettlers.objects.resources.ResourcePile) AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) Movement(spacesettlers.utilities.Movement) Position(spacesettlers.utilities.Position) Base(spacesettlers.objects.Base) AiCore(spacesettlers.objects.AiCore) AbstractObject(spacesettlers.objects.AbstractObject) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction)

Example 13 with AbstractAction

use of spacesettlers.actions.AbstractAction 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 14 with AbstractAction

use of spacesettlers.actions.AbstractAction 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();
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Position(spacesettlers.utilities.Position) Beacon(spacesettlers.objects.Beacon) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction) Base(spacesettlers.objects.Base) MoveToObjectAction(spacesettlers.actions.MoveToObjectAction)

Example 15 with AbstractAction

use of spacesettlers.actions.AbstractAction 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

AbstractAction (spacesettlers.actions.AbstractAction)20 DoNothingAction (spacesettlers.actions.DoNothingAction)19 Ship (spacesettlers.objects.Ship)15 UUID (java.util.UUID)13 HashMap (java.util.HashMap)12 AbstractObject (spacesettlers.objects.AbstractObject)12 Position (spacesettlers.utilities.Position)12 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)11 Base (spacesettlers.objects.Base)11 Beacon (spacesettlers.objects.Beacon)8 Asteroid (spacesettlers.objects.Asteroid)6 AiCore (spacesettlers.objects.AiCore)5 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)3 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 MoveAction (spacesettlers.actions.MoveAction)2 Flag (spacesettlers.objects.Flag)2 LinkedHashSet (java.util.LinkedHashSet)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1