//////////////////////////////////////////////////////////
//::use: #include"sd_lootsystem"
//::
//::
//::  Slayers of Darkmoon loot generation system
//::
//::
//::
//::  Commche 2006
//::

#include "prc_x2_itemprop"
#include "prc_inc_newip"
//#include "nw_i0_generic"
#include "inc_itm_appear"
#include "inc_epicspells"


//////////////////////////////////////////////////////////
//: Constants
//:
//:note* See line 4408(?) for specific item droprate configuration

const int LUCK_CHANCE = 5000; // 1 in x chance getting a much better item (0 for off)
const int DROP_RATE = 1;      // % modifyer for loot drop (see line 4619 for specifics)
const int CHANCE_WORN = 0;   // % chance of worn item (0 for off)
const int CHANCE_BROKEN = 0;  // % chance of broken item (0 for off)

int iRandomShoulder = Random(24),
	iRandomBicep = Random(15),
	iRandomForearm = Random(23),
	iRandomHand = Random(9),
	iRandomThigh = Random(16),
	iRandomShins = Random(21),
	iRandomFeet = Random(16),
	iRandomRobe = Random(11),
	iRandomPelvis = Random(37),
	iRandomBelt = Random(16),
	iRandomNeck = Random(6),
	iRandomChest = Random(61);


 int iRandomArmor1 = Random(175),
	iRandomArmor2 = Random(175),
	iRandomArmor3 = Random(175),
	iRandomArmor4 = Random(175),
	iRandomArmor5 = Random(175),
	iRandomArmor6 = Random(175);

int iRandomWeapon1 = d4(),
	iRandomWeapon2 = d4(),
	iRandomWeapon3 = d4();

int  iRandomCloak = Random(16),
	 iRandomHelmet = Random(124);

int iRandRing = Random(238),
	iRandBelt = Random(255),
	iRandAmulet = Random(255),
	iRandBracer = Random(178),
	iRandGauntlet = Random(199);


//------------------------------------------------------------------------------
// Returns TRUE if oItem is a bludgeoning weapon
//------------------------------------------------------------------------------
int PRCGetBludgeoningWeapon(object oItem)
{
    int nItem = GetBaseItemType(oItem);
    //exclude creature weapons
    if(nItem == BASE_ITEM_CBLUDGWEAPON)
        return FALSE;
    int nWeapon = (StringToInt(Get2DACache("baseitems", "WeaponType", nItem)));
    // 2 = bludgeoning, 5 = bludgeoning/piercing
    return (nWeapon == 2 || nWeapon == 5);
}


// Generates a random weapon
// =======================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the weapon
// iRange = the quality of the weapon: 1=lowest 5=highest
// SockChance = a % chance for the generated weapon to be socketed
// DamBroke = a switch to disable chance of damaged/broken weapon: 0=on 1=off
void DropWeapon(object oMob, object oSack, int iRange, int SockChance, int DamBroke);

// Generates random chest armor
// =======================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the armor
// iRange = the quality of the armor: 1=lowest 5=highest
// SockChance = a % chance for the generated armor to be socketed
// DamBroke = a switch to disable chance of damaged/broken armor: 0=on 1=off
void DropArmor(object oMob, object oSack, int iRange, int SockChance, int DamBroke);

// Generates a random shield
// =======================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the shield
// iRange = the quality of the shield: 1=lowest 5=highest
// SockChance = a % chance for the generated shield to be socketed
// DamBroke = a switch to disable chance of damaged/broken shield: 0=on 1=off
void DropShield(object oMob, object oSack, int iRange, int SockChance, int DamBroke);

// Generates random monk gloves
// =======================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the gloves
// iRange = the quality of the gloves: 1=lowest 5=highest
// SockChance = a % chance for the generated gloves to be socketed
// DamBroke = a switch to disable chance of damaged/broken gloves: 0=on 1=off
void DropMonkGloves(object oMob, object oSack, int iRange, int SockChance, int DamBroke);

// Generates a random magic item (i.e. boots, helm, amulet, ring, belt, bracer)
// ============================================================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the item
// iRange = the quality of the item: 1=lowest 5=highest
// SockChance = a % chance for the generated item to be socketed
// DamBroke = a switch to disable chance of damaged/broken item: 0=on 1=off
void DropMagicItem(object oMob, object oSack, int iRange, int SockChance, int DamBroke);

// Generates a random socket gem
// =======================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the gem
// iRange = the quality of the gem: 1=lowest 5=highest
void DropGem (object oMob, object oSack, int iRange);

// Generates random ranged ammo (only arrows & bolts)
// ==================================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the ammo
// iRange = the quality of the ammo: 1=lowest 5=highest
void DropAmmo(object oMob, object oSack, int iRange);

// Generates a random scroll
// =======================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the scroll
// iRange = the quality of the scroll: 1=lowest 5=highest
// note* this is based on the gold-value of the scroll
void DropScroll(object oMob, object oSack, int iRange);

// Generates a random potion
// =======================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the potion
void DropPotion(object oMob, object oSack);

// Generates a random misc item (i.e. bag)
// =======================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the misc
void DropMisc(object oMob, object oSack);

// Generates a random rod or wand
// =======================================
// oMob = the creature that just died
// oSack = the object into which you will spawn the rod or wand
void DropRodWand(object oMob, object oSack);

// Generates random gold
// ==================================================
// oMob = the creature that just died
// note* the gold amount will be based on the creature's level using the below formula
// Gold = (d20()*Creature LVL)+(15*Creature LVL)+iBonus
// oSack = the object into which you will spawn the ammo
// iBonus = additional gold to be added to the tally
void DropGold(object oMob, object oSack, int iBonus);

// Drop randomly chosen and generated loot & some gold
// ===================================================
// *This is the main call function of the sd lootsystem
// oMob = the creature that just died (the loot dropped is based on their class & level)
// oSack = the object into which you will spawn the loot
void sd_droploot (object oMob, object oSack);

// Drop randomly chosen class set item piece
// ===================================================
// *you can control what class type of set item will drop
// oMob = the creature that just died (the loot dropped is based on their class & level)
// oSack = the object into which you will spawn the loot
// iClass = the pereferred class of the drop
// eg:
// (1 - Fighter (2 - wiz (3 - sorc (4 - rogue/dex fighter (5 -cleric (6 - bard
// (7 - pally (8 - druid (9 - ranger (10 - monk (11 - barb (12 - weapon sets
// Default is 0: this will instead choose randomly from every item regardless
// of class
void DropSetItem(object oMob, object oSack, int iClass = 0);

// CHEST CLEARER
void ClearChest (object oMob)
{
	 //:: Clears all inventory items from the mob, then destroys the mob object itself.
	 object oItem = GetFirstItemInInventory(oMob);									
										
	 while (GetIsObjectValid(oItem))	
	{									
		DestroyObject(oItem);
		oItem = GetNextItemInInventory(oMob);
	}
}

const string COLORTOKEN ="                  ##################$%&'()*+,-./0123456789:;;==?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[[]^_`abcdefghijklmnopqrstuvwxyz{|}~~��������������������������������������������������������������������������������������������������������������������������������";

string ColorString(string sText, int nRed=255, int nGreen=255, int nBlue=255)
{
    return "<c" + GetSubString(COLORTOKEN, nRed, 1) + GetSubString(COLORTOKEN, nGreen, 1) + GetSubString(COLORTOKEN, nBlue, 1) + ">" + sText + "</c>";
}

void SetThreatLevel(object oMob)
{
	int iHD = GetHitDice(OBJECT_SELF);
	int iRange;
	string sName;
	string cName = GetName(oMob);

//:: Define new ranges for each threat level
	if (iHD > 0  && iHD <= 10)   iRange = 1;  // lvl 1-10
	if (iHD > 10 && iHD <= 20)   iRange = 2;  // lvl 11-20
	if (iHD > 20 && iHD <= 30)   iRange = 3;  // lvl 21-30
	if (iHD > 30 && iHD <= 40)   iRange = 4;  // lvl 31-40
	if (iHD > 40 && iHD <= 50)   iRange = 5;  // lvl 41-50
	if (iHD > 50 && iHD <= 60)   iRange = 6;  // lvl 51-60
	if (iHD > 60 && iHD <= 70)   iRange = 7;  // lvl 61-70
	if (iHD > 70 /* && iHD <= 80 */)   iRange = 8;  // lvl 71-80+

//:: Assign colors for each tier
	switch(iRange)
	{
		case 1: sName = ColorString(cName, 255, 255, 255); break;   // White - Low threat
		case 2: sName = ColorString(cName, 255, 255, 0); break;     // Yellow - Slightly higher threat
		case 3: sName = ColorString(cName, 255, 165, 0); break;     // Orange - Moderate threat
		case 4: sName = ColorString(cName, 255, 100, 0); break;     // Deep Orange - Medium threat
		case 5: sName = ColorString(cName, 255, 69, 69); break;     // Light Red - Higher medium threat
		case 6: sName = ColorString(cName, 255, 0, 255); break;     // Magenta - High threat
		case 7: sName = ColorString(cName, 180, 0, 255); break;     // Purple - Very high threat
		case 8: sName = ColorString(cName, 153, 0, 153); break;     // Bright Purple - Highest regular threat
	}

//:: Boss enemies use red regardless of HD
	if (GetLocalInt(OBJECT_SELF, "BOSS") == 1)
		sName = ColorString(cName, 255, 0, 0);  // Red - Boss

	SetName(oMob, sName);
}

void SetEyes(int iType, object oMob)
{
 effect eVis = EffectVisualEffect(iType);
 ApplyEffectToObject(DURATION_TYPE_PERMANENT, eVis, oMob);
}

void NameSack(object oSack)
{
	string sName = GetName(OBJECT_SELF);
	sName+= " Corpse";
	SetName(oSack, sName);
}

void InvClear (object oMob)
{
	object oItem = GetFirstItemInInventory(oMob);
	
	while (GetIsObjectValid(oItem))
	{
		if (GetPlotFlag(oItem) == FALSE)
		{
			DestroyObject(oItem);
		}
		
		oItem = GetNextItemInInventory(oMob);
	}
}

void LootClear (object oMob)
{
	object oItem = GetFirstItemInInventory(oMob);

	while (GetIsObjectValid(oItem))
	{
        DestroyObject(oItem);
        oItem = GetNextItemInInventory(oMob);
	}
	
	DestroyObject(oMob);
}

void DropAlchemy(object oMob, object oSack, int iRange)
{
	string sAlchemical;
	int nRandom = Random(16) + 1;
	int iRoll;
	
	if (iRange==1)
    {//:: Alchemical items
		iRoll = Random(21);
		switch (iRoll)
		{
			case 0: sAlchemical = "prc_brittlebn";		break;
			case 1: sAlchemical = "x1_wmgrenade001";	break;
			case 2: sAlchemical = "x1_wmgrenade002";	break;
			case 3: sAlchemical = "x1_wmgrenade003";	break;
			case 4: sAlchemical = "x1_wmgrenade004";	break;
			case 5: sAlchemical = "x1_wmgrenade005";	break;
			case 6: sAlchemical = "x1_wmgrenade006";	break;
			case 7: sAlchemical = "x1_wmgrenade007";	break;
			case 8: sAlchemical = "prc_it_antitox";		break;
			case 9: sAlchemical = "prc_it_blendcrm";	break;
			case 10: sAlchemical = "prc_it_crcklpdr";	break;
			case 11: sAlchemical = "prc_it_emblmfr";    break;
			case 12: sAlchemical = "prc_it_fareyeoil";	break;
			case 13: sAlchemical = "prc_it_healblm";    break;
			case 14: sAlchemical = "prc_it_keenear";    break;
			case 15: sAlchemical = "prc_it_lockslip";	break;
			case 16: sAlchemical = "prc_it_nerv";		break;
			case 17: sAlchemical = "prc_it_scrmflsk";	break;
			case 18: sAlchemical = "prc_it_shedden";    break;
			case 19: sAlchemical = "prc_it_shedden2";	break;
			case 20: sAlchemical = "prc_it_softfoot";	break;
		}
	} 
	if (iRange >= 2)
    {//:: Alchemical Items
		iRoll = Random(27);	
		switch (iRoll)
		{
			case 0: sAlchemical = "x1_wmgrenade006";	break;				
			case 1: sAlchemical = "x2_it_acidbomb";		break;
			case 2: sAlchemical = "x2_it_firebomb";		break;
			case 3: sAlchemical = "prc_it_shedden3";    break;
			case 4: sAlchemical = "prc_it_shedden4";    break;
			case 5: sAlchemical = "prc_it_shedden5";    break;
			case 6: sAlchemical = "nw_it_medkit004";    break;
			case 7: sAlchemical = "x1_wmgrenade007";	break;
			case 8: sAlchemical = "prc_it_antitox";		break;
			case 9: sAlchemical = "prc_it_blendcrm";	break;
			case 10: sAlchemical = "prc_it_crcklpdr";	break;
			case 11: sAlchemical = "prc_it_emblmfr";    break;
			case 12: sAlchemical = "prc_it_fareyeoil";	break;
			case 13: sAlchemical = "prc_it_healblm";    break;
			case 14: sAlchemical = "prc_it_keenear";    break;
			case 15: sAlchemical = "prc_it_lockslip";	break;
			case 16: sAlchemical = "prc_it_nerv";		break;
			case 17: sAlchemical = "prc_it_scrmflsk";	break;
			case 18: sAlchemical = "prc_it_shedden";    break;
			case 19: sAlchemical = "prc_it_shedden2";	break;
			case 20: sAlchemical = "prc_it_softfoot";	break;
			case 21: sAlchemical = "prc_brittlebn";		break;	
			case 22: sAlchemical = "x1_wmgrenade001";	break;
			case 23: sAlchemical = "x1_wmgrenade002";	break;
			case 24: sAlchemical = "x1_wmgrenade003";	break;
			case 25: sAlchemical = "x1_wmgrenade004";	break;
			case 26: sAlchemical = "x1_wmgrenade005";	break;
	
		}
	}	

	object oAlchemy =  CreateItemOnObject(sAlchemical, oSack, 1);
	
	SetIdentified(oAlchemy, FALSE);
}

void DropScroll(object oMob, object oSack, int iRange)
{
	string sType;
	int nRandom = Random(16) + 1;
	int iRoll;
	
	if (iRange==1)	//:: scrolls up to max value of 300
    {
		iRoll = d100();
		switch (iRoll)
		{
			case 1: sType = "x1_it_sparscr002";		break;
			case 2: sType = "nw_it_sparscr107";		break;
			case 3: sType = "x1_it_sparscr102";		break;
			case 4: sType = "x1_it_spdvscr101";		break;
			case 5: sType = "x2_it_spdvscr202";		break;
			case 6: sType = "x2_it_spdvscr103";		break;
			case 7: sType = "x2_it_spdvscr102";		break;
			case 8: sType = "nw_it_sparscr211";		break;
			case 9: sType = "x1_it_spdvscr202";		break;
			case 10: sType = "nw_it_sparscr212";	break;
			case 11: sType = "nw_it_sparscr112";    break;
			case 12: sType = "x1_it_spdvscr107";    break;
			case 13: sType = "nw_it_sparscr213";    break;
			case 14: sType = "x2_it_sparscr207";    break;
			case 15: sType = "nw_it_sparscr107";    break;
			case 16: sType = "nw_it_spdvscr202";    break;
			case 17: sType = "nw_it_sparscr217";    break;
			case 18: sType = "x2_it_sparscr206";    break;
			case 19: sType = "nw_it_sparscr110";    break;
			case 20: sType = "x2_it_sparscr201";    break;
			case 21: sType = "x1_it_spdvscr301";    break;
			case 22: sType = "x2_it_spdvscr104";    break;
			case 23: sType = "x2_it_spdvscr001";    break;
			case 24: sType = "x2_it_spdvscr203";    break;
			case 25: sType = "x2_it_spdvscr308";    break;
			case 26: sType = "nw_it_sparscr206";    break;
			case 27: sType = "nw_it_sparscr003";    break;
			case 28: sType = "x2_it_spdvscr101";    break;
			case 29: sType = "x2_it_sparscr202";    break;
			case 30: sType = "x1_it_spdvscr102";    break;
			case 31: sType = "x2_it_spdvscr105";    break;
			case 32: sType = "nw_it_sparscr219";    break;
			case 33: sType = "x1_it_sparscr003";    break;
			case 34: sType = "nw_it_sparscr215";    break;
			case 35: sType = "nw_it_sparscr101";    break;
			case 36: sType = "x2_it_spdvscr106";    break;
			case 37: sType = "x1_it_spdvscr103";    break;
			case 38: sType = "x1_it_sparscr101";    break;
			case 39: sType = "x1_it_sparscr101";    break;
			case 40: sType = "x2_it_sparscr305";    break;
			case 41: sType = "x1_it_spdvscr205";    break;
			case 42: sType = "x2_it_sparscr205";    break;
			case 43: sType = "x1_it_sparscr001";    break;
			case 44: sType = "nw_it_sparscr220";    break;
			case 45: sType = "x2_it_sparscr203";    break;
			case 46: sType = "nw_it_sparscr208";    break;
			case 47: sType = "nw_it_sparscr209";    break;
			case 48: sType = "nw_it_sparscr103";    break;
			case 49: sType = "x2_it_spdvscr204";    break;
			case 50: sType = "nw_it_sparscr308";    break;
			case 51: sType = "x2_it_sparscr101";    break;
			case 52: sType = "x2_it_sparscr104";    break;
			case 53: sType = "nw_it_sparscr106";    break;
			case 54: sType = "x1_it_spdvscr104";    break;
			case 55: sType = "x1_it_spdvscr001";    break;
			case 56: sType = "x1_it_spdvscr201";    break;
			case 57: sType = "nw_it_sparscr207";    break;
			case 58: sType = "x2_it_sparscr102";    break;
			case 59: sType = "nw_it_sparscr216";    break;
			case 60: sType = "nw_it_sparscr218";    break;
			case 61: sType = "nw_it_spdvscr201";    break;
			case 62: sType = "nw_it_sparscr004";    break;
			case 63: sType = "nw_it_sparscr104";    break;
			case 64: sType = "x1_it_spdvscr106";    break;
			case 65: sType = "nw_it_sparscr109";    break;
			case 66: sType = "x2_it_sparscr105";    break;
			case 67: sType = "nw_it_sparscr202";    break;
			case 68: sType = "nw_it_sparscr113";    break;
			case 69: sType = "x1_it_spdvscr203";    break;
			case 70: sType = "nw_it_sparscr221";    break;
			case 71: sType = "nw_it_sparscr102";    break;
			case 72: sType = "x2_it_sparscral";     break;
			case 73: sType = "nw_it_sparscr111";    break;
			case 74: sType = "nw_it_sparscr002";    break;
			case 75: sType = "x2_it_spdvscr107";    break;
			case 76: sType = "x2_it_spdvscr205";    break;
			case 77: sType = "nw_it_sparscr201";    break;
			case 78: sType = "nw_it_sparscr001";    break;
			case 79: sType = "x2_it_spdvscr108";    break;
			case 80: sType = "nw_it_sparscr210";    break;
			case 81: sType = "x2_it_sparscr103";    break;
			case 82: sType = "x1_it_sparscr103";    break;
			case 83: sType = "x1_it_spdvscr105";    break;
			case 84: sType = "nw_it_spdvscr203";    break;
			case 85: sType = "nw_it_sparscr108";    break;
			case 86: sType = "nw_it_spdvscr204";    break;
			case 87: sType = "x2_it_sparscr204";    break;
			case 88: sType = "nw_it_sparscr105";    break;
			case 89: sType = "nw_it_sparscr203";    break;
			case 90: sType = "x1_it_sparscr202";    break;
			case 91: sType = "x1_it_sparscr104";    break;
			case 92: sType = "nw_it_sparscr214";    break;
			case 93: sType = "x2_it_spdvscr002";    break;
			case 94: sType = "nw_it_sparscr204";    break;
		}
	}
 
	if (iRange==2)
    {
		iRoll = d100();    //:: scrolls valued 540-1621
		switch (iRoll)
		{
			case 1: sType = "nw_it_sparscr509";    break;
			case 2: sType = "x2_it_spdvscr508";    break;
			case 3: sType = "x2_it_sparscr501";    break;
			case 4: sType = "x2_it_spdvscr501";    break;
			case 5: sType = "nw_it_sparscr414";    break;
			case 6: sType = "x1_it_sparscr502";    break;
			case 7: sType = "x2_it_spdvscr307";    break;
			case 8: sType = "nw_it_sparscr405";    break;
			case 9: sType = "x2_it_spdvscr504";    break;
			case 10: sType = "nw_it_sparscr307";    break;
			case 11: sType = "nw_it_sparscr502";    break;
			case 12: sType = "nw_it_sparscr507";    break;
			case 13: sType = "nw_it_sparscr406";    break;
			case 14: sType = "nw_it_sparscr411";    break;
			case 15: sType = "x2_it_spdvscr402";    break;
			case 16: sType = "x2_it_spdvscr305";    break;
			case 17: sType = "x2_it_spdvscr403";    break;
			case 18: sType = "nw_it_sparscr501";    break;
			case 19: sType = "nw_it_sparscr301";    break;
			case 20: sType = "x1_it_sparscr301";    break;
			case 21: sType = "x2_it_spdvscr404";    break;
			case 22: sType = "x2_it_spdvscr309";    break;
			case 23: sType = "nw_it_sparscr416";    break;
			case 24: sType = "nw_it_sparscr503";    break;
			case 25: sType = "nw_it_sparscr608";    break;
			case 26: sType = "nw_it_sparscr418";    break;
			case 27: sType = "x2_it_spdvscr509";    break;
			case 28: sType = "nw_it_sparscr413";    break;
			case 29: sType = "nw_it_sparscr504";    break;
			case 30: sType = "nw_it_sparscr309";    break;
			case 31: sType = "x1_it_sparscr501";    break;
			case 32: sType = "nw_it_sparscr304";    break;
			case 33: sType = "x1_it_spdvscr403";    break;
			case 34: sType = "x2_it_spdvscr405";    break;
			case 35: sType = "x2_it_spdvscr306";    break;
			case 36: sType = "x2_it_sparscr701";    break;
			case 37: sType = "nw_it_sparscr602";    break;
			case 38: sType = "x1_it_spdvscr303";    break;
			case 39: sType = "x2_it_sparscr304";    break;
			case 40: sType = "nw_it_sparscr508";    break;
			case 41: sType = "x1_it_sparscr303";    break;
			case 42: sType = "x2_it_spdvscr406";    break;
			case 43: sType = "nw_it_sparscr312";    break;
			case 44: sType = "x2_it_spdvscr505";    break;
			case 45: sType = "x2_it_spdvscr302";    break;
			case 46: sType = "nw_it_sparscr505";    break;
			case 47: sType = "x2_it_spdvscr401";    break;
			case 48: sType = "nw_it_sparscr408";    break;
			case 49: sType = "x1_it_spdvscr501";    break;
			case 50: sType = "x2_it_spdvscr301";    break;
			case 51: sType = "x1_it_spdvscr401";    break;
			case 52: sType = "x1_it_spdvscr302";    break;
			case 53: sType = "x2_it_spdvscr310";    break;
			case 54: sType = "nw_it_sparscr314";    break;
			case 55: sType = "x1_it_sparscr401";    break;
			case 56: sType = "x2_it_sparscr303";    break;
			case 57: sType = "x2_it_sparscr602";    break;
			case 58: sType = "nw_it_sparscr511";    break;
			case 59: sType = "nw_it_sparscr512";    break;
			case 60: sType = "nw_it_sparscr417";    break;
			case 61: sType = "nw_it_sparscr513";    break;
			case 62: sType = "nw_it_sparscr310";    break;
			case 63: sType = "nw_it_sparscr302";    break;
			case 64: sType = "x2_it_sparscrmc";     break;
			case 65: sType = "x2_it_spdvscr304";    break;
			case 66: sType = "x1_it_spdvscr402";    break;
			case 67: sType = "x2_it_sparscr301";    break;
			case 68: sType = "x2_it_sparscr502";    break;
			case 69: sType = "nw_it_sparscr506";    break;
			case 70: sType = "nw_it_sparscr401";    break;
			case 71: sType = "x2_it_spdvscr502";    break;
			case 72: sType = "nw_it_sparscr315";    break;
			case 73: sType = "x2_it_spdvscr311";    break;
			case 74: sType = "nw_it_spdvscr402";    break;
			case 75: sType = "x1_it_spdvscr502";    break;
			case 76: sType = "nw_it_sparscr409";    break;
			case 77: sType = "x2_it_spdvscr407";    break;
			case 78: sType = "nw_it_sparscr415";    break;
			case 79: sType = "x2_it_spdvscr312";    break;
			case 80: sType = "x1_it_spdvscr305";    break;
			case 81: sType = "nw_it_spdvscr501";    break;
			case 82: sType = "nw_it_spdvscr301";    break;
			case 83: sType = "nw_it_sparscr402";    break;
			case 84: sType = "nw_it_spdvscr401";    break;
			case 85: sType = "x2_it_sparscr302";    break;
			case 86: sType = "nw_it_sparscr410";    break;
			case 87: sType = "x2_it_spdvscr506";    break;
			case 88: sType = "nw_it_sparscr313";    break;
			case 89: sType = "x2_it_spdvscr507";    break;
			case 90: sType = "x1_it_spdvscr304";    break;
			case 91: sType = "nw_it_sparscr305";    break;
			case 92: sType = "nw_it_sparscr403";    break;
			case 93: sType = "nw_it_sparscr306";    break;
			case 94: sType = "nw_it_sparscr404";    break;
			case 95: sType = "nw_it_sparscr510";    break;
			case 96: sType = "x2_it_sparscr902";    break;
			case 97: sType = "nw_it_sparscr606";    break;
			case 98: sType = "x2_it_spdvscr503";    break;
			case 99: sType = "nw_it_sparscr407";    break;
			case 100: sType = "x1_it_sparscr302";   break;
		}
	}
	
	if (iRange==3)
	{
		iRoll = d100();	//:: scrolls valued 2400-3200
		switch (iRoll)
        {
			case 1: sType = "nw_it_sparscr603";    break;
			case 2: sType = "x1_it_spdvscr701";    break;
			case 3: sType = "x1_it_spdvscr601";    break;
			case 4: sType = "x1_it_sparscr602";    break;
			case 5: sType = "x1_it_sparscr701";    break;
			case 6: sType = "x2_it_spdvscr603";    break;
			case 7: sType = "nw_it_sparscr607";    break;
			case 8: sType = "nw_it_sparscr610";    break;
			case 9: sType = "nw_it_sparscr707";    break;
			case 10: sType = "x1_it_spdvscr605";    break;
			case 11: sType = "x1_it_spdvscr702";    break;
			case 12: sType = "x2_it_spdvscr601";    break;
			case 13: sType = "nw_it_sparscr704";    break;
			case 14: sType = "x1_it_spdvscr703";    break;
			case 15: sType = "x1_it_sparscr601";    break;
			case 16: sType = "x1_it_spdvscr604";    break;
			case 17: sType = "x2_it_sparscr503";    break;
			case 18: sType = "nw_it_sparscr708";    break;
			case 19: sType = "x1_it_spdvscr704";    break;
			case 20: sType = "x1_it_sparscr605";    break;
			case 21: sType = "nw_it_sparscr601";    break;
			case 22: sType = "nw_it_spdvscr701";    break;
			case 23: sType = "x1_it_spdvscr602";    break;
			case 24: sType = "x2_it_spdvscr606";    break;
			case 25: sType = "nw_it_sparscr612";    break;
			case 26: sType = "nw_it_sparscr613";    break;
			case 27: sType = "x2_it_spdvscr604";    break;
			case 28: sType = "x2_it_spdvscr605";    break;
			case 29: sType = "x1_it_sparscr603";    break;
			case 30: sType = "nw_it_sparscr611";    break;
			case 31: sType = "x1_it_spdvscr603";    break;
			case 32: sType = "nw_it_sparscr604";    break;
			case 33: sType = "nw_it_sparscr702";    break;
			case 34: sType = "nw_it_sparscr706";    break;
			case 35: sType = "nw_it_sparscr802";    break;
			case 36: sType = "x2_it_spdvscr702";    break;
			case 37: sType = "nw_it_spdvscr702";    break;
			case 38: sType = "nw_it_sparscr609";    break;
			case 39: sType = "x2_it_sparscr703";    break;
			case 40: sType = "nw_it_sparscr701";    break;
			case 41: sType = "x1_it_sparscr604";    break;
			case 42: sType = "x2_it_spdvscr602";    break;
			case 43: sType = "nw_it_sparscr605";    break;
			case 44: sType = "nw_it_sparscr703";    break;
			case 45: sType = "x2_it_spdvscr803";    break;
			case 46: sType = "nw_it_sparscr614";    break;
			case 47: sType = "nw_it_sparscr614";    break;
			case 48: sType = "x2_it_sparscr601";    break;
			case 49: sType = "x2_it_spdvscr701";    break;
			case 50: sType = "nw_it_sparscr603";    break;
			case 51: sType = "x1_it_spdvscr701";    break;
			case 52: sType = "x1_it_spdvscr601";    break;
			case 53: sType = "x1_it_sparscr602";    break;
			case 54: sType = "x1_it_sparscr701";    break;
			case 55: sType = "x2_it_spdvscr603";    break;
			case 56: sType = "nw_it_sparscr607";    break;
			case 57: sType = "nw_it_sparscr610";    break;
			case 58: sType = "nw_it_sparscr707";    break;
			case 59: sType = "x1_it_spdvscr605";    break;
			case 60: sType = "x1_it_spdvscr702";    break;
			case 61: sType = "x2_it_spdvscr601";    break;
			case 62: sType = "nw_it_sparscr704";    break;
			case 63: sType = "x1_it_spdvscr703";    break;
			case 64: sType = "x1_it_sparscr601";    break;
			case 65: sType = "x1_it_spdvscr604";    break;
			case 66: sType = "x2_it_sparscr503";    break;
			case 67: sType = "nw_it_sparscr708";    break;
			case 68: sType = "x1_it_spdvscr704";    break;
			case 69: sType = "x1_it_sparscr605";    break;
			case 70: sType = "nw_it_sparscr601";    break;
			case 71: sType = "nw_it_spdvscr701";    break;
			case 72: sType = "x1_it_spdvscr602";    break;
			case 73: sType = "x2_it_spdvscr606";    break;
			case 74: sType = "nw_it_sparscr612";    break;
			case 75: sType = "nw_it_sparscr613";    break;
			case 76: sType = "x2_it_spdvscr604";    break;
			case 77: sType = "x2_it_spdvscr605";    break;
			case 78: sType = "x1_it_sparscr603";    break;
			case 79: sType = "nw_it_sparscr611";    break;
			case 80: sType = "x1_it_spdvscr603";    break;
			case 81: sType = "nw_it_sparscr604";    break;
			case 82: sType = "nw_it_sparscr702";    break;
			case 83: sType = "nw_it_sparscr706";    break;
			case 84: sType = "nw_it_sparscr802";    break;
			case 85: sType = "x2_it_spdvscr702";    break;
			case 86: sType = "nw_it_spdvscr702";    break;
			case 87: sType = "nw_it_sparscr609";    break;
			case 88: sType = "x2_it_sparscr703";    break;
			case 89: sType = "nw_it_sparscr701";    break;
			case 90: sType = "x1_it_sparscr604";    break;
			case 91: sType = "x2_it_spdvscr602";    break;
			case 92: sType = "nw_it_sparscr605";    break;
			case 93: sType = "nw_it_sparscr703";    break;
			case 94: sType = "x2_it_spdvscr803";    break;
			case 95: sType = "nw_it_sparscr614";    break;
			case 96: sType = "nw_it_sparscr614";    break;
			case 97: sType = "x2_it_sparscr601";    break;
			case 98: sType = "x2_it_spdvscr701";    break;
			case 99: sType = "x2_it_sparscr703";    break;
			case 100: sType = "nw_it_sparscr701";	break;
       }
    }

	if (iRange==4||iRange==5)
    {
		iRoll = Random(40)+1;	//:: scrolls valued above 3200
		switch (iRoll)
		{
			case 1: sType = "nw_it_sparscr806";    break;
			case 2: sType = "x2_it_spdvscr804";    break;
			case 3: sType = "x1_it_sparscr801";    break;
			case 4: sType = "x1_it_sparscr901";    break;
			case 5: sType = "x2_it_sparscr901";    break;
			case 6: sType = "x2_it_sparscr801";    break;
			case 7: sType = "x1_it_spdvscr803";    break;
			case 8: sType = "x1_it_spdvscr804";    break;
			case 9: sType = "nw_it_sparscr905";	   break;
			case 10: sType = "x2_it_spdvscr901";   break;
			case 11: sType = "nw_it_sparscr908";   break;
			case 12: sType = "nw_it_sparscr902";   break;
			case 13: sType = "nw_it_sparscr803";   break;
			case 14: sType = "nw_it_sparscr912";   break;
			case 15: sType = "nw_it_sparscr809";   break;
			case 16: sType = "x2_it_spdvscr902";   break;
			case 17: sType = "nw_it_sparscr804";   break;
			case 18: sType = "nw_it_sparscr807";   break;
			case 19: sType = "nw_it_sparscr806";   break;
			case 20: sType = "x2_it_spdvscr801";   break;
			case 21: sType = "nw_it_sparscr906";   break;
			case 22: sType = "nw_it_sparscr801";   break;
			case 23: sType = "nw_it_sparscr901";   break;
			case 24: sType = "x2_it_spdvscr802";   break;
			case 25: sType = "nw_it_sparscr903";   break;
			case 26: sType = "nw_it_sparscr808";   break;
			case 27: sType = "nw_it_sparscr910";   break;
			case 28: sType = "x2_it_spdvscr903";   break;
			case 29: sType = "nw_it_sparscr904";   break;
			case 30: sType = "nw_it_sparscr805";   break;
			case 31: sType = "x1_it_spdvscr802";   break;
			case 32: sType = "nw_it_sparscr911";   break;
			case 33: sType = "x1_it_spdvscr901";   break;
			case 34: sType = "nw_it_sparscr909";   break;
			case 35: sType = "nw_it_sparscr907";   break;
			case 36: sType = "x1_it_spdvscr801";   break;
			case 37: sType = "nw_it_sparscr906";   break;
			case 38: sType = "nw_it_sparscr808";   break;
			case 39: sType = "x2_it_sparscr801";   break;
			case 40: sType = "x2_it_spdvscr804";   break;
		}
	}
	
	object oScroll =  CreateItemOnObject(sType, oSack, 1);
	
	SetIdentified(oScroll, FALSE);
}

