#include "NW_O2_CONINCLUDE" void DoOnce(object oTreasure); void CheckContents(object oTreasure); void CheckThing(object oTreasure, object oThing, int iThing); int GetItemStackSizeInInventory(object oTreasure, string sResRef); int P_MAX_ITEMS = 5; // Max number of things to respawn. This should be less // expensive than an array, though less pretty. void main() { object oArea = GetArea(OBJECT_SELF); object oTreasure = GetFirstObjectInArea(oArea); int iLastTime = GetLocalInt(oArea, "AG_TIME"); int iLastDay = GetLocalInt(oArea, "AG_DAY"); int iCurrentHour = GetTimeHour(); int iCurrentDay = GetCalendarDay(); // If same day, is it more than 12 hours later? // If it is the next day, is it more than 12 hour later? // Or is it more than one day since ... if((iCurrentDay == iLastDay && iCurrentHour - 12 >= iLastTime) || (iCurrentDay - 1 == iLastDay && iCurrentHour + 12 >= iLastTime) || iCurrentDay - 1 > iLastDay) { while(GetIsObjectValid(oTreasure)) { if(GetHasInventory(oTreasure) && (GetObjectType(oTreasure) == OBJECT_TYPE_PLACEABLE)) { SetLocalInt(oTreasure, "NW_DO_ONCE", 0); if (GetLocalInt(oTreasure, "pac_do_once") == 0) { SetLocalInt(oTreasure, "pac_do_once", 1); DoOnce(oTreasure); } else { CheckContents(oTreasure); } } oTreasure = GetNextObjectInArea(oArea); } SetLocalInt(oArea, "AG_TIME", iCurrentHour); SetLocalInt(oArea, "AG_DAY", iCurrentDay); } } // Saves the inventory items void DoOnce(object oTreasure) { int iCounter = 0; object oThing = GetFirstItemInInventory(oTreasure); int iThing = GetItemStackSize(oThing); int iGold = GetGold(oTreasure); for (iCounter = 0; iCounter < P_MAX_ITEMS; iCounter++) { SetLocalObject(oTreasure, "oThing_" + IntToString(iCounter), oThing); SetLocalInt(oTreasure, "iThing_" + IntToString(iCounter), iThing); if (GetIsObjectValid(oThing)) { oThing = GetNextItemInInventory(oTreasure); iThing = GetItemStackSize(oThing); } } SetLocalInt(oTreasure, "iGold", iGold); } int GetItemStackSizeInInventory(object oTreasure, string sThing) { if (!GetHasInventory(oTreasure)) { return 0; } object oItem = GetFirstItemInInventory(oTreasure); while (GetIsObjectValid(oItem) && (GetResRef(oItem) != sThing)) { oItem = GetNextItemInInventory(oTreasure); } if (GetIsObjectValid(oItem)) { return GetItemStackSize(oItem); } return 0; } // Now get the the first saved item void CheckContents(object oTreasure) { object oThing = OBJECT_INVALID; int iThing = 1; int iCounter = 0; int iGoldStack = GetGold(oTreasure); int iGoldOriginal = GetLocalInt(oTreasure, "iGold"); for(iCounter = 0; iCounter < P_MAX_ITEMS; iCounter++) { oThing = GetLocalObject(oTreasure, "oThing_" + IntToString(iCounter)); iThing = GetLocalInt(oTreasure, "iThing_" + IntToString(iCounter)); CheckThing(oTreasure, oThing, iThing); } if(iGoldOriginal != iGoldStack) { if(iGoldOriginal > iGoldStack) { int iGoldAdd = iGoldOriginal - iGoldStack; CreateItemOnObject("NW_IT_GOLD001", oTreasure, iGoldAdd); } } } // Now check to see if the first saved item is in the inventory void CheckThing(object oTreasure, object oThing, int iThing) { // See if the object is already in the inventory. If so, don't worry about it. if (!GetIsObjectValid(oThing)) { return; } int iFound = FALSE; object oBoxThing = GetFirstItemInInventory(oTreasure); string sThing = GetResRef(oThing); int nStackSize = iThing; while (GetIsObjectValid(oBoxThing)) { if (sThing == GetResRef(oBoxThing)) { iFound = TRUE; } oBoxThing = GetNextItemInInventory(OBJECT_SELF); } // If item not found in inventory, Create it. if(!iFound) { nStackSize = nStackSize - GetItemStackSizeInInventory(oTreasure, sThing); if(nStackSize > 0) { CreateItemOnObject(GetResRef(oThing), oTreasure, iThing); // It's not in the box } } }