//************************************************
//** ss_treasure                                **
//**                                            **
//** This is the main sctipt for the treasure   **
//** spawn system.  It governes what items are  **
//** spawned and how they are.                  **
//************************************************
//**Created By: Jason Hunter (SiliconScout)     **
//**                                            **
//**Version: 1.8a                               **
//**                                            **
//**Last Changed Date: February 4, 2006         **
//************************************************
void main()
{
    string sType;
    int i;
    int iChance;
    int iNumber;
    string sID;
    int iInventory;
    int iRoll;
    int iCount;
    object oCopy;
    object oTChest;
    object oItem;
    object oItem2;

    int MAX_num_TABLES = 37;    //Maximum # of merchant store pulls



    for (i = 0; i <= MAX_num_TABLES; i++)
    {
        sType = GetLocalString(OBJECT_SELF,"ss_t_chest_"+IntToString(i));
        if (sType != "") // kick out if the string is empty
        {
            iChance= StringToInt(GetStringLeft(sType,3));
            iNumber= StringToInt(GetStringRight(sType,3));

    //This code it used to determine if the items should spawn in unidentified.
    //If the length is = to 7 then it is long enough, if the middle character
    //is a U then set the item unidentified.
//SendMessageToPC(GetFirstPC(),"The string length is "+ IntToString(GetStringLength(sType)));
            sID="I am empty";
            if (GetStringLength(sType) ==7)
            {
                sID = GetStringLeft(sType,4);
                sID = GetStringRight(sID,1);
                sID = GetStringUpperCase(sID);
            }
            else
            {
                //This will stop checking the SID part and speed things up a tiny bit.
                sID = "STOP";
            }


            while (iNumber > 0)
            {
                // pulls the chance denominator desired, if no local int is
                //specified it assumes that the denominator is 100.
                iRoll = GetLocalInt(OBJECT_SELF,"ss_t_chest_"+IntToString(i));

                // This defaults to 100
                if (iRoll == 0) iRoll=100;

                iRoll = (Random(iRoll)+1); //roll chance

                if (iRoll > iChance)
                {
                    iNumber--;
                }else
                {

                    //count up the inventory in the treasure chest
                    oTChest =GetObjectByTag("ss_t_chest_"+IntToString(i));
                    iInventory=0;
                    object oInv = GetFirstItemInInventory(oTChest);
                    while (oInv != OBJECT_INVALID)
                    {
                        iInventory=iInventory+1;
                        oInv=GetNextItemInInventory(oTChest);
                    }
                    iRoll = (Random(iInventory)+1); //roll random item position
                    oItem = GetFirstItemInInventory(oTChest);
                    if (iRoll ==1) // get first item here
                    {
                    }else  //otherwise roll over until I get the correct item.
                    {
                        for (iCount = 2; iCount <= iRoll; iCount++)
                        {
                            oItem = GetNextItemInInventory(oTChest);
                        }
                     }

                     oCopy = CopyItem(oItem,OBJECT_SELF,TRUE);//create item on NPC

                     if (GetLocalInt(oTChest,"finite") > 0 )
                     {
                         //This is a uniqe spawn kill the original
                         // unique spawns limied to a single draw.
                         DestroyObject(oItem,0.01);
                         iNumber=0;
                     }
                     else
                     {
                         iNumber--;
                     }

                     //set the default item properties
                     SetIdentified(oCopy,TRUE);
                     SetPickpocketableFlag(oCopy, TRUE);
                     SetStolenFlag(oCopy,FALSE);
                     SetPlotFlag(oCopy, FALSE);

                    if (sID != "STOP")
                    /*check to see if it should be set as non-ID'd, stolen
                      no PP a combo there of or all 3 */
                    {
                         if (sID == "L") SetPlotFlag(oCopy, TRUE);
                         if (sID == "P") SetPickpocketableFlag(oCopy,FALSE);
                         if (sID == "S") SetStolenFlag(oCopy,TRUE);
                         if (sID == "U") SetIdentified(oCopy,FALSE);

                         if (sID == "A")
                         {
                             SetPickpocketableFlag(oCopy,FALSE);
                             SetPlotFlag(oCopy, TRUE);
                         }

                         if (sID == "B")
                         {
                             SetStolenFlag(oCopy,TRUE);
                             SetPlotFlag(oCopy, TRUE);
                         }

                         if (sID == "C")
                         {
                             SetIdentified(oCopy,FALSE);
                             SetPlotFlag(oCopy, TRUE);
                         }

                         if (sID == "D")
                         {
                             SetStolenFlag(oCopy,TRUE);
                             SetPickpocketableFlag(oCopy,FALSE);
                             SetPlotFlag(oCopy, TRUE);
                         }

                         if (sID == "E")
                         {
                             SetIdentified(oCopy,FALSE);
                             SetPickpocketableFlag(oCopy,FALSE);
                             SetPlotFlag(oCopy, TRUE);
                         }

                         if (sID == "F")
                         {
                             SetStolenFlag(oCopy,TRUE);
                             SetIdentified(oCopy,FALSE);
                             SetPlotFlag(oCopy, TRUE);
                         }

                         if (sID == "G")
                         {
                             SetPickpocketableFlag(oCopy,FALSE);
                             SetStolenFlag(oCopy,TRUE);
                         }

                         if (sID == "H")
                         {
                             SetIdentified(oCopy,FALSE);
                             SetPickpocketableFlag(oCopy,FALSE);
                         }

                         if (sID == "I")
                         {
                             SetStolenFlag(oCopy,TRUE);
                             SetIdentified(oCopy,FALSE);
                             SetPickpocketableFlag(oCopy,FALSE);
                         }

                         if (sID == "T")
                         {
                             SetIdentified(oCopy,FALSE);
                             SetStolenFlag(oCopy,TRUE);
                         }

                         if (sID == "Z")
                         {
                             SetStolenFlag(oCopy,TRUE);
                             SetIdentified(oCopy,FALSE);
                             SetPickpocketableFlag(oCopy,FALSE);
                             SetPlotFlag(oCopy, TRUE);
                         }
                    }
                }
            }
        }
    }
//Generate Gold Coins
    string sGolds = GetLocalString(OBJECT_SELF,"ss_t_golds");

    int iNumDice = StringToInt(GetStringLeft(sGolds,3));
    int iPenalty = StringToInt(GetStringRight(sGolds,3));
    sGolds = GetStringLeft(sGolds,7);
    int iDice= StringToInt(GetStringRight(sGolds,3));

    int iGolds =0;

    for (i=iNumDice; i>0;i--)
    {
        iGolds = iGolds + (Random(iDice)+1);
    }

    iGolds = iGolds + iPenalty;

    if (GetObjectType(OBJECT_SELF) == OBJECT_TYPE_CREATURE)
    {
        GiveGoldToCreature(OBJECT_SELF,iGolds);
    }
    else
    {
        CreateItemOnObject("nw_it_gold001",OBJECT_SELF,iGolds);
    }

//Generate Ammo.
    sID = GetLocalString(OBJECT_SELF,"ss_t_ammo");
    if (sID == "") return;  //kick out as no ammo needs to be made
    //ammo is a go lets get busy!

    string sAmmo=GetStringLeft(sID,1);
    sAmmo = GetStringUpperCase(sAmmo);

    string sNumAmmo = GetStringRight(sID,2);
    int iAmmo = StringToInt(sNumAmmo);
    int iSlot;

    if (sAmmo == "A")
    {
        sAmmo="nw_wamar001";
        iSlot=INVENTORY_SLOT_ARROWS;
    }
    if (sAmmo == "C")
    {
        sAmmo="nw_wambo001";
        iSlot=INVENTORY_SLOT_BOLTS;
    }
    if (sAmmo == "S")
    {
        sAmmo="nw_wambu001";
        iSlot=INVENTORY_SLOT_BULLETS;
    }

    oCopy = CreateItemOnObject(sAmmo,OBJECT_SELF,iAmmo);
    DelayCommand(0.2,ActionEquipItem(oCopy,iSlot));
}