void DropPotion(object oMod, object oSack)
{
	string sPotion;

	int nRandom = Random(41) + 1;
	switch (nRandom)
	{
		case 1: sPotion = "hy_mpotion001";  break;
		case 2: sPotion = "hy_mpotion002";  break;
		case 3: sPotion = "hy_mpotion003";  break;
		case 4: sPotion = "hy_mpotion005";  break;
		case 5: sPotion = "hy_mpotion006";  break;
		case 6: sPotion = "hy_mpotion008";  break;
		case 7: sPotion = "hy_mpotion009";  break;
		case 8: sPotion = "x2_it_mpotion001";  break;
		case 9: sPotion = "nw_it_mpotion001" ; break;
		case 10: sPotion = "nw_it_mpotion002" ; break;
		case 11: sPotion = "nw_it_mpotion003" ; break;
		case 12: sPotion = "nw_it_mpotion004" ; break;
		case 13: sPotion = "nw_it_mpotion005" ; break;
		case 14: sPotion = "nw_it_mpotion006";  break;
		case 15: sPotion = "nw_it_mpotion007";  break;
		case 16: sPotion = "nw_it_mpotion008";  break;
		case 17: sPotion = "nw_it_mpotion009";  break;
		case 18: sPotion = "nw_it_mpotion010";  break;
		case 19: sPotion = "nw_it_mpotion011";  break;
		case 20: sPotion = "nw_it_mpotion012";  break;
		case 21: sPotion = "nw_it_mpotion013";  break;
		case 22: sPotion = "nw_it_mpotion014";  break;
		case 23: sPotion = "nw_it_mpotion015";  break;
		case 24: sPotion = "nw_it_mpotion016";  break;
		case 25: sPotion = "nw_it_mpotion017";  break;
		case 26: sPotion = "nw_it_mpotion018";  break;
		case 27: sPotion = "nw_it_mpotion019";  break;
		case 28: sPotion = "nw_it_mpotion020";  break;
		case 29: sPotion = "x2_it_mpotion001";  break;
		case 30: sPotion = "hy_mpotion001";  break;
		case 31: sPotion = "hy_mpotion002";  break;
		case 32: sPotion = "hy_mpotion003";  break;
		case 33: sPotion = "hy_mpotion004";  break;
		case 34: sPotion = "hy_mpotion005";  break;
		case 35: sPotion = "hy_mpotion006";  break;
		case 36: sPotion = "hy_mpotion007";  break;
		case 37: sPotion = "hy_mpotion008";  break;
		case 38: sPotion = "hy_mpotion009";  break;
		case 39: sPotion = "hy_mpotion010";  break;
		case 40: sPotion = "hy_mpotion011";  break;
		case 41: sPotion = "hy_mpotion012";  break;
	}

	CreateItemOnObject(sPotion, oSack, 1);
}

void DropRodWand(object oMod, object oSack)
{
	string sType;

	int nRandom = Random(16) + 1;
	switch (nRandom)
	{//::: Rods
		case 1: sType = "nw_wmgmrd002";	break;	//res
        case 2: sType = "nw_wmgmrd005";	break;  //rev
        case 3: sType = "nw_wmgrd002";	break;	// ghost
        case 4: sType = "nw_wmgmrd006";	break;	// frost

    //:: Wands
		case 5: sType = "nw_wmgwn011";    break;
        case 6: sType = "nw_wmgwn002";    break;
        case 7: sType = "nw_wmgwn007";    break;
        case 8: sType = "nw_wmgwn004";    break;
        case 9: sType = "nw_wmgwn006";    break;
        case 10: sType = "nw_wmgwn005";    break;
        case 11: sType = "nw_wmgwn012";    break;
        case 12: sType = "nw_wmgwn010";    break;
        case 13: sType = "nw_wmgwn008";    break;
        case 14: sType = "nw_wmgwn009";    break;
        case 15: sType = "nw_wmgwn003";    break;
        case 16: sType = "nw_wmgwn013";    break;
	}
 
	CreateItemOnObject(sType, oSack, 1);
}

void DropMisc(object oMod, object oSack)
{
	string sName;

	int nDice = Random(22)+1;
	switch (nDice)
	{
		case 1: sName = "nw_it_contain002"; break;	//mpouch
		case 2: sName = "nw_it_msmlmisc05"; break;  //healpearl
		case 3: sName = "nw_it_mmidmisc04"; break;  //scab bless
		case 4: sName = "x0_it_msmlmisc05"; break;  //earth elem
		case 5: sName = "x0_it_mmedmisc03"; break;  //harp
		case 6: sName = "x0_it_msmlmisc01"; break;	//water elem
		case 7: sName = "x0_it_msmlmisc02"; break;	//fire elem
		case 8: sName = "x0_it_mmedmisc01"; break;	//shielding
		case 9: sName = "x0_it_mthnmisc21"; break;	//air elem
		case 10: sName = "x0_it_mthnmisc19"; break; //bard lire
		case 11: sName = "x0_it_mthnmisc17"; break; // bard spls
		case 12: sName = "x0_it_mmedmisc02"; break; // bard spls
		case 13: sName = "x0_it_mthnmisc05"; break; // dust app
		case 14: sName = "x0_it_mthnmisc06"; break; // dust dissapp
		case 15: sName = "x0_it_mthnmisc13"; break; // sunbeam
		case 16: sName = "x0_it_mthnmisc15"; break; //
		case 17: sName = "x0_it_mthnmisc14"; break; // lath chal
		case 18: sName = "nw_it_mmidmisc01"; break; // harp charm
		case 19: sName = "nw_it_mmidmisc02"; break; // harp haunt
		case 20: sName = "nw_it_mmidmisc03"; break; // harp pand
		case 21: sName = "x0_it_msmlmisc06"; break; // horn good/evil
		case 22: sName = "x0_it_mthnmisc09"; break; //
	}
		
	CreateItemOnObject(sName, oSack, 1);
}

void DropSetItem(object oMod, object oSack, int iClass)
{
	string sName;
	int iDice;

	if (iClass==0)iClass = d12();

	switch (iClass)
	{
		case 1:
		{ //:: STR Fighter Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_fighter_s1_1"; break;
				case 2: sName = "sd_fighter_s1_2"; break;
				case 3: sName = "sd_fighter_s1_3"; break;
				case 4: sName = "sd_fighter_s1_4"; break;
				case 5: sName = "sd_fighter_s1_5"; break;
				case 6: sName = "sd_fighter_s1_6"; break;
			}
		}
		break;

		case 2:
		{//:: Wizard Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_wizard_s1_1"; break;
				case 2: sName = "sd_wizard_s1_2"; break;
				case 3: sName = "sd_wizard_s1_3"; break;
				case 4: sName = "sd_wizard_s1_4"; break;
				case 5: sName = "sd_wizard_s1_5"; break;
				case 6: sName = "sd_wizard_s1_6"; break;
			}
		}
		break;
		
		case 3:
		{//:: Sorcerer Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_sorc_s1_1"; break;
				case 2: sName = "sd_sorc_s1_2"; break;
				case 3: sName = "sd_sorc_s1_3"; break;
				case 4: sName = "sd_sorc_s1_4"; break;
				case 5: sName = "sd_sorc_s1_5"; break;
				case 6: sName = "sd_sorc_s1_6"; break;
			}
		}
		break;
		
		case 4:
		{//:: DEX Fighter/Rogue Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_fighter_s2_1"; break;
				case 2: sName = "sd_fighter_s2_2"; break;
				case 3: sName = "sd_fighter_s2_3"; break;
				case 4: sName = "sd_fighter_s2_4"; break;
				case 5: sName = "sd_fighter_s2_5"; break;
				case 6: sName = "sd_fighter_s2_6"; break;
			}
		} 
		break;
	
		case 5:
		{//:: Cleric Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_cleric_s1_1"; break;
				case 2: sName = "sd_cleric_s1_2"; break;
				case 3: sName = "sd_cleric_s1_3"; break;
				case 4: sName = "sd_cleric_s1_4"; break;
				case 5: sName = "sd_cleric_s1_5"; break;
				case 6: sName = "sd_cleric_s1_6"; break;
			}
		} 
		break;
		
		case 6:
		{//:: Bard Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_bard_s1_1"; break;
				case 2: sName = "sd_bard_s1_2"; break;
				case 3: sName = "sd_bard_s1_3"; break;
				case 4: sName = "sd_bard_s1_4"; break;
				case 5: sName = "sd_bard_s1_5"; break;
				case 6: sName = "sd_bard_s1_6"; break;
			}
		}
		break;
		
		case 7:
		{//:: Paladin Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_paladin_s1_1"; break;
				case 2: sName = "sd_paladin_s1_2"; break;
				case 3: sName = "sd_paladin_s1_3"; break;
				case 4: sName = "sd_paladin_s1_4"; break;
				case 5: sName = "sd_paladin_s1_5"; break;
				case 6: sName = "sd_paladin_s1_6"; break;
			}
		}
		break;
			
		case 8:
		{//:: Druid Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_druid_s1_1"; break;
				case 2: sName = "sd_druid_s1_2"; break;
				case 3: sName = "sd_druid_s1_3"; break;
				case 4: sName = "sd_druid_s1_4"; break;
				case 5: sName = "sd_druid_s1_5"; break;
				case 6: sName = "sd_druid_s1_6"; break;
			}
		}
		break;
		
		case 9:
		{//:: Ranger Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_ranger_s1_1"; break;
				case 2: sName = "sd_ranger_s1_2"; break;
				case 3: sName = "sd_ranger_s1_3"; break;
				case 4: sName = "sd_ranger_s1_4"; break;
				case 5: sName = "sd_ranger_s1_5"; break;
				case 6: sName = "sd_ranger_s1_6"; break;
			}
		}
		break;
			
		case 10:
		{//:: Monk Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_monk_s1_1"; break;
				case 2: sName = "sd_monk_s1_2"; break;
				case 3: sName = "sd_monk_s1_3"; break;
				case 4: sName = "sd_monk_s1_4"; break;
				case 5: sName = "sd_monk_s1_5"; break;
				case 6: sName = "sd_monk_s1_6"; break;
			}
		}
		break;
		
		case 11:
		{//:: Barbarian Set
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_barb_s1_6"; break;
				case 2: sName = "sd_barb_s1_1"; break;
				case 3: sName = "sd_barb_s1_2"; break;
				case 4: sName = "sd_barb_s1_3"; break;
				case 5: sName = "sd_barb_s1_4"; break;
				case 6: sName = "sd_barb_s1_5"; break;
			}
		}
		break;
		
		case 12:
		{//:: Dual Weapon / Weapon & Shield Sets
			iDice = d6();
			switch(iDice)
			{
				case 1: sName = "sd_weap_s1_1"; break;
				case 2: sName = "sd_weap_s1_2"; break;
				case 3: sName = "sd_weap_s2_1"; break;
				case 4: sName = "sd_weap_s2_2"; break;
				case 5: sName = "sd_weap_s3_1"; break;
				case 6: sName = "sd_weap_s3_2"; break;
			}
		}
		break;
	}
// coming soon: Ring Sets, Ammy/Ring Sets

//////////////////////////////////////
//:Debugging
 FloatingTextStringOnCreature("Class: "+IntToString(iClass), oMod);
 FloatingTextStringOnCreature("Roll: "+IntToString(iDice), oMod);
 FloatingTextStringOnCreature(sName, oMod);

 CreateItemOnObject(sName, oSack, 1);
}

void DropGold (object oMob, object oSack, int iBonus)
{
	string sGold, sLvl;
	int iHD = GetHitDice(oMob);
	int iGold = 0;

	iGold = (d20()*iHD)+(10*iHD)+iBonus;

	CreateItemOnObject("nw_it_gold001", oSack, iGold);

	/////////////////////////////////////////////////////////////////////////
	//Gold Debugging
	//object oKiller = GetFirstPC();
	//sGold = IntToString(iGold);
	//sLvl = IntToString(iHD);
	//FloatingTextStringOnCreature(sGold+" Gold Spawned by lvl "+sLvl+" mob", oKiller);
	//
	//
	//
}

void CastImbue(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iSpell;
	int iUses;
	int iRoll;
	switch (iRange)
	{
		case 1: 
		{
			iRoll = d20();
			if (iRoll==1)iSpell = IP_CONST_CASTSPELL_BURNING_HANDS_2;
			if (iRoll==2)iSpell = IP_CONST_CASTSPELL_BARKSKIN_3;
			if (iRoll==3)iSpell = IP_CONST_CASTSPELL_MAGE_ARMOR_2;
			if (iRoll==4)iSpell = IP_CONST_CASTSPELL_MAGIC_MISSILE_5;
			if (iRoll==5)iSpell = IP_CONST_CASTSPELL_COLOR_SPRAY_2;
			if (iRoll==6)iSpell = IP_CONST_CASTSPELL_DOOM_5;
			if (iRoll==7)iSpell = IP_CONST_CASTSPELL_ENTANGLE_5;
			if (iRoll==8)iSpell = IP_CONST_CASTSPELL_GHOSTLY_VISAGE_3;
			if (iRoll==9)iSpell = IP_CONST_CASTSPELL_BLESS_2;
			if (iRoll==10)iSpell = IP_CONST_CASTSPELL_BULLS_STRENGTH_3;
			if (iRoll==11)iSpell = IP_CONST_CASTSPELL_CATS_GRACE_3;
			if (iRoll==12)iSpell =  IP_CONST_CASTSPELL_CLARITY_3;
			if (iRoll==13)iSpell =  IP_CONST_CASTSPELL_GREASE_2;
			if (iRoll==14)iSpell =  IP_CONST_CASTSPELL_HOLD_PERSON_3;
			if (iRoll==15)iSpell =  IP_CONST_CASTSPELL_HOLD_MONSTER_7;
			if (iRoll==16)iSpell =  IP_CONST_CASTSPELL_INVISIBILITY_3;
			if (iRoll==17)iSpell =  IP_CONST_CASTSPELL_INVISIBILITY_PURGE_5;
			if (iRoll==18)iSpell =  IP_CONST_CASTSPELL_LIGHTNING_BOLT_5;
			if (iRoll==19)iSpell =  IP_CONST_CASTSPELL_SHIELD_5;
			if (iRoll==20)iSpell =  IP_CONST_CASTSPELL_SILENCE_3;
		}
		break;
	
		case 2: 
		{
			iRoll = d20();
			if (iRoll==1)iSpell = IP_CONST_CASTSPELL_DISPLACEMENT_9;
			if (iRoll==2)iSpell = IP_CONST_CASTSPELL_FIREBALL_10;
			if (iRoll==3)iSpell = IP_CONST_CASTSPELL_MAGIC_CIRCLE_AGAINST_ALIGNMENT_5;
			if (iRoll==4)iSpell = IP_CONST_CASTSPELL_SLAY_LIVING_9;
			if (iRoll==5)iSpell = IP_CONST_CASTSPELL_STONESKIN_7;
			if (iRoll==6)iSpell = IP_CONST_CASTSPELL_ICE_STORM_9;
			if (iRoll==7)iSpell = IP_CONST_CASTSPELL_CALL_LIGHTNING_10;
			if (iRoll==8)iSpell = IP_CONST_CASTSPELL_ISAACS_LESSER_MISSILE_STORM_13;
			if (iRoll==9)iSpell = IP_CONST_CASTSPELL_FLAME_ARROW_12;
			if (iRoll==10)iSpell = IP_CONST_CASTSPELL_FLAME_STRIKE_7;
			if (iRoll==11)iSpell = IP_CONST_CASTSPELL_HOLD_PERSON_3;
			if (iRoll==12)iSpell = IP_CONST_CASTSPELL_HOLD_MONSTER_7;
			if (iRoll==13)iSpell = IP_CONST_CASTSPELL_INVISIBILITY_PURGE_5;
			if (iRoll==14)iSpell = IP_CONST_CASTSPELL_INVISIBILITY_SPHERE_5;
			if (iRoll==15)iSpell = IP_CONST_CASTSPELL_LIGHTNING_BOLT_10;
			if (iRoll==16)iSpell =  IP_CONST_CASTSPELL_MINOR_GLOBE_OF_INVULNERABILITY_7;
			if (iRoll==17)iSpell =  IP_CONST_CASTSPELL_STINKING_CLOUD_5;
			if (iRoll==18)iSpell =  IP_CONST_CASTSPELL_SUMMON_CREATURE_IV_7;
			if (iRoll==19)iSpell =  IP_CONST_CASTSPELL_SUMMON_CREATURE_V_9;
			if (iRoll==20)iSpell =  IP_CONST_CASTSPELL_SUMMON_CREATURE_VI_11;
		}
		break;
		
		case 3: 
		{
			iRoll = d12();
			if (iRoll==1)iSpell = IP_CONST_CASTSPELL_SUNBEAM_13;
			if (iRoll==2)iSpell = IP_CONST_CASTSPELL_CONE_OF_COLD_15;
			if (iRoll==3)iSpell = IP_CONST_CASTSPELL_MASS_HEAL_15;
			if (iRoll==4)iSpell = IP_CONST_CASTSPELL_ETHEREAL_VISAGE_15;
			if (iRoll==5)iSpell = IP_CONST_CASTSPELL_GREATER_DISPELLING_15;
			if (iRoll==6)iSpell = IP_CONST_CASTSPELL_ISAACS_GREATER_MISSILE_STORM_15;
			if (iRoll==7)iSpell = IP_CONST_CASTSPELL_CHAIN_LIGHTNING_20;
			if (iRoll==8)iSpell = IP_CONST_CASTSPELL_GREATER_STONESKIN_11;
			if (iRoll==9)iSpell = IP_CONST_CASTSPELL_FIREBRAND_15;
			if (iRoll==10)iSpell = IP_CONST_CASTSPELL_FLAME_ARROW_12;
			if (iRoll==11)iSpell = IP_CONST_CASTSPELL_FLAME_STRIKE_12;
			if (iRoll==12)iSpell = IP_CONST_CASTSPELL_INVISIBILITY_SPHERE_5;
		}
		break;
		
		case 4: 
		{
			iRoll = Random(16);
			if (iRoll==1)iSpell = IP_CONST_CASTSPELL_BIGBYS_FORCEFUL_HAND_15;
			if (iRoll==2)iSpell = IP_CONST_CASTSPELL_HORRID_WILTING_20;
			if (iRoll==3)iSpell = IP_CONST_CASTSPELL_IMPLOSION_17;
			if (iRoll==4)iSpell = IP_CONST_CASTSPELL_INCENDIARY_CLOUD_15;
			if (iRoll==5)iSpell = IP_CONST_CASTSPELL_PREMONITION_15;
			if (iRoll==6)iSpell = IP_CONST_CASTSPELL_MORDENKAINENS_DISJUNCTION_17;
			if (iRoll==7)iSpell = IP_CONST_CASTSPELL_METEOR_SWARM_17;
			if (iRoll==8)iSpell = IP_CONST_CASTSPELL_ISAACS_GREATER_MISSILE_STORM_15;
			if (iRoll==9)iSpell = IP_CONST_CASTSPELL_SUNBEAM_13;
			if (iRoll==10)iSpell = IP_CONST_CASTSPELL_CONE_OF_COLD_15;
			if (iRoll==11)iSpell = IP_CONST_CASTSPELL_MASS_HEAL_15;
			if (iRoll==12)iSpell = IP_CONST_CASTSPELL_ETHEREAL_VISAGE_15;
			if (iRoll==13)iSpell = IP_CONST_CASTSPELL_FIREBRAND_15;
			if (iRoll==14)iSpell = IP_CONST_CASTSPELL_FLAME_ARROW_18;
			if (iRoll==15)iSpell = IP_CONST_CASTSPELL_FLAME_STRIKE_18;
			if (iRoll==16)iSpell = IP_CONST_CASTSPELL_MASS_HEAL_15;
		}
		break;
		
		case 5: 
		{
			iRoll = d20();
			if (iRoll==1)iSpell = IP_CONST_CASTSPELL_WAIL_OF_THE_BANSHEE_17;
			if (iRoll==2)iSpell = IP_CONST_CASTSPELL_TIME_STOP_17;
			if (iRoll==3)iSpell = IP_CONST_CASTSPELL_IMPLOSION_17;
			if (iRoll==4)iSpell = IP_CONST_CASTSPELL_ISAACS_GREATER_MISSILE_STORM_15;
			if (iRoll==5)iSpell = IP_CONST_CASTSPELL_HORRID_WILTING_20;
			if (iRoll==6)iSpell = IP_CONST_CASTSPELL_MORDENKAINENS_SWORD_18;
			if (iRoll==7)iSpell = IP_CONST_CASTSPELL_MORDENKAINENS_DISJUNCTION_17;
			if (iRoll==8)iSpell = IP_CONST_CASTSPELL_MASS_HEAL_15;
			if (iRoll==9)iSpell = IP_CONST_CASTSPELL_BIGBYS_FORCEFUL_HAND_15;
			if (iRoll==10)iSpell = IP_CONST_CASTSPELL_HORRID_WILTING_20;
			if (iRoll==11)iSpell = IP_CONST_CASTSPELL_IMPLOSION_17;
			if (iRoll==12)iSpell = IP_CONST_CASTSPELL_INCENDIARY_CLOUD_15;
			if (iRoll==13)iSpell = IP_CONST_CASTSPELL_PREMONITION_15;
			if (iRoll==14)iSpell = IP_CONST_CASTSPELL_MORDENKAINENS_DISJUNCTION_17;
			if (iRoll==15)iSpell = IP_CONST_CASTSPELL_METEOR_SWARM_17;
			if (iRoll==16)iSpell = IP_CONST_CASTSPELL_ISAACS_GREATER_MISSILE_STORM_15;
			if (iRoll==17)iSpell = IP_CONST_CASTSPELL_SUNBEAM_13;
			if (iRoll==18)iSpell = IP_CONST_CASTSPELL_CONE_OF_COLD_15;
			if (iRoll==19)iSpell = IP_CONST_CASTSPELL_MASS_HEAL_15;
			if (iRoll==20)iSpell = IP_CONST_CASTSPELL_ETHEREAL_VISAGE_15;
		}
		break;

	}

	switch (iRange)
	{
		case 1: 
		{
			iRoll = d3();
			if (iRoll==1)iUses = IP_CONST_CASTSPELL_NUMUSES_5_CHARGES_PER_USE;
			if (iRoll==2)iUses = IP_CONST_CASTSPELL_NUMUSES_4_CHARGES_PER_USE;
			if (iRoll==3)iUses = IP_CONST_CASTSPELL_NUMUSES_3_CHARGES_PER_USE;
			break; 
		}
		case 2: 
		{
			iRoll = d3();
			if (iRoll==1)iUses = IP_CONST_CASTSPELL_NUMUSES_5_CHARGES_PER_USE;
			if (iRoll==2)iUses = IP_CONST_CASTSPELL_NUMUSES_5_CHARGES_PER_USE;
			if (iRoll==3)iUses = IP_CONST_CASTSPELL_NUMUSES_4_CHARGES_PER_USE;
			break;
		}
		case 3: 
		{
			iRoll = d3();
			if (iRoll==1)iUses = IP_CONST_CASTSPELL_NUMUSES_4_CHARGES_PER_USE;
			if (iRoll==2)iUses = IP_CONST_CASTSPELL_NUMUSES_3_CHARGES_PER_USE;
			if (iRoll==3)iUses = IP_CONST_CASTSPELL_NUMUSES_2_CHARGES_PER_USE;
			
			iRoll = d100();
			
			if (iRoll > 95)
			{
				iUses = IP_CONST_CASTSPELL_NUMUSES_1_USE_PER_DAY;
			}
			
			break; 
		}	
		case 4: 
		{
			iRoll = d3();
			
			if (iRoll==1)iUses = IP_CONST_CASTSPELL_NUMUSES_1_CHARGE_PER_USE;
			if (iRoll==2)iUses = IP_CONST_CASTSPELL_NUMUSES_1_USE_PER_DAY;
			if (iRoll==3)iUses = IP_CONST_CASTSPELL_NUMUSES_2_USES_PER_DAY;
			
			iRoll = d100();
			
			if (iRoll > 95)
			{
				iUses = IP_CONST_CASTSPELL_NUMUSES_2_USES_PER_DAY;
			}
			
			break;
		}
		case 5: 
		{	
			iRoll = d3();
			
			if (iRoll==1)iUses = IP_CONST_CASTSPELL_NUMUSES_1_USE_PER_DAY;
			if (iRoll==2)iUses = IP_CONST_CASTSPELL_NUMUSES_2_USES_PER_DAY;
			if (iRoll==3)iUses = IP_CONST_CASTSPELL_NUMUSES_3_USES_PER_DAY;
			
			iRoll = d100();
			
			if (iRoll > 90)
			{
				iUses = IP_CONST_CASTSPELL_NUMUSES_4_USES_PER_DAY;
			}
			
			break;
		}
	}
	
	ipAdd = ItemPropertyCastSpell(iSpell, iUses);
	IPSafeAddItemProperty(oItem, ipAdd);
	iRoll = (d12() * iRange)+1; if (iRoll > 50) iRoll = 50;
	SetItemCharges(oItem, iRoll);
}

