wyvern.kernel.maps
Class MapWriter

java.lang.Object
  extended bywyvern.kernel.maps.MapWriter
Direct Known Subclasses:
PythonMapWriter

public class MapWriter
extends java.lang.Object

Writes out a map to a mapfile.

Version:
1.0, Jan 25, 1998
Author:
Steve Yegge

Field Summary
protected  java.util.HashSet cache_
           
protected  java.util.HashMap externCache_
           
protected  java.lang.String filename_
           
protected  java.io.PrintWriter out_
           
protected  java.util.HashSet terrainCache_
           
 
Constructor Summary
MapWriter()
           
 
Method Summary
protected  boolean compareObjects(GameObject o1, GameObject o2)
          Compares 2 objects to see if they'd write to the mapfile identically.
protected  int extendLeftRight(GameMap map, Point start, int dir, Rectangle bounds, GameObject obj)
          Checks to the left or right of the passed location to see how far this type of object extends.
protected  int extendUpDown(GameMap map, GameObject obj, Rectangle bounds, int dir, int y, int x1, int x2)
          Checks above or below the passed horizontal strip of objects to see how far the strip can be extended up or down.
protected  GameObject findClone(GameMap map, GameObject obj, Point loc)
          Looks for an object at the specified location that exactly matches the passed object.
 java.lang.String getMapClassName(GameMap map)
          Returns an appropriate map-class name for writing to the file.
protected  void iterateObjects(GameMap map, java.util.Iterator it, Point p)
          Iterates over the objects at a particular map location, writing them out in the largest rectangles possible.
protected  void openFile(java.lang.String filename)
          Opens the file for writing, and sets global variables for the filename and output stream.
 boolean overlapping(GameObject o1, GameObject o2)
          Returns true if two otherwise identical objects are overlapping.
protected  void printMapProps(GameMap map, java.io.PrintWriter out)
          Prints map properties to output stream.
protected  void writeDenseMap(GameMap map)
          Writes a dense map.
protected  void writeLocalArchetypes(GameMap map)
          Writes any map-local archetypes to the mapfile.
 void writeMap(GameMap map, java.lang.String filename)
          Writes the entire contents of the specified map.
protected  void writeObject(GameMap map, GameObject obj, Point p)
          Writes an object - called by iterateObjects, after checking if the object is in the cache, discarding players and transient properties, etc.
protected  void writeObject(GameObject obj, int x, int y)
          Writes XML version of a single object.
 void writeObjectAndNeighbors(GameMap map, GameObject obj, Point p)
          Writes an object and all its identical neighbors to a rectangle.
protected  void writeRectangle(GameObject obj, int x, int y, int w, int h)
          Writes out a rectangle in the map containing the passed object at every location in the rectangle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cache_

protected java.util.HashSet cache_

terrainCache_

protected java.util.HashSet terrainCache_

out_

protected java.io.PrintWriter out_

filename_

protected java.lang.String filename_

externCache_

protected java.util.HashMap externCache_
Constructor Detail

MapWriter

public MapWriter()
Method Detail

writeMap

public void writeMap(GameMap map,
                     java.lang.String filename)
              throws java.io.IOException
Writes the entire contents of the specified map.

Parameters:
map - the Map to write
filename - the full path to the map file to write
Throws:
java.io.IOException - if the write fails

writeDenseMap

protected void writeDenseMap(GameMap map)
                      throws java.io.IOException
Writes a dense map.

Parameters:
map - the map to write
Throws:
java.io.IOException - if the write fails

getMapClassName

public java.lang.String getMapClassName(GameMap map)
Returns an appropriate map-class name for writing to the file.

Parameters:
map - the map to write
Returns:
name the name to use in the XML "map" element header

printMapProps

protected void printMapProps(GameMap map,
                             java.io.PrintWriter out)
Prints map properties to output stream.


writeLocalArchetypes

protected void writeLocalArchetypes(GameMap map)
Writes any map-local archetypes to the mapfile.

Parameters:
map - the map to write

iterateObjects

protected void iterateObjects(GameMap map,
                              java.util.Iterator it,
                              Point p)
Iterates over the objects at a particular map location, writing them out in the largest rectangles possible.

Parameters:
map - the map we're writing
it - an Iterator whose elements are GameObjects
p - the Point (map location) we're iterating

writeObject

protected void writeObject(GameMap map,
                           GameObject obj,
                           Point p)
Writes an object - called by iterateObjects, after checking if the object is in the cache, discarding players and transient properties, etc. Then calls writeObjectAndNeighbors to externalize it.


writeObjectAndNeighbors

public void writeObjectAndNeighbors(GameMap map,
                                    GameObject obj,
                                    Point p)
Writes an object and all its identical neighbors to a rectangle.

Parameters:
map - the GameMap we're writing
obj - the object to write into the map
p - the map location we're writing

writeObject

protected void writeObject(GameObject obj,
                           int x,
                           int y)
Writes XML version of a single object.

Parameters:
obj - the object to write into the map
x - the x location for the object
y - the y location for the object

writeRectangle

protected void writeRectangle(GameObject obj,
                              int x,
                              int y,
                              int w,
                              int h)
Writes out a rectangle in the map containing the passed object at every location in the rectangle.

Parameters:
obj - the object to write into the map
x - rectangle left
y - rectangle top
w - rectangle width
h - rectangle height

compareObjects

protected boolean compareObjects(GameObject o1,
                                 GameObject o2)
Compares 2 objects to see if they'd write to the mapfile identically.

Parameters:
o1 - the first object
o2 - the second object
Returns:
true if they would externalize identically

findClone

protected GameObject findClone(GameMap map,
                               GameObject obj,
                               Point loc)
Looks for an object at the specified location that exactly matches the passed object.

Parameters:
map - the map to look in
obj - the object to find a clone of
loc - the point at which to look
Returns:
an identical object, or null if not found

extendLeftRight

protected int extendLeftRight(GameMap map,
                              Point start,
                              int dir,
                              Rectangle bounds,
                              GameObject obj)
Checks to the left or right of the passed location to see how far this type of object extends.

Parameters:
map - the map we're writing
start - the start location to go left or right from
dir - -1 to go left, +1 to go right
bounds - the GameMap bounds, so we don't go OOB
obj - the object we're writing
Returns:
the number of squares left or right that have an object identical to the passed object.

extendUpDown

protected int extendUpDown(GameMap map,
                           GameObject obj,
                           Rectangle bounds,
                           int dir,
                           int y,
                           int x1,
                           int x2)
Checks above or below the passed horizontal strip of objects to see how far the strip can be extended up or down.

Parameters:
map - the GameMap we're writing
obj - the type of object being written
bounds - the GameMap bounds, so we don't go OOB
dir - -1 for up, 1 for down
y - map y coordinate of strip
x1 - left side of strip
x2 - right side of strip
Returns:
the number of squares above or below that have strips of objects identical to the passed object.

overlapping

public boolean overlapping(GameObject o1,
                           GameObject o2)
Returns true if two otherwise identical objects are overlapping.


openFile

protected void openFile(java.lang.String filename)
                 throws java.io.IOException
Opens the file for writing, and sets global variables for the filename and output stream.

Parameters:
filename - the file to open.
Throws:
java.io.IOException