vitro.grid
Class Grid

java.lang.Object
  extended by vitro.Model
      extended by vitro.grid.Grid

public class Grid
extends Model

Grid is a Model implementation representing space as a two-dimensional rectangular grid. Multiple Actors can exist in the same grid cell at once by default, but this behavior can be modified by overriding the passable() predicates.


Field Summary
static int[][] ADJACENT
          A set of x/y deltas for vertically, horizontally and diagonally adjacent cells.
static int[][] DIAGONAL
          A set of x/y deltas for diagonally adjacent cells.
 int height
          The height of the Grid in cells.
 Map<Actor,Location> locations
          A mapping from Actors in this Model to the Grid cell in which they are currently located.
protected  Grid model
          A reference to the current Model.
static int[][] ORTHOGONAL
          A set of x/y deltas for vertically or horizontally adjacent cells.
 int width
          The width of the Grid in cells.
 
Fields inherited from class vitro.Model
actors
 
Constructor Summary
Grid(int width, int height)
          Create a new Grid with a specified size.
 
Method Summary
 Actor actorAt(Location location)
          Find the first Actor at a given Location.
 Set<Actor> actorsAt(Location location)
          Find all Actors at a given Location.
 Set<Actor> actorsAt(Set<Location> locations)
          Find all Actors in a group of Locations.
 Set<Location> allCells()
          Obtain references to all cells on the Grid.
 Set<Location> emptyCells()
          Obtain references to all cells on the Grid which contain no Actors.
 Set<Location> neighbors(Location location, int[][] deltas)
          Obtain references to Location objects representing neighboring cells.
 boolean passable(Actor actor, Location location)
          Check the passability of a given Location with respect to a given Actor.
 Set<Location> passable(Actor actor, Set<Location> locations)
          Find the Locations that a given Actor could move to.
 Set<Location> passableNeighbors(Actor actor, int[][] deltas)
          Performs the same function as neighbors(), but only returns Locations which would be passable to a specified Actor.
 Set<Location> passableNeighbors(Location location, int[][] deltas)
          Performs the same function as neighbors(), but only returns Locations which would be passable to any Actor.
 void put(Actor actor, int x, int y)
          Place a new Actor at a specified Location.
 
Methods inherited from class vitro.Model
cleanup, done
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

model

protected final Grid model
A reference to the current Model.


width

public final int width
The width of the Grid in cells.


height

public final int height
The height of the Grid in cells.


locations

public final Map<Actor,Location> locations
A mapping from Actors in this Model to the Grid cell in which they are currently located.


ORTHOGONAL

public static final int[][] ORTHOGONAL
A set of x/y deltas for vertically or horizontally adjacent cells.


DIAGONAL

public static final int[][] DIAGONAL
A set of x/y deltas for diagonally adjacent cells.


ADJACENT

public static final int[][] ADJACENT
A set of x/y deltas for vertically, horizontally and diagonally adjacent cells.

Constructor Detail

Grid

public Grid(int width,
            int height)
Create a new Grid with a specified size.

Parameters:
width - the width of the Grid in cells.
height - the height of the Grid in cells.
Method Detail

put

public void put(Actor actor,
                int x,
                int y)
Place a new Actor at a specified Location. Equivalent to this.locations.put(actor, new Location(this, x, y))

Parameters:
actor - the Actor to place in the grid.
x - the column in which to place the Actor.
y - the row in which to place the Actor.

neighbors

public Set<Location> neighbors(Location location,
                               int[][] deltas)
Obtain references to Location objects representing neighboring cells. The array of deltas provided is a list of 2-length arrays representing an x-offset followed by a y-offset. See the constant arrays ORTHOGONAL, DIAGONAL and ADJACENT.

Parameters:
location - the origin Location.
deltas - a collection of x and y offsets to neighboring cells.
Returns:
a Set of neighboring Locations.

passableNeighbors

public Set<Location> passableNeighbors(Actor actor,
                                       int[][] deltas)
Performs the same function as neighbors(), but only returns Locations which would be passable to a specified Actor. In the case of deltas with a magnitude greater than one, note that this method only checks passability of destinations and does not check that there is an intervening path of passable adjacent cells.

Parameters:
actor - the actor to consider moving
deltas - a collection of x and y offsets to neighboring cells.
Returns:
a Set of passable neighboring Locations.

passableNeighbors

public Set<Location> passableNeighbors(Location location,
                                       int[][] deltas)
Performs the same function as neighbors(), but only returns Locations which would be passable to any Actor.

Parameters:
location - the origin Location.
deltas - a collection of x and y offsets to neighboring cells.
Returns:
a Set of passable neighboring Locations.

actorAt

public Actor actorAt(Location location)
Find the first Actor at a given Location. If multiple Actors exist at a given Location the results of this method are not guaranteed to be consistent.

Parameters:
location - the Location to check for Actors.
Returns:
the Actor at the Location or null if no Actors exist.

actorsAt

public Set<Actor> actorsAt(Location location)
Find all Actors at a given Location.

Parameters:
location - the Location to check for Actors.
Returns:
a set of Actors at the Location.

actorsAt

public Set<Actor> actorsAt(Set<Location> locations)
Find all Actors in a group of Locations.

Parameters:
locations - a set of Locations to check for Actors.
Returns:
a set of Actors at the Locations.

allCells

public Set<Location> allCells()
Obtain references to all cells on the Grid.

Returns:
a set of Locations representing every cell on the Grid.

emptyCells

public Set<Location> emptyCells()
Obtain references to all cells on the Grid which contain no Actors.

Returns:
a set of Locations representing empty cells on the Grid.

passable

public boolean passable(Actor actor,
                        Location location)
Check the passability of a given Location with respect to a given Actor. This method is leveraged by many of the convenience methods in Grid and by default always returns true. By overriding this method, subclasses can easily add their own passability system of arbitrary complexity.

Parameters:
actor - the Actor to consider.
location - the Location to consider.
Returns:
true if the Actor can move to this Location.

passable

public Set<Location> passable(Actor actor,
                              Set<Location> locations)
Find the Locations that a given Actor could move to. Makes use of passable(actor, location).

Parameters:
actor - the Actor to consider.
locations - a set of Locations to consider.
Returns:
a set of passable Locations.