wyvern.kernel.maps
Class DenseMap

java.lang.Object
  extended bywyvern.kernel.properties.PList
      extended bywyvern.kernel.maps.AbstractGameMap
          extended bywyvern.kernel.maps.DenseMap
All Implemented Interfaces:
Container, GameMap, GameMap.UnloadMapConstants, Hookable, MethodHookable, wyvern.kernel.lighting.Obstructions, PropertyList, SelfExternalizable
Direct Known Subclasses:
PythonMap, SokobanLevel, Vault

public class DenseMap
extends AbstractGameMap

This is the standard Wyvern implementation of the GameMap interface. It uses a 2D array of wyvern.kernel.maps.Cell objects, each of which holds one Terrain and zero or more GameObjects.

Version:
1.0, Aug 15, 1997
Author:
Steve Yegge

Nested Class Summary
 
Nested classes inherited from class wyvern.lib.GameMap
GameMap.UnloadMapConstants
 
Field Summary
protected  java.util.ArrayList addRemoveListeners_
          This is instantiated lazily, when someone calls registerAddRemoveListener().
protected  Rectangle cachedBounds_
           
protected  int count_
          Keeps track of the count of objects in the map.
protected  int[][] lighting_
          The global light-intensity array for the map.
protected  MapCell[][] objects_
           
protected  Terrain outOfBoundsTerrain_
          Terrain to use outside the valid bounds of the map.
protected  Dimension size_
           
 
Fields inherited from class wyvern.kernel.maps.AbstractGameMap
ambient_, changeList_, commands_, dark_, defaultTerrain_, dense_, dirtyRegions_, lights_, locals_, mapHooks_, monsters_, name_, opaqueMap_, path_, perPlayer_, players_, proxHooks_, roomHooks_, suspended_, suspendTime_, suspendTimer_, teleporters_, unloadImmediately_, unloading_, visUpdates_, vpath_
 
Fields inherited from class wyvern.kernel.properties.PList
readOnly_
 
Fields inherited from interface wyvern.lib.GameMap
MAX_HEIGHT, MAX_SEARCH_RADIUS, MAX_WIDTH
 
Fields inherited from interface wyvern.lib.PropertyList
PROPERTY_PACKAGE
 
Fields inherited from interface wyvern.lib.GameMap.UnloadMapConstants
MAP_CONTAINS_PLAYERS, MAP_NOT_LOADED, SUCCESS
 
Constructor Summary
DenseMap()
          Constructs a new DenseMap with no width or height.
DenseMap(Dimension size)
          Constructs a new DenseMap
DenseMap(int width, int height)
          Constructs a new DenseMap
 
Method Summary
 void add(int x, int y, GameObject obj)
          Adds the specified object to the map at the specified location.
 void add(int x, int y, GameObject obj, boolean invalidate)
          Adds specified object, optionally invalidating.
protected  void addIntensityArray(int x, int y, int[][] array, boolean add)
          Adds or subtracts the contribution from a single LightSource into the map's internal lighting array.
 void adjustIntensity(int x, int y, int intensity)
          Manually adjusts the light intensity by a fixed amount at the passed (x, y) location.
protected  boolean checkBounds(int x, int y)
          Makes sure the passed point lies in our bounds.
protected  boolean checkBounds(int x, int y, int w, int h)
          Checks to make sure rectangle is contained within our bounds.
protected  void createLightMatrix()
          Creates and initializes the ambient-lighting matrix.
 int dismantle()
          Unloads the map.
 Rectangle getBounds()
          Returns the bounds of the map.
 int getLightLevel(int x, int y)
          Returns the lighting level at the specified location.
 int getObjectCount()
          Returns an approximate count of the object references currently in this map.
protected  Cell getOrCreatecell(int x, int y)
          Returns the Cell at (x, y), creating it if necessary.
 Terrain getOutOfBoundsTerrain()
          Returns what we use for the out-of-bounds terrain.
 PropertyList getProperties(int x, int y)
          You can get and set properties on a specific map location.
 PropertyList getPropertyList(int x, int y)
          Returns the property list for the given (x, y) map location, creating the list if necessary.
 Dimension getSize()
          Returns the size of the map.
 void initialize(Dimension size)
          Initializes the map to the specified width and height.
 void initialize(int width, int height)
          Initializes the map to the specified width and height.
 boolean isSparse()
          Returns true if this map implementation is sparse.
