#include "inc_item_tools"

// Minor Magic Weapon Table
object Table810a(object oWeapon =OBJECT_INVALID,int iEnc = 0);
// Medium Magic Weapon Table
object Table810b(object oWeapon =OBJECT_INVALID,int iEnc = 0);
// Major Magic Weapon Table
object Table810c(object oWeapon =OBJECT_INVALID,int iEnc = 0);
// Generates a random weapon type.
// See Table 8-11, 8-12,8-13,8-14 on page 184 of DMG
// if iType = 0 a random category will be determined.
// if iType = 1 a common weapon type will be returned.
// if iType = 2 a uncommon weapon type will be returned.
// if iType = 3 a ranged weaponwill be returned.
// Note this does not return a complete resref.
object Table811_14(int iType = 0);

// Melee Weapon Special Properties Table
int Table815a(object oWeapon, int iEnc);
// Melee Weapon Special Properties Table
int Table815b(object oWeapon, int iEnc);
// Melee Weapon Special Properties Table
int Table815c(object oWeapon, int iEnc);

// Ranged Weapon Special Properties Table
int Table816a(object oWeapon, int iEnc);
// Ranged Weapon Special Properties Table
int Table816b(object oWeapon, int iEnc);
// Ranged Weapon Special Properties Table
int Table816c(object oWeapon, int iEnc);

object Table817b();
object Table817c();


