civquest
Class MapData

java.lang.Object
  extended bycivquest.MapData
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
FlatMapData, IsoMapData

public abstract class MapData
extends java.lang.Object
implements java.io.Serializable

Contains all display-agnostic information about a map. Is designed for use with 2D-maps that can be saved in a (two-dimensional) array. MapView-ers work on objects of this class. Some definitions: An array-coordinate is a coordinate within the (subclass-internal) array that saves the data. A map-coordinate is an array-coordinate that marks a Field.

See Also:
Serialized Form

Nested Class Summary
protected  class MapData.Configuration
           
 
Field Summary
static short EQUAL_TO
           
protected  Field[][] fields
          Each object of this array stores that data for one field on the map.
static short GREATER_THAN
           
protected  int mapHeight
          Height of the map.
protected  Registry mapRegistry
           
protected  int mapWidth
          Width of the map.
protected  Registry moveCalcRegistry
           
static short SMALLER_THAN
           
 
Constructor Summary
MapData(int width, int height, boolean nflatearth, int nminbigriverwf, Registry registry)
          Constructs the object.
 
Method Summary
 void addCity(City city, Field field, Nation nation)
          Adds a city.
 void addMapChangeListener(MapChangeListener mcl)
           
 void addUnit(Unit unit, Field field, Nation nation)
          Adds a unit.
 boolean adjustsToLand(Coordinate coord)
           
 boolean adjustsToSea(Coordinate coord)
          Returns if the field specified by coord adjusts to sea (in directions N, NE, E, ..., NW)
abstract  Coordinate adjustToMapSize(Coordinate coord)
          Adjusts the given coordinate to mapsize.
 Coordinate adjustToMapSize(int x, int y)
           
 boolean areIncompleteMovesAllowed()
           
abstract  boolean areNeighborCoords(Coordinate coord1, Coordinate coord2)
          Returns if the two given Coordinates are neighbor-Coordinates.
 boolean calculateDistanceFromCoast()
           
 boolean calculateDistanceFromSea()
          Calculates for all Fields on the map how far they are away from sea.
 int distanceFromHeightLevel(int heightLevel, Coordinate b, int timeout, int operator)
          Gets distance from the current tile to (and included) the tile the meets the requested heightlevel.
abstract  Coordinate getAreaCoord(Rectangle rect, Coordinate coord)
          Translates the given Coordinate (by map-sizes) to be in the specified rectangle.
abstract  int getArrayHeight()
          Returns the height of the internal array that stores the Fields.
abstract  int getArrayWidth()
          Returns the width of the internal array that stores the Fields.
 City getCity(long id)
           
 java.util.Iterator getCityIterator()
           
 Field[] getCoasts(Coordinate coord)
           
abstract  MapData.Configuration getConfiguration()
           
 Field getField(Coordinate c)
           
 Field getField(int x, int y)
          Returns the field at the given (array)-coordinate.
 int getMapHeight()
           
 int getMapWidth()
           
 int getMinBigRiverWaterFlow()
           
 MoveCalculator getMoveCalculator()
           
abstract  int getNeighborFieldDist(Field f1, Field f2)
          Returns the distance between the two given _neighbor_-fields.
abstract  FieldDistanceInfo[] getNeighborFieldDists(Field field)
          Returns an array with FieldDistanceInfo-objects for each neighbor-field of the given Field.
abstract  Field[] getNeighborFields(Field field)
           
abstract  Coordinate getRandomCoordinate()
          Returns a random map-coordinate.
 Unit getUnit(long id)
           
 java.util.Iterator getUnitIterator()
           
 boolean hasNeighborFieldDistanceToCoast(Field field, int distance)
          Returns if any of the 8 neighborfields of the given field has the given distanceToCoast.
 boolean hasNeighborFieldDistanceToSea(Field field, int distance)
          Returns if any of the 8 neighborfields of the given field has the given distanceToSea.
abstract  boolean isArrayCoordOnMap(Coordinate coord)
           
abstract  boolean isArrayCoordOnMap(int x, int y)
           
 boolean isFlatEarth()
           
abstract  boolean isOnEastEdge(Coordinate coord)
          See isOnNorthEdge
abstract  boolean isOnMap(Coordinate coord)
           
abstract  boolean isOnMap(int x, int y)
           
 boolean isOnMapEdge(Coordinate coord)
          Returns false if field adjusts on all four sides to other fields, otherwise true.
abstract  boolean isOnNorthEdge(Coordinate coord)
          A coordinate is on the northern edge if there is no map-coordinate that is north, northeast or northwest of it.
abstract  boolean isOnSouthEdge(Coordinate coord)
          See isOnNorthEdge