protected  java.util.Iterator iterator(int x, int y)
          Returns Iterator on objects at (x, y);
 java.util.Set keySet()
          Returns the set of nonempty points in this map.
protected  java.util.List objectsAt(int x, int y)
          Returns the actual list of objects at (x, y).
protected  java.util.Iterator objectsInRect(int x, int y, int w, int h)
          Implements GameMap.objectsInRect.
 void registerAddRemoveListener(MapAddRemoveListener listener)
          Registers a listener for adding/removing objects.
 void remove(int x, int y, GameObject obj)
          Removes an object from the map at the specified location.
 void remove(int x, int y, GameObject obj, boolean inval)
          Removes an object at the specified map location (x, y).
 void setOutOfBoundsTerrain(java.lang.String terrain)
          Sets the terrain to use outside the valid map bounds.
 void setOutOfBoundsTerrain(Terrain t)
          Sets the terrain to use outside the valid map bounds.
 void setSize(Dimension new_size)
          Changes the size of the map.
 void setTerrain(int x, int y, Terrain t)
          Sets the terrain for this map location.
 Terrain terrainAt(int x, int y)
          Implements GameMap.terrainAt()
 void unregisterAddRemoveListener(MapAddRemoveListener listener)
          Unregisters a listener for adding/removing objects.
 void visitRect(int x, int y, int w, int h, Visitor v)
          Visits all objects in a rectangle.
 
Methods inherited from class wyvern.kernel.maps.AbstractGameMap
addHook, addLight, addLocalArchetype, addMapLifecycleListener, addMethodHook, addMonster, addPlayer, addPlayerListener, addPropertyChangeListener, addPropertyChangeListener, addProximityHook, addRoomHook, addTeleporter, broadcast, broadcast, doneLoading, enableVisUpdates, findAt, findAt, findAt, findAt, findByName, findByName, findFreeSpot, findFreeSpot, findInRect, findTop, findTop, findTop, findUnder, fireMapLoaded, fireMapResumed, fireMapSuspended, fireMapUnloaded, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getArea, getClaim, getCommand, getDangerLevel, getDefaultTerrain, getExits, getFavorites, getHookList, getHooks, getLightingLevel, getLocalArchetype, getLocalArchetypes, getMonsterList, getMusic, getName, getObjectsAt, getPath, getPlayerList, getProfilingInfo, getPropertyChangeListeners, getPropertyChangeListeners, getProximityHookList, getResource, getRestartLoc, getRoomHookList, getStartLocation, getSuspendTime, getTerrainInfo, getTerrainRecord, getTerrainRecord, getUnloadDelay, getUpdateList, getVirtualPath, hasListeners, hasObstructions, initCommon, inMapEditor, invalidate, isBag, isEmpty, isEmpty, isInventory, isLit, isMap, isOpaque, isPerPlayer, isSuspended, isUnloaded, notifyPlayerListeners, readData, recomputeLights, registerCommand, removeFirst, removeHook, removeLight, removeLocalArchetype, removeMapLifecycleListener, removeMethodHook, removePlayer, removePlayerListener, removePropertyChangeListener, removePropertyChangeListener, removeProximityHook, removeRoomHook, removeTeleporter, resetOpaqueFlag, resetOpaqueFlag, resume, runFailedPostHook, runMethodHook, runPostHook, runPreHook, setAutoload, setDangerLevel, setDefaultTerrain, setDefaultTerrain, setFavorite, setLightingLevel, setLit, setName, setPath, setPerPlayer, setStartLocation, setTerrain, setUnloadDelay, shouldWrite, startSuspendTimer, startUnloadTimer, startUnloadTimer, suspend, toString, trackSpecialRemoves, unload, unregisterCommand, updateOpaque, updateTerrainCache, visitObjectsAt, visUpdate, wakeAllMonsters, wakeMonsters, wasAutoloaded, writeData
 