object Table811_14(int iType)  // Table 8 -11 thru 8 -14
    {
    object oItem;
    string sString;
    int iAmount = 1;
    int iRoll = d100();
    if (!iType)
        {
        if (iRoll <= 70) iType = 1;
        else if (iRoll <= 80) iType = 2;
        else iType = 3;
        iRoll = d100();
        }
    switch (iType)
        {
        case 1:// Table 8 -12
            {
            if (iRoll <= 4) sString = "nw_wswdg001";
            else if (iRoll <= 14) sString = "nw_waxgr001";
            else if (iRoll <= 24) sString = "nw_wswgs001";
            else if (iRoll <= 28) sString = "nw_wspka001";
            else if (iRoll <= 41) sString = "nw_wswls001";
            else if (iRoll <= 45) sString = "nw_wblml001";
            else if (iRoll <= 50) sString = "nw_wblml001";
            else if (iRoll <= 54) sString = "nw_waxgr001"; // changed from nunchaku
            else if (iRoll <= 57) sString = "nw_wdbqs001";
            else if (iRoll <= 61) sString = "nw_wswrp001";
            else if (iRoll <= 66) sString = "nw_wswsc001";
            else if (iRoll <= 70) sString = "nw_wplss001";
            else if (iRoll <= 74) sString = "nw_waxbt001"; // changed from siangham
            else if (iRoll <= 84) sString = "nw_wswbs001";
            else if (iRoll <= 89) sString = "nw_wswss001";
            else sString = "dwaxe001";
            break;
            }
        case 2:// Table 8 -13
            {
            if (iRoll <= 3) sString = "nw_wdbax001";
            else if (iRoll <= 7) sString = "nw_waxbt001";
            else if (iRoll <= 10) sString = "nw_wblms001";
            else if (iRoll <= 12) sString = "nw_wblcl001";
            else if (iRoll <= 16) sString = "nw_wbwxl001";
            else if (iRoll <= 19) sString = "nw_wbwxh001";
            else if (iRoll <= 21) sString = "nw_wswdg001";
            else if (iRoll <= 23) sString = "nw_wswls001";
            else if (iRoll <= 24) sString = "nw_wdbma001";
            else if (iRoll <= 31) sString = "nw_wblfh001";
            else if (iRoll <= 35) sString = "nw_wblfl001";
            else if (iRoll <= 37) sString = "nw_wswdg001";
            else if (iRoll <= 39) sString = "nw_wswdg001";
            else if (iRoll <= 41) sString = "nw_wplhb001";
            else if (iRoll <= 43) sString = "nw_wblcl001";
            else if (iRoll <= 45) sString = "nw_wplhb001";
            else if (iRoll <= 48) sString = "nw_wplhb001";
            else if (iRoll <= 51) sString = "nw_wplss001";
            else if (iRoll <= 54) sString = "nw_wblhw001";
            else if (iRoll <= 56) sString = "nw_wblhl001";
            else if (iRoll <= 58) sString = "nw_waxhn001";
            else if (iRoll <= 61) sString = "nw_wspku001";
            else if (iRoll <= 63) sString = "nw_wplss001";
            else if (iRoll <= 65) sString = "nw_wplss001";
            else if (iRoll <= 67) sString = "nw_wplss001";
            else if (iRoll <= 70) sString = "nw_wblms001";
            else if (iRoll <= 72) sString = "nw_wblms001";
            else if (iRoll <= 74) sString = "nw_waxbt001";
            else if (iRoll <= 76) sString = "nw_waxbt001";
            else if (iRoll <= 78) sString = "nw_wplhb001";
            else if (iRoll <= 80) sString = "nw_wblcl001";
            else if (iRoll <= 82) sString = "nw_wplsc001";
            else if (iRoll <= 84) sString = "nw_wthsh001";
            else if (iRoll <= 86) sString = "nw_wspsc001";
            else if (iRoll <= 87) sString = "nw_wdbsw001";
            else if (iRoll <= 91) sString = "nw_wplss001";
            else if (iRoll <= 94) sString = "nw_wdbma001";
            else if (iRoll <= 97) sString = "nw_wblhw001";
            else sString = "x2_it_wpwhip";
            break;
            }
        case 3:// Table 8 -14
            {
            if (iRoll <= 10)
                {
                iRoll = d100();
                if (iRoll <= 50) sString = "nw_wamar001";
                else if (iRoll <= 80) sString = "nw_wambo001";
                else sString = "nw_wambu001";
                iAmount = (Random(100)+1)/2;
                if (iAmount > 99) iAmount = 99;
                if (iAmount < 1) iAmount = 1;
                }
            else if (iRoll <= 15) sString = "nw_wthax001";
            else if (iRoll <= 25) sString = "nw_wbwxh001";
            else if (iRoll <= 35) sString = "nw_wbwxl001";
            else if (iRoll <= 39) sString = "nw_wthdt001";
            else if (iRoll <= 41) sString = "nw_wthdt001";
            else if (iRoll <= 46) sString = "nw_wbwsh001";
            else if (iRoll <= 51) sString = "nw_wbwsh001";
            else if (iRoll <= 56) sString = "nw_wbwsh001";
            else if (iRoll <= 61) sString = "nw_wbwsh001";
            else if (iRoll <= 65) sString = "nw_wbwsl001";
            else if (iRoll <= 75) sString = "nw_wbwln001";
            else if (iRoll <= 80) sString = "nw_wbwln001";
            else if (iRoll <= 85) sString = "nw_wbwln001";
            else if (iRoll <= 90) sString = "nw_wbwln001";
            else if (iRoll <= 95) sString = "nw_wbwln001";
            else sString = "nw_wbwln001";
            break;
            }
        };
    oItem = CreateItemOnObject(sString,OBJECT_SELF,iAmount);
    SetLocalInt(oItem,"BaseValue",GetGoldPieceValue(oItem));
    return oItem;
    }

object Table810a(object oWeapon,int iEnc)
    {
    int iRoll = d100();
    if (!GetIsObjectValid(oWeapon))
        oWeapon = Table811_14();
    SetLocalInt(oWeapon,"MW",TRUE);
    itemproperty ipItem;
    if (GetWeaponRanged(oWeapon))
        {
        if (iRoll <= 70) {AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyAttackBonus(1),oWeapon);iEnc += 1;}
        else if (iRoll <= 85) {AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyAttackBonus(2),oWeapon);iEnc += 2;}
        else
            {
            iEnc = Table815a(oWeapon,iEnc); //Special Powers
            oWeapon = Table810a(oWeapon,iEnc); //Reroll for more powers.
            }
        }
    else
        {
        if (iRoll <= 70) {AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyEnhancementBonus(1),oWeapon);iEnc += 1;}
        else if (iRoll <= 85) {AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyEnhancementBonus(2),oWeapon);iEnc += 2;}
        else
            {
            iEnc = Table815a(oWeapon,iEnc); //Special Powers
            oWeapon = Table810a(oWeapon,iEnc); //Reroll for more powers.
            }
        }
    SetLocalInt(oWeapon,"Enc",iEnc);
    return oWeapon;
    }