abstract  boolean isOnWestEdge(Coordinate coord)
          See isOnNorthEdge
 boolean isPressureUsed()
           
abstract  Coordinate mapCoordinate(Coordinate coord)
           
 void setCityOwner(City city, Nation nation)
          Sets the owner of to .
 void setHeightLevel(int x, int y, int hlevel)
           
 void setMinBigRiverWaterFlow(int nminbigriverwf)
           
 void setPressureUsed(boolean npressureused)
           
 void setUnitOwner(Unit unit, Nation nation)
          Sets the owner of a unit.
 void setUnitPosition(Unit unit, Field field)
          Sets the position of the given Unit to the given Field.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mapRegistry

protected Registry mapRegistry

moveCalcRegistry

protected Registry moveCalcRegistry

mapHeight

protected int mapHeight
Height of the map.


mapWidth

protected int mapWidth
Width of the map.


fields

protected Field[][] fields
Each object of this array stores that data for one field on the map.


SMALLER_THAN

public static final short SMALLER_THAN
See Also:
Constant Field Values

EQUAL_TO

public static final short EQUAL_TO
See Also:
Constant Field Values

GREATER_THAN

public static final short GREATER_THAN
See Also:
Constant Field Values
Constructor Detail

MapData

public MapData(int width,
               int height,
               boolean nflatearth,
               int nminbigriverwf,
               Registry registry)
Constructs the object. Note that no fields get initialized - this is the task of a child-class.

Method Detail

mapCoordinate

public abstract Coordinate mapCoordinate(Coordinate coord)

getMapHeight

public int getMapHeight()

getMapWidth

public int getMapWidth()

isFlatEarth

public boolean isFlatEarth()

setPressureUsed

public void setPressureUsed(boolean npressureused)

isPressureUsed

public boolean isPressureUsed()

setMinBigRiverWaterFlow

public void setMinBigRiverWaterFlow(int nminbigriverwf)

getMinBigRiverWaterFlow

public int getMinBigRiverWaterFlow()

addMapChangeListener

public void addMapChangeListener(MapChangeListener mcl)

getMoveCalculator

public MoveCalculator getMoveCalculator()

getField

public Field getField(int x,
                      int y)
Returns the field at the given (array)-coordinate. Note that - depending on the implementation - this function may return null (if you want a field that does not exist).


getField

public Field getField(Coordinate c)

setHeightLevel

public void setHeightLevel(int x,
                           int y,
                           int hlevel)

getCoasts

public Field[] getCoasts(Coordinate coord)

calculateDistanceFromCoast

public boolean calculateDistanceFromCoast()

calculateDistanceFromSea

public boolean calculateDistanceFromSea()
Calculates for all Fields on the map how far they are away from sea. Returns false if the calculation fails because there are no sea-fields on the map, otherwise true. Saves the results into the Fields- objects.


hasNeighborFieldDistanceToCoast

public boolean hasNeighborFieldDistanceToCoast(Field field,
                                               int distance)
Returns if any of the 8 neighborfields of the given field has the given distanceToCoast.


hasNeighborFieldDistanceToSea

public boolean hasNeighborFieldDistanceToSea(Field field,
                                             int distance)
Returns if any of the 8 neighborfields of the given field has the given distanceToSea.


distanceFromHeightLevel

public int distanceFromHeightLevel(int heightLevel,
                                   Coordinate b,
                                   int timeout,
                                   int operator)
Gets distance from the current tile to (and included) the tile the meets the requested heightlevel. If the tile is the same as heightLevel or if it doesn't mange to find it within $timeout amount of tiles, zero is returned. Passes as int as an operator, useful in cases you want to find heigtlevels equal, greater or smaller than heightLevel -1 is smaller (SMALLER_THAN) 0 is equal (EQUAL_TO) 1 is greater (GREATER_THAN)


adjustsToSea

public boolean adjustsToSea(Coordinate coord)
Returns if the field specified by coord adjusts to sea (in directions N, NE, E, ..., NW)


adjustsToLand

public boolean adjustsToLand(Coordinate coord)

getUnit

public Unit getUnit(long id)

getUnitIterator

public java.util.Iterator getUnitIterator()

addUnit

public void addUnit(Unit unit,
                    Field field,
                    Nation nation)
Adds a unit. Performs all necessary datastructure-updates.

Parameters:
unit -
field -
nation -

setUnitPosition

public void setUnitPosition(Unit unit,
                            Field field)
Sets the position of the given Unit to the given Field. In other words, this function moves a unit. If the unit was located on some field before, it is removed from there first, so calling this function is all you need for moving a unit. THIS FUNCTION IGNORES SPEED AND DISTANCES - IT JUST WORKS LOW-LEVEL ON THE DATASTRUCTURES.

