DMH-Online

Dungeon Masters Helper · Est. 2005

Areas Scripts

NWN Script: clean_trash_001.htm

Author Sticky: Post Working Scripts Here
Lokmar Iskaran Posted: Wednesday, 26 June 07:13PM
These scripts are designed to run a routine every 10 rounds (1 minute) and remove all items from an area that have been sitting on the floor for 5 minutes.

The first script is for the module's OnHeartbeat function. It goes through player inventory and sets a custom tag called "Age" to zero. This makes sure that any player-dropped item gets it's counters set to zero.

Code:
// Lokmar Iskaran's clean-up scripts
//
// This is the MODULE OnHeartbeat script, which makes sure all PC held items have
// reset "Age" tags.
void main()
{
    int modulecounter1 = 0;
    modulecounter1 = GetLocalInt(OBJECT_SELF, "modulecounter1");
    if (modulecounter1 == 0)
    {
        SetLocalInt(OBJECT_SELF, "modulecounter1", 2);
        // First tick, so first six seconds have already passed, thus we set it to 2
        // we're counting from 1 to 11 rather than 0 to 10, because 0 is the result
        // returned if the LocalInt does not exist... (as was tested above.)
    }
    else
    {
        modulecounter1++;
        if (modulecounter1 < 11)  // since counter starts at 1, need to go to 11 for 10 rounds
        {
            SetLocalInt(OBJECT_SELF, "modulecounter1", modulecounter1);
        }
        else
        {
            SetLocalInt(OBJECT_SELF, "modulecounter1", 1);
            // here we go through player inventory and reset all items' "Age" tag to zero.
            // this way, if they pick up items, it resets the age, so they have a correct
            // lifespan when dropped again
            // this code should go in the world/module heartbeat function
            object charObj = OBJECT_INVALID;
            object invObj = OBJECT_INVALID;
            charObj = GetFirstPC();
            while (charObj != OBJECT_INVALID)
            {
                if (GetArea(charObj) == OBJECT_SELF)
                {
                    invObj = GetFirstItemInInventory(charObj);
                    while (invObj != OBJECT_INVALID)
                    {
                        SetLocalInt(invObj, "Age", 0);
                        invObj = GetNextItemInInventory(charObj);
                    }
                }
            charObj = GetNextPC();
            }
        }
    }
}



The next is an area OnHeartbeat script that should be used in any area that you wish to have automatically cleaned of old items. (Useful in persisten worlds.)

Code:
// Lokmar Iskaran's clean-up scripts
//
// This is the AREA OnHeartbeat script that should be used for any area where
// you want items left on the floor to be deleted after the "Age" variable reaches
// a certain limit.  The codes fires every 10 rounds, which is one minute... and
// the item "Age" limit is current set to "< 5" (5 minutes, essentially) which
// can be altered below.
// Or, you could just set a module variable for the age limit and modify that in
// just the one spot.  It's up to you.

void main()
{
    int areacounter = 0;
    areacounter = GetLocalInt(OBJECT_SELF, "AreaCounter1");
    if (areacounter == 0)
    {
        SetLocalInt(OBJECT_SELF, "AreaCounter1", 2);
        // First tick, so first six seconds have already passed, thus we set it to 2
        // we're counting from 1 to 11 rather than 0 to 10, because 0 is the result
        // returned if the LocalInt does not exist... (as was tested above.)
    }
    else
    {
        areacounter++;
        if (areacounter < 11)  // since area counter starts at 1, need to go to 11 for 10 rounds
        {
            SetLocalInt(OBJECT_SELF, "AreaCounter1", areacounter);
        }
        else
        {
            SetLocalInt(OBJECT_SELF, "AreaCounter1", 1);
            object testObj = OBJECT_INVALID;
            location baseloc = Location(OBJECT_SELF, Vector (),0.0);
            int objAge = 0;
            int obcounter = 1;
            testObj = GetNearestObjectToLocation(OBJECT_TYPE_ITEM, baseloc, obcounter);
            while (testObj != OBJECT_INVALID)
            {
                objAge = GetLocalInt(testObj, "Age");
                objAge++;
                if (objAge < 5)
                {
                    SetLocalInt(testObj, "Age", objAge);
                }
                else
                {
                    DestroyObject(testObj, 0.0);
                }
                obcounter++;
                testObj = GetNearestObjectToLocation(OBJECT_TYPE_ITEM, baseloc, obcounter);
            }
        }
    }
}



This should help those of you who want persistent worlds where dropped items keep cluttering up the joint.

⌂ All Scripts ← PreviousNext → 👁 Category List

All Areas Scripts

📄 ambient_npc_ani 📄 area_spawner 📄 change_lighting 📄 clean_trash_001 📄 clean_trash_002 📄 clean_trash_003 📄 despawn_mobs 📄 despawn_mobs_002 📄 disable_sound 📄 full_edge_transition_001 📄 full_edge_transition_002 📄 full_edge_transition_003 📄 full_edge_transition_004 📄 full_edge_transition_004a 📄 get_pcs 📄 guard_patrols 📄 keyed_transitions 📄 random_enc_001 📄 random_enc_002 📄 random_enc_003 📄 random_enc_004 📄 random_enc_006 📄 random_enc_009 📄 random_encounter_005 📄 random_interiors 📄 random_interiors_001a 📄 restrict_access 📄 sitting_area