wyvern.lib.classes.random
Class WeaponEnchanter

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

public class WeaponEnchanter
extends java.lang.Object

An object that can produce magical weapons of a given level by picking a base weapon and adding enchantments.

To use it, instantiate one and (optionally) configure it by setting properties. Then call generate() to generate a weapon. You can use it to generate more than one weapon. You need to call reset() before each subsequent call to generate().

For example, to generate a random 15th-level weapon with a 20% chance of being cursed, you'd do this:

   WeaponEnchanter w = new WeaponEnchanter();
   w.setCurseChance(20);
   GameObject weapon = w.generate();
 
or in Jython:
   w = new WeaponEnchanter();
   w.setCurseChance(20);
   weapon = w.generate();
 

Version:
2.0, Oct 21, 2003
Author:
Steve Yegge

Nested Class Summary
static class WeaponEnchanter.WeaponEnchantment
          A class to encapsulate an enchantment.
 
Field Summary
protected  int baseLevel_
          The level of nonmagical weapon to start with.
protected  int blessChance_
          Chance the weapon will be blessed, if it's not cursed.
protected  int curseChance_
          Chance the weapon will be cursed.
protected  int damnationChance_
          Chance that the curse on a weapon will be Damnation.
static int DEFAULT_BLESS_CHANCE
          The default chance that a randomly generated level-1 weapon have a .
static int DEFAULT_CURSE_CHANCE
          The default chance that a randomly generated level-1 weapon have a curse on it.
static int DEFAULT_CURSE_CHANCE_MALIGNED
          If we randomly decided that the weapon has reversed ("maligned") enchantments (e.g. -5 strength), this is the chance that the item will be cursed, such that the player can't unwield it.
static int DEFAULT_DAMNATION_CHANCE
          Same as cursing, but worse.
static int DEFAULT_MALIGN_CHANCE
          The default chance that a randomly generated level-1 weapon will have a cursed enchantment.
static int DEFAULT_MISSILE_MAX_QUANTITY
          How much ammo we generate, max, by default.
protected  int enchant1_
          Level of first random enchantment to add to the item.
protected  int enchant2_
          Level of second random enchantment to add to the item.
protected  int enchantment_
          Total levels of enchantment to add to the base item.
protected  int level_
          Desired level of magical weapon to generate.
protected  int malignChance_
          Chance the weapon will be maligned; i.e., its enchantments are negative.
static int MAX_BASELEVEL_WEAPON_LEVEL
          The highest level non-magical weapon in the game (currently an elven broadsword)
static int MAX_LEVEL
          Maximum level of weapon that will be generated.
static int MAX_SINGLE_ENCHANTMENT
          The maximum individual enchantment a random weapon can have.
protected  GameObject weapon_
          the nonmagic weapon we create, then enchant
 
Constructor Summary
WeaponEnchanter()
           
 
Method Summary
protected  void addAccuracy(GameObject weapon, int value)
          Adds an enchantment of accuracy to the weapon.
protected  void addDamage(GameObject weapon, int value)
          Adds an enchantment of damage to the weapon.
protected  void addDurability(GameObject weapon, int value)
          Adds an enchantment of durability to the weapon.
protected  void addEnchantments()
          Adds 1 or 2 enchantments, depending on the results of the call to determineEnchantments().
protected  void addLight(GameObject weapon, int value)
          Adds an enchantment of light to the weapon.
protected  void addQuantityIfNeeded()
          If the generated weapon is a missile, or has a "gen-number" property, adds a quantity to the weapon.
 java.lang.String addRandomEnchantment(GameObject weapon, int value, java.lang.String exclude)
          Adds a random enchantment to the passed weapon of the passed value.
protected  void addSpeed(GameObject weapon, int value)
          Adds an enchantment of speed to the weapon.
protected  void addWeight(GameObject weapon, int value)
          Adds an enchantment of weight to the weapon.
protected  void blessOrCurseItem()
          Decides whether the item should be blessed or cursed.
protected  int chooseBaseLevel()
          Chooses the base (i.e. unenchanted) weapon level to use for the item.
protected  int chooseWeightedValue(int base)
          Uses a normal distribution to choose a value from 1 to n, on average closer to n than 1.
protected  GameObject createBaseWeapon(int level)
          Creates a random unenchanted weapon of the passed level.
 GameObject createLevel1Weapon()
          Returns a random level-1 weapon.
protected  void createNonMagicWeapon()
          Picks a random base weapon level and instantiates a random nonmagic weapon of that level.