object Table810b(object oWeapon, int iEnc)
    {
    int iRoll = d100();
    if (!GetIsObjectValid(oWeapon))
        oWeapon = Table811_14();
    SetLocalInt(oWeapon,"MW",TRUE);
    if (GetWeaponRanged(oWeapon))
        {
        if (iRoll <= 10){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyAttackBonus(1),oWeapon);iEnc += 1;}
        else if (iRoll <= 20){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyAttackBonus(2),oWeapon);iEnc += 2;}
        else if (iRoll <= 58){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyAttackBonus(3),oWeapon);iEnc += 3;}
        else if (iRoll <= 62){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyAttackBonus(4),oWeapon);iEnc += 4;}
        else if (iRoll <= 68) //Specific Weapon
            {
            DestroyObject(oWeapon);
            oWeapon = Table817b();
            iEnc = 0;
            SetLocalInt(oWeapon,"MW",TRUE);
            }
        else
            {
            iEnc = Table815b(oWeapon,iEnc); //Special Powers
            oWeapon = Table810b(oWeapon,iEnc); //Reroll for more powers.
            }
        }
    else
        {
        if (iRoll <= 10){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyEnhancementBonus(1),oWeapon);iEnc += 1;}
        else if (iRoll <= 20){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyEnhancementBonus(2),oWeapon);iEnc += 2;}
        else if (iRoll <= 58){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyEnhancementBonus(3),oWeapon);iEnc += 3;}
        else if (iRoll <= 62){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyEnhancementBonus(4),oWeapon);iEnc += 4;}
        else if (iRoll <= 68) //Specific Weapon
            {
            DestroyObject(oWeapon);
            oWeapon = Table817b();
            iEnc = 0;
            SetLocalInt(oWeapon,"MW",TRUE);
            }
        else
            {
            iEnc = Table815b(oWeapon,iEnc); //Special Powers
            oWeapon = Table810b(oWeapon,iEnc); //Reroll for more powers.
            }
        }
    SetLocalInt(oWeapon,"Enc",iEnc);
    return oWeapon;
    }

object Table810c(object oWeapon, int iEnc)
    {
    int iTemp;
    int iRoll = d100();
    if (!GetIsObjectValid(oWeapon))
        oWeapon = Table811_14();
    SetLocalInt(oWeapon,"MW",TRUE);
    if (GetWeaponRanged(oWeapon))
        {
        if (iRoll <= 20){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyAttackBonus(3),oWeapon);iEnc += 3;}
        else if (iRoll <= 38){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyAttackBonus(4),oWeapon);iEnc += 4;}
        else if (iRoll <= 49){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyAttackBonus(5),oWeapon);iEnc += 5;}
        else if (iRoll <= 63) //Specfic Weapon
            {
            DestroyObject(oWeapon);
            oWeapon = Table817c();
            iEnc = 0;
            SetLocalInt(oWeapon,"MW",TRUE);
            }
        else
            {
            iEnc = Table815c(oWeapon,iEnc); //Special Powers
            oWeapon = Table810c(oWeapon,iEnc); //Reroll for more powers.
            }
        }
    else
        {
        if (iRoll <= 20){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyEnhancementBonus(3),oWeapon);iEnc += 3;}
        else if (iRoll <= 38){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyEnhancementBonus(4),oWeapon);iEnc += 4;}
        else if (iRoll <= 49){AddItemProperty(DURATION_TYPE_PERMANENT,ItemPropertyEnhancementBonus(5),oWeapon);iEnc += 5;}
        else if (iRoll <= 63) //Specfic Weapon
            {
            DestroyObject(oWeapon);
            oWeapon = Table817c();
            iEnc = 0;
            SetLocalInt(oWeapon,"MW",TRUE);
            }
        else
            {
            iEnc = Table815c(oWeapon,iEnc); //Special Powers
            oWeapon = Table810c(oWeapon,iEnc); //Reroll for more powers.
            }
        }
    SetLocalInt(oWeapon,"Enc",iEnc);
    return oWeapon;
    }

