////////////////////////////////////////////////////////////////////////////////
// hourly_cleanup - This script is called once per hour and will analyze two
// areas.  The first area should be the second area from the previous area.  If
// the same units that should not stay there for an hour are still there it will
// clean them up.   The second area will be a new area.
// By Deva B. Winblood    09/22/2006
////////////////////////////////////////////////////////////////////////////////


/////////////////////////
// PROTOTYPES
/////////////////////////

// FILE: hourly_cleanup         FUNCTION: Cleanup_Area()
// This function will mark objects for checking next pass.  If those objects
// are still present on second pass it will destroy them.
void Cleanup_Area(object oWaypoint,int bFirst=TRUE);



///////////////////////////////////////////////////////////////////[ MAIN ]/////
void main()
{
   object oMod=GetModule();
   object oFirst=GetLocalObject(oMod,"oHCFirst");
   int nCount=GetLocalInt(oMod,"nHCCount");
   object oSecond=GetObjectByTag("CLEANUP_SENSITIVE",nCount);
   if (!GetIsObjectValid(oSecond))
   { // start over
       SetLocalInt(oMod,"nHCCount",1);
       oSecond=GetObjectByTag("CLEANUP_SENSITIVE",0);
   } // start over
   else
   { // advance nCount
       nCount++;
       SetLocalInt(oMod,"nHCCount",nCount);
   } // advance nCount
   if (GetIsObjectValid(oFirst))
   { // handle first
       Cleanup_Area(oFirst,TRUE);
   } // handle firse
   // handle second
   DelayCommand(HoursToSeconds(1)/2.0,Cleanup_Area(oSecond,FALSE));
   // handle second
   SetLocalObject(oMod,"oHCFirst",oSecond);
   if (!GetLocalInt(oMod,"bDoNotControlPop")) DelayCommand(10.0,ExecuteScript("pop_control",oMod));
}
//////////////////////////////////////////////////////////////////[ MAIN ]//////


///////////////////////
// FUNCTIONS
///////////////////////


void Cleanup_Area(object oWaypoint,int bFirst=TRUE)
{ // PURPOSE: See prototype
  // Version History
  // mm/dd/yyyy | MODIFIED BY      | DESCRIPTION
  // 09/22/2006 | Deva B. Winblood | Initial version
    int nN;
    object oOb;
    int nFlagged=0;
    string sArea=GetTag(GetArea(oWaypoint));
    string sStoredArea;
    int nState;
    string sID;
    string sTime="D:"+IntToString(GetCalendarDay())+" H:"+IntToString(GetTimeHour());
    PrintString("Cleanup_Area Routine has begun at time "+sTime+" for area ["+GetName(GetArea(oWaypoint))+"] FIRST?:"+IntToString(bFirst));
    oOb=GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR,PLAYER_CHAR_IS_PC,oWaypoint,1);
    if (GetIsObjectValid(oOb))
    { // PC present
        PrintString("    PC Present.  No cleanup done.");
        return;
    } // PC present
    nN=1;
    oOb=GetNearestObject(OBJECT_TYPE_CREATURE,oWaypoint,nN);
    while(GetIsObjectValid(oOb))
    { // cleanup
        if (!GetIsPC(oOb)&&!GetIsDM(oOb))
        { // not a PC or DM
            sID=GetLocalString(oOb,"sTeamID");
            sID=GetStringUpperCase(sID);
            if (sID=="SPID"||sID=="DWF"||sID=="UNC"||sID=="UND")
            { // is a team based NPC
                nState=GetLocalInt(oOb,"nMState");
                if(nState!=2&&nState!=7&&!GetLocalInt(oOb,"bChampion")&&!GetIsInCombat(oOb))
                { // not in guard or stand state
                    if (bFirst)
                    { // first
                        sStoredArea=GetLocalString(oOb,"sHCFlag");
                        if (sStoredArea==sArea)
                        { // destroy
                            nFlagged++;
                            DelayCommand(1.0,DestroyObject(oOb));
                        } // destroy
                    } // first
                    else
                    { // second
                        SetLocalString(oOb,"sHCFlag",sArea);
                        nFlagged++;
                    } // second
                } // not in guard or stand state
            } // is a team based NPC
        } // not a PC or DM
        nN++;
        oOb=GetNearestObject(OBJECT_TYPE_CREATURE,oWaypoint,nN);
    } // cleanup
    if (bFirst)  PrintString("   NPCs marked for destruction:"+IntToString(nFlagged));
    else {       PrintString("   NPCs flagged to check again:"+IntToString(nFlagged)); }
} // Cleanup_Area()