////////////////////////////////////////////////////////////////////////////////
// Lair Cleaner HB
// By Deva Bryson Winblood.
////////////////////////////////////////////////////////////////////////////////
#include "antistuck_h"
int fnIsManaObject(object oItem)
{ // return true if mana crystal
  int nRet=FALSE;
  string sRes=GetResRef(oItem);
  if (sRes=="mana_crystal_5"||sRes=="mana_crystal_2"||sRes=="mana_crystal_1") nRet=TRUE;
  return nRet;
} // fnIsManaObject()

int fnIsSoulObject(object oItem)
{ // return true if is soul token
  int nRet=FALSE;
  string sRes=GetResRef(oItem);
  if (sRes=="soultoken") nRet=TRUE;
  return nRet;
} // fnIsSoulObject()

void fnDestroyContainer(object oContainer,object oStorage,object oTaxes,object oLead);

///////////////////////////////////////////////////////////////////// MAIN
void main()
{
   object oMe=OBJECT_SELF;
   string sID=GetLocalString(oMe,"sTeamID");
   int nState=GetLocalInt(oMe,"nState");
   object oDest=GetLocalObject(oMe,"oDest");
   object oTarg=GetLocalObject(oMe,"oTarg");
   object oWP;
   object oManaVault=GetNearestObjectByTag(sID+"_VAULT",oMe,1);
   object oSouls=GetNearestObjectByTag("StrangeApparatus",oMe,1);
   object oStorage=GetNearestObjectByTag(sID+"_STORAGE",oMe,1);
   object oItem;
   object oOb;
   object oChest=GetNearestObjectByTag(sID+"_CHEST",oMe,1);
   int nType;
   int nN;
   float fDist;
   int nSC=GetLocalInt(oMe,"nStuckCount");
   float fLDist=GetLocalFloat(oMe,"fLDist");
   object oLead=GetLocalObject(GetModule(),"oTeamLead"+sID);
   if (GetIsInCombat(oMe)==FALSE&&IsInConversation(oMe)==FALSE&&GetNearestCreature(CREATURE_TYPE_IS_ALIVE,TRUE,oMe,1,CREATURE_TYPE_REPUTATION,REPUTATION_TYPE_ENEMY,CREATURE_TYPE_PERCEPTION,PERCEPTION_SEEN)==OBJECT_INVALID)
   { // safe to act
     switch(nState)
     { // cleaner state
       case 0: { // look for things to clean
         oTarg=OBJECT_INVALID;
         oDest=OBJECT_INVALID;
         nN=1;
         oItem=GetNearestObject(OBJECT_TYPE_ITEM,oMe,nN);
         while(oItem!=OBJECT_INVALID&&oTarg==OBJECT_INVALID)
         { // find item target
           if (GetPlotFlag(oItem)==FALSE&&GetTag(oItem)!="rts_it_report")
           { // not plot
             if (fnIsManaObject(oItem)==FALSE)
             { // not mana crystal
               oTarg=oItem;
             } // not mana crystal
             else if (fnIsManaObject(oItem)==TRUE)
             { // is mana crystal
               oWP=oManaVault;
               fDist=GetDistanceBetween(oWP,oItem);
               if (fDist>8.0)
               { // far away get it
                 oTarg=oItem;
               } // far away get it
             } // is mana crystal
           } // not plot
           nN++;
           oItem=GetNearestObject(OBJECT_TYPE_ITEM,oMe,nN);
         } // find item target
         if (oTarg==OBJECT_INVALID)
         { // look for non-plot containers
           nN=1;
           //AssignCommand(oMe,SpeakString("*bodies?*"));
           oOb=GetNearestObject(OBJECT_TYPE_PLACEABLE,oMe,nN);
           while(oOb!=OBJECT_INVALID&&oTarg==OBJECT_INVALID)
           { // look for container
             //AssignCommand(oMe,SpeakString("*Looking at '"+GetName(oOb)+"'*"));
             if (oOb!=oChest&&oOb!=oStorage)
             { // not plot
               //AssignCommand(oMe,SpeakString("** storage or tax chest **"));
               if (GetHasInventory(oOb)==TRUE)
               { // has inventory
                 //AssignCommand(oMe,SpeakString("*bodies to remove*"));
                 oTarg=oOb;
               } // has inventory
             } // not plot
             nN++;
             oOb=GetNearestObject(OBJECT_TYPE_PLACEABLE,oMe,nN);
           } // look for container
         } // look for non-plot containers
         if (oTarg==OBJECT_INVALID)
         { // no target was found - wander
           //AssignCommand(oMe,SpeakString("*wandering*"));
           SetLocalInt(oMe,"nState",3);
         } // bo target was found - wander
         else
         {
            SetLocalObject(oMe,"oTarg",oTarg);
            SetLocalObject(oMe,"oDest",oTarg);
            SetLocalInt(oMe,"nState",1);
         }
         break;
       } // look for things to clean
       case 1: { // move to destination
         fDist=GetDistanceBetween(oMe,oDest);
         if (GetArea(oMe)==GetArea(oDest)&&fDist<1.5)
         { // arrived
           SetLocalInt(oMe,"nState",2);
         } // arrived
         else
         { // move
           ASActionMoveToObject(oDest,TRUE,1.0);
         } // move
         break;
       } // move to destination
       case 2: { // care for item to clean
         nType=GetObjectType(oTarg);
         if (nType==OBJECT_TYPE_PLACEABLE)
         { // container
           AssignCommand(oMe,SpeakString("*container*"));
           oOb=oTarg;
           AssignCommand(oMe,ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,4.5));
           /*oTarg=GetFirstItemInInventory(oOb);
           while(oTarg!=OBJECT_INVALID)
           { // inventory
             if (fnIsManaObject(oTarg)==TRUE)
             { // teleport to mana vault
               //AssignCommand(oMe,ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,4.5));
               oItem=CopyObject(oTarg,GetLocation(oManaVault));
               DelayCommand(2.0,DestroyObject(oTarg));
              // SetLocalInt(oMe,"nState",0);
             } // teleport to mana vault
             else if (fnIsSoulObject(oTarg)==TRUE)
             { // store in soul receptacle
               //AssignCommand(oMe,ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,4.5));
               oItem=CopyObject(oTarg,GetLocation(oMe),OBJECT_INVALID);
               DelayCommand(2.0,DestroyObject(oTarg));
               SetLocalObject(oMe,"oDest",oSouls);
               SetLocalInt(oMe,"nState",5);
             } // store in soul receptacle
             else
             { // convert
               //AssignCommand(oMe,ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,4.5));
               nN=GetGoldPieceValue(oTarg);
               nN=nN/5;
               if(nN<1&&GetGoldPieceValue(oTarg)>0) nN=1;
               if (nN>0&&nN<100)
               { // place gold in chest
                 DelayCommand(2.0,DestroyObject(oTarg));
                 oItem=CreateItemOnObject("nw_it_gold001",oChest,nN);
               } // place gold in chest
               else if (nN>99)
               { // place in storage
                 oItem=CreateItemOnObject(GetResRef(oTarg),oStorage,GetItemStackSize(oTarg));
                 DelayCommand(2.0,DestroyObject(oTarg));
               } // place in storage
               else
               {
                 DelayCommand(2.0,DestroyObject(oTarg));
               }
             } // convert
             oTarg=GetNextItemInInventory(oOb);
           } // inventory
           DelayCommand(8.0,DestroyObject(oOb));*/
           fnDestroyContainer(oOb,oStorage,oChest,oLead);
           SetLocalInt(oMe,"nState",0);
         } // container
         else if (nType==OBJECT_TYPE_ITEM)
         { // item
           AssignCommand(oMe,SpeakString("*"+GetName(oTarg)+"*"));
           if (fnIsManaObject(oTarg)==TRUE)
           { // teleport to mana vault
             AssignCommand(oMe,ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,4.5));
             oItem=CopyObject(oTarg,GetLocation(oManaVault));
             DestroyObject(oTarg);
             SetLocalInt(oMe,"nState",0);
           } // teleport to mana vault
           else if (fnIsSoulObject(oTarg)==TRUE)
           { // store in soul receptacle
             AssignCommand(oMe,ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,4.5));
             oItem=CopyObject(oTarg,GetLocation(oMe),oMe);
             DestroyObject(oTarg);
             SetLocalObject(oMe,"oDest",oSouls);
             SetLocalInt(oMe,"nState",5);
           } // store in soul receptacle
           else
           { // convert
             AssignCommand(oMe,ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,4.5));
             nN=GetGoldPieceValue(oTarg);
             nN=nN/5;
             if(nN<1&&GetGoldPieceValue(oTarg)>0) nN=1;
             if (nN>0&&nN<100)
             { // place gold in chest
               DestroyObject(oTarg);
               oItem=CreateItemOnObject("nw_it_gold001",oChest,nN);
               SendMessageToPC(oLead,GetName(oMe)+" has placed "+IntToString(nN)+" gold in your "+GetName(oChest)+".");
             } // place gold in chest
             else if (nN>99)
             { // place in storage
               oItem=CreateItemOnObject(GetResRef(oTarg),oStorage,GetItemStackSize(oTarg));
               DestroyObject(oTarg);
             } // place in storage
             else
             {
               DestroyObject(oTarg);
             }
             SetLocalInt(oMe,"nState",0);
           } // convert
         } // item
         else
         { // error
           AssignCommand(oMe,SpeakString("*huh?*"));
           SetLocalInt(oMe,"nState",3); // wander
         } // error
         break;
       } // care for item to clean
       case 3: { // pick wander destination
         nN=d20();
         oOb=GetNearestObject(OBJECT_TYPE_WAYPOINT,oMe,nN);
         if (oOb==OBJECT_INVALID) oOb=GetNearestObject(OBJECT_TYPE_WAYPOINT,oMe,d12());
         if (oOb==OBJECT_INVALID) oOb=GetNearestObject(OBJECT_TYPE_WAYPOINT,oMe,d8());
         if (oOb==OBJECT_INVALID) oOb=GetNearestObject(OBJECT_TYPE_WAYPOINT,oMe,d6());
         if (oOb==OBJECT_INVALID) oOb=GetNearestObject(OBJECT_TYPE_WAYPOINT,oMe,d4());
         if (oOb==OBJECT_INVALID) oOb=GetNearestObject(OBJECT_TYPE_WAYPOINT,oMe,1);
         SetLocalObject(oMe,"oDest",oOb);
         SetLocalInt(oMe,"nState",4);
         break;
       } // pick wander destination
       case 4: { // move to wander destination
         fDist=GetDistanceBetween(oMe,oDest);
         if (GetArea(oMe)==GetArea(oDest)&&fDist<1.5)
         { // arrived
           SetLocalInt(oMe,"nState",0);
         } // arrived
         else
         { // move
           ASActionMoveToObject(oDest,TRUE,1.0);
         } // move
         break;
       } // move to wander destination
       case 5: { // deliver item
         fDist=GetDistanceBetween(oMe,oDest);
         if (GetArea(oMe)==GetArea(oDest)&&fDist<1.5)
         { // arrived
           SetLocalInt(oMe,"nState",0);
         } // arrived
         else
         { // move
           ASActionMoveToObject(oDest,TRUE,1.0);
         } // move
         break;
       } // deliver item
     } // cleaner state
   } // safe to act
}//////////////////////////////////////////////////////////////////// MAIN

void fnDestroyContainer(object oContainer,object oStorage,object oTaxes,object oLead)
{ // empty items
  object oItem=GetFirstItemInInventory(oContainer);
  int nVal;
  object oNew;
  while(oItem!=OBJECT_INVALID)
  {
    DelayCommand(0.2,DestroyObject(oItem));
    nVal=GetGoldPieceValue(oItem);
    if (GetPlotFlag(oItem)==TRUE||nVal>99)
    { // place item in chest
      oNew=CreateItemOnObject(GetResRef(oItem),oStorage,GetItemStackSize(oItem));
    } // place item in chest
    else
    { // convert to gold
      nVal=nVal/5;
      if (nVal<1) nVal=1;
      oNew=CreateItemOnObject("nw_it_gold001",oTaxes,nVal);
      SendMessageToPC(oLead,GetName(OBJECT_SELF)+" has placed "+IntToString(nVal)+" in your "+GetName(oTaxes)+".");
    } // convert to gold
    oItem=GetNextItemInInventory(oContainer);
  }
  DelayCommand(0.3,DestroyObject(oContainer));
} // fnDestroyContainer()