Methods inherited from class wyvern.kernel.properties.PList
addProperty, addTransientProperty, adjustDoubleProperty, adjustIntProperty, adjustLongProperty, adjustTransientDoubleProperty, adjustTransientIntProperty, adjustTransientLongProperty, countLocalProperties, debugProperties, dismantlePropertyList, getDoubleProperty, getInheritedProperty, getInteger, getIntProperty, getLocalProperties, getLocalProperty, getLongProperty, getParent, getPersistentDoubleProperty, getPersistentIntProperty, getPersistentLocalProperties, getPersistentLongProperty, getPersistentProperty, getProperties, getProperties, getPropertiesIncludingTransients, getPropertiesIncludingTransients, getProperty, getSerializableProperties, getSerializableProperty, getStringProperty, getTransientDoubleProperty, getTransientIntProperty, getTransientLongProperty, getTransientProperties, getTransientProperty, hasLocalProperty, hasPersistentProperty, hasProperty, hasTransientProperty, inheritProperty, isReadOnly, isRemoved, isTransientlyRemoved, printLocalProperties, printProperties, printProperties, printTransientProperties, removeProperty, removeTransientProperty, setDoubleProperty, setIntProperty, setLongProperty, setParent, setProperty, setReadOnly, setTransientDoubleProperty, setTransientIntProperty, setTransientLongProperty, setTransientProperty, toString, transientlyRemoveProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface wyvern.lib.PropertyList
addProperty, addTransientProperty, adjustDoubleProperty, adjustIntProperty, adjustLongProperty, adjustTransientDoubleProperty, adjustTransientIntProperty, adjustTransientLongProperty, countLocalProperties, getDoubleProperty, getInheritedProperty, getIntProperty, getLocalProperties, getLocalProperty, getLongProperty, getParent, getPersistentDoubleProperty, getPersistentIntProperty, getPersistentLocalProperties, getPersistentLongProperty, getPersistentProperty, getProperties, getProperties, getPropertiesIncludingTransients, getPropertiesIncludingTransients, getProperty, getSerializableProperties, getSerializableProperty, getStringProperty, getTransientDoubleProperty, getTransientIntProperty, getTransientLongProperty, getTransientProperties, getTransientProperty, hasLocalProperty, hasPersistentProperty, hasProperty, hasTransientProperty, inheritProperty, isReadOnly, isRemoved, isTransientlyRemoved, printLocalProperties, printProperties, printProperties, printTransientProperties, removeProperty, removeTransientProperty, setDoubleProperty, setIntProperty, setLongProperty, setParent, setProperty, setReadOnly, setTransientDoubleProperty, setTransientIntProperty, setTransientLongProperty, setTransientProperty, toString, transientlyRemoveProperty
 

Field Detail

size_

protected Dimension size_

objects_

protected MapCell[][] objects_

lighting_

