////////////////////////////////////////////////////////////////////////////////
// item and body cleanup
//  By Deva Bryson Winblood
////////////////////////////////////////////////////////////////////////////////
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 main()
{
   float fDist;
   object oMe=OBJECT_SELF;
   object oOb;
   object oI;
   int nC=1;
   int nValue;
   int nSG;
   string sTag;
   int bGotItAll=TRUE;
   object oStore=OBJECT_INVALID;
   string sStoreTag=GetLocalString(oMe,"sStoreTag");
   if (GetStringLength(sStoreTag)>0) oStore=GetObjectByTag(sStoreTag);
   SetLocalInt(oMe,"nGNBDisabled",TRUE);
   // loose items
   if (oStore==OBJECT_INVALID) AssignCommand(oMe,SpeakString("*I don't know where my store is*"));
   oOb=GetNearestObject(OBJECT_TYPE_ITEM,oMe,nC);
   fDist=GetDistanceBetween(oMe,oOb);
   while(oOb!=OBJECT_INVALID&&fDist<30.0)
   { // object
     if (GetPlotFlag(oOb)==FALSE&&fnIsManaObject(oOb)==FALSE)
     { // not plot
       if (fDist<8.0)
       { // instant clean
         nValue=GetGoldPieceValue(oOb);
         nValue=FloatToInt(IntToFloat(nValue)*0.8);
         if (nValue>0&&oStore!=OBJECT_INVALID)
         { // give gold to store
           nSG=GetStoreGold(oStore);
           nSG=nSG+nValue;
           SetStoreGold(oStore,nSG);
         } // give gold to store
         AssignCommand(oMe,SpeakString("*cleaned '"+GetName(oOb)+"'*"));
         DelayCommand(1.0,DestroyObject(oOb));
       } // instant clean
       else
       { // move to object
         AssignCommand(oMe,ActionMoveToObject(oOb,FALSE,1.0));
         nC=1000;
         bGotItAll=FALSE;
       } // move to object
     } // not plot
     nC++;
     oOb=GetNearestObject(OBJECT_TYPE_ITEM,oMe,nC);
     fDist=GetDistanceBetween(oMe,oOb);
   } // object
   // look for bodies and remains
   nC=1;
   oOb=GetNearestObject(OBJECT_TYPE_PLACEABLE,oMe,nC);
   fDist=GetDistanceBetween(oMe,oOb);
   while(oOb!=OBJECT_INVALID&&fDist<30.0)
   { // object
     if (fDist<8.0&&GetHasInventory(oOb)==TRUE)
     { // instant clean
       oI=GetFirstItemInInventory(oOb);
       while(oI!=OBJECT_INVALID)
       { // inventory
         nValue=GetGoldPieceValue(oI);
         nValue=FloatToInt(IntToFloat(nValue)*0.8);
         if (nValue<1) nValue=1;
         if (nValue>0&&oStore!=OBJECT_INVALID)
         { // destroy
           DelayCommand(1.0,DestroyObject(oI));
           nSG=GetStoreGold(oStore);
           nSG=nSG+nValue;
           SetStoreGold(oStore,nSG);
         } // destroy
         oI=GetNextItemInInventory(oOb);
       } // inventory
       DelayCommand(3.0,DestroyObject(oOb));
     } // instant clean
     else if (GetHasInventory(oOb)==TRUE&&bGotItAll==TRUE)
     { // move
       AssignCommand(oMe,ActionMoveToObject(oOb,FALSE,1.0));
       nC=1000;
       bGotItAll=FALSE;
     } // move
     nC++;
     oOb=GetNearestObject(OBJECT_TYPE_ITEM,oMe,nC);
     fDist=GetDistanceBetween(oMe,oOb);
   } // object
   if (bGotItAll==TRUE) SetLocalInt(oMe,"nGNBDisabled",FALSE);
   else {DelayCommand(5.0,ExecuteScript("itemcleanup",OBJECT_SELF)); }
}