public LinearPathSegment(Point2D start, Point2D end) { this.start = start; this.end = end; length = start.distance(end); } }
/** * Find the closest point in the graph for a defined point. * @param point for which the closest defined point should be looked up. * @return closest point to <code>point</code> */ public Point2D findClosest(Point2D point) { double distance = Double.MAX_VALUE; Point2D p = null; for (Point2D tempPoint : getHeuristic().keySet()) { if (point.distance(tempPoint) < distance) { distance = point.distance(tempPoint); p = tempPoint; } } return p; }
@VisibleForTesting protected double calculateWeight(Point2D from, Point2D to) { if (from.equals(to)) { return 0; } // Should calculate the amount of land between the two points return from.distance(to); }
@VisibleForTesting Pair<ICity, IWare> findClosestCityProducingTradableWare(IAIPlayer player, INavigableVessel vessel) { TreeMap<Double, Pair<ICity, IWare>> distances = new TreeMap<>(); Point2D location = vessel.getLocation(); for (IWare ware : tradableWares) { ICity city = findCitySupplyingWare(player, ware, vessel, new HashSet<>()); double distance = location.distance(city.getCoordinates()); Pair<ICity, IWare> pair = new Pair<>(city, ware); distances.put(distance, pair); } return distances.pollFirstEntry().getValue(); }
private Optional<ICity> findCity(double unscaledX, double unscaledY) { Point2D p = new Point2D(unscaledX, unscaledY); for (ICity city : map.getCities()) { double distance = p.distance(city.getCoordinates()); if (distance <= ImageUtil.CITY_RADIUS) { return Optional.of(city); } } return Optional.empty(); }
/** * @param other the other component * @return distance in pixels from this position to the other */ public double distance(PositionComponent other) { return getValue().distance(other.getValue()); }
@Override public List<ICity> findListWithConsumptionMinimalDistance(ICity distanceToCity, IWare ware, INavigableVessel vessel) { TreeMap<Double, ICity> cityMap = new TreeMap<>(); List<ICity> reachableCities = proxy.getAllReachableNonBlockadedCities(vessel); double mapWidth = map.getDimension().getWidth(); for (ICity city : productionAndConsumption.keySet() ) { if (!city.equals(distanceToCity)) { int consumptionAmount = productionAndConsumption.get(city).getConsumptionAmount(ware); double distance = distanceToCity.getCoordinates().distance(city.getCoordinates()); double distanceFactor = distance/mapWidth; if (reachableCities.contains(city)) { cityMap.put(1 / (distanceFactor * consumptionAmount), city); } } } return cityMap.values().stream().collect(Collectors.toList()); }
/** * Check if the two vessels are visible to each other. * @param vessel1 first vessel * @param vessel2 second vessel * @return true if both vessels are within the visible range. */ private boolean areVisibleToEachOther(INavigableVessel vessel1, INavigableVessel vessel2) { if (displayAllShips) { return true; } double distance = vessel1.getLocation().distance(vessel2.getLocation()); return distance <= visibleRange; } @Subscribe
@Override public List<ICity> findListWithProductionsMinimalDistance(ICity distanceToCity, IWare ware, INavigableVessel vessel) { TreeMap<Double, ICity> cityMap = new TreeMap<>(); List<ICity> reachableCities = proxy.getAllReachableNonBlockadedCities(vessel); double mapWidth = map.getDimension().getWidth(); for (ICity city : productionAndConsumption.keySet() ) { if (!city.equals(distanceToCity)) { int productionAmount = productionAndConsumption.get(city).getProductionAmount(ware); double distance = distanceToCity.getCoordinates().distance(city.getCoordinates()); double distanceFactor = distance/mapWidth; if (reachableCities.contains(city)) { cityMap.put(1 / (distanceFactor * productionAmount), city); } } } return cityMap.values().stream().collect(Collectors.toList()); }
@Override public List<ICity> findCitiesWithNeedMinimalDistance(ICity distanceToCity, IWare ware, INavigableVessel vessel) { TreeMap<Double, ICity> cityMap = new TreeMap<>(); double mapWidth = map.getDimension().getWidth(); List<ICity> reachableCities = proxy.getAllReachableCities(vessel); for (ICity city : productionAndConsumption.keySet() ) { if (!city.equals(distanceToCity)) { int productionAmount = productionAndConsumption.get(city).getProductionAmount(ware); int consumtionAmount = productionAndConsumption.get(city).getConsumptionAmount(ware); double consumedRation = productionAmount - consumtionAmount; double distance = distanceToCity.getCoordinates().distance(city.getCoordinates()); double distanceFactor = distance/mapWidth; if (reachableCities.contains(city)) { cityMap.put(1 / (distanceFactor * consumedRation), city); } } } return Lists.reverse(cityMap.values().stream().collect(Collectors.toList())); } }
@Override protected boolean additionalJoinRequirementsMet(IAIPlayer player, ICity city, Optional<INavigableVessel> vessel) { double distance = player.getHometown().getCoordinates().distance(city.getCoordinates()); return distance < maxDistanceInPixels; } }
@VisibleForTesting void addSourcePointInternal(Point2D source, boolean isCity) { synchronized (lock) { if (!heuristic.containsKey(source)) { heuristicProvider.addSourceNodeToHeuristic(source); } if (graph.containsNode(source)) { return; } List<Point2D> nearest = new ArrayList<>(); for (Point2D node : graph) { double distance = node.distance(source); if (distance <= DIAG_CHECK_DISTANCE) { nearest.add(node); } } graph.addNode(source, false); for (Point2D point : nearest) { addEdges(source, point, isCity, false); } } } @Override
@Override protected boolean additionalJoinRequirementsMet(IAIPlayer player, ICity city, Optional<INavigableVessel> vessel) { for (ICity otherCity : map.getCities()) { Optional<ITradingOffice> optOffice = player.findTradingOffice(otherCity); if (optOffice.isPresent()) { double distance = otherCity.getCoordinates().distance(city.getCoordinates()); if (distance < maxDistanceInPixels) { return true; } } } return false; } }
@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
/** * Check wether the ship is nearing a city. * @param vessel to be checked * @return true if the vessel is near a city. */ public boolean isNearingCity(INavigableVessel vessel) { Point2D location = vessel.getLocation(); for (ICity city : map.getCities()) { Point2D coordinates = city.getCoordinates(); double distance = coordinates.distance(location); if (distance <= CITY_RADIUS) { return true; } } return false; }
double distanceOnePenaltyPoint = width / (nbRequiredWares * 4); Optional<ICity> optCity = findCityWithAllRequiredWares(new ArrayList<>(wares), player, vessel, new HashSet<>(excluded)); if (optCity.isPresent() && optCity.get().getCoordinates().distance(source.getCoordinates()) < distanceOnePenaltyPoint * nbRequiredWares) { return optCity.get(); for (ICity city : cities) { double penality; double distance = city.getCoordinates().distance(source.getCoordinates()); penality = distance / distanceOnePenaltyPoint; for (IWare ware : wares) {
@Override protected double calculateWeight(Point2D from, Point2D to) { if (from.equals(to)) { return 0; } double distance = from.distance(to); try { int landPixels = imageService.countLandPixels(to, CHECK_DISTANCE*2, getSegments(from, to)); int tangentialLandPixels = imageService.countLandPixels(to, (int) Math.round(DIAG_CHECK_DISTANCE), getTangentialSegments(from, to)); double recastLandPixels = landPixels/ (9.0 * 3); double recastTangentialPixels = tangentialLandPixels / (15.0); return distance + recastLandPixels + recastTangentialPixels; } catch (IOException e) { logger.error("Failed to count the land pixels near "+to, e); } return distance; }
@VisibleForTesting void handleShipLocationUpdate() { for (MapSegmentedImage mapSegment : dataModel.getMapSegments()) { Point2D location = mapSegment.getLocation(); List<INavigableVessel> shipsToCheck = locationTracker.getShipsInSegments(location, locationDistance); for (INavigableVessel ship : shipsToCheck) { if (location.distance(ship.getLocation()) <= locationDistance && !dataModel.isInCheckedList(ship)) { LocalDateTime dealine = date.getCurrentDate().plusDays(1); MapSegmentDataCheck check = new MapSegmentDataCheck(ship, mapSegment, dealine); dataModel.getCheckList().put(check.getShip(), check); } } } }
/** * {@inheritDoc}<br> * Choose the nearest city from the set defined in the trade route. * @param vessel that should travel to a city, so that the trade cycle can be reinitialized * @return */ @Override public ICity getCityToRestartTradeCycle(INavigableVessel vessel) { ITradeMissionData missionData = ((IAIPlayer)vessel.getOwner()).getTradeMission(vessel); ITradeRoute route = ((TradeRouteMissionData)missionData).getTradeRoute(); double distance = Double.MAX_VALUE; ICity destination = null; for (ITradeRouteStop stop : route.getTradeStops()) { double d = vessel.getLocation().distance(stop.getTradeStop().getCoordinates()); if (d < distance) { distance = d; destination = stop.getTradeStop(); } } return destination; }