//::///////////////////////////////////////////////
//:: Area_Clean_Usr
//:: Copyright (c) 2004 Indigo-Red
//:://////////////////////////////////////////////
/*
    Designed to clean the area when no players are present.
    Works in conjunction with the following two functions:
        CheckForPC() & TimerCheck()
    (Examine the "area_clean_inc" script for more detail)
*/
//:://////////////////////////////////////////////
//:: Created By:    Johnnie "Nekyia" Casiano
//:: Created On:    20040208
//:://////////////////////////////////////////////

// **** CONSTANTS
float Default=1.0; //If no time is specified for the area, the default is 3 minutes


// **** FUNCTION DECLARATIONS

// Function gets the area's delay; if none found assigns it the default (300 secs)
float GetDelay(object Area=OBJECT_SELF);

// Function screens objects in the area to make sure they should be destroyed
int CleanCheck(object Trash, object Area=OBJECT_SELF);

//  Function used to clear the area of any NPCs, loot bags and general trash
void CleanArea(object Area=OBJECT_SELF);

//Function to clear the inventory prior to destroying the container (BodyBag)
void CleanInventory(object Bag);


// **** CUSTOM FUNCTIONS
float GetDelay(object Area=OBJECT_SELF)
{
    float Delay=GetLocalFloat(Area,"Delay");
    // Set a default delay to an area that has not been specifically defined
        if(Delay==0.0){
            SetLocalFloat(Area,"Delay",Default);}
    //SendMessageToPC(GetFirstPC(),
        //"Delay setting for "+GetName(Area)+" is "+FloatToString(Delay));
    return Delay;   // Delay set on area
}

int CleanCheck(object Trash, object Area=OBJECT_SELF)
{
   //SendMessageToPC(GetFirstPC(),"Checking: "+GetName(Trash));
    if(GetIsPC(Trash) && !GetIsDM(Trash)){   // Halt if a PC is found
        return 3;}
    if(GetIsEncounterCreature(Trash)){
        return TRUE;}
    if(GetObjectType(Trash)==OBJECT_TYPE_ITEM){
    // Designed to recycle dropped items & loot if there is a shop assigned
    //  to the area. A separate script can be used on the loot shop
    //  to further move the loot to their appropriate treasure pile.
        object Loot=GetObjectByTag(GetLocalString(Area,"Loot"));
            if(GetIsObjectValid(Loot) && !GetIsPC(Loot) && GetHasInventory(Loot))
            {CopyItem(Trash,Loot,TRUE);}
        return TRUE;}
    if(GetObjectType(Trash)==OBJECT_TYPE_PLACEABLE && GetTag(Trash)=="BodyBag"){
        CleanInventory(Trash);
        return TRUE;}
    return FALSE;
}


void CleanArea(object Area=OBJECT_SELF)
{   // Halts cleanup if a player is present
    if(GetLocalInt(Area,"PCCount")>=1){
        //SendMessageToPC(GetFirstPC(),"Cleanup Aborted");
        return;}
    // Begin scanning through the various objects in the area
    //SendMessageToPC(GetFirstPC(),"Cleanup Started for area: "+GetTag(Area));
    object Trash=GetFirstObjectInArea(Area);
        while(GetIsObjectValid(Trash)){
            int Verify=CleanCheck(Trash);
                if(Verify==3){
                    return;}
                if(Verify==TRUE){
                    float Delay=GetDelay()-Random(FloatToInt(GetDelay()));
                    DelayCommand(Delay,DestroyObject(Trash));
                    //SendMessageToPC(GetFirstPC(),"Will Destroy: "+GetName(Trash));
                    //SendMessageToPC(GetFirstPC(),"Object Tag: "+GetTag(Trash));
                    //SendMessageToPC(GetFirstPC(),"Time To Purge: "+FloatToString(Delay));
                }
        Trash=GetNextObjectInArea(Area);
        }
    SetLocalInt(Area,"Timer",0);
    //SendMessageToPC(GetFirstPC(),
        //"Cleanup for "+GetName(Area)+" is complete.");
}


void CleanInventory(object Bag)
{
    object Item=GetFirstItemInInventory(Bag);
        while(GetIsObjectValid(Item)){
            //SendMessageToPC(GetExitingObject(),"Destroying from BodyBag:"+GetName(Item));
            DestroyObject(Item);
        Item=GetNextItemInInventory(Bag);
        }
    //SendMessageToPC(GetExitingObject(),GetName(Bag)+" now empty.");
}


void main()
{
    if(GetUserDefinedEventNumber()==69){
        float Delay=GetDelay()+Random(FloatToInt(GetDelay()));
        DelayCommand(Delay,CleanArea());
        //SendMessageToPC(GetFirstPC(),"Signal Received");
        //SendMessageToPC(GetFirstPC(),"Delay:"+FloatToString(Delay));
    }
    return;
}
