#include "_sh_inc_list"
#include "69_hench_lib"
#include "quest_inc"

void Body(object oExiting)
{
    //A workaround to prevent the OnEnter event body from firing after loading a saved game
    if (GetLocalInt(GetModule(), "LoadCooldown") == TRUE) return;

    if (!GetIsPC(oExiting)) return;

    //Subtrack from the counter of PCs in the area
    SetLocalInt(OBJECT_SELF, "Players", GetLocalInt(OBJECT_SELF, "Players")-1);

    //The PC fails their quest if they leave the quest area before completing the quest
    string sDB = CharacterDB(oExiting);

    if (GetLocalInt(oExiting, "FailIfLeaves") == TRUE) //this is for players doing an escort quest and leaving when shit hits the fan
    {
        DestroyEscortedClient(oExiting);
        RemoveJournalQuestEntry("random_quest", oExiting, FALSE);
        DeleteQuest(oExiting);
    }
    DeleteLocalInt(oExiting, "FailIfLeaves");
    DeleteLocalInt(oExiting, "BanditsPaid");

    if (GetLocalObject(OBJECT_SELF, "PartyLeader") == oExiting && GetIsQuestComplete(oExiting) == FALSE && GetIsQuestFailed(oExiting) == FALSE && GetCampaignString(sDB, "QUEST_TEMPLATE") != "escort" && GetCampaignString(sDB, "QUEST_TEMPLATE") != "lost")
    {
        SetCampaignInt(sDB, "QUEST_JOURNAL", GetCampaignInt(sDB, "QUEST_JOURNAL")+2);
        AddRandomQuestJournalEntry(oExiting, 3);
    }

    string sAreaString = GetLocalString(OBJECT_SELF, "AreaString");
    DeleteLocalInt(oExiting, "TalkedAdv");
    DeleteLocalInt(oExiting, "Duel");
    int nPopulated = FALSE;
    object oObject;

    if (GetLocalInt(OBJECT_SELF, "Players") > 0) nPopulated = TRUE;

    if (nPopulated == FALSE)
    {
        DeleteLocalInt(OBJECT_SELF, "IsPopulated");
        DeleteLocalInt(OBJECT_SELF, "NO_ESCAPE");
        DeleteLocalInt(OBJECT_SELF, "ThugNoticed");
        DeleteLocalInt(OBJECT_SELF, "TributePaid");
        DeleteLocalInt(OBJECT_SELF, "Level");
        DeleteLocalInt(OBJECT_SELF, "AmbushPrepared");
        DeleteLocalInt(OBJECT_SELF, "Duel");
        DeleteLocalInt(OBJECT_SELF, "MerchantTrouble");
        DeleteLocalInt(OBJECT_SELF, "DiscoveredVillage");
        DeleteLocalInt(OBJECT_SELF, "DiscoveredDungeon");
        DeleteLocalObject(OBJECT_SELF, "PartyLeader");
        if (GetLocalInt(OBJECT_SELF, "Battle") != 0) MusicBattleChange(OBJECT_SELF, GetLocalInt(OBJECT_SELF, "Battle"));

        //Delete all chests, items and creatures in the area
        oObject = GetFirstObjectInArea(OBJECT_SELF);
        while (GetIsObjectValid(oObject))
        {
            if (GetTag(oObject) == "anc_tchest" || GetObjectType(oObject) == OBJECT_TYPE_CREATURE || GetObjectType(oObject) == OBJECT_TYPE_ITEM)
            {
                if ( !GetIsPC(oObject) && !GetIsPC(GetMaster(oObject)) && !GetIsPC(GetMaster(GetMaster(oObject))) && !GetIsPC(GetLastMaster(oObject)) ) //do NOT delete PCs and their associates
                DestroyObject(oObject);
                if ( !GetIsPC(oObject) && GetIsDead(oObject) && GetLocalInt(oObject, "Escorted") == TRUE) //...unless they are a dead escorted NPC
                DestroyObject(oObject);
            }
            if (GetTag(oObject) == "anc_pcloot")
            {
                object oItem = GetFirstItemInInventory(oObject);
                while (GetIsObjectValid(oItem))
                {
                    DestroyObject(oItem);
                    oItem = GetNextItemInInventory(oObject);
                }
                DestroyObject(oObject);
            }
            /*if (GetObjectType(oObject) == OBJECT_TYPE_TRIGGER)
            {
                DestroyObject(oObject);
            }*/
            oObject = GetNextObjectInArea(OBJECT_SELF);
        }

        //Add this area to the list of free areas (1.06)
        string sRegionString = GetStringLeft(sAreaString, 1);
        string sList = "sList"+sRegionString+"q";
        object oList = GetLocalObject(GetModule(), sList);
        int i;
        string sListString = ListGetString(oList, i);
        for (i = 1; i <= ListGetElementCount(oList); i++)
        {
            if (ListGetString(oList, i) == sAreaString)
            {
                i = -1;
                break;
            }
        }
        if (i != -1) ListAddString(oList, sAreaString);

        //TEST
        /*int i;
        for (i = 1; i <= ListGetElementCount(oList); i++)
        {
            FloatingTextStringOnCreature(ListGetString(oList, i), oExiting);
        }
        FloatingTextStringOnCreature("Onexit done, Onenter now", oExiting);*/
    }
}

void main()
{
    object oExiting = GetExitingObject();
    DelayCommand(1.0, Body(oExiting));
}