//::///////////////////////////////////////////////
//:: Scarface's Persistent Banking
//:: sfpb_close
//:://////////////////////////////////////////////
/*
Written By Scarface
Modified By AmanShadar
*/
//////////////////////////////////////////////////
#include "sfpb_config"
#include "x4_inc_functions"
void main()
{
// Vars
object oPC = GetLastClosedBy();
object oChest = OBJECT_SELF;
location lLoc = GetLocation(oPC);
string sModName = CharacterDB(oPC);
string sUserID = GetLocalString(oChest, "USER_ID");
int nCount, iCopy;
int iBagID = 1; //start with 1, as 0 comes from lack of variable
object oNewItem;
// Lock the chest
SetLocked(oChest, TRUE);
// First loop to check for containers
// This loop now just counts the items,
// and adds the Bag ID to the bag and items in the bag.
object oItem = GetFirstItemInInventory(oChest);
object oBagItem;
while (GetIsObjectValid(oItem))
{
// Item count
nCount++;
if (GetHasInventory(oItem))
{
//add Bag ID variable to bag and items
SetLocalInt(oItem, "BagID", iBagID);
oBagItem = GetFirstItemInInventory(oItem);
while(GetIsObjectValid(oBagItem))
{
//add Bag ID to item in bag.
SetLocalInt(oBagItem, "BagID", iBagID);
// Item count
oBagItem = GetNextItemInInventory(oItem);
}
iBagID++; //for next bag, if found
//Scarface's original "No Bags" policy, we are going around that.
/*
// Send a message to the player
FloatingTextStringOnCreature("Containers/bags are NOT allowed to" +
IntToString(MAX_ITEMS) + " be stored!!!" +
"\nPlease remove the container/bag.", oPC);
// Unlock chest and end script
SetLocked(oChest, FALSE);
return;
*/
}
if (nCount > MAX_ITEMS)
{
// Send a message to the player
FloatingTextStringOnCreature("Only a maximum of " +
IntToString(MAX_ITEMS) + " items are allowed to be stored!!!" +
"\nPlease remove the excess items.", oPC);
// Unlock chest and end script
SetLocked(oChest, FALSE);
return;
}
// Next item
oItem = GetNextItemInInventory(oChest);
}
// Spawn in the NPC storer
object oStorer = CreateObject(OBJECT_TYPE_CREATURE, "sfpb_storage", lLoc, FALSE, sUserID);
// Loop through all items in the chest and copy them into
// the NPC storers inventory and destroy the originals
int iGold = GetGold(oChest);
SetCampaignInt(sModName, "GOLD_IN_LOCKER", iGold);
oItem = GetFirstItemInInventory(oChest);
//First the containers' items get marked to no copy, as they will be when the
//container itself is copied
while(GetIsObjectValid(oItem))
{
if (GetHasInventory(oItem))
{
oBagItem = GetFirstItemInInventory(oItem);
while(GetIsObjectValid(oBagItem))
{
//Mark as copied
SetLocalInt(oBagItem, "copied", 1);
// Next item
oBagItem = GetNextItemInInventory(oItem);
}
}
oItem = GetNextItemInInventory(oChest);
}
oItem = GetFirstItemInInventory(oChest);
//now copy the items
while (GetIsObjectValid(oItem))
{
// This is to stop the duping bug, the dupe bug happened when a player
// would exit the server while still holding a chest open, the reason for
// the duping was the NPC storer would never spawn in this case thus not
// having anywhere to store the items, which ended up the items storing
// back into the chest duplicating itself, now if this happens, the players
// items will not be saved thus avoiding any unwanted item duplicates.
if (!GetIsObjectValid(oStorer))
{
// Delete the local CD Key
DeleteLocalString(oChest, "USER_ID");
// Unlock Chest
SetLocked(oChest, FALSE);
return;
}
// Copy item to the storer
if (GetHasInventory(oItem))
{
oNewItem = CopyObject(oItem, GetLocation(oStorer), oStorer);
}
else
{
iCopy = GetLocalInt(oItem, "copied");
if(iCopy != 1) oNewItem = CopyItem(oItem, oStorer, TRUE);
}
// Destroy Original
DestroyObject(oItem);
//remove the copied variable from the new item.
DeleteLocalInt(oNewItem, "copied");
// Next item
oItem = GetNextItemInInventory(oChest);
}
// Save the NPC storer into the database
StoreCampaignObject(sModName, DATABASE_ITEM + sUserID, oStorer);
// Destroy NPC storer
DestroyObject(oStorer);
// Delete the local CD Key
DeleteLocalString(oChest, "USER_ID");
// Unlock Chest
DelayCommand(5.0, SetLocked(oChest, FALSE));
}