wyvern.kernel.monsters
Class Talker

java.lang.Object
  extended bywyvern.kernel.monsters.Talker
All Implemented Interfaces:
SelfExternalizable

public class Talker
extends java.lang.Object
implements SelfExternalizable

A Talker property allows you to make a monster or NPC reply to a query given via the "talk" command. It allows you to add cycling or random responses, and you can answer more than one subject string with the same response or set of responses.

Version:
1.0, Feb 20, 1998, 1.1, Jul 14, 2000
Author:
kiz, stevey

Nested Class Summary
 class Talker.Reply
          Encapsulates a reply and any attributes it might have.
 class Talker.Response
          Keeps track of the response(s) to a given subject.
 
Field Summary
protected  java.lang.String npcName_
          the name of the NPC currently talking.
protected  java.util.HashMap topics_
          Subjects that the talker knows about.
 
Constructor Summary
Talker()
          Constructs a new Talker property with no filename.
Talker(java.lang.String name)
          Constructs a new Talker object.
 
Method Summary
protected  void barf(java.lang.String type, java.lang.String name)
          Throws a semi-descriptive parse error.
protected  void combatElement(Talker.Response r, java.util.LinkedList out)
          Adds a element tag.
 Talker.Response createResponse(java.util.List keywords, java.lang.String reply, boolean nosay)
          Creates an initial response with the specified keywords and initial reply.
 java.lang.String getCombatMessage()
          Returns a combat message.
protected  java.lang.String getNodeText(org.w3c.dom.Element node)
          Returns the text from a text element.
 Talker.Response getResponse(java.lang.String query)
          Looks up the Response for a given subject.
 java.util.Set getResponses()
          Returns all the Responses for this Talker.
 boolean isEmpty()
          True if the talker is empty.
 boolean knowsSubject(java.lang.String subject)
          Returns true if we have a Response for the passed subject.
 Talker makeClone()
          Creates a clone of this Talker object.
 void newSubject(java.util.List keywords, Talker.Response r)
          Sets a new subject with an existing response.
protected  void parseChance(org.w3c.dom.Element node, Talker.Response response)
          Parses a "chance" attribute in a topic or combat node.
protected  void parseCombatMessage(org.w3c.dom.Element node)
          Parses a combat message to use when the monster is in combat.
protected  void parseElement(org.w3c.dom.Element node)
          Parses an XML element from the talk data.