int Table815a(object oWeapon,int iEnc)
    {
    int iTemp;
    int iRoll = d100();
    if (iRoll <= 15) // Defending Weapon
        {
        iEnc += 1;
        iTemp = GetEnchantmentBonus(oWeapon);
        if (iTemp < 1) iTemp = 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyACBonus(iTemp));
        }
    else if (iRoll <= 25) // Flaming Weapon
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 35)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 45)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 55)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_SONIC,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 70)
        {
        if (GetWeaponSlashing(oWeapon))
            {
            iEnc += 1;
            IPSafeAddItemProperty(oWeapon,ItemPropertyKeen());
            }
        else
            return Table815a(oWeapon,iEnc);
        }
    else if (iRoll <= 80)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyBonusFeat(IP_CONST_FEAT_CLEAVE));
        }
    else if (iRoll <= 89) // Unable to add spell storing so did acid.
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ACID,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 99) // Unable to add throwing so did a little damage reduction.
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageReduction(IP_CONST_DAMAGEREDUCTION_1,IP_CONST_DAMAGESOAK_5_HP));
        }
    else//Roll twice
        {
        iEnc = Table815a(oWeapon,iEnc);
        iEnc = Table815a(oWeapon,iEnc);
        }
    return iEnc;
    }

int Table815b(object oWeapon, int iEnc)
    {
    int iRoll = d100();
    int iTemp;
    if (iRoll <= 10) // Defending Weapon
        {
        iEnc += 1;
        iTemp = GetEnchantmentBonus(oWeapon);
        if (iTemp < 1) iTemp = 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyACBonus(iTemp));
        }
    else if (iRoll <= 15) // Flaming Weapon
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 20)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 25)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 30)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_SONIC,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 40)
        {
        if (!IPGetIsBludgeoningWeapon(oWeapon))
            {
            iEnc += 1;
            IPSafeAddItemProperty(oWeapon,ItemPropertyKeen());
            }
        else
            return Table815b(oWeapon,iEnc);
        }
    else if (iRoll <= 50)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyBonusFeat(IP_CONST_FEAT_CLEAVE));
        }
    else if (iRoll <= 51) // Unable to add spell storing so did acid.
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ACID,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 56) // Unable to add throwing so did a little damage reduction.
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageReduction(IP_CONST_DAMAGEREDUCTION_1,IP_CONST_DAMAGESOAK_5_HP));
        }
    else if (iRoll <= 59)
        {
        iEnc +=2;
        iTemp = GetBaneRace();
        IPSafeAddItemProperty(oWeapon,ItemPropertyEnhancementBonusVsRace(iTemp,GetEnchantmentBonus(oWeapon)+2));
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsRace(iTemp,IP_CONST_DAMAGETYPE_MAGICAL,IP_CONST_DAMAGEBONUS_2d6));
        }
    else if (iRoll <= 62)
        {
        if (GetWeaponBludgeoning(oWeapon))
            {
            iEnc += 2;
            IPSafeAddItemProperty(oWeapon,ItemPropertyOnHitProps(IP_CONST_ONHIT_SLAYRACE,IP_CONST_ONHIT_SAVEDC_14,IP_CONST_RACIALTYPE_UNDEAD));
            }
        else
            iEnc = Table815b(oWeapon,iEnc);
        }
    else if (iRoll <= 65)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(IP_CONST_DAMAGEBONUS_1d10));
        }
    else if (iRoll <= 68)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(IP_CONST_DAMAGEBONUS_1d10));
        }
    else if (iRoll <= 71)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(IP_CONST_DAMAGEBONUS_1d10));
        }
    else if (iRoll <= 76)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_SONIC,IP_CONST_DAMAGEBONUS_1d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(IP_CONST_DAMAGEBONUS_1d10));
        }
    else if (iRoll <= 79)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyOnHitProps(IP_CONST_ONHIT_WOUNDING,IP_CONST_ONHIT_SAVEDC_20));
        }
    else if (iRoll <= 82)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_EVIL,IP_CONST_DAMAGETYPE_DIVINE,IP_CONST_DAMAGEBONUS_2d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_GOOD));
        }
    else if (iRoll <= 85)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_GOOD,IP_CONST_DAMAGETYPE_DIVINE,IP_CONST_DAMAGEBONUS_2d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_EVIL));
        }
    else if (iRoll <= 88)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC,IP_CONST_DAMAGETYPE_DIVINE,IP_CONST_DAMAGEBONUS_2d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL));
        }
    else if (iRoll <= 91)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL,IP_CONST_DAMAGETYPE_DIVINE,IP_CONST_DAMAGEBONUS_2d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC));
        }
    else if (iRoll <= 92)
        {
        iEnc += 4;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_MAGICAL,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 93)
        {
        iEnc += 4;
        IPSafeAddItemProperty(oWeapon,ItemPropertyOnHitCastSpell(IP_CONST_ONHIT_CASTSPELL_SLOW,IP_CONST_SPELLLEVEL_5));
        }
    else if (iRoll <= 95)
        {
        iEnc += 4;
        IPSafeAddItemProperty(oWeapon,ItemPropertyHaste());
        }
    else//Roll twice
        {
        iEnc = Table815b(oWeapon,iEnc);
        iEnc = Table815b(oWeapon,iEnc);
        }
    return iEnc;
    }