void WeapOnHit(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iProperty;
	int iSaveDC;
	int iSpecial;
	int iRoll;
	
	switch (iRange)
	{
		case 1: 
		{
			iRoll = d3();
			if (iRoll==1)iProperty = IP_CONST_ONHIT_DOOM;
			if (iRoll==2)iProperty = IP_CONST_ONHIT_DEAFNESS;
			if (iRoll==3)iProperty = IP_CONST_ONHIT_SLOW;
		}
		break;
		case 2: 
		{
			iRoll = d4();
			if (iRoll==1)iProperty = IP_CONST_ONHIT_DOOM;
			if (iRoll==2)iProperty = IP_CONST_ONHIT_DEAFNESS;
			if (iRoll==3)iProperty = IP_CONST_ONHIT_SLOW;
			if (iRoll==4)iProperty = IP_CONST_ONHIT_SLEEP;
		}
		break;
		case 3: 
		{
			iRoll = d6();
			if (iRoll==1)iProperty = IP_CONST_ONHIT_DOOM;
			if (iRoll==2)iProperty = IP_CONST_ONHIT_DEAFNESS;
			if (iRoll==3)iProperty = IP_CONST_ONHIT_SLOW;
			if (iRoll==4)iProperty = IP_CONST_ONHIT_SLEEP;
			if (iRoll==5)iProperty = IP_CONST_ONHIT_DAZE;
			if (iRoll==6)iProperty = IP_CONST_ONHIT_STUN;
		}
		break;
		case 4: 
		{
			iRoll = d10();
			if (iRoll==1)iProperty = IP_CONST_ONHIT_DOOM;
			if (iRoll==2)iProperty = IP_CONST_ONHIT_SILENCE;
			if (iRoll==3)iProperty = IP_CONST_ONHIT_SLOW;
			if (iRoll==4)iProperty = IP_CONST_ONHIT_SLEEP;
			if (iRoll==5)iProperty = IP_CONST_ONHIT_DAZE;
			if (iRoll==6)iProperty = IP_CONST_ONHIT_STUN;
			if (iRoll==7)iProperty = IP_CONST_ONHIT_HOLD;
			if (iRoll==8)iProperty = IP_CONST_ONHIT_CONFUSION;
			if (iRoll==9)iProperty = IP_CONST_ONHIT_ABILITYDRAIN;
			if (iRoll==10)iProperty = IP_CONST_ONHIT_FEAR;
		}
		break;
		case 5: 
		{
			iRoll = d12();
			if (iRoll==1)iProperty = IP_CONST_ONHIT_DOOM;
			if (iRoll==2)iProperty = IP_CONST_ONHIT_SILENCE;
			if (iRoll==3)iProperty = IP_CONST_ONHIT_SLOW;
			if (iRoll==4)iProperty = IP_CONST_ONHIT_SLEEP;
			if (iRoll==5)iProperty = IP_CONST_ONHIT_DAZE;
			if (iRoll==6)iProperty = IP_CONST_ONHIT_STUN;
			if (iRoll==7)iProperty = IP_CONST_ONHIT_HOLD;
			if (iRoll==8)iProperty = IP_CONST_ONHIT_CONFUSION;
			if (iRoll==9)iProperty = IP_CONST_ONHIT_ABILITYDRAIN;
			if (iRoll==10)iProperty = IP_CONST_ONHIT_FEAR;
			if (iRoll==11)iProperty = IP_CONST_ONHIT_BLINDNESS;
			if (iRoll==12)iProperty = IP_CONST_ONHIT_LEVELDRAIN;
		}
		
		break;
	}

	switch (iRange)
	{
		case 1: 
		{
			iRoll = d2();
			if (iRoll==1)iSaveDC = IP_CONST_ONHIT_SAVEDC_14;
			if (iRoll==2)iSaveDC = IP_CONST_ONHIT_SAVEDC_14;
			break; 
		}
		case 2: 
		{
			iRoll = d3();
			if (iRoll==1)iSaveDC = IP_CONST_ONHIT_SAVEDC_14;
			if (iRoll==2)iSaveDC = IP_CONST_ONHIT_SAVEDC_14;
			if (iRoll==3)iSaveDC = IP_CONST_ONHIT_SAVEDC_14;
			break; 
		}
		case 3: 
		{
			iRoll = d3();
			if (iRoll==1)iSaveDC = IP_CONST_ONHIT_SAVEDC_14;
			if (iRoll==2)iSaveDC = IP_CONST_ONHIT_SAVEDC_14;
			if (iRoll==3)iSaveDC = IP_CONST_ONHIT_SAVEDC_16;
			
			iRoll = d100();
			if (iRoll > 95)
			{
				iSaveDC = IP_CONST_ONHIT_SAVEDC_18;
			}
			break; 
		}
		case 4: 
		{
			iRoll = d4();

			if (iRoll==1)iSaveDC = IP_CONST_ONHIT_SAVEDC_14;
			if (iRoll==2)iSaveDC = IP_CONST_ONHIT_SAVEDC_14;
			if (iRoll==3)iSaveDC = IP_CONST_ONHIT_SAVEDC_16;
			if (iRoll==4)iSaveDC = IP_CONST_ONHIT_SAVEDC_18;
		
			iRoll=d100();
		
			if (iRoll > 95)
			{
				iSaveDC = IP_CONST_ONHIT_SAVEDC_20;
			}
			
			break;
		}
		case 5: 
		{
			iRoll = d4();
			
			if (iRoll==1)iSaveDC = IP_CONST_ONHIT_SAVEDC_16;
			if (iRoll==2)iSaveDC = IP_CONST_ONHIT_SAVEDC_18;
			if (iRoll==3)iSaveDC = IP_CONST_ONHIT_SAVEDC_20;
			if (iRoll==4)iSaveDC = IP_CONST_ONHIT_SAVEDC_22;
			
			iRoll=d100();
			
			if (iRoll>90)
			{
				iSaveDC = IP_CONST_ONHIT_SAVEDC_24;
			}
			break;
		}
	}

	switch (iRange)
	{
		case 1: case 2: case 3: case 4: case 5: 
		{
			iRoll = Random(5);
			if (iRoll==1)iSpecial = IP_CONST_ONHIT_DURATION_5_PERCENT_5_ROUNDS;
			if (iRoll==2)iSpecial = IP_CONST_ONHIT_DURATION_10_PERCENT_4_ROUNDS;
			if (iRoll==3)iSpecial = IP_CONST_ONHIT_DURATION_25_PERCENT_3_ROUNDS;
			if (iRoll==4)iSpecial = IP_CONST_ONHIT_DURATION_50_PERCENT_2_ROUNDS;
			if (iRoll==5)iSpecial = IP_CONST_ONHIT_DURATION_75_PERCENT_1_ROUND;
			break; 
		}
	}
	
	ipAdd = ItemPropertyOnHitProps(iProperty, iSaveDC, iSpecial);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void SpellSlot(object oItem, int iRange, int iNum)
{
	itemproperty ipAdd;
	itemproperty ipClass;
	int iLvl, i;
	int iClass;
	int iSpec;
	int iRoll;
	iRoll = d8();
	switch (iRoll)
	{
		case 1: 
		{
			iClass = IP_CONST_CLASS_BARD; iSpec = 1;
			ipClass = ItemPropertyLimitUseByClass(IP_CONST_CLASS_BARD);
		} 
		break;
		case 2: 
		{
			iClass = IP_CONST_CLASS_DRUID;
			ipClass = ItemPropertyLimitUseByClass(IP_CONST_CLASS_DRUID);
		} 
		break;
		case 3: 
		{
			iClass = IP_CONST_CLASS_SORCERER;
			ipClass = ItemPropertyLimitUseByClass(IP_CONST_CLASS_SORCERER);
		} 
		break;
		case 4: 
		{
			iClass = IP_CONST_CLASS_WIZARD;
			ipClass = ItemPropertyLimitUseByClass(IP_CONST_CLASS_WIZARD);
		} 
		break;
		case 5: 
		{
			iClass = IP_CONST_CLASS_PALADIN; iSpec = 2;
			ipClass = ItemPropertyLimitUseByClass(IP_CONST_CLASS_PALADIN);
		} 
		break;
		case 6: 
		{
			iClass = IP_CONST_CLASS_RANGER; iSpec = 2;
			ipClass = ItemPropertyLimitUseByClass(IP_CONST_CLASS_RANGER);
		} 
		break;
		case 7: 
		{
			iClass = IP_CONST_CLASS_CLERIC;
			ipClass = ItemPropertyLimitUseByClass(IP_CONST_CLASS_CLERIC);
		} 
		break;
		case 8: 
		{
			iClass = IP_CONST_CLASS_WIZARD;
			ipClass = ItemPropertyLimitUseByClass(IP_CONST_CLASS_WIZARD);
		} 
		break;
	}

	for (i = 0; i < iNum; i++)
	{
		if (iSpec == 1)
		{
			switch (iRange)  // Bard max lvl 6
			{
				case 1: iLvl = 1; break;                          // 1
				case 2: {iLvl = d3();if(iLvl==3)iLvl=2;}break;    // 1-2
				case 3: {iLvl = d3()+2;if(iLvl==5)iLvl=3;}break;  // 3-4
				case 4: {iLvl = d3()+3;if(iLvl==6)iLvl=4;}break;  // 4-5
				case 5: {iLvl = d3()+4;if(iLvl==7)iLvl=5;}break;  // 5-6
			}
		}
		else if (iSpec == 2)
		{
			switch (iRange)  // Pally & Ranger max lvl 4
			{
				case 1: iLvl = 1; break;                            // 1
				case 2: {iLvl = d3();if(iLvl==3)iLvl=2;}break;      // 1-2
				case 3: iLvl = d3(); break;                         // 1-3
				case 4: iLvl = d3()+1; break;                       // 2-4
				case 5: {iLvl = d3()+2;if(iLvl==5)iLvl=3;}break;    // 3-4
			}
		}
		else
		{
			switch (iRange)  // The rest max lvl 9
			{
				case 1: iLvl = 1; break;                         // 1
				case 2: iLvl = d4(); break;                      // 1-4
				case 3: iLvl = d4()+1; break;                    // 2-5
				case 4: iLvl = d6()+2; break;                    // 3-8
				case 5: iLvl = d6()+4; break;                    // 5-9
			}
		}
		ipAdd = ItemPropertyBonusLevelSpell(iClass, iLvl);
		AddItemProperty(DURATION_TYPE_PERMANENT, ipAdd, oItem);
	}
	
	IPSafeAddItemProperty(oItem, ipClass);
}

void ImbueDivinity(object oItem, int iRange)	//:: PRC8
{
	itemproperty ipAdd;	
	int iEnh;

	switch (iRange)
	{
		case 1:
		{
			//:: Cantrips & 1st lvl spells
			iEnh = Random(2); break;                     
		}
		case 2:
		{
			//:: 1st & 2nd lvl spells
			iEnh = Random(2)+1; break;                     
		}
		case 3:
		{
			//:: 2nd & 3rd lvl spells
			iEnh = Random(2)+2; break;                     
		}			
		case 4:
		{
			//:: 3rd & 4th lvl spells
			iEnh = Random(2)+3; break;                     
		}			
		case 5:
		{
			//:: 3rd, 4th & 5th lvl spells
			iEnh = Random(3)+3; break;                     
		}			
	}
	
	ipAdd = ItemPropertyDivinity(iEnh);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void ImbueWizardry(object oItem, int iRange)	//:: PRC8
{
	itemproperty ipAdd;	
	int iEnh;

	switch (iRange)
	{
		case 1:
		{
			//:: Cantrips & 1st lvl spells
			iEnh = Random(2); break;                     
		}
		case 2:
		{
			//:: 1st & 2nd lvl spells
			iEnh = Random(2)+1; break;                     
		}
		case 3:
		{
			//:: 2nd & 3rd lvl spells
			iEnh = Random(2)+2; break;                     
		}			
		case 4:
		{
			//:: 3rd & 4th lvl spells
			iEnh = Random(2)+3; break;                     
		}			
		case 5:
		{
			//:: 3rd, 4th & 5th lvl spells
			iEnh = Random(3)+3; break;                     
		}			
	}
	
	ipAdd = ItemPropertyWizardry(iEnh);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void MightyEnhance(object oItem, int iRange)
{
	itemproperty ipAdd;
	
	int iEnh = d4();

	switch (iRange)
	{
		case 1: iEnh+=0; break;                       // 1-4
		case 2: iEnh+=1; break;                       // 2-5
		case 3: iEnh+=2; break;                       // 3-6
		case 4: iEnh+=3; break;                       // 4-7
		case 5: iEnh+=4; break;                       // 5-8
	}
	
	ipAdd = ItemPropertyMaxRangeStrengthMod(iEnh);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void BowEnhance(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iEnh = d3();

	switch (iRange)
	{
		case 1: if (iEnh==3)iEnh=1; break;            // 1-2
		case 2: {iEnh+=1;if (iEnh==4)iEnh=1;} break;  // 2-3
		case 3: {iEnh+=1;if (iEnh==4)iEnh=2;} break;  // 3-4
		case 4: {iEnh+=2;if (iEnh==5)iEnh=3;} break;  // 4-5
		case 5: {iEnh+=3;if (iEnh==6)iEnh=4;} break;  // 5-6
	}
	
	ipAdd = ItemPropertyAttackBonus(iEnh);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void AmmoUnlim(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iType;
	int iDam;
	int iRoll = d12();
	
	switch (iRange)
	{
		case 1:
		{
			iRoll = d3();
			if (iRoll==1)iType = IP_CONST_UNLIMITEDAMMO_BASIC;
			if (iRoll==2)iType = IP_CONST_UNLIMITEDAMMO_BASIC;
			if (iRoll==3)iType = IP_CONST_UNLIMITEDAMMO_PLUS1;
		}
		break;
		case 2: 
		{
			iRoll = d4();
			if (iRoll==1)iType = IP_CONST_UNLIMITEDAMMO_PLUS2;
			if (iRoll==2)iType = IP_CONST_UNLIMITEDAMMO_PLUS2;
			if (iRoll==3)iType = IP_CONST_UNLIMITEDAMMO_1D6COLD;
			if (iRoll==4)iType = IP_CONST_UNLIMITEDAMMO_1D6LIGHT;
		}
		break;
		case 3: 
		{
			iRoll = d6();
			if (iRoll==1)iType = IP_CONST_UNLIMITEDAMMO_PLUS2;
			if (iRoll==2)iType = IP_CONST_UNLIMITEDAMMO_PLUS2;
			if (iRoll==3)iType = IP_CONST_UNLIMITEDAMMO_PLUS3;
			if (iRoll==4)iType = IP_CONST_UNLIMITEDAMMO_1D6FIRE;
			if (iRoll==5)iType = IP_CONST_UNLIMITEDAMMO_1D6COLD;
			if (iRoll==6)iType = IP_CONST_UNLIMITEDAMMO_1D6LIGHT;
		}
		break;
		case 4: 
		{
			iRoll = d6();
			if (iRoll==1)iType = IP_CONST_UNLIMITEDAMMO_PLUS3;
			if (iRoll==2)iType = IP_CONST_UNLIMITEDAMMO_PLUS4;
			if (iRoll==3)iType = IP_CONST_UNLIMITEDAMMO_PLUS4;
			if (iRoll==4)iType = IP_CONST_UNLIMITEDAMMO_1D6FIRE;
			if (iRoll==5)iType = IP_CONST_UNLIMITEDAMMO_1D6COLD;
			if (iRoll==6)iType = IP_CONST_UNLIMITEDAMMO_1D6LIGHT;
		}
		break;
		case 5: 
		{
			iRoll = d6();
			if (iRoll==1)iType = IP_CONST_UNLIMITEDAMMO_PLUS4;
			if (iRoll==2)iType = IP_CONST_UNLIMITEDAMMO_PLUS5;
			if (iRoll==3)iType = IP_CONST_UNLIMITEDAMMO_PLUS5;
			if (iRoll==4)iType = IP_CONST_UNLIMITEDAMMO_1D6FIRE;
			if (iRoll==5)iType = IP_CONST_UNLIMITEDAMMO_1D6COLD;
			if (iRoll==6)iType = IP_CONST_UNLIMITEDAMMO_1D6LIGHT;
		}
		break;
	}
	
	ipAdd = ItemPropertyUnlimitedAmmo(iType);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void AmmoEnhance(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iType;
	int iDam;
	int iRoll = d10();
	
	switch (iRoll)
	{
		case 1: iType = IP_CONST_DAMAGETYPE_ACID; 			break;
		case 2: iType = IP_CONST_DAMAGETYPE_BLUDGEONING; 	break;
		case 3: iType = IP_CONST_DAMAGETYPE_COLD; 			break;
		case 4: iType = IP_CONST_DAMAGETYPE_ELECTRICAL; 	break;
		case 5: iType = IP_CONST_DAMAGETYPE_FIRE; 			break;
		case 6: iType = IP_CONST_DAMAGETYPE_MAGICAL; 		break;
		case 7: iType = IP_CONST_DAMAGETYPE_NEGATIVE; 		break;
		case 8: iType = IP_CONST_DAMAGETYPE_PIERCING; 		break;
		case 9: iType = IP_CONST_DAMAGETYPE_SLASHING; 		break;
		case 10: iType = IP_CONST_DAMAGETYPE_SONIC; 		break;
	}
	switch (iRange)
	{
		case 1: iDam = IP_CONST_DAMAGEBONUS_1; break;
		case 2: 
		{
			iRoll = d4();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d4;
		}
		break;
		case 3: 
		{
			iRoll = d6();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==5)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==6)iDam = IP_CONST_DAMAGEBONUS_1d4;
		}
		break;
		case 4: 
		{
			iRoll = d8();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==5)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==6)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==7)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==8)iDam = IP_CONST_DAMAGEBONUS_1d8;
		}
		break;
		case 5: 
		{
			iRoll = d10();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==5)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==6)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==7)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==8)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==9)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==10)iDam = IP_CONST_DAMAGEBONUS_1d10;
		}
		break;
	}
	
	ipAdd = ItemPropertyDamageBonus(iType, iDam);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void WeapEnhance(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iEnh = d3();

	switch (iRange)
	{
		case 1: if (iEnh==3)iEnh=1; break;            // 1-2
		case 2: {iEnh+=1;if (iEnh==4)iEnh=1;} break;  // 2-3
		case 3: {iEnh+=1;if (iEnh==4)iEnh=2;} break;  // 3-4
		case 4: {iEnh+=2;if (iEnh==5)iEnh=3;} break;  // 4-5
		case 5: {iEnh+=3;if (iEnh==6)iEnh=4;} break;  // 5-6
	}
	
	ipAdd = ItemPropertyAttackBonus(iEnh);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void RangedImbue(object oItem)
{
	itemproperty ipAdd;
	int iType;
	int iRoll = d3();

	switch (iRoll)
	{
		case 1: if (iRoll==1)iType=IP_CONST_DAMAGETYPE_BLUDGEONING; break;
		case 2: if (iRoll==2)iType=IP_CONST_DAMAGETYPE_SLASHING; break;
		case 3: if (iRoll==3)iType=IP_CONST_DAMAGETYPE_PIERCING; break;
	}
	
	ipAdd = ItemPropertyExtraRangeDamageType(iType);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void MeleeImbue(object oItem)
{
	itemproperty ipAdd;
	int iType;
	int iRoll = d3();

	switch (iRoll)
	{
		case 1: if (iRoll==1)iType=IP_CONST_DAMAGETYPE_BLUDGEONING; break;
		case 2: if (iRoll==2)iType=IP_CONST_DAMAGETYPE_SLASHING; break;
		case 3: if (iRoll==3)iType=IP_CONST_DAMAGETYPE_PIERCING; break;
	}
	
	ipAdd = ItemPropertyExtraMeleeDamageType(iType);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void MassCritImbue(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iDam;
	int iCol;
	int iType;
	int iRoll;

	switch (iRange)
	{
		case 1: 
		{
			iRoll = d3();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
		}
		break;
		case 2: 
		{
			iRoll = d4();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_2;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d4;
			
			iRoll=d100();
			{
				if (iRoll>90)iDam = IP_CONST_DAMAGEBONUS_1d6;
			}
		}
		break;
		case 3: 
		{
			iRoll = d6();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==5)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==6)iDam = IP_CONST_DAMAGEBONUS_1d6;
		
			iRoll=d100();
			{
				if (iRoll>95)iDam = IP_CONST_DAMAGEBONUS_1d8;
			}
		}
		break;
		case 4: 
		{
			iRoll = d10();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==5)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==6)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==7)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==8)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==9)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==10)iDam = IP_CONST_DAMAGEBONUS_1d8;
			
			iRoll=d100();
			{
				if (iRoll > 95)iDam =  IP_CONST_DAMAGEBONUS_1d10;
			}
		}
		break;
		case 5: 
		{
			iRoll = d12();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==5)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==6)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==7)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==8)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==9)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==10)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==11)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==12)iDam = IP_CONST_DAMAGEBONUS_1d10;
			
			iRoll=d100();
			{
				if (iRoll > 95)iDam =  IP_CONST_DAMAGEBONUS_2d6;
			}
		}
		break;
	}
	
	ipAdd = ItemPropertyMassiveCritical(iDam);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void DamageTypeImbue(object oItem, int iRange)
{
	itemproperty ipAdd;
	itemproperty ipVis;
	int iDam;
	int iCol;
	int iType;
	int iRoll = d10();

	switch (iRoll)
	{
		case 1: {iType = IP_CONST_DAMAGETYPE_ACID; iCol=4;} break;
		case 2: {iType = IP_CONST_DAMAGETYPE_BLUDGEONING;} break;
		case 3: {iType = IP_CONST_DAMAGETYPE_COLD; iCol=2;} break;
		case 4: {iType = IP_CONST_DAMAGETYPE_ELECTRICAL; iCol=5;} break;
		case 5: {iType = IP_CONST_DAMAGETYPE_FIRE; iCol=3;} break;
		case 6: {iType = IP_CONST_DAMAGETYPE_MAGICAL; iCol=7;} break;
		case 7: {iType = IP_CONST_DAMAGETYPE_NEGATIVE; iCol=1;} break;
		case 8: {iType = IP_CONST_DAMAGETYPE_PIERCING; iCol=1;} break;
		case 9: {iType = IP_CONST_DAMAGETYPE_SLASHING; iCol=1;} break;
		case 10: {iType = IP_CONST_DAMAGETYPE_SONIC; iCol=7; }break;
	}
	switch (iRange)
	{
		case 1: 
		{
			iRoll = d2();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1;
		}
		break;
		case 2: 
		{
			iRoll = d4();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_2;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d4;
		}
		break;
		case 3: 
		{
			iRoll = d6();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==5)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==6)iDam = IP_CONST_DAMAGEBONUS_1d6;
		}
		break;
		case 4: 
		{
			iRoll = d10();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d4;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==5)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==6)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==7)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==8)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==9)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==10)iDam = IP_CONST_DAMAGEBONUS_1d8;
		}
		break;
		case 5: 
		{
			iRoll = d12();
			if (iRoll==1)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==2)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==3)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==4)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==5)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==6)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==7)iDam = IP_CONST_DAMAGEBONUS_1d6;
			if (iRoll==8)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==9)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==10)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==11)iDam = IP_CONST_DAMAGEBONUS_1d8;
			if (iRoll==12)iDam = IP_CONST_DAMAGEBONUS_1d10;
		}
		break;
	}
	
	ipAdd = ItemPropertyDamageBonus(iType, iDam);
	IPSafeAddItemProperty(oItem, ipAdd);
	//ipAdd = ItemPropertyVisualEffect(ITEM_VISUAL_EVIL);
	//IPSafeAddItemProperty(oItem, ipAdd);

}

