rlgamekit.map.generators
Class MapUtils

java.lang.Object
  extended by rlgamekit.map.generators.MapUtils

public class MapUtils
extends java.lang.Object

Utility methods for map handling.


Nested Class Summary
static interface MapUtils.OpenSquare
          Must be subclassed in a game specific manner to tell which sqaures are considered open.
 
Constructor Summary
MapUtils()
           
 
Method Summary
 void fillArea(LayeredMap map, int layer, int x, int y, int w, int h, int[] values)
          Fill an area of a map with values.
 void fillScriptArea(LayeredMap map, int x, int y, int w, int h, java.lang.String value)
          Sets the script code for a rectangular area of the map.
 void filterOverlapping(java.util.ArrayList<java.awt.Rectangle> list, int border)
          Filter a list of rectangles and so that only non-overlapping recangles remain in the list.
 java.awt.Point findNearOpenSquare(LayeredMap map, int x, int y, int range, MapUtils.OpenSquare predicate)
          Find closest open square around x,y in range "range".
 java.util.ArrayList<java.awt.Rectangle> findPatternMatches(LayeredMap map, int layer, java.lang.String[] pattern, int areaWidth, int areaHeight)
          Find locations that match an array of patterns.
 java.util.ArrayList<java.awt.Point> findRooms(LayeredMap map, int width, int height, MapUtils.OpenSquare predicate)
          Find all open areas of the given size.
 boolean isInside(int posX, int posY, int x, int y, int w, int h)
          Checks weather a position is inside a rectangle.
 void placeDecoCluster(LayeredMap map, int[] tiles, int x, int y, int layer, java.util.Random rand)
          Place a cluster of tiles elements at location x,y.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MapUtils

public MapUtils()
Method Detail

findRooms

public java.util.ArrayList<java.awt.Point> findRooms(LayeredMap map,
                                                     int width,
                                                     int height,
                                                     MapUtils.OpenSquare predicate)
Find all open areas of the given size.

Parameters:
map - The map to scan.
width - Min. width to find
height - Min. height to find
predicate - Object to check which map cells count as open.
Returns:
A list of all open areas bigger or equal the given size.

findNearOpenSquare

public java.awt.Point findNearOpenSquare(LayeredMap map,
                                         int x,
                                         int y,
                                         int range,
                                         MapUtils.OpenSquare predicate)
Find closest open square around x,y in range "range".

Parameters:
x - map X coordinate
y - map Y coordinate
range - max radius of search
predicate - To check squares if they count as open
Returns:
null if no suitable location was found, the found coordinate otherwise.

findPatternMatches

public java.util.ArrayList<java.awt.Rectangle> findPatternMatches(LayeredMap map,
                                                                  int layer,
                                                                  java.lang.String[] pattern,
                                                                  int areaWidth,
                                                                  int areaHeight)
Find locations that match an array of patterns. Within the map bounds each area of (areaWidth,areaHeight) will be checked if it matches the given pattern. To do this, in each area, each line will be compared to the corresponding line of the pattern, and checked if the line matches the pattern. Pattern lines are given as regular expressions. This method does fairly expensive calculations, particularly noticeable for large areas. It allows overlapping areas in the result.

Parameters:
map - The map to scan.
layer - The layer within the map to scan.
pattern - The array of regexps, one for each line of the pattern.
areaWidth - Area width
areaHeight - Area height
Returns:
A list of rectangles where the pattern matched the map

isInside

public boolean isInside(int posX,
                        int posY,
                        int x,
                        int y,
                        int w,
                        int h)
Checks weather a position is inside a rectangle. Does not allocate any objects.

Parameters:
posX - position x coordinate.
posY - position y coordinate.
x - rectangle top
y - rectangle left
w - rectangle width
h - rectangle height
Returns:
true if position is inside, false otherwise.

placeDecoCluster

public void placeDecoCluster(LayeredMap map,
                             int[] tiles,
                             int x,
                             int y,
                             int layer,
                             java.util.Random rand)
Place a cluster of tiles elements at location x,y. The cluster will spread from x-1,y-1 to x+1,y+1 at most.

Parameters:
tiles - Deco tile array, must have at least 2 elements, better 3
layer - The map layer to affect.

fillArea

public void fillArea(LayeredMap map,
                     int layer,
                     int x,
                     int y,
                     int w,
                     int h,
                     int[] values)
Fill an area of a map with values.

Parameters:
map - The map to work on.
layer - The map layer to work on.
x - Left of the area.
y - Top of the area.
w - Width of the area.
h - Height of the area.
values - Array of tile numbers. Actual values will be chosen randomly from this.

fillScriptArea

public void fillScriptArea(LayeredMap map,
                           int x,
                           int y,
                           int w,
                           int h,
                           java.lang.String value)
Sets the script code for a rectangular area of the map.

Parameters:
map - The map to work on.
x - Left of the area.
y - Top of the area.
w - Width of the area.
h - Height of the area.
value - The script code to set.

filterOverlapping

public void filterOverlapping(java.util.ArrayList<java.awt.Rectangle> list,
                              int border)
Filter a list of rectangles and so that only non-overlapping recangles remain in the list.

Parameters:
list - The list of rectangles
border - The amount of overlap that is tolerated