wyvern.kernel.combat
Class Body

java.lang.Object
  extended bywyvern.kernel.combat.Body
All Implemented Interfaces:
MutableProperty, PartsList, SelfExternalizable
Direct Known Subclasses:
Arachnid, Avian, Draconian, Humanoid, LizardMan, Marilith, NagaCroc, NagaForm, NagaHydra, NoParts, Piscine, Quadruped, Serpentine, Tiger

public abstract class Body
extends java.lang.Object
implements PartsList, SelfExternalizable, MutableProperty

This class encapsulates a list of body parts that can be used to wear & wield things.

Version:
1.0, Jun 08, 1998
Author:
Steve Yegge

Field Summary
protected  BodyPart[] dist_
           
protected  java.util.ArrayList parts_
           
protected  int total_
           
 
Constructor Summary
Body()
          Constructs a new Body
 
Method Summary
 void addPart(BodyPart part)
          Adds the specified body-part to the list.
 void addPartFirst(BodyPart part)
          Adds a part to the front of the list.
protected abstract  void addParts()
          This method is invoked by the Body constructor to tell the subclass to add its parts.
protected  void addWeightedPart(BodyPart part, int weight)
          Adds a new part with the specified weight.
 BodyPart chooseRandomPart()
          Asks for a body-part to use as a hit location, computed using the relative "weight" (size) of each body part.
 java.lang.Object createClone()
          Creates a clone of this parts list.
 boolean equals(java.lang.Object o)
          Returns true if the passed parts list matches this one.
 BodyPart find(GenericPredicate p)
          Iterates over the parts, looking for one that satisfies the passed predicate.
 BodyPart get(int index)
          Returns the part at the specified index, or null if the index is out of bounds.
 BodyPart getByHashcode(int hashcode)
          Retrieves a part from the list by its hash code.
 java.util.List getParts()
          Returns the body parts in this list.
 int getTotalProbability()
          Returns the total of the probability weights of the parts in this body.
 boolean isEmpty()
          Returns true if there are no parts in the list.
 java.util.Iterator iterator()
          Returns an iterator over the parts-list.
protected  void parsePartElement(org.w3c.dom.Element node)
          Parses a "part" element and creates a BodyPart from it.
protected  void parsePartProperties(org.w3c.dom.Element node, BodyPart part)
          Parses the XML property declarations and adds them to the (already-constructed) BodyPart
protected  void parseToHit(BodyPart part, org.w3c.dom.Element child, java.lang.String value)
          Parses a "tohit" element and sets the to-hit on the body part
protected  void parseWC(BodyPart part, org.w3c.dom.Element child, java.lang.String value)
          Parses a "wc" element and adds the resulting WC to the body part
 void readData(org.w3c.dom.Node bean)
          Tells the object to parse its externalized representation from a list of lines.
 void removePart(BodyPart part)
          Removes the specified body-part from the list.
 void replacePart(BodyPart part)
          Replaces a body part in the list.
 boolean shouldWrite()
          Returns true if we should write the property at all.
 int size()
          Returns the number of parts in the list.
 java.lang.String toString()
          Returns a String representation of the parts-list.
 java.util.List writeData()
          Tells the object to write its properties out to a text file.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

parts_

protected java.util.ArrayList parts_

dist_

protected BodyPart[] dist_

total_

protected int total_
Constructor Detail

Body

public Body()
Constructs a new Body

Method Detail

addPartFirst

public void addPartFirst(BodyPart part)
Adds a part to the front of the list.

Specified by:
addPartFirst in interface PartsList

addPart

public void addPart(BodyPart part)
Adds the specified body-part to the list.

Specified by:
addPart in interface PartsList
Parameters:
part - the part to add

replacePart

public void replacePart(BodyPart part)
Replaces a body part in the list.

Parameters:
part - the part to replace. Finds the part in the list with the same unique name and replace it with the new part. If the parts don't have unique names, just replaces the first part with the same name.

removePart

public void removePart(BodyPart part)
Removes the specified body-part from the list.

Specified by:
removePart in interface PartsList
Returns:
the part to remove

getByHashcode

public BodyPart getByHashcode(int hashcode)
Retrieves a part from the list by its hash code.

Specified by:
getByHashcode in interface PartsList
Returns:
the part, or null

find

public BodyPart find(GenericPredicate p)
Iterates over the parts, looking for one that satisfies the passed predicate.

Specified by:
find in interface PartsList
Parameters:
p - the Predicate function

iterator

public java.util.Iterator iterator()
Returns an iterator over the parts-list.

Specified by:
iterator in interface PartsList
Returns:
a java.util.Iterator

get

public BodyPart get(int index)
Returns the part at the specified index, or null if the index is out of bounds.

Specified by:
get in interface PartsList
Parameters:
index - the index in the parts-list
Returns:
the part at that index, or null if out-of-bounds

isEmpty

public boolean isEmpty()
Returns true if there are no parts in the list.

Specified by:
isEmpty in interface PartsList

size

public int size()
Returns the number of parts in the list.

Specified by:
size in interface PartsList

getParts

public java.util.List getParts()
Returns the body parts in this list.

Specified by:
getParts in interface PartsList

getTotalProbability

public int getTotalProbability()
Returns the total of the probability weights of the parts in this body.


chooseRandomPart

public BodyPart chooseRandomPart()
Asks for a body-part to use as a hit location, computed using the relative "weight" (size) of each body part.

Specified by:
chooseRandomPart in interface PartsList
Returns:
a random body part, using a weighted distribution

createClone

public java.lang.Object createClone()
Creates a clone of this parts list.

Specified by:
createClone in interface MutableProperty

toString

public java.lang.String toString()
Returns a String representation of the parts-list.


readData

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

Specified by:
readData in interface SelfExternalizable
Parameters:
bean - the DOM node representing the bean element
Throws:
java.lang.Exception

parsePartElement

protected void parsePartElement(org.w3c.dom.Element node)
                         throws java.lang.Exception
Parses a "part" element and creates a BodyPart from it.

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

parsePartProperties

protected void parsePartProperties(org.w3c.dom.Element node,
                                   BodyPart part)
                            throws java.lang.Exception
Parses the XML property declarations and adds them to the (already-constructed) BodyPart

Parameters:
node - the "part" element, with optional child nodes
Throws:
java.lang.Exception

parseWC

protected void parseWC(BodyPart part,
                       org.w3c.dom.Element child,
                       java.lang.String value)
                throws java.lang.Exception
Parses a "wc" element and adds the resulting WC to the body part

Throws:
java.lang.Exception

parseToHit

protected void parseToHit(BodyPart part,
                          org.w3c.dom.Element child,
                          java.lang.String value)
                   throws java.lang.Exception
Parses a "tohit" element and sets the to-hit on the body part

Throws:
java.lang.Exception

writeData

public java.util.List writeData()
                         throws java.lang.Exception
Tells the object to write its properties out to a text file. The object does not need to write the block braces; they are written to the stream by the system.

Specified by:
writeData in interface SelfExternalizable
Returns:
a String representation that can be written to the text file.
Throws:
java.lang.Exception

shouldWrite

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

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

equals

public boolean equals(java.lang.Object o)
Returns true if the passed parts list matches this one.


addParts

protected abstract void addParts()
This method is invoked by the Body constructor to tell the subclass to add its parts.


addWeightedPart

protected void addWeightedPart(BodyPart part,
                               int weight)
Adds a new part with the specified weight.