//New enchantarmor 10-21-05
//#include "_persist_01a"
#include "x2_inc_itemprop"
#include "aps_include"
void GetNextItemPossessedBy(object oPC, string sItemTag);
void GetNextStackedItem(object oPC, string sEssenceTag, int iCount);
void CreateAnObject(string sResource, object oPC, int iStackSize);
object CreatePlaceable(string sObject, location lPlace, float fDuration);
object oTestValid = OBJECT_INVALID;
void EnchantArmor(object oItem,int wBonus);
void EnPowerArmor(object oItem,int DamageType,int DamageQty);
void GiveToPC(object oItem, object oPC);
string sDialogResRef="";
string sEssence;
void main()
{
  object oPC = GetLastDisturbed();
  object oItem = GetInventoryDisturbItem();
  string sItemTag = GetTag(oItem);
  string sReEnchantMessage = "INVALID";
  int iReEnchant = 0;
  int iBaseItem=0;
  int iMaxReEnchant;
  int iMaxReEnchant2;
  int iMaxReEnchant3;
  int wBonus=1;
  int wEffect=0;
  int iEnpower=0;

int iEnchantSkill = GetPersistentInt(oPC,"iEnchantSkill","UOACraft");
int iEnchantChance = iEnchantSkill;


  if (GetInventoryDisturbType()!= INVENTORY_DISTURB_TYPE_ADDED)
   {
     if (sItemTag=="ENCHANT_ARMOR1")
     {
      SetLocalInt(oPC,"iEnchant",1);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +1.",oPC,FALSE);
     }

    if (sItemTag=="ENCHANT_ARMOR2")
     {
     if (iEnchantChance >= 100)
      {
      SetLocalInt(oPC,"iEnchant",2);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +2.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENCHANT_ARMOR3")
     {
     if (iEnchantChance >= 157)
      {
      SetLocalInt(oPC,"iEnchant",3);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +3.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENCHANT_ARMOR4")
     {
     if (iEnchantChance >= 214)
      {
      SetLocalInt(oPC,"iEnchant",4);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +4.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENCHANT_ARMOR5")
     {
     if (iEnchantChance >= 271)
      {
      SetLocalInt(oPC,"iEnchant",5);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +5",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENCHANT_ARMOR6")
     {
     if (iEnchantChance >= 328)
      {
      SetLocalInt(oPC,"iEnchant",6);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +6.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENCHANT_ARMOR7")
     {
     if (iEnchantChance >= 385)
      {
      SetLocalInt(oPC,"iEnchant",7);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +7.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }
    if (sItemTag=="ENCHANT_ARMOR8")
     {
     if (iEnchantChance >= 442)
      {
      SetLocalInt(oPC,"iEnchant",8);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +8.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }
    if (sItemTag=="ENCHANT_ARMOR9")
     {
     if (iEnchantChance >= 500)
      {
      SetLocalInt(oPC,"iEnchant",9);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +9.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }
    if (sItemTag=="ENCHANT_ARMOR10")
     {
     if (iEnchantChance >= 590)
      {
      SetLocalInt(oPC,"iEnchant",10);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +10.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }
    if (sItemTag=="ENCHANT_ARMOR11")
     {
     if (iEnchantChance >= 640)
      {
      SetLocalInt(oPC,"iEnchant",11);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +11.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }
    if (sItemTag=="ENCHANT_ARMOR12")
     {
     if (iEnchantChance >= 730)
      {
      SetLocalInt(oPC,"iEnchant",12);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +12.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }
    if (sItemTag=="ENCHANT_ARMOR13")
     {
     if (iEnchantChance >= 840)
      {
      SetLocalInt(oPC,"iEnchant",13);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +13.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }
    if (sItemTag=="ENCHANT_ARMOR14")
     {
     if (iEnchantChance >= 950)
      {
      SetLocalInt(oPC,"iEnchant",14);
      SetLocalInt(oPC,"iEnpower",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enchant a HELM to +14.",oPC,FALSE);
      }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enchant Armor with this.",oPC,FALSE);
      }
    if (sItemTag=="ENPOWER_ARMOR1")
     {
     if (iEnchantChance >= 500)
      {
      SetLocalInt(oPC,"iEnpower",1);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Acid Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR2")
     {
     if (iEnchantChance >= 500)
      {
      SetLocalInt(oPC,"iEnpower",2);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Cold Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR3")
     {
     if (iEnchantChance >= 500)
      {
      SetLocalInt(oPC,"iEnpower",3);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Electrical Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR4")
     {
     if (iEnchantChance >= 500)
      {
      SetLocalInt(oPC,"iEnpower",4);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Fire Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR5")
     {
     if (iEnchantChance >= 700)
      {
      SetLocalInt(oPC,"iEnpower",5);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Acid Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR6")
     {
     if (iEnchantChance >= 700)
      {
      SetLocalInt(oPC,"iEnpower",6);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Cold Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR7")
     {
     if (iEnchantChance >= 700)
      {
      SetLocalInt(oPC,"iEnpower",7);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Electrical Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR8")
     {
     if (iEnchantChance >= 700)
      {
      SetLocalInt(oPC,"iEnpower",8);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Fire Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR9")
     {
     if (iEnchantChance >= 800)
      {
      SetLocalInt(oPC,"iEnpower",9);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Acid Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR10")
     {
     if (iEnchantChance >= 800)
      {
      SetLocalInt(oPC,"iEnpower",10);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Cold Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR11")
     {
     if (iEnchantChance >= 800)
      {
      SetLocalInt(oPC,"iEnpower",12);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Fire Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR12")
     {
     if (iEnchantChance >= 800)
      {
      SetLocalInt(oPC,"iEnpower",13);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Slashing Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR13")
     {
     if (iEnchantChance >= 900)
      {
      SetLocalInt(oPC,"iEnpower",14);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Piercing Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR14")
     {
     if (iEnchantChance >= 900)
      {
      SetLocalInt(oPC,"iEnpower",15);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Bludgeoning Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR15")
     {
     if (iEnchantChance >= 900)
      {
      SetLocalInt(oPC,"iEnpower",16);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Negative Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    if (sItemTag=="ENPOWER_ARMOR16")
     {
     if (iEnchantChance >= 800)
      {
      SetLocalInt(oPC,"iEnpower",11);
      SetLocalInt(oPC,"iEnchant",0);
      FloatingTextStringOnCreature("Enchanting forge is now set to Enpower a HELM with Electrical Resist.",oPC,FALSE);
     }
      else
      FloatingTextStringOnCreature("You do not have sufficient skill to enpower Armor with this.",oPC,FALSE);
      }

    CopyObject(oItem,GetLocation(oPC),OBJECT_SELF,GetTag(oItem));
    DestroyObject(oItem);
    return;
   }


   // The following 3 lines are to ensure compatability with UOAbigal's Persistent Token System.
   // You can replace them with whatever 'no-drop' code you have or comment them out.
   string sNoDropFlag = (GetStringLeft(GetTag(oItem),6));
   if (sNoDropFlag == "NoDrop" || sNoDropFlag == "TOKEN_"||sNoDropFlag=="_TBOX_")
    return;
   if (GetBaseItemType(oItem)==BASE_ITEM_LARGEBOX)
    {
     DestroyObject(oItem);
     SendMessageToPC(oPC,"To avoid possible dupe exploits, the container placed in this bag may be destroyed.");
     return;
    }
   // End of compatability portion.

   if (GetLocalInt(OBJECT_SELF,"iAmInUse")!=0)
    {
     FloatingTextStringOnCreature("You must wait for this item to finish enchanting..",oPC,FALSE);
     CopyItem(oItem,oPC,TRUE);
     DestroyObject(oItem);
     return;
    }

   iBaseItem = GetBaseItemType(oItem);
   string sItemResRef = "";
   int iStackSize = 0;
   int iMaxStack = 5;
   string sSuccess = "";

// Determins the PC level in the class WIZARD (CLASS_TYPE_WIZARD)
/*    int wizard_lvl = 0;
    wizard_lvl = GetLevelByClass(CLASS_TYPE_WIZARD,oPC);
    SendMessageToPC(oPC,"Your Level in Wizard == " + IntToString(wizard_lvl));

    if (wizard_lvl <=7 )
     {
       CopyItem(oItem,oPC,TRUE);
       FloatingTextStringOnCreature("You are not a powerful enough wizard you cannot Enchant this item!",oPC,FALSE);
       DestroyObject(oItem);
       return;
    }
*/

   if (GetStringLeft(sItemTag,5) != "HELM_")
    {
       CopyItem(oItem,oPC,TRUE);
       FloatingTextStringOnCreature("You cannot Enchant this item!",oPC,FALSE);
       DestroyObject(oItem);
       return;
    }

   //int iEnchantChance = GetTokenPair(oPC,14,2);
//  int iEnchantSkill = GetCampaignInt("UOACraft","iEnchantSkill",oPC);
//  int iEnchantChance = iEnchantSkill;


   if (iEnchantChance <=350)
    {
     iEnchantChance = GetAbilityScore(oPC,ABILITY_INTELLIGENCE)*3;
     iEnchantChance = iEnchantChance + GetAbilityScore(oPC,ABILITY_DEXTERITY) + GetAbilityScore(oPC,ABILITY_WISDOM);
     iEnchantChance = iEnchantChance * 3;
     if (iEnchantChance >350) iEnchantChance = 350;
     if (iEnchantSkill > iEnchantChance) iEnchantChance = iEnchantSkill;
    }

   if (sItemResRef=="INVALID")
    {
       SendMessageToPC(oPC,"You cannot Enchant this item!");
       CopyItem(oItem,oPC,TRUE);
       DestroyObject(oItem,0.1);
       return;
    }

   if (iEnchantChance < 1)
    {
     if (iReEnchant==0)
       {
        FloatingTextStringOnCreature("You have no idea how to Enchant this item.",oPC,FALSE);
       }
      else
       {
        FloatingTextStringOnCreature("You have no idea how to reEnchant this item.",oPC,FALSE);
       }
     CopyItem(oItem,oPC,TRUE);
     DestroyObject(oItem);
     return;
    }

   string sFlame= "";
   int iEnchant = GetLocalInt(oPC,"iEnchant");
   float fAngleToVector = 0.4;

   switch(iEnchant)     //se chance of success
    {
     case 1:
      {
       fAngleToVector = 0.5;
  //     iMaxStack = iMaxStack+3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance+150;
       // sFlame = "plc_flamesmall";
       break;
      }
     case 2:
      {
       fAngleToVector = 0.6;
    //   iMaxStack = iMaxStack+1;
       if (iReEnchant==0)iEnchantChance = iEnchantChance+100;
        // sFlame = "plc_flamesmall";
       break;
      }
     case 3:
      {
       fAngleToVector = 0.6;
//       iMaxStack = iMaxStack+1;
       if (iReEnchant==0)iEnchantChance = iEnchantChance+75;
        // sFlame = "plc_flamesmall";
       break;
      }
     case 4:
      {
       fAngleToVector = 0.2;
//       iMaxStack = iMaxStack-2;
       if (iReEnchant==0)iEnchantChance = iEnchantChance+50;
//           sFlame = "plc_flamelarge";
       break;
      }
     case 5:
      {
       fAngleToVector = 0.3;
  //     iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance;
        // sFlame = "plc_flamelarge";
        break;
      }
     case 6:
      {
       fAngleToVector = 0.3;
    //   iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance-100;
        // sFlame = "plc_flamelarge";
        break;
      }
     case 7:
      {
       fAngleToVector = 0.3;
      // iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance-250;
        // sFlame = "plc_flamelarge";
        break;
      }
     case 8:
      {
       fAngleToVector = 0.4;
      // iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance-270;
       //DelayCommand(6.0,SendMessageToPC(oPC,"Value of iEnchant : " + IntToString(iEnchant)));
       //DelayCommand(6.0,SendMessageToPC(oPC,"Current Enchanting chance : "+ IntToString(iEnchantChance)+"%"));
         sFlame = "plc_flamemedium";
         break;
      }
     case 9:
      {
       fAngleToVector = 0.4;
      // iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance-290;
         sFlame = "plc_flamemedium";
         break;
      }
     case 10:
      {
       fAngleToVector = 0.4;
      // iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance-310;
         sFlame = "plc_flamemedium";
        break;
      }
     case 11:
      {
       fAngleToVector = 0.4;
      // iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance-330;
         sFlame = "plc_flamelarge";
         break;
      }
     case 12:
      {
       fAngleToVector = 0.4;
      // iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance-350;
         sFlame = "plc_flamelarge";
         break;
      }
     case 13:
      {
       fAngleToVector = 0.4;
      // iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance-370;
         sFlame = "plc_flamelarge";
         break;
      }
     case 14:
      {
       fAngleToVector = 0.4;
      // iMaxStack = iMaxStack -3;
       if (iReEnchant==0)iEnchantChance = iEnchantChance-390;
         sFlame = "plc_flamelarge";
         break;
      }
     default:
      {
        sFlame = "plc_flamemedium";
       break;
      }
    }
////////////////////////////////////////////////////////////////////////
    iEnpower = GetLocalInt(oPC,"iEnpower");
    switch(iEnpower)
        {
         case 1:
          {
            sEssence = "essence001";
            if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                {
                SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_ACID);
                SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_10);
                }
            break;
          }
         case 2:
           {
           sEssence = "essence017";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_COLD);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_10);
                  }
            break;
         }
         case 3:
           {
           sEssence = "essence006";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_ELECTRICAL);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_10);
                  }
            break;
         }
         case 4:
           {
           sEssence = "essence011";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_FIRE);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_10);
                  }
                break;
         }
         case 5:
            {
            sEssence = "essence001";
            if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                {
                SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_ACID);
                SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_15);
                }
            break;
          }
         case 6:
           {
           sEssence = "essence017";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_COLD);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_15);
                  }
            break;
         }
         case 7:
           {
           sEssence = "essence006";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_ELECTRICAL);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_15);
                  }
            break;
         }
         case 8:
           {
           sEssence = "essence011";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_FIRE);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_15);
                  }
                break;
         }
         case 9:
            {
            sEssence = "essence001";
            if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                {
                SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_ACID);
                SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_20);
                }
            break;
          }
         case 10:
           {
           sEssence = "essence017";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_COLD);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_20);
                  }
            break;
         }
         case 11:
           {
           sEssence = "essence006";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_ELECTRICAL);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_20);
                  }
            break;
         }
         case 12:
           {
           sEssence = "essence011";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_FIRE);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_20);
                  }
                  break;
         }
         case 13:
           {
           sEssence = "essence016";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_SLASHING);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_15);
                  }
                break;
         }
         case 14:
            {
            sEssence = "essence016";
            if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                {
                SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_PIERCING);
                SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_15);
                }
            break;
          }
         case 15:
           {
           sEssence = "essence016";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_BLUDGEONING);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_15);
                  }
            break;
         }
         case 16:
           {
           sEssence = "essence016";
                if(GetItemPossessedBy(oPC,sEssence) != OBJECT_INVALID)
                  {
                    SetLocalInt(oPC,"DamageType",IP_CONST_DAMAGETYPE_NEGATIVE);
                    SetLocalInt(oPC,"DamageQty",IP_CONST_DAMAGERESIST_15);
                  }
            break;
           }
         default:
          {
             int DamageType=0;
             int DamageQty=0;
           break;
          }
        }  //end of switch

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

   location lSelf = GetLocation(OBJECT_SELF);
   float  fSelf = GetFacing(OBJECT_SELF);
   vector vSelf = GetPosition(OBJECT_SELF);
   object oArea = GetArea(OBJECT_SELF);
   vector vFire;
   int vDirection;
   vFire = vSelf + (AngleToVector(fSelf) * fAngleToVector);
   location lFire = Location(oArea,vFire,fSelf);
   object oFire = CreatePlaceable(sFlame, lFire, 6.0);

   AssignCommand(oPC,PlaySound("al_mg_chntmagic1"));
   AssignCommand(oPC,PlaySound("al_mg_crystalgd1"));
   AssignCommand(oPC,PlaySound("al_mg_jacobs1"));
   AssignCommand(oPC,ActionPlayAnimation(ANIMATION_LOOPING_GET_MID,1.0,4.0));
   PlayAnimation(ANIMATION_PLACEABLE_ACTIVATE,1.0,6.0);

   SetLocalInt(OBJECT_SELF,"iAmInUse",99);
   DelayCommand(7.0,SetLocalInt(OBJECT_SELF,"iAmInUse",0));
   //ensure at least 1 respawn 10 minutes after used...
   //this is to prevent a broken placeable that is used, with a 'in use' delay
   //which would cancel the respawn
   if (GetLocalInt(OBJECT_SELF,"iAmSetToRespawn")!=99)
    {
     SetLocalInt(OBJECT_SELF,"iAmSetToRespawn",99);
     DelayCommand(600.0,ExecuteScript("_onclose_clear",OBJECT_SELF));
    }

   int iRandom = 0;

   int iSuccess = 0;
   int iSkillGain = 0;

////////////////////////////////////////////begin stackables
  object oTemp = OBJECT_INVALID;
  oTemp = GetItemPossessedBy(oPC,sEssence);
  object oEssence = oTemp;
  string sEssenceTag = GetTag(oEssence);
  int iCount = 0;
  int iEssence = 1;
      iCount = GetNumStackedItems(oTemp);
      DestroyObject(oTemp);
   if (iCount<iEssence)
    {
     iEssence = iEssence - iCount;
     //DelayCommand(2.0,GetNextStackedItem(oPC,sEssenceTag,iEssence)); //Not needed as long as only use 1 essence.
    }
   else
    {
     if (iCount > iEssence)
      {
       iCount = iCount - iEssence;
       SendMessageToPC(oPC,"You should get back "+IntToString(iCount)+" essences");
       DelayCommand(1.0,CreateAnObject(sEssence,oPC,iCount));
      }
    }
////////////////////////////////////////////////////////////////end stackables

   iEnpower = GetLocalInt(oPC,"iEnpower");
   iEnchantChance = iEnchantChance - (iEnchantSkill * iEnpower/100);
   //SendMessageToPC(oPC,"Value of iEnpower : " + IntToString(iEnpower));
   SendMessageToPC(oPC,"Current Enchanting chance : "+ IntToString(iEnchantChance)+"%");

   if (Random(1000) <= iEnchantChance)
     {
      iSuccess = 1;
      iStackSize = 1;
     if (iEnchant>=1)
       {
       DelayCommand(5.0,AssignCommand(oPC,ActionPlayAnimation(ANIMATION_LOOPING_CONJURE1,1.0,1.0)));
       DelayCommand(5.0,AssignCommand(oPC,ActionPlayAnimation(ANIMATION_FIREFORGET_VICTORY1,1.0,1.0)));
       DelayCommand(5.0,EnchantArmor(oItem,iEnchant));
//       DelayCommand(5.0,EnchantItem(oItem));
       DelayCommand(6.0, GiveToPC(oItem,oPC));
       sSuccess = "You successfully Enchant the Item";
       DelayCommand(6.0,FloatingTextStringOnCreature(sSuccess,oPC,FALSE));
       DelayCommand(5.8,PlaySound("vs_chant_conj_lm"));
       DelayCommand(6.0,ApplyEffectToObject(DURATION_TYPE_TEMPORARY,EffectVisualEffect(VFX_DUR_BLUR,FALSE),OBJECT_SELF,2.0));
       } //end of iEnchant if

      int iEnpower = GetLocalInt(oPC,"iEnpower");
      int DamageType =GetLocalInt(oPC,"DamageType");
      int DamageQty =GetLocalInt(oPC,"DamageQty");
     if (iEnpower>=1)
       {
       DelayCommand(5.0,AssignCommand(oPC,ActionPlayAnimation(ANIMATION_LOOPING_CONJURE2,1.0,1.0)));
       DelayCommand(5.0,AssignCommand(oPC,ActionPlayAnimation(ANIMATION_FIREFORGET_VICTORY2,1.0,1.0)));
       DelayCommand(5.0,EnPowerArmor(oItem,DamageType,DamageQty));
       DelayCommand(6.0, GiveToPC(oItem,oPC));
       sSuccess = "You successfully Enpower the Item";
      DelayCommand(6.0,FloatingTextStringOnCreature(sSuccess,oPC,FALSE));
      DelayCommand(5.8,PlaySound("vs_chant_ench_hm"));
      DelayCommand(6.0,ApplyEffectToObject(DURATION_TYPE_TEMPORARY,EffectVisualEffect(VFX_BEAM_LIGHTNING,FALSE),OBJECT_SELF,2.0));
      } //end of iEnpower if

     }
    else
     {
      DelayCommand(5.0,AssignCommand(oPC,ActionPlayAnimation(ANIMATION_LOOPING_PAUSE_TIRED,1.0,1.0)));
      DelayCommand(6.0,FloatingTextStringOnCreature("The incantation fails and the item is lost.",oPC,FALSE));
      DelayCommand(5.8,PlaySound("as_na_steamshrt2"));
      DelayCommand(6.0,ApplyEffectToObject(DURATION_TYPE_TEMPORARY,EffectVisualEffect(VFX_DUR_PARALYZED ,FALSE),OBJECT_SELF,2.0));
     }
   DelayCommand(6.0,DestroyObject(oItem));

   if (iSuccess == 1)
    {
     iRandom = Random(1000);
        if (iEnchantSkill > 700) iRandom = iRandom + 25;
        if (iEnchantSkill > 998) iRandom = iRandom - 20;
        if (iEnchantSkill > 999) iRandom = 0;
     if (iRandom > iEnchantSkill)
      {
       if (d10(1)+1 >= iEnchantChance/100) iSkillGain = 1;
      }
    }

//Ensure no more than 1 skill gain every 10 seconds to avoid token droppage.
      if (iSkillGain ==1)
       {
        if (GetLocalInt(oPC,"iSkillGain")!= 0)
          {
           iSkillGain = 0;
          }
         else
          {
           SetLocalInt(oPC,"iSkillGain",99);
           DelayCommand(10.0,SetLocalInt(oPC,"iSkillGain",0));
          }
       }  // end of if iskill gain == 1


   if (iSkillGain ==1)
    {
     string sOldSkill = "";
     string sOldSkill2 = "";
     iEnchantSkill++;
     sOldSkill2 = IntToString(iEnchantSkill);
     sOldSkill = "."+GetStringRight(sOldSkill2,1);
     if (iEnchantSkill > 9)
       {
        sOldSkill = GetStringLeft(sOldSkill2,GetStringLength(sOldSkill2)-1)+sOldSkill;
       }
      else
       {
        sOldSkill = "0"+sOldSkill;
       }
     if (iEnchantSkill <= 1000)
      {
//       DelayCommand(6.0,SetTokenPair(oPC,14,2,iEnchantSkill));
       DelayCommand(6.0,SetPersistentInt(oPC,"iEnchantSkill",iEnchantSkill,0,"UOACraft"));
       DelayCommand(6.0,SendMessageToPC(oPC,"==================================="));
       DelayCommand(6.0,SendMessageToPC(oPC,"Your skill in Enchanting has gone up!"));
       DelayCommand(6.0,SendMessageToPC(oPC,"Current Enchanting skill : "+ sOldSkill+"%"));
       DelayCommand(6.0,SendMessageToPC(oPC,"==================================="));
       DelayCommand(6.0, GiveXPToCreature(oPC, 5));
       }

    }  // end of skill gain
}     //end of main
void CreateAnObject(string sResource, object oPC, int iStackSize)
 {

     CreateItemOnObject(sResource,oPC,iStackSize);
     return;
 }

object CreatePlaceable(string sObject, location lPlace, float fDuration)
{
  object oPlaceable = CreateObject(OBJECT_TYPE_PLACEABLE,sObject,lPlace,FALSE);
  if (fDuration != 0.0)
    DestroyObject(oPlaceable,fDuration);
  return oPlaceable;
}

void EnchantArmor(object oItem,int wBonus)
    {
       itemproperty iBonus = ItemPropertyACBonus(wBonus);
    IPSafeAddItemProperty(oItem,iBonus);
    }

void GiveToPC(object oItem, object oPC)
    {
        CopyItem(oItem,oPC,TRUE);
    }
 void EnPowerArmor(object oItem,int DamageType,int DamageQty)
    {
      itemproperty iEffect = ItemPropertyDamageResistance(DamageType,DamageQty);
    IPSafeAddItemProperty(oItem,iEffect);
    }


/*
IP_CONST_DAMAGEBONUS_1
IP_CONST_DAMAGEBONUS_10
IP_CONST_DAMAGEBONUS_1d10
IP_CONST_DAMAGEBONUS_1d12
IP_CONST_DAMAGEBONUS_1d4
IP_CONST_DAMAGEBONUS_1d6
IP_CONST_DAMAGEBONUS_1d8
IP_CONST_DAMAGEBONUS_2
IP_CONST_DAMAGEBONUS_2d10
IP_CONST_DAMAGEBONUS_2d12
IP_CONST_DAMAGEBONUS_2d4
IP_CONST_DAMAGEBONUS_2d6
IP_CONST_DAMAGEBONUS_2d8
IP_CONST_DAMAGEBONUS_3
IP_CONST_DAMAGEBONUS_4
IP_CONST_DAMAGEBONUS_5
IP_CONST_DAMAGEBONUS_6
IP_CONST_DAMAGEBONUS_7
IP_CONST_DAMAGEBONUS_8
IP_CONST_DAMAGEBONUS_9


IP_CONST_DAMAGETYPE_ACID
Acid damagetype.
IP_CONST_DAMAGETYPE_BLUDGEONING
Bludgenoning damagetype.
IP_CONST_DAMAGETYPE_COLD
Cold damagetype.
IP_CONST_DAMAGETYPE_DIVINE
Divine energy damage type.
IP_CONST_DAMAGETYPE_ELECTRICAL
Electrical damage type.
IP_CONST_DAMAGETYPE_FIRE
Fire damage type.
IP_CONST_DAMAGETYPE_MAGICAL
Any magical (i.e. not physical) damage type.
IP_CONST_DAMAGETYPE_NEGATIVE
Negative energy damage type.
IP_CONST_DAMAGETYPE_PHYSICAL
Physical (i.e. not magical) damage type.
IP_CONST_DAMAGETYPE_PIERCING
Piercing damage type.
IP_CONST_DAMAGETYPE_POSITIVE
Positive energy damage type.
IP_CONST_DAMAGETYPE_SLASHING
Slashing damage type.
IP_CONST_DAMAGETYPE_SONIC
Sonic damage type.
IP_CONST_DAMAGETYPE_SUBDUAL
Subdual damage type.
*/

/*
IP_CONST_DAMAGERESIST_10 Resist 10 damage.

IP_CONST_DAMAGERESIST_15 Resist 15 damage.

IP_CONST_DAMAGERESIST_20 Resist 20 damage.

IP_CONST_DAMAGERESIST_25 Resist 25 damage.

IP_CONST_DAMAGERESIST_30 Resist 30 damage.

*/
/*
IP_CONST_DAMAGETYPE_ACID Acid damagetype.

IP_CONST_DAMAGETYPE_BLUDGEONING Bludgenoning damagetype.

IP_CONST_DAMAGETYPE_COLD Cold damagetype.

IP_CONST_DAMAGETYPE_DIVINE Divine energy damage type.

IP_CONST_DAMAGETYPE_ELECTRICAL Electrical damage type.

IP_CONST_DAMAGETYPE_FIRE Fire damage type.

IP_CONST_DAMAGETYPE_MAGICAL Any magical (i.e. not physical) damage type.

IP_CONST_DAMAGETYPE_NEGATIVE Negative energy damage type.

IP_CONST_DAMAGETYPE_PHYSICAL Physical (i.e. not magical) damage type.

IP_CONST_DAMAGETYPE_PIERCING Piercing damage type.

IP_CONST_DAMAGETYPE_POSITIVE Positive energy damage type.

IP_CONST_DAMAGETYPE_SLASHING Slashing damage type.

IP_CONST_DAMAGETYPE_SONIC Sonic damage type.

IP_CONST_DAMAGETYPE_SUBDUAL Subdual damage type

*/