#include "fcb_inc_prot"

//==========================================/
//create all
//==========================================/

//==========================================/
void array_main()
//==========================================/
    {
    //2DA file rows that will be read
    //range: [lower bound 31:16][upper bound 15:0]
    //invalid rows may be included - they are skipped during read.
    SetLocalInt(FCB_HOST, "IPRP_ABILITIES_0", 5);
    SetLocalInt(FCB_HOST, "IPRP_ACMODTYPE_0", 4);
    SetLocalInt(FCB_HOST, "IPRP_ALIGNGRP_0", 5);
    SetLocalInt(FCB_HOST, "IPRP_ALIGNMENT_0", 8);
    SetLocalInt(FCB_HOST, "IPRP_AMMOCOST_0", 15);
    SetLocalInt(FCB_HOST, "IPRP_AMMOTYPE_0", 2);
    SetLocalInt(FCB_HOST, "IPRP_AMOUNT_0", 4);
    SetLocalInt(FCB_HOST, "IPRP_ARCSPELL_0", 19);
    //SetLocalInt(FCB_HOST, "IPRP_BASE1_0", 0);
    SetLocalInt(FCB_HOST, "IPRP_BLADECOST_0", 5);
    SetLocalInt(FCB_HOST, "IPRP_BONUSCOST_0", 12);
    SetLocalInt(FCB_HOST, "IPRP_CHARGECOST_0", 13);
    SetLocalInt(FCB_HOST, "IPRP_COLOR_0", 6);
    SetLocalInt(FCB_HOST, "IPRP_COMBATDAM_0", 2);
    SetLocalInt(FCB_HOST, "IPRP_COSTTABLE_0", 27);

    SetLocalInt(FCB_HOST, "IPRP_DAMAGECOST_0", 30);
    SetLocalInt(FCB_HOST, "IPRP_DAMAGECOST_1", 71 << BITLEN_2DA_RANGE | 80); //PRC

    SetLocalInt(FCB_HOST, "IPRP_DAMAGETYPE_0", 14); //13
    SetLocalInt(FCB_HOST, "IPRP_DAMVULCOST_0", 7);

    SetLocalInt(FCB_HOST, "IPRP_FEATS_0", 39);
    SetLocalInt(FCB_HOST, "IPRP_FEATS_1", 250 << BITLEN_2DA_RANGE | 254); //PRC
    SetLocalInt(FCB_HOST, "IPRP_FEATS_2", 271 << BITLEN_2DA_RANGE | 385); //PRC
    SetLocalInt(FCB_HOST, "IPRP_FEATS_3", 399 << BITLEN_2DA_RANGE | 570); //PRC

    SetLocalInt(FCB_HOST, "IPRP_IMMUNCOST_0", 7);
    SetLocalInt(FCB_HOST, "IPRP_IMMUNITY_0", 9);
    SetLocalInt(FCB_HOST, "IPRP_KITCOST_0", 50);
    SetLocalInt(FCB_HOST, "IPRP_LIGHTCOST_0", 4);
    SetLocalInt(FCB_HOST, "IPRP_MELEECOST_0", 20);
    SetLocalInt(FCB_HOST, "IPRP_MONSTCOST_0", 58);
    SetLocalInt(FCB_HOST, "IPRP_MONSTERHIT_0", 9);
    SetLocalInt(FCB_HOST, "IPRP_NEG10COST_0", 10);
    SetLocalInt(FCB_HOST, "IPRP_NEG5COST_0", 10); //PRC 10
    SetLocalInt(FCB_HOST, "IPRP_ONHIT_0", 25);
    SetLocalInt(FCB_HOST, "IPRP_ONHITCOST_0", 70); //PRC 70

    SetLocalInt(FCB_HOST, "IPRP_ONHITDUR_0", 4); //PRC 20
    SetLocalInt(FCB_HOST, "IPRP_ONHITDUR_1", 20 << BITLEN_2DA_RANGE | 20);

    SetLocalInt(FCB_HOST, "IPRP_ONHITSPELL_0", 140); //PRC 202
    SetLocalInt(FCB_HOST, "IPRP_ONHITSPELL_1", 200 << BITLEN_2DA_RANGE | 202);

    SetLocalInt(FCB_HOST, "IPRP_PARAMTABLE_0", 11);
    SetLocalInt(FCB_HOST, "IPRP_POISON_0", 5);
    SetLocalInt(FCB_HOST, "IPRP_PROTECTION_0", 19);
    SetLocalInt(FCB_HOST, "IPRP_REDCOST_0", 5);

    SetLocalInt(FCB_HOST, "IPRP_RESISTCOST_0", 10); //PRC 20
    SetLocalInt(FCB_HOST, "IPRP_RESISTCOST_1", 20 << BITLEN_2DA_RANGE | 20);

    SetLocalInt(FCB_HOST, "IPRP_SAVEELEMENT_0", 21);
    SetLocalInt(FCB_HOST, "IPRP_SAVINGTHROW_0", 3);
    SetLocalInt(FCB_HOST, "IPRP_SKILLCOST_0", 50);
    //SetLocalInt(FCB_HOST, "IPRP_SLOTSCOST_0", 0);
    SetLocalInt(FCB_HOST, "IPRP_SOAKCOST_0", 13); //PRC 13
    SetLocalInt(FCB_HOST, "IPRP_SPELLCOST_0", 242); //PRC 242
    SetLocalInt(FCB_HOST, "IPRP_SPELLCSTR_0", 39);
    SetLocalInt(FCB_HOST, "IPRP_SPELLLVCOST_0", 9);
    SetLocalInt(FCB_HOST, "IPRP_SPELLLVLIMM_0", 9);

    SetLocalInt(FCB_HOST, "IPRP_SPELLS_0", 500); //PRC 1138
    SetLocalInt(FCB_HOST, "IPRP_SPELLS_1", 511 << BITLEN_2DA_RANGE | 569);
    SetLocalInt(FCB_HOST, "IPRP_SPELLS_2", 600 << BITLEN_2DA_RANGE | 802);

    SetLocalInt(FCB_HOST, "IPRP_SPELLSHL_0", 7);
    SetLocalInt(FCB_HOST, "IPRP_SRCOST_0", 60); //PRC 60

    //SetLocalInt(FCB_HOST, "IPRP_STAMINACOAT_0", 0);
    //SetLocalInt(FCB_HOST, "IPRP_TERRAINTYPE_0", 0);
    SetLocalInt(FCB_HOST, "IPRP_TRAPCOAT_0", 11);
    SetLocalInt(FCB_HOST, "IPRP_TRAPS_0", 4);
    SetLocalInt(FCB_HOST, "IPRP_VISUALFX_0", 6);
    SetLocalInt(FCB_HOST, "IPRP_WALK_0", 1);
    SetLocalInt(FCB_HOST, "IPRP_WEIGHTCOST_0", 5);
    SetLocalInt(FCB_HOST, "IPRP_WEIGHTINC_0", 5);

    //--------------------------------------/
    SetLocalInt(FCB_HOST, "itempropdef_0", 84);
    SetLocalInt(FCB_HOST, "itemprops_0", 84);
    SetLocalInt(FCB_HOST, "itemvalue_0", 59);

    SetLocalInt(FCB_HOST, "Classes_0", 38); //PRC 254
    SetLocalInt(FCB_HOST, "Classes_1", 51 << BITLEN_2DA_RANGE | 56);
    SetLocalInt(FCB_HOST, "Classes_2", 65 << BITLEN_2DA_RANGE | 105);
    SetLocalInt(FCB_HOST, "Classes_3", 113 << BITLEN_2DA_RANGE | 134);
    SetLocalInt(FCB_HOST, "Classes_4", 149 << BITLEN_2DA_RANGE | 181);

    SetLocalInt(FCB_HOST, "racialtypes_0", 253); //CEP+PRC 253
    SetLocalInt(FCB_HOST, "skills_0", 26);
    SetLocalInt(FCB_HOST, "Skills_0", 26);

    SetLocalInt(FCB_HOST, "disease_0", 16);

    SetLocalInt(FCB_HOST, "poison_0", 44); //PRC 114
    SetLocalInt(FCB_HOST, "poison_1", 100 << BITLEN_2DA_RANGE | 114);

    SetLocalInt(FCB_HOST, "gender_0", 4);

    //--------------------------------------/
    //--------------------------------------/
    menu_main();
    chcnt_main();

    DelayCommand(0.0, iprp_main(0, 20));
    DelayCommand(0.0, iprp_main(21, 50));
    DelayCommand(0.0, iprp_main(51, 84));
    }

