//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AdvGrid"
#pragma link "BaseGrid"
#pragma resource "*.dfm"
TForm1 *Form1;
int rmdnum, count;
AnsiString  itemlist[6][305];
void loadItems();

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    loadItems();
}
//---------------------------------------------------------------------------

void loadItems()
{
    FILE *f;
    AnsiString temp;
    char c;
    f = fopen("items.txt","rb");
    if (f == NULL) {
        ShowMessage("File not found");
        return;
    }
    c = fgetc(f);
    while (c != EOF) {
        int itype,iindex;
        AnsiString temp="";
        itype = StrToInt(c); // Item Type
        c = fgetc(f); // :
        c = fgetc(f); // Item index
        while ((c != ' ') && (c != ':')) {
            temp += c;
            c = fgetc(f); // Item index
        }
        iindex = StrToInt(temp);
        c = fgetc(f); // Item name
        temp = "";
        while (c != 13) { // CR
            temp += c;
            c = fgetc(f); // Item name
        }
        itemlist[itype-1][iindex] = temp;
        c = fgetc(f); // LF
        c = fgetc(f); // next item or EOF
    }
    fclose(f);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    if (OpenDialog1->Execute()) {
        Screen->Cursor = crHourGlass;
        FILE *f;
        char c;
        int iTemp;
        AnsiString Value;

        f = fopen(OpenDialog1->FileName.c_str(),"rb");
        if (f == NULL) {
            ShowMessage("Error Loading File");
            Screen->Cursor = crDefault;
            return;
        }
        iTemp = fgetc(f);
        Value = "";
        for (int i=0;i<iTemp;i++) {
            c = fgetc(f);
            Value = Value + AnsiString(c);
        }
        Memo1->Lines->Clear();
        Memo1->Lines->Add(Value);
        rmdnum = fgetc(f);
        rmdnum += fgetc(f) << 8;
        rmdnum += fgetc(f) << 16;
        rmdnum += fgetc(f) << 24;
        Memo1->Lines->Add("File Num: " + IntToStr(rmdnum));

        count = fgetc(f);
        count += fgetc(f) << 8;
        count += fgetc(f) << 16;
        count += fgetc(f) << 24;
        Memo1->Lines->Add("Count: " + IntToStr(count));

        AdvStringGrid1->RowCount = count+1;
        AdvStringGrid1->Cells[0][0]  = "Type";
        AdvStringGrid1->Cells[1][0]  = "";
        AdvStringGrid1->Cells[2][0]  = "Name";
        //Pull name from item list for english
        AdvStringGrid1->Cells[3][0]  = "Description";
        AdvStringGrid1->Cells[4][0]  = "HP";
        AdvStringGrid1->Cells[5][0]  = "MP";
        AdvStringGrid1->Cells[6][0]  = "Att";
        AdvStringGrid1->Cells[7][0]  = "Def";
        AdvStringGrid1->Cells[8][0]  = "Str";
        AdvStringGrid1->Cells[9][0]  = "Spr";
        AdvStringGrid1->Cells[10][0] = "Dex";
        AdvStringGrid1->Cells[11][0] = "Pow";
        AdvStringGrid1->Cells[12][0] = "Level";
        AdvStringGrid1->Cells[13][0] = "";
        AdvStringGrid1->Cells[14][0] = "";
        AdvStringGrid1->Cells[15][0] = "";
        AdvStringGrid1->Cells[16][0] = "";
        AdvStringGrid1->Cells[17][0] = "";
        AdvStringGrid1->Cells[18][0] = "Wearable by";
        //Sword, Gun,Bow,Wand
        //0 = everyone
        //11111111
        //10000000  = Kit
        //01000000  = Cannon
        //00100000  = Jarrexx
        //00010000  =
        //00001000  =
        //00000100  =
        //00000010  = -\
        //              --> Azlar/Philar
        //00000001  = -/
        AdvStringGrid1->Cells[19][0] = "";
        AdvStringGrid1->Cells[20][0] = "Weight";
        AdvStringGrid1->Cells[21][0] = "Item slot";
        //300 = rings
        //100 = 2 handed show in weapon and shield
        // 14 = consumable / disposable(i.e just delete)
        //        (food/boosters/microfilm/potions/flowers/dies/etc...)
        //  8 = Quest item / Belts
        //  7 = 1 handed weapon
        //  6 = Gloves
        //  5 = Shield
        //  4 = Boots
        //  3 = Pants
        //  2 = Armor
        //  1 = necklace
        //  0 = Hat
        AdvStringGrid1->Cells[22][0] = "";
        AdvStringGrid1->Cells[23][0] = "";
        AdvStringGrid1->Cells[24][0] = "";
        AdvStringGrid1->Cells[25][0] = "";
        AdvStringGrid1->Cells[26][0] = "Price";

        for (int item=1; item<count+1; item++) {
            signed int iTemp=0;
            //type
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[0][item] = IntToStr(iTemp);
            //index??
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[1][item] = IntToStr(iTemp);
            //name size
            iTemp = fgetc(f);
            //name
            Value = "";
            for (int i=0;i<iTemp;i++) {
                c = fgetc(f);
                Value = Value + AnsiString(c);
            }
//            AdvStringGrid1->Cells[2][item] = Value;
            AdvStringGrid1->Cells[2][item] = itemlist[rmdnum-1][item-1];
            //description size
            iTemp = fgetc(f);
            //descri[tion
            Value = "";
            for (int i=0;i<iTemp;i++) {
                c = fgetc(f);
                Value = Value + AnsiString(c);
            }
            AdvStringGrid1->Cells[3][item] = Value;
            for (int i=0;i<14;i++) {
                //??
                iTemp = fgetc(f);
                iTemp += fgetc(f) << 8;
                iTemp += fgetc(f) << 16;
                iTemp += fgetc(f) << 24;
                AdvStringGrid1->Cells[4+i][item] = IntToStr(iTemp);
            }
            //??
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[18][item] = IntToStr(iTemp);

            iTemp = fgetc(f);
            AdvStringGrid1->Cells[19][item] = IntToStr(iTemp);
            //??
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[20][item] = IntToStr(iTemp);

            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[21][item] = IntToStr(iTemp);

            iTemp = fgetc(f);
            AdvStringGrid1->Cells[22][item] = IntToStr(iTemp);
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[23][item] = IntToStr(iTemp);
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[24][item] = IntToStr(iTemp);
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[25][item] = IntToStr(iTemp);

            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[26][item] = IntToStr(iTemp);
        }
        fclose(f);
        Screen->Cursor = crDefault;
    }
}
//---------------------------------------------------------------------------
