wyvern.common.tiles
Class TileRegistry

java.lang.Object
  extended bywyvern.common.tiles.TileRegistry

public final class TileRegistry
extends java.lang.Object

This is a simple database that establishes a unique integer ID, called a tile number for every game tile, where a tile is any game image to be displayed on the client.

The database exists so the server doesn't have to send image pathnames over the network on every screen update. The server looks up a tile's ID given its path, and the client retrieves the path from the ID.

The basic functions provided here are:

The last function is particularly useful because Wizard code can't query the filesystem outside the wizard's personal directory. Wizard code often needs to check whether a given image exists (e.g. a corpse for a player, or a particular view for a monster) in order to perform some interesting function.

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

Field Summary
static int NO_IMAGE
           
 
Constructor Summary
TileRegistry()
           
 
Method Summary
static boolean contains(java.lang.String path)
          Queries whether the specified image exists.
static boolean contains(java.lang.String category, java.lang.String image)
          Deprecated. use contains(path) instead
static java.lang.String getAbsolutePath(java.lang.String path)
          Returns the absolute path to an image file, given its relative path.
static java.lang.String getDirectory(java.lang.String path)
          Returns the absolute directory on disk for the given relative image path.
static TileInfo getInfo(java.lang.String relpath)
          Finds any images that are related to this one and caches them.
static TileInfo getInfo(java.lang.String category, java.lang.String image)
          Deprecated. use lookup(relpath) instead
static long getLastModified(java.lang.String path)
          Returns the time when the file was last modified.
static java.lang.String getRoot(java.lang.String path)
          Returns the root directory in which to look for artwork.
static byte[] getTileMappings()
          Returns the current tile-number assignments that have been given out by the server.
static int getUnknownImage()
          Returns the "question-mark" image to display if we don't know what the object looks like.
static java.lang.String lookup(int ID)
          Given an integer, retrieves the corresponding tile name.
static int lookup(java.lang.String image)
          Given relative path to a tile, not split into 2 components, returns its ID.
static int lookup(java.lang.String category, java.lang.String image)
          Deprecated. use lookup(path) instead
static void main(java.lang.String[] args)
          For testing functions.
static byte[] readImageFile(java.lang.String path)
          Returns a byte array containing the contents of the specified image file, for sending to a client.
 void store(java.lang.String path, int ID)
          Stores a tile in the database under the specified key.
static void store(java.lang.String cat, java.lang.String name, int ID)
          Deprecated. use store(path, ID) instead
static boolean uncache(java.lang.String path)
          Removes an image from the cache (e.g. a Wizard uploaded a new version and wants to test it).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_IMAGE

public static final int NO_IMAGE
See Also:
Constant Field Values
Constructor Detail

TileRegistry

public TileRegistry()
Method Detail

lookup

public static java.lang.String lookup(int ID)
Given an integer, retrieves the corresponding tile name.

Parameters:
ID - the unique integer ID of the tile to retrieve
Returns:
the relative path to the tile file

lookup

public static int lookup(java.lang.String category,
                         java.lang.String image)
Deprecated. use lookup(path) instead

Given the relative path to a tile, returns its ID.

Parameters:
category - the relative path to the image in the art directory
image - the image file name in that category
Returns:
the integer ID of that image, -1 if not found

lookup

public static int lookup(java.lang.String image)
Given relative path to a tile, not split into 2 components, returns its ID.

Parameters:
image - the category + image
Returns:
the integer ID of the image, -1 if not found

uncache

public static boolean uncache(java.lang.String path)
Removes an image from the cache (e.g. a Wizard uploaded a new version and wants to test it).

Parameters:
path - the category + "/"+ image
Returns:
true if the object was in the cache in the first place

contains

public static boolean contains(java.lang.String category,
                               java.lang.String image)
Deprecated. use contains(path) instead

Queries whether the specified image exists. As a side effect, the image is loaded if it wasn't already. It's better to call lookup() directly if you're checking to see if the image exists before looking it up, to avoid a double lookup.