protected  void curseEnchantments()
          Reverses enchantment number(s), and marks the weapon as "is-cursed", for the Appraisal skill.
 void curseWeapon()
          Curses (with getDamnationChance() of damning) an item.
protected  void determineEnchantments()
          Sets the total enchantment and the individual enchantment(s), randomly, based on the item level and item base.
protected  void flagEnchantmentForAppraisers()
          Sets the total enchantment in a "base-enchantment" property for the Appraisal skill.
 GameObject generate()
          Generates a random weapon.
static GameObject generate(int level, java.lang.String name, int value)
          Generates a random weapon using the specified single enchantment
 int getBaseLevel()
          Returns the base level of nonmagical weapon to start with, before adding enchantments.
 int getBlessChance()
          Returns chance that the item will be blessed (1-100)
 int getCurseChance()
          Returns chance that the item will be cursed (1-100)
 int getDamnationChance()
          Returns chance that the item will be damned (1-100), if it rolls the getDamnationChance().
 int getLevel()
          Returns level of the weapon to be generated
 int getMalignChance()
          Returns chance the item will have a maligned (negative) enchantment
protected  java.lang.String getMaxWC(GameObject weapon)
          Returns the highest weapon class of this weapon.
 int getTotalEnchantment()
          Returns the total enchantment (the sum of up to 2 individual enchantments) we decided to place on the weapon.
 GameObject getWeapon()
          Returns the weapon to add enchantments to
protected  GameObject instantiate(Archetype arch)
          Instantiates an archetype, catching any exception.
 boolean isMaligned()
          Returns true if the weapon we're generating is maligned (i.e. the enchantments are reversed, and have negative values.)
static void main(java.lang.String[] args)
          Tests the enchanter functions.
 java.util.List parseEnchantment(java.lang.String input)
          Take a string like "of agility +5, damage +2" and parses it into a java.util.List of WeaponEnchantments.
 void reset()
          Resets the WeaponEnchanter properties to their default values.
 void setBaseLevel(int base)
          Sets the base level to use for the weapon.
 void setBlessChance(int blessChance)
          Sets chance that the item will be blessed (1-100)
 void setCurseChance(int curseChance)
          Sets chance that the item will be cursed (1-100)
 void setDamnationChance(int chance)
          Sets chance that the item will be damned (1-100)
 void setLevel(int level)
          Sets level of the weapon to be generated.
 void setMalignChance(int malignChance)
          Sets chance the item will have a maligned (negative) enchantment
 void setWeapon(GameObject weapon)
          Sets the weapon to add enchantments to
protected  void setWieldLevel()
          Sets an appropriate "wield-level" on the weapon, based on its final enchantment(s), so that newbies can't wield it to major advantage.
protected  void sortEnchantments()
          If 2nd enchantment is greater, swaps them (easier to read).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MALIGN_CHANCE

public static final int DEFAULT_MALIGN_CHANCE
The default chance that a randomly generated level-1 weapon will have a cursed enchantment. It goes up 1 point per level, up to a maximum of 50% at level 20. Override with setMalignChance().

See Also:
Constant Field Values

DEFAULT_BLESS_CHANCE

public static final int DEFAULT_BLESS_CHANCE
The default chance that a randomly generated level-1 weapon have a . It goes up 1 point per level, up to a maximum of 50% at level 20. Override with setBlessChance().

See Also:
Constant Field Values

DEFAULT_CURSE_CHANCE

public static final int DEFAULT_CURSE_CHANCE
The default chance that a randomly generated level-1 weapon have a curse on it. It goes up 1 point per level, up to a maximum of 50% at level 20. Override with setCurseChance().

See Also:
Constant Field Values

DEFAULT_CURSE_CHANCE_MALIGNED

public static final int DEFAULT_CURSE_CHANCE_MALIGNED
If we randomly decided that the weapon has reversed ("maligned") enchantments (e.g. -5 strength), this is the chance that the item will be cursed, such that the player can't unwield it.

See Also:
Constant Field Values

DEFAULT_DAMNATION_CHANCE

public static final int DEFAULT_DAMNATION_CHANCE
Same as cursing, but worse. Only this percentage of all cursed items wind up damned. Override with getDamnationChance().

See Also:
Constant Field Values

MAX_LEVEL

public static final int MAX_LEVEL
Maximum level of weapon that will be generated.

See Also:
Constant Field Values

MAX_BASELEVEL_WEAPON_LEVEL

public static final int MAX_BASELEVEL_WEAPON_LEVEL
The highest level non-magical weapon in the game (currently an elven broadsword)

