wyvern.lib.classes.random
Class RandomList

java.lang.Object
  extended bywyvern.lib.classes.random.RandomList

public class RandomList
extends java.lang.Object

Parses a treasure/monster list.

Treasure and monster lists are a way to parameterize random generators. The list format is one object per line, with comments beginning with "#" and blank lines OK.

The format for each line is:

   class-or-arch [chance=#] {[random-]property=value}*
 

Class or Archetype:

If archetype, it's a local path like what's found in map and archetype files. Examples:

   monsters/dragon/red_dragon
   wiz/pamela/dungeon/bikini
 
If it's a classname, it needs to be fully-qualified, e.g. "wyvern.lib.classes.Teleporter".

Properties:

Specified properties are added to the generated object. The generator will look for a setProperty() function with the same name as the specified property (first letter gets uppercased). Failing that, it attempts to set the property with that name in the object's property-list.

If "random-" prepends the property name, the property value will be a random number from 1 to value. Value can be a range, such as 5..10 or 0..100, in which case the random number will be chosen from that range.

Examples:

   weapons/arrow random-quantity=20
 
   objects/treasure/diamond random-value=500..1000
 

Weighting:

The "chance" field is optional. If no lines in the file have a "chance" field, then all lines are given equal weighting, 1/N where N is the total number of lines.

If any of the objects specified a "chance" weight, as an integer, then any objects with no "chance" property are given a default chance of 1.

Examples: The following is a list with 3 objects, all archetypes. The axe has a total chance of 5/8 (62.5%), the bow 2/8 (25%), and the bullwhip 1/3 (12.5%):

   weapons/stone_axe chance=5
   weapons/orcish_bow chance=2
   weapons/bullwhip
 

Version:
1.0, Nov 08, 1999
Author:
Steve Yegge

Field Summary
protected  java.util.List objects_
           
protected  int totalWeight_
           
 
Method Summary
protected  void addObject(wyvern.lib.classes.random.RandomList.RandomObject obj)
          Adds an object wrapper to the list.
 GameObject generate()
          Chooses one of the objects in the list and instantiates it.
static RandomList loadList(java.lang.String name)
          Parses and loads the specified treasure list, and returns it as a RandomList instance.
static void main(java.lang.String[] args)
          Tests functionality.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

objects_

protected java.util.List objects_

totalWeight_

protected int totalWeight_
Method Detail

loadList

public static RandomList loadList(java.lang.String name)
Parses and loads the specified treasure list, and returns it as a RandomList instance.

Parameters:
name - the list name, either one of the names in world/lists, or a wiz list like "wiz/foo/bar". The ".list" extension is optional.
Returns:
the parsed RandomList. Note: the lists are cached, so if the specified list has already been loaded, you'll get the cached version. Returns null if there's an IOException reading the file.

generate

public GameObject generate()
                    throws java.lang.Exception
Chooses one of the objects in the list and instantiates it.

Returns:
the object, or null if there was an error creating the list (e.g. no objects in the list).
Throws:
any - exception - the class may fail to load, or newInstance() may fail, or one of the specified properties may have the wrong value for the class.
java.lang.Exception

addObject

protected void addObject(wyvern.lib.classes.random.RandomList.RandomObject obj)
Adds an object wrapper to the list.

Parameters:
obj - a RandomObject produced by parsing a list line

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Tests functionality.

Throws:
java.lang.Exception