//ai_mine - Mining routine for NPC Units
//By Deva Bryson Winblood.  04/30/2005
//////////////////////////////////////////////////////////////////////
#include "antistuck_h"
#include "prc_inc_racial"

void fnMine(object oMineable)
{ // PURPOSE: Handle the mining of the object
  object oMe=OBJECT_SELF;
  string sID=GetLocalString(oMe,"sTeamID");
  int nP=d100();
  object oItem;
  int nAmt=GetLocalInt(oMineable,"nResAmt");
  string sRes=GetResRef(oMineable);
  AssignCommand(oMe,ClearAllActions());
  AssignCommand(oMe,ActionAttack(oMineable,TRUE));
  AssignCommand(oMineable,PlaySound("as_cv_minepick1"));
  if (MyPRCGetRacialType(oMe)==RACIAL_TYPE_DWARF) nP=nP-10;
  else if (MyPRCGetRacialType(oMe)==RACIAL_TYPE_GNOME) nP=nP-5;
  if (sRes=="resgold")
  { // gold
    if (nP<25)
    { // gold mined
      CreateItemOnObject("gold_nugget",oMe,1);
      nAmt=nAmt-1;
      SetLocalInt(oMineable,"nResAmt",nAmt);
      if (nAmt<1) DestroyObject(oMineable);
    } // gold mined
  } // gold
  else if (sRes=="resadm")
  { // adamantine
    if (nP<6)
    { // adamantine mined
      CreateItemOnObject("bar_admant",oMe,1);
      nAmt=nAmt-1;
      SetLocalInt(oMineable,"nResAmt",nAmt);
      if (nAmt<1) DestroyObject(oMineable);
    } // adamantine mined
  } // adamantine
  else if (sRes=="resiron")
  { // iron
    if (nP<10)
    { // iron mined
      CreateItemOnObject("bar_iron",oMe,1);
      nAmt=nAmt-1;
      SetLocalInt(oMineable,"nResAmt",nAmt);
      if (nAmt<1) DestroyObject(oMineable);
    } // iron mined
  } // iron
  else if (sRes=="mithral")
  { // mithral
    if (nP<3)
    { // mithral mined
      CreateItemOnObject("bar_mith",oMe,1);
      nAmt=nAmt-1;
      SetLocalInt(oMineable,"nResAmt",nAmt);
      if (nAmt<1) DestroyObject(oMineable);
    } // mithral mined
  } // mithral
} // fnMine()


int fnCountMined(object oMe,string sTag,int nMax)
{ // PURPOSE: Return how much of the item has been mined
    int nCount=0;
    object oItem=GetItemPossessedBy(oMe,sTag);
    if (GetIsObjectValid(oItem)) SetLocalInt(oMe,"bDeposit",TRUE);
    if (GetIsObjectValid(oItem)&&nMax>1)
    { // count
        oItem=GetFirstItemInInventory(oMe);
        while(GetIsObjectValid(oItem)&&nCount<nMax)
        { // count
            if (GetTag(oItem)==sTag)
            { // match
                SetLocalInt(oItem,"bDeposit",TRUE);
                nCount++;
            } // match
            oItem=GetNextItemInInventory(oMe);
        } // count
    } // count
    else if (GetIsObjectValid(oItem))
    { // count at least 1
         SetLocalInt(oItem,"bDeposit",TRUE);
         nCount=1;
    } // count at least 1
    return nCount;
} // fnCountMined()


void main()
{
   object oMe=OBJECT_SELF;
   int nParm=GetLocalInt(oMe,"nParm");
   object oMineable;
   string sTag;
   int nMax;
   string sItem;
   DeleteLocalObject(oMe,"oDestWP");
   object oEnemy=GetNearestCreature(CREATURE_TYPE_IS_ALIVE,TRUE,oMe,1,CREATURE_TYPE_REPUTATION,REPUTATION_TYPE_ENEMY,CREATURE_TYPE_PERCEPTION,PERCEPTION_SEEN);
   if (GetIsInCombat(oMe)!=TRUE&&IsInConversation(oMe)!=TRUE&&oEnemy==OBJECT_INVALID)
   { // okay to mine
     if (nParm==1)
     { // gold
       sTag="RESGOLD";
       sItem="gold_nugget";
       nMax=4;
     } // gold
     else if (nParm==2)
     { // iron
       sTag="RESIRON";
       sItem="x2_it_cmat_iron";
       nMax=2;
     } // iron
     else if (nParm==3)
     { // mithral
       sTag="RESMITH";
       sItem="x2_it_cmat_mith";
       nMax=1;
     } // mithral
     else if (nParm==4)
     { // adamantine
       sTag="RESADM";
       sItem="x2_it_cmat_adam";
       nMax=1;
     } // adamantine
     if (fnCountMined(oMe,sItem,nMax)>=nMax)
     { // take back to lair
         sTag="";
         sItem="";
         nMax=0;
     } // take back to lair
     oMineable=GetNearestObjectByTag(sTag,oMe);
     if (GetIsObjectValid(oMineable)&&GetObjectType(oMineable)==OBJECT_TYPE_PLACEABLE)
     { // object found
       if (GetDistanceBetween(oMe,oMineable)>2.0)
       { // move to object
         ASActionMoveToObject(oMineable,TRUE,1.5);
       } // move to object
       else
       { // mine
         AssignCommand(oMe,fnMine(oMineable));
       } // mine
     } // object found
     else
     { // nothing to mine return to lair
       SetLocalInt(oMe,"nMState",2);
       SetLocalInt(oMe,"nSState",0);
       AssignCommand(oMe,ClearAllActions(TRUE));
       AssignCommand(oMe,SpeakString("I need to take minerals back to the throne."));
       DeleteLocalObject(oMe,"oDestWP");
       SetLocalInt(oMe,"nParm",5);
       object oGArea=GetObjectByTag(GetLocalString(oMe,"sTeamID")+"_START");
       SetLocalObject(oMe,"oGArea",oGArea);
     } // nothing to mine return to lair
   } // okay to mine
}