Author Topic: Help with my on open script  (Read 481 times)

Legacy_Jackrabbit_Slim

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Help with my on open script
« on: September 16, 2015, 09:48:43 pm »


               

I'm trying to put this script in my on open event for my chests to  have them respawn after a set time.


 


Currently it works fine except one little glitch. If I do not take anything out of the chest and wait for the timer to reset, the next time I open it I only get an item and no gold.


 


Also, I would like to add in the % chance of more than one item being generated and make it up to three. I looked inside nw_o2_coninclude and tried adding it myself but no dice. Any help would be most appreciated.


 


Thanks in advance!



#include "nw_o2_coninclude"



void FillInventory()
{
    object oObject = GetFirstItemInInventory();
    // First, do a while loop to destroy any contents that the chest might
    // contain
    while (oObject != OBJECT_INVALID)
    {
        DestroyObject(oObject);
        oObject = GetNextItemInInventory();
    }

}

void main()
{
    float fDelay = 60.0;
    if (GetLocalInt(OBJECT_SELF,"NW_DO_ONCE") != 0)
    {
       return;
    }
    SetLocalInt(OBJECT_SELF,"NW_DO_ONCE",1);
    DelayCommand(fDelay,SetLocalInt(OBJECT_SELF,"NW_DO_ONCE",0));
    ShoutDisturbed();
    FillInventory();


    object oLastOpener = GetLastOpener();
   // Custom Drop script for low treasure.

      int nDrop = d100(1);
      int nWhich = d100(1);

      int nRandomize = Random( Random( GetTimeMillisecond() +1) +1); // This makes the random number function behave randomly.
      int nMinGPDrop = 10;     // Minimum amount of gold to drop.
      int nMaxGPDrop = 25; // Maximum amount of gold to drop.
      int nGPDrop = Random( nMaxGPDrop -nMinGPDrop +1) +nMinGPDrop;

      if (nDrop<100) //or whatever % you want
      {
      CreateItemOnObject( "nw_it_gold001", OBJECT_SELF, nGPDrop);

      }
      int nDropChance = d20(1);
      int nWhichItem = d20(1);
      if (nDropChance<100) //% CHANCE OF DROP

      {
     switch(nWhichItem)

      {

      // LOW TREASURE TABLE

      case 1: CreateItemOnObject("nw_wamar001");
      break;
      case 2: CreateItemOnObject("nw_wambo001");
      break;
      case 3: CreateItemOnObject("nw_wambu001");
      break;
      case 4: CreateItemOnObject("nw_aarcl011");
      break;
      case 5: CreateItemOnObject("nw_aarcl005");
      break;
      case 6: CreateItemOnObject("nw_aarcl009");
      break;
      case 7: CreateItemOnObject("nw_aarcl001");
      break;
      case 8: CreateItemOnObject("nw_aarcl002");
      break;
      case 9: CreateItemOnObject("nw_aarcl012");
      break;
      case 10: CreateItemOnObject("x0_armhe005");
      break;
      case 11: CreateItemOnObject("nw_wbwmsl001");
      break;
      case 12: CreateItemOnObject("nw_it_mring024");
      break;
      case 13: CreateItemOnObject("nw_it_mring012");
      break;
      case 14: CreateItemOnObject("nw_wbwmsh002");
      break;
      case 15: CreateItemOnObject("nw_wbwmxl002");
      break;
      case 16: CreateItemOnObject("nw_wbwmxh002");
      break;
      case 17: CreateItemOnObject("nw_wbwmln002");
      break;
      case 18: CreateItemOnObject("nw_wmgwn006");
      break;
      case 19: CreateItemOnObject("nw_wmgwn013");
      break;
      case 20: CreateItemOnObject("x0_it_mring002");
      break;

         }



}
}





               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Help with my on open script
« Reply #1 on: September 17, 2015, 01:17:33 pm »


               

The problem you are having is that the gold is stackable. So you first set the existing gold item in the chest to be destroyed, BUT this will not happen until the script ends. Then you create more gold, which gets added to the stack of existing gold (one item, larger stack size). Then the script ends and the item gets destroyed.


 


One way to fix this is to put a slight delay on the creation of the new items/gold. You'll need to move that to a function. 


 


As to the second part... move your which item switch statement and the creation of that item to its own function as well.  Leave the chance stuff in the main routine. Then you can make a second roll with a lower chance and call it again if needed.


               
               

               
            

Legacy_Jackrabbit_Slim

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Help with my on open script
« Reply #2 on: September 17, 2015, 05:54:04 pm »


               

