Search in sources :

Example 31 with Position

use of spacesettlers.utilities.Position 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 32 with Position

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

Example 33 with Position

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

the class AggressiveHeuristicAsteroidCollectorTeamClient method getWeaponShipAction.

/**
 * Gets the action for the weapons based ship
 * @param space
 * @param ship
 * @return
 */
private AbstractAction getWeaponShipAction(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;
        goingForCore.put(ship.getId(), false);
        aimingForBase.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);
        goingForCore.put(ship.getId(), true);
        aimingForBase.put(ship.getId(), false);
        return newAction;
    }
    // otherwise aim for the nearest enemy ship
    if (current == null || current.isMovementFinished(space)) {
        aimingForBase.put(ship.getId(), false);
        goingForCore.put(ship.getId(), false);
        Ship enemy = pickNearestEnemyShip(space, ship);
        AbstractAction newAction = null;
        if (enemy == null) {
            // there is no enemy 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 {
            newAction = new MoveToObjectAction(space, currentPosition, enemy, enemy.getPosition().getTranslationalVelocity());
        }
        return newAction;
    } else {
        return ship.getCurrentAction();
    }
}
Also used : Position(spacesettlers.utilities.Position) AiCore(spacesettlers.objects.AiCore) Beacon(spacesettlers.objects.Beacon) Ship(spacesettlers.objects.Ship) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction) Base(spacesettlers.objects.Base) MoveToObjectAction(spacesettlers.actions.MoveToObjectAction)

Example 34 with Position

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

the class CollisionHandler method adjustCentersAtCollision.

/**
 * Adjusts the centers of object1 and object2 so that their
 * collision occurs when they are touching, not overlapping
 *
 * This code is a bug fix and is credited to Troy Southard, Spring 2016.
 *
 * @param object1
 * @param object2
 * @param space
 */