//==========================================/
//main/sub menues
//==========================================/

//==========================================/
void menu_main()
//==========================================/
    {
        {
        int file_type_iprp = create_main_file_type_link(MAIN_FOLDER_INDEX_IPRP) | SCRIPT_INDEX_DISP_IPRP;
        create_main_file_int(MAIN_FOLDER_INDEX_MAINMENU, UI_TXT_IPRP_ENTER, file_type_iprp);
        }

        {
        int file_type_iprpr = create_main_file_type_link(MAIN_FOLDER_INDEX_IPRPR) | SCRIPT_INDEX_DISP_IPRP;
        create_main_file_int(MAIN_FOLDER_INDEX_MAINMENU, UI_TXT_IPRPR_ENTER, file_type_iprpr);
        }

        {
        int file_type = create_main_file_type_link(MAIN_FOLDER_INDEX_CHARGE) | SCRIPT_INDEX_DISP_HEADER;
        create_main_file_int(MAIN_FOLDER_INDEX_MAINMENU, UI_TXT_CHARGE_ENTER + UI_TXT_INDENT, file_type);
        }

        //--------------------------------------/
        {
        int file_type = create_main_file_type_link(MAIN_FOLDER_INDEX_DUPE) | SCRIPT_INDEX_DUPE_SEL_0;
        create_main_file_int(MAIN_FOLDER_INDEX_MAINMENU, UI_TXT_DUPE_ENTER, file_type);
        }

        {
        int file_type = create_main_file_type_link(MAIN_FOLDER_INDEX_SETNAME) | SCRIPT_INDEX_LISTEN_SETNAME;
        create_main_file_int(MAIN_FOLDER_INDEX_MAINMENU, UI_TXT_SETNAME_ENTER, file_type);
        }

        {
        int file_type = create_main_file_type_link(MAIN_FOLDER_INDEX_COLORGUIDE) | SCRIPT_INDEX_COLORGUIDE;
        create_main_file_int(MAIN_FOLDER_INDEX_MAINMENU, UI_TXT_COLORGUIDE_ENTER, file_type);
        }

        set_main_folder_index_start(MAIN_FOLDER_RESERVE_INDEX);

        //--------------------------------------/
        //--------------------------------------/
        {
        create_main_file_int(MAIN_FOLDER_INDEX_IPRP_MENU, UI_TXT_IPRP_MENU_ADD_NORMAL, SCRIPT_INDEX_IPRP_SEL_1);
        }

        {
        create_main_file_int(MAIN_FOLDER_INDEX_IPRPR_MENU, UI_TXT_IPRPR_MENU_REMOVE, SCRIPT_INDEX_IPRPR_SEL_1);
        }

        //--------------------------------------/
        {
        create_main_file_int(MAIN_FOLDER_INDEX_SETNAME, UI_TXT_SETNAME_MENU_ACCEPT, SCRIPT_INDEX_SETNAME_SEL_1);
        create_main_file_int(MAIN_FOLDER_INDEX_SETNAME, UI_TXT_SETNAME_MENU_REENTER, SCRIPT_INDEX_LISTEN_SETNAME);
        create_main_file_int(MAIN_FOLDER_INDEX_SETNAME, UI_TXT_SETNAME_MENU_DEFAULT, SCRIPT_INDEX_SETNAME_DEFAULT);
        }

        {
        create_main_file_int(MAIN_FOLDER_INDEX_CHARGE_MENU, UI_TXT_CHARGE_MENU_SET, SCRIPT_INDEX_CHARGE_SEL_1);
        }

        {
        create_main_file_int(MAIN_FOLDER_INDEX_DUPE, UI_TXT_DUPE_MENU_ACCEPT, SCRIPT_INDEX_DUPE_SEL_1);
        }
    }

