/*--------------------------------------------------------

Script Name: gen_geg_treas
----------------------------------------------------------
Created By: Genisys(Guile)
Created On: 4/14/09
----------------------------------------------------------
This script is executed from an object's OnOpen or OnDeath
event, it used to generate an Enchanting Gem.

To Utilize this script simply place the following code
after void main() {  in the event script you desire to spawn
the Gems into (by % chance)

I've added a second box, for more rare gems, this should
allow you to control how often certain ones get passed out..

ExecuteScript("gen_geg_treas", OBJECT_SELF);

----------------------------------------------------------*/
//Required Include!
#include "gen_enc_config"

//PROTOTYPE DECLARED
int nCount(object oBox);
int GetPercentRoll();
int GetRandomPercentile();

//MAIN SCRIPT
void main()
{

 int nONCE = GetLocalInt(OBJECT_SELF, "GEN_GEM");
 if(nONCE ==1)
 {
  //Stop here we only generate the treasure
  //1 time ever for this object (OnDeath!!)
  return;
 }
 else
 {
  SetLocalInt(OBJECT_SELF, "GEN_GEM", 1);
 }


 //The % Roll
 int a = GetRandomPercentile();
 int j = GetRandomPercentile();

 //The box that holds the gems!
 object oBox = GetObjectByTag("gen_chest_sys");
 object oBox2 = GetObjectByTag("gen_treasure");
 object oSpawn;

 //Get the total # of gems in the chest..
 int b = nCount(oBox);
 int f = nCount(oBox2);

 int c = Random(b);//the selected item..
 int g = Random(f);//the selected item..

 int d = c +1; //add +1 to the count!
 int h = g +1; //Add +1 to the count!

 int e = 0; //The increment counter..
 int k = 0; //the increment counter..

 object oItem; //the item we are getting

 //Only a RARE 8 % chance of getting a decent gem...
 if(a<=70 && a >=63)
 {
  oItem = GetFirstItemInInventory(oBox);
  while(GetIsObjectValid(oItem))
  {
   e+=1;

   if(e==d)
   {
    //Create the item on the Object this script is used on
    //Copy the variable too (CRITICAL!)
    CopyItem(oItem, OBJECT_SELF, TRUE);
   }

   oItem = GetNextItemInInventory(oBox);
  }
 }

 //If only a RARE 3% chance of getting a good gem!
 if(j>=51 && j>=49)
 {
  oItem = GetFirstItemInInventory(oBox);
  while(GetIsObjectValid(oItem))
  {
   k+=1;

   if(k==h)
   {
    //Create the item on the Object this script is used on
    //Copy the variable too (CRITICAL!)
    CopyItem(oItem, OBJECT_SELF, TRUE);
   }

   oItem = GetNextItemInInventory(oBox);
  }
 }

//Main Script End..
}

//PROTOTYPE DEFINED
int nCount(object oBox)
{
 int i = 0;
 object oGem = GetFirstItemInInventory(oBox);
 while(GetIsObjectValid(oGem))
 {
  i+=1;

  oGem = GetNextItemInInventory(oBox);
 }

 return i;
}

//Prototype Defined
int GetPercentRoll()
{
 int nPercent = Random(100) + 1;
 int i;

  //Get a more random interpretation
 if(nPercent <=20)
 { i = d20(1); }
 else if(nPercent>=21 && nPercent<=40)
 { i = d20(1) + 20; }
 else if(nPercent>=41 && nPercent<=60)
 { i = d20(1) + 40; }
 else if(nPercent>=61 && nPercent<=80)
 { i = d20(1) + 60; }
 else if(nPercent>=81 && nPercent<=100)
 { i = d20(1) + 80; }
 return i;
}

int GetRandomPercentile()
{

 int nHour = GetTimeHour();
 int nX, a;
 for (nX = 0;nX <= nHour;nX++) {
    a=Random(100)+1; }



 //If between 10-90 don't do anything..
 if(a<=10 || a<=90)
 {
   //do nothing...
 }
 //Otherwise reroll 1 more time
 //Note: we can still have a roll of 1-10 or 90-100
 else
 {
   a = GetPercentRoll();
 }

return a;

}