See Also:
Constant Field Values

DEFAULT_MISSILE_MAX_QUANTITY

public static final int DEFAULT_MISSILE_MAX_QUANTITY
How much ammo we generate, max, by default.

See Also:
Constant Field Values

MAX_SINGLE_ENCHANTMENT

public static final int MAX_SINGLE_ENCHANTMENT
The maximum individual enchantment a random weapon can have. E.g. if it's a 20th-level item, it may have +10 strength and +9 dodge, but it can't have +11 strength (if the max is set to +10).

See Also:
Constant Field Values

malignChance_

protected int malignChance_
Chance the weapon will be maligned; i.e., its enchantments are negative.


curseChance_

protected int curseChance_
Chance the weapon will be cursed.


damnationChance_

protected int damnationChance_
Chance that the curse on a weapon will be Damnation.


blessChance_

protected int blessChance_
Chance the weapon will be blessed, if it's not cursed.


level_

protected int level_
Desired level of magical weapon to generate.


baseLevel_

protected int baseLevel_
The level of nonmagical weapon to start with.


weapon_

protected GameObject weapon_
the nonmagic weapon we create, then enchant


enchantment_

protected int enchantment_
Total levels of enchantment to add to the base item.


enchant1_

protected int enchant1_
Level of first random enchantment to add to the item.


enchant2_

protected int enchant2_
Level of second random enchantment to add to the item.

Constructor Detail

WeaponEnchanter

public WeaponEnchanter()
Method Detail

getLevel

public int getLevel()
Returns level of the weapon to be generated

Returns:
level of the weapon to be generated

setLevel

public void setLevel(int level)
Sets level of the weapon to be generated.

Parameters:
level - level of the weapon to be generated (1-MAX_LEVEL)

getCurseChance

public int getCurseChance()
Returns chance that the item will be cursed (1-100)

Returns:
chance that the item will be cursed (1-100)

setCurseChance

public void setCurseChance(int curseChance)
Sets chance that the item will be cursed (1-100)

Parameters:
curseChance - chance that the item will be cursed (1-100)

getDamnationChance

public int getDamnationChance()
Returns chance that the item will be damned (1-100), if it rolls the getDamnationChance().

Returns:
chance that the item will be damned if we decided it was going to be damned.

setDamnationChance

public void setDamnationChance(int chance)
Sets chance that the item will be damned (1-100)

Parameters:
chance - chance that the item will be damned (1-100)

getBlessChance

public int getBlessChance()
Returns chance that the item will be blessed (1-100)

Returns:
chance that the item will be blessed (1-100)

setBlessChance

public void setBlessChance(int blessChance)
Sets chance that the item will be blessed (1-100)

Parameters:
blessChance - chance that the item will be blessed (1-100)

getMalignChance

public int getMalignChance()
Returns chance the item will have a maligned (negative) enchantment

Returns:
chance the item will have a maligned enchantment

setMalignChance

public void setMalignChance(int malignChance)
Sets chance the item will have a maligned (negative) enchantment

Parameters:
malignChance - chance the item will have a maligned enchantment

getBaseLevel

public int getBaseLevel()
Returns the base level of nonmagical weapon to start with, before adding enchantments.

Returns:
the weapon base level

setBaseLevel

public void setBaseLevel(int base)
Sets the base level to use for the weapon.

Parameters:
base - the level (1-8) of nonmagical item to enchant, to reach the desired level.

isMaligned

public boolean isMaligned()
Returns true if the weapon we're generating is maligned (i.e. the enchantments are reversed, and have negative values.)


getTotalEnchantment

public int getTotalEnchantment()
Returns the total enchantment (the sum of up to 2 individual enchantments) we decided to place on the weapon.


getWeapon

public GameObject getWeapon()
Returns the weapon to add enchantments to

Returns:
the weapon to add enchantments to

setWeapon

public void setWeapon(GameObject weapon)
Sets the weapon to add enchantments to

Parameters:
weapon - the weapon to add enchantments to

reset

public void reset()
Resets the WeaponEnchanter properties to their default values. Called automatically at the beginning of generate().


parseEnchantment

public java.util.List parseEnchantment(java.lang.String input)
Take a string like "of agility +5, damage +2" and parses it into a java.util.List of WeaponEnchantments.


generate

public static GameObject generate(int level,
                                  java.lang.String name,
                                  int value)
Generates a random weapon using the specified single enchantment