void ACmisc(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iAC = d3();

	switch (iRange)
	{
		case 1: if(iAC==3)iAC=1; break;             // 1-2
		case 2: {iAC+=1;if (iAC==4)iAC=1;} break;   // 2-3
		case 3: {iAC+=1;if (iAC==4)iAC=2;} break;   // 3-4
		case 4: {iAC+=2;if (iAC==5)iAC=2;} break;   // 4-5
		case 5: {iAC+=3;if (iAC==6)iAC=3;} break;   // 5-6
	}
	
	ipAdd = ItemPropertyACBonus(iAC);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void ACimbue(object oItem, int iRange, int iSpec)
{
	itemproperty ipAdd;
	int iAC = d3();
	int iPen;
	int iRoll;
	int iAbil;
	
	switch (iRange)
	{
		case 1: if(iAC==3)iAC=1; break;             // 1-2
		case 2: {iAC+=1;if (iAC==4)iAC=1;} break;   // 2-3
		case 3: {iAC+=1;if (iAC==4)iAC=2;} break;   // 3-4
		case 4: {iAC+=2;if (iAC==5)iAC=2;} break;   // 4-5
		case 5: {iAC+=3;if (iAC==6)iAC=3;} break;   // 5-6
	}
	ipAdd = ItemPropertyACBonus(iAC);

	if (iSpec==1){iPen = d4()+1; ipAdd = ItemPropertyDecreaseAC(IP_CONST_ACMODIFIERTYPE_ARMOR, iPen);}
	if (iSpec==2){iPen = d4()+1; ipAdd = ItemPropertyEnhancementPenalty(iPen);}
	if (iSpec==3)
	{
		iPen = d4()+1;
		iRoll = d6();
		switch(iRoll)
		{
			case 1: iAbil = ABILITY_DEXTERITY; break;
			case 2: iAbil = ABILITY_INTELLIGENCE; break;
			case 3: iAbil = ABILITY_WISDOM; break;
			case 4: iAbil = ABILITY_CHARISMA; break;
			case 5: iAbil = ABILITY_CONSTITUTION; break;
			case 6: iAbil = ABILITY_STRENGTH; break;
		}
		
		ipAdd = ItemPropertyDecreaseAbility(iAbil, iPen);
	}
	
	if (iSpec==4)
	{
		iPen = d4()+1; ipAdd = ItemPropertyAttackPenalty(iPen);
	}

	IPSafeAddItemProperty(oItem, ipAdd);
}

void MIMMimbue(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iType;
	int iRoll;

	switch (iRange)
	{
		case 1: {} break;
		case 2: 
		{
			iRoll = Random(35);
			if (iRoll==1)iType = IP_CONST_IMMUNITYMISC_POISON;
			if (iRoll==2)iType = IP_CONST_IMMUNITYMISC_DISEASE;
			if (iRoll==3)iType = IP_CONST_IMMUNITYSPELL_SLEEP;
			if (iRoll==4)iType = IP_CONST_IMMUNITYSPELL_WEB;
			if (iRoll==5)iType = IP_CONST_IMMUNITYSPELL_RAY_OF_FROST;
			if (iRoll==6)iType = IP_CONST_IMMUNITYSPELL_GREASE;
			if (iRoll==7)iType = IP_CONST_IMMUNITYSPELL_COLOR_SPRAY;
			if (iRoll==8)iType = IP_CONST_IMMUNITYSPELL_DAZE;
			if (iRoll==9)iType = IP_CONST_IMMUNITYSPELL_BURNING_HANDS;
			if (iRoll==10)iType = IP_CONST_IMMUNITYSPELL_BLINDNESS_AND_DEAFNESS;
			if (iRoll==11)iType = IP_CONST_IMMUNITYSPELL_LIGHTNING_BOLT;
			if (iRoll==12)iType = IP_CONST_IMMUNITYSPELL_MAGIC_MISSILE;
			if (iRoll==13)iType = IP_CONST_FEAT_ALERTNESS;
			if (iRoll==14)iType = IP_CONST_FEAT_ARMOR_PROF_HEAVY;
			if (iRoll==15)iType = IP_CONST_FEAT_ARMOR_PROF_MEDIUM;
			if (iRoll==16)iType = IP_CONST_FEAT_ARMOR_PROF_LIGHT;
			if (iRoll==17)iType = IP_CONST_FEAT_CLEAVE;
			if (iRoll==18)iType = IP_CONST_FEAT_COMBAT_CASTING;
			if (iRoll==19)iType = IP_CONST_FEAT_DODGE;
			if (iRoll==20)iType = IP_CONST_FEAT_POWERATTACK;
			if (iRoll==21)iType = IP_CONST_FEAT_SPELLFOCUSABJ;
			if (iRoll==22)iType = IP_CONST_FEAT_SPELLFOCUSCON;
			if (iRoll==23)iType = IP_CONST_FEAT_SPELLFOCUSDIV;
			if (iRoll==24)iType = IP_CONST_FEAT_SPELLFOCUSENC;
			if (iRoll==25)iType = IP_CONST_FEAT_SPELLFOCUSEVO;
			if (iRoll==26)iType = IP_CONST_FEAT_SPELLFOCUSILL;
			if (iRoll==27)iType = IP_CONST_FEAT_SPELLFOCUSNEC;
			if (iRoll==28)iType = IP_CONST_FEAT_SPELLPENETRATION;
			if (iRoll==29)iType = IP_CONST_FEAT_WEAPON_PROF_EXOTIC;
			if (iRoll==30)iType = IP_CONST_FEAT_WEAPON_PROF_MARTIAL;
			if (iRoll==31)iType = IP_CONST_FEAT_EXTRA_TURNING;
			if (iRoll==32)iType = IP_CONST_FEAT_AMBIDEXTROUS;
			if (iRoll==33)iType = IP_CONST_FEAT_SNEAK_ATTACK_1D6;
			if (iRoll==34)iType = IP_CONST_FEAT_WEAPFINESSE;
			if (iRoll==35)iType = IP_CONST_FEAT_ARMOR_PROF_HEAVY;
		} 
		break;
		case 3: 
		{
			iRoll = Random(35);
			if (iRoll==1)iType = IP_CONST_IMMUNITYSPELL_DOOM;
			if (iRoll==2)iType = IP_CONST_IMMUNITYMISC_DISEASE;
			if (iRoll==3)iType = IP_CONST_IMMUNITYSPELL_CLOUDKILL;
			if (iRoll==4)iType = IP_CONST_IMMUNITYSPELL_GREASE;
			if (iRoll==5)iType = IP_CONST_IMMUNITYMISC_POISON;
			if (iRoll==6)iType = IP_CONST_IMMUNITYSPELL_SLEEP;
			if (iRoll==7)iType = IP_CONST_IMMUNITYSPELL_WEB;
			if (iRoll==8)iType = IP_CONST_IMMUNITYSPELL_RAY_OF_FROST;
			if (iRoll==9)iType = IP_CONST_IMMUNITYSPELL_ENTANGLE;
			if (iRoll==10)iType = IP_CONST_IMMUNITYSPELL_MELFS_ACID_ARROW;
			if (iRoll==11)iType = IP_CONST_IMMUNITYSPELL_SLOW;
			if (iRoll==12)iType = IP_CONST_IMMUNITYSPELL_DOMINATE_PERSON;
			if (iRoll==13)iType = IP_CONST_FEAT_ALERTNESS;
			if (iRoll==14)iType = IP_CONST_FEAT_WEAPON_PROF_EXOTIC;
			if (iRoll==15)iType = IP_CONST_FEAT_WEAPSPEUNARM;
			if (iRoll==16)iType = IP_CONST_FEAT_ARMOR_PROF_LIGHT;
			if (iRoll==17)iType = IP_CONST_FEAT_CLEAVE;
			if (iRoll==18)iType = IP_CONST_FEAT_COMBAT_CASTING;
			if (iRoll==19)iType = IP_CONST_FEAT_WHIRLWIND;
			if (iRoll==20)iType = IP_CONST_FEAT_POWERATTACK;
			if (iRoll==21)iType = IP_CONST_FEAT_SPELLFOCUSABJ;
			if (iRoll==22)iType = IP_CONST_FEAT_SPELLFOCUSCON;
			if (iRoll==23)iType = IP_CONST_FEAT_SPELLFOCUSDIV;
			if (iRoll==24)iType = IP_CONST_FEAT_SPELLFOCUSENC;
			if (iRoll==25)iType = IP_CONST_FEAT_SPELLFOCUSEVO;
			if (iRoll==26)iType = IP_CONST_FEAT_SPELLFOCUSILL;
			if (iRoll==27)iType = IP_CONST_FEAT_SPELLFOCUSNEC;
			if (iRoll==28)iType = IP_CONST_FEAT_SPELLPENETRATION;
			if (iRoll==29)iType = IP_CONST_FEAT_WEAPON_PROF_EXOTIC;
			if (iRoll==30)iType = IP_CONST_FEAT_WEAPON_PROF_MARTIAL;
			if (iRoll==31)iType = IP_CONST_FEAT_EXTRA_TURNING;
			if (iRoll==32)iType = IP_CONST_FEAT_AMBIDEXTROUS;
			if (iRoll==33)iType = 32;
			if (iRoll==34)iType = IP_CONST_FEAT_WEAPFINESSE;
			if (iRoll==35)iType = IP_CONST_FEAT_ARMOR_PROF_HEAVY;
		}
		break;
		case 4: 
		{
			iRoll = Random(35);
			if (iRoll==1)iType = IP_CONST_IMMUNITYMISC_POISON;
			if (iRoll==2)iType = IP_CONST_IMMUNITYMISC_DISEASE;
			if (iRoll==3)iType = IP_CONST_IMMUNITYMISC_FEAR;
			if (iRoll==4)iType = IP_CONST_IMMUNITYMISC_PARALYSIS;
			if (iRoll==5)iType = IP_CONST_IMMUNITYSPELL_FIREBALL;
			if (iRoll==6)iType = IP_CONST_IMMUNITYMISC_DISEASE;
			if (iRoll==7)iType = IP_CONST_IMMUNITYSPELL_HAMMER_OF_THE_GODS;
			if (iRoll==8)iType = IP_CONST_IMMUNITYMISC_LEVEL_ABIL_DRAIN;
			if (iRoll==9)iType = IP_CONST_IMMUNITYSPELL_METEOR_SWARM;
			if (iRoll==10)iType = IP_CONST_IMMUNITYSPELL_POWER_WORD_KILL;
			if (iRoll==11)iType = IP_CONST_IMMUNITYSPELL_PRISMATIC_SPRAY;
			if (iRoll==12)iType = IP_CONST_IMMUNITYSPELL_SILENCE;
			if (iRoll==13)iType = 32;
			if (iRoll==14)iType = IP_CONST_FEAT_WEAPON_PROF_EXOTIC;
			if (iRoll==15)iType = IP_CONST_FEAT_WEAPSPEUNARM;
			if (iRoll==16)iType = IP_CONST_FEAT_POINTBLANK;
			if (iRoll==17)iType = IP_CONST_FEAT_CLEAVE;
			if (iRoll==18)iType = IP_CONST_FEAT_COMBAT_CASTING;
			if (iRoll==19)iType = IP_CONST_FEAT_WHIRLWIND;
			if (iRoll==20)iType = IP_CONST_FEAT_POWERATTACK;
			if (iRoll==21)iType = IP_CONST_FEAT_SPELLFOCUSABJ;
			if (iRoll==22)iType = IP_CONST_FEAT_SPELLFOCUSCON;
			if (iRoll==23)iType = IP_CONST_FEAT_SPELLFOCUSDIV;
			if (iRoll==24)iType = IP_CONST_FEAT_SPELLFOCUSENC;
			if (iRoll==25)iType = IP_CONST_FEAT_SPELLFOCUSEVO;
			if (iRoll==26)iType = IP_CONST_FEAT_SPELLFOCUSILL;
			if (iRoll==27)iType = IP_CONST_FEAT_SPELLFOCUSNEC;
			if (iRoll==28)iType = IP_CONST_FEAT_SPELLPENETRATION;
			if (iRoll==29)iType = IP_CONST_FEAT_WEAPON_PROF_EXOTIC;
			if (iRoll==30)iType = IP_CONST_FEAT_WEAPON_PROF_MARTIAL;
			if (iRoll==31)iType = IP_CONST_FEAT_EXTRA_TURNING;
			if (iRoll==32)iType = IP_CONST_FEAT_AMBIDEXTROUS;
			if (iRoll==33)iType = 32;
			if (iRoll==34)iType = IP_CONST_FEAT_WEAPFINESSE;
			if (iRoll==35)iType = IP_CONST_FEAT_ARMOR_PROF_HEAVY;
		}
		break;
		case 5: 
		{
			iRoll = Random(35);
			if (iRoll==1)iType = IP_CONST_IMMUNITYMISC_POISON;
			if (iRoll==2)iType = IP_CONST_IMMUNITYMISC_DISEASE;
			if (iRoll==3)iType = IP_CONST_IMMUNITYMISC_FEAR;
			if (iRoll==4)iType = IP_CONST_IMMUNITYMISC_PARALYSIS;
			if (iRoll==5)iType = IP_CONST_IMMUNITYSPELL_METEOR_SWARM;
			if (iRoll==6)iType = IP_CONST_IMMUNITYSPELL_HEALING_CIRCLE;
			if (iRoll==7)iType = IP_CONST_IMMUNITYMISC_KNOCKDOWN;
			if (iRoll==8)iType = IP_CONST_IMMUNITYMISC_LEVEL_ABIL_DRAIN;
			if (iRoll==9)iType = IP_CONST_IMMUNITYSPELL_HAMMER_OF_THE_GODS;
			if (iRoll==10)iType = IP_CONST_IMMUNITYSPELL_HEAL;
			if (iRoll==11)iType = IP_CONST_IMMUNITYSPELL_IMPLOSION;
			if (iRoll==12)iType = IP_CONST_IMMUNITYSPELL_HARM;
			if (iRoll==13)iType = IP_CONST_IMMUNITYSPELL_MASS_BLINDNESS_AND_DEAFNESS;
			if (iRoll==14)iType = IP_CONST_IMMUNITYSPELL_METEOR_SWARM;
			if (iRoll==15)iType = IP_CONST_IMMUNITYSPELL_POWER_WORD_KILL;
			if (iRoll==16)iType = IP_CONST_IMMUNITYSPELL_PRISMATIC_SPRAY;
			if (iRoll==17)iType = IP_CONST_IMMUNITYSPELL_STORM_OF_VENGEANCE;
			if (iRoll==18)iType = IP_CONST_IMMUNITYSPELL_WAIL_OF_THE_BANSHEE;
			if (iRoll==19)iType = IP_CONST_IMMUNITYSPELL_WORD_OF_FAITH;
			if (iRoll==20)iType = IP_CONST_FEAT_SNEAK_ATTACK_2D6;
			if (iRoll==21)iType = IP_CONST_FEAT_SPELLFOCUSABJ;
			if (iRoll==22)iType = IP_CONST_FEAT_SPELLFOCUSCON;
			if (iRoll==23)iType = IP_CONST_FEAT_SPELLFOCUSDIV;
			if (iRoll==24)iType = IP_CONST_FEAT_SPELLFOCUSENC;
			if (iRoll==25)iType = IP_CONST_FEAT_SPELLFOCUSEVO;
			if (iRoll==26)iType = IP_CONST_FEAT_SPELLFOCUSILL;
			if (iRoll==27)iType = IP_CONST_FEAT_SPELLFOCUSNEC;
			if (iRoll==28)iType = IP_CONST_FEAT_SPELLPENETRATION;
			if (iRoll==29)iType = IP_CONST_FEAT_WEAPON_PROF_EXOTIC;
			if (iRoll==30)iType = IP_CONST_FEAT_WEAPON_PROF_MARTIAL;
			if (iRoll==31)iType = IP_CONST_FEAT_EXTRA_TURNING;
			if (iRoll==32)iType = IP_CONST_FEAT_AMBIDEXTROUS;
			if (iRoll==33)iType = IP_CONST_FEAT_SNEAK_ATTACK_1D6;
			if (iRoll==34)iType = IP_CONST_FEAT_WEAPFINESSE;
			if (iRoll==35)iType = IP_CONST_FEAT_ARMOR_PROF_HEAVY;
		}
		break;
	}
	
	ipAdd = ItemPropertyImmunityMisc(iType);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void RESimbue(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iRes;
	int iType;
	int iRoll = Random(9)+1;

	switch (iRoll)
	{
		case 1: iType = IP_CONST_DAMAGETYPE_ACID; break;
		case 2: iType = IP_CONST_DAMAGETYPE_BLUDGEONING; break;
		case 3: iType = IP_CONST_DAMAGETYPE_COLD; break;
		case 4: iType = IP_CONST_DAMAGETYPE_ELECTRICAL; break;
		case 5: iType = IP_CONST_DAMAGETYPE_FIRE; break;
		case 6: iType = IP_CONST_DAMAGETYPE_PHYSICAL; break;
		case 7: iType = IP_CONST_DAMAGETYPE_PIERCING; break;
		case 8: iType = IP_CONST_DAMAGETYPE_SLASHING; break;
		case 9: iType = IP_CONST_DAMAGETYPE_SONIC; break;
	}

	switch (iRange)
	{
		case 1:  iRes = IP_CONST_DAMAGERESIST_5; break;
		case 2: 
		{
			iRoll=d4();
			if (iRoll==1)iRes = IP_CONST_DAMAGERESIST_5;
			if (iRoll==2)iRes = IP_CONST_DAMAGERESIST_5;
			if (iRoll==3)iRes = IP_CONST_DAMAGERESIST_5;
			if (iRoll==4)iRes = IP_CONST_DAMAGERESIST_10;
		}
		break;
		case 3: 
		{
			iRoll=d3();
			if (iRoll==1)iRes = IP_CONST_DAMAGERESIST_5;
			if (iRoll==2)iRes = IP_CONST_DAMAGERESIST_5;
			if (iRoll==3)iRes = IP_CONST_DAMAGERESIST_10;
		}
		break;
		case 4: 
		{
			iRoll=d4();
			if (iRoll==1)iRes = IP_CONST_DAMAGERESIST_5;
			if (iRoll==2)iRes = IP_CONST_DAMAGERESIST_10;
			if (iRoll==3)iRes = IP_CONST_DAMAGERESIST_10;
			if (iRoll==4)iRes = IP_CONST_DAMAGERESIST_10;
		}
		break;
		case 5: 
		{
			iRoll=d4();
			if (iRoll==1)iRes = IP_CONST_DAMAGERESIST_10;
			if (iRoll==2)iRes = IP_CONST_DAMAGERESIST_10;
			if (iRoll==3)iRes = IP_CONST_DAMAGERESIST_10;
			if (iRoll==4)iRes = IP_CONST_DAMAGERESIST_10;
		}
		break;
	}
	ipAdd = ItemPropertyDamageResistance(iType, iRes);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void AbilityImbue(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iAbil;
	int iType;
	int iRoll = d6();

	switch (iRoll)
	{
		case 1: iType = ABILITY_DEXTERITY; break;
		case 2: iType = ABILITY_INTELLIGENCE; break;
		case 3: iType = ABILITY_WISDOM; break;
		case 4: iType = ABILITY_CHARISMA; break;
		case 5: iType = ABILITY_CONSTITUTION; break;
		case 6: iType = ABILITY_STRENGTH; break;
	}

	switch (iRange)
	{
		case 1: {iAbil = d3();  if (iAbil==3)iAbil=1; break;}  // 1-2
		case 2: {iAbil = d3();if (iAbil==4)iAbil=1; break;}  // 2-3
		case 3: {iAbil = d3();if (iAbil==4)iAbil=1; break;}  // 3-4
		case 4: {iAbil = d3()+1;if (iAbil==5)iAbil=2; break;}  // 4-5
		case 5: {iAbil = d3()+1;if (iAbil==6)iAbil=2; break;}  // 5-6
	}
	
	ipAdd = ItemPropertyAbilityBonus(iType, iAbil);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void MiscImbue(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iRoll;
	
	switch (iRange)
	{
		case 1: 
		{
			iRoll = Random(43);

			if (iRoll==1)ipAdd = ItemPropertyLight(IP_CONST_LIGHTBRIGHTNESS_NORMAL, IP_CONST_LIGHTCOLOR_WHITE);
			if (iRoll==2)ipAdd = ItemPropertyLight(IP_CONST_LIGHTBRIGHTNESS_NORMAL, IP_CONST_LIGHTCOLOR_BLUE);
			if (iRoll==3)ipAdd = ItemPropertyBonusSavingThrow(IP_CONST_SAVEBASETYPE_FORTITUDE, 2);
			if (iRoll==4)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLEEP);
			if (iRoll==5)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WEB);
			if (iRoll==6)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_RAY_OF_FROST);
			if (iRoll==7)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_GREASE);
			if (iRoll==8)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_COLOR_SPRAY);
			if (iRoll==9)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_DAZE);
			if (iRoll==10)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BURNING_HANDS);
			if (iRoll==11)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BLINDNESS_AND_DEAFNESS);
			if (iRoll==12)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_LIGHTNING_BOLT);
			if (iRoll==13)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_MAGIC_MISSILE);
			if (iRoll==14)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_DODGE);
			if (iRoll==15)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSABJ);
			if (iRoll==16)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSCON);
			if (iRoll==17)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSDIV);
			if (iRoll==18)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSENC);
			if (iRoll==19)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSEVO);
			if (iRoll==20)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSILL);
			if (iRoll==21)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSNEC);
			if (iRoll==22)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLPENETRATION);
			if (iRoll==23)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_EXOTIC);
			if (iRoll==24)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_MARTIAL);
			if (iRoll==25)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_EXTRA_TURNING);
			if (iRoll==26)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_AMBIDEXTROUS);
			if (iRoll==27)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SNEAK_ATTACK_1D6);
			if (iRoll==28)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPFINESSE);
			if (iRoll==29)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_ARMOR_PROF_HEAVY);
			if (iRoll==30)ipAdd = ItemPropertyBonusFeat(32);
			if (iRoll==31)ipAdd = ItemPropertyArcaneSpellFailure(IP_CONST_ARCANE_SPELL_FAILURE_MINUS_5_PERCENT);
			if (iRoll==32)ipAdd = ItemPropertyArcaneSpellFailure(IP_CONST_ARCANE_SPELL_FAILURE_MINUS_10_PERCENT);
			if (iRoll==33)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 2);
			if (iRoll==34)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 2);
			if (iRoll==35)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 2);
			if (iRoll==36)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 2);
			if (iRoll==37)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 2);
			if (iRoll==38)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 3);
			if (iRoll==39)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 3);
			if (iRoll==40)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 3);
			if (iRoll==41)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 3);
			if (iRoll==42)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 3);
			if (iRoll==43)ipAdd = ItemPropertyImmunityToSpellLevel(1);
		}
		break;
		case 2: 
		{
			iRoll = Random(43);
			if (iRoll==1)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_HOLD_PERSON);
			if (iRoll==2)ipAdd = ItemPropertyBonusSavingThrow(IP_CONST_SAVEBASETYPE_REFLEX, 2);
			if (iRoll==3)ipAdd = ItemPropertyBonusSavingThrow(IP_CONST_SAVEBASETYPE_FORTITUDE, 2);
			if (iRoll==4)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLEEP);
			if (iRoll==5)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WEB);
			if (iRoll==6)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_RAY_OF_FROST);
			if (iRoll==7)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_GREASE);
			if (iRoll==8)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_COLOR_SPRAY);
			if (iRoll==9)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_DAZE);
			if (iRoll==10)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BURNING_HANDS);
			if (iRoll==11)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BLINDNESS_AND_DEAFNESS);
			if (iRoll==12)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_LIGHTNING_BOLT);
			if (iRoll==13)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_MAGIC_MISSILE);
			if (iRoll==14)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_DODGE);
			if (iRoll==15)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSABJ);
			if (iRoll==16)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSCON);
			if (iRoll==17)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSDIV);
			if (iRoll==18)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSENC);
			if (iRoll==19)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSEVO);
			if (iRoll==20)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSILL);
			if (iRoll==21)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSNEC);
			if (iRoll==22)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLPENETRATION);
			if (iRoll==23)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_EXOTIC);
			if (iRoll==24)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_MARTIAL);
			if (iRoll==25)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_EXTRA_TURNING);
			if (iRoll==26)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_AMBIDEXTROUS);
			if (iRoll==27)ipAdd = ItemPropertyBonusFeat(32);
			if (iRoll==28)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPFINESSE);
			if (iRoll==29)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_ARMOR_PROF_HEAVY);
			if (iRoll==30)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_POWERATTACK);
			if (iRoll==31)ipAdd = ItemPropertyArcaneSpellFailure(IP_CONST_ARCANE_SPELL_FAILURE_MINUS_10_PERCENT);
			if (iRoll==32)ipAdd = ItemPropertyArcaneSpellFailure(IP_CONST_ARCANE_SPELL_FAILURE_MINUS_10_PERCENT);
			if (iRoll==33)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 3);
			if (iRoll==34)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 3);
			if (iRoll==35)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 3);
			if (iRoll==36)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 3);
			if (iRoll==37)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 3);
			if (iRoll==38)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 4);
			if (iRoll==39)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 4);
			if (iRoll==40)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 4);
			if (iRoll==41)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 4);
			if (iRoll==42)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 4);
			if (iRoll==43)ipAdd = ItemPropertyImmunityToSpellLevel(1);
		}
		break;
		case 3: 
		{
			iRoll = Random(56);
			if (iRoll==1)ipAdd = ItemPropertyDarkvision();
			if (iRoll==2)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_POWER_WORD_STUN);
			if (iRoll==3)ipAdd = ItemPropertyVampiricRegeneration(1);
			if (iRoll==4)ipAdd = ItemPropertyBonusSavingThrow(IP_CONST_SAVEBASETYPE_FORTITUDE, 2);
			if (iRoll==5)ipAdd = ItemPropertyRegeneration(1);
			if (iRoll==6)ipAdd = ItemPropertyDarkvision();
			if (iRoll==7)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_CLOUDKILL);
			if (iRoll==8)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_GREASE);
			if (iRoll==9)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_FLAME_ARROW);
			if (iRoll==10)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLEEP);
			if (iRoll==11)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WEB);
			if (iRoll==12)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_RAY_OF_FROST);
			if (iRoll==13)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_ENTANGLE);
			if (iRoll==14)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_MELFS_ACID_ARROW);
			if (iRoll==15)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLOW);
			if (iRoll==16)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_DOMINATE_PERSON);
			if (iRoll==17)ipAdd = ItemPropertyBonusFeat(32);
			if (iRoll==18)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSABJ);
			if (iRoll==19)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSCON);
			if (iRoll==20)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSDIV);
			if (iRoll==21)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSENC);
			if (iRoll==22)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSEVO);
			if (iRoll==23)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSILL);
			if (iRoll==24)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSNEC);
			if (iRoll==25)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLPENETRATION);
			if (iRoll==26)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_EXOTIC);
			if (iRoll==27)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_MARTIAL);
			if (iRoll==28)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_EXTRA_TURNING);
			if (iRoll==29)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_AMBIDEXTROUS);
			if (iRoll==30)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_EXTRA_TURNING);
			if (iRoll==31)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPFINESSE);
			if (iRoll==32)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_ARMOR_PROF_HEAVY);
			if (iRoll==33)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_KNOCKDOWN);
			if (iRoll==34)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WHIRLWIND);
			if (iRoll==35)ipAdd = ItemPropertyArcaneSpellFailure(IP_CONST_ARCANE_SPELL_FAILURE_MINUS_15_PERCENT);
			if (iRoll==36)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLEEP);
			if (iRoll==37)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WEB);
			if (iRoll==38)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_RAY_OF_FROST);
			if (iRoll==39)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_GREASE);
			if (iRoll==40)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_COLOR_SPRAY);
			if (iRoll==41)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_DAZE);
			if (iRoll==42)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BURNING_HANDS);
			if (iRoll==43)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BLINDNESS_AND_DEAFNESS);
			if (iRoll==44)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_LIGHTNING_BOLT);
			if (iRoll==45)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_MAGIC_MISSILE);
			if (iRoll==46)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 4);
			if (iRoll==47)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 4);
			if (iRoll==48)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 4);
			if (iRoll==49)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 4);
			if (iRoll==50)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 4);
			if (iRoll==51)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 5);
			if (iRoll==52)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 5);
			if (iRoll==53)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 5);
			if (iRoll==54)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 5);
			if (iRoll==55)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 5);
			if (iRoll==56)ipAdd = ItemPropertyImmunityToSpellLevel(1);
		}
		break;
		case 4: 
		{
			iRoll = Random(72);
			if (iRoll==1)ipAdd = ItemPropertyDarkvision();
			if (iRoll==2)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_POWER_WORD_KILL);
			if (iRoll==3)ipAdd = ItemPropertyVampiricRegeneration(3);
			if (iRoll==4)ipAdd = ItemPropertyBonusSavingThrow(IP_CONST_SAVEBASETYPE_FORTITUDE, 3);
			if (iRoll==5)ipAdd = ItemPropertyRegeneration(1);
			if (iRoll==6)ipAdd = ItemPropertyBonusSavingThrow(IP_CONST_SAVEBASETYPE_FORTITUDE, 2);
			if (iRoll==7)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_FIREBALL);
			if (iRoll==8)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_DOOM);
			if (iRoll==9)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_HAMMER_OF_THE_GODS);
			if (iRoll==10)ipAdd = ItemPropertyBonusSavingThrow(IP_CONST_SAVEBASETYPE_FORTITUDE, 1);
			if (iRoll==11)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_METEOR_SWARM);
			if (iRoll==12)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_POWER_WORD_KILL);
			if (iRoll==13)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_PRISMATIC_SPRAY);
			if (iRoll==14)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SILENCE);
			if (iRoll==15)ipAdd = ItemPropertyBonusFeat(33);
			if (iRoll==16)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSABJ);
			if (iRoll==17)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSCON);
			if (iRoll==18)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSDIV);
			if (iRoll==19)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSENC);
			if (iRoll==20)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSEVO);
			if (iRoll==21)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSILL);
			if (iRoll==22)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSNEC);
			if (iRoll==23)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLPENETRATION);
			if (iRoll==24)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_EXOTIC);
			if (iRoll==25)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_MARTIAL);
			if (iRoll==26)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_EXTRA_TURNING);
			if (iRoll==27)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_AMBIDEXTROUS);
			if (iRoll==28)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_EXTRA_TURNING);
			if (iRoll==29)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPFINESSE);
			if (iRoll==30)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_ARMOR_PROF_HEAVY);
			if (iRoll==31)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_KNOCKDOWN);
			if (iRoll==32)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_RAPID_SHOT);
			if (iRoll==33)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SHIELD_PROFICIENCY);
			if (iRoll==34)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WHIRLWIND);
			if (iRoll==35)ipAdd = ItemPropertyArcaneSpellFailure(IP_CONST_ARCANE_SPELL_FAILURE_MINUS_20_PERCENT);
			if (iRoll==36)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLEEP);
			if (iRoll==37)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WEB);
			if (iRoll==38)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_RAY_OF_FROST);
			if (iRoll==39)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_GREASE);
			if (iRoll==40)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_COLOR_SPRAY);
			if (iRoll==41)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_DAZE);
			if (iRoll==42)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BURNING_HANDS);
			if (iRoll==43)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BLINDNESS_AND_DEAFNESS);
			if (iRoll==44)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_LIGHTNING_BOLT);
			if (iRoll==45)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_MAGIC_MISSILE);
			if (iRoll==46)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_CLOUDKILL);
			if (iRoll==47)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_GREASE);
			if (iRoll==48)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_FLAME_ARROW);
			if (iRoll==49)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLEEP);
			if (iRoll==50)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WEB);
			if (iRoll==51)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_RAY_OF_FROST);
			if (iRoll==52)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_ENTANGLE);
			if (iRoll==53)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_MELFS_ACID_ARROW);
			if (iRoll==54)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLOW);
			if (iRoll==55)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_DOMINATE_PERSON);
			if (iRoll==56)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_ENERVATION);
			if (iRoll==57)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_ENERGY_DRAIN);
			if (iRoll==58)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_FINGER_OF_DEATH);
			if (iRoll==59)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 5);
			if (iRoll==60)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 5);
			if (iRoll==61)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 5);
			if (iRoll==62)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 5);
			if (iRoll==63)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 5);
			if (iRoll==64)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 6);
			if (iRoll==65)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 6);
			if (iRoll==66)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 6);
			if (iRoll==67)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 6);
			if (iRoll==68)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 6);
			if (iRoll==69)ipAdd = ItemPropertyImmunityToSpellLevel(2);
			if (iRoll==70)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 7);
			if (iRoll==71)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 7);
			if (iRoll==72)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 7);
			if (iRoll==73)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 7);
		}
		break;
		case 5: 
		{
			iRoll = Random(86);
			if (iRoll==1)ipAdd = ItemPropertyDarkvision();
			if (iRoll==2)ipAdd = ItemPropertyBonusSavingThrow(IP_CONST_SAVEBASETYPE_REFLEX, 3);
			if (iRoll==3)ipAdd = ItemPropertyVampiricRegeneration(2);
			if (iRoll==4)ipAdd = ItemPropertyBonusSavingThrow(IP_CONST_SAVEBASETYPE_FORTITUDE, 3);
			if (iRoll==5)ipAdd = ItemPropertyRegeneration(2);
			if (iRoll==6)ipAdd = ItemPropertyRegeneration(1);
			if (iRoll==7)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_HAMMER_OF_THE_GODS);
			if (iRoll==8)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_HEAL);
			if (iRoll==9)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_IMPLOSION);
			if (iRoll==10)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_HARM);
			if (iRoll==11)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_MASS_BLINDNESS_AND_DEAFNESS);
			if (iRoll==12)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_METEOR_SWARM);
			if (iRoll==13)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_POWER_WORD_KILL);
			if (iRoll==14)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_PRISMATIC_SPRAY);
			if (iRoll==15)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_STORM_OF_VENGEANCE);
			if (iRoll==16)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WAIL_OF_THE_BANSHEE);
			if (iRoll==17)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WORD_OF_FAITH);
			if (iRoll==18)ipAdd = ItemPropertyBonusFeat(33);
			if (iRoll==19)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSABJ);
			if (iRoll==20)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSCON);
			if (iRoll==21)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSDIV);
			if (iRoll==22)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSENC);
			if (iRoll==23)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSEVO);
			if (iRoll==24)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSILL);
			if (iRoll==25)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLFOCUSNEC);
			if (iRoll==26)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SPELLPENETRATION);
			if (iRoll==27)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_EXOTIC);
			if (iRoll==28)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPON_PROF_MARTIAL);
			if (iRoll==29)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_EXTRA_TURNING);
			if (iRoll==30)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_AMBIDEXTROUS);
			if (iRoll==31)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_EXTRA_TURNING);
			if (iRoll==32)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WEAPFINESSE);
			if (iRoll==33)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_ARMOR_PROF_HEAVY);
			if (iRoll==34)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_KNOCKDOWN);
			if (iRoll==35)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_RAPID_SHOT);
			if (iRoll==36)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_SHIELD_PROFICIENCY);
			if (iRoll==37)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_WHIRLWIND);
			if (iRoll==38)ipAdd = ItemPropertyBonusFeat(IP_CONST_FEAT_TWO_WEAPON_FIGHTING);
			if (iRoll==39)ipAdd = ItemPropertyArcaneSpellFailure(IP_CONST_ARCANE_SPELL_FAILURE_MINUS_30_PERCENT);
			if (iRoll==40)ipAdd = ItemPropertyArcaneSpellFailure(IP_CONST_ARCANE_SPELL_FAILURE_MINUS_35_PERCENT);
			if (iRoll==41)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLEEP);
			if (iRoll==42)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WEB);
			if (iRoll==43)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_RAY_OF_FROST);
			if (iRoll==44)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_GREASE);
			if (iRoll==45)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_COLOR_SPRAY);
			if (iRoll==46)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_DAZE);
			if (iRoll==47)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BURNING_HANDS);
			if (iRoll==48)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_BLINDNESS_AND_DEAFNESS);
			if (iRoll==49)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_LIGHTNING_BOLT);
			if (iRoll==50)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_MAGIC_MISSILE);
			if (iRoll==51)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_CLOUDKILL);
			if (iRoll==52)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_GREASE);
			if (iRoll==53)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_FLAME_ARROW);
			if (iRoll==54)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLEEP);
			if (iRoll==55)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_WEB);
			if (iRoll==56)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_RAY_OF_FROST);
			if (iRoll==57)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_ENTANGLE);
			if (iRoll==58)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_MELFS_ACID_ARROW);
			if (iRoll==59)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_SLOW);
			if (iRoll==60)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_DOMINATE_PERSON);
			if (iRoll==61)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_ENERVATION);
			if (iRoll==62)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_ENERGY_DRAIN);
			if (iRoll==63)ipAdd = ItemPropertySpellImmunitySpecific(IP_CONST_IMMUNITYSPELL_FINGER_OF_DEATH);
			if (iRoll==64)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 6);
			if (iRoll==65)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 6);
			if (iRoll==66)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 6);
			if (iRoll==67)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 6);
			if (iRoll==68)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 6);
			if (iRoll==69)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 7);
			if (iRoll==70)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 7);
			if (iRoll==71)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 7);
			if (iRoll==72)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 7);
			if (iRoll==73)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 7);
			if (iRoll==74)ipAdd = ItemPropertyImmunityToSpellLevel(2);
			if (iRoll==75)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 8);
			if (iRoll==76)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 8);
			if (iRoll==77)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 8);
			if (iRoll==78)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 8);
			if (iRoll==79)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 6);
			if (iRoll==80)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_WIZARD, 9);
			if (iRoll==81)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_CLERIC, 9);
			if (iRoll==82)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_DRUID, 9);
			if (iRoll==83)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_SORCERER, 9);
			if (iRoll==84)ipAdd = ItemPropertyBonusLevelSpell(IP_CONST_CLASS_BARD, 7);
		}
		break;
	}
	
	IPSafeAddItemProperty(oItem, ipAdd);
}

void SaveImbue(object oItem, int iRange)
{
	itemproperty ipAdd;
	int iAbil;
	int iType;
	int iRoll = d6();
	
	switch (iRoll)
	{
		case 1: iType = IP_CONST_SAVEBASETYPE_FORTITUDE; break;
		case 2: iType = IP_CONST_SAVEBASETYPE_REFLEX; break;
		case 3: iType = IP_CONST_SAVEBASETYPE_WILL; break;
		case 4: iType = IP_CONST_SAVEBASETYPE_FORTITUDE; break;
		case 5: iType = IP_CONST_SAVEBASETYPE_REFLEX; break;
		case 6: iType = IP_CONST_SAVEBASETYPE_WILL; break;
	}

	switch (iRange)
	{
		case 1: {iAbil = d3();  if (iAbil==3)iAbil=1; break;}  // 1-2
		case 2: {iAbil = d3();if (iAbil==4)iAbil=1; break;}  // 2-3
		case 3: {iAbil = d3();if (iAbil==5)iAbil=1; break;}  // 3-4
		case 4: {iAbil = d3();if (iAbil==6)iAbil=2; break;}  // 4-5
		case 5: {iAbil = d3()+1;if (iAbil==7)iAbil=2; break;}  // 5-6
	}
	
	ipAdd = ItemPropertyBonusSavingThrow(iType, iAbil);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void ImpEvasionImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyImprovedEvasion();
	IPSafeAddItemProperty(oItem, ipAdd);
}

void TruseeingImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyTrueSeeing();
	IPSafeAddItemProperty(oItem, ipAdd);
}

void DarkvisionImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyDarkvision();
	IPSafeAddItemProperty(oItem, ipAdd);
}

void FreedomImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyFreeAction();
	IPSafeAddItemProperty(oItem, ipAdd);
}

void RegenImbue(object oItem, int iRange)
{
	int iRegen;
	itemproperty ipAdd;

	switch (iRange)
	{
		case 1: {iRegen = d2();if (iRegen==3)iRegen=1; break;}   //1-2
		case 2: iRegen = d2(); break;                            //1-3
		case 3: iRegen = d2(); break;                          //1-4
		case 4: {iRegen = d2();if (iRegen==5)iRegen=2;break;}  //2-4
		case 5: iRegen = d2();break;                           //2-5
	}
	
	ipAdd = ItemPropertyRegeneration(iRegen);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void VampRegenImbue(object oItem, int iRange)
{
	int iRegen;
	itemproperty ipAdd;
	
	switch (iRange)
	{
		case 1: {iRegen = d3();if (iRegen==3)iRegen=1; break;}   //1-2
		case 2: iRegen = d3(); break;                            //1-3
		case 3: iRegen = d3()+1; break;                          //1-4
		case 4: {iRegen = d3()+1;if (iRegen==5)iRegen=3;break;}  //2-4
		case 5: iRegen = d3()+2;break;                           //2-5
	}
	
	ipAdd = ItemPropertyVampiricRegeneration(iRegen);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void EvilImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyVisualEffect(ITEM_VISUAL_EVIL);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void HolyImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyVisualEffect(ITEM_VISUAL_HOLY);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void FireImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyVisualEffect(ITEM_VISUAL_FIRE);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void ElecImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyVisualEffect(ITEM_VISUAL_ELECTRICAL);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void AcidImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyVisualEffect(ITEM_VISUAL_ACID);
	IPSafeAddItemProperty(oItem, ipAdd);
}

void HasteImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyHaste();
	IPSafeAddItemProperty(oItem, ipAdd);
}

void KeenImbue(object oItem)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyKeen();
	IPSafeAddItemProperty(oItem, ipAdd);	
}

void WoundingImbue(object oItem, int iRange)
{
	itemproperty ipAdd;
	ipAdd = ItemPropertyOnHitProps(IP_CONST_ONHIT_WOUNDING, iRange-1, iRange);
	IPSafeAddItemProperty(oItem, ipAdd);	
}

void EchobladeImbue(object oItem)	//:: PRC8
{
	if(!PRCGetBludgeoningWeapon(oItem))
	{
		itemproperty ipAdd;
		ipAdd = ItemPropertyEchoblade();
		IPSafeAddItemProperty(oItem, ipAdd);
	}
}

void DropArmor (object oMob, object oSack, int iRange, int SockChance, int iChest)
{
	object oItem;
	itemproperty ipAdd;
	string sType, sIName, sName, sSocks, AbilName, ResName, HasteName;
	int iQual = 0;
	int iAbilQual = 0;
	int iResQual = 0;
	int iHasteQual = 0;
	int iRoll = d10();
	
	switch(iRoll)
	{
		case 1: sType = "sdarmor8"; break;
		case 2: sType = "sdarmor7"; break;
		case 3: sType = "sdarmor6"; break;
		case 4: sType = "sdarmor5"; break;
		case 5: sType = "sdarmor4"; break;
		case 6: sType = "sdarmor3"; break;
		case 7: sType = "sdarmor2"; break;
		case 8: sType = "sdarmor1"; break;
		case 9: sType = "sdarmor0"; break;
		case 10: sType = "sdarmor02"; break;
	}
	oItem = CreateItemOnObject(sType, oSack, 1, "sf_socket_item");
	
	// chance for socketed item
	iRoll = d100();
	if (iRoll < SockChance)
	{
		iRoll=d6();
		SetLocalInt(oItem, "SOCKETS", iRoll);
		sSocks = IntToString(iRoll);
		ipAdd = ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER, IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE);
		IPSafeAddItemProperty(oItem, ipAdd);

		sName = ColorString("Socketed "+sIName+" ("+sSocks+")", 72, 209, 204 );
		SetName(oItem, sName);
		return;
	}
	iRoll = d100();
	if (iRoll <= CHANCE_WORN && iRoll > CHANCE_BROKEN && iChest != 1)  // chance of being worn
	{
		sName = ColorString("Worn "+sIName, 192, 192, 192);
		SetName(oItem, sName);
		SetIdentified(oItem, TRUE);
		return;
	}
	if (iRoll<=CHANCE_BROKEN&&iChest!=1)             // chance of being broken
	{
		DelayCommand(0.2, ACimbue(oItem, iRange, 1));
		sName = ColorString("Broken "+sIName, 255, 0, 0);
		SetName(oItem, sName);
		SetIdentified(oItem, TRUE);
		return;
	}

	SetIdentified(oItem, FALSE);
//////////////////////////////////////////// Lvls 1-5

//:: AC bonus
	DelayCommand(0.2, ACimbue(oItem, iRange, 0));
	++iQual;

//:: Ability bonus
	iRoll = d100();
	if (iRoll>90)
	{
		DelayCommand(0.2, AbilityImbue(oItem, iRange));
		++iQual;
		++iAbilQual;
	}

////////////////////////////////////////// Lvls 6-10

	if (iRange==2)
	{
	//:: Ability bonus
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
			++iAbilQual;
		}
	//:: Misc
		iRoll = d100();
		if (iRoll>90)
		{
			DelayCommand(0.2,MiscImbue(oItem, iRange));
			++iQual;
		}
	//:: Damage Res bonus
		iRoll = d100();
		if (iRoll > 85)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
			++iResQual;
		}
		iRoll = d100();
		if (iRoll > 97)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
	}

////////////////////////////////////////// Lvls 11-20

	if (iRange==3)
	{
	//:: Ability bonus
		iRoll = d100();
		if (iRoll>40)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
			++iAbilQual;
		}
	//:: Damage Res bonus
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
			++iResQual;
		}
	//:: Damage Res bonus
		iRoll = d100();
		if (iRoll>89)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
			++iResQual;
		}
	//::: Misc
		iRoll = d100();
		if (iRoll>75)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}

		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}

		// Misc Immunity
		//     iRoll = d100();
		//     if (iRoll>90)
		//       {
		//        DelayCommand(0.2, MIMMimbue(oItem, iRange));
		//        ++iQual;
		//       }
	//:: Haste
		iRoll = d100();
		if (iRoll>95)
		{
			DelayCommand(0.2, HasteImbue(oItem));
			++iQual;
			++iHasteQual;
		}
	}
	
////////////////////////////////////////// Lvls 20-30

	if (iRange==4)
	{
	//:: Ability bonus x 2
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
			++iAbilQual;
		}

		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
			++iAbilQual;
		}
	//:: Damage Res bonus x 2
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
			++iResQual;
		}
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
			++iResQual;
		}
	//:: Misc
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>90)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>95)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}

		// Misc Immunity
		//     iRoll = d100();
		//     if (iRoll>75)
		//       {
		//        DelayCommand(0.2, MIMMimbue(oItem, iRange));
		//        ++iQual;
		//       }
		
	//:: Haste
		iRoll = d100();
		if (iRoll>95)
		{
			DelayCommand(0.2, HasteImbue(oItem));		
			++iQual;
			++iHasteQual;
		}
	}

////////////////////////////////////////// Lvls 30-40

	if (iRange==5)
	{
	//:: Ability bonus x 2
		iRoll = d100();
		if (iRoll > 50)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
			++iAbilQual;
		}

		iRoll = d100();
		if (iRoll > 60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
			++iAbilQual;
		}


		if (iRoll > 70)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
			++iAbilQual;
		}

		if (iRoll > 85)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
			++iAbilQual;
		}

	//:: Damage Res bonus x 3
		iRoll = d100();
		if (iRoll > 60)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
			++iResQual;
		}
		iRoll = d100();
		if (iRoll > 70)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
			++iResQual;
		}
		iRoll = d100();
		if (iRoll > 80)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
			++iResQual;
		}
		iRoll = d100();
		if (iRoll > 90)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
			++iResQual;
		}

	//:: Misc
		iRoll = d100();
		if (iRoll > 70)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll > 75)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll > 80)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>90)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
		// Misc Immunity
		//     iRoll = d100();
		//    if (iRoll>70)
		//       {
		//       DelayCommand(0.2, MIMMimbue(oItem, iRange));
		//       ++iQual;
		}

		//    iRoll = d100();
		//    if (iRoll>75)
		//       {
		//        DelayCommand(0.2, MIMMimbue(oItem, iRange));
		//        ++iQual;
		//       }

		//    iRoll = d100();
		//     if (iRoll>90)
		//      {
		//       DelayCommand(0.2, MIMMimbue(oItem, iRange));
		//       ++iQual;
		//      }


		//     iRoll = d100();
		//    if (iRoll>95)
		//      {
		//       DelayCommand(0.2, MIMMimbue(oItem, iRange));
		//       ++iQual;
		//      }
	//:: Haste
		iRoll = d100();
		if (iRoll>90)
		{
			DelayCommand(0.2, HasteImbue(oItem));
			++iQual;
			++iHasteQual;
		}

		DelayCommand(0.4, ChangeArmor(oItem, iRandomShoulder, iRandomBicep, iRandomForearm, iRandomHand, iRandomThigh, iRandomShins, iRandomFeet, iRandomPelvis, iRandomBelt, iRandomNeck, iRandomChest, iRandomArmor1, iRandomArmor2, iRandomArmor3, iRandomArmor4, iRandomArmor5, iRandomArmor6, oSack));


	switch(iAbilQual)
	{
		case 1: AbilName = ColorString(" Neophyte", 1, 255, 1);break;
		case 2: AbilName = ColorString(" Journeyman", 1, 255, 1);break;
		case 3: AbilName = ColorString(" Seraphim", 1, 255, 255);break;
		case 4: AbilName = ColorString(" Archon", 1, 255, 255);break;
		case 5: AbilName = ColorString(" Paragon", 1, 255, 255);break;
	}
	switch(iResQual)
	{
		case 0: ResName = ColorString(" Guarded", 255, 255, 255);break;
		case 1: ResName = ColorString(" Stout", 1, 255, 1);break;
		case 2: ResName = ColorString(" Resilient", 1, 255, 1);break;
		case 3: ResName = ColorString(" Sentinel", 1, 255, 255);break;
		case 4: ResName = ColorString(" Inpenetrable", 1, 255, 255);break;
	}
	switch(iHasteQual)
	{
		case 1: HasteName = "Swift ";    break;
	}
	switch(iQual)
	{
		case 1: sName = ColorString(HasteName + "Magical Armour ",255, 255, 255) + "of the" + ResName + AbilName; break;
		case 2: sName = ColorString(HasteName + "Enchanted Armour ", 30, 180, 30)+ "of the" + ResName + AbilName; break;
		case 3: sName = ColorString(HasteName + "Enchanted Armour ", 30, 180, 30)+ "of the" + ResName + AbilName; break;
		case 4: sName = ColorString(HasteName + "Imbued Armour ", 5, 90, 255)+ "of the" + ResName + AbilName; break;
		case 5: sName = ColorString(HasteName + "Imbued Armour ", 5, 90, 255)+ "of the" + ResName + AbilName; break;
		case 6: sName = ColorString(HasteName + "Planar Armour ", 185, 1, 200)+ "of the" + ResName + AbilName; break;
		case 7: sName = ColorString(HasteName + "Planar Armour ", 185, 1, 200)+ "of the" + ResName + AbilName; break;
		case 8: sName = ColorString(HasteName + "Divine Armour ", 255, 245, 210)+ "of the" + ResName + AbilName; break;
		case 9: sName = ColorString(HasteName + "Divine Armour ", 255, 245, 210)+ "of the" + ResName + AbilName; break;
		case 10: sName = ColorString(HasteName + "Ancient Armour ", 180, 180, 80)+ "of the" + ResName + AbilName; break;
	}

	DelayCommand(0.4,SetName(oItem, sName));
}

void DropShield (object oMob, object oSack, int iRange, int SockChance, int iChest)
{
	object oItem;
	itemproperty ipAdd;
	string sType, sName, sIName, sSocks;
	int iQual = 0;
	int iRoll = d20();
	
	switch(iRoll)
	{
		case 1: {sType = "sdTower1"; sIName = "Targe";} break;
		case 2: {sType = "sdLarge1"; sIName = "Wooden Shield";} break;
		case 3: {sType = "sdSmall1"; sIName = "Buckler";} break;
		case 4: {sType = "sdTower2"; sIName = "Deflector";} break;
		case 5: {sType = "sdLarge2"; sIName = "Protector";} break;
		case 6: {sType = "sdSmall2"; sIName = "Heater";} break;
		case 7: {sType = "sdTower3"; sIName = "Defender";} break;
		case 8: {sType = "sdLarge3"; sIName = "Reinforced Shield";} break;
		case 9: {sType = "sdSmall3"; sIName = "Arm Blocker";} break;
		case 10: {sType = "sdTower4"; sIName = "Centurion";} break;
		case 11: {sType = "sdLarge4"; sIName = "Iron Shield";} break;
		case 12: {sType = "sdLarge5"; sIName = "Medium Shield";} break;
		case 13: {sType = "sdLarge6"; sIName = "Vanguards Brace";} break;
		case 14: {sType = "sdLarge7"; sIName = "Protector";} break;
		case 15: {sType = "sdLarge8"; sIName = "Kite Shield";} break;
		case 16: {sType = "sdLarge9"; sIName = "Protector";} break;
		case 17: {sType = "sdLarge10"; sIName = "Kite Shield";} break;
		case 18: {sType = "sdSmall3"; sIName = "Buckler";} break;
		case 19: {sType = "sdTower2"; sIName = "Gothic Shield";} break;
		case 20: {sType = "sdSmall1"; sIName = "Heater";} break;
	}

	oItem = CreateItemOnObject(sType, oSack, 1, "sf_socket_item");

	// chance for socketed item

	iRoll = d100();
	if (iRoll < SockChance)
	{
		iRoll=d6();
		SetLocalInt(oItem, "SOCKETS", iRoll);
		sSocks = IntToString(iRoll);
		ipAdd = ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER, IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE);
		IPSafeAddItemProperty(oItem, ipAdd);

		sName = ColorString("Socketed "+sIName+" ("+sSocks+")", 72, 209, 204 );
		SetName(oItem, sName);
		return;
	}
	iRoll = d100();
	if (iRoll <= CHANCE_WORN && iRoll > CHANCE_BROKEN && iChest != 1)	// chance of being worn
	{
		sName = ColorString("Worn "+sIName, 192, 192, 192);
		SetName(oItem, sName);
		SetIdentified(oItem, TRUE);
		return;
	}
	if (iRoll <= CHANCE_BROKEN && iChest != 1)	// chance of being broken
	{
		DelayCommand(0.2, ACimbue(oItem, iRange, 1));
		sName = ColorString("Broken "+sIName, 255, 0, 0);
		SetName(oItem, sName);
		SetIdentified(oItem, TRUE);
		return;
	}

	SetIdentified(oItem, FALSE);

//////////////////////////////////////////// Lvls 1-5

//:: Ac bonus
	DelayCommand(0.2, ACmisc(oItem, iRange));
	++iQual;

////////////////////////////////////////// Lvls 6-10

	if (iRange==2)
	{
		//:: Ability bonus
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
		// Save
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}
	}

////////////////////////////////////////// Lvls 11-20

	if (iRange==3)
	{
	//:: Ability bonus
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
	//:: Damage Res bonus
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>90)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
		}
	//:: Saves
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}
	}

////////////////////////////////////////// Lvls 20-30

	if (iRange==4)
	{
	//:: Ability bonus
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
	//:: Damage Res bonus
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>75)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
		}
	//:: Misc
		iRoll = d100();
		if (iRoll>75)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
	//:: Saves
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}
	}

////////////////////////////////////////// Lvls 30-40

	if (iRange==5)
	{
	//:: Ability bonus x 2
		iRoll = d100();
		if (iRoll>50)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}

		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}

	//:: Damage Res bonus x 2
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>90)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
		}
	//:: Misc
		iRoll = d100();
		if (iRoll>90)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>85)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
	//:: Save
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>90)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>94)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}

	}

	switch(iQual)
	{
		case 1: sName = ColorString("Magical "+sIName,255, 255, 255); break;
		case 2: sName = ColorString("Mystical "+sIName, 30, 180, 30); break;
		case 3: sName = ColorString("Enchanted "+sIName, 30, 180, 30); break;
		case 4: sName = ColorString("Imbued "+sIName, 5, 90, 255); break;
		case 5: sName = ColorString("Arcane "+sIName, 5, 90, 255); break;
		case 6: sName = ColorString("Lesser Planar "+sIName, 185, 1, 200); break;
		case 7: sName = ColorString("Greater Planar "+sIName, 185, 1, 200); break;
		case 8: sName = ColorString("Divine "+sIName, 255, 245, 210); break;
		case 9: sName = ColorString("Eldritch "+sIName, 255, 245, 210); break;
		case 10: sName = ColorString("Ancient "+sIName, 180, 180, 80); break;
	}
	
	SetName(oItem, sName);
}

void DropMagicItem (object oMob, object oSack, int iRange, int SockChance, int iChest)
{
	string sType, sName, sIName, sSocks;
	object oItem;
	itemproperty ipAdd;
	int iID = 0;
	int iQual = 0;
	int iRoll = d20();
	
	switch(iRoll)
	{
		case 1: {sType = "sdammy1"; 	sIName = "Amulet";iID = 1;} break;
		case 2: {sType = "sdring1"; 	sIName = "Ring";iID = 2;} break;
		case 3: {sType = "sdboots1"; 	sIName = "Boots";iID = 1;}break;
		case 4: {sType = "sdbracers1";	sIName = "Wristband";} break;
		case 5: {sType = "sdhelm1";		sIName = "Helm";} break;
		case 6: {sType = "sdcloak1";	sIName = "Cloak";iID = 1;} break;
		case 7: {sType = "sdbelt1";		sIName = "Belt";} break;
		case 8: {sType = "sdammy2";		sIName = "Talisman";iID = 1;} break;
		case 9: {sType = "sdring2";		sIName = "Band";iID = 2;} break;
		case 10: {sType = "sdboots2";	sIName = "Sabatons";iID = 1;} break;
		case 11: {sType = "sdbracers2";	sIName = "Bracers";} break;
		case 12: {sType = "sdhelm2";	sIName = "Visor";} break;
		case 13: {sType = "sdcloak2";	sIName = "Cape";iID = 1;} break;
		case 14: {sType = "sdbelt2";	sIName = "Thick Belt";} break;
		case 15: {sType = "sdammy3";	sIName = "Charm";iID = 1;} break;
		case 16: {sType = "sdring3";	sIName = "Circle";iID = 2;} break;
		case 17: {sType = "sdboots3";	sIName = "Greaves";iID = 1;} break;
		case 18: {sType = "sdbracers3";	sIName = "Armband";} break;
		case 19: {sType = "sdhelm3";	sIName = "Vanguard";} break;
		case 20: {sType = "sdring4";	sIName = "Coil";iID = 2;} break;
	}
	
//:: Chance for socketed item
	oItem = CreateItemOnObject(sType, oSack, 1, "sf_socket_item");

	iRoll = d100();
	if (iRoll < SockChance)
	{
		iRoll=d6();
		SetLocalInt(oItem, "SOCKETS", iRoll);
		sSocks = IntToString(iRoll);
		ipAdd = ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER, IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE);
		IPSafeAddItemProperty(oItem, ipAdd);

		sName = ColorString("Socketed "+sIName+" ("+sSocks+")", 72, 209, 204 );
		SetName(oItem, sName);
		return;
	}
	iRoll = d100();
	if (iRoll <= CHANCE_WORN && iRoll > CHANCE_BROKEN && iChest != 1)	//:: Chance of being worn
	{
		sName = ColorString("Worn "+sIName, 192, 192, 192);
		SetName(oItem, sName);
		SetIdentified(oItem, TRUE);
		return;
	}
	if (iRoll <= CHANCE_BROKEN && iChest != 1)	//:: Chance of being broken
	{
		DelayCommand(0.2, ACimbue(oItem, iRange, 1));
		sName = ColorString("Broken "+sIName, 255, 0, 0);
		SetName(oItem, sName);
		SetIdentified(oItem, TRUE);
		return;
	}

	SetIdentified(oItem, FALSE);

///////////////////////////////////////////// Hench Reward Code

	object oKiller = GetFirstPC();

	if (GetMaster(oMob)!=OBJECT_INVALID)
	{
		SetIdentified(oItem, TRUE);
		SetItemCursedFlag(oItem, TRUE);
	}

//////////////////////////////////////////// Lvls 1-5

//:: AC bonus for ammy, cloak & boots or ability bonus otherwise
	if (iID == 1)
	{
		DelayCommand(0.2, ACmisc(oItem, iRange));
		++iQual;
	}
	else
    {
		iRoll = d100();
		if (iRoll > 94) //:: 5% Chance for Divinty / Wizardry
		{
			iRoll = d2(1);
			if (iRoll == 1)
			{
				DelayCommand(0.2, ImbueWizardry(oItem, iRange));
				iQual+=3;
			}
			else
			{
				DelayCommand(0.2, ImbueDivinity(oItem, iRange));
				iQual+=3;
			}					
		}
		
		DelayCommand(0.2, AbilityImbue(oItem, iRange));
		++iQual;
	}

 ////////////////////////////////////////// Lvls 6-10

	if (iRange == 2)
    {
	//:: Ability bonus
		 iRoll = d100();
		 if (iRoll > 60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}

		if (iID == 2)
		{
			iRoll = d100();
			if (iRoll > 94) //:: 5% Chance for Divinty / Wizardry
			{
				iRoll = d2(1);
				if (iRoll == 1)
				{
					DelayCommand(0.2, ImbueWizardry(oItem, iRange));
					iQual+=3;
				}
				else
				{
					DelayCommand(0.2, ImbueDivinity(oItem, iRange));
					iQual+=3;
				}					
			}
			else if (iRoll > 65)  //:: 30% Chance for spell slots
			{
				iRoll = d3();   // 1-3 slots
				DelayCommand(0.2, SpellSlot(oItem, iRange, iRoll));
				iQual+=2;
			}
		}
		else
		{
		//:: Saving Throw bonus
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
		}
    }

////////////////////////////////////////// Lvls 11-20

	if (iRange == 3)
	{
	//:: Ability bonus &/or AC Bonus
		iRoll = d100();
		if (iRoll > 55)
		{
			DelayCommand(0.2, ACmisc(oItem, iRange));
			++iQual;
		}		
		iRoll = d100();
		if (iRoll > 55)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
	//:: Rings ONLY
		if (iID == 2)
		{	
			iRoll = d100();
			if (iRoll > 89) //:: 10% Chance for Divinty / Wizardry
			{
				iRoll = d2(1);
				if (iRoll == 1)
				{
					DelayCommand(0.2, ImbueWizardry(oItem, iRange));
					iQual+=3;
				}
				else
				{
					DelayCommand(0.2, ImbueDivinity(oItem, iRange));
					iQual+=3;
				}					
			}
			else if (iRoll > 59)  //:: 30% Chance for spell slots
			{
				iRoll = d3()+1;   // 2-4 slots
				DelayCommand(0.2, SpellSlot(oItem, iRange, iRoll));
				iQual+=2;
			}
		}
		else
		{//:: Misc Bonus
			iRoll = d100();
			if (iRoll > 80)
			{
				DelayCommand(0.2, MiscImbue(oItem, iRange));
				iQual+=2;
			}
		//:: Saving Throw Bonuses
			iRoll = d100();
			if (iRoll>70)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
		}	
	}

////////////////////////////////////////// Lvls 20-30

	if (iRange == 4)
	{
	//:: 2x Ability bonus &/or AC Bonus
		iRoll = d100();
		if (iRoll > 55)
		{
			DelayCommand(0.2, ACmisc(oItem, iRange));
			++iQual;
		}		
		iRoll = d100();
		if (iRoll > 55)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll > 60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}		
	//:: Rings ONLY
		if (iID==2)
		{	
			iRoll = d100();
			if (iRoll > 89) //:: 10% Chance for Divinty / Wizardry
			{
				iRoll = d2(1);
				if (iRoll == 1)
				{
					DelayCommand(0.2, ImbueWizardry(oItem, iRange));
					iQual+=3;
				}
				else
				{
					DelayCommand(0.2, ImbueDivinity(oItem, iRange));
					iQual+=3;
				}					
			}
			else if (iRoll > 59)  //:: 30% Chance for spell slots
			{
				iRoll = d4()+2;   // 3-6 slots
				DelayCommand(0.2, SpellSlot(oItem, iRange, iRoll));
				iQual+=2;
			}
		}
		else
		{//:: Misc Itemprop
			iRoll = d100();
			if (iRoll > 80)
			{
				DelayCommand(0.2, MiscImbue(oItem, iRange));
				++iQual;
			}
		//:: Misc Immunity
			iRoll = d100();
			if (iRoll > 80)
			{
				DelayCommand(0.2, MIMMimbue(oItem, iRange));
				iQual+=2;
			}			
		//:: Damage Res bonus
			iRoll = d100();
			if (iRoll > 80)
			{
				DelayCommand(0.2, RESimbue(oItem, iRange));
				++iQual;
			}
		//:: Saving Throw Bonuses
			iRoll = d100();
			if (iRoll > 70)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}		
		}
	}
	
////////////////////////////////////////// Lvls 30-40

	if (iRange == 5)
	{
	//:: 2x Ability bonus &/or AC Bonus
		iRoll = d100();
		if (iRoll > 55)
		{
			DelayCommand(0.2, ACmisc(oItem, iRange));
			++iQual;
		}		
		iRoll = d100();
		if (iRoll > 55)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll > 60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}		
	//:: Damage Res bonus x 2
		iRoll = d100();
		if (iRoll > 70)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll >75)
		{
			DelayCommand(0.2, RESimbue(oItem, iRange));
			++iQual;
		}		
	//:: Rings ONLY
		if (iID==2)
		{	
			iRoll = d100();
			if (iRoll > 89) //:: 10% Chance for Divinty / Wizardry
			{
				iRoll = d2(1);
				if (iRoll == 1)
				{
					DelayCommand(0.2, ImbueWizardry(oItem, iRange));
					iQual+=3;
				}
				else
				{
					DelayCommand(0.2, ImbueDivinity(oItem, iRange));
					iQual+=3;
				}					
			}
			else if (iRoll > 59)  //:: 30% Chance for spell slots
			{
				iRoll = d6()+2;   // 3-8 slots
				DelayCommand(0.2, SpellSlot(oItem, iRange, iRoll));
				iQual+=2;
			}
		}
		else
	//:: Misc
		iRoll = d100();
		if (iRoll>50)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>90)
		{
			DelayCommand(0.2, MiscImbue(oItem, iRange));
			++iQual;
		}
	//:: Misc Immunity
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, MIMMimbue(oItem, iRange));
			iQual+=2;
		}
	//:: Save
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}
	}
	
	switch(iQual)
	{
		case 1: sName = ColorString("Magical "+sIName,255, 255, 255); break;
		case 2: sName = ColorString("Mystical "+sIName, 30, 180, 30); break;
		case 3: sName = ColorString("Enchanted "+sIName, 30, 180, 30); break;
		case 4: sName = ColorString("Imbued "+sIName, 5, 90, 255); break;
		case 5: sName = ColorString("Arcane "+sIName, 5, 90, 255); break;
		case 6: sName = ColorString("Lesser Planar "+sIName, 185, 1, 200); break;
		case 7: sName = ColorString("Greater Planar "+sIName, 185, 1, 200); break;
		case 8: sName = ColorString("Divine "+sIName, 255, 245, 210); break;
		case 9: sName = ColorString("Eldritch "+sIName, 255, 245, 210); break;
		case 10: sName = ColorString("Ancient "+sIName, 180, 180, 80); break;
	}
	
	SetName(oItem, sName);
	
	if (GetBaseItemType(oItem) == BASE_ITEM_CLOAK)
	{
		DelayCommand(0.4, ColorHelmClk(oItem, iRandomArmor1, iRandomArmor2, iRandomArmor3, iRandomArmor4, iRandomArmor5, iRandomArmor6, iRandomCloak, oSack));
	}

	if (GetBaseItemType(oItem) == BASE_ITEM_HELMET)
	{
		DelayCommand(0.4, ColorHelmClk(oItem, iRandomArmor1, iRandomArmor2, iRandomArmor3, iRandomArmor4, iRandomArmor5, iRandomArmor6, iRandomHelmet, oSack));
	}

	if (GetBaseItemType(oItem) == BASE_ITEM_RING)
	{
		DelayCommand(0.4, ChangeRingAmuBelt(oItem, iRandRing, oSack));
	}

	if (GetBaseItemType(oItem) == BASE_ITEM_BELT)
	{
		DelayCommand(0.4, ChangeRingAmuBelt(oItem, iRandBelt, oSack));
	}

	if (GetBaseItemType(oItem) == BASE_ITEM_AMULET)
	{
		DelayCommand(0.4, ChangeRingAmuBelt(oItem, iRandAmulet, oSack));
	}

	if (GetBaseItemType(oItem) == BASE_ITEM_BRACER)
	{
		DelayCommand(0.4, ChangeRingAmuBelt(oItem, iRandBracer, oSack));
	}
}

