Introduction
The Combatant Class keeps information about individual combatants. Think of them as characters in an RPG.
Documentation
Combatant Exceptions
The following Exceptions are relevant to the Defense class:
AttackError
A non-Attack class based object was passed to the combatant’s AddAttack method
DefenseError
A non-Defense class based object was passed to the combatant’s AddDefense method
Predefined Attacks and Defenses
There is one basic attack given to each combatant and one defense given to each combatant. They don’t do a whole lot on their own:
SimpleAttack
The default attack as defined by the Attack type.
SimpleDefense
The default defense as defined by the Defense type.
Instance Attributes
Combatant attributes usually are in lowercase. Here’s a list of attributes and their defaults:
- name -- passed during creation
- strength -- default 1
- speed -- default 1
- maxlife -- default 20
- life -- starts at maxlife
- chancetohit -- defaults 50
- basedamage -- default 0
- log -- default []
- misscomments -- default ['%s misses’]
- hitcomments -- default ['%s attacks for %d points’]
- Attacks -- default [SimpleAttack]
- Defenses -- default [SimpleDefense]
The misscomments and hitcomments lists are strings with formatting information to pass along to the combat manger. Having several options can color the play-by-play without getting too redundant.
Class Methods
These are the public methods for the Combatant Class.
AddAttack (attackitem)
If the attackitem is an instance of Combat.Attack or one of its subclasses, then the Attack object is added to Attacks list. Otherwise it raises an AttackError.
AddDefense (defenseitem)
If the defenseitem is an instance of Combat.Defense or one of its subclasses, then the Defense object is added to Defenses list. Otherwise it raises an AttackError.
SetBaseDamage (integer)
This method sets the basedamage to an integer. This represents bonus damage that the combatant might do.
generateDamage
This method returns an integer based on the basedamage attribute.
attack
This method returns a random attack mode from the instances Attacks list.
isdead
Returns true if the life attribute is less than or equal to zero, otherwise returns false .
takedamage (integer)
Reduces life by some amount. This method will probably depreciate in the next revision.
addlog (s)
Adds the string to the log list. This is used for writing up play-by-plays of fights
dumplog
Returns the log as a list of strings. This is used in the play-by-play generation
missstring
Returns a random item from the misscomments attribute.
attackstring
Returns a random item from the hitcomments attribute.
armorclass
Adds the BaseArmorClass of each defence and returns their sum
Using Combatant
Subclassing the Combatant Class
Declare the subclass and call the Combat.Combatant.__init__(self,name) method immediately, then override with your defaults. The Combat Manager is supposed to handle most of the attribute manipulation (to cut down on cheats or perhaps because I’m paranoid about this), so there isn’t much you have to do unless you want to add features.
For example in SlugFest Sluggers have permanent effect codes and temporary effect codes, so I’ve added those attributes, and I delete the SimpleAttack Attack instance in SlugFest. See the SlugFest tutorial for more information.
Creating Instances
All you have to do is pass the name when initializing the Combatant, then use the AddAttack and AddDefence methods to give it something to work with.
Future Plans
- The takedamage method will disappear. I don’t want to allow a subclass of Combatant that doesn’t take damage. It will still be possible to do this, but it will require more tricks than declaring takedamage(self,i): pass .