wyvern.kernel.maps
Class MapLoader

java.lang.Object
  extended bywyvern.kernel.properties.PList
      extended bywyvern.kernel.maps.MapLoader
All Implemented Interfaces:
GameMapLoader, PropertyList

public class MapLoader
extends PList
implements GameMapLoader

This class can read a GameMap implementation from an XML file.

Version:
1.0, Jul 10, 1999
Author:
Steve Yegge

Field Summary
protected  boolean editor_
          Set in loadMap from the 'editor' parameter.
protected  java.lang.String filename_
           
protected  GameMap map_
           
protected  int monsterCount_
          Keep track of how many monsters or generators we've loaded.
protected  int objectCount_
           
protected static org.python.util.PythonInterpreter python_
           
protected  boolean throwExceptions_
           
 
Fields inherited from class wyvern.kernel.properties.PList
readOnly_
 
Fields inherited from interface wyvern.lib.PropertyList
PROPERTY_PACKAGE
 
Constructor Summary
MapLoader()
           
 
Method Summary
protected  GameMap createMap(org.w3c.dom.Element root)
          Creates and begins initialization of the map, from the file.
 boolean inMapEditor()
          Returns true if we've been told we're loading the map inside the Map Editor.
protected  GameObject instantiateArchetype(Archetype arch)
          Takes an Archetype that we parsed, turns it into a GameObject inheriting from the archetype, and copies certain properties from the Archetype to the object.
protected  GameMap instantiateMap(java.lang.String classname, Dimension size)
          Instantiates the map class, if possible.
 GameMap instantiatePyMap(java.lang.String filename, Dimension size)
          Instantiates a map with a python class in the header.
protected  Terrain instantiateTerrain(Archetype arch)
          Instantiates a Terrain object, possibly returning a flyweight version from the global Terrain cache.
 GameMap loadMap(java.lang.String filename)
          Reads a map from a Wyvern XML map file.
protected  GameMap loadPythonMap(java.lang.String filename)
          Loads a map defined in a python file.
protected  void parseLocalArchetype(org.w3c.dom.Element node)
          Parses and initializes a map-local archetype definition.
protected  Dimension parseMapSize(org.w3c.dom.Element header)
          Gets width & height attributes, if any, as integers.
protected  GameObject parseObject(org.w3c.dom.Element node)
          Parses an object definition, either inline or as an archetype reference, and returns an initialized object.
protected  java.lang.Object readLocation(java.lang.String line)
          Reads a location descriptor - a point, rectangle or perhaps eventually some other shape or list of points.
protected  void readObject(GameMap map, org.w3c.dom.Element node, java.lang.String loc)
          Reads a single object definition from the map.
protected  void readObjects(GameMap map, org.w3c.dom.Element root)
          Reads the object definitions and builds the map.
protected  void setDefaultTerrain(GameMap map)
          Transfers the "terrain" property into instance data.
 void setMapEditor(boolean editor)
          Sets whether the map is being loaded within the Map Editor
protected  void setMapPath(GameMap map, java.lang.String filename)
          Sets the path for the map.
protected  void setObject(GameObject m, GameMap map, int x, int y)
          Places an object in the map.
 void throwExceptions(boolean exceptions)
          Turns exception-throwing on or off.
 boolean throwingExceptions()
          Returns true if this GameMapLoader is configured to throw exceptions when it encounters errors loading a map.
 
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, getProfilingInfo, 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, toString, transientlyRemoveProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

editor_

protected boolean editor_
Set in loadMap from the 'editor' parameter. True if we're loading a map for the map editor.


filename_

protected java.lang.String filename_

map_

protected GameMap map_

objectCount_

protected int objectCount_

throwExceptions_

protected boolean throwExceptions_

monsterCount_

protected int monsterCount_
Keep track of how many monsters or generators we've loaded. If we finish loading the map, and there are no monsters or generators, then we set its default unload-delay to be fairly short, to free up the memory. NOTE: we can't actually DO this until we've fixed the map memory leaks, or it'll just make it worse.


python_

protected static org.python.util.PythonInterpreter python_
Constructor Detail

MapLoader

public MapLoader()
Method Detail

throwExceptions

public void throwExceptions(boolean exceptions)
Description copied from interface: GameMapLoader
Turns exception-throwing on or off. By default, it's off, and the MapLoader simply returns null if it can't load a map. If you set this option, it will throw exceptions that you can use for debugging the problem with your map.

Note: if the map header in the .map file has an "errors-ok" boolean property, it will continue without throwing any exceptions if it fails to load a particular archetype in the map. "errors-ok" maps will only throw exceptions if the map was fundamentally not loadable, e.g. if the map class couldn't be loaded, or the map's XML is corrupted.

Specified by:
throwExceptions in interface GameMapLoader

throwingExceptions

public boolean throwingExceptions()
Description copied from interface: GameMapLoader
Returns true if this GameMapLoader is configured to throw exceptions when it encounters errors loading a map.

Specified by:
throwingExceptions in interface GameMapLoader
Returns:
true if throwing exceptions

setMapEditor

public void setMapEditor(boolean editor)
Description copied from interface: GameMapLoader
Sets whether the map is being loaded within the Map Editor

Specified by:
setMapEditor in interface GameMapLoader
Parameters:
editor - true if being loaded into Map Editor, false if being loaded into an actual game session. If true, sets the transient property "@mapedit" on the loaded maps.

inMapEditor