Thanks Meaglyn. Not sure how to add the delay function though. I'm still learning scripting and that one is beyond me at the moment. '<img'>


I did add a function for the item creation. It's still a bit wonky though since in my testing the 10% chance for the 2nd item seems to be showing up a lot more than 10%. I would appreciate any more suggestions or examples on how to fix my current script.


 


Thanks!



#include "nw_o2_coninclude"



void FillInventory()
{
    object oObject = GetFirstItemInInventory();
    // First, do a while loop to destroy any contents that the chest might
    // contain
    while (oObject != OBJECT_INVALID)
    {
        DestroyObject(oObject);
        oObject = GetNextItemInInventory();
    }

}
void MakeItems()
{



      int nWhichItem = d20(1);



      {
     switch(nWhichItem)

      {

      // LOW TREASURE TABLE

      case 1: CreateItemOnObject("nw_wamar001");
      break;
      case 2: CreateItemOnObject("nw_wambo001");
      break;
      case 3: CreateItemOnObject("nw_wambu001");
      break;
      case 4: CreateItemOnObject("nw_aarcl011");
      break;
      case 5: CreateItemOnObject("nw_aarcl005");
      break;
      case 6: CreateItemOnObject("nw_aarcl009");
      break;
      case 7: CreateItemOnObject("nw_aarcl001");
      break;
      case 8: CreateItemOnObject("nw_aarcl002");
      break;
      case 9: CreateItemOnObject("nw_aarcl012");
      break;
      case 10: CreateItemOnObject("x0_armhe005");
      break;
      case 11: CreateItemOnObject("nw_wbwmsl001");
      break;
      case 12: CreateItemOnObject("nw_it_mring024");
      break;
      case 13: CreateItemOnObject("nw_it_mring012");
      break;
      case 14: CreateItemOnObject("nw_wbwmsh002");
      break;
      case 15: CreateItemOnObject("nw_wbwmxl002");
      break;
      case 16: CreateItemOnObject("nw_wbwmxh002");
      break;
      case 17: CreateItemOnObject("nw_wbwmln002");
      break;
      case 18: CreateItemOnObject("nw_wmgwn006");
      break;
      case 19: CreateItemOnObject("nw_wmgwn013");
      break;
      case 20: CreateItemOnObject("x0_it_mring002");
      break;


         }
}

}






void main()
{
    float fDelay = 60.0;
    if (GetLocalInt(OBJECT_SELF,"NW_DO_ONCE") != 0)
    {
       return;
    }
    object oLastOpener = GetLastOpener();
    SetLocalInt(OBJECT_SELF,"NW_DO_ONCE",1);
    DelayCommand(fDelay,SetLocalInt(OBJECT_SELF,"NW_DO_ONCE",0));
    ShoutDisturbed();
    FillInventory();



   // Custom Drop script for low treasure.

      int nDrop = d100(1);
      int nWhich = d100(1);

      int nRandomize = Random( Random( GetTimeMillisecond() +1) +1); // This makes the random number function behave randomly.
      int nMinGPDrop = 10;     // Minimum amount of gold to drop.
      int nMaxGPDrop = 25; // Maximum amount of gold to drop.
      int nGPDrop = Random( nMaxGPDrop -nMinGPDrop +1) +nMinGPDrop;

      if (nDrop<100) //or whatever % you want
      {
      CreateItemOnObject( "nw_it_gold001", OBJECT_SELF, nGPDrop);

      }
      // make 1st roll for treasure
      int nDropChance = d20(1);
      if (nDropChance<100) //100% CHANCE OF DROP
      MakeItems();

      // make 2nd roll for treasure

      int nDropChance2 = d20(1);
      if (nDropChance2<10) //10% CHANCE OF DROP
      {
      MakeItems();

      }





}


               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Help with my on open script
« Reply #3 on: September 17, 2015, 06:12:27 pm »


               

Make this part into a function too:



void createGold(object oObj, int nGPDrop)
{
CreateItemOnObject( "nw_it_gold001", oObj, nGPDrop);

}

The call it with



object oSelf = OBJECT_SELF;
DelayCommand(0.05, createGold(oSelf, nGPDrop));

in the main code where you currently create the gold.


 


Good job making the item creation into a function.


 


The percentages are off because you are rolling a d20 and checking if it's less the 10. That's 50%.


               
               

               
            

Legacy_Jackrabbit_Slim

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Help with my on open script
« Reply #4 on: September 17, 2015, 08:22:10 pm »


               

Thank you so much, works like a charm! I shouldn't do math while on my first cup of coffee either. '<img'>