int Table815c(object oWeapon, int iEnc)
    {
    int iRoll = d100();
    int iTemp;
    if (iRoll <= 3) // Flaming Weapon
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 6)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 9)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 12)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_SONIC,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 17)
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyBonusFeat(IP_CONST_FEAT_CLEAVE));
        }
    else if (iRoll <= 19) // Unable to add spell storing so did acid.
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ACID,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 21) // Unable to add throwing so did a little damage reduction.
        {
        iEnc += 1;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageReduction(IP_CONST_DAMAGEREDUCTION_1,IP_CONST_DAMAGESOAK_5_HP));
        }
    else if (iRoll <= 26)
        {
        iEnc +=2;
        iTemp = GetBaneRace();
        IPSafeAddItemProperty(oWeapon,ItemPropertyEnhancementBonusVsRace(iTemp,GetEnchantmentBonus(oWeapon)+2));
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsRace(iTemp,IP_CONST_DAMAGETYPE_MAGICAL,IP_CONST_DAMAGEBONUS_2d6));
        }
    else if (iRoll <= 29)
        {
        if (GetWeaponBludgeoning(oWeapon))
            {
            iEnc += 2;
            IPSafeAddItemProperty(oWeapon,ItemPropertyOnHitProps(IP_CONST_ONHIT_SLAYRACE,IP_CONST_ONHIT_SAVEDC_14,IP_CONST_RACIALTYPE_UNDEAD));
            }
        else
            iEnc = Table815b(oWeapon,iEnc);
        }
    else if (iRoll <= 33)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(IP_CONST_DAMAGEBONUS_1d10));
        }
    else if (iRoll <= 37)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(IP_CONST_DAMAGEBONUS_1d10));
        }
    else if (iRoll <= 41)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(IP_CONST_DAMAGEBONUS_1d10));
        }
    else if (iRoll <= 44)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_SONIC,IP_CONST_DAMAGEBONUS_1d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(IP_CONST_DAMAGEBONUS_1d10));
        }
    else if (iRoll <= 47)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyOnHitProps(IP_CONST_ONHIT_WOUNDING,IP_CONST_ONHIT_SAVEDC_20));
        }
    else if (iRoll <= 52)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_EVIL,IP_CONST_DAMAGETYPE_DIVINE,IP_CONST_DAMAGEBONUS_2d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_GOOD));
        }
    else if (iRoll <= 57)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_GOOD,IP_CONST_DAMAGETYPE_DIVINE,IP_CONST_DAMAGEBONUS_2d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_EVIL));
        }
    else if (iRoll <= 62)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC,IP_CONST_DAMAGETYPE_DIVINE,IP_CONST_DAMAGEBONUS_2d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL));
        }
    else if (iRoll <= 67)
        {
        iEnc += 2;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL,IP_CONST_DAMAGETYPE_DIVINE,IP_CONST_DAMAGEBONUS_2d6));
        IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC));
        }
    else if (iRoll <= 71)
        {
        iEnc += 4;
        IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_MAGICAL,IP_CONST_DAMAGEBONUS_1d6));
        }
    else if (iRoll <= 73)
        {
        iEnc += 4;
        IPSafeAddItemProperty(oWeapon,ItemPropertyOnHitCastSpell(IP_CONST_ONHIT_CASTSPELL_SLOW,IP_CONST_SPELLLEVEL_5));
        }
    else if (iRoll <= 76)
        {
        iEnc += 4;
        IPSafeAddItemProperty(oWeapon,ItemPropertyHaste());
        }
    else if (iRoll <= 80)
        {
        if (GetWeaponSlashing(oWeapon))
            {
            iEnc += 5;
            IPSafeAddItemProperty(oWeapon,ItemPropertyOnHitProps(IP_CONST_ONHIT_VORPAL,20));
            }
        else
            iEnc = Table815c(oWeapon,iEnc);
        }
    else//Roll twice
        {
        iEnc = Table815c(oWeapon,iEnc);
        iEnc = Table815c(oWeapon,iEnc);
        }
    return iEnc;
    }

