// 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();
    }


}