Parameters:
unit -
field -

setUnitOwner

public void setUnitOwner(Unit unit,
                         Nation nation)
Sets the owner of a unit. If it has an owner before, it is removed from his datastructures first. So calling this function is all you need for changing the owner of a unit.

Parameters:
unit -
nation -

getCity

public City getCity(long id)

getCityIterator

public java.util.Iterator getCityIterator()

addCity

public void addCity(City city,
                    Field field,
                    Nation nation)
Adds a city. Performs all necessary datastructure-updates.

Parameters:
city - a city, may not be null
nation - nation that should own the city, may be null, then, no one will own the city
field - field to place the city on, may not be null, may not contain a city before

setCityOwner

public void setCityOwner(City city,
                         Nation nation)
Sets the owner of to . Both the references city -> nation and nation -> city are updated. Escpecially, if the city belonged to some other nation before, it will be removed from the datastructures of that nation.

Parameters:
city - a city, may not be null
nation - a nation, may not be null

getArrayWidth

public abstract int getArrayWidth()
Returns the width of the internal array that stores the Fields. Useful for loops.


getArrayHeight

public abstract int getArrayHeight()
Returns the height of the internal array that stores the Fields. Useful for loops.


adjustToMapSize

public abstract Coordinate adjustToMapSize(Coordinate coord)
Adjusts the given coordinate to mapsize. This means: If coord already is a map-coordinate, it is returned unchanged. If coord is north/south of the northern/southern edge of map, null is returned. If earth is flat, the same applies to west/east, otherwise, a translation by x * mapWidth is performed and a valid map-coordinate returned. This function is especially useful for recursive functions that are called for some neighbor-coordinates of the original field.


adjustToMapSize

public Coordinate adjustToMapSize(int x,
                                  int y)

isOnMapEdge

public boolean isOnMapEdge(Coordinate coord)
Returns false if field adjusts on all four sides to other fields, otherwise true. No call of adjustToMapSize is performed.


isArrayCoordOnMap

public abstract boolean isArrayCoordOnMap(Coordinate coord)

isArrayCoordOnMap

public abstract boolean isArrayCoordOnMap(int x,
                                          int y)

isOnMap

public abstract boolean isOnMap(Coordinate coord)

isOnMap

public abstract boolean isOnMap(int x,
                                int y)

isOnNorthEdge

public abstract boolean isOnNorthEdge(Coordinate coord)
A coordinate is on the northern edge if there is no map-coordinate that is north, northeast or northwest of it. The same applies to isOnEastEdge, isOnSouthEdge, isOnWestEdge.


isOnEastEdge

public abstract boolean isOnEastEdge(Coordinate coord)
See isOnNorthEdge


isOnSouthEdge

public abstract boolean isOnSouthEdge(Coordinate coord)
See isOnNorthEdge


isOnWestEdge

public abstract boolean isOnWestEdge(Coordinate coord)
See isOnNorthEdge


getAreaCoord

public abstract Coordinate getAreaCoord(Rectangle rect,
                                        Coordinate coord)
Translates the given Coordinate (by map-sizes) to be in the specified rectangle. Returns the translated coordinate, if this is possible, null otherwise. NOTE: Do NOT ALTER either rect nor coord within the implementation. CLONE it instead!!!


getRandomCoordinate

public abstract Coordinate getRandomCoordinate()
Returns a random map-coordinate.


areNeighborCoords

public abstract boolean areNeighborCoords(Coordinate coord1,
                                          Coordinate coord2)
Returns if the two given Coordinates are neighbor-Coordinates. Includes testing for neighbor- coordinates on map-edges, if earth is not flat (= one at the east edge, one at the west edge). Does NOT call adjustToMapSize!


getNeighborFieldDist

public abstract int getNeighborFieldDist(Field f1,
                                         Field f2)
Returns the distance between the two given _neighbor_-fields. NOTE: If f1 and f2 are not neighbor-fields, the return-value of this function is undefined!!!

Parameters:
f1 - any Field
f2 - any Field
Returns:
distance between f1 and f2 as described

getNeighborFields

public abstract Field[] getNeighborFields(Field field)

getNeighborFieldDists

public abstract FieldDistanceInfo[] getNeighborFieldDists(Field field)
Returns an array with FieldDistanceInfo-objects for each neighbor-field of the given Field. The array may contain null-values for non-existent Fields.

Parameters:
field - any Field
Returns:
an array of FieldDistanceInfo-objects as described

getConfiguration

public abstract MapData.Configuration getConfiguration()

areIncompleteMovesAllowed

public boolean areIncompleteMovesAllowed()