//==========================================/
//item properties
//==========================================/

//==========================================/
void iprp_main(int iprp_main_row, int iprp_main_endrow)
//==========================================/
    {
    int folder_index = MAIN_FOLDER_INDEX_IPRP;
    int file_type_menu = create_main_file_type_link(SCRIPT_INDEX_IPRP_SEL_0 | MAIN_FOLDER_INDEX_IPRP_MENU);

    while(iprp_main_row <= iprp_main_endrow)
        {
        string subtype_strref = Get2DAString("itempropdef", "Name", iprp_main_row);
        string subtype_resref = Get2DAString("itempropdef", "SubTypeResRef", iprp_main_row);

        string costtable_resref = "";
        string costtable_row = Get2DAString("itempropdef", "CostTableResRef", iprp_main_row);
        if(costtable_row != "" && costtable_row != "0")
            {
            costtable_resref = Get2DAString("IPRP_COSTTABLE", "Name", StringToInt(costtable_row));
            }

        string paramtable_resref = "";
        string paramtable_row = Get2DAString("itempropdef", "Param1ResRef", iprp_main_row);
        if(paramtable_row != "")
            {
            paramtable_resref = Get2DAString("IPRP_PARAMTABLE", "TableResRef", StringToInt(paramtable_row));
            }

        int folder_target = 0;
        if(subtype_resref != "")
            {
            folder_target = iprp_subtype_file(subtype_resref, costtable_resref, paramtable_resref, file_type_menu);
            }
        else if(costtable_resref != "" || paramtable_resref != "")
            {
            folder_target = iprp_cost_param1_file(costtable_resref, paramtable_resref, file_type_menu);
            }

        int file_type = create_main_file_type_link(folder_target, file_type_menu);
        create_main_file_int(folder_index, GetStringByStrRef(StringToInt(subtype_strref)), file_type, iprp_main_row);

        iprp_main_row++;
        }
    }