int Table816a(object oWeapon, int iEnc)
    {
    int iAmmo = GetIsAmmo(oWeapon);
    int iRoll = d100();
    if (iRoll <= 20)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_SONIC,IP_CONST_DAMAGEBONUS_1d6));
        else
            IPSafeAddItemProperty(oWeapon,ItemPropertyUnlimitedAmmo(IP_CONST_UNLIMITEDAMMO_BASIC));
        }
    else if (iRoll <= 40)
        {
        iEnc += 1;
        if (!iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ACID,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_ACID);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else if (iRoll <= 60)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_FIRE);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else if (iRoll <= 80)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_ELECTRICAL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_COLD);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    return iEnc;
    }

int Table816b(object oWeapon, int iEnc)
    {
    int iTemp;
    int iAmmo = GetBaseItemType(oWeapon);
    if (iAmmo == BASE_ITEM_ARROW
     || iAmmo == BASE_ITEM_BULLET
     || iAmmo == BASE_ITEM_BOLT)
        iAmmo = TRUE;
    else
        iAmmo = FALSE;
    int iRoll = d100();
    if (iRoll <= 15)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_SONIC,IP_CONST_DAMAGEBONUS_1d6));
        else
            IPSafeAddItemProperty(oWeapon,ItemPropertyUnlimitedAmmo(IP_CONST_UNLIMITEDAMMO_BASIC));
        }
    else if (iRoll <= 30)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ACID,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_ACID);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else if (iRoll <= 35)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_FIRE);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else if (iRoll <= 40)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_ELECTRICAL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else if (iRoll <= 45)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_COLD);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else if (iRoll <= 50)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(DAMAGE_BONUS_2d10));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_FIRE);
            IPSafeAddItemProperty(oWeapon,ItemPropertyUnlimitedAmmo(IP_CONST_UNLIMITEDAMMO_1D6FIRE));
            }
        }
    else if (iRoll <= 55)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(DAMAGE_BONUS_2d10));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_COLD);
            IPSafeAddItemProperty(oWeapon,ItemPropertyUnlimitedAmmo(IP_CONST_UNLIMITEDAMMO_1D6COLD));
            }
        }
    else if (iRoll <= 60)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(DAMAGE_BONUS_2d10));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_COLD);
            IPSafeAddItemProperty(oWeapon,ItemPropertyUnlimitedAmmo(IP_CONST_UNLIMITEDAMMO_1D6LIGHT));
            }
        }
    else if (iRoll <= 66)
        {
        iEnc += 2;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsRace(GetBaneRace(),IP_CONST_DAMAGETYPE_MAGICAL,DAMAGE_BONUS_2d6));
        else
            {
            iTemp = GetBaneRace();
            SetLocalInt(oWeapon,"Bane",iTemp);
            SetLocalInt(oWeapon,"SpecialAmmo",3);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_MAGICAL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyAttackBonusVsRace(iTemp,GetEnchantmentBonus(oWeapon)+2));
            }
        }
    else if (iRoll <= 74)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_EVIL,IP_CONST_DAMAGETYPE_DIVINE,DAMAGE_BONUS_2d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_GOOD));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",2);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_DIVINE);
            SetLocalInt(oWeapon,"AlignmentTarget",IP_CONST_ALIGNMENTGROUP_EVIL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_GOOD));
            }
        }
    else if (iRoll <= 82)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_GOOD,IP_CONST_DAMAGETYPE_DIVINE,DAMAGE_BONUS_2d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_EVIL));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",2);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_DIVINE);
            SetLocalInt(oWeapon,"AlignmentTarget",IP_CONST_ALIGNMENTGROUP_GOOD);
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_EVIL));
            }
        }
    else if (iRoll <= 90)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC,IP_CONST_DAMAGETYPE_DIVINE,DAMAGE_BONUS_2d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",2);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_DIVINE);
            SetLocalInt(oWeapon,"AlignmentTarget",IP_CONST_ALIGNMENTGROUP_CHAOTIC);
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL));
            }
        }
    else if (iRoll <= 98)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL,IP_CONST_DAMAGETYPE_DIVINE,DAMAGE_BONUS_2d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",2);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_DIVINE);
            SetLocalInt(oWeapon,"AlignmentTarget",IP_CONST_ALIGNMENTGROUP_LAWFUL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC));
            }
        }
    else // Roll again twice
        {
        iEnc = Table816b(oWeapon, iEnc);
        iEnc = Table816b(oWeapon, iEnc);
        }
    return iEnc;
    }

