Class Regions


  • public class Regions
    extends java.lang.Object
    A class offering common operations on regions.
    • Field Detail

      • excludedRegions

        public static java.util.Collection<Region> excludedRegions
        will be ignored from getAllNeighbours
      • RADIUS

        public static final int RADIUS
        Inner radius (or half distance) of hexagons.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Regions

        public Regions()
    • Method Detail

      • setExcludedRegions

        public static void setExcludedRegions​(java.util.Collection<Region> excludedRegions)
        sets the excluded Regions
        Parameters:
        excludedRegions -
      • getAllNeighbours

        @Deprecated
        public static java.util.Map<CoordinateID,​Region> getAllNeighbours​(java.util.Map<CoordinateID,​Region> regions,
                                                                                ID center,
                                                                                int radius,
                                                                                java.util.Map<ID,​RegionType> excludedRegionTypes)
        Deprecated.
        now obsolete
        Retrieve the regions within radius around region center.
        Parameters:
        regions - a map containing the existing regions.
        center - the region the neighbours of which are retrieved.
        radius - the maximum distance between center and any region to be regarded as a neighbour within radius.
        excludedRegionTypes - region types that disqualify regions as valid neighbours. This also may be null
        Returns:
        a map with all neighbours that were found, including region center. The keys are instances of class ID, values are objects of class Region.
        Throws:
        java.lang.IllegalArgumentException - If center is not a CoordinatID
      • getAllNeighbours

        public static java.util.Map<CoordinateID,​Region> getAllNeighbours​(java.util.Map<CoordinateID,​Region> regions,
                                                                                CoordinateID center,
                                                                                int radius,
                                                                                java.util.Map<ID,​RegionType> excludedRegionTypes)
        Retrieve the regions within radius around region center.
        Parameters:
        regions - a map containing the existing regions.
        center - the region the neighbours of which are retrieved.
        radius - the maximum distance between center and any region to be regarded as a neighbour within radius.
        excludedRegionTypes - region types that disqualify regions as valid neighbours.
        Returns:
        a map with all neighbours that were found, including region center. The keys are instances of class Coordinate, values are objects of class Region. This map is created in this method.
      • getAllNeighbours

        public static java.util.Map<CoordinateID,​Region> getAllNeighbours​(java.util.Map<CoordinateID,​Region> regions,
                                                                                CoordinateID center,
                                                                                java.util.Map<ID,​RegionType> excludedRegionTypes)
        Retrieve the regions directly connected with the center region (including itself).
        Parameters:
        regions - a map containing the existing regions.
        center - the region the neighbours of which are retrieved.
        excludedRegionTypes - region types that disqualify regions as valid neighbours.
        Returns:
        a map with all neighbours that were found, including region center. The keys are instances of class Coordinate, values are objects of class Region. This map is created in this method.
        See Also:
        Region.getNeighbors()
      • getCoordinateNeighbours

        public static java.util.Map<Direction,​Region> getCoordinateNeighbours​(GameData data,
                                                                                    CoordinateID center)
        Calculates the neighbors of a region based on coordinates, i.e., it tests all neighboring coordinates and adds them if the region exists in the data. It follows wrappers if necessary.
        Returns:
        A map with Direction/Region entries of existing neighbors. This map is created in this method.
        See Also:
        Region.getNeighbors()
      • getCoordinateNeighbours

        public static java.util.Map<Direction,​Region> getCoordinateNeighbours​(java.util.Map<CoordinateID,​Region> regions,
                                                                                    CoordinateID center)
        Calculates the neighbors of a region based on coordinates, i.e., it tests all neighboring coordinates and adds them if the region exists in the data.
        Returns:
        A map with Direction/Region entries of existing neighbors. This map is created in this method.
        See Also:
        Region.getNeighbors()
      • getDirections

        public static java.lang.String getDirections​(GameData data,
                                                     CoordinateID start,
                                                     CoordinateID dest,
                                                     java.util.Map<ID,​RegionType> excludedRegionTypes,
                                                     int radius)
        Find a way from one region to another region and get the directions in which to move to follow a sequence of regions. This is virtually the same as
         getDirections(getPath(regions, start, dest, excludedRegionTypes, radius));
         
        Returns:
        a String telling the direction statements necessary to follow the sequence of regions contained in regions.
        See Also:
        getPath(Map, CoordinateID, CoordinateID, Map)
      • getDirections

        public static java.lang.String getDirections​(java.util.Collection<Region> regions)
        Get the directions in which to move to follow a sequence of regions.
        Parameters:
        regions - an ordered consecutive sequence of regions.
        Returns:
        a String telling the direction statements necessary to follow the sequence of regions contained in regions.
      • getDirectionObjectsOfRegions

        public static java.util.List<Direction> getDirectionObjectsOfRegions​(java.util.Collection<Region> regions)
        Converts a list of coordinate into a list of Directions between them. If successive coordinates are not direct neighbors, those directions will be omitted!
        See Also:
        getDirectionObjectsOfCoordinates(Collection)
      • getDirectionObjectsOfCoordinates

        public static java.util.List<Direction> getDirectionObjectsOfCoordinates​(java.util.Collection<CoordinateID> coordinates)
        Converts a list of coordinate into a list of Directions between them. If successive coordinates are not direct neighbors, those directions will be omitted! Note also, that this cannot consider wrapping paths!
      • getDirectionObjectsOfCoordinates

        public static java.util.List<Direction> getDirectionObjectsOfCoordinates​(GameData data,
                                                                                 java.util.Collection<CoordinateID> coordinates)
        Converts a list of coordinate into a list of Directions between them. If successive coordinates are not direct neighbors, those directions will be omitted! If data!=null, this method will assume that the coordinates correspond to real regions (not wrapped ones) and try finding the right directions between them.
      • getDistances

        public static void getDistances​(java.util.Map<CoordinateID,​Region> regions,
                                        CoordinateID start,
                                        CoordinateID dest,
                                        int maxDist,
                                        Regions.Metric metric)
        Returns distance information about all Coordinates in regions that are at most as far as dest or as far as maxDist away from start. The result is stored in the metric information.
        Parameters:
        regions - Regions will be looked up in this map.
        start - The origin of the search.
        dest - The coordinate that must be reached, or null
        maxDist - The maximum distance to search. The search stops if all regions with at most this distance have been visited.
        metric - A metric for computing distances.
      • getPath2

        public static java.util.List<Region> getPath2​(java.util.Map<CoordinateID,​Region> regions,
                                                      CoordinateID start,
                                                      CoordinateID dest,
                                                      java.util.Map<ID,​RegionType> excludedRegionTypes,
                                                      Regions.Metric outerMetric,
                                                      int radius,
                                                      int streetRadius)
        Returns a path from start to dest based on the predecessor information in records. Accounts for skipped regions.
        Parameters:
        outerMetric - The metric for predecessor information
        radius - This method applies a RoadMetric with this minimal...
        streetRadius - ...and this maximal speed
        Returns:
        A list of regions from start to dest (both inclusively)
      • getLandDistance

        public static int getLandDistance​(GameData data,
                                          CoordinateID start,
                                          CoordinateID dest,
                                          java.util.Map<ID,​RegionType> excludedRegionTypes,
                                          int radius,
                                          int streetRadius)
        Returns the distance on a shortest path over land from start to dest, excluding certain region types and considering streets.
        Parameters:
        data -
        start -
        dest -
        excludedRegionTypes - regions with these types will be ignored (not entered) in the search.
        radius - The number of regions that can be travelled per turn without streets
        streetRadius - The number of regions that can be travelled per turn on streets
        Returns:
        The distance in turns from start to dest. Integer.MAX_VALUE if there is no connection
      • getLandPath

        public static java.util.List<Region> getLandPath​(GameData data,
                                                         CoordinateID start,
                                                         CoordinateID dest,
                                                         java.util.Map<ID,​RegionType> excludedRegionTypes,
                                                         int radius,
                                                         int streetRadius)
        Returns a shortest path over land from start to dest, excluding certain region types and considering streets.
        Parameters:
        data -
        start -
        dest -
        excludedRegionTypes - regions with these types will be ignored (not entered) in the search.
        radius - The number of regions that can be travelled per turn without streets
        streetRadius - The number of regions that can be travelled per turn on streets
      • containsHarbour

        public static boolean containsHarbour​(Region r,
                                              java.util.Collection<BuildingType> harbours)
      • containsBuilding

        public static boolean containsBuilding​(Region r,
                                               BuildingType type)
        Returns true if r contains a build with the specified type
      • getShipDistance

        public static int getShipDistance​(GameData data,
                                          CoordinateID start,
                                          Direction returnDirection,
                                          CoordinateID destination,
                                          int speed)
        Returns the distance on a shortest path for a ship from start to dest.
        Parameters:
        data -
        start -
        returnDirection - The ship's shore or Direction.INVALID
        destination -
        Returns:
        The distance in turns from start to dest. Integer.MAX_VALUE if there is no connection.
      • planShipRoute

        public static java.util.List<Region> planShipRoute​(Ship ship,
                                                           GameData data,
                                                           CoordinateID destination)
        Returns a route for the ship from its current region to its destination.
      • planShipRoute

        public static java.util.List<Region> planShipRoute​(GameData data,
                                                           CoordinateID start,
                                                           Direction returnDirection,
                                                           CoordinateID destination,
                                                           int speed)
        Finds a shortest path for a ship from start to destination.
        Parameters:
        data -
        start -
        returnDirection - The ship's shore or Direction.INVALID
        destination -
        speed - The number of regions per week
        Returns:
        A list of region from start to destination, or null if no path exists.
      • planShipRouteWithLength

        protected static magellan.library.utils.Regions.PathWithLength planShipRouteWithLength​(GameData data,
                                                                                               CoordinateID start,
                                                                                               Direction returnDirection,
                                                                                               CoordinateID destination,
                                                                                               int speed)
        Finds a shortest path for a ship from start to destination.
        Parameters:
        data -
        start -
        returnDirection - The ship's shore or Direction.INVALID
        destination -
        speed - The number of regions per week
        Returns:
        A list of region from start to destination, or null if no path exists and the length (in turns) of this path.
      • getDirection

        public static Direction getDirection​(Region from,
                                             Region to)
        Returns the (first) direction from prev to cur even if it is wrapped around.
      • getMovement

        public static java.util.List<CoordinateID> getMovement​(GameData data,
                                                               Unit u)
        Retrieve the coordinates the unit passes from the messages of the regions.
        Parameters:
        data - the unit
        Returns:
        a List of Coordinate objects of the path the unit used (although evaluated via backtracking) from start to end.
      • getNonOceanRegionTypes

        public static java.util.Map<ID,​RegionType> getNonOceanRegionTypes​(Rules rules)
        Returns a map of all RegionTypes that are not flagged as ocean.
        Parameters:
        rules - Rules of the game
        Returns:
        map of all non-ocean RegionTypes
      • getOceanRegionTypes

        public static java.util.Map<ID,​RegionType> getOceanRegionTypes​(Rules rules)
        Returns a map of all RegionTypes that are flagged as ocean.
        Parameters:
        rules - Rules of the game
        Returns:
        map of all ocean RegionTypes
      • getLandRegionTypes

        public static java.util.Map<ID,​RegionType> getLandRegionTypes​(Rules rules)
        Returns a map of all RegionTypes that are flagged as land.
        Parameters:
        rules - Rules of the game
        Returns:
        map of all land RegionTypes
      • getNonLandRegionTypes

        public static java.util.Map<ID,​RegionType> getNonLandRegionTypes​(Rules rules)
        Returns a map of all RegionTypes that are not flagged as land.
        Parameters:
        rules - Rules of the game
        Returns:
        map of all non-land RegionTypes
      • getFeuerwandRegionType

        public static RegionType getFeuerwandRegionType​(GameData data)
        Returns the RegionType that is named as Feuerwand.
        Parameters:
        data - needed for correct rules
        Returns:
        RegionType Feuerwand
      • isCompleteRoadConnection

        public static boolean isCompleteRoadConnection​(Region r1,
                                                       Region r2)
        returns true, if a working road connection is established between r1 and r2 we assume, both regions are neighbours
        Parameters:
        r1 - a region
        r2 - another region
      • getAllNeighbours

        public static java.util.Collection<CoordinateID> getAllNeighbours​(CoordinateID center,
                                                                          int radius)
        Returns all coordinates with a distance of at most radius to center.
      • getDist

        public static int getDist​(CoordinateID r1,
                                  CoordinateID r2)
        Compute distance between two coordinates. Contributed by Hubert Mackenberg. Thanks.
      • getNewBorderID

        public static IntegerID getNewBorderID​(Region r,
                                               Border border)
        Returns an ID for an new Border to be add
        Parameters:
        r -
        border -
      • calculateCoastBorders

        public static void calculateCoastBorders​(GameData data)
        Checks all regions and recalculates the BitMap for the borders, where is ocean and where not
        Parameters:
        data - GameData
      • convexHull

        public static java.util.List<CoordinateID> convexHull​(java.util.Collection<CoordinateID> points)
        Computes the convex hull of a given set of coordinates.
        Parameters:
        points -
        Returns:
        A list of vertices of the convex hull in counter-clockwise order.
      • insideConvex

        public static int insideConvex​(CoordinateID point,
                                       java.util.List<CoordinateID> hull)
        Tests if the given point is inside the given convex hull. The points of hull must be sorted counterclockwise and in the same layer. Attention: The points that are inside and on the edges do not necessarily form a connected region.
        Parameters:
        point -
        hull -
        Returns:
        1 if the point is inside, -1 if it is outside and 0 if it lies on the perimeter of the hull.
      • insidePolygon

        public static int insidePolygon​(CoordinateID point,
                                        java.util.List<CoordinateID> vertices)
        Returns 1 if the point is inside the polygon described by the given vertices, 0 if it is on an edge of the polygon, or -1 if it is outside. The vertices must be given in clockwise or counter-clockwise order. Attention: The points that are inside and on the edges do not necessarily form a connected region.
        Parameters:
        point -
        vertices -
        Returns:
        1, 0, or -1 if the point is inside, on an edge, or outside the polygon, respectively
      • coords2Points

        public static void coords2Points​(java.util.List<CoordinateID> vertices,
                                         double[] xx,
                                         double[] yy)
        Converts the given coordinates to lists of map coordinates
        Parameters:
        vertices - A collection of coordinates
        xx - An array with at least the same size as the collection of coordinates. The x-coordinates of the corresponding points will be stored in this array.
        yy - An array with at least the same size as the collection of coordinates. The y-coordinates of the corresponding points will be stored in this array.
      • hexagonIntersects

        public static boolean hexagonIntersects​(CoordinateID point,
                                                double[] xx,
                                                double[] yy)
        Return true if the hexagon with inner radius RADIUS intersects the borders of the polygon given by the xx and yy coordinates.
        Parameters:
        point -
        xx -
        yy -
        Returns:
        true if the hexagon intersects the polygon.
      • almostIntersects

        public static boolean almostIntersects​(double x1,
                                               double y1,
                                               double x2,
                                               double y2,
                                               double x3,
                                               double y3,
                                               double x4,
                                               double y4)
        Returns true if the line segment (x1,y1)->(x2,y2) "almost" intersects (x3,y3)->(x4,y4). This method may report line segments as intersecting that are just very close (closer than EPSILON).
        Parameters:
        x1 -
        y1 -
        x2 -
        y2 -
        x3 -
        y3 -
        x4 -
        y4 -
        Returns:
        true iff the line segments intersect.
      • insidePolygon

        public static int insidePolygon​(double px,
                                        double py,
                                        double[] xx,
                                        double[] yy)
        Returns 1 if the point px, py is inside the polygon described by the given x and y coordinates, 0 if it is on an edge of the polygon, or -1 if it is outside. The vertices must be given in clockwise or counter-clockwise order. Attention: The set of points that are inside and on the edges do not necessarily form a connected region.
        Parameters:
        px -
        py -
        xx -
        yy -
        Returns:
        1, 0, or -1 if the point is inside, on an edge, or outside the polygon, respectively
      • filter

        public static java.util.List<CoordinateID> filter​(int layer,
                                                          java.util.Collection<CoordinateID> points)
        Returns a list that consists of the coordinates in points whose z-coordinates are in the given layer.
        Parameters:
        layer -
        points -
        Returns:
        New list of coordinates.
      • getComponents

        public static java.util.Collection<? extends java.util.Collection<Region>> getComponents​(java.util.Map<CoordinateID,​Region> regions)