@VisibleForTesting void addDestinationPointInternal(Point2D newPoint, boolean isCity) { synchronized (lock) { if (!heuristic.containsKey(newPoint)) { heuristicProvider.addSourceNodeToHeuristic(newPoint); // once reached there we must leave again. heuristicProvider.addTargetNodeToHeuristic(newPoint); heuristicProvider.getHeuristic(); } Preconditions.checkArgument(heuristic.containsKey(newPoint), "Destination not part of the heuristic: "+newPoint); if (graph.containsNode(newPoint)) { return; } List<Point2D> nearest = new ArrayList<>(); for (Point2D node : graph) { double distance = node.distance(newPoint); if (distance <= DIAG_CHECK_DISTANCE) { nearest.add(node); } } graph.addNode(newPoint, true); for (Point2D point : nearest) { addEdges(newPoint, point, isCity, false); } } } @VisibleForTesting
} /** * Select a new destination for the pirate. * @param pirate free sea pirate */ private void selectDestination(ISeaPirate pirate) { Point2D destination; if (pirate instanceof INonFreeSeaPirate && ((INonFreeSeaPirate) pirate).roamingNearCity().isPresent()) { ICity city = ((INonFreeSeaPirate) pirate).roamingNearCity().get(); destination = mapService.getRandomPointAtSeaNear(city.getCoordinates()); } else { destination = mapService.getLocationAtOtherEndOfMap(pirate.getShip().getLocation()); } // make sure that the source of the pirate location is in the graph Point2D adjustedDestination = aStarHeuristicProvider.findClosest(destination); aStarHeuristicProvider.addTargetNodeToHeuristic(adjustedDestination); logger.debug("Source point of pirate is {}", pirate.getShip().getLocation()); seafaringService.travelNotBetweenCities(pirate.getShip(), adjustedDestination); }