use of spacesettlers.actions.MoveAction 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;
}
use of spacesettlers.actions.MoveAction in project spacesettlers by amymcgovern.
the class TestMoveAction method testpdControlMoveToAlongY.
/**
* Test moving to the goal along the y dimension
* (50, 40)
* (50, 50)
* (50,60)
*
* @throws SpaceSettlersActionException
*/
@Test
public void testpdControlMoveToAlongY() throws SpaceSettlersActionException {
// first positive y (50, 60)
Position currentLoc = new Position(50, 50);
Position goalLoc = new Position(50, 60);
currentLoc.setOrientation(Math.PI / 2);
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(), Math.PI / 2, 0.01);
assertEquals(currentLoc.getX(), 50, 0.05);
assertEquals(currentLoc.getY(), 60, 0.05);
// then to the negative y
currentLoc = new Position(50, 50);
currentLoc.setOrientation(0);
goalLoc = new Position(50, 40);
currentLoc.setOrientation(-Math.PI / 2);
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 / 2, 0.01);
assertEquals(currentLoc.getX(), 50, 0.05);
assertEquals(currentLoc.getY(), 40, 0.05);
}
use of spacesettlers.actions.MoveAction 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;
}
use of spacesettlers.actions.MoveAction 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);
}
use of spacesettlers.actions.MoveAction 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);
}
Aggregations