wyvern.kernel.maps
Class Frame

java.lang.Object
  extended bywyvern.kernel.maps.Frame
All Implemented Interfaces:
DisplayPolicy, LightingConstants, Visitor
Direct Known Subclasses:
EyeFrame

public class Frame
extends java.lang.Object
implements DisplayPolicy, Visitor, LightingConstants

This is the DisplayPolicy implementation that handles sending map updates to the client map view. Each MapCamera in the game has one Frame object (at a time) for gathering the updates.

When it's time to gather the frame data, a DisplayPolicy is given a set of invalid map rectangles. It's the DisplayPolicy's responsibility to query the map for the information needed to generate a "snapshot" of the map for the client.

This DisplayPolicy implementation gets all the map objects in its invalid regions and sorts them into "buckets" based on their DrawingLayer properties. The Frame decides how many objects in each layer to send to the client. For some layers it may be only one; for other layers it may choose to send 2 or more objects per cell.

The Frame will provide a mechanism for "input filters" to filter the object stream coming from the map. This is the way, for example, that the visibility and lighting-model computations are merged into the data that goes to the client.

Version:
1.0, Aug 28, 1997, 1.1, Sept 7, 1998
Author:
Steve Yegge

Nested Class Summary
protected  class Frame.VisibilityConstants
          Basically we hardcode all the properties on the agent that affect what they can see.
 
Field Summary
protected  Commandable agent_
           
protected  short[] allTerrain_
          A packed 2D array for sending over all the terrain.
protected  wyvern.kernel.lighting.RayShadowCaster caster_
           
protected  boolean completeUpdate_
          Set to true when visiting map objects during a complete frame refresh, false when doing a partial refresh.
protected  java.util.HashSet frameCache_
           
protected  boolean handheld_
          True if player is playing on a PDA.
protected  int height_
           
protected  boolean lightOff_
           
protected  GameMap map_
           
protected  int nightVision_
          The amount to add to the light level based on racial night vision.
protected  java.util.ArrayList objects_
          This set contains a "sparse array" of the objects to send to the client.
protected  java.util.HashMap terrain_
          Information about the terrain & borders - used for smaller updates where the whole view isn't being sent over.
protected  int[][] terrainInfo_
          A 2D array of terrain records used in drawing borders.
protected  BitMatrix updateMap_
           
protected  int viewx_
           
protected  int viewy_
           
protected  Frame.VisibilityConstants visCache_
          This is an optimization, plain and simple.
protected  int width_
           
 
Fields inherited from interface wyvern.lib.properties.LightingConstants
DARKNESS, DAYLIGHT, DIM, DUSK, GLOOM, NUM_SHADES, SHADED, TWILIGHT
 
Constructor Summary
Frame(Commandable agent)
          Constructs a new Frame object with a default 1x1 bounding rectangle, anchored at (0, 0) in the map.
Frame(Commandable agent, int x, int y, int width, int height)
          Constructs a new Frame object with the specified bounds.
 
Method Summary
protected  void add(GameObject obj)
          Adds an object to the specified layer in the snapshot.
protected  void addDescriptor(int x, int y, int layer, int tile, GameObject obj, int alpha)
          Adds an object appearance descriptor to the sorted frame list.
 void addObject(GameObject obj)
          Adds an object to the frame.
 void addTerrain(int x, int y)
          Adds a terrain object.
 void addUpdateRegions(MapDirtyRegions updates)
          Adds in all the cells in the map that have become invalid since this method was last called.
protected  void cacheVisFlags()
          Looks up any properties on the agent we need in advance, and remembers them.
 boolean canSee(int x, int y)
          Returns true if the player has a clear line of sight to this square.
 boolean checkAdd(GameObject obj)
          Returns true if this object should be added to the snapshot.
protected  int computeDistance(int x, int y)
          Computes the distance from the passed point to the caster.
protected  int computeNightVision(int distance)
          Returns the lighting level contribution from night vision.
protected  boolean computeXRayVision(int distance)
          Returns true if the agent's x-ray vision allows them to see this point regardless of line-of-sight.
 void discardChanges()
          Discards any information the frame is currently caching.
 void doRayCast()
          Invokes a raycaster to see what's visible to the frame.
static java.lang.String getProfilingInfo()
          Returns a profiling string.
 java.lang.Object[] getSparseTerrainInfo()
          Returns information about the terrain in, and immediately adjacent to, the update rectangles.
 int[][] getTerrainInfo()
          Returns the array of terrain records produced in the last call to grabAll().
 void grabAll()
          Grabs the entire view from the map.
 boolean grabLight(int x, int y)
          Grabs the lighting tile at (x, y).
 boolean grabLocation(int tcol, int trow)
          Grabs all the objects at the given map location and adds their appearances to the frame.
 void grabObjects()
          This is called by the CameraManager to tell the Frame which regions of the camera have become invalid, so it can refresh them.
 boolean isHandheld()
          Returns true if player is playing on a handheld device.
 short[] makeSparseArray()
          Returns an array of the objects to draw in the client view.
 void printSparseTree()
          Prints all the objects in the tree (debugging method).
 void setBounds(int x, int y, int w, int h, GameMap map)
          Sets the bounds of the frame.
 void setBounds(int x, int y, int w, int h, GameMap map, boolean raycast)
          Sets the bounds of the frame.
 void setHandheld(boolean handheld)
          Sets whether we're playing on a handheld.
