void CreateArcaneScroll(object oTarget, object oAdventurer, int nModifier = 0)
{
  int nMaxSpells = 21;
  int nHD = GetHitDice(oAdventurer) + nModifier;
  int nScroll = 1;
  int nLevel = 1;

  // Create an arcane scroll of level 4-7
  nLevel = d4() + 3;
  nScroll = Random(nMaxSpells) + 1;
  int nMax = 5;
  switch (nLevel)
  {
    case 0: nMax = 4; break;
    case 1: nMax = 13; break;
    case 2: nMax = 21; break;
    case 3: nMax = 15; break;
    case 4: nMax = 17; break;
    case 5: nMax = 13; break;
    case 6: nMax = 14; break;
    case 7: nMax = 8; break;
    case 8: nMax = 9; break;
    case 9: nMax = 12; break;
  }
  if (nScroll > nMax) nScroll = nMax;

  string sRes = "nw_it_sparscr216";

  if (nScroll < 10)
  {
    sRes = "NW_IT_SPARSCR" + IntToString(nLevel) + "0" + IntToString(nScroll);
  }
  else
  {
    sRes = "NW_IT_SPARSCR" + IntToString(nLevel) + IntToString(nScroll);
  }
  CreateItemOnObject(sRes, oTarget, 1);
  }

void CreateDivineScroll(object oTarget, object oAdventurer, int nModifier=0)
{
  int nHD = GetHitDice(oAdventurer) + nModifier;
  string sScroll = "";

  // Create one of these 9 random divine scrolls
  int nRandom = Random(9) + 1;
  switch (nRandom)
  {
    case 1: sScroll = "nw_it_spdvscr201"; break;
    case 2: sScroll = "nw_it_spdvscr202"; break;
    case 3: sScroll = "nw_it_spdvscr203"; break;
    case 4: sScroll = "nw_it_spdvscr204"; break;
    case 5: sScroll = "nw_it_spdvscr301"; break;
    case 6: sScroll = "nw_it_spdvscr302"; break;
    case 7: sScroll = "nw_it_spdvscr401"; break;
    case 8: sScroll = "nw_it_spdvscr402"; break;
    case 9: sScroll = "nw_it_spdvscr501"; break;
  }

  CreateItemOnObject(sScroll, oTarget, 1);

}

void main()
{
  object oPC = GetLastOpenedBy();
  if (GetLocalInt(OBJECT_SELF,"Done") == 1)
    return;
  if (GetIsObjectValid(oPC) == FALSE)
    return;
  int nProbArcane = 70;
  int nProbDivine = 30;
  int nRandom = d100();
  string sResref;
  if (nRandom <= nProbArcane)
    CreateArcaneScroll(OBJECT_SELF,oPC);
  if (nRandom <= nProbDivine)
    CreateDivineScroll(OBJECT_SELF,oPC);
  int nBookNum = Random(11) + 1;
  if (nBookNum < 10)
    sResref = "greyhawkbook00" + IntToString(nBookNum);
  else
    sResref = "greyhawkbook0" + IntToString(nBookNum);
  CreateItemOnObject(sResref,OBJECT_SELF);
  SetLocalInt(OBJECT_SELF,"Done",1);
}
