//sChest = string SourceChest tag
//oChest = Object Destination Chest
// grab random object from source Chest
// and create a copy of it in Destination chest
// the source chests are in the Dungeon Master's Palace
// and have unique names
void MakeTreasure(string sChest,object oChest){
 int nCountItems;
 int nRandomItem;
 object oSource;
 object oItem;
 string sResRefItem;
 oSource = GetObjectByTag(sChest);
 if(!GetIsObjectValid(oSource))return;
 //count item logic
 oItem=GetFirstItemInInventory(oSource);
 nCountItems = 0;
 while(GetIsObjectValid(oItem)){
    nCountItems++;
    oItem=GetNextItemInInventory(oSource);
 }
 //now get random item
 nRandomItem=Random(nCountItems)+1;
 oItem=GetFirstItemInInventory(oSource);
 while(nRandomItem>1){
  nRandomItem--;
  oItem=GetNextItemInInventory(oSource);
 }
 sResRefItem = GetResRef(oItem);
 CreateItemOnObject(sResRefItem,oChest);
}

void main()
{
   object oThisChest = OBJECT_SELF;
   location lThisChest = GetLocation(oThisChest);
   float fDelay = 10.0; // delay in seconds
   int nFlag= 0;
   string sSourceChest; // Source of Treasure
   nFlag = GetLocalInt(oThisChest, "Used");
   sSourceChest = GetLocalString(GetArea(oThisChest),"SourceChest");
   if(nFlag > 0) return; // timer in progress
   //2 pieces of junk, 1 good item
   //obviously chests with these tags apear in the
   //Dungeon Masters Palace and have the appropriate
   //items in them
   MakeTreasure("cgchestjunk",oThisChest);
   if(d2() > 1 ) MakeTreasure("cgchestjunk",oThisChest);
   if(d3() > 1 ) MakeTreasure(sSourceChest,oThisChest);
   //MakeTreasure("cgchest3",oThisChest);
   SetLocalInt(oThisChest,"Used",1); // this chest has been
   // used already, don't do it again

}