protected  void parseTopic(org.w3c.dom.Element node)
          Parses a talk topic.
 void readData(org.w3c.dom.Node node)
          Tells the object to parse its externalized representation from a list of lines.
 void removeSubject(java.util.List keywords)
          Removes the list of keywords from the Talker.
 java.lang.String removeUnnecessaryWords(java.lang.String s)
          Removes any unnecessary text from the subject that you are asking about.
 void setName(java.lang.String name)
          Sets the name of the NPC doing the talking.
 boolean shouldWrite()
          Returns true if we should write the object at all.
 java.lang.String talk(java.lang.String query, java.lang.String player)
          This method takes a query (and the name of the player who's talking) and figures out what to say by reading a "talk file".
protected  void topicElement(Talker.Response r, java.util.LinkedList out)
          Adds a element tag.
 java.util.List writeData()
          Tells the object to write its data out to a text file.
protected  void writeReplies(Talker.Response r, java.util.LinkedList out)
          Writes the reply or random/cyclic replies.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

npcName_

protected java.lang.String npcName_
the name of the NPC currently talking.


topics_

protected java.util.HashMap topics_
Subjects that the talker knows about. Requires exact matches. Keys are the subject strings; values are Response objects that return the only, random or cycling response to the subject.

Constructor Detail

Talker

public Talker()
Constructs a new Talker property with no filename. The name must be set (via setName()) befor the Talker can be used.


Talker

public Talker(java.lang.String name)
Constructs a new Talker object.

Parameters:
name - the name of the file to load, minus extension (typically the NPC name this Talker is associated with).
Method Detail

setName

public void setName(java.lang.String name)
Sets the name of the NPC doing the talking.


talk

public java.lang.String talk(java.lang.String query,
                             java.lang.String player)
This method takes a query (and the name of the player who's talking) and figures out what to say by reading a "talk file".

Parameters:
query - the user's query string, minus the "talk" verb
player - the name of the player doing the query
Returns:
a LinkedList of lines to print to the requestor. Returns null if there is no response.

getCombatMessage

public java.lang.String getCombatMessage()
Returns a combat message. Returns null if no message should be printed.


removeUnnecessaryWords

public java.lang.String removeUnnecessaryWords(java.lang.String s)
Removes any unnecessary text from the subject that you are asking about. Whitespace, the words "about " and such are all cut out.


getResponse

public Talker.Response getResponse(java.lang.String query)
Looks up the Response for a given subject.

Parameters:
query - the subject/keyword to look up
Returns:
response the response, or null if there isn't one

knowsSubject

public boolean knowsSubject(java.lang.String subject)
Returns true if we have a Response for the passed subject.


readData

public void readData(org.w3c.dom.Node node)
              throws java.lang.Exception
Tells the object to parse its externalized representation from a list of lines.

Specified by:
readData in interface SelfExternalizable
Parameters:
node - the XML DOM representation of the externalized bean object. Its child could be a text node, in which case the bean has to do all the parsing for the data. It could also wrap a series of elements (e.g. archetypes), in which case the bean may wish to leverage the XML parsing utilities that the game provides in the ArchetypeManager.

Throws:
java.lang.Exception

parseElement

protected void parseElement(org.w3c.dom.Element node)
                     throws java.lang.Exception
Parses an XML element from the talk data.

Parameters:
node - the element
Throws:
java.lang.Exception

parseTopic

protected void parseTopic(org.w3c.dom.Element node)
Parses a talk topic.


parseCombatMessage

protected void parseCombatMessage(org.w3c.dom.Element node)
Parses a combat message to use when the monster is in combat.


getNodeText

protected java.lang.String getNodeText(org.w3c.dom.Element node)
Returns the text from a text element.

Parameters:
node - the text node beginning of the line.

parseChance

protected void parseChance(org.w3c.dom.Element node,
                           Talker.Response response)
Parses a "chance" attribute in a topic or combat node.


barf

protected void barf(java.lang.String type,
                    java.lang.String name)
Throws a semi-descriptive parse error.


removeSubject

public void removeSubject(java.util.List keywords)
Removes the list of keywords from the Talker. If it's all the keywords for a given subject, the response goes away as well.


newSubject

public void newSubject(java.util.List keywords,
                       Talker.Response r)
Sets a new subject with an existing response.


createResponse

public Talker.Response createResponse(java.util.List keywords,
                                      java.lang.String reply,
                                      boolean nosay)
Creates an initial response with the specified keywords and initial reply. Used by map editor when first creating a new topic.


getResponses

public java.util.Set getResponses()
Returns all the Responses for this Talker. Each Response keeps track of one or more subjects for which it a response.


isEmpty

public boolean isEmpty()
True if the talker is empty.


writeData

public java.util.List writeData()
                         throws java.lang.Exception
Tells the object to write its data out to a text file.

Specified by:
writeData in interface SelfExternalizable
Returns:
a list of lines to be written to the text file, formatted appropriately.
Throws:
java.lang.Exception

shouldWrite

public boolean shouldWrite()
Returns true if we should write the object at all.

Specified by:
shouldWrite in interface SelfExternalizable
Returns:
true to write the object, false to skip it

combatElement

protected void combatElement(Talker.Response r,
                             java.util.LinkedList out)
Adds a element tag.


topicElement

protected void topicElement(Talker.Response r,
                            java.util.LinkedList out)
Adds a element tag.

Parameters:
r - the response we're printing
out - the list of lines to add the XML to

writeReplies

protected void writeReplies(Talker.Response r,
                            java.util.LinkedList out)
Writes the reply or random/cyclic replies.

Parameters:
r - the response
out - the linked list of lines to output

makeClone

public Talker makeClone()
Creates a clone of this Talker object. Used by map editor.