Search in sources :

Example 11 with Movement

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

the class AbstractAction method getMovement.

/**
 * Calls the abstract getMovement with supplied timeout for response. If
 * nothing is returned in that time, simple return DoNothingAction (new Movement())
 *
 * @return
 */
public Movement getMovement(Toroidal2DPhysics space, Ship ship, int timeout) {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Movement> future = executor.submit(new SpacewarActionCallable(this, space, ship));
    Movement movement = null;
    try {
        // start
        movement = future.get(timeout, TimeUnit.MILLISECONDS);
    // finished in time
    } catch (TimeoutException e) {
        // was terminated
        // return no movement
        movement = new Movement();
    } catch (InterruptedException e) {
        // we were interrupted (should not happen but lets be good programmers)
        // return no movement
        movement = new Movement();
        e.printStackTrace();
    } catch (ExecutionException e) {
        // the executor threw and exception (should not happen but lets be good programmers)
        // return no movement
        movement = new Movement();
    }
    executor.shutdownNow();
    return movement;
}
Also used : Movement(spacesettlers.utilities.Movement) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with Movement

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

the class TestMoveAction method testpdControlMoveToGoal.

/**
 * Test moving to the goal
 *
 * (40, 40)
 * 			(50,50)
 * (40, 60)
 *
 * @throws SpaceSettlersActionException
 */
@Test
public void testpdControlMoveToGoal() throws SpaceSettlersActionException {
    // first to -3pi/4
    Position currentLoc = new Position(50, 50);
    Position goalLoc = new Position(40, 40);
    currentLoc.setOrientation(-(3 * Math.PI) / 4);
    moveAction = new MoveAction();
    Vector2D accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    Movement movement = new Movement();
    while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
        movement.setTranslationalAcceleration(accel);
        currentLoc = space.applyMovement(currentLoc, movement, timestep);
        accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    }
    assertEquals(currentLoc.getOrientation(), -(3 * Math.PI) / 4, 0.01);
    assertEquals(currentLoc.getX(), 40, 0.05);
    assertEquals(currentLoc.getY(), 40, 0.05);
    // then to 3pi/4
    currentLoc = new Position(50, 50);
    currentLoc.setOrientation(0);
    goalLoc = new Position(40, 60);
    currentLoc.setOrientation((3 * Math.PI) / 4);
    accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
        movement.setTranslationalAcceleration(accel);
        currentLoc = space.applyMovement(currentLoc, movement, timestep);
        accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    }
    assertEquals(currentLoc.getOrientation(), (3 * Math.PI) / 4, 0.01);
    assertEquals(currentLoc.getX(), 40, 0.05);
    assertEquals(currentLoc.getY(), 60, 0.05);
}
Also used : MoveAction(spacesettlers.actions.MoveAction) Movement(spacesettlers.utilities.Movement) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Example 13 with Movement

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

the class TestMoveAction method testpdControlMoveToAlongX.

/**
 * Test moving to the goal
 *
 * (30, 50) 	(50,50)  (70, 50)
 *
 * @throws SpaceSettlersActionException
 */
@Test
public void testpdControlMoveToAlongX() throws SpaceSettlersActionException {
    // first positive x (60,50)
    Position currentLoc = new Position(50, 50);
    Position goalLoc = new Position(70, 50);
    currentLoc.setOrientation(0);
    moveAction = new MoveAction();
    Vector2D accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    Movement movement = new Movement();
    while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
        movement.setTranslationalAcceleration(accel);
        currentLoc = space.applyMovement(currentLoc, movement, timestep);
        accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    }
    assertEquals(currentLoc.getOrientation(), 0, 0.01);
    assertEquals(currentLoc.getX(), 70, 0.05);
    assertEquals(currentLoc.getY(), 50, 0.05);
    // then to the negative x
    currentLoc = new Position(50, 50);
    currentLoc.setOrientation(0);
    goalLoc = new Position(30, 50);
    currentLoc.setOrientation(-Math.PI);
    accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
        movement.setTranslationalAcceleration(accel);
        currentLoc = space.applyMovement(currentLoc, movement, timestep);
        accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    }
    assertEquals(currentLoc.getOrientation(), -Math.PI, 0.01);
    assertEquals(currentLoc.getX(), 30, 0.05);
    assertEquals(currentLoc.getY(), 50, 0.05);
}
Also used : MoveAction(spacesettlers.actions.MoveAction) Movement(spacesettlers.utilities.Movement) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Example 14 with Movement

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