protected int[][] lighting_
The global light-intensity array for the map. If the map is completely lit (doesn't have "dark" property), then the array will be null.


outOfBoundsTerrain_

protected Terrain outOfBoundsTerrain_
Terrain to use outside the valid bounds of the map.


cachedBounds_

protected Rectangle cachedBounds_

count_

protected int count_
Keeps track of the count of objects in the map. It's only an approximate count, since I don't want to use even -more- memory making a per-map (weak) cache of objects we've counted. Someday I might. It counts large objects multiple times, since it's really counting object references, but that's probably OK, since it's a proxy for memory usage, and large objects have slightly higher mem usage (larger image, more locs in PointList, larger tile-appearance array, etc.) It's less memory than individual 1x1 objects, but whatever. It's an OK proxy.


addRemoveListeners_

protected java.util.ArrayList addRemoveListeners_
This is instantiated lazily, when someone calls registerAddRemoveListener().

Constructor Detail

DenseMap

public DenseMap()
Constructs a new DenseMap with no width or height. Caller has to follow this with a call to initialize(), or the DenseMap won't function at all.


DenseMap

public DenseMap(int width,
                int height)
Constructs a new DenseMap

Parameters:
width - the map width
height - the map height

DenseMap

public DenseMap(Dimension size)
Constructs a new DenseMap

Parameters:
size - the map size
Method Detail

initialize

public void initialize(int width,
                       int height)
Initializes the map to the specified width and height. You can only call this once.

Parameters:
width - map width
height - map width

initialize

public void initialize(Dimension size)
Initializes the map to the specified width and height. You can only call this one time per map. The map uses this to create its internal data structures, if you didn't call one of the constructors that takes a size.

Parameters:
size - the size for the map
Throws:
java.lang.IllegalStateException - if the map has already been initialized

createLightMatrix

protected void createLightMatrix()
Creates and initializes the ambient-lighting matrix.


terrainAt

public Terrain terrainAt(int x,
                         int y)
Implements GameMap.terrainAt()

Parameters:
x - map x location
y - map y location
Returns:
the Terrain object at (x, y)
See Also:
GameMap.terrainAt(int, int)

visitRect

public void visitRect(int x,
                      int y,
                      int w,
                      int h,
                      Visitor v)
Visits all objects in a rectangle.

Parameters:
x - left edge
y - top edge
w - rect width
h - rect height
v - the Visitor to inflict on the objects
See Also:
Visitor

add

public void add(int x,
                int y,
                GameObject obj)
Adds the specified object to the map at the specified location.

Specified by:
add in interface GameMap
Overrides:
add in class AbstractGameMap
Parameters:
x - map x location
y - map y location
obj - the object to add

add

public void add(int x,
                int y,
                GameObject obj,
                boolean invalidate)
Adds specified object, optionally invalidating.

Parameters:
x - map x location
y - map y location
obj - the object to add
invalidate - true to invalidate map on object's bounds

remove

public void remove(int x,
                   int y,
                   GameObject obj)
Removes an object from the map at the specified location. Automatically invalidates on object's bounds.

Specified by:
remove in interface GameMap
Overrides:
remove in class AbstractGameMap
Parameters:
x - map x location
y - map y location
obj - the object reference to remove. Does nothing if the specified obj reference isn't found at (x, y).

remove

public void remove(int x,
                   int y,
                   GameObject obj,
                   boolean inval)
Removes an object at the specified map location (x, y).

Parameters:
x - map x location
y - map y location
obj - the object reference to remove. Does nothing if the specified obj reference isn't found at (x, y).
inval - true to invalidate on object's bounds

setTerrain

public void setTerrain(int x,
                       int y,
                       Terrain t)
Sets the terrain for this map location.

Specified by:
setTerrain in interface GameMap
Overrides:
setTerrain in class AbstractGameMap
Parameters:
x - map x location
y - map y location
t - Terrain to put there

addIntensityArray

protected void addIntensityArray(int x,
                                 int y,
                                 int[][] array,
                                 boolean add)
Adds or subtracts the contribution from a single LightSource into the map's internal lighting array.

Specified by:
addIntensityArray in class AbstractGameMap
Parameters:
x - the x location at which to apply the array
y - the y location at which to apply the array
array - the array to apply. The light source is typically centered in the array, but this method doesn't do any coordinate translation. If you pass (x=3, y=3) and an array where (width=5, height=5), the lighting will be updated in the map from locations (3, 3) to (8, 8).
add - true to add the array; false to subtract it

adjustIntensity

public void adjustIntensity(int x,
                            int y,
                            int intensity)
Manually adjusts the light intensity by a fixed amount at the passed (x, y) location. Useful for objects that want to manage their own illumination, or for tweaking an area of a map to be brighter or darker than the ambient intensity.

Specified by:
adjustIntensity in interface GameMap
Specified by:
adjustIntensity in class AbstractGameMap
Parameters:
x - the x location to adjust
y - the y location to adjust
intensity - the +/- amount by which to adjust the lighting level

getLightLevel

public int getLightLevel(int x,
                         int y)
Returns the lighting level at the specified location. Takes into account the ambient (overall) intensity, as well as contributions from any local light sources. This is used, for instance, by the camera system to decide how dark to draw the square.

Specified by:
getLightLevel in interface GameMap
Overrides:
getLightLevel in class AbstractGameMap
Parameters:
x - map x location
y - map y location
Returns:
the cumulative lighting level at (x, y). If zero or negative, the square is completely dark.

getSize

public Dimension getSize()
Returns the size of the map.

Returns:
the size of the map

setSize

public void setSize(Dimension new_size)
Changes the size of the map.

Parameters:
new_size - the size for the map
Throws:
java.lang.IllegalStateException - if you call it from outside the map editor code.

getBounds

public Rectangle getBounds()
Returns the bounds of the map.

Returns:
the map bounds

isSparse

public final boolean isSparse()
Returns true if this map implementation is sparse.


setOutOfBoundsTerrain

public void setOutOfBoundsTerrain(Terrain t)
Sets the terrain to use outside the valid map bounds.


getOutOfBoundsTerrain

public Terrain getOutOfBoundsTerrain()
Returns what we use for the out-of-bounds terrain.


setOutOfBoundsTerrain

public void setOutOfBoundsTerrain(java.lang.String terrain)
                           throws java.lang.Exception
Sets the terrain to use outside the valid map bounds.

Throws:
java.lang.Exception

objectsInRect

protected java.util.Iterator objectsInRect(int x,
                                           int y,
                                           int w,
                                           int h)
Implements GameMap.objectsInRect. This is a rather expensive (and, as currently implemented, inefficient) method. I wonder how useful it will be, or if it will typically be used for special cases like finding all the walls.

Special Note: this will NOT return any Terrain objects; they must be enumerated separately.

Specified by:
objectsInRect in class AbstractGameMap
Parameters:
x - rect x
y - rect y
w - rect width
h - rect height
Returns:
an iterator over the passed rectangle

iterator

protected java.util.Iterator iterator(int x,
                                      int y)
Returns Iterator on objects at (x, y);

Specified by:
iterator in class AbstractGameMap
Parameters:
x - map x
y - map y
Returns:
an Iterator that does NOT include the Terrain at (x, y) (Can return null if out-of-bounds). The iterator can be empty, if it's in bounds and there are no non-Terrain objects there.

objectsAt

protected java.util.List objectsAt(int x,
                                   int y)
Returns the actual list of objects at (x, y). Caller should not change the list! Returns the actual list of objects in this cell. Don't mess with it. You can iterate through it with a for-loop or while-loop. (It's an array-backed list, so this is just as fast as using an Iterator).

