Search in sources :

Example 6 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class PacifistFlagCollectorTeamClient 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) {
            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 7 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class RandomTeamClient method getMovementStart.

@Override
public Map<UUID, AbstractAction> getMovementStart(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
    HashMap<UUID, AbstractAction> randomActions = new HashMap<UUID, AbstractAction>();
    for (AbstractObject actionable : actionableObjects) {
        if (actionable instanceof Ship) {
            Ship ship = (Ship) actionable;
            AbstractAction current = ship.getCurrentAction();
            // if we finished, make a new spot in space to aim for
            if (current == null || current.isMovementFinished(space)) {
                Position currentPosition = ship.getPosition();
                Position newGoal = space.getRandomFreeLocationInRegion(random, Ship.SHIP_RADIUS, (int) currentPosition.getX(), (int) currentPosition.getY(), RANDOM_MOVE_RADIUS);
                MoveAction newAction = null;
                newAction = new MoveAction(space, currentPosition, newGoal);
                // System.out.println("Ship is at " + currentPosition + " and goal is " + newGoal);
                SpacewarGraphics graphic = new CircleGraphics(1, getTeamColor(), newGoal);
                graphics.add(graphic);
                // Vector2D shortVec = space.findShortestDistanceVector(currentPosition, newGoal);
                // LineShadow lineShadow = new LineShadow(currentPosition, newGoal, shortVec);
                // newShadows.add(lineShadow);
                randomActions.put(ship.getId(), newAction);
            } else {
                randomActions.put(ship.getId(), ship.getCurrentAction());
            }
        } else {
            // it is a base and random doesn't do anything to bases
            randomActions.put(actionable.getId(), new DoNothingAction());
        }
    }
    return randomActions;
}
Also used : MoveAction(spacesettlers.actions.MoveAction) SpacewarGraphics(spacesettlers.graphics.SpacewarGraphics) HashMap(java.util.HashMap) Position(spacesettlers.utilities.Position) AbstractObject(spacesettlers.objects.AbstractObject) CircleGraphics(spacesettlers.graphics.CircleGraphics) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction)

Example 8 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class JSpaceSettlersComponent method drawShadow.

/**
 * Handles drawing things in a tororodially wrapped world.  Code
 * comes from the spacewar 1 simulator.
 *
 * @param graphic SpacewarGraphic to be drawn
 * @param graphics java level Graphics object
 */
private void drawShadow(final SpacewarGraphics graphic, final Graphics2D graphics) {
    Position position = graphic.getActualLocation();
    // don't draw a graphic at a bad position
    if (position == null) {
        return;
    }
    double x = position.getX();
    double y = position.getY();
    if (x < graphic.getHalfWidth()) {
        // goes off screen to left
        graphic.setDrawLocation(new Position(x + width, y));
        graphic.draw(graphics);
        if (y < graphic.getHalfHeight()) {
            // also goes off screen to the bottom
            graphic.setDrawLocation(new Position(x, y + height));
            graphic.draw(graphics);
            graphic.setDrawLocation(new Position(x + width, y + height));
            graphic.draw(graphics);
        } else if (y >= height - graphic.getHalfHeight()) {
            // also goes off screen to the top
            graphic.setDrawLocation(new Position(x, y - height));
            graphic.draw(graphics);
            graphic.setDrawLocation(new Position(x + width, y - height));
            graphic.draw(graphics);
        }
    } else if (x >= width - graphic.getHalfWidth()) {
        // goes off screen to right
        graphic.setDrawLocation(new Position(x - width, y));
        graphic.draw(graphics);
        if (y < graphic.getHalfHeight()) {
            // goes off screen to bottom
            graphic.setDrawLocation(new Position(x, y + height));
            graphic.draw(graphics);
            graphic.setDrawLocation(new Position(x - width, y + height));
            graphic.draw(graphics);
        } else if (y >= height - graphic.getHalfHeight()) {
            // also goes off screen to the top
            graphic.setDrawLocation(new Position(x, y - height));
            graphic.draw(graphics);
            graphic.setDrawLocation(new Position(x - width, y - height));
            graphic.draw(graphics);
        }
    } else if (y < graphic.getHalfHeight()) {
        // goes off screen to bottom
        graphic.setDrawLocation(new Position(x, y + height));
        graphic.draw(graphics);
    } else if (y >= height - graphic.getHalfHeight()) {
        // goes off screen to top
        graphic.setDrawLocation(new Position(x, y - height));
        graphic.draw(graphics);
    }
    graphic.setDrawLocation(position);
    graphic.draw(graphics);
}
Also used : Position(spacesettlers.utilities.Position)