Parameters:
category - the relative path to the image in the art directory
image - the image file name in that category
Returns:
true if found, false if not

contains

public static boolean contains(java.lang.String path)
Queries whether the specified image exists. As a side effect, the image is loaded if it wasn't already. It's better to call lookup() directly if you're checking to see if the image exists before looking it up, to avoid a double lookup.

Parameters:
path - the image path, such as "monsters/goblin/ogre"
Returns:
true if found, false if not

store

public static void store(java.lang.String cat,
                         java.lang.String name,
                         int ID)
Deprecated. use store(path, ID) instead

Associates a tile with the specified ID.

Parameters:
cat - the relative path to the tile under the artwork dir
name - the filename minus extension
ID - the integer ID to give to the tile.

store

public void store(java.lang.String path,
                  int ID)
Stores a tile in the database under the specified key.

On the server side, the most common lookup will be obtaining an ID from a string, so we key on the string with IDs as values.

Parameters:
path - the relative path to the image, including its name
ID - the integer ID to give to the tile.

getUnknownImage

public static int getUnknownImage()
Returns the "question-mark" image to display if we don't know what the object looks like.

Returns:
the tile number of the question-mark image

getInfo

public static TileInfo getInfo(java.lang.String category,
                               java.lang.String image)
Deprecated. use lookup(relpath) instead

Returns extra information about the image.

Parameters:
category - the relative path to the image
image - the base image name, such as "fireball"
Returns:
a TileInfo with information about available directions and animation frames for the image.

getInfo

public static TileInfo getInfo(java.lang.String relpath)
Finds any images that are related to this one and caches them. Many images have multiple views or animation frames, and we want people to be able to do quick lookups to see which views or animations exist for an image. The first time someone asks for a TileInfo for an image, we create the TileInfo for it by scanning the directory for related images.

Parameters:
relpath - the relative path to the image, plus its name. You can include the ".N1" extension if you like, or leave it off.
Returns:
a TileInfo with information about available directions and animation frames for the image. Returns null if the passed image doesn't exist, OR if the image exists but doesn't have any views or animation frames.

readImageFile

public static byte[] readImageFile(java.lang.String path)
Returns a byte array containing the contents of the specified image file, for sending to a client. Keeps a cache of

Parameters:
path - path to the file
Returns:
the data, or null if file wasn't found

getTileMappings

public static byte[] getTileMappings()
Returns the current tile-number assignments that have been given out by the server. Sending the tile/image mappings up front cuts down on bandwidth (since each tile/image request from the client is a roundtrip and screen refresh), and also minimizes the number of "image-not-available" images shown on the client during the session.

The disadvantages are:

However, I think the benefits outweigh the problems.

Returns:
a (non-gzipped) sequence of numbers and strings, written by a DataOutputStream. Each entry in the sequence is a short value (the tile number), followed by a UTF value (the image).

getLastModified

public static long getLastModified(java.lang.String path)
Returns the time when the file was last modified. Calls java.io.File.lastModified on the file, which returns a nearly useless system-dependent number; this method just turns it into a string, but it's not parseable as a date and can only be used to compare against other calls to lastModified().

Parameters:
path - relative path to the file
Returns:
the lastModified time, as a long value, in String format

getAbsolutePath

public static java.lang.String getAbsolutePath(java.lang.String path)
Returns the absolute path to an image file, given its relative path.

Parameters:
path - relative image path, extension optional
Returns:
full path on disk to the file

getDirectory

public static java.lang.String getDirectory(java.lang.String path)
Returns the absolute directory on disk for the given relative image path.

Parameters:
path - e.g. "objects/jade_key" or "wiz/foo/bar"
Returns:
the directory where it lives (absolute path)

getRoot

public static java.lang.String getRoot(java.lang.String path)
Returns the root directory in which to look for artwork.

Parameters:
path - the relative path to the picture. If it starts with "wiz", then the root will be the game root, else the root is the game root plus "art/game" (the default art directory).

main

public static void main(java.lang.String[] args)
For testing functions.