Parameters:
level - the level of item to generate
name - the enchantment name, such as "accuracy"
value - the enchantment value, from -10 to +10
Returns:
a random weapon using the specified enchantment
Throws:
java.lang.IllegalArgumentException - if name or value couldn't be used or parsed

generate

public GameObject generate()
Generates a random weapon. You should set the curseChance and other properties before calling this method.

Returns:
a randomly-generated weapon, or possibly null if something went wrong.

addRandomEnchantment

public java.lang.String addRandomEnchantment(GameObject weapon,
                                             int value,
                                             java.lang.String exclude)
Adds a random enchantment to the passed weapon of the passed value.

Parameters:
weapon - the weapon
value - the value of the enchantment (1-10)
exclude - enchantment not to use (meaning it has already been placed on the item, so exclude it from the possible list) (i.e. "accuracy", "damage", "durability", "weight", "speed", or "light") Pass "" (empty string) for no exclusions. I know this API sucks - sorry 'bout that.

chooseWeightedValue

protected int chooseWeightedValue(int base)
Uses a normal distribution to choose a value from 1 to n, on average closer to n than 1. We do this, rather than just choosing a linear random value from 1 to n, to cut down on the number of very large enchantments that get generated.


addAccuracy

protected void addAccuracy(GameObject weapon,
                           int value)
Adds an enchantment of accuracy to the weapon.


addDurability

protected void addDurability(GameObject weapon,
                             int value)
Adds an enchantment of durability to the weapon.


addSpeed

protected void addSpeed(GameObject weapon,
                        int value)
Adds an enchantment of speed to the weapon.


addDamage

protected void addDamage(GameObject weapon,
                         int value)
Adds an enchantment of damage to the weapon.


getMaxWC

protected java.lang.String getMaxWC(GameObject weapon)
Returns the highest weapon class of this weapon.

Returns:
a string like "wc-cut"

addLight

protected void addLight(GameObject weapon,
                        int value)
Adds an enchantment of light to the weapon.


addWeight

protected void addWeight(GameObject weapon,
                         int value)
Adds an enchantment of weight to the weapon.


instantiate

protected GameObject instantiate(Archetype arch)
Instantiates an archetype, catching any exception.

Parameters:
arch - the archetype to instantiate
Returns:
an item of that archetype, or null if it couldn't be instantiated

curseWeapon

public void curseWeapon()
Curses (with getDamnationChance() of damning) an item.


createNonMagicWeapon

protected void createNonMagicWeapon()
Picks a random base weapon level and instantiates a random nonmagic weapon of that level. This is the weapon we'll then enchant to get it to the desired level.


createBaseWeapon

protected GameObject createBaseWeapon(int level)
Creates a random unenchanted weapon of the passed level.

Returns:
the weapon, or null if anything went wrong.

chooseBaseLevel

protected final int chooseBaseLevel()
Chooses the base (i.e. unenchanted) weapon level to use for the item. Will be somewhere between 1 and level-1, with a cap at the maximum base weapon level. The level of the weapon must have been set via setLevel().

Returns:
a random base level to use.

createLevel1Weapon

public GameObject createLevel1Weapon()
Returns a random level-1 weapon. Level-1 weapons are never enchanted.

Returns:
a level-1 weapon, or null if none could be generated

determineEnchantments

protected void determineEnchantments()
Sets the total enchantment and the individual enchantment(s), randomly, based on the item level and item base. Weapon will have either 1 or 2 enchantments.


sortEnchantments

protected void sortEnchantments()
If 2nd enchantment is greater, swaps them (easier to read).


curseEnchantments

protected void curseEnchantments()
Reverses enchantment number(s), and marks the weapon as "is-cursed", for the Appraisal skill. Precondition: enchantment(s) must have been chosen already.


addEnchantments

protected void addEnchantments()
Adds 1 or 2 enchantments, depending on the results of the call to determineEnchantments().


blessOrCurseItem

protected void blessOrCurseItem()
Decides whether the item should be blessed or cursed.


addQuantityIfNeeded

protected void addQuantityIfNeeded()
If the generated weapon is a missile, or has a "gen-number" property, adds a quantity to the weapon. Throwing stars and darts are both hurled weapons (non-missiles), but they ask for quantities to be set.


flagEnchantmentForAppraisers

protected void flagEnchantmentForAppraisers()
Sets the total enchantment in a "base-enchantment" property for the Appraisal skill.


setWieldLevel

protected void setWieldLevel()
Sets an appropriate "wield-level" on the weapon, based on its final enchantment(s), so that newbies can't wield it to major advantage.


main

public static void main(java.lang.String[] args)
Tests the enchanter functions.