Example 9 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class CollisionHandler method translatePosition.

/**
 * Translates a position by its velocity*timeStep
 *
 * This code is a bug fix and is credited to Troy Southard, Spring 2016.
 *
 * @param space
 * @param position position to be translated
 * @param timestep how long to translate
 * @return the translated position
 */
private Position translatePosition(Toroidal2DPhysics space, Position position, double timeStep) {
    // new x,y coordinates
    double newX = position.getX() + (position.getTranslationalVelocityX() * timeStep);
    double newY = position.getY() + (position.getTranslationalVelocityY() * timeStep);
    Position newPosition = new Position(newX, newY, position.getOrientation());
    newPosition.setAngularVelocity(position.getAngularVelocity());
    newPosition.setTranslationalVelocity(position.getTranslationalVelocity());
    space.toroidalWrap(newPosition);
    return newPosition;
}
Also used : Position(spacesettlers.utilities.Position)

Example 10 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class SpaceSettlersSimulator method createTeam.

/**
 * Make the actual team (holds a pointer to the client)
 *
 * @param teamConfig
 * @param teamClient
 * @return
 */
public Team createTeam(HighLevelTeamConfig teamConfig, TeamClient teamClient, TeamClientConfig teamClientConfig) {
    // it succeeded!  Now make the team ships
    int numShips = Math.min(simConfig.getMaximumInitialShipsPerTeam(), teamClientConfig.getNumberInitialShipsInTeam());
    Team team = new Team(teamClient, teamClientConfig.getLadderName(), simConfig.getMaximumShipsPerTeam());
    for (int s = 0; s < numShips; s++) {
        // put the ships in the initial region for the team
        // moved this to ship radius * 4 to keep ships away from each other initially
        Position freeLocation = simulatedSpace.getRandomFreeLocationInRegion(random, Ship.SHIP_RADIUS * 4, teamConfig.getInitialRegionULX(), teamConfig.getInitialRegionULY(), teamConfig.getInitialRegionLRX(), teamConfig.getInitialRegionLRY());
        System.out.println("Starting ship for team " + team.getTeamName() + " in location " + freeLocation);
        Ship ship = new Ship(teamConfig.getTeamName(), team.getTeamColor(), freeLocation);
        team.addShip(ship);
    }
    teams.add(team);
    return team;
}
Also used : Position(spacesettlers.utilities.Position) Team(spacesettlers.clients.Team) Ship(spacesettlers.objects.Ship)

Aggregations

Position (spacesettlers.utilities.Position)54 Test (org.junit.Test)28 Vector2D (spacesettlers.utilities.Vector2D)26 Ship (spacesettlers.objects.Ship)18 Movement (spacesettlers.utilities.Movement)16 Asteroid (spacesettlers.objects.Asteroid)14 AbstractAction (spacesettlers.actions.AbstractAction)12 DoNothingAction (spacesettlers.actions.DoNothingAction)12 Base (spacesettlers.objects.Base)11 Beacon (spacesettlers.objects.Beacon)10 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)9 UUID (java.util.UUID)6 MoveAction (spacesettlers.actions.MoveAction)6 AbstractObject (spacesettlers.objects.AbstractObject)6 AiCore (spacesettlers.objects.AiCore)5 HashMap (java.util.HashMap)4 Team (spacesettlers.clients.Team)2 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)2 Flag (spacesettlers.objects.Flag)2 ResourcePile (spacesettlers.objects.resources.ResourcePile)2