public boolean inMapEditor()
Description copied from interface: GameMapLoader
Returns true if we've been told we're loading the map inside the Map Editor. Don't use this.

Specified by:
inMapEditor in interface GameMapLoader
Returns:
true if someone called setMapEditor(true)

loadMap

public GameMap loadMap(java.lang.String filename)
                throws java.lang.Exception
Description copied from interface: GameMapLoader
Reads a map from a Wyvern XML map file. Don't try to do this on a map with the same path (i.e. from the same file) that's currently available via World.getMap(), or it'll throw an exception.

Specified by:
loadMap in interface GameMapLoader
Parameters:
filename - the full path to the file. If it ends with ".py", it'll be instantiated as a python map.
Returns:
the map
Throws:
java.lang.Exception

loadPythonMap

protected GameMap loadPythonMap(java.lang.String filename)
                         throws java.lang.Exception
Loads a map defined in a python file.

Parameters:
filename - the full path to the python file
Throws:
java.lang.Exception

setMapPath

protected void setMapPath(GameMap map,
                          java.lang.String filename)
Sets the path for the map. Stored in an instance variable. You can retrieve the path by calling map.getPath().

Parameters:
map - the map
filename - the absolute path to the file.

createMap

protected GameMap createMap(org.w3c.dom.Element root)
                     throws java.lang.Exception
Creates and begins initialization of the map, from the file.

Parameters:
root - the "map" element of the XML mapfile
Returns:
a map with its properties parsed & set.
Throws:
java.lang.Exception

instantiateMap

protected GameMap instantiateMap(java.lang.String classname,
                                 Dimension size)
                          throws java.lang.Exception
Instantiates the map class, if possible.

Parameters:
classname - the GameMap implementation class
size - the width & height (must be non-null)
Returns:
the instantiated GameMap
Throws:
java.lang.Exception

instantiatePyMap

public GameMap instantiatePyMap(java.lang.String filename,
                                Dimension size)
                         throws java.lang.Exception
Instantiates a map with a python class in the header.

Parameters:
size - the map dimensions
filename - the relative path to the mapfile, such as "wiz/rhialto/maps/python_map.py"
Throws:
java.lang.Exception

parseMapSize

protected Dimension parseMapSize(org.w3c.dom.Element header)
                          throws java.lang.Exception
Gets width & height attributes, if any, as integers.

Returns:
a Dimension if both attrs were present, else null
Throws:
java.lang.Exception

setDefaultTerrain

protected void setDefaultTerrain(GameMap map)
                          throws java.lang.Exception
Transfers the "terrain" property into instance data.

Throws:
java.lang.Exception

readObjects

protected void readObjects(GameMap map,
                           org.w3c.dom.Element root)
                    throws java.lang.Exception
Reads the object definitions and builds the map.

Parameters:
map - the map to build
root - the XML element, stripped of the header node
Throws:
java.lang.Exception

readObject

protected void readObject(GameMap map,
                          org.w3c.dom.Element node,
                          java.lang.String loc)
                   throws java.lang.Exception
Reads a single object definition from the map. A definition consists of a location (2 or 4 coordinates) followed by a class or archetype name and (optionally) a list of properties.

This method also places the object in the map.

Parameters:
map - the GameMap to add the object to
node - the DOM Element containing the object to add
loc - the location at which to put the object, e.g. "2 14" or "3 3 10 10".
Throws:
java.lang.Exception

readLocation

protected java.lang.Object readLocation(java.lang.String line)
                                 throws java.lang.Exception
Reads a location descriptor - a point, rectangle or perhaps eventually some other shape or list of points.

Parameters:
line - the line containing the location descriptor
Returns:
a Point or Rectangle object
Throws:
java.lang.NumberFormatException - (or other exception) if the line is malformed
java.lang.Exception

parseObject

protected GameObject parseObject(org.w3c.dom.Element node)
                          throws java.lang.Exception
Parses an object definition, either inline or as an archetype reference, and returns an initialized object.

Parameters:
node - the DOM Element containing the object to add
Returns:
the object parsed and initialized from the node
Throws:
java.lang.Exception

instantiateArchetype

protected GameObject instantiateArchetype(Archetype arch)
                                   throws java.lang.Exception
Takes an Archetype that we parsed, turns it into a GameObject inheriting from the archetype, and copies certain properties from the Archetype to the object.

Parameters:
arch - the Archetype to instantiate
Returns:
an object created from the arch
Throws:
java.lang.Exception

instantiateTerrain

protected Terrain instantiateTerrain(Archetype arch)
                              throws java.lang.Exception
Instantiates a Terrain object, possibly returning a flyweight version from the global Terrain cache.

Parameters:
arch - an archetype of class Terrain
Returns:
a Terrain, or null if couldn't load it
Throws:
java.lang.Exception

setObject

protected void setObject(GameObject m,
                         GameMap map,
                         int x,
                         int y)
                  throws java.lang.Exception
Places an object in the map.

Parameters:
map - the map to put it in
m - the object to place. If it's a Monster, and we're starting monsters, it starts it.
x - the x location to place it at
y - the y location to place it at
Throws:
java.lang.Exception

parseLocalArchetype

protected void parseLocalArchetype(org.w3c.dom.Element node)
                            throws java.lang.Exception
Parses and initializes a map-local archetype definition. Adds the resulting Archetype to the map's local-archetype list.

Parameters:
node - the DOM element representing the local archetype
Throws:
java.lang.Exception