int Table816c(object oWeapon, int iEnc)
    {
    int iTemp;
    int iAmmo = GetBaseItemType(oWeapon);
    if (iAmmo == BASE_ITEM_ARROW
     || iAmmo == BASE_ITEM_BULLET
     || iAmmo == BASE_ITEM_BOLT)
        iAmmo = TRUE;
    else
        iAmmo = FALSE;
    int iRoll = d100();
    if (iRoll <= 10)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_FIRE);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else if (iRoll <= 20)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_ELECTRICAL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else if (iRoll <= 30)
        {
        iEnc += 1;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_COLD);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else if (iRoll <= 40)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE,IP_CONST_DAMAGEBONUS_1d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(DAMAGE_BONUS_2d10));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_FIRE);
            IPSafeAddItemProperty(oWeapon,ItemPropertyUnlimitedAmmo(IP_CONST_UNLIMITEDAMMO_1D6FIRE));
            }
        }
    else if (iRoll <= 50)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_COLD,IP_CONST_DAMAGEBONUS_1d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(DAMAGE_BONUS_2d10));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_COLD);
            IPSafeAddItemProperty(oWeapon,ItemPropertyUnlimitedAmmo(IP_CONST_UNLIMITEDAMMO_1D6COLD));
            }
        }
    else if (iRoll <= 60)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_ELECTRICAL,IP_CONST_DAMAGEBONUS_1d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyMassiveCritical(DAMAGE_BONUS_2d10));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_COLD);
            IPSafeAddItemProperty(oWeapon,ItemPropertyUnlimitedAmmo(IP_CONST_UNLIMITEDAMMO_1D6LIGHT));
            }
        }
    else if (iRoll <= 65)
        {
        iEnc += 2;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsRace(GetBaneRace(),IP_CONST_DAMAGETYPE_MAGICAL,DAMAGE_BONUS_2d6));
        else
            {
            iTemp = GetBaneRace();
            SetLocalInt(oWeapon,"Bane",iTemp);
            SetLocalInt(oWeapon,"SpecialAmmo",3);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_MAGICAL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyAttackBonusVsRace(iTemp,GetEnchantmentBonus(oWeapon)+2));
            }
        }
    else if (iRoll <= 70)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_EVIL,IP_CONST_DAMAGETYPE_DIVINE,DAMAGE_BONUS_2d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_GOOD));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",2);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_DIVINE);
            SetLocalInt(oWeapon,"AlignmentTarget",IP_CONST_ALIGNMENTGROUP_EVIL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_GOOD));
            }
        }
    else if (iRoll <= 75)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_GOOD,IP_CONST_DAMAGETYPE_DIVINE,DAMAGE_BONUS_2d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_EVIL));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",2);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_DIVINE);
            SetLocalInt(oWeapon,"AlignmentTarget",IP_CONST_ALIGNMENTGROUP_GOOD);
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_EVIL));
            }
        }
    else if (iRoll <= 80)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC,IP_CONST_DAMAGETYPE_DIVINE,DAMAGE_BONUS_2d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",2);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_DIVINE);
            SetLocalInt(oWeapon,"AlignmentTarget",IP_CONST_ALIGNMENTGROUP_CHAOTIC);
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL));
            }
        }
    else if (iRoll <= 85)
        {
        iEnc += 2;
        if (iAmmo)
            {
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonusVsAlign(IP_CONST_ALIGNMENTGROUP_LAWFUL,IP_CONST_DAMAGETYPE_DIVINE,DAMAGE_BONUS_2d6));
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC));
            }
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",2);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_DIVINE);
            SetLocalInt(oWeapon,"AlignmentTarget",IP_CONST_ALIGNMENTGROUP_LAWFUL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyLimitUseByAlign(IP_CONST_ALIGNMENTGROUP_CHAOTIC));
            }
        }
    else if (iRoll <= 90)
        {
        if (iAmmo)
            {
            iEnc = Table816c(oWeapon, iEnc); //Reroll
            }
        else
            {
            iEnc += 4;
            IPSafeAddItemProperty(oWeapon,ItemPropertyHaste());
            }
        }
    else if (iRoll <= 97)
        {
        iEnc += 5;
        if (iAmmo)
            IPSafeAddItemProperty(oWeapon,ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_MAGICAL,IP_CONST_DAMAGEBONUS_1d6));
        else
            {
            SetLocalInt(oWeapon,"SpecialAmmo",TRUE);
            SetLocalInt(oWeapon,"DamageType",IP_CONST_DAMAGETYPE_MAGICAL);
            IPSafeAddItemProperty(oWeapon,ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER_SELF_ONLY,IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE));
            }
        }
    else // Roll again twice
        {
        iEnc = Table816c(oWeapon, iEnc);
        iEnc = Table816c(oWeapon, iEnc);
        }
    return iEnc;
    }

