wyvern.kernel.commands
Class AbstractAI

java.lang.Object
  extended byjava.lang.Thread
      extended bywyvern.kernel.commands.AbstractAI
All Implemented Interfaces:
AI, java.lang.Runnable
Direct Known Subclasses:
BeelineAI, ShipAI, StandardAI

public abstract class AbstractAI
extends java.lang.Thread
implements AI

Common superclass all AI objects. An AbstractAI has several functions:

  1. Keep track of the commandables in its control, in case they need to cooperate.
  2. Implement the built-in commands for its commandables.
  3. Communicate with the Dispatcher to command the commandables when they need something to do.
For (1), it keeps a hashtable of its commandables. AbstractAI Subclasses may choose to keep the commandables in multiple data structures, as convenient.

For (2), the AbstractAI keeps a CommandList containing the standard game commands, which are all coded to work for commandables.

For (3), the AI has a thread managing a queue of EventQueues. When a commandable's EventQueue is finished waiting from the previous command, the queue notifies the AI. The AI puts the commandable's queue in its internal queue, and puts commands into the queues ("think") as fast as it can.

Version:
1.0, Sep 08, 1997
Author:
Steve Yegge

Field Summary
protected  java.util.Set commandables_
          This is a self-synchronized set of the Commandable objects under this AI's control.
protected  CommandList commandList_
          This is a list of built-in game commands.
protected  java.util.List queues_
          This is a self-synchronized list of the empty queues that the AI currently needs to produce (or "think") for.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Fields inherited from interface wyvern.lib.AI
DEFAULT_MONSTER_INERTIA, DEFAULT_WANDER_RANGE
 
Constructor Summary
AbstractAI()
          Constructs a new AbstractAI with the default thread name.
AbstractAI(java.lang.String name)
          Constructs a new AbstractAI with the specified thread name.
 
Method Summary
 void addCommandable(Commandable m)
          Adds a Commandable to this AI's sphere of influence.
 void addToHateList(Commandable defender, Commandable attacker)
          Adds someone to a monster's I-hate-you list.
 java.lang.String chooseRandomDir()
          Chooses a random direction for the commandable.
static int getAverageTime(java.lang.String classname)
          Returns average wall-clock time spent in think() for a given AI class.
 Command getCommand(CommandEvent cmd, Commandable agent)
          Returns the Command that will handle the specified command for a commandable.
static int getNumThinks(java.lang.String classname)
          Returns the total number of times think() has been invoked for a given AI class.
static java.lang.String getProfilingInfo()
          Returns profiling information across all AIs active in the game.
static int getTotalTime(java.lang.String classname)
          Returns total time spent in think() for a given AI class.
 boolean isControlling(Commandable c)
          Returns true if the AI is producing for the specified Commandable's queue (i.e. it thinks it's controlling the commandable).
 void moveRandomly(Commandable agent)
          Moves the monster in a random direction, possibly pausing.
 void notifyAttacked(Commandable defender, Commandable attacker)
          When a monster is attacked, it sends this message to its AI.
 void registerCommand(java.lang.String command, Command handler, Commandable agent)
          Registers a command with the AI.
 void removeCommandable(Commandable c)
          Removes a Commandable from this AI's control.
 void requestEvent(EventQueue q)
          Notifies the AI that a queue is ready to be serviced.
 void run()
          Starts the AbstractAI thread.
protected  void serviceQueues()
          "Think" for all the ready commandables.
abstract  void think(Commandable m, EventQueue q)
          Decides what to do next.
 void thinkForQueue(EventQueue q)
          Takes a queue and puts something in it, based on properties of the owner of the queue.
 void unregisterCommand(java.lang.String command, Command handler, Commandable agent)
          Unregisters a command with the AI.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

commandables_

protected java.util.Set commandables_
This is a self-synchronized set of the Commandable objects under this AI's control.


commandList_

protected CommandList commandList_
This is a list of built-in game commands. Since they're stateless, it will eventually share references to singleton command handlers with the rest of the game.


queues_

protected java.util.List queues_
This is a self-synchronized list of the empty queues that the AI currently needs to produce (or "think") for.

Constructor Detail

AbstractAI

public AbstractAI()
Constructs a new AbstractAI with the default thread name.


AbstractAI

public AbstractAI(java.lang.String name)
Constructs a new AbstractAI with the specified thread name.

This constructor creates the table of commandables (often monsters) under its control, the list of queues to produce commands for, and a list of builtin command handlers for common commands that monsters use.

Method Detail

addCommandable

public void addCommandable(Commandable m)
Description copied from interface: AI
Adds a Commandable to this AI's sphere of influence.

Specified by:
addCommandable in interface AI
Parameters:
m - a Commandable to add.

