Behold, Nenya, Ring of Water and repository for our media and animation related
goodies, both Java 2D and LWJGL/JME 3D. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@1 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
@@ -0,0 +1,390 @@
|
||||
//
|
||||
// $Id: AutoFringer.java 4145 2006-05-24 01:24:24Z ray $
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.miso.tile;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Transparency;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.samskivert.util.CheapIntMap;
|
||||
import com.samskivert.util.QuickSort;
|
||||
|
||||
import com.threerings.media.image.BufferedMirage;
|
||||
import com.threerings.media.image.ImageManager;
|
||||
import com.threerings.media.image.ImageUtil;
|
||||
|
||||
import com.threerings.media.tile.NoSuchTileSetException;
|
||||
import com.threerings.media.tile.Tile;
|
||||
import com.threerings.media.tile.TileManager;
|
||||
import com.threerings.media.tile.TileSet;
|
||||
import com.threerings.media.tile.TileUtil;
|
||||
|
||||
import com.threerings.miso.Log;
|
||||
import com.threerings.miso.data.MisoSceneModel;
|
||||
|
||||
/**
|
||||
* Automatically fringes a scene according to the rules in the supplied
|
||||
* fringe configuration.
|
||||
*/
|
||||
public class AutoFringer
|
||||
{
|
||||
/**
|
||||
* Constructs an instance that will fringe according to the rules in
|
||||
* the supplied fringe configuration.
|
||||
*/
|
||||
public AutoFringer (FringeConfiguration fringeconf, ImageManager imgr,
|
||||
TileManager tmgr)
|
||||
{
|
||||
_fringeconf = fringeconf;
|
||||
_imgr = imgr;
|
||||
_tmgr = tmgr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute and return the fringe tile to be inserted at the specified
|
||||
* location.
|
||||
*/
|
||||
public BaseTile getFringeTile (MisoSceneModel scene, int col, int row,
|
||||
HashMap masks)
|
||||
{
|
||||
// get the tileset id of the base tile we are considering
|
||||
int underset = scene.getBaseTileId(col, row) >> 16;
|
||||
|
||||
// start with a clean temporary fringer map
|
||||
_fringers.clear();
|
||||
boolean passable = true;
|
||||
|
||||
// walk through our influence tiles
|
||||
for (int y = row - 1, maxy = row + 2; y < maxy; y++) {
|
||||
for (int x = col - 1, maxx = col + 2; x < maxx; x++) {
|
||||
// we sensibly do not consider ourselves
|
||||
if ((x == col) && (y == row)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// determine the tileset for this tile
|
||||
int btid = scene.getBaseTileId(x, y);
|
||||
int baseset = (btid <= 0) ?
|
||||
scene.getDefaultBaseTileSet() : (btid >> 16);
|
||||
|
||||
// determine if it fringes on our tile
|
||||
int pri = _fringeconf.fringesOn(baseset, underset);
|
||||
if (pri == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FringerRec fringer = (FringerRec)_fringers.get(baseset);
|
||||
if (fringer == null) {
|
||||
fringer = new FringerRec(baseset, pri);
|
||||
_fringers.put(baseset, fringer);
|
||||
}
|
||||
|
||||
// now turn on the appropriate fringebits
|
||||
fringer.bits |= FLAGMATRIX[y - row + 1][x - col + 1];
|
||||
|
||||
// See if a tile that fringes on us kills our passability,
|
||||
// but don't count the default base tile against us, as
|
||||
// we allow users to splash in the water.
|
||||
if (passable && (btid > 0)) {
|
||||
try {
|
||||
BaseTile bt = (BaseTile) _tmgr.getTile(btid);
|
||||
passable = bt.isPassable();
|
||||
} catch (NoSuchTileSetException nstse) {
|
||||
Log.warning("Autofringer couldn't find a base " +
|
||||
"set while attempting to figure passability " +
|
||||
"[error=" + nstse + "].");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if nothing fringed, we're done
|
||||
int numfringers = _fringers.size();
|
||||
if (numfringers == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// otherwise compose a FringeTile from the specified fringes
|
||||
FringerRec[] frecs = new FringerRec[numfringers];
|
||||
for (int ii = 0, pp = 0; ii < 16; ii++) {
|
||||
FringerRec rec = (FringerRec)_fringers.getValue(ii);
|
||||
if (rec != null) {
|
||||
frecs[pp++] = rec;
|
||||
}
|
||||
}
|
||||
|
||||
BaseTile frTile = new BaseTile();
|
||||
frTile.setPassable(passable);
|
||||
composeFringeTile(frTile, frecs, masks, TileUtil.getTileHash(col, row));
|
||||
return frTile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compose a FringeTile out of the various fringe images needed.
|
||||
*/
|
||||
protected void composeFringeTile (
|
||||
Tile frTile, FringerRec[] fringers, HashMap masks, int hashValue)
|
||||
{
|
||||
// sort the array so that higher priority fringers get drawn first
|
||||
QuickSort.sort(fringers);
|
||||
|
||||
BufferedImage ftimg = null;
|
||||
for (int ii = 0; ii < fringers.length; ii++) {
|
||||
int[] indexes = getFringeIndexes(fringers[ii].bits);
|
||||
for (int jj = 0; jj < indexes.length; jj++) {
|
||||
try {
|
||||
ftimg = getTileImage(ftimg, fringers[ii].baseset,
|
||||
indexes[jj], masks, hashValue);
|
||||
} catch (NoSuchTileSetException nstse) {
|
||||
Log.warning("Autofringer couldn't find a needed tileset " +
|
||||
"[error=" + nstse + "].");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
frTile.setImage(new BufferedMirage(ftimg));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve or compose an image for the specified fringe.
|
||||
*/
|
||||
protected BufferedImage getTileImage (
|
||||
BufferedImage ftimg, int baseset, int index,
|
||||
HashMap masks, int hashValue)
|
||||
throws NoSuchTileSetException
|
||||
{
|
||||
FringeConfiguration.FringeTileSetRecord tsr =
|
||||
_fringeconf.getFringe(baseset, hashValue);
|
||||
int fringeset = tsr.fringe_tsid;
|
||||
TileSet fset = _tmgr.getTileSet(fringeset);
|
||||
|
||||
if (!tsr.mask) {
|
||||
// oh good, this is easy
|
||||
Tile stamp = fset.getTile(index);
|
||||
return stampTileImage(stamp, ftimg, stamp.getWidth(),
|
||||
stamp.getHeight());
|
||||
}
|
||||
|
||||
// otherwise, it's a mask.. look for it in the cache..
|
||||
Long maskkey = Long.valueOf((((long) baseset) << 32) +
|
||||
(fringeset << 16) + index);
|
||||
BufferedImage img = (BufferedImage)masks.get(maskkey);
|
||||
if (img == null) {
|
||||
BufferedImage fsrc = fset.getRawTileImage(index);
|
||||
BufferedImage bsrc = _tmgr.getTileSet(baseset).getRawTileImage(0);
|
||||
img = ImageUtil.composeMaskedImage(_imgr, fsrc, bsrc);
|
||||
masks.put(maskkey, img);
|
||||
}
|
||||
ftimg = stampTileImage(img, ftimg, img.getWidth(null),
|
||||
img.getHeight(null));
|
||||
|
||||
return ftimg;
|
||||
}
|
||||
|
||||
/** Helper function for {@link #getTileImage}. */
|
||||
protected BufferedImage stampTileImage (Object stamp, BufferedImage ftimg,
|
||||
int width, int height)
|
||||
{
|
||||
// create the target image if necessary
|
||||
if (ftimg == null) {
|
||||
ftimg = _imgr.createImage(width, height, Transparency.BITMASK);
|
||||
}
|
||||
Graphics2D gfx = (Graphics2D)ftimg.getGraphics();
|
||||
try {
|
||||
if (stamp instanceof Tile) {
|
||||
((Tile)stamp).paint(gfx, 0, 0);
|
||||
} else {
|
||||
gfx.drawImage((BufferedImage)stamp, 0, 0, null);
|
||||
}
|
||||
} finally {
|
||||
gfx.dispose();
|
||||
}
|
||||
return ftimg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the fringe index specified by the fringebits. If no index
|
||||
* is available, try breaking down the bits into contiguous regions of
|
||||
* bits and look for indexes for those.
|
||||
*/
|
||||
protected int[] getFringeIndexes (int bits)
|
||||
{
|
||||
int index = BITS_TO_INDEX[bits];
|
||||
if (index != -1) {
|
||||
int[] ret = new int[1];
|
||||
ret[0] = index;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// otherwise, split the bits into contiguous components
|
||||
|
||||
// look for a zero and start our first split
|
||||
int start = 0;
|
||||
while ((((1 << start) & bits) != 0) && (start < NUM_FRINGEBITS)) {
|
||||
start++;
|
||||
}
|
||||
|
||||
if (start == NUM_FRINGEBITS) {
|
||||
// we never found an empty fringebit, and since index (above)
|
||||
// was already -1, we have no fringe tile for these bits.. sad.
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
ArrayList indexes = new ArrayList();
|
||||
int weebits = 0;
|
||||
for (int ii=(start + 1) % NUM_FRINGEBITS; ii != start;
|
||||
ii = (ii + 1) % NUM_FRINGEBITS) {
|
||||
|
||||
if (((1 << ii) & bits) != 0) {
|
||||
weebits |= (1 << ii);
|
||||
} else if (weebits != 0) {
|
||||
index = BITS_TO_INDEX[weebits];
|
||||
if (index != -1) {
|
||||
indexes.add(Integer.valueOf(index));
|
||||
}
|
||||
weebits = 0;
|
||||
}
|
||||
}
|
||||
if (weebits != 0) {
|
||||
index = BITS_TO_INDEX[weebits];
|
||||
if (index != -1) {
|
||||
indexes.add(Integer.valueOf(index));
|
||||
}
|
||||
}
|
||||
|
||||
int[] ret = new int[indexes.size()];
|
||||
for (int ii=0; ii < ret.length; ii++) {
|
||||
ret[ii] = ((Integer) indexes.get(ii)).intValue();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* A record for holding information about a particular fringe as we're
|
||||
* computing what it will look like.
|
||||
*/
|
||||
static protected class FringerRec implements Comparable
|
||||
{
|
||||
int baseset;
|
||||
int priority;
|
||||
int bits;
|
||||
|
||||
public FringerRec (int base, int pri)
|
||||
{
|
||||
baseset = base;
|
||||
priority = pri;
|
||||
}
|
||||
|
||||
public int compareTo (Object o)
|
||||
{
|
||||
return priority - ((FringerRec) o).priority;
|
||||
}
|
||||
|
||||
public String toString ()
|
||||
{
|
||||
return "[base=" + baseset + ", pri=" + priority +
|
||||
", bits=" + Integer.toString(bits, 16) + "]";
|
||||
}
|
||||
}
|
||||
|
||||
// fringe bits
|
||||
// see docs/miso/fringebits.png
|
||||
//
|
||||
protected static final int NORTH = 1 << 0;
|
||||
protected static final int NORTHEAST = 1 << 1;
|
||||
protected static final int EAST = 1 << 2;
|
||||
protected static final int SOUTHEAST = 1 << 3;
|
||||
protected static final int SOUTH = 1 << 4;
|
||||
protected static final int SOUTHWEST = 1 << 5;
|
||||
protected static final int WEST = 1 << 6;
|
||||
protected static final int NORTHWEST = 1 << 7;
|
||||
|
||||
protected static final int NUM_FRINGEBITS = 8;
|
||||
|
||||
// A matrix mapping adjacent tiles to which fringe bits
|
||||
// they affect.
|
||||
// (x and y are offset by +1, since we can't have -1 as an array index)
|
||||
// again, see docs/miso/fringebits.png
|
||||
//
|
||||
protected static final int[][] FLAGMATRIX = {
|
||||
{ NORTHEAST, (NORTHEAST | EAST | SOUTHEAST), SOUTHEAST },
|
||||
{ (NORTHWEST | NORTH | NORTHEAST), 0, (SOUTHEAST | SOUTH | SOUTHWEST) },
|
||||
{ NORTHWEST, (NORTHWEST | WEST | SOUTHWEST), SOUTHWEST }
|
||||
};
|
||||
|
||||
/**
|
||||
* The fringe tiles we use. These are the 17 possible tiles made
|
||||
* up of continuous fringebits sections.
|
||||
* Huh? see docs/miso/fringebits.png
|
||||
*/
|
||||
protected static final int[] FRINGETILES = {
|
||||
SOUTHEAST,
|
||||
SOUTHWEST | SOUTH | SOUTHEAST,
|
||||
SOUTHWEST,
|
||||
NORTHEAST | EAST | SOUTHEAST,
|
||||
NORTHWEST | WEST | SOUTHWEST,
|
||||
NORTHEAST,
|
||||
NORTHWEST | NORTH | NORTHEAST,
|
||||
NORTHWEST,
|
||||
|
||||
SOUTHWEST | WEST | NORTHWEST | NORTH | NORTHEAST,
|
||||
NORTHWEST | NORTH | NORTHEAST | EAST | SOUTHEAST,
|
||||
NORTHWEST | WEST | SOUTHWEST | SOUTH | SOUTHEAST,
|
||||
SOUTHWEST | SOUTH | SOUTHEAST | EAST | NORTHEAST,
|
||||
|
||||
NORTHEAST | NORTH | NORTHWEST | WEST | SOUTHWEST | SOUTH | SOUTHEAST,
|
||||
SOUTHEAST | EAST | NORTHEAST | NORTH | NORTHWEST | WEST | SOUTHWEST,
|
||||
SOUTHWEST | SOUTH | SOUTHEAST | EAST | NORTHEAST | NORTH | NORTHWEST,
|
||||
NORTHWEST | WEST | SOUTHWEST | SOUTH | SOUTHEAST | EAST | NORTHEAST,
|
||||
|
||||
// all the directions!
|
||||
NORTH | NORTHEAST | EAST | SOUTHEAST |
|
||||
SOUTH | SOUTHWEST | WEST | NORTHWEST
|
||||
};
|
||||
|
||||
// A reverse map of the above array, for quickly looking up which tile
|
||||
// we want.
|
||||
protected static final int[] BITS_TO_INDEX;
|
||||
|
||||
// Construct the BITS_TO_INDEX array.
|
||||
static {
|
||||
int num = (1 << NUM_FRINGEBITS);
|
||||
BITS_TO_INDEX = new int[num];
|
||||
|
||||
// first clear everything to -1 (meaning there is no tile defined)
|
||||
for (int ii=0; ii < num; ii++) {
|
||||
BITS_TO_INDEX[ii] = -1;
|
||||
}
|
||||
|
||||
// then fill in with the defined tiles.
|
||||
for (int ii=0; ii < FRINGETILES.length; ii++) {
|
||||
BITS_TO_INDEX[FRINGETILES[ii]] = ii;
|
||||
}
|
||||
}
|
||||
|
||||
protected ImageManager _imgr;
|
||||
protected TileManager _tmgr;
|
||||
protected FringeConfiguration _fringeconf;
|
||||
protected CheapIntMap _fringers = new CheapIntMap(16);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// $Id: BaseTile.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.miso.tile;
|
||||
|
||||
import com.threerings.media.tile.Tile;
|
||||
|
||||
/**
|
||||
* Extends the tile class to add support for tile passability.
|
||||
*
|
||||
* @see BaseTileSet
|
||||
*/
|
||||
public class BaseTile extends Tile
|
||||
{
|
||||
/**
|
||||
* Returns whether or not this tile can be walked upon by character
|
||||
* sprites.
|
||||
*/
|
||||
public boolean isPassable ()
|
||||
{
|
||||
return _passable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures this base tile as passable or impassable.
|
||||
*/
|
||||
public void setPassable (boolean passable)
|
||||
{
|
||||
_passable = passable;
|
||||
}
|
||||
|
||||
/** Whether the tile is passable. */
|
||||
protected boolean _passable = true;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// $Id: BaseTileSet.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.miso.tile;
|
||||
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
import com.threerings.media.image.Colorization;
|
||||
import com.threerings.media.tile.SwissArmyTileSet;
|
||||
import com.threerings.media.tile.Tile;
|
||||
|
||||
/**
|
||||
* The base tileset extends the swiss army tileset to add support for tile
|
||||
* passability. Passability is used to determine whether traverser objects
|
||||
* (generally sprites made to "walk" around the scene) can traverse a
|
||||
* particular tile in a scene.
|
||||
*/
|
||||
public class BaseTileSet extends SwissArmyTileSet
|
||||
{
|
||||
/**
|
||||
* Sets the passability information for the tiles in this tileset.
|
||||
* Each entry in the array corresponds to the tile at that tile index.
|
||||
*/
|
||||
public void setPassability (boolean[] passable)
|
||||
{
|
||||
_passable = passable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the passability information for the tiles in this tileset.
|
||||
*/
|
||||
public boolean[] getPassability ()
|
||||
{
|
||||
return _passable;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
protected Tile createTile ()
|
||||
{
|
||||
return new BaseTile();
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
protected void initTile (Tile tile, int tileIndex, Colorization[] zations)
|
||||
{
|
||||
super.initTile(tile, tileIndex, zations);
|
||||
((BaseTile)tile).setPassable(_passable[tileIndex]);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
protected void toString (StringBuilder buf)
|
||||
{
|
||||
super.toString(buf);
|
||||
buf.append(", passable=").append(StringUtil.toString(_passable));
|
||||
}
|
||||
|
||||
/** Whether each tile is passable. */
|
||||
protected boolean[] _passable;
|
||||
|
||||
/** Increase this value when object's serialized state is impacted by
|
||||
* a class change (modification of fields, inheritance). */
|
||||
private static final long serialVersionUID = 1;
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
//
|
||||
// $Id: FringeConfiguration.java 3403 2005-03-14 23:58:02Z mdb $
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.miso.tile;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.samskivert.util.HashIntMap;
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
/**
|
||||
* Used to manage data about which base tilesets fringe on which others
|
||||
* and how they fringe.
|
||||
*/
|
||||
public class FringeConfiguration implements Serializable
|
||||
{
|
||||
/**
|
||||
* The path (relative to the resource directory) at which the fringe
|
||||
* configuration should be loaded and stored.
|
||||
*/
|
||||
public static final String CONFIG_PATH = "config/miso/tile/fringeconf.dat";
|
||||
|
||||
public static class FringeRecord implements Serializable
|
||||
{
|
||||
/** The tileset id of the base tileset to which this applies. */
|
||||
public int base_tsid;
|
||||
|
||||
/** The fringe priority of this base tileset. */
|
||||
public int priority;
|
||||
|
||||
/** A list of the possible tilesets that can be used for fringing. */
|
||||
public ArrayList tilesets = new ArrayList();
|
||||
|
||||
/** Used when parsing the tilesets definitions. */
|
||||
public void addTileset (FringeTileSetRecord record)
|
||||
{
|
||||
tilesets.add(record);
|
||||
}
|
||||
|
||||
/** Did everything parse well? */
|
||||
public boolean isValid ()
|
||||
{
|
||||
return ((base_tsid != 0) && (priority > 0));
|
||||
}
|
||||
|
||||
/** Generates a string representation of this instance. */
|
||||
public String toString ()
|
||||
{
|
||||
return "[base_tsid=" + base_tsid + ", priority=" + priority +
|
||||
", tilesets=" + StringUtil.toString(tilesets) + "]";
|
||||
}
|
||||
|
||||
/** Increase this value when object's serialized state is impacted
|
||||
* by a class change (modification of fields, inheritance). */
|
||||
private static final long serialVersionUID = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to parse the tileset fringe definitions.
|
||||
*/
|
||||
public static class FringeTileSetRecord implements Serializable
|
||||
{
|
||||
/** The tileset id of the fringe tileset. */
|
||||
public int fringe_tsid;
|
||||
|
||||
/** Is this a mask? */
|
||||
public boolean mask;
|
||||
|
||||
/** Did everything parse well? */
|
||||
public boolean isValid ()
|
||||
{
|
||||
return (fringe_tsid != 0);
|
||||
}
|
||||
|
||||
/** Generates a string representation of this instance. */
|
||||
public String toString ()
|
||||
{
|
||||
return "[fringe_tsid=" + fringe_tsid + ", mask=" + mask + "]";
|
||||
}
|
||||
|
||||
/** Increase this value when object's serialized state is impacted
|
||||
* by a class change (modification of fields, inheritance). */
|
||||
private static final long serialVersionUID = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a parsed FringeRecord to this instance. This is used when parsing
|
||||
* the fringerecords from xml.
|
||||
*/
|
||||
public void addFringeRecord (FringeRecord frec)
|
||||
{
|
||||
_frecs.put(frec.base_tsid, frec);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the first base tileset fringes upon the second, return the
|
||||
* fringe priority of the first base tileset, otherwise return -1.
|
||||
*/
|
||||
public int fringesOn (int first, int second)
|
||||
{
|
||||
FringeRecord f1 = (FringeRecord) _frecs.get(first);
|
||||
|
||||
// we better have a fringe record for the first
|
||||
if (null != f1) {
|
||||
|
||||
// it had better have some tilesets defined
|
||||
if (f1.tilesets.size() > 0) {
|
||||
|
||||
FringeRecord f2 = (FringeRecord) _frecs.get(second);
|
||||
|
||||
// and we only fringe if second doesn't exist or has a lower
|
||||
// priority
|
||||
if ((null == f2) || (f1.priority > f2.priority)) {
|
||||
return f1.priority;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a random FringeTileSetRecord from amongst the ones
|
||||
* listed for the specified base tileset.
|
||||
*/
|
||||
public FringeTileSetRecord getFringe (int baseset, int hashValue)
|
||||
{
|
||||
FringeRecord f = (FringeRecord) _frecs.get(baseset);
|
||||
return (FringeTileSetRecord) f.tilesets.get(
|
||||
hashValue % f.tilesets.size());
|
||||
}
|
||||
|
||||
/** The mapping from base tileset id to fringerecord. */
|
||||
protected HashIntMap _frecs = new HashIntMap();
|
||||
|
||||
/** Increase this value when object's serialized state is impacted by
|
||||
* a class change (modification of fields, inheritance). */
|
||||
private static final long serialVersionUID = 1;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// $Id: MisoTileManager.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.miso.tile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.samskivert.io.StreamUtil;
|
||||
|
||||
import com.threerings.resource.ResourceManager;
|
||||
import com.threerings.util.CompiledConfig;
|
||||
|
||||
import com.threerings.media.image.ImageManager;
|
||||
import com.threerings.media.tile.TileManager;
|
||||
|
||||
import com.threerings.miso.Log;
|
||||
|
||||
/**
|
||||
* Extends the basic tile manager and provides support for automatically
|
||||
* generating fringes in between different types of base tiles in a scene.
|
||||
*/
|
||||
public class MisoTileManager extends TileManager
|
||||
{
|
||||
/**
|
||||
* Creates a tile manager and provides it with a reference to the
|
||||
* image manager from which it will load tileset images.
|
||||
*
|
||||
* @param imgr the image manager via which the tile manager will
|
||||
* decode and cache images.
|
||||
*/
|
||||
public MisoTileManager (ResourceManager rmgr, ImageManager imgr)
|
||||
{
|
||||
super(imgr);
|
||||
|
||||
// look for a fringe configuration in the appropriate place
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = rmgr.getResource(FRINGE_CONFIG_PATH);
|
||||
FringeConfiguration config = (FringeConfiguration)
|
||||
CompiledConfig.loadConfig(in);
|
||||
|
||||
// if we've found it, create our auto fringer with it
|
||||
_fringer = new AutoFringer(config, imgr, this);
|
||||
|
||||
} catch (IOException ioe) {
|
||||
Log.warning("Unable to load fringe configuration " +
|
||||
"[path=" + FRINGE_CONFIG_PATH +
|
||||
", error=" + ioe + "].");
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(in);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the auto fringer that has been configured for use by this
|
||||
* tile manager. This will only be valid if this tile manager has been
|
||||
* provided with a miso tileset repository via {@link
|
||||
* #setTileSetRepository}.
|
||||
*/
|
||||
public AutoFringer getAutoFringer ()
|
||||
{
|
||||
return _fringer;
|
||||
}
|
||||
|
||||
/** The entity that performs the automatic fringe layer generation. */
|
||||
protected AutoFringer _fringer;
|
||||
|
||||
/** The path (in the classpath) to the serialized fringe
|
||||
* configuration. */
|
||||
protected static final String FRINGE_CONFIG_PATH =
|
||||
"config/miso/tile/fringeconf.dat";
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// $Id: CompileFringeConfigurationTask.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.miso.tile.tools;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.tools.ant.BuildException;
|
||||
import org.apache.tools.ant.Task;
|
||||
|
||||
import com.samskivert.io.PersistenceException;
|
||||
|
||||
import com.threerings.util.CompiledConfig;
|
||||
|
||||
import com.threerings.media.tile.tools.MapFileTileSetIDBroker;
|
||||
|
||||
import com.threerings.miso.tile.tools.xml.FringeConfigurationParser;
|
||||
|
||||
/**
|
||||
* Compile fringe configuration.
|
||||
*/
|
||||
public class CompileFringeConfigurationTask extends Task
|
||||
{
|
||||
public void setTileSetMap (File tsetmap)
|
||||
{
|
||||
_tsetmap = tsetmap;
|
||||
}
|
||||
|
||||
public void setFringeDef (File fringedef)
|
||||
{
|
||||
_fringedef = fringedef;
|
||||
}
|
||||
|
||||
public void setTarget (File target)
|
||||
{
|
||||
_target = target;
|
||||
}
|
||||
|
||||
public void execute () throws BuildException
|
||||
{
|
||||
// make sure the source file exists
|
||||
if (!_fringedef.exists()) {
|
||||
throw new BuildException("Fringe definition file not found " +
|
||||
"[path=" + _fringedef.getPath() + "].");
|
||||
}
|
||||
|
||||
// set up the tileid broker
|
||||
MapFileTileSetIDBroker broker;
|
||||
try {
|
||||
broker = new MapFileTileSetIDBroker(_tsetmap);
|
||||
} catch (PersistenceException pe) {
|
||||
throw new BuildException("Couldn't set up tileset mapping " +
|
||||
"[path=" + _tsetmap.getPath() +
|
||||
", error=" + pe.getCause() + "].");
|
||||
}
|
||||
|
||||
FringeConfigurationParser parser = new FringeConfigurationParser(
|
||||
broker);
|
||||
Serializable config;
|
||||
try {
|
||||
config = parser.parseConfig(_fringedef);
|
||||
} catch (Exception e) {
|
||||
throw new BuildException("Failure parsing config definition", e);
|
||||
}
|
||||
|
||||
try {
|
||||
// and write it on out
|
||||
CompiledConfig.saveConfig(_target, config);
|
||||
} catch (Exception e) {
|
||||
throw new BuildException("Failure writing serialized config", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected File _tsetmap;
|
||||
protected File _fringedef;
|
||||
protected File _target;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// $Id: BaseTileSetRuleSet.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.miso.tile.tools.xml;
|
||||
|
||||
import org.apache.commons.digester.Digester;
|
||||
|
||||
import com.samskivert.util.StringUtil;
|
||||
import com.samskivert.xml.CallMethodSpecialRule;
|
||||
|
||||
import com.threerings.media.tile.tools.xml.SwissArmyTileSetRuleSet;
|
||||
|
||||
import com.threerings.miso.Log;
|
||||
import com.threerings.miso.tile.BaseTileSet;
|
||||
|
||||
/**
|
||||
* Parses {@link BaseTileSet} instances from a tileset description. Base
|
||||
* tilesets extend swiss army tilesets with the addition of a passability
|
||||
* flag for each tile.
|
||||
*
|
||||
* @see SwissArmyTileSetRuleSet
|
||||
*/
|
||||
public class BaseTileSetRuleSet extends SwissArmyTileSetRuleSet
|
||||
{
|
||||
// documentation inherited
|
||||
public void addRuleInstances (Digester digester)
|
||||
{
|
||||
super.addRuleInstances(digester);
|
||||
|
||||
digester.addRule(
|
||||
_prefix + TILESET_PATH + "/passable", new CallMethodSpecialRule() {
|
||||
public void parseAndSet (String bodyText, Object target)
|
||||
{
|
||||
int[] values = StringUtil.parseIntArray(bodyText);
|
||||
boolean[] passable = new boolean[values.length];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
passable[i] = (values[i] != 0);
|
||||
}
|
||||
BaseTileSet starget = (BaseTileSet)target;
|
||||
starget.setPassability(passable);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public boolean isValid (Object target)
|
||||
{
|
||||
BaseTileSet set = (BaseTileSet)target;
|
||||
boolean valid = super.isValid(target);
|
||||
|
||||
// check for a <passable> element
|
||||
if (set.getPassability() == null) {
|
||||
Log.warning("Tile set definition missing valid <passable> " +
|
||||
"element [set=" + set + "].");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
protected Class getTileSetClass ()
|
||||
{
|
||||
return BaseTileSet.class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
//
|
||||
// $Id: FringeConfigurationParser.java 3749 2005-11-09 04:00:16Z mdb $
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.miso.tile.tools.xml;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.apache.commons.digester.Digester;
|
||||
|
||||
import com.samskivert.util.StringUtil;
|
||||
import com.samskivert.xml.SetPropertyFieldsRule;
|
||||
import com.samskivert.xml.ValidatedSetNextRule;
|
||||
|
||||
import com.threerings.tools.xml.CompiledConfigParser;
|
||||
|
||||
import com.threerings.media.tile.TileSetIDBroker;
|
||||
|
||||
import com.threerings.miso.Log;
|
||||
import com.threerings.miso.tile.FringeConfiguration.FringeRecord;
|
||||
import com.threerings.miso.tile.FringeConfiguration.FringeTileSetRecord;
|
||||
import com.threerings.miso.tile.FringeConfiguration;
|
||||
|
||||
/**
|
||||
* Parses fringe config definitions.
|
||||
*/
|
||||
public class FringeConfigurationParser extends CompiledConfigParser
|
||||
{
|
||||
public FringeConfigurationParser (TileSetIDBroker broker)
|
||||
{
|
||||
_idBroker = broker;
|
||||
}
|
||||
|
||||
|
||||
// documentation inherited
|
||||
protected Serializable createConfigObject ()
|
||||
{
|
||||
return new FringeConfiguration();
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
protected void addRules (Digester digest)
|
||||
{
|
||||
// configure top-level constraints
|
||||
String prefix = "fringe";
|
||||
digest.addRule(prefix, new SetPropertyFieldsRule());
|
||||
|
||||
// create and configure fringe config instances
|
||||
prefix += "/base";
|
||||
digest.addObjectCreate(prefix, FringeRecord.class.getName());
|
||||
|
||||
ValidatedSetNextRule.Validator val;
|
||||
val = new ValidatedSetNextRule.Validator() {
|
||||
public boolean isValid (Object target) {
|
||||
if (((FringeRecord) target).isValid()) {
|
||||
return true;
|
||||
} else {
|
||||
Log.warning("A FringeRecord was not added because it was " +
|
||||
"improperly specified [rec=" + target + "].");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
ValidatedSetNextRule vrule;
|
||||
vrule = new ValidatedSetNextRule("addFringeRecord", val) {
|
||||
// parse the fringe record, converting tileset names to
|
||||
// tileset ids
|
||||
public void begin (String namespace, String lname, Attributes attrs)
|
||||
throws Exception
|
||||
{
|
||||
FringeRecord frec = (FringeRecord) digester.peek();
|
||||
|
||||
for (int ii=0; ii < attrs.getLength(); ii++) {
|
||||
String name = attrs.getLocalName(ii);
|
||||
if (StringUtil.isBlank(name)) {
|
||||
name = attrs.getQName(ii);
|
||||
}
|
||||
String value = attrs.getValue(ii);
|
||||
|
||||
if ("name".equals(name)) {
|
||||
if (_idBroker.tileSetMapped(value)) {
|
||||
frec.base_tsid = _idBroker.getTileSetID(value);
|
||||
} else {
|
||||
Log.warning("Skipping unknown base " +
|
||||
"tileset [name=" + value + "].");
|
||||
}
|
||||
|
||||
} else if ("priority".equals(name)) {
|
||||
frec.priority = Integer.parseInt(value);
|
||||
} else {
|
||||
Log.warning("Skipping unknown attribute " +
|
||||
"[name=" + name + "].");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
digest.addRule(prefix, vrule);
|
||||
|
||||
// create the tileset records in each fringe record
|
||||
prefix += "/tileset";
|
||||
digest.addObjectCreate(prefix, FringeTileSetRecord.class.getName());
|
||||
|
||||
val = new ValidatedSetNextRule.Validator() {
|
||||
public boolean isValid (Object target) {
|
||||
if (((FringeTileSetRecord) target).isValid()) {
|
||||
return true;
|
||||
} else {
|
||||
Log.warning("A FringeTileSetRecord was not added because " +
|
||||
"it was improperly specified " +
|
||||
"[rec=" + target + "].");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
vrule = new ValidatedSetNextRule("addTileset", val) {
|
||||
// parse the fringe tilesetrecord, converting tileset names to ids
|
||||
public void begin (String namespace, String lname, Attributes attrs)
|
||||
throws Exception
|
||||
{
|
||||
FringeTileSetRecord f = (FringeTileSetRecord) digester.peek();
|
||||
|
||||
for (int ii=0; ii < attrs.getLength(); ii++) {
|
||||
String name = attrs.getLocalName(ii);
|
||||
if (StringUtil.isBlank(name)) {
|
||||
name = attrs.getQName(ii);
|
||||
}
|
||||
String value = attrs.getValue(ii);
|
||||
|
||||
if ("name".equals(name)) {
|
||||
if (_idBroker.tileSetMapped(value)) {
|
||||
f.fringe_tsid = _idBroker.getTileSetID(value);
|
||||
} else {
|
||||
Log.warning("Skipping unknown fringe " +
|
||||
"tileset [name=" + value + "].");
|
||||
}
|
||||
|
||||
} else if ("mask".equals(name)) {
|
||||
f.mask = Boolean.valueOf(value).booleanValue();
|
||||
} else {
|
||||
Log.warning("Skipping unknown attribute " +
|
||||
"[name=" + name + "].");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
digest.addRule(prefix, vrule);
|
||||
}
|
||||
|
||||
protected TileSetIDBroker _idBroker;
|
||||
}
|
||||
Reference in New Issue
Block a user