the class TestMoveAction method testpdControlOrientToGoal.

/**
 * Test orienting in both directions
 *
 * (40, 40)
 * 			(50,50)
 * (40, 60)
 *
 * @throws SpaceSettlersActionException
 */
@Test
public void testpdControlOrientToGoal() throws SpaceSettlersActionException {
    // first to -3pi/4
    Position currentLoc = new Position(50, 50);
    Position goalLoc = new Position(40, 40);
    moveAction = new MoveAction();
    double accel = moveAction.pdControlOrientToGoal(space, goalLoc, currentLoc, 0);
    Movement movement = new Movement();
    while (Math.abs(accel) > MoveAction.TARGET_REACHED_ACCEL) {
        movement.setAngularAccleration(accel);
        currentLoc = space.applyMovement(currentLoc, movement, timestep);
        accel = moveAction.pdControlOrientToGoal(space, goalLoc, currentLoc, 0);
    }
    assertEquals(currentLoc.getOrientation(), -(3 * Math.PI) / 4, 0.01);
    // then to 3pi/4
    currentLoc = new Position(50, 50);
    currentLoc.setOrientation(0);
    goalLoc = new Position(40, 60);
    accel = moveAction.pdControlOrientToGoal(space, goalLoc, currentLoc, 0);
    movement = new Movement();
    while (Math.abs(accel) > MoveAction.TARGET_REACHED_ACCEL) {
        movement.setAngularAccleration(accel);
        currentLoc = space.applyMovement(currentLoc, movement, timestep);
        accel = moveAction.pdControlOrientToGoal(space, goalLoc, currentLoc, 0);
    }
    assertEquals(currentLoc.getOrientation(), (3 * Math.PI) / 4, 0.01);
}
Also used : MoveAction(spacesettlers.actions.MoveAction) Movement(spacesettlers.utilities.Movement) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Example 15 with Movement

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

the class TestMoveActionWithOrientation method testpdControlMoveToAlongX.

/**
 * Test moving to the goal
 *
 * (30, 50) 	(50,50)  (70, 50)
 *
 * @throws SpaceSettlersActionException
 */
@Test
public void testpdControlMoveToAlongX() throws SpaceSettlersActionException {
    // first positive x (60,50)
    Position currentLoc = new Position(50, 50);
    Position goalLoc = new Position(70, 50);
    currentLoc.setOrientation(0);
    moveAction = new MoveActionWithOrientation();
    Vector2D accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    Movement movement = new Movement();
    while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
        movement.setTranslationalAcceleration(accel);
        currentLoc = space.applyMovement(currentLoc, movement, timestep);
        accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    }
    assertEquals(currentLoc.getOrientation(), 0, 0.01);
    assertEquals(currentLoc.getX(), 70, 0.05);
    assertEquals(currentLoc.getY(), 50, 0.05);
    // then to the negative x
    currentLoc = new Position(50, 50);
    currentLoc.setOrientation(0);
    goalLoc = new Position(30, 50);
    currentLoc.setOrientation(-Math.PI);
    accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
        movement.setTranslationalAcceleration(accel);
        currentLoc = space.applyMovement(currentLoc, movement, timestep);
        accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
    }
    assertEquals(currentLoc.getOrientation(), -Math.PI, 0.01);
    assertEquals(currentLoc.getX(), 30, 0.05);
    assertEquals(currentLoc.getY(), 50, 0.05);
}
Also used : Movement(spacesettlers.utilities.Movement) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Aggregations

Movement (spacesettlers.utilities.Movement)20 Position (spacesettlers.utilities.Position)16 Test (org.junit.Test)15 Vector2D (spacesettlers.utilities.Vector2D)15 MoveAction (spacesettlers.actions.MoveAction)4 LinkedHashSet (java.util.LinkedHashSet)1 UUID (java.util.UUID)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 AbstractAction (spacesettlers.actions.AbstractAction)1 DoNothingAction (spacesettlers.actions.DoNothingAction)1 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)1 AbstractObject (spacesettlers.objects.AbstractObject)1 AiCore (spacesettlers.objects.AiCore)1 Base (spacesettlers.objects.Base)1 Ship (spacesettlers.objects.Ship)1 ResourcePile (spacesettlers.objects.resources.ResourcePile)1