// Copyright (C) 2002, Leo Lipelis
// need to declare this function ahead of its use
// to avoid compilation error;
// this function will destroy an object and everything
// inside of it recursively
void TrashObject(object obj)
{
    // if this is not a container, just destroy it and we're done
    if (GetHasInventory(obj) == FALSE) {
        DestroyObject(obj);
    } else {
        object oItem = GetFirstItemInInventory(obj);
        // destroy everything in the inventory first
        while (oItem != OBJECT_INVALID)
        {
            TrashObject(oItem);
            oItem = GetNextItemInInventory(obj);
        }
        // destroy the container itself
        DestroyObject(obj);
    }
}
void main()
{
    int iItemDropLifeSpan = 20;
    // iCounterMax must always be greater than iItemDropLifeSpan
    int iCounterMax = 1000;
    string sTrashMark = "iTrashMark";
    int iCounter = GetLocalInt(OBJECT_SELF, "iCounter");
    iCounter++;
    iCounter = iCounter % iCounterMax;
    if (iCounter == 0) { iCounter++; } // 0 is reserved
    SetLocalInt(OBJECT_SELF, "iCounter", iCounter);
    object obj = GetFirstObjectInArea();
    while (obj != OBJECT_INVALID)
    {
        int iObjType = GetObjectType(obj);
        // match based on type first, because comparing int
        // is faster than string; wrong types don't reach
        // string comparison
        if (iObjType == OBJECT_TYPE_PLACEABLE)
        {
            string sTag = GetTag(obj);
            // this assumes that every item drop from a monster
            // comes in a placeable container with a tag name
            // of "Body Bag"; it seems to be true, but if this
            // assumption is false, this code needs to be fixed
            if (sTag == "Body Bag")
            {
                int iTrashMark = GetLocalInt(obj, sTrashMark);
                if (iTrashMark == 0) {
                    SetLocalInt(obj, sTrashMark, iCounter);
                } else {
                    int iTimeAlive = 0;
                    if (iCounter >= iTrashMark) {
                        iTimeAlive = iCounter - iTrashMark;
                    } else {
                        iTimeAlive = iCounterMax - iTrashMark + iCounter - 1;
                    }
                    if (iTimeAlive > iItemDropLifeSpan) {
                        TrashObject(obj);
                    }
                }
            }
        }
        obj = GetNextObjectInArea();
    }

// Modification by Dalantriel
// Caltulate average PC level to scale difficulty

// Calculate number of PCs on the level
    int nNumberOfPCs = 0;
    int nPlayerFactor = 0;
    int nChallenge = 0;
    int nRandom;
    object oPC = GetFirstPC();
    while (GetIsObjectValid(oPC))
    {
        if (GetIsPC(oPC))
        {
            nNumberOfPCs++;
        }
        oPC = GetNextPC();
    }

    if (nNumberOfPCs == 1)
    {
    nChallenge = (GetFactionAverageLevel(GetFirstPC())) - 3;
    }
    else if (nNumberOfPCs == 2)
    {
    nChallenge = (GetFactionAverageLevel(GetFirstPC())) - 1;
    }
    else if (nNumberOfPCs > 5)
    {
    nChallenge = (GetFactionAverageLevel(GetFirstPC())) + 1;
    }
    else
    {
    nChallenge = GetFactionAverageLevel(GetFirstPC());
    }

    SetLocalInt(OBJECT_SELF, "nChallengelevel", nChallenge);

    if (nChallenge > 11)
    {
    SetLocalInt(OBJECT_SELF, "nDifficultylevel", 4);
    }
    else if (nChallenge > 9)
    {
    SetLocalInt(OBJECT_SELF, "nDifficultylevel", 3);
    }
    else if (nChallenge > 7)
    {
    SetLocalInt(OBJECT_SELF, "nDifficultylevel", 2);
    }
    else if (nChallenge > 4)
    {
    SetLocalInt(OBJECT_SELF, "nDifficultylevel", 1);
    }
    else
    {
    SetLocalInt(OBJECT_SELF, "nDifficultylevel", 0);
    }

/*
// Announce End of scenario to players and restart Module

    if(GetLocalInt(OBJECT_SELF, "nGoodNPCDead") > 0)
    {
        ActionSpeakString("*** Scenario has been Lost. Log out and Restart ***", TALKVOLUME_SHOUT);
        int nDeadtime = 0;
        if ((nDeadtime = GetLocalInt(OBJECT_SELF, "DeadTime")) < 20)
        {
            // SetLocalInt(OBJECT_SELF, "DeadTime", nDeadtime + 1);
            ActionSpeakString("*** Scenario has been Lost. Log out and Restart ***", TALKVOLUME_SHOUT);
        }
        else
        {
            // StartNewModule(GetModuleName());
            ActionSpeakString("*** Scenario has been Lost. Log out and Restart ***", TALKVOLUME_SHOUT);
        }
    }
*/
}