static short stuffPoint(short x, short y)
          Stuffs two short values ( which must each be < 256 ) into a single short value with x in the high byte and y in the low byte.
 void visit(GameObject obj)
          Visits an object in the map and puts its appearance in the frame.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

viewx_

protected int viewx_

viewy_

protected int viewy_

width_

protected int width_

height_

protected int height_

map_

protected GameMap map_

updateMap_

protected BitMatrix updateMap_

lightOff_

protected boolean lightOff_

terrain_

protected java.util.HashMap terrain_
Information about the terrain & borders - used for smaller updates where the whole view isn't being sent over. Each square that needs to be updated has a little 3x3 array of terrain "records" containing neighboring border information.


allTerrain_

protected short[] allTerrain_
A packed 2D array for sending over all the terrain.


terrainInfo_

protected int[][] terrainInfo_
A 2D array of terrain records used in drawing borders. Replaces allTerrain_ when borders are turned on in the client.


frameCache_

protected java.util.HashSet frameCache_

objects_

protected java.util.ArrayList objects_
This set contains a "sparse array" of the objects to send to the client. They're sorted in three dimensions: first on Z-depth (i.e. drawing layer), next horizontally, then vertically. Each element of the set is an ObjDescriptor containing the data to send to the client, including the bitmap tile number and the x/y view offsets at which to draw the bitmap.


completeUpdate_

protected boolean completeUpdate_
Set to true when visiting map objects during a complete frame refresh, false when doing a partial refresh.


caster_

protected wyvern.kernel.lighting.RayShadowCaster caster_

visCache_

protected Frame.VisibilityConstants visCache_
This is an optimization, plain and simple. I don't want to have to do property lookups on the player/monster for every single object we're visiting - it's expensive. So before I start iterating over the objects, I look up all the built-in visibility properties I've hardcoded into this crappy implementation, and store them as flags on this object for quick lookup.


agent_

protected Commandable agent_

nightVision_

protected int nightVision_
The amount to add to the light level based on racial night vision.


handheld_

protected boolean handheld_
True if player is playing on a PDA.

Constructor Detail

Frame

public Frame(Commandable agent)
Constructs a new Frame object with a default 1x1 bounding rectangle, anchored at (0, 0) in the map. The caller should use setBounds() to change the frame bounds before using it.


Frame

public Frame(Commandable agent,
             int x,
             int y,
             int width,
             int height)
Constructs a new Frame object with the specified bounds.

Parameters:
x - the left (x) coord of the snapshot (in world coords)
y - the top (y) coord of the snapshot (in world coords)
width - the width of the snapshot we're taking
height - the height of the snapshot we're taking
Method Detail

grabObjects

public void grabObjects()
This is called by the CameraManager to tell the Frame which regions of the camera have become invalid, so it can refresh them.

The Frame implementation gets a Visitor and visits the map at all the invalid spots. It also gathers the terrain. The data is sorted into drawing layers for the client.

Before calling this method, the MapCamera must add all the invalid rectangles to the Frame with calls to addUpdateRect(), so the frame knows what map squares to visit.

Specified by:
grabObjects in interface DisplayPolicy

grabLocation

public boolean grabLocation(int tcol,
                            int trow)
Grabs all the objects at the given map location and adds their appearances to the frame.

Parameters:
tcol - map x location (in map coordinate space)
trow - map y location (in map coordinate space)
Returns:
true if we could see this location

grabLight

public boolean grabLight(int x,
                         int y)
Grabs the lighting tile at (x, y).

Parameters:
x - map x location
y - map y location
Returns:
true if we're drawing a completely dark tile at this location (i.e. player can't see there)

computeNightVision

protected int computeNightVision(int distance)
Returns the lighting level contribution from night vision. Only takes into account the point's distance from the agent,

Parameters:
distance - the distance from the point to the agent
Returns:
how much to add to the lighting level at this point

computeXRayVision

protected boolean computeXRayVision(int distance)
Returns true if the agent's x-ray vision allows them to see this point regardless of line-of-sight.

Parameters:
distance - the distance from the agent to the point
Returns:
true if they can see it via x-ray vision

computeDistance

protected int computeDistance(int x,
                              int y)
Computes the distance from the passed point to the caster.

Parameters:
x - map x
y - map y
Returns:
distance

grabAll

public void grabAll()
Grabs the entire view from the map. Called when camera is completely invalid.

Specified by:
grabAll in interface DisplayPolicy

doRayCast

public void doRayCast()
Invokes a raycaster to see what's visible to the frame.

