//#include "_persist_01a"

void CreateAnObject(string sResource, object oPC, int iStackSize);


void main()
{

  object oPC = GetLastUsedBy();
  object oItem = OBJECT_SELF;
  string sTagSelf = GetTag(OBJECT_SELF);
  string sItemResRef = "";
  string sSuccess = "";
  int iSkillGain = 0;

  if (GetLocalInt(OBJECT_SELF,"iAmInUse")!=0)
   {
    FloatingTextStringOnCreature("Only one person can quarry this gem at a time.",oPC,FALSE);
    return;
   }
  if (GetLocalInt(oPC,"iAmDigging")!=0)
   {
    FloatingTextStringOnCreature("You may only dig one area at a time.",oPC,FALSE);
    return;
   }


  //int iGemSkill = GetTokenPair(oPC,13,8);
  int iGemSkill = GetCampaignInt("UOACraft","iGemQuarrySkill",oPC);
  int iGemChance = iGemSkill;
  if (iGemSkill <350)
   {
    iGemChance = GetAbilityScore(oPC,ABILITY_STRENGTH)*5;
    iGemChance = iGemChance + (GetAbilityScore(oPC,ABILITY_DEXTERITY)*3);
    iGemChance = iGemChance + (GetAbilityScore(oPC,ABILITY_WISDOM)*2);
    iGemChance = iGemChance *3;
    if (iGemChance>350)iGemChance = 350;
    if (iGemSkill>iGemChance) iGemChance = iGemSkill;
   }



  // Determine type of quarry and apply difficulty penalty
  // If you wish to have each progressively harder stone guarunteed to give
  // a higher gem type, remove the '+IntToString(Random(x)+1)' section, and
  // at the end of "gemstone00" change it to "gemstone002", "gemstone003", etc
  // The order of the lines is progressively harder, from "gemstone002" to "gemstone006"

  int iPenalty = 200;
  sItemResRef="gemstone001";
  if (sTagSelf=="GemQuarry_SemiSoftStone")
   {
    iPenalty = 500;
    if (Random(100)<51) sItemResRef="gemstone002";
   }
  if (sTagSelf=="GemQuarry_MediumStone")
   {
    iPenalty = 600;
    sItemResRef="gemstone00"+IntToString(Random(2)+1);
   }
  if (sTagSelf=="GemQuarry_SemiHardStone")
   {
    iPenalty = 700;
    sItemResRef="gemstone00"+IntToString(d4(1));;
   }
  if (sTagSelf=="GemQuarry_HardStone")
   {
    iPenalty = 800;
    sItemResRef="gemstone00"+IntToString(Random(5)+1);
   }
  if (sTagSelf=="GemQuarry_VeryHardStone")
   {
    iPenalty = 900;
    sItemResRef="gemstone00" +IntToString(d6(1));
   }

  iGemChance = iGemChance - iPenalty;
  if (iGemChance <1)
   {
    FloatingTextStringOnCreature("You have no idea how to quarry gems from this kind of stone.",oPC,FALSE);
    return;
   }


  SetLocalInt(OBJECT_SELF,"iAmInUse",99);
  DelayCommand(15.0,SetLocalInt(OBJECT_SELF,"iAmInUse",0));
  SetLocalInt(oPC,"iAmDigging",99);
  DelayCommand(15.1,SetLocalInt(oPC,"iAmDigging",0));

 // This snippet of code is cut-n-paste direct from ATS
 // Reason for this is because I had no clue how to assign an increase
 // in the z-axis of the location of the anvil for sparks to display.
 // After reading through this code, it is obvious that vEffecrPos.z
 // is the line which assigns this.  Due to my own ignorance in this issue
 // I have decided to leave this snippet of code intact with this credit to
 // the original ATS script coders, whomever they may have been.
 // The only modification to this code is locAnvil was changed to locBath.

  location locBath = GetLocation(OBJECT_SELF);
  vector vEffectPos = GetPositionFromLocation(locBath);
  vEffectPos.z += 1.0;
  location locEffect = Location( GetAreaFromLocation(locBath), vEffectPos,GetFacingFromLocation(locBath) );

  ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY), locEffect);
  ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPECIAL_RED_WHITE), locEffect);
  DelayCommand(0.1,PlaySound("as_cv_chiseling1"));
  DelayCommand(2.9,PlaySound("as_cv_chiseling3"));
  DelayCommand(8.8,PlaySound("as_cv_chiseling3"));
  DelayCommand(0.6,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(1.2,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(2.1,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(2.8,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(3.2,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(3.8,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(4.1,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(4.6,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(5.3,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(6.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(6.5,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(7.2,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(7.9,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(8.4,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(9.2,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(9.7,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(10.3,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(10.9,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(11.4,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(12.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(12.6,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(13.1,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(13.7,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(14.3,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(14.7,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(15.6,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(16.1,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(16.7,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  AssignCommand(oPC,ActionPlayAnimation(ANIMATION_LOOPING_GET_MID,1.0,15.0));

  iGemChance = iGemChance+100;  //Minimum of 10% chance to get a gem each attempt.
                                //Maximum chance for hardest Gemstone is 20%.

  if (Random(1000)<=iGemChance)
    {
     DelayCommand(16.0,FloatingTextStringOnCreature("You have successfully quarried up a gemstone encrusted in minerals.",oPC,FALSE));
     DelayCommand(16.1,CreateAnObject(sItemResRef,oPC,1));
     if (Random(1000) >= iGemSkill)
      {
       if (d10(1)+1 >= iGemChance/100) iSkillGain = 1;
      }
    }
   else
    {
     DelayCommand(16.0,FloatingTextStringOnCreature("You spend some time chipping away at the stone but to no avail.",oPC,FALSE));
     return;
    }


 //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));
          }
       }


  if (iSkillGain ==1)
    {
     string sOldSkill = "";
     string sOldSkill2 = "";
     iGemSkill++;
     sOldSkill2 = IntToString(iGemSkill);
     sOldSkill = "."+GetStringRight(sOldSkill2,1);
     if (iGemSkill > 9)
       {
        sOldSkill = GetStringLeft(sOldSkill2,GetStringLength(sOldSkill2)-1)+sOldSkill;
       }
      else
       {
        sOldSkill = "0"+sOldSkill;
       }
     if (iGemSkill <= 1000)
      {
       //DelayCommand(17.0,SetTokenPair(oPC,13,8,iGemSkill));
       DelayCommand(17.0,SetCampaignInt("UOACraft","iGemQuarrySkill",iGemSkill,oPC));
       DelayCommand(17.0,SendMessageToPC(oPC,"========================================"));
       DelayCommand(17.0,SendMessageToPC(oPC,"Your skill in gem quarrying has gone up!"));
       DelayCommand(17.0,SendMessageToPC(oPC,"Current gem quarrying skill : "+ sOldSkill+"%"));
       DelayCommand(17.0,SendMessageToPC(oPC,"========================================"));
       if (GetLocalInt(GetModule(),"_UOACraft_XP")!=0) DelayCommand(16.9,GiveXPToCreature(oPC,GetLocalInt(GetModule(),"_UOACraft_XP")));
      }
    }




}

void CreateAnObject(string sResource, object oPC, int iStackSize)
 {
  CreateItemOnObject(sResource,oPC,iStackSize);
  return;
 }