object Table817b()
    {
    int iRoll = d100();
    string sString;
    if      (iRoll <= 20) sString = "arrow601";
    else if (iRoll <= 40) sString = "bolt601";
    else if (iRoll <= 55) sString = "spear601";
    else if (iRoll <= 65) sString = "arrow602";
    else if (iRoll <= 70) sString = "dagger601";
    else if (iRoll <= 72) sString = "spear602";
    else if (iRoll <= 74) sString = "dagger602";
    else if (iRoll <= 76) sString = "baxe601";
    else if (iRoll <= 79) sString = "spear603";
    else if (iRoll <= 82) sString = "dagger603";
    else if (iRoll <= 85) sString = "ssword601";
    else if (iRoll <= 88) sString = "mace601";
    else if (iRoll <= 91) sString = "lsword601";
    else if (iRoll <= 94) sString = "lbow601";
    else if (iRoll <= 96) sString = "lsword602";
    else if (iRoll <= 98) sString = "lsword603";
    else sString = "gaxe601";
    return CreateItemOnObject(sString);
    }

object Table817c()
    {
    int iRoll = d100();
    string sString;
    if      (iRoll <= 04) sString = "spear601";
    else if (iRoll <= 09) sString = "arrow602";
    else if (iRoll <= 11) sString = "spear602";
    else if (iRoll <= 13) sString = "arrow603";
    else if (iRoll <= 17) sString = "dagger602";
    else if (iRoll <= 20) sString = "baxe601";
    else if (iRoll <= 25) sString = "spear603";
    else if (iRoll <= 30) sString = "dagger603";
    else if (iRoll <= 35) sString = "ssword601";
    else if (iRoll <= 40) sString = "mace601";
    else if (iRoll <= 45) sString = "lsword601";
    else if (iRoll <= 50) sString = "lbow601";
    else if (iRoll <= 55) sString = "lsword602";
    else if (iRoll <= 60) sString = "lsword603";
    else if (iRoll <= 66) sString = "gaxe601";
    else if (iRoll <= 72) sString = "gsword601";
    else if (iRoll <= 78) sString = "rapier601";
    else if (iRoll <= 81) sString = "bsword601";
    else if (iRoll <= 83) sString = "lsword604";
    else if (iRoll <= 85) sString = "scimitar601";
    else if (iRoll <= 87) sString = "whammer601";
    else if (iRoll <= 90) sString = "mace602";
    else if (iRoll <= 96) sString = "lsword605";
    else sString = "ssword602";
    return CreateItemOnObject(sString);
    }