Specified by:
doRayCast in interface DisplayPolicy

visit

public void visit(GameObject obj)
Visits an object in the map and puts its appearance in the frame.

Specified by:
visit in interface Visitor
Parameters:
obj - the object (a GameObject) to examine

addObject

public void addObject(GameObject obj)
Adds an object to the frame. Calls checkAdd() to see if it wants the object; subclasses can override checkAdd() if they like. This is a callback for the Visitor that the Frame employs to get its objects.

Parameters:
obj - the object to consider for adding

setBounds

public void setBounds(int x,
                      int y,
                      int w,
                      int h,
                      GameMap map,
                      boolean raycast)
Sets the bounds of the frame. (Tells the frame where the camera is currently looking.)

Specified by:
setBounds in interface DisplayPolicy
Parameters:
x - map x
y - map y
w - view width
h - view height
map - the map
raycast - true to do a raycast

setBounds

public void setBounds(int x,
                      int y,
                      int w,
                      int h,
                      GameMap map)
Sets the bounds of the frame. (Tells the frame where the camera is currently looking.)

Specified by:
setBounds in interface DisplayPolicy
Parameters:
x - map x
y - map y
w - view width
h - view height
map - the map

discardChanges

public void discardChanges()
Discards any information the frame is currently caching. Used when the frame is invalidated.


canSee

public boolean canSee(int x,
                      int y)
Returns true if the player has a clear line of sight to this square.

Specified by:
canSee in interface DisplayPolicy
Parameters:
x - map x
y - map y
Returns:
true if their line of sight to (x, y) is unblocked

addUpdateRegions

public void addUpdateRegions(MapDirtyRegions updates)
Adds in all the cells in the map that have become invalid since this method was last called.

Specified by:
addUpdateRegions in interface DisplayPolicy
Parameters:
updates - a matrix, the size of the map, that has all areas that have become invalid in the map since the last camera refresh.

addTerrain

public void addTerrain(int x,
                       int y)
Adds a terrain object. Called by grabObjects() for each square that has become invalid since the last refresh.

We need to send over border information with the terrain, so for each square to be redrawn, we add a 3x3 int[][] of info (obtained from the map) so the client can draw the center square.

Parameters:
x - the x map location (not view location)
y - the y map location (not view location)

checkAdd

public boolean checkAdd(GameObject obj)
Returns true if this object should be added to the snapshot. Default implementation is for player client; it returns false if the object is invisible to the player.

Specified by:
checkAdd in interface DisplayPolicy
Parameters:
obj - the object to consider for adding
Returns:
true if the object is visible to the player, false if it's invisible. Note: use canSee() to see if the player has line-of-sight to the object. You really need to call both methods (this and canSee()) to determine whether a player can see a given object.

add

protected void add(GameObject obj)
Adds an object to the specified layer in the snapshot.

Parameters:
obj - the object (needed for hashcode comparison)

addDescriptor

protected void addDescriptor(int x,
                             int y,
                             int layer,
                             int tile,
                             GameObject obj,
                             int alpha)
Adds an object appearance descriptor to the sorted frame list.

Parameters:
x - map x
y - map y
layer - drawing layer
tile - tile number
obj - the object whose bitmap we're drawing. Can be null, if we're drawing some sort of effect (e.g. lighting level).
alpha - the transparency, 0-100

getTerrainInfo

public int[][] getTerrainInfo()
Returns the array of terrain records produced in the last call to grabAll().


getSparseTerrainInfo

public java.lang.Object[] getSparseTerrainInfo()
Returns information about the terrain in, and immediately adjacent to, the update rectangles.

Returns:
multiple return values stuffed into an Object array. The first entry in the array is a short[] of "stuffed" view coords at which to redraw the terrain. The second entry is an array (int[][][]) of 3x3 2D terrain descriptors, one per location in the short[].

stuffPoint

public static short stuffPoint(short x,
                               short y)
Stuffs two short values ( which must each be < 256 ) into a single short value with x in the high byte and y in the low byte.

Parameters:
x - a short value between -128 and 127
y - a short value between -128 and 127
Returns:
a short value with x in the high byte and y in the low byte

makeSparseArray

public short[] makeSparseArray()
Returns an array of the objects to draw in the client view. The array consists of pairs of short values, the first being the "stuffed" (x,y) view coordinates and the second being the tile number of the bitmap to draw. If the high bit is set, the following byte is the alpha level (0-100).


printSparseTree

public void printSparseTree()
Prints all the objects in the tree (debugging method).


cacheVisFlags

protected void cacheVisFlags()
Looks up any properties on the agent we need in advance, and remembers them.


isHandheld

public boolean isHandheld()
Returns true if player is playing on a handheld device.

Returns:
true if player is on a PDA

setHandheld

public void setHandheld(boolean handheld)
Sets whether we're playing on a handheld.

Parameters:
handheld - true if playing on a handheld

getProfilingInfo

public static java.lang.String getProfilingInfo()
Returns a profiling string.