//#include "_persist_01a"

void main()
{
  object oPC = GetLastOpenedBy();
  //int iSmithSkill = GetTokenPair(oPC,13,4); // Weaponsmith
  int iSmithSkill = GetCampaignInt("UOACraft","iSmithSkill",oPC);
  int iSmithChance = iSmithSkill;
  //int iArmorSkill = GetTokenPair(oPC,13,5); // ArmorCraft
  int iArmorSkill = GetCampaignInt("UOACraft","iArmorSkill",oPC);
  int iArmorChance = iArmorSkill;
  int iIngotType = GetLocalInt(oPC,"iUseIngotType");
  int iIngotMod = iIngotType*25;

  if (GetItemPossessedBy(oPC,"NoDrop_SkillLogBook")==OBJECT_INVALID)
   {
    CreateItemOnObject("skilllogbook",oPC,1);
   }

  if (iSmithChance < 350)
   {
    iSmithChance = GetAbilityScore(oPC,ABILITY_STRENGTH)*5;
    iSmithChance = iSmithChance+(GetAbilityScore(oPC,ABILITY_DEXTERITY)*3);
    iSmithChance = iSmithChance+(GetAbilityScore(oPC,ABILITY_INTELLIGENCE)*2);
    iSmithChance = iSmithChance*3;
    if (iSmithChance>350)iSmithChance=350;
    if (iSmithSkill > iSmithChance) iSmithChance=iSmithSkill;
   }
 if (iArmorChance < 350)
   {
    iArmorChance = GetAbilityScore(oPC,ABILITY_STRENGTH)*5;
    iArmorChance = iArmorChance+(GetAbilityScore(oPC,ABILITY_DEXTERITY)*3);
    iArmorChance = iArmorChance+(GetAbilityScore(oPC,ABILITY_INTELLIGENCE)*2);
    iArmorChance = iArmorChance*3;
    if (iArmorChance>350)iArmorChance=350;
    if (iArmorSkill > iArmorChance) iArmorChance=iArmorSkill;
   }


 // Create Ingot Type FlagSwitches
 if (iIngotType != 0)CreateItemOnObject("flagswitch010",OBJECT_SELF,1);
 if (iIngotType != 1)CreateItemOnObject("flagswitch011",OBJECT_SELF,1);
 if (iIngotType != 2)CreateItemOnObject("flagswitch012",OBJECT_SELF,1);
 if (iIngotType != 3)CreateItemOnObject("flagswitch013",OBJECT_SELF,1);
 if (iIngotType != 4)CreateItemOnObject("flagswitch014",OBJECT_SELF,1);
 if (iIngotType != 5)CreateItemOnObject("flagswitch015",OBJECT_SELF,1);
 if (iIngotType != 6)CreateItemOnObject("flagswitch016",OBJECT_SELF,1);
 if (iIngotType != 7)CreateItemOnObject("flagswitch017",OBJECT_SELF,1);
 if (iIngotType != 8)CreateItemOnObject("flagswitch018",OBJECT_SELF,1);
 if (iIngotType != 9)CreateItemOnObject("flagswitch019",OBJECT_SELF,1);
 if (iIngotType != 10)CreateItemOnObject("flagswitch020",OBJECT_SELF,1);
 if (iIngotType != 11)CreateItemOnObject("flagswitch021",OBJECT_SELF,1);
 if (iIngotType != 12)CreateItemOnObject("flagswitch022",OBJECT_SELF,1);


 if (iIngotType==9) iIngotMod = 90;    // Silver is little easier than Bronze
 if (iIngotType==10) iIngotMod = 175;  // Mithril is hard as Verite
 if (iIngotType==11) iIngotMod = 200;  // Adamantite is hard as Valorite
 if (iIngotType==12) iIngotMod = 150;  // Platinum is hard as Agapite

 // Modify the skill based on current ingot selection
 if (iIngotMod >0) iIngotMod = iIngotMod + 250;
 iSmithChance = iSmithChance - iIngotMod;
 iArmorChance = iArmorChance - iIngotMod;

   // Create tokens for items
   if (iArmorChance>0)CreateItemOnObject("pattern022",OBJECT_SELF,1);        /// Metal Studs (For studded leather)
   if (iSmithChance>0) CreateItemOnObject("pattern069",OBJECT_SELF,1);       /// Normal Sling Bullets
   if (iSmithChance>50) CreateItemOnObject("pattern042",OBJECT_SELF,1);      /// Normal Throwing Darts
   if (iSmithChance>50) CreateItemOnObject("pattern021",OBJECT_SELF,1);      /// Normal Shuriken
   if (iSmithChance>50) CreateItemOnObject("pattern070",OBJECT_SELF,1);      /// Normal Arrowheads
   if (iSmithChance>50) CreateItemOnObject("pattern062",OBJECT_SELF,1);      /// Normal Bolt Tips
   if (iSmithChance>50) CreateItemOnObject("pattern045",OBJECT_SELF,1);      /// Normal Kama
   if (iSmithChance>100) CreateItemOnObject("pattern032",OBJECT_SELF,1);     /// Normal Dagger
   if (iSmithChance>100) CreateItemOnObject("pattern064",OBJECT_SELF,1);     /// Normal Kukri
   if (iArmorChance>100) CreateItemOnObject("pattern111",OBJECT_SELF,1);     /// Normal Ringmail Armor
   if (iSmithChance>150) CreateItemOnObject("pattern027",OBJECT_SELF,1);     /// Normal Shortsword
   if (iSmithChance>150) CreateItemOnObject("pattern049",OBJECT_SELF,1);     /// Normal Handaxe
   if (iSmithChance>150) CreateItemOnObject("pattern039",OBJECT_SELF,1);     /// Normal Throwing Axes
   if (iSmithChance>150) CreateItemOnObject("pattern028",OBJECT_SELF,1);     /// Normal Sickle
   if (iArmorChance>150) CreateItemOnObject("pattern201",OBJECT_SELF,1);     /// Normal Horsehair Helmet
   if (iArmorChance>150) CreateItemOnObject("pattern200",OBJECT_SELF,1);     /// Normal Pot Helmet
   if (iArmorChance>150) CreateItemOnObject("pattern202",OBJECT_SELF,1);     /// Normal Spike Helmet
   if (iArmorChance>150) CreateItemOnObject("pattern203",OBJECT_SELF,1);     /// Normal Stag Helmet
   if (iArmorChance>150) CreateItemOnObject("pattern204",OBJECT_SELF,1);     /// Normal Winged Helmet
   if (iSmithChance>200) CreateItemOnObject("pattern031",OBJECT_SELF,1);     /// Normal Mace
   if (iArmorChance >=250) CreateItemOnObject("pattern029",OBJECT_SELF,1);   /// Normal Small Shield
   if (iArmorChance >=250) CreateItemOnObject("pattern110",OBJECT_SELF,1);   /// Normal Chainmail Shirt
   if (iSmithChance >=250) CreateItemOnObject("pattern046",OBJECT_SELF,1);   /// Normal Longsword
   if (iSmithChance >=250) CreateItemOnObject("pattern040",OBJECT_SELF,1);   /// Normal Light Flail
   if (iSmithChance >=250) CreateItemOnObject("pattern044",OBJECT_SELF,1);   /// Normal Light Hammer
   if (iSmithChance >=250) CreateItemOnObject("pattern024",OBJECT_SELF,1);   /// Normal Rapier
   if (iArmorChance >=300) CreateItemOnObject("pattern108",OBJECT_SELF,1);   /// Normal scale mail armor
   if (iSmithChance >=300) CreateItemOnObject("pattern023",OBJECT_SELF,1);   /// Normal Morning Star
   if (iSmithChance >=300) CreateItemOnObject("pattern060",OBJECT_SELF,1);   /// Normal Battleaxe
   if (iSmithChance >=300) CreateItemOnObject("pattern030",OBJECT_SELF,1);   /// Normal Spear
   if (iSmithChance >=300) CreateItemOnObject("pattern025",OBJECT_SELF,1);   /// Normal Scimitar
   if (iArmorChance >=350) CreateItemOnObject("pattern107",OBJECT_SELF,1);   /// Normal Chainmail Armor
   if (iArmorChance >=350) CreateItemOnObject("pattern106",OBJECT_SELF,1);   /// Normal Breastplate
   if (iSmithChance >=350) CreateItemOnObject("pattern063",OBJECT_SELF,1);   /// Normal Bastard Sword
   if (iArmorChance >=350) CreateItemOnObject("pattern057",OBJECT_SELF,1);   /// Normal Large Shield
   if (iSmithChance >=350) CreateItemOnObject("pattern048",OBJECT_SELF,1);   /// Normal Heavy Flail
   if (iSmithChance >=350) CreateItemOnObject("pattern050",OBJECT_SELF,1);   /// Normal Warhammer
   if (iSmithChance >=350) CreateItemOnObject("pattern056",OBJECT_SELF,1);   /// Normal Katana
   if (iArmorChance >=400) CreateItemOnObject("pattern100",OBJECT_SELF,1);   /// Normal Banded Mail Armor
   if (iArmorChance >=400) CreateItemOnObject("pattern105",OBJECT_SELF,1);   /// Normal Splint Mail Armor
   if (iSmithChance >=400) CreateItemOnObject("pattern053",OBJECT_SELF,1);   /// Normal Greatsword
   if (iSmithChance >=400) CreateItemOnObject("pattern020",OBJECT_SELF,1);   /// Normal Halberd
   if (iSmithChance >=400) CreateItemOnObject("pattern026",OBJECT_SELF,1);   /// Normal Scythe
   if (iSmithChance >=400) CreateItemOnObject("pattern123",OBJECT_SELF,1);   /// Normal Greataxe
   if (iSmithChance >=400)
    {
     if (GetRacialType(oPC)==RACIAL_TYPE_DWARF)
                           CreateItemOnObject("pattern124",OBJECT_SELF,1);   /// Normal Dwarven Waraxe
    }
   if (iArmorChance >=450) CreateItemOnObject("pattern102",OBJECT_SELF,1);   /// Normal Half Plate Armor
   if (iArmorChance >=450) CreateItemOnObject("pattern043",OBJECT_SELF,1);   /// Normal Tower Shield
   if (iSmithChance >=450) CreateItemOnObject("pattern033",OBJECT_SELF,1);   /// Normal Dire Mace
   if (iSmithChance >=450) CreateItemOnObject("pattern059",OBJECT_SELF,1);   /// Normal Double Axe
   if (iArmorChance >=500) CreateItemOnObject("pattern101",OBJECT_SELF,1);   /// Normal Full Plate Armor
   if (iSmithChance >=500) CreateItemOnObject("pattern041",OBJECT_SELF,1);   /// Normal Two-Bladed Sword


 // Search for and destroy any hidden 'body bags' from prior incarnations of this placeable
 object oSearchForBag = GetNearestObjectByTag("Body Bag",OBJECT_SELF,1);
 if (oSearchForBag == OBJECT_INVALID)return;
 object oBagItem = OBJECT_INVALID;
 if (GetDistanceToObject(oSearchForBag)<= 0.2)
  {
   //SendMessageToPC(GetFirstPC(),"Body bag found.. destroying contents..");
   oBagItem = GetFirstItemInInventory(oSearchForBag);
   while (oBagItem != OBJECT_INVALID)
    {
     //SendMessageToPC(GetFirstPC(),"Destroying : "+GetName(oBagItem));
     DestroyObject(oBagItem);
     oBagItem = GetNextItemInInventory(oSearchForBag);
    }
   DestroyObject(oSearchForBag,1.0);
  }
}