Specified by:
objectsAt in class AbstractGameMap

checkBounds

protected boolean checkBounds(int x,
                              int y)
Makes sure the passed point lies in our bounds.

Parameters:
x - map x
y - map y
Returns:
true if it's in bounds

checkBounds

protected boolean checkBounds(int x,
                              int y,
                              int w,
                              int h)
Checks to make sure rectangle is contained within our bounds.

Parameters:
x - rect x
y - rect y
w - rect width
h - rect height
Returns:
true if the entire rectangle is in bounds

getOrCreatecell

protected Cell getOrCreatecell(int x,
                               int y)
Returns the Cell at (x, y), creating it if necessary.


keySet

public java.util.Set keySet()
Returns the set of nonempty points in this map. May not be implemented for all map types, but it's guaranteed to be implemented if isSparse() returns true for this map.


dismantle

public int dismantle()
Unloads the map. Removes all references to all objects in the map to help avoid memory leaks.

Specified by:
dismantle in interface GameMap
Overrides:
dismantle in class AbstractGameMap

getObjectCount

public int getObjectCount()
Returns an approximate count of the object references currently in this map.

Returns:
the count of object references in this map. Only counts each Terrain object once, even if the Terrain object has multiple references in the map. For instance, in most maps, grass is a flyweight, and there's only one instance of a Terrain object for all the grass in the map.

registerAddRemoveListener

public void registerAddRemoveListener(MapAddRemoveListener listener)
Registers a listener for adding/removing objects.

Parameters:
listener - the listener

unregisterAddRemoveListener

public void unregisterAddRemoveListener(MapAddRemoveListener listener)
Unregisters a listener for adding/removing objects.

Parameters:
listener - the MapAddRemoveListener

getProperties

public PropertyList getProperties(int x,
                                  int y)
You can get and set properties on a specific map location.

This can be handy, for example, if you want to do a very fast check to see if a particular kind of object is at some location (x, y). The object should set a property on the cell saying that it's there, and then others can look for that property. It's faster than going through all the objects at (x, y), checking each one to see if it's the type you're looking for.

Parameters:
x - map x location
y - map y location
Returns:
the list of properties at that location, or NULL if nobody has set any properties at that location.

getPropertyList

public PropertyList getPropertyList(int x,
                                    int y)
Returns the property list for the given (x, y) map location, creating the list if necessary.

You call this method if you're about to set a property in the list. It creates an empty PropertyList for this map location, if nobody has set any properties at that (x, y) yet.

PLEASE don't call this if you're just checking to see if a location has a certain property. Only call it if you're going to set or adjust a property value for the location.

Parameters:
x - map x location
y - map y location
Returns:
the list of properties at (x, y) for this map. Creates the list if necessary, so the return value will always be non-null.