removeCommandable

public void removeCommandable(Commandable c)
Description copied from interface: AI
Removes a Commandable from this AI's control.

Specified by:
removeCommandable in interface AI
Parameters:
c - the Commandable to remove

registerCommand

public void registerCommand(java.lang.String command,
                            Command handler,
                            Commandable agent)
Description copied from interface: AI
Registers a command with the AI.

Specified by:
registerCommand in interface AI
Parameters:
command - the command to register
handler - the Command object who's registering it
agent - the agent for which to register the command

unregisterCommand

public void unregisterCommand(java.lang.String command,
                              Command handler,
                              Commandable agent)
Description copied from interface: AI
Unregisters a command with the AI.

Specified by:
unregisterCommand in interface AI
Parameters:
command - the command to unregister
handler - the Command object who's unregistering it
agent - the agent that used to have the command registered

getCommand

public Command getCommand(CommandEvent cmd,
                          Commandable agent)
Description copied from interface: AI
Returns the Command that will handle the specified command for a commandable.

Specified by:
getCommand in interface AI
Parameters:
agent - the Commandable who is being commanded
cmd - the command the Commandable is trying to perform
Returns:
the Command object to handle the command

think

public abstract void think(Commandable m,
                           EventQueue q)
Description copied from interface: AI
Decides what to do next. Subclasses must override this and put a command in the queue, even if it's just a WaitCommand.

Specified by:
think in interface AI
Parameters:
m - the Commandable to think for
q - the Commandable's event queue, for convenience (it's the same as the one returned by m.getQueue() );

isControlling

public boolean isControlling(Commandable c)
Description copied from interface: AI
Returns true if the AI is producing for the specified Commandable's queue (i.e. it thinks it's controlling the commandable).

Specified by:
isControlling in interface AI
Parameters:
c - the Commandable to check
Returns:
true if it's in this AI's list

run

public void run()
Starts the AbstractAI thread.

Specified by:
run in interface java.lang.Runnable

serviceQueues

protected void serviceQueues()
"Think" for all the ready commandables.


thinkForQueue

public void thinkForQueue(EventQueue q)
Description copied from interface: AI
Takes a queue and puts something in it, based on properties of the owner of the queue. Don't call this. It's called when the AI's thread pool runs the queue.

Specified by:
thinkForQueue in interface AI
Parameters:
q - the Commandable's event queue, for convenience (it's the same as the one returned by m.getQueue() );

requestEvent

public void requestEvent(EventQueue q)
Description copied from interface: AI
Notifies the AI that a queue is ready to be serviced.

Specified by:
requestEvent in interface AI
Parameters:
q - the EventQueue that's ready for an event

chooseRandomDir

public java.lang.String chooseRandomDir()
Chooses a random direction for the commandable. Utility method for subclasses.

Returns:
a direction command, or "wait" 1 time in 9

notifyAttacked

public void notifyAttacked(Commandable defender,
                           Commandable attacker)
Description copied from interface: AI
When a monster is attacked, it sends this message to its AI. The AI can do whatever it likes with the information. Most AIs make the monster hostile towards the attacker.

Specified by:
notifyAttacked in interface AI
Parameters:
defender - the monster under the AI's control
attacker - whoever attacked the monster

moveRandomly

public void moveRandomly(Commandable agent)
Moves the monster in a random direction, possibly pausing. If the monster has a "stay-near" property (wyvern.lib.Point), the monster will try to stay within "wander-range" of the specified point, with a default range if not specified.

Parameters:
agent - the monster to command

addToHateList

public void addToHateList(Commandable defender,
                          Commandable attacker)
Description copied from interface: AI
Adds someone to a monster's I-hate-you list.

Specified by:
addToHateList in interface AI
Parameters:
defender - the monster who now hates someone
attacker - the player or monster the hater hates

getTotalTime

public static int getTotalTime(java.lang.String classname)
Returns total time spent in think() for a given AI class.

Parameters:
classname - something like wyvern.kernel.monsters.StandardAI
Returns:
the total wall-clock time spent in think() for that class

getNumThinks

public static int getNumThinks(java.lang.String classname)
Returns the total number of times think() has been invoked for a given AI class.

Parameters:
classname - something like wyvern.kernel.monsters.StandardAI
Returns:
the total number of times think() has been called for that AI class

getAverageTime

public static int getAverageTime(java.lang.String classname)
Returns average wall-clock time spent in think() for a given AI class.

Parameters:
classname - something like wyvern.kernel.monsters.StandardAI
Returns:
average time (total time over number of think() calls)

getProfilingInfo

public static java.lang.String getProfilingInfo()
Returns profiling information across all AIs active in the game.

Returns:
a printout of all the AI profiling info