void DropMonkGloves (object oMob, object oSack, int iRange, int SockChance, int iChest)
{
	object oItem;
	itemproperty ipAdd;
	string sType, sName, sIName, sSocks;
	int iDice1, iDice2, iRoll;
	int iQual;
	int iWType = 0;

	iRoll = d6();
	switch (iRoll)
	{
		case 1: sType = "sd_mgloves"; break;
		case 2: sType = "sd_mgloves1"; break;
		case 3: sType = "sd_mgloves2"; break;
		case 4: sType = "sd_mgloves3"; break;
		case 5: sType = "sd_mgloves4"; break;
		case 6: sType = "sd_mgloves5"; break;
	}

	iRoll = d20();
	switch (iRoll)
	{
		case 1: sIName = "War Talons"; break;
		case 2: sIName = "Blood Claws"; break;
		case 3: sIName = "Pulverizers"; ;break;
		case 4: sIName = "Ninja Claws"; break;
		case 5: sIName = "War Gloves"; break;
		case 6: sIName = "Flesh Knuckles"; break;
		case 7: sIName = "Death Mitts"; break;
		case 8: sIName = "Palm Guards"; break;
		case 9: sIName = "Knuckledusters"; ;break;
		case 10: sIName = "Tiger Fists"; break;
		case 11: sIName = "Tauntlets"; break;
		case 12: sIName = "Chi Bracelets"; break;
		case 13: sIName = "Dragon Claws"; break;
		case 14: sIName = "Steel Palms"; break;
		case 15: sIName = "Exploding Fists"; break;
		case 16: sIName = "Nose Breakers"; break;
		case 17: sIName = "Stiff Fingers"; ;break;
		case 18: sIName = "Heart Piercers"; break;
		case 19: sIName = "Digit Devastators"; break;
		case 20: sIName = "Gore Gauntlets"; break;
	}

	oItem = CreateItemOnObject(sType, oSack, 1, "sf_socket_item");

	//:: Monk
	iRoll = d100();
	if (iRoll <= CHANCE_WORN && iRoll > CHANCE_BROKEN && iChest != 1)	//:: Chance of being worn
	{
		sName = ColorString("Worn "+sIName, 192, 192, 192);
		SetName(oItem, sName);
		SetIdentified(oItem, TRUE);
		return;
	}
	if (iRoll <= CHANCE_BROKEN && iChest != 1)	//:: Chance of being broken
	{
		DelayCommand(0.2, ACimbue(oItem, iRange, 1));
		sName = ColorString("Broken "+sIName, 255, 0, 0);
		SetName(oItem, sName);
		SetIdentified(oItem, TRUE);
		return;
	}

	SetIdentified(oItem, FALSE);

//////////////////////////////////////////// Lvls 1-5      ::Monk2::

	//:: Attack bonus
	DelayCommand(0.2, BowEnhance(oItem, iRange));
	++iQual;

////////////////////////////////////////// Lvls 6-10      ::Monk2::

	if (iRange==2)
	{
	//:: Ability bonus
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
	//:: Damage Bonus
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
	//:: Haste
		iRoll = d100();
		if (iRoll==98)
		{
			DelayCommand(0.2, HasteImbue(oItem));
			iQual+=2;
		}
	}

////////////////////////////////////////// Lvls 11-20     ::Monk2::

	if (iRange==3)
	{
	//:: Ability bonus x 2
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
	//:: Damage Bonus x 2
		iRoll = d100();
		if (iRoll>50)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>55)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
	//::: Haste
		iRoll = d100();
		if (iRoll>=95)
		{
			DelayCommand(0.2, HasteImbue(oItem));
			++iQual;
		}
	}

////////////////////////////////////////// Lvls 20-30     ::Monk2::

	if (iRange==4)
	{
	//:: Ability bonus x 2
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>40)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
	//:: Damage Bonus x 3
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>50)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
	//:: Haste
		iRoll = d100();
		if (iRoll>=95)
		{
			DelayCommand(0.2, HasteImbue(oItem));
			++iQual;
		}
	//:: Save
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}
	}

////////////////////////////////////////// Lvls 30-40     ::Monk2::

	if (iRange==5)
	{
	//:: Ability bonus x 2
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
		}
	//:: Damage Bonus x 3
		iRoll = d100();
		if (iRoll>30)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>50)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		if (iRoll>70)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		if (iRoll>85)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		if (iRoll>92)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		if (iRoll>95)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
	//:: Haste
		iRoll = d100();
		if (iRoll>=90)
		{
			DelayCommand(0.2, HasteImbue(oItem));
			++iQual;
		}
	//:: Save
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, SaveImbue(oItem, iRange));
			++iQual;
		}
	}

	switch(iQual)
	{
		case 1: sName = ColorString("Magical "+sIName,255, 255, 255); break;
		case 2: sName = ColorString("Mystical "+sIName, 30, 180, 30); break;
		case 3: sName = ColorString("Enchanted "+sIName, 30, 180, 30); break;
		case 4: sName = ColorString("Imbued "+sIName, 5, 90, 255); break;
		case 5: sName = ColorString("Arcane "+sIName, 5, 90, 255); break;
		case 6: sName = ColorString("Lesser Planar "+sIName, 185, 1, 200); break;
		case 7: sName = ColorString("Greater Planar "+sIName, 185, 1, 200); break;
		case 8: sName = ColorString("Divine "+sIName, 255, 245, 210); break;
		case 9: sName = ColorString("Eldritch "+sIName, 255, 245, 210); break;
		case 10: sName = ColorString("Ancient "+sIName, 180, 180, 80); break;
	}
	
	SetName(oItem, sName);
	SetIdentified(oItem, FALSE);
	DelayCommand(0.4, ChangeRingAmuBelt(oItem, iRandGauntlet, oSack));
}

void DropWeapon (object oMob, object oSack, int iRange, int SockChance, int iChest)
{
	object oItem;
	itemproperty ipAdd;
	string sType, sName, sIName, sSocks, DType;
	int iRoll;
	int iQual = 0;
	int iWType = 0;

	iRoll = Random(54)+1;
	switch(iRoll)
	{
	//:: Axes
		case 1: 
		{
			sType = "sdgaxe"; iRoll = d3();
			
			if (iRoll==1)sIName = "Greataxe";
			if (iRoll==2)sIName = "Two Handed Axe";
			if (iRoll==3)sIName = "Executioner's Axe";
		}
		break;
		case 2: 
		{
			sType = "sdwaxe"; iRoll = d3();
			
			if (iRoll==1)sIName = "Dwarven War Axe";
			if (iRoll==2)sIName = "War Axe";
			if (iRoll==3)sIName = "Broad Axe";
		}
		break;
		case 3: 
		{
			sType = "sdbaxe"; iRoll = d3();
			if (iRoll==1)sIName = "Battleaxe";
			if (iRoll==2)sIName = "Berserker Axe";
			if (iRoll==3)sIName = "Siege Axe";
		}
		break;
		case 4: 
		{
			sType = "sdhaxe"; iRoll = d3();
			if (iRoll==1)sIName = "Handaxe";
			if (iRoll==2)sIName = "Cleaver";
			if (iRoll==3)sIName = "Tomahawk";
		}
		break;

	//:: Bladed
		case 5: 
		{
			sType = "sd_bastard"; iRoll = d3();
			if (iRoll==1)sIName = "Bastard Sword";
			if (iRoll==2)sIName = "War Sword";
			if (iRoll==3)sIName = "Arming Sword";
		}
		break;
		case 6: 
		{
			sType = "sdlsword"; iRoll = d3();
			if (iRoll==1)sIName = "Longsword";
			if (iRoll==2)sIName = "Knight's Sword";
			if (iRoll==3)sIName = "Broadsword ";
		}
		break;
		case 7: 
		{ //:: Falchion
			sType = "prc_wswfa001"; iRoll = d3();
			if (iRoll==1)sIName = "Falchion";
			if (iRoll==2)sIName = "Nagamaki";
			if (iRoll==3)sIName = "Chopper";
		}
		break;	
		case 8: 
		{
			sType = "sdssword"; iRoll = d3();
			if (iRoll==1)sIName = "Short Sword";
			if (iRoll==2)sIName = "Gladius";
			if (iRoll==3)sIName = "Xiphos";
		}
		break;
		case 9: 
		{
			sType = "sdgsword"; iRoll = d3();
			if (iRoll==1)sIName = "Greatsword";
			if (iRoll==2)sIName = "Claymore";
			if (iRoll==3)sIName = "Linebreaker";
		}
		break;
		case 10: 
		{
			sType = "sdkatana"; iRoll = d3();
			if (iRoll==1)sIName = "Katana";
			if (iRoll==2)sIName = "Samurai Sword";
			if (iRoll==3)sIName = "Nihonto";
		}
		break;
		case 11: 
		{
			sType = "sdscim"; iRoll = d3();
			if (iRoll==1)sIName = "Scimitar";
			if (iRoll==2)sIName = "Shamshir";
			if (iRoll==3)sIName = "Talwar";
		}
		break;
		case 12: 
		{
			sType = "sdrapier"; iRoll = d3();
			if (iRoll==1)sIName = "Rapier";
			if (iRoll==2)sIName = "Sabre";
			if (iRoll==3)sIName = "Court Sword";
		}
		break;
		case 13: 
		{
			sType = "sddagger"; iRoll = d6();
			if (iRoll==1)sIName = "Dagger";
			if (iRoll==2)sIName = "Dirk";
			if (iRoll==3)sIName = "Knife";
			if (iRoll==4)sIName = "Stiletto";
			if (iRoll==5)sIName = "Poignard";
			if (iRoll==6)sIName = "Main-gauche";
		}
		break;

	//:: Exotic
		case 14: 
		{
			sType = "sdstaff3"; iRoll = d3();
			iWType = 2;
			if (iRoll==1)sIName = "Arcane Staff";
			if (iRoll==2)sIName = "Runic Staff ";
			if (iRoll==3)sIName = "Spellstaff ";
		}
		break;
		case 15: 
		{
			sType = "sdkama"; iRoll = d3();
			if (iRoll==1)sIName = "Kama";
			if (iRoll==2)sIName = "Monk Claw";
			if (iRoll==3)sIName = "Ripper";}
		break;
		case 16: 
		{
			sType = "sdkukri"; iRoll = d3();
			if (iRoll==1)sIName = "Kukri";
			if (iRoll==2)sIName = "Machete";
			if (iRoll==3)sIName = "Scorpion Tail";
		}
		break;
		case 17: 
		{	
			sType = "sdlbow"; iRoll = d3();
			iWType = 1;
			if (iRoll==1)sIName = "Longbow";
			if (iRoll==2)sIName = "Warbow";
			if (iRoll==3)sIName = "Greatbow";
		}
		break;

	//:: Blunt
		case 18: 
		{
			sType = "sdclub"; iRoll = d3();
			if (iRoll==1)sIName = "Club";
			if (iRoll==2)sIName = "Baton";
			if (iRoll==3)sIName = "Truncheon";
		}
		break;
		case 19: 
		{
			sType = "sdhflail"; iRoll = d3();
			if (iRoll==1)sIName = "Heavy Flail";
			if (iRoll==2)sIName = "War Flail";
			if (iRoll==3)sIName = "Bastard Flail";
		}
		break;
		case 20: 
		{
			sType = "sdlflail"; iRoll = d3();
			if (iRoll==1)sIName = "Light Flail";
			if (iRoll==2)sIName = "Chain Mace";
			if (iRoll==3)sIName = "Infanty Flail";
		}
		break;
		case 21: 
		{
			sType = "sdstaff1"; iRoll = d3();
			iWType = 2;
			if (iRoll==1)sIName = "Magestaff";
			if (iRoll==2)sIName = "Mystic Staff";
			if (iRoll==3)sIName = "Wyrdstaff ";
		}
		break;
		case 22: 
		{
			sType = "sdwhamm"; iRoll = d3();
			if (iRoll==1)sIName = "Warhammer";
			if (iRoll==2)sIName = "Battlehammer";
			if (iRoll==3)sIName = "Earthshaker";
		}
		break;
		case 23: 
		{
			sType = "sdmace"; iRoll = d3();
			if (iRoll==1)sIName = "Light Mace";
			if (iRoll==2)sIName = "Flanged Mace";
			if (iRoll==3)sIName = "Cudgel";
		}
		break;
		case 24: 
		{
			sType = "sdmstar"; iRoll = d3();
			if (iRoll==1)sIName = "Morningstar ";
			if (iRoll==2)sIName = "Ball-and-Chain";
			if (iRoll==3)sIName = "Warstar";
		}
		break;

	//:: Double Sided
		case 25: 
		{
			sType = "sddbsword"; iRoll = d3();
			if (iRoll==1)sIName = "Two-Bladed Sword";
			if (iRoll==2)sIName = "Doublesword";
			if (iRoll==3)sIName = "Twinblade";
		}
		break;
		case 26: 
		{
			sType = "sddsmace"; iRoll = d3();
			if (iRoll==1)sIName = "Dire Mace";
			if (iRoll==2)sIName = "Double Mace";
			if (iRoll==3)sIName = "Twin Basher";
		}
		break;
		case 27: 
		{
			sType = "sddsaxe"; iRoll = d3();
			if (iRoll==1)sIName = "Double Axe";
			if (iRoll==2)sIName = "Crowd Cutter";
			if (iRoll==3)sIName = "Dervish";
		}
		break;
		case 28: 
		{
			sType = "sdqstaff"; iRoll = d3();
			if (iRoll==1)sIName = "Quarterstaff";
			if (iRoll==2)sIName = "Battlestaff";
			if (iRoll==3)sIName = "Bo";
		}
		break;

	//:: Polearms
		case 29: 
		{
			sType = "sdhalberd"; iRoll = d4();
			if (iRoll==1)sIName = "Halberd";
			if (iRoll==2)sIName = "Poleaxe";
			if (iRoll==3)sIName = "Glaive";
			if (iRoll==3)sIName = "Bardiche";
		}
		break;
		case 30: 
		{
			sType = "sdscythe"; iRoll = d3();
			if (iRoll==1)sIName = "Scythe";
			if (iRoll==2)sIName = "Reaper";
			if (iRoll==3)sIName = "Harvester";
		}
		break;
		case 31: 
		{
			sType = "sdspear"; iRoll = d3();
			if (iRoll==1)sIName = "Spear";
			if (iRoll==2)sIName = "Pike";
			if (iRoll==3)sIName = "Harpoon";
		}
		break;

	//:: Whip
		case 32: 
		{
			sType = "sdwhip"; iRoll = d3();
			if (iRoll==1)sIName = "Whip";
			if (iRoll==2)sIName = "Bullwhip";
			if (iRoll==3)sIName = "Lash";
		}
		break;

	//:: Ranged
		case 33: 
		{
			sType = "sdsbow"; iRoll = d3();
			iWType = 1;
			if (iRoll==1)sIName = "Shortbow";
			if (iRoll==2)sIName = "Hunting Bow";
			if (iRoll==3)sIName = "Lightbow";
		}
		break;
		case 34: 
		{
			sType = "sdlcbow"; iRoll = d3();
			iWType = 1;
			if (iRoll==1)sIName = "Light Crossbow";
			if (iRoll==2)sIName = "Crossbow";
			if (iRoll==3)sIName = "Traveler�s Crossbow";
		}
		break;
		case 35: 
		{
			sType = "sdhcbow"; iRoll = d3();
			iWType = 1;
			if (iRoll==1)sIName = "Heavy Crossbow";
			if (iRoll==2)sIName = "Siege Crossbow";
			if (iRoll==3)sIName = "Arbalest";
		}
		break;
		
	//:: Mage
		case 36: 
		{
			sType = "sdlhamm"; iRoll = d3();
			if (iRoll==1)sIName = "Light Hammer";
			if (iRoll==2)sIName = "Forge Hammer";
			if (iRoll==3)sIName = "Hand Hammer";
		}
		break;
		case 37: 
		{
			sType = "sdstaff2"; iRoll = d3();
			iWType = 2;
			if (iRoll==1)sIName = "Mystic Cane";
			if (iRoll==2)sIName = "Thaumic Conduit";
			if (iRoll==3)sIName = "Leyline Channeler";
		}
		break;
		case 38: 
		{
			sType = "sdsickle"; iRoll = d3();
			if (iRoll==1)sIName = "Sickle";
			if (iRoll==2)sIName = "Moonblade";
			if (iRoll==3)sIName = "Crescent Blade";
		}
		break;
		case 39: 
		{
			sType = "sdmgloves"; iRoll = d3();
			iWType = 3;
			if (iRoll==1)sIName = "Gloves";
			if (iRoll==2)sIName = "Fistwraps";
			if (iRoll==3)sIName = "Gauntlets";
		}
		break;
	//:: PRC / CEP Weapons
		case 40: 
		{ //:: Trident
			sType = "nw_wpltr001"; iRoll = d3();
			if (iRoll==1)sIName = "Trident";
			if (iRoll==2)sIName = "Fork";
			if (iRoll==3)sIName = "Fuscina";
		}
		break;	
		case 41: 
		{ //:: Heavy Pick
			sType = "prc_wblph001"; iRoll = d3();
			if (iRoll==1)sIName = "Heavy Pick";
			if (iRoll==2)sIName = "War Pick";
			if (iRoll==3)sIName = "Mattock";
		}
		break;	
		case 42: 
		{ //:: Light Pick
			sType = "prc_wblpl001"; iRoll = d3();
			if (iRoll==1)sIName = "Light Pick";
			if (iRoll==2)sIName = "Hand Pick";
			if (iRoll==3)sIName = "Rock Hammer";
		}
		break;		
		case 43: 
		{ //:: Goad
			sType = "prc_wspgd001"; iRoll = d3();
			if (iRoll==1)sIName = "Goad";
			if (iRoll==2)sIName = "Prong";
			if (iRoll==3)sIName = "Prod";
		}
		break;		
		case 44: 
		{ //:: Katar
			sType = "prc_wswdp001"; iRoll = d3();
			if (iRoll==1)sIName = "Katar";
			if (iRoll==2)sIName = "Punch-dagger";
			if (iRoll==3)sIName = "Tekpi";
		}
		break;	
		case 45: 
		{ //:: Sai
			sType = "prc_wswsi001"; iRoll = d3();
			if (iRoll==1)sIName = "Sai";
			if (iRoll==2)sIName = "Jitte";
			if (iRoll==3)sIName = "Defender";
		}
		break;			
		case 46: 
		{ //:: Eagle Claw
			sType = "bdd_eagle_claw"; iRoll = d3();
			if (iRoll==1)sIName = "Eagle Claw";
			if (iRoll==2)sIName = "Claw";
			if (iRoll==3)sIName = "Talon";
		}
		break;		
		case 47: 
		{ //:: Elven Courtblade
			sType = "prc_wspec001"; iRoll = d3();
			if (iRoll==1)sIName = "Elven Courtblade";
			if (iRoll==2)sIName = "Courtblade";
			if (iRoll==3)sIName = "Elven Greatsword";
		}
		break;		
		case 48: 
		{ //:: Elven Lightblade
			sType = "prc_wspel001"; iRoll = d3();
			if (iRoll==1)sIName = "Elven Lightblade";
			if (iRoll==2)sIName = "Lightblade";
			if (iRoll==3)sIName = "Elven Gladius";
		}
		break;	
		case 49: 
		{ //:: Elven Thinblade
			sType = "prc_wspet001"; iRoll = d3();
			if (iRoll==1)sIName = "Elven Thinblade";
			if (iRoll==2)sIName = "Thinblade";
			if (iRoll==3)sIName = "Elven Epee";
		}
		break;
		case 50: 
		{ //:: Maul
			sType = "prc_wxblma001"; iRoll = d3();
			if (iRoll==1)sIName = "Maul";
			if (iRoll==2)sIName = "Great Hammer";
			if (iRoll==3)sIName = "Slegehammer";
		}
		break;		
		case 51: 
		{ //:: Heavy Mace
			sType = "prc_wxblmh001"; iRoll = d3();
			if (iRoll==1)sIName = "Heavy Mace";
			if (iRoll==2)sIName = "Footman�s Mace";
			if (iRoll==3)sIName = "Pernach";
		}
		break;		
		case 52: 
		{ //:: Nunchaku
			sType = "prc_wblnn001"; iRoll = d3();
			if (iRoll==1)sIName = "Nunchaku";
			if (iRoll==2)sIName = "Thresher";
			if (iRoll==3)sIName = "Split-club";
		}
		break;	
		case 53: 
		{ //:: Sap
			sType = "prc_wspsp001"; iRoll = d3();
			if (iRoll==1)sIName = "Sap";
			if (iRoll==2)sIName = "Blackjack";
			if (iRoll==3)sIName = "Shot-sack";
		}
		break;	
		case 54: 
		{ //:: Double Scimitar
			sType = "prc_wxdbsc001"; iRoll = d3();
			if (iRoll==1)sIName = "Double Scimitar";
			if (iRoll==2)sIName = "Twin Sabre";
			if (iRoll==3)sIName = "Double Shamshir";
		}
		break;
		case 55: 		
		{ //:: Sling
			iWType = 1;
			sType = "sdsling"; iRoll = d3();
			if (iRoll==1)sIName = "Sling";
			if (iRoll==2)sIName = "Strap";
			if (iRoll==3)sIName = "Funda";
		}
		break;	
	}

//:: Chance for socketed item
	oItem = CreateItemOnObject(sType, oSack, 1, "sf_socket_item");

	SetName(oItem, sIName);

	iRoll = d100();
	if (iRoll < SockChance)
	{
		iRoll=d6();
		SetLocalInt(oItem, "SOCKETS", iRoll);
		sSocks = IntToString(iRoll);
		ipAdd = ItemPropertyCastSpell(IP_CONST_CASTSPELL_UNIQUE_POWER, IP_CONST_CASTSPELL_NUMUSES_UNLIMITED_USE);
		IPSafeAddItemProperty(oItem, ipAdd);

		sName = ColorString("Socketed "+sIName+" ("+sSocks+")", 72, 209, 204 );
		SetName(oItem, sName);
		return;
	}

	if (iWType==1)		//:: Ranged Weapons
	{
		iQual=0;

	//:: Ranged

	//:: Chance for worn or broken item
		iRoll = d100();
		if (iRoll <= CHANCE_WORN && iRoll > CHANCE_BROKEN && iChest != 1)	//:: Chance of being worn
		{
			sName = ColorString("Worn "+sIName, 192, 192, 192);
			SetName(oItem, sName);
			SetIdentified(oItem, TRUE);
			return;
		}
		if (iRoll <= CHANCE_BROKEN && iChest != 1)	//:: Chance of being broken
		{
			DelayCommand(0.2, ACimbue(oItem, iRange, 1));
			sName = ColorString("Broken "+sIName, 255, 0, 0);
			SetName(oItem, sName);
			SetIdentified(oItem, TRUE);
			return;
		}

		SetIdentified(oItem, FALSE);

//////////////////////////////////////////// Lvls 1-5	::Ranged::

		//:: Attack bonus

		DelayCommand(0.2, BowEnhance(oItem, iRange));
		++iQual;

////////////////////////////////////////// Lvls 6-10	::Ranged::

		if (iRange==2)
		{
		//:: Ability bonus
			iRoll = d100();
			if (iRoll>70)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
		//:: Massive Crits
			iRoll = d100();
			if (iRoll > 70)
			{
				DelayCommand(0.2, MassCritImbue(oItem, iRange));
				++iQual;
			}
		//:: Mighty
			iRoll = d100();
			if (iRoll > 70)
			{
				DelayCommand(0.2, MightyEnhance(oItem, iRange));
				++iQual;
			}
		//:: Haste
			iRoll = d100();
			if (iRoll >= 98)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		}

////////////////////////////////////////// Lvls 11-20	::Ranged::

		if (iRange==3)
		{
		//:: Mighty
			iRoll = d100();
			if (iRoll>65)
			{
				DelayCommand(0.2, MightyEnhance(oItem, iRange));
				++iQual;
			}
		//:: Ability bonus
			iRoll = d100();
			if (iRoll>65)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
		//:: Massive Crits
			iRoll = d100();
			if (iRoll>65)
			{
				DelayCommand(0.2, MassCritImbue(oItem, iRange));
				++iQual;
			}
		//:: Extra range damage bonus
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, RangedImbue(oItem));
				++iQual;
			}
		//:: Save
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste
			iRoll = d100();
			if (iRoll>=95)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		}

////////////////////////////////////////// Lvls 20-30	::Ranged::

		if (iRange==4)
		{
		//:: Ability bonus
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}

		//:: Massive Crits
			iRoll = d100();
			if (iRoll>55)
			{
				DelayCommand(0.2, MassCritImbue(oItem, iRange));
				++iQual;
			}
		//:: Mighty
			iRoll = d100();
			if (iRoll>55)
			{
				DelayCommand(0.2, MightyEnhance(oItem, iRange));
				++iQual;
			}
		//:: Extra ranged damage bonus
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, RangedImbue(oItem));
				iQual+=2;
			}
		//:: Save
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste
			iRoll = d100();
			if (iRoll>=90)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		}

////////////////////////////////////////// Lvls 30-40     ::Ranged::

		if (iRange==5)
		{
		//:: Ability bonus x 2
			iRoll = d100();
			if (iRoll>60)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>65)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
		//:: Mighty
			iRoll = d100();
			if (iRoll>45)
			{
				DelayCommand(0.2, MightyEnhance(oItem, iRange));
				++iQual;
			}
		//:: Extra melee damage bonus
			iRoll = d100();
			if (iRoll>70)
			{
				DelayCommand(0.2, RangedImbue(oItem));
				iQual+=2;
			}
		//:: Massive Crits
			iRoll = d100();
			if (iRoll>50)
			{
				DelayCommand(0.2, MassCritImbue(oItem, iRange));
				++iQual;
			}
		//:: Save
			iRoll = d100();
			if (iRoll>85)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste
			iRoll = d100();
			if (iRoll>=85)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		}
	}
	else if (iWType==2)	//:: Mage "weapons"
	{
	//:: Mage
		iQual=0;

		iRoll = d100();
		if (iRoll <= CHANCE_WORN && iRoll > CHANCE_BROKEN && iChest != 1)	//:: Chance of being worn
		{
			sName = ColorString("Worn "+sIName, 192, 192, 192);
			SetName(oItem, sName);
			SetIdentified(oItem, TRUE);
			return;
		}
		if (iRoll <= CHANCE_BROKEN && iChest != 1)	//:: Chance of being broken
		{
			DelayCommand(0.2, ACimbue(oItem, iRange, 1));
			sName = ColorString("Broken "+sIName, 255, 0, 0);
			SetName(oItem, sName);
			SetIdentified(oItem, TRUE);
			return;
		}
//////////////////////////////////////////// Lvls 1-5      ::Mage::

		//:: Enhancement bonus
		DelayCommand(0.2, WeapEnhance(oItem, iRange));
		++iQual;

////////////////////////////////////////// Lvls 6-10     ::Mage::

		if (iRange==2)
		{
		//:: Extra Spell Slot  1-2
			iRoll = d100();
			if (iRoll>60)
			{
				iRoll = d3();if (iRoll==3)iRoll==2;
				DelayCommand(0.2, SpellSlot(oItem, iRange, iRoll));
				++iQual;
			}

		//:: Spell Bonus
			iRoll = d100();
			if (iRoll>50)
			{
				DelayCommand(0.2, CastImbue(oItem, iRange));
				++iQual;
			}
		}

////////////////////////////////////////// Lvls 11-20     ::Mage::

		if (iRange==3)
		{
		//:: Ability bonus
			iRoll = d100();
			if (iRoll>50)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
		//:: Extra Spell Slot  1-4
			iRoll = d100();
			if (iRoll>45)
			{
				iRoll = d4();
				DelayCommand(0.2, SpellSlot(oItem, iRange, iRoll));
				++iQual;
			}
		//:: Spell Bonus x 2
			iRoll = d100();
			if (iRoll>40)
			{
				DelayCommand(0.2, CastImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>45)
			{
				DelayCommand(0.2, CastImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste bonus
			iRoll = d100();
			if (iRoll>95)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		}

////////////////////////////////////////// Lvls 20-30     ::Mage::

		if (iRange==4)
		{
		//:: Ability bonus
			iRoll = d100();
			if (iRoll>45)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
		//:: Extra Spell Slot  1-6
			iRoll = d100();
			if (iRoll==40)
			{
				iRoll = d6();
				DelayCommand(0.2, SpellSlot(oItem, iRange, iRoll));
			}
		//:: Spell Bonus x 2
			iRoll = d100();
			if (iRoll>35)
			{
				DelayCommand(0.2, CastImbue(oItem, iRange));
				++iQual;
			}

			iRoll = d100();
			if (iRoll>45)
			{
				DelayCommand(0.2, CastImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste bonus
			iRoll = d100();
			if (iRoll>92)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		//:: Extra melee damage bonus
			iRoll = d100();
			if (iRoll>85)
			{
				DelayCommand(0.2, MeleeImbue(oItem));
				++iQual;
			}
		//:: Save
			iRoll = d100();
			if (iRoll>60)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
		}

////////////////////////////////////////// Lvls 30-40     ::Mage::

		if (iRange==5)
		{
		//:: Ability bonus
			iRoll = d100();
			if (iRoll>35)
			{
			DelayCommand(0.2, AbilityImbue(oItem, iRange));
			++iQual;
			}
		//:: Extra Spell Slot  1-8
			iRoll = d100();
			if (iRoll>35)
			{
			iRoll = d8();
			DelayCommand(0.2, SpellSlot(oItem, iRange, iRoll));
			++iQual;
			}

		//:: Spell Bonus x 3
			iRoll = d100();
			if (iRoll>30)
			{
				DelayCommand(0.2, CastImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>40)
			{
				DelayCommand(0.2, CastImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>50)
			{
				DelayCommand(0.2, CastImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste bonus
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				++iQual;
			}
		//:: Extra melee damage bonus
			iRoll = d100();
			if (iRoll>70)
			{
				DelayCommand(0.2, MeleeImbue(oItem));
				iQual+=2;
			}
		//:: Save
			iRoll = d100();
			if (iRoll>55)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
		}
	}
	else if (iWType==3)	//: Monk Gloves
	{
	//:: Monk
		iRoll = d100();
		if (iRoll <= CHANCE_WORN && iRoll > CHANCE_BROKEN && iChest != 1)  //:: Chance of being worn
		{
			sName = ColorString("Worn "+sIName, 192, 192, 192);
			SetName(oItem, sName);
			SetIdentified(oItem, TRUE);
			return;
		}
		if (iRoll <= CHANCE_BROKEN && iChest != 1)	//:: Chance of being broken
		{
			DelayCommand(0.2, ACimbue(oItem, iRange, 1));
			sName = ColorString("Broken "+sIName, 255, 0, 0);
			SetName(oItem, sName);
			SetIdentified(oItem, TRUE);
			return;
		}

		SetIdentified(oItem, FALSE);

	//////////////////////////////////////////// Lvls 1-5      ::Monk::

		//:: Attack bonus
		DelayCommand(0.2, BowEnhance(oItem, iRange));
		++iQual;

	////////////////////////////////////////// Lvls 6-10      ::Monk::

		if (iRange==2)
		{
		//:: Ability bonus
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
		//:: Damage Bonus
			iRoll = d100();
			if (iRoll>70)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste
			iRoll = d100();
			if (iRoll==95)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		}

	////////////////////////////////////////// Lvls 11-20     ::Monk::

		if (iRange==3)
		{
		//:: Ability bonus x 2
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
		//:: Damage Bonus x 2
			iRoll = d100();
			if (iRoll>60)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
		//:: Damage Bonus x 2
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste
			iRoll = d100();
			if (iRoll>=90)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		}

////////////////////////////////////////// Lvls 20-30     ::Monk::

		if (iRange==4)
		{
		//:: Ability bonus x 2
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
		//:: Damage Bonus x 3
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>60)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste
			iRoll = d100();
			if (iRoll>=85)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		//:: Save
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
		}

////////////////////////////////////////// Lvls 30-40     ::Monk::

		if (iRange==5)
		{
		//:: Ability bonus x 2
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, AbilityImbue(oItem, iRange));
				++iQual;
			}
		//:: Damage Bonus x 3
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>60)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
		//:: Haste
			iRoll = d100();
			if (iRoll>=80)
			{
				DelayCommand(0.2, HasteImbue(oItem));
				iQual+=2;
			}
		//:: Save
			iRoll = d100();
			if (iRoll>70)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
		}
	}
	else				//::: Melee weapons
	{

		iRoll = d100();
		if (iRoll <= CHANCE_WORN && iRoll > CHANCE_BROKEN && iChest != 1)  //:: Chance of being worn
		{
			sName = ColorString("Worn "+sIName, 192, 192, 192);
			SetName(oItem, sName);
			SetIdentified(oItem, TRUE);
			return;
		}
		if (iRoll<=CHANCE_BROKEN&&iChest!=1)	//:: Chance of being broken
		{
			DelayCommand(0.2, ACimbue(oItem, iRange, 1));
			sName = ColorString("Broken "+sIName, 255, 0, 0);
			SetName(oItem, sName);
			SetIdentified(oItem, TRUE);
			return;
		}

//////////////////////////////////////////// Lvls 1-5      ::Melee::

	//:: Enhancement bonus
		DelayCommand(0.2, WeapEnhance(oItem, iRange));

		switch (iRange)
		{
			case 1: iQual+=1;
			case 2: iQual+=1;
			case 3: iQual+=2;
			case 4: iQual+=2;
			case 5: iQual+=3;
		}		

////////////////////////////////////////// Lvls 6-10     ::Melee::

		if (iRange==1)
		{
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}

			iRoll = d100();
			if (iRoll>98)
			{
				DelayCommand(0.2, VampRegenImbue(oItem, iRange));
				++iQual;
			}
		}
		if (iRange==2)
		{
		//: Damage bonus
			iRoll = d100();
			if (iRoll>85)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
		//:: Massive Crits
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, MassCritImbue(oItem, iRange));
				++iQual;
			}
		//:: Keen bonus
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, KeenImbue(oItem));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>95)
			{
				DelayCommand(0.2, WeapOnHit(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>95)
			{
				DelayCommand(0.2, VampRegenImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll > 98)
			{
				DelayCommand(0.2, EchobladeImbue(oItem));
				iQual+=2;
			}	
			iRoll = d100();
			if (iRoll > 98)
			{
				DelayCommand(0.2, WoundingImbue(oItem, iRange));
				iQual+=2;
			}				
		}

////////////////////////////////////////// Lvls 11-20     ::Melee::

		if (iRange==3)
		{
		//:: Damage bonus
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
		//:: Massive Crits
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, MassCritImbue(oItem, iRange));
				++iQual;
			}
		//:: Extra melee damage bonus
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, MeleeImbue(oItem));
				++iQual;
			}
		//:: Keen bonus
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, KeenImbue(oItem));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, WeapOnHit(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>95)
			{
				DelayCommand(0.2, VampRegenImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>97)
			{
				DelayCommand(0.2, EchobladeImbue(oItem));
				iQual+=2;
			}	
			iRoll = d100();
			if (iRoll > 98)
			{
				DelayCommand(0.2, WoundingImbue(oItem, iRange));
				iQual+=2;
			}				
		}

