public final class Simulator extends Object implements Icon
To play Darwin, you do not need to read the source code for this class or understand how it works.
The array passed to the Simulator constructor indicates maps spawn point labels (numbers in the map source) to subclasses to be loaded for them. The outer edge of the map is forced to wall blocks regardless of whether it is specified that way or not.
Simulator implements Icon so that it can be rendered. It can also be displayed in text mode in text mode using toString().
Check Simulator.getResult to see if the simulation has ended.
The "time" referred to in the player's guide is virtual. Taking actions does not consume wall-clock time. Instead, the creature's time accumulator is penalized as if the action had occured. This enables the simulator to run faster than real-time. The different speed modes create wall-clock delays between turns (for which no creature is penalized) so that humans can watch the action at a comfortable pace.
All public methods are synchronized unless provably threadsafe.
Morgan McGuire
morgan@cs.williams.edu
Inspired by the RoboRally board game, Steve Freund's "Darwin" lab assignment, and Nick Parlante's "Darwin's World" assignment.
Modifier and Type | Class and Description |
---|---|
static class |
Simulator.Condition |
static class |
Simulator.QuietRunnable
A version of Runnable that suppresses output
|
static class |
Simulator.Result
Return value of
getResult() |
static class |
Simulator.Species
Aggregate information about the performance of an entire species
|
Modifier and Type | Field and Description |
---|---|
static long |
ATTACK_COST |
static long |
DELAY_COST |
static long |
EMIT_PHEROMONE_COST |
static long |
FOG_MOVE_FORWARD_PENTALTY_COST
The additional cost of a move forward operation that enters fog.
|
static long |
KILL_TIME
Any creature that takes more wall-clock time than this is
automatically converted into an Apple (killed).
|
static long |
LOOK_COST
Deprecated.
|
static long |
MILLISECONDS
One millisecond in nanoseconds.
|
static long |
MOVE_BACKWARD_COST |
static long |
MOVE_FORWARD_COST |
static long |
MUD_PENALTY_COST
Additional cost for moving, turning, or attacking while in mud.
|
static long |
MZ_TIME_LIMIT
A maze game ends after this many virtual nanoseconds, even if
no Creature has won.
|
static int |
NS_ASCENSIONS_TO_WIN |
static long |
NS_TIME_LIMIT
A non-maze game ends after this many virtual nanoseconds, even if
no Creature has won.
|
static long |
OBSERVE_COST |
static int |
SCALE_2D
2D grid size in pixels.
|
static long |
SECONDS
One second in nanoseconds.
|
static Class[] |
sharedClasses
Shared between ClassLoader (Sandbox) instances
|
static long |
TURN_COST |
Constructor and Description |
---|
Simulator(String mapFilename,
Class[] creatures)
Loads the specified map, instantiating each of the creatures
for the numbered spots in the map.
|
Modifier and Type | Method and Description |
---|---|
boolean |
attack(Creature e)
Attack the creature in front of this one.
|
static void |
beginSecureExecution()
Turns on the MaximumSecurityManager and prevents Observations
from containing classNames.
|
void |
delay(Creature c)
Take the delay action
|
void |
emitPheromone(Creature e,
String p) |
long |
getComputeTimeSinceSpawn(Creature e) |
Creature |
getCreature(int x,
int y)
Returns the creature at x, y on the grid.
|
Color |
getCreatureColor(Class c) |
Color |
getCreatureColor(int classId) |
int |
getCreatureCount(Class c)
Returns the number of creatures of this species alive in the map.
|
Dimension |
getDimensions()
Returns the size of the map.
|
Direction |
getDirection(Creature e)
Throws a ConvertedError if the Creature is not in the world.
|
int |
getIconHeight() |
int |
getIconWidth() |
static Image |
getImage(Class cs,
Direction d)
Returns the image for this Creature subclass.
|
static Image |
getImage(Creature c,
Direction d) |
String |
getInfo()
Returns a description of the simulator
|
long |
getIntraTurnTime()
Returns the wall-clock time of waitBetweenTurns() in
nanoseconds.
|
static Color |
getMostCommonColor(Map<Integer,Integer> map) |
int |
getNaturalHeight() |
int |
getNaturalWidth() |
int |
getNumSpeciesLeft()
Returns the number of different species left alive, excluding Flytraps.
|
String |
getPheromone(Creature e)
Throws a ConvertedError if the Creature is not in the world.
|
Point |
getPosition(Creature e)
Clones the position.
|
Simulator.Result |
getResult()
Returns a description of the final outcome, or null if the game has not yet ended.
|
static Sound |
getSound(Class cs,
Simulator.Condition condition) |
static Sound |
getSound(Creature c,
Simulator.Condition condition)
Returns the appropriate sound for this creature.
|
long |
getTime()
Returns the number of virtual milliseconds since the game
began.
|
long |
getTime(Creature e)
Total time for this creature; on spawn, this is inherited from the parent.
|
String |
getTitle()
Name of this map
|
String |
getToolTip(Point p)
Assumes that p is in bounds
|
long |
getTotalTimeSinceSpawn(Creature e) |
int |
getTurns(Creature e)
Returns the number of turns this creature has taken.
|
long |
getTurnTime()
Returns the time in virtual nanoseconds since the current creature's
current turn started.
|
static String |
getVersion()
Reports the version of this simulator.
|
boolean |
getView3D() |
boolean |
inFog(Creature e)
Returns true if the Creature's current location is in fog.
|
boolean |
inMud(Creature e)
Returns true if the Creature's current location is in mud.
|
boolean |
isAlive(Creature c)
A creature's thread can't be stopped without potentially
releasing locks that it is using for synchronization, so this
method tells a creature when it should die.
|
boolean |
isEnchanted(Creature e)
Throws a ConvertedError if the Creature is not in the world.
|
boolean |
isRunning()
True while the simulation is running (not stopped).
|
static Class |
loadClass(String name,
boolean secure)
Loads a class in its own Sandbox.
|
boolean |
move(Creature e,
int distance)
Moves this creature forward if the space in front if it is
empty, otherwise does not move.
|
static void |
nanoSleep(long t)
On some systems, Thread.sleep(long, int) has a minimum sleep time of 1ms.
|
Observation[] |
observe(Creature e) |
void |
paintIcon(Component c,
Graphics _g,
int tx,
int ty) |
static String |
readMapTitle(String mapFilename) |
Point |
screenPointToLocation(int x,
int y)
Returns the location under the position (x, y) if in 2D mode
and in bounds, and null otherwise
|
void |
setIntraTurnTime(long t)
Set the wall-clock time to wait between turns in nanoseconds.
|
void |
setSelectedCreature(Creature c) |
void |
setView3D(boolean b)
If true, the view renders in 3D
|
static String |
shortName(String s)
Strips the capital letters off the right of a creature name.
|
int |
shrineClassId(Creature e)
If this creature is on a shrine, returns the classID of the
creatures that own the shrine.
|
void |
start(String mapfile,
Class[] creatures)
Load a new map and start a simulation.
|
void |
stop()
End simulation, without destroying final state.
|
long |
timeLimit()
Total time allowed in virtual nanoseconds for the game on the current map.
|
String |
toString()
Returns a printable text representation of the map
|
void |
turnLeft(Creature e) |
void |
turnRight(Creature e)
Called by a creature to turn
|
public static final Class[] sharedClasses
public static final long SECONDS
public static final long MILLISECONDS
public static final long KILL_TIME
public static final long EMIT_PHEROMONE_COST
public static final long DELAY_COST
public static final long OBSERVE_COST
public static final long LOOK_COST
public static final long ATTACK_COST
public static final long TURN_COST
public static final long MOVE_FORWARD_COST
public static final long MOVE_BACKWARD_COST
public static final long FOG_MOVE_FORWARD_PENTALTY_COST
public static final long MUD_PENALTY_COST
public static final int NS_ASCENSIONS_TO_WIN
public static final long MZ_TIME_LIMIT
public static final long NS_TIME_LIMIT
public static final int SCALE_2D
public static Class loadClass(String name, boolean secure) throws ClassNotFoundException, IOException
ClassNotFoundException
IOException
public static void beginSecureExecution()
public String getInfo()
public long timeLimit()
public long getTime(Creature e)
public long getTotalTimeSinceSpawn(Creature e)
public long getComputeTimeSinceSpawn(Creature e)
public int getTurns(Creature e)
public Point getPosition(Creature e)
public Direction getDirection(Creature e)
public boolean isEnchanted(Creature e)
public boolean inFog(Creature e)
public boolean inMud(Creature e)
public int shrineClassId(Creature e)
Creature.UNINITIALIZED_CLASS_ID
Throws a ConvertedError if the Creature is not in the
world.public String getPheromone(Creature e)
public Creature getCreature(int x, int y)
public static String getVersion()
public String getTitle()
public long getTime()
public Dimension getDimensions()
public long getIntraTurnTime()
public void setIntraTurnTime(long t)
public void setSelectedCreature(Creature c)
public void setView3D(boolean b)
public boolean getView3D()
public boolean isRunning()
public Color getCreatureColor(int classId)
public int getNumSpeciesLeft()
public int getCreatureCount(Class c)
public long getTurnTime()
public Simulator.Result getResult()
public void start(String mapfile, Class[] creatures)
public void stop()
public static void nanoSleep(long t) throws InterruptedException
InterruptedException
public String toString()
public void turnRight(Creature e)
public void turnLeft(Creature e)
public Observation[] observe(Creature e)
public boolean move(Creature e, int distance)
Throws ConvertedError if the Creature is not in the world.
distance
- must be -1 or +1public void delay(Creature c)
public boolean attack(Creature e)
public boolean isAlive(Creature c)
All creatures are considered dead once the simulator is stopped.
public int getIconHeight()
getIconHeight
in interface Icon
public int getNaturalHeight()
public int getIconWidth()
getIconWidth
in interface Icon
public int getNaturalWidth()
public Point screenPointToLocation(int x, int y)
public static Image getImage(Class cs, Direction d)
public static Sound getSound(Creature c, Simulator.Condition condition)
public static Sound getSound(Class cs, Simulator.Condition condition)
public static String shortName(String s)