//::///////////////////////////////////////////////////////////////
//:: Database item storage "lite"
//:: Written by Rich Dersheimer - June 2003
//:: Allows database storage of a container item's worth of objects
//::///////////////////////////////////////////////////////////////
//:: Usage: PC must have a "container" item in his inventory, with
//:: the tag "STORAGE", in order to store items in the database,
//:: and to retrieve any stored items from the database.
//:: Additionally, there must be a usable placeable, or some other
//:: way to trigger this script. I've attached it to the OnUsed
//:: event of a chest, but I imagine it could be triggered in a
//:: conversation with a "Storage Unit Manager" or "Banker" NPC.
//::
//:: To store items in the database, put the items inside the
//:: storage box, and trigger this script.
//::
//:: To retrieve items from the database, make sure the storage
//:: box is empty, and trigger this script.
//::
//:: You can't store any items if you've already got some stored,
//:: and you can't retrieve any if your storage box has something
//:: in it. That way, the database only stores a limited number
//:: of items (any combination up to 35 small items) and hopefully
//:: the process doesn't use up too much bandwidth.
//::
//:: Thanks to BaadF00d on the NWN scripting forum for sparking
//:: the discussion and thought that led to this script.
//::
//:: NWN scripters have my permission to use and abuse this script
//:: as they see fit, but do try to give me credit for the code :)
//:: - Rich Dersheimer
//::///////////////////////////////////////////////////////////////

void main()
{
    object oPC = OBJECT_SELF;
    object oBox = GetItemPossessedBy(oPC, "bs_storage");
    object oItem;
    string sDatabaseName = "LODStorage"; // put your database name here
    int nBoxUsed = GetCampaignInt(sDatabaseName, "BoxUsed");
    int nCount = 1;

    // make sure a PC used the chest
    if(!GetIsPC(oPC))
        return;

    // make sure the PC has a storage box
    if(oBox == OBJECT_INVALID)
    {
        SendMessageToPC(oPC, "In order to store or retrieve items, you must have a storage box in your inventory!");
        return;
    }

    if(nBoxUsed == FALSE) // nothing is stored, attempt to store items
    {
        // make sure there is something in the box
        if(GetFirstItemInInventory(oBox) == OBJECT_INVALID)
        {
            SendMessageToPC(oPC, "There was nothing in your box to store.");
            return;
        }

        oItem = GetFirstItemInInventory(oBox);

        while(oItem != OBJECT_INVALID)
        {
            // store the item in the database
            StoreCampaignObject(sDatabaseName, "Item" + IntToString(nCount), oItem, oPC);

            // increase the stored item index count
            nCount++;

            // nuke the item you just stored
            DestroyObject(oItem);

            // get the next item
            oItem = GetNextItemInInventory(oBox);
        }

        // flag the database as having stored some of your items
        SetCampaignInt(sDatabaseName, "BoxUsed", TRUE);
    }
    else // something is stored, so attempt to retrieve stored items
    {
        // make sure there is nothing in the box
        if(GetFirstItemInInventory(oBox) != OBJECT_INVALID)
        {
            SendMessageToPC(oPC, "Your box must be empty to retrieve your stored items!");
            return;
        }

        // retrieve the first item stored
        oItem = RetrieveCampaignObject(sDatabaseName, "Item" + IntToString(nCount), GetLocation(oPC), oBox, oPC);

        // loop through all items stored
        while(oItem != OBJECT_INVALID)
        {
            // remove the retrieved item from the database
            DeleteCampaignVariable(sDatabaseName, "Item" + IntToString(nCount), oPC);

            // increase the stored item index count
            nCount++;

            // get the next item
            oItem = RetrieveCampaignObject(sDatabaseName, "Item" + IntToString(nCount), GetLocation(oPC), oBox, oPC);
        }

        // flag the database as having no items stored by you
        SetCampaignInt(sDatabaseName, "BoxUsed", FALSE);
    }
}