////////////////////////////////////////// Lvls 20-30     ::Melee::

		if (iRange==4)
		{
		//:: Damage bonus x 2
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			if (iRoll>75)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			if (iRoll>85)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
		//:: Massive Crits
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, MassCritImbue(oItem, iRange));
				++iQual;
			}
		//:: Extra melee damage bonus
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, MeleeImbue(oItem));
				++iQual;
			}
		//:: Keen bonus
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, KeenImbue(oItem));
				++iQual;
			}
		//:: Save
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, WeapOnHit(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>97)
			{
				DelayCommand(0.2, VampRegenImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>97)
			{
				DelayCommand(0.2, EchobladeImbue(oItem));
				iQual+=2;
			}	
			iRoll = d100();
			if (iRoll > 98)
			{
				DelayCommand(0.2, WoundingImbue(oItem, iRange));
				iQual+=2;
			}					
		}

////////////////////////////////////////// Lvls 30-40     ::Melee::

		if (iRange==5)
		{
		//:: Damage bonus x 3
			iRoll = d100();
			if (iRoll>85)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>75)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>80)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			if (iRoll>96)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			if (iRoll>97)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
			if (iRoll>98)
			{
				DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
				++iQual;
			}
		//:: Massive Crits
			iRoll = d100();
			if (iRoll>70)
			{
				DelayCommand(0.2, MassCritImbue(oItem, iRange));
				++iQual;
			}
		//:: Extra melee damage bonus
			iRoll = d100();
			if (iRoll>70)
			{
				DelayCommand(0.2, MeleeImbue(oItem));
				++iQual;
			}
		//:: Keen bonus
			iRoll = d100();
			if (iRoll>70)
			{
				DelayCommand(0.2, KeenImbue(oItem));
				++iQual;
			}
		//:: Save
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, SaveImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>90)
			{
				DelayCommand(0.2, WeapOnHit(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>95)
			{
				DelayCommand(0.2, WeapOnHit(oItem, iRange));
				++iQual;
			}

			iRoll = d100();
			if (iRoll>95)
			{
				DelayCommand(0.2, WeapOnHit(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>97)
			{
				DelayCommand(0.2, VampRegenImbue(oItem, iRange));
				++iQual;
			}
			iRoll = d100();
			if (iRoll>97)
			{
				DelayCommand(0.2, EchobladeImbue(oItem));
				iQual+=2;
			}	
			iRoll = d100();
			if (iRoll > 96)
			{
				DelayCommand(0.2, WoundingImbue(oItem, iRange));
				iQual+=2;
			}					
		}
	}

	if (iQual>10)iQual=10;
	if (iQual==0)iQual=1;

	////////////////////////////////////////////////////////
	//: Debugging
	//
	/*
	FloatingTextStringOnCreature("ResRef: "+sType, GetFirstPC());
	FloatingTextStringOnCreature("Qual: "+IntToString(iQual), GetFirstPC());
	FloatingTextStringOnCreature("Range: "+IntToString(iRange), GetFirstPC());
	*/

	switch(iQual)
	{
		case 1: sName = ColorString("Magical "+sIName,255, 255, 255); break;
		case 2: sName = ColorString("Mystical "+sIName, 30, 180, 30); break;
		case 3: sName = ColorString("Enchanted "+sIName, 30, 180, 30); break;
		case 4: sName = ColorString("Imbued "+sIName, 5, 90, 255); break;
		case 5: sName = ColorString("Arcane "+sIName, 5, 90, 255); break;
		case 6: sName = ColorString("Lesser Planar "+sIName, 185, 1, 200); break;
		case 7: sName = ColorString("Greater Planar "+sIName, 185, 1, 200); break;
		case 8: sName = ColorString("Divine "+sIName, 255, 245, 210); break;
		case 9: sName = ColorString("Eldritch "+sIName, 255, 245, 210); break;
		case 10: sName = ColorString("Ancient "+sIName, 180, 180, 80); break;
	}
	SetName(oItem, sName);
	SetIdentified(oItem, FALSE);

	if (GetBaseItemType(oItem) == BASE_ITEM_DOUBLEAXE)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d3(), d3(), d3(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_TWOBLADEDSWORD)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d3(), d3(), d3(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_SCYTHE)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d3(), d3(), d3(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_GREATAXE)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_HANDAXE)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_KATANA)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_RAPIER)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));	
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_HEAVYFLAIL)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_LIGHTHAMMER)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));	
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_LIGHTMACE)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));	
	}	
	if (GetBaseItemType(oItem) == BASE_ITEM_MORNINGSTAR)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_DIREMACE)
	{
	DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_HALBERD)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_SHORTSPEAR)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_TRIDENT)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_HEAVYCROSSBOW)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_LIGHTCROSSBOW)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_THROWINGAXE)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_DWARVENWARAXE)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d8(), d6(), d6(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_BATTLEAXE)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d8(), d6(), d6(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_BASTARDSWORD)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d6(), d6(), d6(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_DAGGER)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d6(), d6(), d6(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}	
	if (GetBaseItemType(oItem) == BASE_ITEM_SHORTSWORD)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d6(), d6(), d6(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_CLUB)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d6(), d6(), d6(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_SHORTBOW)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d6(), d6(), d6(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_GREATSWORD)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d4(), d4(), d4(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_WARHAMMER)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, Random(6) + 1, Random(6) + 1, Random(6) + 1, iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_MAGICSTAFF)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, Random(6) + 1, Random(6) + 1, Random(6) + 1, iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_LONGSWORD)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d8(), d8(), d8(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_LONGBOW)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, d8(), d8(), d8(), iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_SCIMITAR)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, Random(4) + 1, Random(4) + 1, Random(4) + 1,iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_LIGHTFLAIL)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, Random(4) + 1, Random(4) + 1, Random(4) + 1,iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
	if (GetBaseItemType(oItem) == BASE_ITEM_QUARTERSTAFF)
	{
		DelayCommand(0.4, ChangeWeaponAppearance(oItem, Random(4) + 1, Random(4) + 1, Random(4) + 1,iRandomWeapon1, iRandomWeapon2 , iRandomWeapon3, oSack));
	}
}

void DropAmmo (object oMob, object oSack, int iRange)
{
	object oItem;
	string sType, sName, sIName;
	int iRoll = d3();
	int iQual;
	int iStack = d12()*5;

	switch(iRoll)
	{
	//:: Ammo
		case 1: {sType = "sdarrow"; iRoll = d3();
		if (iRoll==1)sIName = "Arrow";
		if (iRoll==2)sIName = "Sagitta";
		if (iRoll==3)sIName = "War Arrow";}
		break;
		case 2: {sType = "sdbolt"; iRoll = d3();
		if (iRoll==1)sIName = "Bolt";
		if (iRoll==2)sIName = "Shaft";
		if (iRoll==3)sIName = "Quarrel";}
		break;
		case 3: {sType = "sdbullet"; iRoll = d3();
		if (iRoll==1)sIName = "Bullet";
		if (iRoll==2)sIName = "Slug";
		if (iRoll==3)sIName = "Stone";}
		break;
	}

	oItem = CreateItemOnObject(sType, oSack, iStack);

	//:: Ammo
	iRoll = d100();
	if (iRoll <= 10)  // 10% chance of worn item      ::Ammo::
	{
		sName = ColorString("Worn "+sIName, 192, 192, 192);
		SetName(oItem, sName);
		return;
	}

	SetIdentified(oItem, FALSE);

//////////////////////////////////////////// Lvls 1-5	::Ammo::

	if (iRange==1)
	{
	//:: Damage bonus
		DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
		++iQual;
		
	//++iQual;  // Debugging
	}

////////////////////////////////////////// Lvls 6-10	::Ammo::

	if (iRange==2)
	{
	//:: Damage bonus
		iRoll = d100();
		if (iRoll>40)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
	//:: Vamp Regen bonus
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, VampRegenImbue(oItem, iRange));
			++iQual;
		}
	}

////////////////////////////////////////// Lvls 11-20	::Ammo::

	if (iRange==3)
	{
	//:: Damage bonus x 2
		iRoll = d100();
		if (iRoll>40)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>50)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
	//:: Vamp Regen bonus
		iRoll = d100();
		if (iRoll>70)
		{
			DelayCommand(0.2, VampRegenImbue(oItem, iRange));
			++iQual;
		}
	//:: Unlim Ammo bonus
		iRoll = d100();
		if (iRoll>95)
		{
			DelayCommand(0.2, AmmoEnhance(oItem, iRange));
			iQual+=2;
		}
	}

////////////////////////////////////////// Lvls 20-30     ::Ammo::

	if (iRange==4)
	{
	//:: Damage bonus x 3
		iRoll = d100();
		if (iRoll>30)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>45)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>65)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
	//:: Vamp Regen bonus
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, VampRegenImbue(oItem, iRange));
			++iQual;
		}
	//:: Unlim Ammo bonus
		iRoll = d100();
		if (iRoll>85)
		{
			DelayCommand(0.2, AmmoEnhance(oItem, iRange));
			iQual+=2;
		}
	}

////////////////////////////////////////// Lvls 30-40     ::Ammo::

	if (iRange==5)
	{
	//:: Damage bonus x 4
		iRoll = d100();
		if (iRoll>20)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>30)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>40)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
		iRoll = d100();
		if (iRoll>60)
		{
			DelayCommand(0.2, DamageTypeImbue(oItem, iRange));
			++iQual;
		}
	//:: Vamp Regen bonus
		iRoll = d100();
		if (iRoll>50)
		{
			DelayCommand(0.2, VampRegenImbue(oItem, iRange));
			++iQual;
		}
	//:: Unlim Ammo bonus
		iRoll = d100();
		if (iRoll>80)
		{
			DelayCommand(0.2, AmmoEnhance(oItem, iRange));
			iQual+=2;
		}
	}
	switch(iQual)
	{
		case 1: sName = ColorString("Magical "+sIName,255, 255, 255); break;
		case 2: sName = ColorString("Mystical "+sIName, 30, 180, 30); break;
		case 3: sName = ColorString("Enchanted "+sIName, 30, 180, 30); break;
		case 4: sName = ColorString("Imbued "+sIName, 5, 90, 255); break;
		case 5: sName = ColorString("Arcane "+sIName, 5, 90, 255); break;
		case 6: sName = ColorString("Lesser Planar "+sIName, 185, 1, 200); break;
		case 7: sName = ColorString("Greater Planar "+sIName, 185, 1, 200); break;
		case 8: sName = ColorString("Divine "+sIName, 255, 245, 210); break;
		case 9: sName = ColorString("Eldritch "+sIName, 255, 245, 210); break;
		case 10: sName = ColorString("Ancient "+sIName, 180, 180, 80); break;
	}
	
	SetName(oItem, sName);

}

void DropGem (object oMob, object oSack, int iRange)
{
	object oItem;

	int iRoll;
	int iVal1;
	int iVal2;

	string sType1;
	string sType2;
	string sType3;
	string sName, pName;

	iRoll=d100();
	if (iRoll<32)
	oItem = CreateItemOnObject("sd_rune", oSack, 1);
	else if (iRoll>32&&iRoll<65)
	oItem = CreateItemOnObject("sd_rune2", oSack, 1);
	else if (iRoll>65)
	oItem = CreateItemOnObject("sd_rune3", oSack, 1);

	SetIdentified(oItem, FALSE);

	iRoll = d10();
	if (iRoll==1)		//:: Mass Crits
	{
		switch(iRange)
		{
			case 1:iVal1 = d3();   break;  //1-3
			case 2:iVal1 = d3()+1; break;  //2-4
			case 3:iVal1 = d4()+2; break;  //3-6
			case 4:iVal1 = d4()+4; break;  //5-8
			case 5:iVal1 = d6()+4; break;  //7-10
		}
		if (iVal1==0)iVal1=1;
		sName = "+"+IntToString(iVal1)+" Brutal Gem";
		sName = ColorString(sName, 127, 255, 212 );
		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", "MASSIVE_CRITICAL");
		SetLocalInt(oItem, "AMOUNT", iVal1);

	}
	else if (iRoll==2)	//::: Ability rune
	{
		switch(iRange)
		{
			case 1: iVal1 = 1;break;           //1
			case 2: iVal1 = Random(2)+1;break; //1-2
			case 3: iVal1 = d3();break;        //1-3
			case 4: iVal1 = d4();break;        //1-4
			case 5: iVal1 = d3()+1;break;      //2-4
		}
		iRoll=d6();
		switch(iRoll)
		{
			case 1: {sType2 = "STRENGTH_ABILITY_BONUS"; sType1=" Mighty"; break;}
			case 2: {sType2 = "DEXTERITY_ABILITY_BONUS"; sType1=" Deft";    break;}
			case 3: {sType2 = "WISDOM_ABILITY_BONUS"; sType1=" Wise";    break;}
			case 4: {sType2 = "CONSTITUTION_ABILITY_BONUS"; sType1=" Hardy";    break;}
			case 5: {sType2 = "INTELLEGENCE_ABILITY_BONUS"; sType1=" Clever";    break;}
			case 6: {sType2 = "CHARISMA_ABILITY_BONUS"; sType1=" Charming";    break;}
		}
		if (iVal1==0)iVal1=2;
		sName = "+"+IntToString(iVal1)+sType1+" Gem";
		sName = ColorString(sName, 127, 255, 212 );
		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", sType2);
		SetLocalInt(oItem, "AMOUNT", iVal1);

	}
	else if (iRoll==3)	//:: AC rune
	{
		switch(iRange)
		{
			case 1: iVal1 = 1;break;           //1
			case 2: iVal1 = Random(2)+1;break; //1-2
			case 3: iVal1 = d3();break;        //1-3
			case 4: iVal1 = d3()+2;break;      //3-5
			case 5: iVal1 = d3()+3;break;      //4-6
		}
		
		if (iVal1==0) iVal1=1;
		
		sName = "+"+IntToString(iVal1)+" AC Gem";
		sName = ColorString(sName, 127, 255, 212 );
		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", "AC_BONUS");
		SetLocalInt(oItem, "AMOUNT", iVal1);

	}
	else if (iRoll==4)	//:: AB rune
	{
		switch(iRange)
		{
			case 1: iVal1 = Random(2)+1;break; //1-2
			case 2: iVal1 = d3();break;        //1-3
			case 3: iVal1 = d3()+1;break;      //2-4
			case 4: iVal1 = d3()+2;break;      //3-5
			case 5: iVal1 = d4()+3;break;      //4-6
		}
		
		if (iVal1==0) iVal1=2;
		
		sName = "+"+IntToString(iVal1)+" Attack Gem";
		sName = ColorString(sName, 127, 255, 212 );
		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", "ATTACK_BONUS");
		SetLocalInt(oItem, "AMOUNT", iVal1);

	}
	else if (iRoll==5)	//:: Enhancement rune
	{
		switch(iRange)
		{
			case 1: iVal1 = Random(2)+1;break;  //1-2
			case 2: iVal1 = d3();break;         //1-3
			case 3: iVal1 = d3()+1;break;       //2-4
			case 4: iVal1 = d3()+2;break;       //3-5
			case 5: iVal1 = d3()+3;break;       //4-6
		}
		
		if (iVal1==0)iVal1=2;
		
		sName = "+"+IntToString(iVal1)+" Enhancement Gem";
		sName = ColorString(sName, 127, 255, 212 );
		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", "ENHANCEMENT_BONUS");
		SetLocalInt(oItem, "AMOUNT", iVal1);

	}
	else if (iRoll==6)	//:: Damage rune
	{
		switch(iRange)
		{
			case 1:
			{
				iRoll=d3();
				if (iRoll==1)iVal1 = 1;
				if (iRoll==2)iVal1 = 2;
				if (iRoll==3)iVal1 = 3;
			}
			break;
			case 2:
			{
				iRoll=d4();
				if (iRoll==1)iVal1 = 1;
				if (iRoll==2)iVal1 = 2;
				if (iRoll==3)iVal1 = 3;
				if (iRoll==4)iVal1 = 4;
			}
			break;
			case 3:
			{
				iRoll=d6();
				if (iRoll==1)iVal1 = 2;
				if (iRoll==2)iVal1 = 2;
				if (iRoll==3)iVal1 = 3;
				if (iRoll==4)iVal1 = 4;
				if (iRoll==5)iVal1 = 5;
				if (iRoll==6)iVal1 = 6;
			}
			break;
			case 4:
			{
				iRoll=d8();
				if (iRoll==1)iVal1 = 3;
				if (iRoll==2)iVal1 = 3;
				if (iRoll==3)iVal1 = 4;
				if (iRoll==4)iVal1 = 4;
				if (iRoll==5)iVal1 = 5;
				if (iRoll==6)iVal1 = 5;
				if (iRoll==7)iVal1 = 6;
				if (iRoll==8)iVal1 = 6;
			}
			break;
			case 5:
			{
				iRoll=d10();
				if (iRoll==1)iVal1 = 2;
				if (iRoll==2)iVal1 = 2;
				if (iRoll==3)iVal1 = 3;
				if (iRoll==4)iVal1 = 4;
				if (iRoll==5)iVal1 = 4;
				if (iRoll==6)iVal1 = 5;
				if (iRoll==7)iVal1 = 5;
				if (iRoll==8)iVal1 = 6;
				if (iRoll==9)iVal1 = 7;
				if (iRoll==10)iVal1 = 8;
			}
			break;
		}
		iRoll = d12();
		switch (iRoll)
		{
			case 1: {sType2 = "ACID_DAMAGE_BONUS"; sType1 = "Corrosive Gem"; break;}
			case 2: {sType2 = "BLUDGEONING_DAMAGE_BONUS"; sType1 = "Thumping Gem";    break;}
			case 3: {sType2 = "COLD_DAMAGE_BONUS"; sType1 = "Ice Gem";    break;}
			case 4: {sType2 = "DIVINE_DAMAGE_BONUS";sType1 = "Holy Gem";    break;}
			case 5: {sType2 = "ELECTIRCAL_DAMAGE_BONUS";sType1 = "Zapping Gem";    break;}
			case 6: {sType2 = "FIRE_DAMAGE_BONUS"; sType1 = "Scorching Gem";    break;}
			case 7: {sType2 = "MAGICAL_DAMAGE_BONUS"; sType1 = "Magical Gem";    break;}
			case 8: {sType2 = "NEGATIVE_DAMAGE_BONUS"; sType1 = "Evil Gem";    break;}
			case 9: {sType2 = "PIERCING_DAMAGE_BONUS"; sType1 = "Impaling Gem";    break;}
			case 10: {sType2 = "POSITIVE_DAMAGE_BONUS"; sType1 = "Benevolent Gem";    break;}
			case 11: {sType2 = "SLASHING_DAMAGE_BONUS"; sType1 = "Slicing Gem";    break;}
			case 12: {sType2 = "SONIC_DAMAGE_BONUS"; sType1 = "Booming Gem";    break;}
		}
		
		if (iVal1==0)iVal1=2;
		
		sName = "+"+IntToString(iVal1)+" "+sType1;
		sName = ColorString(sName, 127, 255, 212 );
		
		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", sType2);
		SetLocalInt(oItem, "AMOUNT", iVal1);

	}
	else if (iRoll==7)	//:: VampRegen
	{
		iRoll = d4();
		sType1 = "VAMPIRIC_REGENERATION";
		switch(iRange)
		{
			case 1: iVal1 = 1;break;          //1
			case 2: iVal1 = Random(2)+1;break; //1-2
			case 3: iVal1 = d3();break;        //1-3
			case 4: iVal1 = d3()+1;break;      //2-4
			case 5: iVal1 = d3()+2;break;      //3-5
		}
		sName = "+"+IntToString(iVal1)+" Vamp Regen Gem";
		sName = ColorString(sName, 127, 255, 212 );
		
		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", "VAMPIRIC_REGENERATION");
		SetLocalInt(oItem, "AMOUNT", iVal1);

	}
	else  if (iRoll==8)	//:: Misc
	{
		switch(iRange)
		{
			case 1:
			{
				iRoll = d6();
				if (iRoll==1){sType1 = "Benign"; sType2 = "Cracked";}
				if (iRoll==2){sType1 = "Benign"; sType2 = "Cracked";}
				if (iRoll==3){sType1 = "KEEN"; sType2 = "Deadly";}
				if (iRoll==4){sType1 = "HASTE"; sType2 = "Speedy";}
				if (iRoll==5){sType1 = "FREEDOM"; sType2 = "Unstoppable";}
				if (iRoll==6){sType1 = "Benign"; sType2 = "Cracked";}
			}
			break;
			case 2:
			{
				iRoll = d6();
				if (iRoll==1){sType1 = "HOLY_AVENGER"; sType2 = "Vengeful";}
				if (iRoll==2){sType1 = "FREEDOM"; sType2 = "Unstoppable";}
				if (iRoll==3){sType1 = "HASTE"; sType2 = "Speedy";}
				if (iRoll==4){sType1 = "KEEN"; sType2 = "Deadly";}
				if (iRoll==5){sType1 = "Benign"; sType2 = "Cracked";}
				if (iRoll==6){sType1 = "Benign"; sType2 = "Cracked";}
			}
			break;
			case 3:
			{
				iRoll = d6();
				if (iRoll==1){sType1 = "HOLY_AVENGER"; sType2 = "Vengeful";}
				if (iRoll==2){sType1 = "KEEN"; sType2 = "Deadly";}
				if (iRoll==3){sType1 = "FREEDOM"; sType2 = "Unstoppable";}
				if (iRoll==4){sType1 = "IMPROVED_EVASION"; sType2 = "Slippery";}
				if (iRoll==5){sType1 = "HOLY_AVENGER"; sType2 = "Vengeful";}
				if (iRoll==6){sType1 = "HASTE"; sType2 = "Speedy";}
			}
			break;
			case 4:
			{
				iRoll = d6();
				if (iRoll==1){sType1 = "HOLY_AVENGER"; sType2 = "Vengeful";}
				if (iRoll==2){sType1 = "KEEN"; sType2 = "Deadly";}
				if (iRoll==3){sType1 = "FREEDOM"; sType2 = "Unstoppable";}
				if (iRoll==4){sType1 = "IMPROVED_EVASION"; sType2 = "Slippery";}
				if (iRoll==5){sType1 = "HASTE"; sType2 = "Speedy";}
				if (iRoll==6){sType1 = "HASTE"; sType2 = "Speedy";}
			}
			break;
			case 5:
			{
				iRoll = d6();
				if (iRoll==1){sType1 = "KEEN"; sType2 = "Deadly";}
				if (iRoll==2){sType1 = "KEEN"; sType2 = "Deadly";}
				if (iRoll==3){sType1 = "FREEDOM"; sType2 = "Unstoppable";}
				if (iRoll==4){sType1 = "IMPROVED_EVASION"; sType2 = "Slippery";}
				if (iRoll==5){sType1 = "HASTE"; sType2 = "Speedy";}
				if (iRoll==6){sType1 = "TRUE_SEEING"; sType2 = "Ocular";}
			}
			break;
		}

		pName = sType2+" Gem";

		if (sType2=="Cracked")
		{
			sName = ColorString(pName, 255, 0, 0);
			SetName(oItem, sName);
			SetIdentified(oItem, TRUE);
			return;
		}
		else  sName = ColorString(pName, 127, 255, 212 );

		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", sType1);
	}
	else if (iRoll==9)	//:: Regen
	{
		iRoll = d4();
		sType1 = "REGENERATION";
		switch(iRange)
		{
			case 1: iVal1 = 1;break;          //1
			case 2: iVal1 = Random(2)+1;break; //1-2
			case 3: iVal1 = d3();break;        //1-3
			case 4: iVal1 = d3()+1;break;      //2-4
			case 5: iVal1 = d3()+2;break;      //3-5
		}
		sName = "+"+IntToString(iVal1)+" Regeneration Gem";
		sName = ColorString(sName, 127, 255, 212 );
		
		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", "REGENERATION");
		SetLocalInt(oItem, "AMOUNT", iVal1);
	}
	else if (iRoll==10)	//:: Damage rune
	{
		switch(iRange)
		{
			case 1: iVal1 = 5;break;   //  -/5 res
			case 2: iVal1 = 5;break;   //  -/5 res
			case 3: iVal1 = 10;break;  //  -/10 res
			case 4: iVal1 = 15;break;  //  -/15 res
			case 5: iVal1 = 20;break;  //  -/20 res
		}
		iRoll = d12();
		switch (iRoll)
		{
			case 1: {sType2 = "ACID_DAMAGE_RESISTANCE"; sType1 = "Alkaline Gem"; break;}
			case 2: {sType2 = "BLUDGEONING_DAMAGE_RESISTANCE"; sType1 = "Solid Gem";    break;}
			case 3: {sType2 = "COLD_DAMAGE_RESISTANCE"; sType1 = "Warm Gem";    break;}
			case 4: {sType2 = "DIVINE_DAMAGE_RESISTANCE";sType1 = "Absolution Gem";    break;}
			case 5: {sType2 = "ELECTRICAL_DAMAGE_RESISTANCE";sType1 = "Grounding Gem";    break;}
			case 6: {sType2 = "FIRE_DAMAGE_RESISTANCE"; sType1 = "Cool Gem";    break;}
			case 7: {sType2 = "MAGICAL_DAMAGE_RESISTANCE"; sType1 = "Mantle Gem";    break;}
			case 8: {sType2 = "NEGATIVE_DAMAGE_RESISTANCE"; sType1 = "Bright Gem";    break;}
			case 9: {sType2 = "PIERCING_DAMAGE_RESISTANCE"; sType1 = "Shell Gem";    break;}
			case 10: {sType2 = "POSITIVE_DAMAGE_RESISTANCE"; sType1 = "Malevolent Gem";    break;}
			case 11: {sType2 = "SLASHING_DAMAGE_RESISTANCE"; sType1 = "Mesh Gem";    break;}
			case 12: {sType2 = "SONIC_DAMAGE_RESISTANCE"; sType1 = "Dampening Gem";    break;}
		}
		if (iVal1==0)iVal1=2;
		sName = "-/"+IntToString(iVal1)+" "+sType1;
		sName = ColorString(sName, 127, 255, 212 );
		
		SetName(oItem, sName);
		SetLocalString(oItem, "GEM_TYPE", sType2);
		SetLocalInt(oItem, "AMOUNT", iVal1);
	}
}

