/* Tables 7-7*/
#include "inc_loot_mund"
/* Table 8-3*/
#include "inc_loot_armor"
/* Table 8-10*/
#include "inc_loot_weapons"
/* Table 8-18*/
#include "inc_loot_potions"
/* Table 8-19*/
#include "inc_loot_rings"
/* Table 8-20*/
#include "inc_loot_rod"
/* Table 8-21*/
#include "inc_loot_scrolls"
/* Table 8-26*/
#include "inc_loot_staff"
/* Table 8-27*/
#include "inc_loot_wand"


// Subfunction of GenerateLoot
// Determines types and amounts of loot per CR
void GenerateItems(object oMonster,int iCR, int iItems);
// Sub function of GenerateItems
// Generates the actual loot.
void GenerateMagicItem(object oContainer, int iNum, int iCategory);
//Minor Magic Item Table
void Table82a();
//Medium Magic Item Table
void Table82b();
//Major Magic Item Table
void Table82c();
// Minor Misc Magic Item Table
void Table828();
// Medium Misc Magic Item Table
void Table829();
// Major Misc Magic Item Table
void Table830();

void GenerateItems(object oMonster,int iCR, int iItems)
    {
    int iRoll = d100();
    int iMundane,iMinor,iMedium,iMajor,iEpic;
    if (iCR > 20)
        {
        iEpic = iCR;
        iCR = 20;
        }
    switch (iCR)
        {
        case 1:
            {
            if (iRoll <= 71) break;
            else if (iRoll <= 95) {iMundane = 1;break;}
            else {iMinor = 1;break;}
            }
        case 2:
            {
            if (iRoll <= 49) break;
            else if (iRoll <= 95) {iMundane = 1;break;}
            else {iMinor = 1;break;}
            }
        case 3:
            {
            if (iRoll <= 49) break;
            else if (iRoll <= 95) {iMundane = d3();break;}
            else {iMinor = 1;break;}
            }
        case 4:
            {
            if (iRoll <= 42) break;
            else if (iRoll <= 62) {iMundane = d4();break;}
            else {iMinor = 1;break;}
            }
        case 5:
            {
            if (iRoll <= 57) break;
            else if (iRoll <= 67) {iMundane = d4();break;}
            else {iMinor = d3();break;}
            }
        case 6:
            {
            if (iRoll <= 54) break;
            else if (iRoll <= 59) {iMundane = d4();break;}
            else if (iRoll <= 99) {iMinor = d3();break;}
            else {iMedium = 1;break;}
            }
        case 7:
            {
            if (iRoll <= 51) break;
            else if (iRoll <= 97) {iMinor = d3();break;}
            else {iMedium = 1;break;}
            }
        case 8:
            {
            if (iRoll <= 48) break;
            else if (iRoll <= 96) {iMinor = d4();break;}
            else {iMedium = 1;break;}
            }
        case 9:
            {
            if (iRoll <= 43) break;
            else if (iRoll <= 91) {iMinor = d4();break;}
            else {iMedium = 1;break;}
            }
        case 10:
            {
            if (iRoll <= 40) break;
            else if (iRoll <= 88) {iMinor = d4();break;}
            else if (iRoll <= 99) {iMedium = 1;break;}
            else {iMajor = 1;break;}
            }
        case 11:
            {
            if (iRoll <= 31) break;
            else if (iRoll <= 84) {iMinor = d4();break;}
            else if (iRoll <= 98) {iMedium = 1;break;}
            else {iMajor = 1;break;}
            }
        case 12:
            {
            if (iRoll <= 27) break;
            else if (iRoll <= 82) {iMinor = d6();break;}
            else if (iRoll <= 97) {iMedium = 1;break;}
            else {iMajor = 1;break;}
            }
        case 13:
            {
            if (iRoll <= 19) break;
            else if (iRoll <= 73) {iMinor = d6();break;}
            else if (iRoll <= 95) {iMedium = 1;break;}
            else {iMajor = 1;break;}
            }
        case 14:
            {
            if (iRoll <= 19) break;
            else if (iRoll <= 58) {iMinor = d6();break;}
            else if (iRoll <= 92) {iMedium = 1;break;}
            else {iMajor = 1;break;}
            }
        case 15:
            {
            if (iRoll <= 11) break;
            else if (iRoll <= 46) {iMinor = d10();break;}
            else if (iRoll <= 90) {iMedium = 1;break;}
            else {iMajor = 1;break;}
            }
        case 16:
            {
            if (iRoll <= 40) break;
            else if (iRoll <= 46) {iMinor = d10();break;}
            else if (iRoll <= 90) {iMedium = d3();break;}
            else {iMajor = 1;break;}
            }
        case 17:
            {
            if (iRoll <= 33) break;
            else if (iRoll <= 83) {iMedium = d3();break;}
            else {iMajor = 1;break;}
            }
        case 18:
            {
            if (iRoll <= 24) break;
            else if (iRoll <= 80) {iMedium = d4();break;}
            else {iMajor = 1;break;}
            }
        case 19:
            {
            if (iRoll <= 4) break;
            else if (iRoll <= 70) {iMedium = d4();break;}
            else {iMajor = 1;break;}
            }
        case 20:
            {
            if (iRoll <= 25) break;
            else if (iRoll <= 65) {iMedium = d4();break;}
            else {iMajor = d3();break;}
            }
        };
    if (iEpic)
        {
        if (iEpic == 21) iMajor += 1;
        else if (iEpic == 22) iMajor += 2;
        else if (iEpic == 23) iMajor += 4;
        else if (iEpic == 24) iMajor += 6;
        else if (iEpic == 25) iMajor += 9;
        else if (iEpic == 26) iMajor += 12;
        else if (iEpic == 27) iMajor += 17;
        else if (iEpic == 28) iMajor += 23;
        else if (iEpic == 29) iMajor += 31;
        else if (iEpic >= 30) iMajor += 42;
        }
    if (iMundane)
        GenerateMundane(iMundane, oMonster, 1);
    else if (iMinor)
        GenerateMagicItem(oMonster,iMinor,1);
    else if (iMedium)
        GenerateMagicItem(oMonster,iMedium,2);
    else if (iMajor)
        GenerateMagicItem(oMonster,iMajor,3);
    }

