com.realtime.crossfire.jxclient.map
Class CfMap

java.lang.Object
  extended by com.realtime.crossfire.jxclient.map.CfMap

public class CfMap
extends java.lang.Object

Represents a map (as seen by the client). A map is a grid in which CfMapSquares can be stored.

The map will be automatically enlarged by accesses to new squares. Not yet set squares are considered dark.

All accesses must be synchronized on the map instance.


Field Summary
private  java.util.Set<CfMapSquare> dirtyMapSquares
          The "dirty" map squares that have been modified.
private  int maxPx
          The right edge of the defined patches in patch.
private  int maxPy
          The bottom edge of the defined patches in patch.
private  int maxX
          The right edge of the defined tiles.
private  int maxY
          The bottom edge of the defined tiles.
private  int minPx
          The left edge of the defined patches in patch.
private  int minPy
          The top edge of the defined patches in patch.
private  int minX
          The left edge of the defined tiles.
private  int minY
          The top edge of the defined tiles.
private  int ox
          Result values for getMapPatch(int,int) and expandTo(int,int).
private  int oy
          Result values for getMapPatch(int,int) and expandTo(int,int).
private  CfMapPatch[][] patch
          Array of (possibly) defined squares.
private  int patchX
          Left edge of viewable area.
private  int patchY
          Top edge of viewable area.
 
Constructor Summary
CfMap()
           
 
Method Summary
 void clearSquare(int x, int y)
          Clears the content of one square.
private  CfMapPatch[][] copyPatches(int newWidth, int newHeight, int offsetX, int offsetY, int height, int width)
          Returns a copy of a rectangular area of patch.
 void dirty(int x, int y)
          Marks a single square as dirty.
private  void dirtyFace(int x, int y, int layer, Face face)
          Marks one face as "dirty".
private  void expandFace(int x, int y, int layer, Face face, CfMapSquare oldMapSquare, CfMapSquare newMapSquare)
          Adds or removes "head" pointer to/from tail-parts of a face.
private  CfMapPatch expandTo(int x, int y)
          (Possibly) expands the defined area of the map to a given position.
 int getColor(int x, int y)
          Returns the magic map color value of one square.
 int getDarkness(int x, int y)
          Returns the darkness value of one square.
 java.util.Set<CfMapSquare> getDirtyMapSquares()
          Returns the dirty map squares.
 Face getFace(int x, int y, int layer)
          Determines the face of one square.
 CfMapSquare getHeadMapSquare(int x, int y, int layer)
          Returns the map square of the head of a multi-square object.