void DropEpicSpellSeed(object oMob, object oSack)
{//:: Start DropEpicSpellSeed()
	int iEpic = GetIsEpicSpellcaster(oMob);
	
	string sItem;
	
	if(iEpic > 1)
		return;	
	
    int nESS = Random(29)+1;
    switch (nESS)
    {
        case 1: sItem = "epic_sp_unseenwa";    break;  //  Epic Spell Unseen Wanderer
        case 2: sItem = "epic_sd_afflict";      break;  //  Epic Spell Seed Afflict
        case 3: sItem = "epic_sd_animate";      break;  //  Epic Spell Seed Animate
        case 4: sItem = "epic_sd_animdead";     break;  //  Epic Spell Seed Animate Dead
        case 5: sItem = "epic_sd_armor";        break;  //  Epic Spell Seed Armor
        case 6: sItem = "epic_sd_banish";       break;  //  Epic Spell Seed Banish
        case 7: sItem = "epic_sd_compel";       break;  //  Epic Spell Seed Compel
        case 8: sItem = "epic_sd_conceal";      break;  //  Epic Spell Seed Conceal
        case 9: sItem = "epic_sd_conjure";      break;  //  Epic Spell Seed Conjure
        case 10: sItem = "epic_sd_contact";     break;  //  Epic Spell Seed Contact
        case 11: sItem = "epic_sd_delude";      break;  //  Epic Spell Seed Delude
        case 12: sItem = "epic_sd_destroy";     break;  //  Epic Spell Seed Destroy
        case 13: sItem = "epic_sd_dispel";      break;  //  Epic Spell Seed Dispel
        case 14: sItem = "epic_sd_energy";      break;  //  Epic Spell Seed Energy
        case 15: sItem = "epic_sd_foresee";     break;  //  Epic Spell Seed Foresee
        case 16: sItem = "epic_sd_fortify";     break;  //  Epic Spell Seed Forify
        case 17: sItem = "epic_sd_heal";        break;  //  Epic Spell Seed Heal
        case 18: sItem = "epic_sd_life";        break;  //  Epic Spell Seed Life
        case 19: sItem = "epic_sd_light";       break;  //  Epic Spell Seed Light
        case 20: sItem = "epic_sd_oppos";       break;  //  Epic Spell Seed Opposition
        case 21: sItem = "epic_sd_reflect";     break;  //  Epic Spell Seed Reflect
        case 22: sItem = "epic_sd_reveal";      break;  //  Epic Spell Seed Reveal
        case 23: sItem = "epic_sd_shadow";      break;  //  Epic Spell Seed Shadow
        case 24: sItem = "epic_sd_slay";        break;  //  Epic Spell Seed Slay
        case 25: sItem = "epic_sd_summon";      break;  //  Epic Spell Seed Summon
        case 26: sItem = "epic_sd_time";        break;  //  Epic Spell Seed Time
        case 27: sItem = "epic_sd_transfrm";    break;  //  Epic Spell Seed Transform
        case 28: sItem = "epic_sd_transprt";    break;  //  Epic Spell Seed Transport
        case 29: sItem = "epic_sd_ward";        break;  //  Epic Spell Seed Ward
    }

    if ((GetLocalInt(GetModule(), "LOOT_DEBUG") > 0 ))
        {
            SpeakString("DropEpicSpellSeed()");
            SpeakString("Item rolled case "+IntToString(nESS)+" sItem "+sItem);
        }
		
	object oItem = CreateItemOnObject("sItem", oSack, 1);	
	
}

void DropEpicSpellBook(object oMob, object oSack)
{//:: Start DropEpicSpellBook()
	int iEpic = GetIsEpicSpellcaster(oMob);
	
	if(iEpic > 1)
		return;	
	
	string sItem;
	
    int nESB = Random(60)+1;
    switch (nESB)
    {
		case 1: sItem = "epic_sp_audstone";    break;  //  Epic Spell Audience of Stone
		case 2: sItem = "epic_sp_achilles";    break;  //  Epic Spell Achilles Heel
		case 3: sItem = "epic_sp_allmart";     break;  //  Epic Spell Allied Martyr
		case 4: sItem = "epic_sp_allhope";     break;  //  Epic Spell All Hope Lost
		case 5: sItem = "epic_sp_anarchy";     break;  //  Epic Spell Anarchy's Call
		case 6: sItem = "epic_sp_animblas";    break;  //  Epic Spell Animus Blast
		case 7: sItem = "epic_sp_animbliz";    break;  //  Epic Spell Animus Blizzard
		case 8: sItem = "epic_sp_armyunfa";    break;  //  Epic Spell Army Unfallen
		case 9: sItem = "epic_sp_batbound";    break;  //  Epic Spell Battle Bounding
		case 10: sItem = "epic_sp_champval";    break;  //  Epic Spell Champion's Valor
		case 11: sItem = "epic_sp_contresu";    break;  //  Epic Spell Contingent Resurrection
		case 12: sItem = "epic_sp_contreun";    break;  //  Epic Spell Contingent Reunion
		case 13: sItem = "epic_sp_deadeyes";    break;  //  Epic Spell Deadeye Sense
		case 14: sItem = "epic_sp_dreamscp";    break;  //  Epic Spell Dreamscape
		case 15: sItem = "epic_sp_deathmrk";    break;  //  Epic Spell Deathmark
		case 16: sItem = "epic_sp_dullblad";    break;  //  Epic Spell Dullblades
		case 17: sItem = "epic_sp_dweomert";    break;  //  Epic Spell Dweomer Thief
		case 18: sItem = "epic_sp_enslave";     break;  //  Epic Spell Enslave
		case 19: sItem = "epic_sp_epmagarm";    break;  //  Epic Spell Epic Mage Armor
		case 20: sItem = "epic_sp_eprepuls";    break;  //  Epic Spell Epic Repulsion
		case 21: sItem = "epic_sp_epspellr";    break;  //  Epic Spell Epic Spell Reflection
		case 22: sItem = "epic_sp_epwardin";    break;  //  Epic Spell Epic Warding
		case 23: sItem = "epic_sp_eterfree";    break;  //  Epic Spell Eternal Freedom
		case 24: sItem = "epic_sp_fleetnes";    break;  //  Epic Spell Fleetness of Foot
		case 25: sItem = "epic_sp_greatrui";    break;  //  Epic Spell Greater Ruin
		case 26: sItem = "epic_sp_grspellr";    break;  //  Epic Spell Greater Spell Resistance
		case 27: sItem = "epic_sp_grtimest";    break;  //  Epic Spell Greater Timestop
		case 28: sItem = "epic_sp_hellball";    break;  //  Epic Spell Hellball
		case 29: sItem = "epic_sp_hellsend";    break;  //  Epic Spell Hell Send
		case 30: sItem = "epic_sp_hercalli";    break;  //  Epic Spell Herculean Alliance
		case 31: sItem = "epic_sp_hercempo";    break;  //  Epic Spell Herculean Empowerment
		case 32: sItem = "epic_sp_impenetr";    break;  //  Epic Spell Impenetrability
		case 33: sItem = "epic_sp_leechfie";    break;  //  Epic Spell Leech Field
		case 34: sItem = "epic_sp_legendar";    break;  //  Epic Spell Legendary Artisan
		case 35: sItem = "epic_sp_lifeforc";    break;  //  Epic Spell Life Force Transfer
		case 36: sItem = "epic_sp_magmabur";    break;  //  Epic Spell Magma Burst
		case 37: sItem = "epic_sp_momentom";    break;  //  Epic Spell Momento Mori
		case 38: sItem = "epic_sp_mummdust";    break;  //  Epic Spell Mummy Dust
		case 39: sItem = "epic_sp_nailedsk";    break;  //  Epic Spell Nailed to the Sky
		case 40: sItem = "epic_sp_nightsun";    break;  //  Epic Spell Night's Undoing
		case 41: sItem = "epic_sp_orderres";    break;  //  Epic Spell Order Restored
		case 42: sItem = "epic_sp_pathsbec";    break;  //  Epic Spell Paths Become Known
		case 43: sItem = "epic_sp_peerless";    break;  //  Epic Spell Peerless Penitence
		case 44: sItem = "epic_sp_pestilen";    break;  //  Epic Spell Pestilence
		case 45: sItem = "epic_sp_planarce";    break;  //  Epic Spell Planar Cell
		case 46: sItem = "epic_sp_psionics";    break;  //  Epic Spell Psionic Salvo
		case 47: sItem = "epic_sp_risenreu";    break;  //  Epic Spell Risen Reunited
		case 48: sItem = "epic_sp_ruin";        break;  //  Epic Spell Ruin
		case 49: sItem = "epic_sp_singsund";    break;  //  Epic Spell Singular Sunder
		case 50: sItem = "epic_sp_spelworm";    break;  //  Epic Spell Spell Worm
		case 51: sItem = "epic_sp_stormman";    break;  //  Epic Spell Storm Mantle
		case 52: sItem = "epic_sp_summaber";    break;  //  Epic Spell Summon Aberration
		case 53: sItem = "epic_sp_superbdi";    break;  //  Epic Spell Superb Dispelling
		case 54: sItem = "epic_sp_symrusta";    break;  //  Epic Spell Symrustar's Spellbinding
		case 55: sItem = "epic_sp_thewithe";    break;  //  Epic Spell The Withering
		case 56: sItem = "epic_sp_tolodine";    break;  //  Epic Spell Tolodine's Killing Wind
		case 57: sItem = "epic_sp_transvit";    break;  //  Epic Spell Transcendent Vitality
		case 58: sItem = "epic_sp_twinfien";    break;  //  Epic Spell Twinfiend
		case 59: sItem = "epic_sp_unholydi";    break;  //  Epic Spell Unholy Disciple
		case 60: sItem = "epic_sp_unimping";    break;  //  Epic Spell Unimpinged
    }

    if ((GetLocalInt(GetModule(), "LOOT_DEBUG") > 0 ))
        {
            SpeakString("DropEpicSpellBook()");
            SpeakString("Item rolled case "+IntToString(nESB)+" sItem "+sItem);
        }
		
	object oItem = CreateItemOnObject("sItem", oSack, 1);		
}

void sd_droploot (object oMob, object oSack)
{
	object oKiller = GetLastKiller();
	//effect eVFX = EffectVisualEffect(VFX_IMP_REMOVE_CONDITION);
	//effect eDust = EffectVisualEffect(VFX_IMP_DUST_EXPLOSION);
	effect eLink /* = EffectLinkEffects(eVFX, eDust) */;

	// no loot if spawned in Ascension
	// chances are it was an uber guard
	// and not the PC that did the killing.
	// This is to prevent easy looting higher mobs

	if (GetTag(GetArea(oMob))=="TownofAscension")	return;
	
	if (GetTag(GetArea(oMob))=="TrespassersTavern")	return;
	
	if (GetClassByPosition(1, oMob) == CLASS_TYPE_COMMONER)	return;

	// animals dont usually carry wares - but you can skin em!
	// This is to prevent tiny rats dropping full plate mail - can't have that!


	/////////////////////////////////////////
	//::Droprate config::
	//
	int DamBroke = 0;
	int lMod, mMod;

	// adjust the droprate modifIer based on game difficulty
	// if there is no game difficulty: Default to normal
	int iDiff = GetLocalInt(GetModule(), "sd_game_diff");
	if (iDiff==0)lMod = DROP_RATE; //default
	if (iDiff==1)lMod = DROP_RATE;
	if (iDiff==2)lMod = DROP_RATE;

	// Make monk gloves a rare drop except when the player is a lvl 5+ monk
	if (GetHasFeat(FEAT_SUPERIOR_UNARMED_STRIKE, oKiller)) mMod = 4;

	if (GetLevelByClass(CLASS_TYPE_MONK, oKiller)>5||
	GetLevelByClass(CLASS_TYPE_MONK, GetMaster(oKiller))>5)mMod = 8;

	else mMod=lMod-1;

//:: Bosses have high chance to drop loot (never broken or worn)
	if (GetLocalInt(oMob, "BOSS")==1){lMod = 85; DamBroke = 1;}

	int WeapChance =    1;		// % chance to drop a weapon
	int MonkChance =    1;		// % chance to drop monk gloves
	int SockChance =    1;		// % chance to drop a socketed item
	int ArmorChance =   1;		// % chance to drop armor or a shield
	int MItemChance =   1;		// % chance to drop a magic item
	int RodWandChance = 1;		// % chance to drop a wand/rod item
	int AmmoChance =    1;		// % chance to drop a bolt or an arrow
	int GoldChance =    0;		// % chance to drop some gold
	int PotChance =     1;		// % chance to drop a potion
	int ScrollChance =  1;		// % chance to drop a magic scroll
	int GemChance =     lMod;	// % chance to drop a socket gem
	int MiscChance =    1;		// % chance to drop a miscellaneous item
	int SetItemChance = 1;		// % chance to drop a class item setpiece
	int AlchemyChance = 1;
	int EpicSpellSeedChance = 1;
	int EpicSpellBookChance = 1;	
	//
	//
	/////////////////////////////////////////////////
	//::initiate variables::                       //

	int iDice;
	int iHD = GetHitDice(oMob);
	int iRange;
	int iMage;

	/////////////////////////////////////////////////

	//:: only casters drop scrolls, wands and rods

	if (GetLevelByClass(CLASS_TYPE_DRUID)>0||GetLevelByClass(CLASS_TYPE_BARD)>0
	||GetLevelByClass(CLASS_TYPE_CLERIC)>0||GetLevelByClass(CLASS_TYPE_SORCERER)>0
	||GetLevelByClass(CLASS_TYPE_WIZARD)>0) iMage=1;

	/////////////////////////////////////////////////
	//:: Quality range based on level of monster::

	if (iHD > 0  && iHD < 11)iRange=1;			// lvl 1-10
	if (iHD > 10 && iHD < 19)iRange=2;			// lvl 11-18
	if (iHD > 18 && iHD < 28)iRange=3;			// lvl 19-27
	if (iHD > 27 && iHD < 36)iRange=4;			// lvl 28-35
	if (iHD > 35 /* && iHD < 61 */)iRange=5;	// lvl 36-40

//:: Chance of a more powerful item
	if (LUCK_CHANCE>0)
	{
		iDice = Random(LUCK_CHANCE);
		if (iDice==LUCK_CHANCE){++iRange; if (iRange==6)iRange=5;}
	}

///////////////////
//:Debugging
//:
// FloatingTextStringOnCreature("* Luck has favoured you *", GetFirstPC());}

////////////////////////////////////////////////

//:: Gold Roll
	iDice = d100();
	if (iDice<GoldChance+1)
	{
		DropGold(oMob, oSack, DamBroke);
	}
	if (iDice<GoldChance+1)
	{
		FloatingTextStringOnCreature("Your defeated foe has dropped gold!", oKiller); 
	}
	if (iDice<GoldChance+1)
	{
		ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eLink, GetLocation(oSack));
	}

//:: Rod/Wand Roll
	iDice = d100();
	if (iDice<RodWandChance+1)
	{
		DropRodWand(oMob, oSack); 
	}
	if (iDice<RodWandChance+1) 
	{
		FloatingTextStringOnCreature("Your defeated foe has dropped a wand!", oKiller);
	}
	if (iDice<RodWandChance+1)
	{
		ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eLink, GetLocation(oSack));
	}

// SetItem Roll ** Comming Soon **
	//iDice = d100();
	//if (iDice<SetItemChance+1)DropSetItem(oMob, oSack);

//:: Weapon Roll
	iDice = d100();
	if (iDice < WeapChance+1)
	{
		DropWeapon(oMob, oSack, iRange, SockChance, DamBroke);
		FloatingTextStringOnCreature("Your defeated foe has dropped a weapon!", oKiller); 		
		ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eLink, GetLocation(oSack));
	}

//:: Monk Gloves Roll
	iDice = d100();
	if (iDice<MonkChance + 1)
	{
		DropMonkGloves(oMob, oSack, iRange, SockChance, DamBroke);
		FloatingTextStringOnCreature("Your defeated foe has dropped gloves!", oKiller);
		ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eLink, GetLocation(oSack));
	}

//:: Armor or shield Roll
	iDice = d100();
	if (iDice < ArmorChance + 1)
	{
		iDice = d100();
		if (iDice>59)DropShield(oMob, oSack, iRange, SockChance, DamBroke);
		else DropArmor(oMob, oSack, iRange, SockChance, DamBroke);
		if (iDice<ArmorChance+1) FloatingTextStringOnCreature("Your defeated foe has dropped armor!", oKiller);
		if (iDice<ArmorChance+1) ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eLink, GetLocation(oSack));
		if (iDice>59) FloatingTextStringOnCreature("Your defeated foe has dropped armour!", oKiller);
		if (iDice>59) ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eLink, GetLocation(oSack));
	}

//:: Magic Item Roll
	iDice = d100();
	if (iDice < MItemChance + 1)
	{
		DropMagicItem(oMob, oSack, iRange, SockChance, DamBroke);
		FloatingTextStringOnCreature("Your defeated foe has dropped a magic item!", oKiller);
		ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eLink, GetLocation(oSack));
	}
	
//:: Misc Item Roll
	iDice = d100();
	if (iDice < MiscChance + 1)
	{
		DropMisc(oMob, oSack);
		FloatingTextStringOnCreature("Your defeated foe has dropped an artifact!", oKiller);
		ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eLink, GetLocation(oSack));
	}
	
//:: Ranged Ammo Roll
	iDice = d100();
	if (iDice < AmmoChance + 1)
	{
		DropAmmo(oMob, oSack, iRange);
		FloatingTextStringOnCreature("Your defeated foe has dropped ranged ammunition!", oKiller);
	}
	
//:: Potion Roll
	iDice = d100();
	if (iDice < PotChance + 1)
	{
		DropPotion(oMob, oSack);
		FloatingTextStringOnCreature("Your defeated foe has dropped a potion!", oKiller);
	}
	
//:: Scroll Roll
	iDice = d100();
	if (iDice < ScrollChance + 1)
	{
		DropScroll(oMob, oSack, iRange);
		FloatingTextStringOnCreature("Your defeated foe has dropped a scroll!", oKiller);
	}

//:: Alchemical Item Roll
	iDice = d100();
	if (iDice < AlchemyChance + 1) 
	{
		DropAlchemy(oMob, oSack, iRange);
	}	
	
//:: Epic Spell Seed Item Roll
	iDice = d100();
	if(iRange > 3)
	{
		if (iDice < EpicSpellSeedChance + 1) 
		{
			DropEpicSpellSeed(oMob, oSack);	
			FloatingTextStringOnCreature("Your defeated foe has dropped an epic spell seed!", oKiller);
		}
	}
	
//:: Epic Book Book Item Roll
	iDice = d100();
	if(iRange > 4)
	{
		if (iDice < EpicSpellBookChance + 1) 
		{
			DropEpicSpellBook(oMob, oSack);	
			FloatingTextStringOnCreature("Your defeated foe has dropped an epic spell book!", oKiller);
		}
	}
}

void sd_dropboss(object oMob, object oSack)
{
    object oKiller = GetLastKiller();

//:: Early exit if mob is killed in Stonewatch to prevent looting high-end mobs easily
    if (GetTag(GetArea(oMob)) == "Stonewatch") return;

//:: Droprate configuration
    int DamBroke = 0;
    int lMod = DROP_RATE; // Default drop rate
    int mMod; // Modifyer for monk gloves drop rareness

//:: Determine the game difficulty modifier
    int iDiff = GetLocalInt(GetModule(), "sd_game_diff");
    if(iDiff > 0) lMod = DROP_RATE; // No change needed if not default because all set to same

//:: Make monk gloves a rare drop unless the player/Master is a lvl 5+ monk
    if (GetLevelByClass(CLASS_TYPE_MONK, oKiller) > 5 || GetLevelByClass(CLASS_TYPE_MONK, GetMaster(oKiller)) > 5)
        mMod = 8;
    else 
        mMod = lMod - 1;

//:: Boss specific settings
    if (GetLocalInt(oMob, "BOSS") == 1)
    {
        lMod = 85; // Higher chance for bosses
        DamBroke = 1; // Boss items are never broken or worn
    }

//:: Droprate chances initialization
    int WeapChance = 5;
    int MonkChance = 3;
    int SockChance = 1;
    int ArmorChance = 5;
    int MItemChance = 3;
    int RodWandChance = 1;
    int AmmoChance = 1;
    int GoldChance = 100;
    int PotChance = 65;
    int ScrollChance = 45;
    int GemChance = 1;
    int MiscChance = 1;
    int SetItemChance = 1;
	int AlchemyChance = 1;
	int EpicSpellSeedChance = 1;
	int EpicSpellBookChance = 1;	

//:: Determine quality range based on monster level
    int iHD = GetHitDice(oMob);
    int iRange = (iHD + 9) / 10; // This formula directly computes the range from HD
    if (iRange > 5) iRange = 5; // Cap the range at 5

//:: Chance of a more powerful item based on luck
    if (LUCK_CHANCE > 0 && Random(LUCK_CHANCE) == LUCK_CHANCE - 1)
        iRange = iRange < 5 ? iRange + 1 : 5;

//:: Only casters drop scrolls, wands, and rods
    int iMage = GetLevelByClass(CLASS_TYPE_DRUID, oMob) > 0 || GetLevelByClass(CLASS_TYPE_BARD, oMob) > 0 ||
                GetLevelByClass(CLASS_TYPE_CLERIC, oMob) > 0 || GetLevelByClass(CLASS_TYPE_SORCERER, oMob) > 0 ||
                GetLevelByClass(CLASS_TYPE_WIZARD, oMob) > 0 ? 1 : 0;

//:: Drop functions based on the outcome of random chances
    int iDice;
    
//:: Gold Drop
    iDice = d20();
    if (iDice < GoldChance + 1)
        DropGold(oMob, oSack, DamBroke);

//:: Rod/Wand Drop, only for casters
    if (iMage)
    {
        iDice = d100();
        if (iDice < RodWandChance + 1)
            DropRodWand(oMob, oSack);
    }

//:: Weapon Drop
    iDice = d10();
    if (iDice < WeapChance + 1)
        DropWeapon(oMob, oSack, iRange, SockChance, DamBroke);

//:: Monk Gloves Drop
    iDice = d20();
    if (iDice < MonkChance + 1) 
        DropMonkGloves(oMob, oSack, iRange, SockChance, DamBroke);

//:: Armor or Shield Drop
    iDice = d20();
    if (iDice < ArmorChance + 1) 
    {
        iDice = d100();
        if (iDice > 59) 
            DropShield(oMob, oSack, iRange, SockChance, DamBroke);
        else 
            DropArmor(oMob, oSack, iRange, SockChance, DamBroke);
    }

//:: Magic Item Drop
    iDice = d20();
    if (iDice < MItemChance + 1) 
        DropMagicItem(oMob, oSack, iRange, SockChance, DamBroke);

//:: Misc Item Drop
    iDice = d100();
    if (iDice < MiscChance + 1)
        DropMisc(oMob, oSack);

//:: Ranged Ammo Drop
    iDice = d100();
    if (iDice < AmmoChance + 1)
        DropAmmo(oMob, oSack, iRange);

//:: Potion Drop
    iDice = d100();
    if (iDice < PotChance + 1)
        DropPotion(oMob, oSack);

//:: Scroll Drop
    iDice = d100();
    if (iDice < ScrollChance + 1)
        DropScroll(oMob, oSack, iRange);

//:: Alchemical Item Roll
	iDice = d100();
	if (iDice < AlchemyChance+1) 
	{
		DropAlchemy(oMob, oSack, iRange);
	}		
	
//:: Epic Spell Seed Item Roll
	iDice = d100();
	if (iDice < EpicSpellSeedChance + 1) 
	{
		DropEpicSpellSeed(oMob, oSack);	
	}	
	
//:: Epic Book Book Item Roll
    if (iMage)
    {
		iDice = d100();
		if (iDice < EpicSpellBookChance + 1) 
		{
			DropEpicSpellBook(oMob, oSack);	
		}
	}		
}

void wk_chestloot (object oSack)
{
	object oKiller = GetLastUsedBy();
	object oMob = GetLastUsedBy();
	/////////////////////////////////////////
	//::Droprate config::
	//
	int DamBroke = 0;
	int lMod;

	lMod = DROP_RATE; //default

	/////////////////////////////////////////////////
	//::initiate variables::                       //
	int iDice;
	int iHD = GetLocalInt(OBJECT_SELF,"BossLoot");
	int iRange = iHD;
	int iMage;


	// Bosses have high chance to drop loot (never broken or worn)
	lMod = lMod + iHD;


	int WeapChance =    3+lMod;        // % chance to drop a weapon
	int SockChance =    1;            // % chance to drop a socketed item
	int ArmorChance =   2+lMod;        // % chance to drop armor or a shield
	int MItemChance =   2+lMod;        // % chance to drop a magic item
	int RodWandChance = 1+lMod;        // % chance to drop a wand/rod item
	int AmmoChance =    1+lMod;        // % chance to drop a bolt or an arrow
	int GoldChance =    46+lMod;       // % chance to drop some gold
	int PotChance =     1+lMod;       // % chance to drop a potion
	int ScrollChance =  1+lMod;       // % chance to drop a magic scroll
	int GemChance =     lMod;          // % chance to drop a socket gem
	int MiscChance =    1;             // % chance to drop a miscellaneous item
	int SetItemChance = 1;             // % chance to drop a class item setpiece
	int AlchemyChance = 1+lMod;

	//:: Chance of a more powerful item

	if (LUCK_CHANCE>0)
	{
		iDice = Random(LUCK_CHANCE);
		if (iDice == 1) iRange++;
		if (iRange > 5) iRange=5;
	}

	////////////////////////////////////////////////
	// Cycle through the various drops until the number of items for the boss are
	// all dropped.  This guarantees that each boss level will be distinct.
	// Note that the luck chance to increase the value of the loot does not
	// affect the number of items to drop.


	// Rod/Wand Roll
	iDice = d100();
	if (iDice<RodWandChance+1)DropRodWand(oMob, oSack);

	// SetItem Roll ** Comming Soon **
	//iDice = d100();
	//if (iDice<SetItemChance+1)DropSetItem(oMob, oSack);

	// Weapon Roll
	iDice = d20();
	if (iDice<WeapChance+1)DropWeapon(oMob, oSack, iRange, SockChance, DamBroke);

	// Armor or shield Roll
	iDice = d20();
	if (iDice < ArmorChance+1)
	{
		iDice = d100();
		if (iDice>69)
		{
			DropShield(oMob, oSack, iRange, SockChance, DamBroke);
		}
		else 
		{
			DropArmor(oMob, oSack, iRange, SockChance, DamBroke);
		}
	}
	// Magic Item Roll
	iDice = d20();
	if (iDice<MItemChance+1)DropMagicItem(oMob, oSack, iRange, SockChance, DamBroke);

	// Misc Item Roll
	iDice = d100();
	if (iDice<MiscChance+1)DropMisc(oMob, oSack);

	// Ranged Ammo Roll
	iDice = d100();
	if (iDice<AmmoChance+1)DropAmmo(oMob, oSack, iRange);

	// Pot Roll
	iDice = d20();
	if (iDice<PotChance+1)DropPotion(oMob, oSack);

	// Pot Roll
	iDice = d20();
	if (iDice<PotChance+1)DropPotion(oMob, oSack);

	// Scroll Roll
	iDice = d100();
	if (iDice<ScrollChance+1)DropScroll(oMob, oSack, iRange);

	// Alchemical Item Roll
	iDice = d100();
	if (iDice < AlchemyChance+1) DropAlchemy(oMob, oSack, iRange);	
}

void hy_barrelloot (object oSack)
{
	object oKiller = GetLastUsedBy();
	object oMob = GetLastUsedBy();
	/////////////////////////////////////////
	//::Droprate config::
	//
	int DamBroke = 0;
	int lMod;

	lMod = DROP_RATE; //default

	/////////////////////////////////////////////////
	//::initiate variables::                       //
	int iDice;
	int iHD = GetLocalInt(OBJECT_SELF,"BossLoot");
	int iRange = iHD;
	int iMage;

	// Bosses have high chance to drop loot (never broken or worn)
	lMod = lMod + iHD;

	int PotChance =     25+lMod;       // % chance to drop a potion
	int ScrollChance =  5+lMod;       // % chance to drop a magic scroll
	int AlchemyChance = 1+lMod;
	
	////////////////////////////////////////////////
	// Cycle through the various drops until the number of items for the boss are
	// all dropped.  This guarantees that each boss level will be distinct.
	// Note that the luck chance to increase the value of the loot does not
	// affect the number of items to drop.

	// SetItem Roll ** Comming Soon **
	//iDice = d100();
	//if (iDice<SetItemChance+1)DropSetItem(oMob, oSack);

	// Alchemical Item Roll
	iDice = d100();
	if (iDice < AlchemyChance+1) DropAlchemy(oMob, oSack, iRange);
	
	// Potion Roll
	iDice = d100();
	if (iDice<PotChance+1)DropPotion(oMob, oSack);

	// Potion Roll
	iDice = d100();
	if (iDice<PotChance+1)DropPotion(oMob, oSack);

	// Scroll Roll
	iDice = d100();
	if (iDice<ScrollChance+1)DropScroll(oMob, oSack, iRange);
}

void hy_bookcaseloot (object oSack)
{
	object oKiller = GetLastUsedBy();
	object oMob = GetLastUsedBy();
	/////////////////////////////////////////
	//::Droprate config::
	//
	int DamBroke = 0;
	int lMod;

	lMod = DROP_RATE; //default

	/////////////////////////////////////////////////
	//::initiate variables::                       //
	int iDice;
	int iHD = GetLocalInt(OBJECT_SELF,"BossLoot");
	int iRange = iHD;
	int iMage;


	// Bosses have high chance to drop loot (never broken or worn)
	lMod = lMod + iHD;

	int ScrollChance =  50+lMod;       // % chance to drop a magic scroll

	////////////////////////////////////////////////
	// Cycle through the various drops until the number of items for the boss are
	// all dropped.  This guarantees that each boss level will be distinct.
	// Note that the luck chance to increase the value of the loot does not
	// affect the number of items to drop.

	// SetItem Roll ** Comming Soon **
	//iDice = d100();
	//if (iDice<SetItemChance+1)DropSetItem(oMob, oSack);

	// Scroll Roll
	iDice = d100();
	if (iDice<ScrollChance+1)DropScroll(oMob, oSack, iRange);

	// Scroll Roll
	iDice = d100();
	if (iDice<ScrollChance+1)DropScroll(oMob, oSack, iRange);
}

///////////////////////////
//: For test compiling only
//::void main(){}