//==========================================/
int iprp_subtype_file(string subtype_resref, string costtable_resref, string paramtable_resref, int file_type_menu)
//==========================================/
    {
    int folder_index = GetLocalInt(FCB_HOST, MAIN_FOLDER_INDEX_LINK_ + subtype_resref + costtable_resref + paramtable_resref);
    if(folder_index < 1)
        {
        folder_index = create_main_folder(subtype_resref + costtable_resref + paramtable_resref);

        int folder_target = iprp_cost_param1_file(costtable_resref, paramtable_resref, file_type_menu);
        //----------------------------------------//
        int block = 0;
        int subtype_range = GetLocalInt(FCB_HOST, subtype_resref + UI_TXT_UNDERSCORE + "0");

        while(subtype_range > 0)
            {
            int subtype_endrow = FILTER_R1 & subtype_range;
            int subtype_row = subtype_range >>> BITLEN_2DA_RANGE;

            while(subtype_row <= subtype_endrow)
                {
                string subtype_strref = Get2DAString(subtype_resref, "Name", subtype_row);
                if(subtype_strref != "")
                    {
                    int file_type = 0;
                    //param1 override
                    //----------------------------------------//
                    string paramtable_row = Get2DAString(subtype_resref, "Param1ResRef", subtype_row);
                    if(paramtable_row != "")
                        {
                        string paramtable_resref_override = Get2DAString("IPRP_PARAMTABLE", "TableResRef", StringToInt(paramtable_row));
                        int folder_target_override = iprp_cost_param1_file(costtable_resref, paramtable_resref_override, file_type_menu);

                        file_type = create_main_file_type_link(folder_target_override, file_type_menu);
                        }
                    //----------------------------------------//
                    else
                        {
                        file_type = create_main_file_type_link(folder_target, file_type_menu);
                        }

                    create_main_file_int(folder_index, GetStringByStrRef(StringToInt(subtype_strref)), file_type, subtype_row);
                    }
                subtype_row++;
                }

            block++;
            subtype_range = GetLocalInt(FCB_HOST, subtype_resref + UI_TXT_UNDERSCORE + IntToString(block));
            }
        }

    return folder_index;
    }