void GenerateMagicItem(object oContainer, int iNum, int iCategory)
    {
    int i,iType;
    string sResRef;
    object oObject;
    for (i = 1; i <= iNum; i++)
        {
        if (iCategory == 1)
            Table82a();
        else if (iCategory == 2)
            Table82b();
        else if (iCategory == 3)
            Table82c();
        oObject = CreateItemOnObject(sResRef,oContainer);
        iType = GetBaseItemType(oObject);
        if (iType == BASE_ITEM_ARROW
         || iType == BASE_ITEM_BOLT
         || iType == BASE_ITEM_BULLET)
            {
            iType = StringToInt(GetStringRight(sResRef,3));
            if (iType <= 600)
                CreateItemOnObject(sResRef,oContainer,Random(50));
            }
        }
    }

void Table82a() // Converted for 9
    {
    int iRoll = d100();
    if (iRoll <= 4) SetIdentified(Table83a(),FALSE);
    else if (iRoll <= 9) SetIdentified(Table810a(),FALSE);
    else if (iRoll <= 44) Table818a();
    else if (iRoll <= 46) Table819a();
    else if (iRoll <= 81) // 1d3 scrolls
        {
        for (iRoll = 1; iRoll<= d3(); iRoll +=1)
            {
            Table821(1);
            }
        }
    else if (iRoll <= 91) Table827(1);
    else Table828();
    }

void Table82b() // Converted for 9
    {
    int iRoll = d100();
    if (iRoll <= 10)      SetIdentified(Table83b(),FALSE);
    else if (iRoll <= 20) SetIdentified(Table810b(),FALSE);
    else if (iRoll <= 30) Table818b();
    else if (iRoll <= 40) Table819b();
    else if (iRoll <= 50) Table820b();
    else if (iRoll <= 65) // 1d4 scrolls
        {
        for (iRoll = 1; iRoll<= d4(); iRoll +=1)
            {
            Table821(2);
            }
        }
    else if (iRoll <= 68) Table826b();
    else if (iRoll <= 83) Table827(2);
    else Table829();
    }

void Table82c() //Converted for 9
    {
    int iRoll = d100();
    string sString;
    if (iRoll <= 10)      SetIdentified(Table83c(),FALSE);
    else if (iRoll <= 20) SetIdentified(Table810c(),FALSE);
    else if (iRoll <= 35) Table818c();
    else if (iRoll <= 35) Table819c();
    else if (iRoll <= 45) Table820c();
    else if (iRoll <= 55) // 1d6 scrolls
        {
        for (iRoll = 1; iRoll<= d6(); iRoll +=1)
            {
            Table821(3);
            }
        }
    else if (iRoll <= 75) Table826c();
    else if (iRoll <= 80) Table827(3);
    else Table830();
    }


void Table828() // Converted
    {
    string sResRef;
    int iRoll = d100();
    if (iRoll >= 99)
        Table828(); //Reroll
    else
        CreateItemOnObject("misca"+Convert(iRoll));
    }

void Table829() // Converted
    {
    string sResRef;
    int iRoll = d100();
    if (iRoll >= 99)
        Table829(); //Reroll
    else
        CreateItemOnObject("miscb"+Convert(iRoll));
    }
void Table830()//Converted
    {
    string sResRef;
    int iRoll = d100();
    if (iRoll <= 2)         iRoll = 2;
    else if (iRoll <= 4)    iRoll = 4;
    else if (iRoll <= 6)    iRoll = 6;
    else if (iRoll <= 8)    iRoll = 8;
    else if (iRoll <= 10)   iRoll = 10;
    else if (iRoll <= 12)   iRoll = 12;
    else if (iRoll <= 14)   iRoll = 14;
    else if (iRoll <= 20)   {} //Dead spot to keep table correct.
    else if (iRoll <= 22)   iRoll = 22;
    else if (iRoll <= 24)   iRoll = 24;
    else if (iRoll <= 26)   iRoll = 26;
    else if (iRoll <= 28)   iRoll = 28;
    else if (iRoll <= 30)   iRoll = 30;
    else if (iRoll <= 32)   iRoll = 32;
    else if (iRoll <= 42)   {} //Dead spot to keep table correct.
    else if (iRoll <= 44)   Table830(); //Reroll
    else if (iRoll <= 52)   {}// Dead Spot to keep table correct.
    else if (iRoll <= 54)   iRoll = 54;
    else if (iRoll <= 76)   {} //Dead spot to keep table correct.
    else if (iRoll <= 80)   iRoll = 80;
    else if (iRoll == 96)   Table830(); //Reroll
    else if (iRoll <= 99)   {} //Dead spot to keep table correct.
    else if (iRoll == 100)  Table830(); //Reroll

    if (iRoll == 38) // Horn of Valhalla
        {
        iRoll = d100();
        if (iRoll <= 40)        sResRef = "miscc1039";
        else if (iRoll <= 75)   sResRef = "miscc2039";
        else if (iRoll <= 90)   sResRef = "miscc3039";
        else                    sResRef = "miscc4039";
        }
    else
        sResRef = "miscc"+Convert(iRoll);
    CreateItemOnObject(sResRef);
    }
//void main () {}
