The attack is one of the most fundamental functions in a d20 game.  It is also one of the most complicated.
 It starts out simply enough:
[assuming the routine is a method of the attacker’s class]

Function Hit(vDefender as Thing) as Boolean
     Return RollDice(1,20) + Me.AttackBonus >= vDefender.ArmorClass
End Function

But Lothar the Fighter has earned Weapon Focus(Longsword) Feat, which means his attack bonus will be higher when he’s using a longsword.  That means we need to know the weapon. 

Function Hit(vDefender as Thing, vWeapon as Thing) as Boolean
     Return RollDice(1,20) + Me.AttackBonus(vWeapon) >= vDefender.ArmorClass
End Function

Lothar happens to be a hill giant attacking a dwarf, so the dwarf gets a special dodge bonus.  That means ArmorClass depends on the attacker.

Function Hit(vDefender as Thing, vWeapon as Thing) as Boolean
     Return RollDice(1,20) + Me.AttackBonus(vWeapon) >= vDefender.ArmorClass(Me)
End Function

Now here is where it starts to get messy.  Lothar is struck by a vampire and loses a level.  The next day he fails the save and loses the level permanently.  So the level at which a feat or quality was gained matters.

Function AttackBonus(vWeapon as Thing) as Long
      Dim Bonus as long = 0
                For each L in Me.Levels
                                If L.WeaponFocus(vWeapon.WeaponType)  Then Bonus +=2
                Next
      Return Bonus
End Function

The game engine will require more information than whether or not a hit was successful.  If Lothar fires into a grapple, he might hit a different target.   The amount and type of damage from a successful hit are relevant to damage reduction and death from massive damage.  What if Lothar is subject to a shape change and only some of his qualities apply while others rely on his new form?

As the developer takes into account additional feats, active spells, and special qualities, the number of function calls can climb exponentially.  How can the developer minimize the overhead while producing the required outputs without a big bowl of “spaghetti code”?  Stay tuned and we’ll discuss some strategies in upcoming articles.




Leave a Reply.