uwcse.sim
Class WorldModel

java.lang.Object
  |
  +--uwcse.sim.WorldModel

public class WorldModel
extends java.lang.Object

A WorldModel is a relatively efficient representation of the world. The world is chopped up into a number of cells so that interactions can be efficiently computed. Things will typically ask the model about their "neighbors" (those Things that live in the same simulation cell) and questions such as these can be efficiently answered. Things can also ask questions like: give me the list of things that are in the neighboring cell... The smaller the cellsize, the more "fine-grained" the simulation will be; a very large cellsize can be used for simple, flat, simulations where regional associations are not important.

The WorldModel is also responsible for "stepping" the simulation. Each step fo the simulation consists of several phases:

birth
Things added to the simulation on the previous turn are placed into the cells they inhabit.
action
Every thing is asked to do its action
reap
Dead things are removed from their cells, and things that have moved are placed into the appropriate new cells they inhabit.
notification
any viewers of the simulation are notified


Field Summary
static int DEFAULT_CELL_SIZE
          The default cell size.
static Offset EAST
          Often used offset representing the neighboring cell to the east.
static Offset NORTH
          Often used offset representing the neighboring cell to the north.
static Offset NORTHEAST
          Often used offset representing the neighboring cell to the northeast.
static Offset NORTHWEST
          Often used offset representing the neighboring cell to the northwest.
static Offset SOUTH
          Often used offset representing the neighboring cell to the south.
static Offset SOUTHEAST
          Often used offset representing the neighboring cell to the southeast.
static Offset SOUTHWEST
          Often used offset representing the neighboring cell to the southwest.
static Offset WEST
          Often used offset representing the neighboring cell to the west.
 
Constructor Summary
WorldModel()
          Create a new, empty world model of default cell size.
WorldModel(int cellSize)
          Create a new, empty world model of the given cell size.
 
Method Summary
 void add(Thing t)
          Add the given thing to the world.
 void addView(WorldView v)
          Add the given view.
 void birth()
          Add all items in the nursery to the world.
 void doTurn()
          The basic turn of the simulation consists of calling birth(), action(), reap(), and notifyViews().
 java.util.Iterator getThings()
          Get all the things in the world.
 java.util.List getThings(Thing t)
          Get the neighbors of this thing, that is, those Things that inhabit the same cell.
 java.util.List getThings(Thing t, java.util.List offSets)
          Get the things that inhabit the cells that are in the given list of directions from the cell inhabited by the given thing.
 java.util.List getThings(Thing t, Offset dir)
          Get the things that inhabit the cell that is in the given direction from the cell inhabited by the given thing.
 void go()
          Call init(), and then loop forever, calling doTurn() and sleeping for the sleepTime.
 void go(int cycles)
          Call init(), and then loop for the given number of cycles.
 void init()
          Initialize the world.
 void map(uwcse.sim.WorldModel.CallBack cb)
          Map this call back over the world.
 void notifyViews()
          Notify the views to let them do their thing.
 void remove(Thing t)
          Remove a thing from the world map.
 void setSleepTime(int newTime)
           
 int size()
          Answer the number of things in the world.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_CELL_SIZE

public static final int DEFAULT_CELL_SIZE
The default cell size.

EAST

public static final Offset EAST
Often used offset representing the neighboring cell to the east.

NORTH

public static final Offset NORTH
Often used offset representing the neighboring cell to the north.

NORTHEAST

public static final Offset NORTHEAST
Often used offset representing the neighboring cell to the northeast.

NORTHWEST

public static final Offset NORTHWEST
Often used offset representing the neighboring cell to the northwest.

SOUTH

public static final Offset SOUTH
Often used offset representing the neighboring cell to the south.

SOUTHEAST

public static final Offset SOUTHEAST
Often used offset representing the neighboring cell to the southeast.

SOUTHWEST

public static final Offset SOUTHWEST
Often used offset representing the neighboring cell to the southwest.

WEST

public static final Offset WEST
Often used offset representing the neighboring cell to the west.
Constructor Detail

WorldModel

public WorldModel()
Create a new, empty world model of default cell size.

WorldModel

public WorldModel(int cellSize)
Create a new, empty world model of the given cell size.
Method Detail

add

public void add(Thing t)
Add the given thing to the world. The thing is actually moved into the nursery, so that it can be added in an orderly manner during the birth phase of the simulation.

addView

public void addView(WorldView v)
Add the given view.

birth

public void birth()
Add all items in the nursery to the world.

doTurn

public void doTurn()
The basic turn of the simulation consists of calling birth(), action(), reap(), and notifyViews().

getThings

public java.util.Iterator getThings()
Get all the things in the world.

getThings

public java.util.List getThings(Thing t)
Get the neighbors of this thing, that is, those Things that inhabit the same cell.

getThings

public java.util.List getThings(Thing t,
                                java.util.List offSets)
Get the things that inhabit the cells that are in the given list of directions from the cell inhabited by the given thing.

getThings

public java.util.List getThings(Thing t,
                                Offset dir)
Get the things that inhabit the cell that is in the given direction from the cell inhabited by the given thing.

go

public void go()
Call init(), and then loop forever, calling doTurn() and sleeping for the sleepTime.

go

public void go(int cycles)
Call init(), and then loop for the given number of cycles.

init

public void init()
Initialize the world. Usually overriden by derived classes.

map

public void map(uwcse.sim.WorldModel.CallBack cb)
Map this call back over the world.

notifyViews

public void notifyViews()
Notify the views to let them do their thing.

remove

public void remove(Thing t)
Remove a thing from the world map.

setSleepTime

public void setSleepTime(int newTime)

size

public int size()
Answer the number of things in the world.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object