Wiz Programming Tutorials

You can write classes to generate custom inventories for your monsters. All you need to do is write a python class that inherits from wyvern.kernel.monsters.InvGenerator, and override the generate() method.

Here's an example orc, wiz/rhialto/orc.arch, that uses a custom inventory. You set the name of your python class in a string property called "auto-inv":

<!-- an orc that uses our own inventory generator -->
<arch path="monsters/goblin/orc">
  <string name="auto-inv" value="wiz/rhialto/python/orc_inv.py"/>

  <!-- corpse-chance set to 100% while we're testing it -->
  <int name="corpse-chance" value="100"/>
</arch>

Here's the python class that generates the Orc inventory:

""" File: orc_inv.py
Orc auto-inventory generator.

Author: Steve Yegge, June 10, 2001
Copyright © 2003, Cabochon Technologies, Inc.

"""

from wyvern.lib import *
from wyvern.kernel.monsters import InvGenerator

class orc_inv(InvGenerator):

# generates the inventory for an orc
#
# Pass: the orc to create inventory for
# Return: nothing, but puts the items
# into the orc's inventory
#
def generate(self, mon):

inv = mon.getInventory()

# Add misc armor - we re-roll for each piece, so each orc
# has a 25% chance of having chainmail, 25% of a shield, etc.
# Any given orc can have from zero to four pieces of armor.

if self.roll() < 25:
self.addItem ( inv, "armor/armor/orcish_chainmail" )
if self.roll() < 25:
self.addItem ( inv, "armor/shield/orc_shield" )
if self.roll() < 25:
self.addItem ( inv, "armor/boots/orcish_boots" )
if self.roll() < 25:
self.addItem ( inv, "armor/helmet/helmet" )

# we'll choose one weapon, so we only do one roll
w = self.roll();

# 5% chance for a spear
if w < 5:
self.addItem ( inv, "weapons/orcish_spear" )

# 45% chance for an axe
elif w < 50:
self.addItem ( inv, "weapons/stone_axe" )

# 25% chance for a bow and arrows
elif w < 75:
self.addItem ( inv, "weapons/orcish_bow" )
arrows = self.cloneItem ( "weapons/arrow" )
arrows.setQuantity ( Range.randomValue ( 10, 20 ))
try:
inv.add ( arrows )
except Exception, xc:
Kernel.fine ( "wiz/rhialto/python/orc_inv",
"generate",
"Couldn't add arrows: " + str(xc))

# 20% chance for an orcish scimitar
elif w < 95:
self.addItem ( inv, "weapons/orcish_scimitar" )

# 5% chance for a broadsword
else:
self.addItem ( inv, "weapons/broadsword" )


# 30% chance of random treasure:
# 20% chance for random level 1 treasure
# 10% chance for random level 2 treasure
t = self.roll();
if t < 20:
self.randomItem ( inv, "random/treasure/random_treasure", 1 )
elif t < 30:
self.randomItem ( inv, "random/treasure/random_treasure", 2 )pre>

This class, orc_inv, inherits from wyvern.kernel.monsters.InvGenerator. InvGenerator provides some useful functions:

  1. roll: Rolls a random percentage from 1 to 100.
  2. addItem: Takes an archetype path (as a string), clones it, and sticks it in the passed inventory.
  3. cloneItem: Clones an item from its archetype path. You do this when you want to change the properties on the cloned item before putting it in the monster's inventory. In the example above, we set the arrow quantity from 10 to 20 arrows, if orc is getting some arrows.
  4. randomItem: clones a random archetype with the specified level.

That's pretty much all there is to it! If you know a little Jython, you can put anything you want into a monster's inventory.

<< Previous Chapter Next Chapter >>