private  CfMapPatch getMapPatch(int x, int y)
          Checks if a given position is within the defined map area.
 CfMapSquare getMapSquare(int x, int y)
          Returns a map square.
 CfMapSquare getMapSquareUnlessDirty(int x, int y)
          Returns a map square.
 int getOffsetX()
          Returns the offset to convert an absolute x-coordinate of a map square (CfMapSquare.getX() to a relative x-coordinate.
 int getOffsetY()
          Returns the offset to convert an absolute y-coordinate of a map square (CfMapSquare.getY() to a relative y-coordinate.
private  void increase(int dx, int dy)
          Increases the defined area of the map.
 boolean isDirty(CfMapSquare mapSquare)
          Returns whether a CfMapSquare is dirty.
 boolean isFogOfWar(int x, int y)
          Determines if the tile is not up-to-date.
 void scroll(int dx, int dy)
          Scrolls the map.
 void setColor(int x, int y, int color)
          Sets the magic map color of one square.
 void setDarkness(int x, int y, int darkness)
          Sets the darkness value of one square.
 void setFace(int x, int y, int layer, Face face)
          Sets the face of one square.
private  void setFaceInternal(int x, int y, int layer, Face face)
          Sets the face of one square.
private  void setHeadMapSquare(int x, int y, int layer, CfMapSquare mapSquare, boolean setAlways)
          Sets the map square containing the head face for a layer.
private static int size(int min, int max)
          Calculates the number of patches needed to hold tiles between two patch coordinates.
 void squareModified(CfMapSquare mapSquare)
          Marks a CfMapSquare as dirty.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dirtyMapSquares

@NotNull
private final java.util.Set<CfMapSquare> dirtyMapSquares
The "dirty" map squares that have been modified.


maxPx

private int maxPx
The right edge of the defined patches in patch.


maxPy

private int maxPy
The bottom edge of the defined patches in patch.


maxX

private int maxX
The right edge of the defined tiles.


maxY

private int maxY
The bottom edge of the defined tiles.


minPx

private int minPx
The left edge of the defined patches in patch.


minPy

private int minPy
The top edge of the defined patches in patch.


minX

private int minX
The left edge of the defined tiles.


minY

private int minY
The top edge of the defined tiles.


ox

private int ox
Result values for getMapPatch(int,int) and expandTo(int,int).


oy

private int oy
Result values for getMapPatch(int,int) and expandTo(int,int).


patch

@Nullable
private CfMapPatch[][] patch
Array of (possibly) defined squares.


patchX

private int patchX
Left edge of viewable area.


patchY

private int patchY
Top edge of viewable area.

Constructor Detail

CfMap

public CfMap()
Method Detail

clearSquare

public void clearSquare(int x,
                        int y)
Clears the content of one square. Note: the old square content remains available until at least one value will be changed ("fog of war").

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square

copyPatches

@NotNull
private CfMapPatch[][] copyPatches(int newWidth,
                                           int newHeight,
                                           int offsetX,
                                           int offsetY,
                                           int height,
                                           int width)
Returns a copy of a rectangular area of patch.

Parameters:
newWidth - the width of the new area
newHeight - the height of the new area
offsetX - the x-offset into the new area
offsetY - the y-offset into the new area
height - the height of the area to copy
width - the width of the area to copy
Returns:
the copy

dirty

public void dirty(int x,
                  int y)
Marks a single square as dirty.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square

dirtyFace

private void dirtyFace(int x,
                       int y,
                       int layer,
                       @NotNull
                       Face face)
Marks one face as "dirty". This function is called when the head part becomes a fog-of-war tile. This means the face has to be redrawn.

Parameters:
x - the x-coordinate of the tail part of the face
y - the y-coordinate of the tail part of the face
layer - the layer of the face
face - the face to mark dirty

expandFace

private void expandFace(int x,
                        int y,
                        int layer,
                        @NotNull
                        Face face,
                        @NotNull
                        CfMapSquare oldMapSquare,
                        @Nullable
                        CfMapSquare newMapSquare)
Adds or removes "head" pointer to/from tail-parts of a face.

Parameters:
x - the x-coordinate of the tail part to expand
y - the y-coordinate of the tail part to expand
layer - the layer to expand
face - the face to expand
oldMapSquare - the map square of the tail part
newMapSquare - the map square of the tail part to add pointers, or null to remove pointers

expandTo

@NotNull
private CfMapPatch expandTo(int x,
                                    int y)
(Possibly) expands the defined area of the map to a given position.

Parameters:
x - the x-coordinate to expand the defined area to
y - the y-coordinate to expand the defined area to
Returns:
the map patch or null if the coordinates are out of map bounds

getColor

public int getColor(int x,
                    int y)
Returns the magic map color value of one square.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
Returns:
the color

getDarkness

public int getDarkness(int x,
                       int y)
Returns the darkness value of one square.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
Returns:
the darkness value of the square; 0=dark, 255=full bright; not yet set faces return 0

getDirtyMapSquares

@NotNull
public java.util.Set<CfMapSquare> getDirtyMapSquares()
Returns the dirty map squares. The result may be modified by the caller.

Returns:
the dirty map squares

getFace

@Nullable
public Face getFace(int x,
                             int y,
                             int layer)
Determines the face of one square.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
layer - the layer of the face
Returns:
the face; dark (i.e. not yet set) faces return null

getHeadMapSquare

@Nullable
public CfMapSquare getHeadMapSquare(int x,
                                             int y,
                                             int layer)
Returns the map square of the head of a multi-square object.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
layer - the layer to return the head for
Returns:
the head map square, or null if this square does not contain a multi-tail

getMapPatch

@Nullable
private CfMapPatch getMapPatch(int x,
                                        int y)
Checks if a given position is within the defined map area.

Returns additional information in ox and oy.

Parameters:
x - the x-coordinate to check
y - the y-coordinate to check
Returns:
the map patch or null if the coordinates are out of map bounds

getMapSquare

@NotNull
public CfMapSquare getMapSquare(int x,
                                        int y)
Returns a map square.

Parameters:
x - the x-coordinate
y - the y-coordinate
Returns:
the map square

getMapSquareUnlessDirty

@Nullable
public CfMapSquare getMapSquareUnlessDirty(int x,
                                                    int y)
Returns a map square.

Parameters:
x - the x-coordinate
y - the y-coordinate
Returns:
the map square or null if it would be dirty

getOffsetX

public int getOffsetX()
Returns the offset to convert an absolute x-coordinate of a map square (CfMapSquare.getX() to a relative x-coordinate.

Returns:
the x offset

getOffsetY

public int getOffsetY()
Returns the offset to convert an absolute y-coordinate of a map square (CfMapSquare.getY() to a relative y-coordinate.

Returns:
the y offset

increase

private void increase(int dx,
                      int dy)
Increases the defined area of the map.

Parameters:
dx - the increase in x-direction; dx<0 means "expand (-dx) tiles to the left", dx>0 means "expand (dx) tiles to the right"
dy - the increase in y-direction; dy<0 means "expand (-dy) tiles to the top", dy>0 means "expand (dy) tiles to the bottom"

isDirty

public boolean isDirty(@NotNull
                       CfMapSquare mapSquare)
Returns whether a CfMapSquare is dirty.

Parameters:
mapSquare - the map square
Returns:
whether this square needs redraw

isFogOfWar

public boolean isFogOfWar(int x,
                          int y)
Determines if the tile is not up-to-date.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
Returns:
whether the tile contains fog-of-war data

scroll

public void scroll(int dx,
                   int dy)
Scrolls the map.

Parameters:
dx - the x-difference to scroll
dy - the y-difference to scroll

setColor

public void setColor(int x,
                     int y,
                     int color)
Sets the magic map color of one square.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
color - the color to set

setDarkness

public void setDarkness(int x,
                        int y,
                        int darkness)
Sets the darkness value of one square.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
darkness - the darkness value to set; 0=dark, 255=full bright

setFace

public void setFace(int x,
                    int y,
                    int layer,
                    @Nullable
                    Face face)
Sets the face of one square. This function clears fog-of-war state if necessary.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
layer - the layer to set
face - the face to set; may be null to remove the face

setFaceInternal

private void setFaceInternal(int x,
                             int y,
                             int layer,
                             @Nullable
                             Face face)
Sets the face of one square.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
layer - the layer to set
face - the face to set; may be null to remove the face

setHeadMapSquare

private void setHeadMapSquare(int x,
                              int y,
                              int layer,
                              @Nullable
                              CfMapSquare mapSquare,
                              boolean setAlways)
Sets the map square containing the head face for a layer.

Parameters:
x - the x-coordinate of the square
y - the y-coordinate of the square
layer - the layer for the new head face between 0 and LAYERS-1
mapSquare - the map square containing the head face; may be null
setAlways - if set, always update the face; if unset, only update when fog-of-war

size

private static int size(int min,
                        int max)
Calculates the number of patches needed to hold tiles between two patch coordinates.

Parameters:
min - the minimum coordinate
max - the maximum coordinate
Returns:
the number of patches

squareModified

public void squareModified(@NotNull
                           CfMapSquare mapSquare)
Marks a CfMapSquare as dirty.

Parameters:
mapSquare - the map square