private void adjustCentersAtCollision(AbstractObject object1, AbstractObject object2, Toroidal2DPhysics space) {
    // position reference
    Position pos1 = object1.getPosition();
    Position pos2 = object2.getPosition();
    // x,y positions
    double x1 = pos1.getX();
    double x2 = pos2.getX();
    double y1 = pos1.getY();
    double y2 = pos2.getY();
    // x,y velocities
    // negative because time is being reversed
    double u1 = -pos1.getTranslationalVelocityX();
    double u2 = -pos2.getTranslationalVelocityX();
    double v1 = -pos1.getTranslationalVelocityY();
    double v2 = -pos2.getTranslationalVelocityY();
    // object radii
    double r1 = object1.getRadius();
    double r2 = object2.getRadius();
    // Find when radius1 + radius2 == Distance(object1, object2)
    // ==>
    // Solve[r1 + r2 == Sqrt[((x2 + u2*t)-(x1 + u1*t))^2 + ((y2 + v2*t)-(y1 + v1*t))^2], t]
    // ==>
    // Gives two results for t
    double tPlus = (-2 * u1 * x1 + 2 * u2 * x1 + 2 * u1 * x2 - 2 * u2 * x2 - 2 * v1 * y1 + 2 * v2 * y1 + 2 * v1 * y2 - 2 * v2 * y2 + // +
    Math.sqrt(Math.pow(2 * u1 * x1 - 2 * u2 * x1 - 2 * u1 * x2 + 2 * u2 * x2 + 2 * v1 * y1 - 2 * v2 * y1 - 2 * v1 * y2 + 2 * v2 * y2, 2) - 4 * (Math.pow(u1, 2) - 2 * u1 * u2 + Math.pow(u2, 2) + Math.pow(v1, 2) - 2 * v1 * v2 + Math.pow(v2, 2)) * (Math.pow(x1, 2) + Math.pow(x2, 2) + Math.pow(y1, 2) + Math.pow(y2, 2) - Math.pow(r1, 2) - Math.pow(r2, 2) - 2 * r1 * r2 - 2 * x1 * x2 - 2 * y1 * y2))) / (2 * (Math.pow(u1, 2) - 2 * u1 * u2 + Math.pow(u2, 2) + Math.pow(v1, 2) - 2 * v1 * v2 + Math.pow(v2, 2)));
    double tMinus = (-2 * u1 * x1 + 2 * u2 * x1 + 2 * u1 * x2 - 2 * u2 * x2 - 2 * v1 * y1 + 2 * v2 * y1 + 2 * v1 * y2 - 2 * v2 * y2 - // -
    Math.sqrt(Math.pow(2 * u1 * x1 - 2 * u2 * x1 - 2 * u1 * x2 + 2 * u2 * x2 + 2 * v1 * y1 - 2 * v2 * y1 - 2 * v1 * y2 + 2 * v2 * y2, 2) - 4 * (Math.pow(u1, 2) - 2 * u1 * u2 + Math.pow(u2, 2) + Math.pow(v1, 2) - 2 * v1 * v2 + Math.pow(v2, 2)) * (Math.pow(x1, 2) + Math.pow(x2, 2) + Math.pow(y1, 2) + Math.pow(y2, 2) - Math.pow(r1, 2) - Math.pow(r2, 2) - 2 * r1 * r2 - 2 * x1 * x2 - 2 * y1 * y2))) / (2 * (Math.pow(u1, 2) - 2 * u1 * u2 + Math.pow(u2, 2) + Math.pow(v1, 2) - 2 * v1 * v2 + Math.pow(v2, 2)));
    // determine which solution is correct
    // t must lie between 0 and the length of a simulator time step
    // Amy McGovern: added 2.0 * duration instead of just duration because of double precision issues
    double time = 0;
    // System.out.println("Time adjustment solutions tplus = " + tPlus + " tminus = " + tMinus);
    if (Math.abs(tPlus) < (2.0 * space.getTimestepDuration()))
        time = tPlus;
    else if (Math.abs(tMinus) < (2.0 * space.getTimestepDuration()))
        time = tMinus;
    // System.out.println("time adjustment is " + time);
    // adjusted object centers
    Position adjustedPos1 = translatePosition(space, pos1, -time);
    Position adjustedPos2 = translatePosition(space, pos2, -time);
    // set adjusted object centers
    object1.setPosition(adjustedPos1);
    object2.setPosition(adjustedPos2);
}
Also used : Position(spacesettlers.utilities.Position)

Example 35 with Position

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

the class Toroidal2DPhysics method respawnDeadObjects.

/**
 * Respawns any dead objects in new random locations.  Ships
 * have a delay before they can respawn.
 */
public void respawnDeadObjects(Random random, double asteroidMaxVelocity) {
    for (AbstractObject object : allObjects) {
        if (!object.isAlive() && object.canRespawn()) {
            Position newPosition = null;
            // flags should re-spawn at a randomly chosen starting location
            if (object instanceof Flag) {
                Flag flag = (Flag) object;
                newPosition = flag.getNewStartingPosition(random);
                // ensure their starting location is free (to handle the thought bug the class
                // introduced of putting a ship or a base where the flag should spawn)
                newPosition = getRandomFreeLocationInRegion(random, flag.getRadius() * 2, (int) newPosition.getX(), (int) newPosition.getY(), 75);
            } else {
                // note this is time 2 in order to ensure objects don't spawn touching (and just to get
                // them a bit farther apart
                newPosition = getRandomFreeLocation(random, object.getRadius() * 2);
            }
            object.setPosition(newPosition);
            object.setAlive(true);
            object.setDrawable(true);
            // reset the UUID if it is a asteroid or beacon
            if (object instanceof Asteroid || object instanceof Beacon) {
                object.resetId();
            }
            // make moveable asteroids move again when they respawn
            if (object.isMoveable() && !(object instanceof Flag)) {
                Vector2D randomMotion = Vector2D.getRandom(random, asteroidMaxVelocity);
                object.getPosition().setTranslationalVelocity(randomMotion);
            }
        }
    }
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) AbstractObject(spacesettlers.objects.AbstractObject) Beacon(spacesettlers.objects.Beacon) Flag(spacesettlers.objects.Flag)

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