BattleField { public double wallSmoothing(Point2D.Double startLocation, double startAngle, int orientation, double wallStick) { double wallDistanceX = Math.min(startLocation.x - 18, width - startLocation.x - 18); double wallDistanceY = Math.min(startLocation.y - 18, height - startLocation.y - 18); if (wallDistanceX > wallStick && wallDistanceY > wallStick) { return startAngle; } double angle = startAngle; double testX = startLocation.x + (Math.sin(angle) * wallStick); double testY = startLocation.y + (Math.cos(angle) * wallStick); double testDistanceX = Math.min(testX - 18, width - testX - 18); double testDistanceY = Math.min(testY - 18, height - testY - 18); double adjacent = 0; int g = 0; while ((testDistanceX < 0 || testDistanceY < 0) && g++ < 25) { if (testDistanceY < 0 && testDistanceY < testDistanceX) { angle = (testY < 18) ? Math.PI : 0; adjacent = wallDistanceY; } else if (testDistanceX < 0 && testDistanceX <= testDistanceY) { angle = (testX < 18) ? (3 * HALF_PI) : HALF_PI; adjacent = wallDistanceX; } if (adjacent < 0) { if (-adjacent > wallStick) { wallStick += -adjacent; } angle += Math.PI - orientation * (Math.abs(Math.acos(-adjacent/wallStick)) - 0.0005); } else { angle += orientation * (Math.abs(Math.acos(adjacent/wallStick)) + 0.0005); } testX = startLocation.x + (Math.sin(angle) * wallStick); testY = startLocation.y + (Math.cos(angle) * wallStick); testDistanceX = Math.min(testX - 18, width - testX - 18); testDistanceY = Math.min(testY - 18, height - testY - 18); } return angle; } BattleField(double width, double height); Point2D.Double translateToField(Point2D.Double p); double orbitalWallDistance(Point2D.Double sourceLocation,
      Point2D.Double targetLocation, double bulletPower, int direction); double directToWallDistance(Point2D.Double targetLocation,
      double distance, double heading, double bulletPower); double wallSmoothing(Point2D.Double startLocation, double startAngle,
      int orientation, double wallStick); final Rectangle2D.Double rectangle; final double width; final double height; }
@Test public void testWallSmoothingEastWall() { BattleField battleField = newBattleField(800, 600); Double botLocation = newLocation(700, 300); double goAngle = Utils.normalAbsoluteAngle(battleField.wallSmoothing( botLocation, Math.PI / 2, 1, 120)); assertTrue(goAngle > Math.PI * 0.6 && goAngle < Math.PI); assertTrue(battleField.rectangle.contains( DiaUtils.project(botLocation, goAngle, 120))); }
@Test public void testWallSmoothingWestWall() { BattleField battleField = newBattleField(800, 600); Double botLocation = newLocation(100, 300); double goAngle = Utils.normalAbsoluteAngle(battleField.wallSmoothing( botLocation, 3 * Math.PI / 2, -1, 120)); assertTrue(goAngle < Math.PI * 1.4 && goAngle > Math.PI); assertTrue(battleField.rectangle.contains( DiaUtils.project(botLocation, goAngle, 120))); }
@Test public void testWallSmoothingSouthWall() { BattleField battleField = newBattleField(800, 600); Double botLocation = newLocation(400, 100); double goAngle = Utils.normalAbsoluteAngle(battleField.wallSmoothing( botLocation, Math.PI, 1, 120)); assertTrue(goAngle > Math.PI && goAngle < Math.PI * 1.5); assertTrue(battleField.rectangle.contains( DiaUtils.project(botLocation, goAngle, 120))); }
@Test public void testWallSmoothingNorthWall() { BattleField battleField = newBattleField(800, 600); Double botLocation = newLocation(400, 700); double goAngle = Utils.normalAbsoluteAngle(battleField.wallSmoothing( botLocation, 0, -1, 120)); assertTrue(goAngle < 2 * Math.PI && goAngle < 1.5 * Math.PI); assertTrue(battleField.rectangle.contains( DiaUtils.project(botLocation, goAngle, 120))); }
@Test public void testWallSmoothingNorthWallNoSmoothing() { BattleField battleField = newBattleField(800, 600); Double botLocation = newLocation(400, 500); double goAngle = Utils.normalAbsoluteAngle(battleField.wallSmoothing( botLocation, 0, -1, 40)); assertEquals(0, goAngle, 0.001); assertTrue(battleField.rectangle.contains( DiaUtils.project(botLocation, goAngle, 40))); }
@Test public void testWallSmoothingSouthWallNoSmoothing() { BattleField battleField = newBattleField(800, 600); double heading = Math.PI * 0.8; Double botLocation = newLocation(400, 59); double goAngle = Utils.normalAbsoluteAngle(battleField.wallSmoothing( botLocation, heading, 1, 40)); assertEquals(heading, goAngle, 0.001); assertTrue(battleField.rectangle.contains( DiaUtils.project(botLocation, goAngle, 40))); }
@Test public void testWallSmoothingEastWallFromOutOfBounds() { BattleField battleField = newBattleField(800, 600); Double botLocation = newLocation(805, 300); double goAngle = Utils.normalAbsoluteAngle(battleField.wallSmoothing( botLocation, Math.PI / 2, 1, 120)); assertTrue(goAngle > Math.PI && goAngle < Math.PI * 1.5); assertTrue(battleField.rectangle.contains( DiaUtils.project(botLocation, goAngle, 120))); }
@Test public void testWallSmoothingEastWallFromOutOfBoundsReverse() { BattleField battleField = newBattleField(800, 600); Double botLocation = newLocation(805, 300); double goAngle = Utils.normalAbsoluteAngle(battleField.wallSmoothing( botLocation, Math.PI / 2, -1, 120)); assertTrue(goAngle > Math.PI * 1.5 && goAngle < 2 * Math.PI); assertTrue(battleField.rectangle.contains( DiaUtils.project(botLocation, goAngle, 120))); }
@Test public void testWallSmoothingEastWallFromWayOutOfBounds() { BattleField battleField = newBattleField(800, 600); Double botLocation = newLocation(1205, 300); double goAngle = Utils.normalAbsoluteAngle(battleField.wallSmoothing( botLocation, Math.PI / 2, 1, 120)); assertTrue(goAngle > Math.PI && goAngle < Math.PI * 1.5); }