//==========================================/
int iprp_cost_param1_file(string costtable_resref, string paramtable_resref, int file_type_menu)
//==========================================/
    {
    int folder_index = GetLocalInt(FCB_HOST, MAIN_FOLDER_INDEX_LINK_ + costtable_resref + paramtable_resref);
    if(folder_index < 1)
        {
        int folder_index_cost = 0;
        int folder_index_param1 = 0;

        //----------------------------------------//
        if(paramtable_resref != "")
            {
            folder_index_param1 = GetLocalInt(FCB_HOST, MAIN_FOLDER_INDEX_LINK_ + paramtable_resref);

            if(folder_index_param1 < 1)
                {
                folder_index_param1 = create_main_folder(paramtable_resref);

                int block = 0;
                int param1_range = GetLocalInt(FCB_HOST, paramtable_resref + UI_TXT_UNDERSCORE + "0");

                while(param1_range > 0)
                    {
                    int param1_endrow = FILTER_R1 & param1_range;
                    int param1_row = param1_range >>> BITLEN_2DA_RANGE;

                    while(param1_row <= param1_endrow)
                        {
                        string param1_strref = Get2DAString(paramtable_resref , "Name", param1_row);
                        if(param1_strref != "")
                            {
                            create_main_file_int(folder_index_param1, GetStringByStrRef(StringToInt(param1_strref)), file_type_menu, param1_row);
                            }
                        param1_row++;
                        }

                    block++;
                    param1_range = GetLocalInt(FCB_HOST, paramtable_resref + UI_TXT_UNDERSCORE + IntToString(block));
                    }
                }

            folder_index = folder_index_param1;
            }

        //----------------------------------------//
        if(costtable_resref != "")
            {
            folder_index_cost = create_main_folder(costtable_resref + paramtable_resref);

            int block = 0;
            int cost_range = GetLocalInt(FCB_HOST, costtable_resref + UI_TXT_UNDERSCORE + "0");

            while(cost_range > 0)
                {
                int cost_endrow = FILTER_R1 & cost_range;
                int cost_row = cost_range >>> BITLEN_2DA_RANGE;

                while(cost_row <= cost_endrow)
                    {
                    string costtable_strref = Get2DAString(costtable_resref, "Name", cost_row);
                    if(costtable_strref != "")
                        {
                        int file_type = create_main_file_type_link(folder_index_param1, file_type_menu);
                        create_main_file_int(folder_index_cost, GetStringByStrRef(StringToInt(costtable_strref)), file_type, cost_row);
                        }
                    cost_row++;
                    }

                block++;
                cost_range = GetLocalInt(FCB_HOST, costtable_resref + UI_TXT_UNDERSCORE + IntToString(block));
                }

            folder_index = folder_index_cost;
            }
        }

    return folder_index;
    }

//==========================================/
//charge count
//==========================================/

//==========================================/
void chcnt_main()
//==========================================/
    {
    int file_type_menu = create_main_file_type_link(SCRIPT_INDEX_CHARGE_SEL_0 | MAIN_FOLDER_INDEX_CHARGE_MENU);

    //make folders
    //----------------------------------------//
    int n = 1;
    while(n <= ITEM_CHARGE_CNT_MAX)
        {
        create_main_file_int(MAIN_FOLDER_INDEX_CHARGE, IntToString(n) + UI_TXT_UNIT_CHARGE, file_type_menu, n);

        n++;
        }
    }
