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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int filenum=0;

struct itemKind {
    int type;
    int index;
    int count;
};

struct rsmItem {
    AnsiString Name;
    int index;
    int left,  top;
    int right, bottum;
    int itemcount;
    itemKind *items;
    int patern;
    int quantity;
    int respawn;
};

rsmItem map[100];
int items;

AnsiString  itemlist[6][304];
AnsiString  maplist[177];
AnsiString  monlist[104];
int mopinfo[991];

void loadItems();
void loadMaps();
void loadMons();

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

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-1] = temp;
        c = fgetc(f); // LF
        c = fgetc(f); // next item or EOF
    }
    fclose(f);
}
//---------------------------------------------------------------------------

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

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

    f = fopen("mopinfo.txt","rb");
    if (f == NULL) {
        ShowMessage("File not found");
        return;
    }
    c = fgetc(f);
    while (c != EOF) {
        int itype;
        AnsiString temp="";
        while ((c != ' ') && (c != 9)) {
            temp += c;
            c = fgetc(f); // Item index
        }
        itype = StrToInt(temp);
        while ((c == ' ') || (c == 9)) {
            c = fgetc(f);
        }
        temp = "";
        while ((c != ' ') && (c != 9)) {
            temp += c;
            c = fgetc(f);
        }
        mopinfo[itype] = StrToInt(temp);
        while (c != 13) { // CR
            c = fgetc(f); // Item name
        }
        c = fgetc(f); // LF
        c = fgetc(f); // next item or EOF
    }
    fclose(f);
}
//---------------------------------------------------------------------------

void ShowMap(int mapnumber)
{
    Form1->Label1->Caption = maplist[mapnumber];
}
//---------------------------------------------------------------------------

AnsiString parseMon(int type)
{
    AnsiString temp;
    temp = monlist[mopinfo[type]];
    if ((temp != "") && (Form1->ComboBox1->Items->IndexOf(temp) == -1)) {
        Form1->ComboBox1->Items->Add(temp);
    }
    return temp;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    FILE *f;
    char Value[100];
    AnsiString temp;
    char c;
    sprintf(Value,"C:\\projects\\bin\\Data\\Mop\\mop%5d.rsm",filenum);
    for (unsigned int i = 0; i < strlen(Value); i++) {
        if (Value[i] == ' ') {
            Value[i] = '0';
        }
    }
    f = fopen(Value,"rb");
    if (f == NULL) {
        if (filenum < 500) {
            filenum++;
            Button1Click(Sender);
        } else {
            ShowMessage("File not found");
        }
        return;
    }
    //file logic
    Screen->Cursor = crHourGlass;
    c = fgetc(f);
    int icomments = 0;
    int iempty    = 0;
    items = 0;
    ComboBox1->Items->Clear();
    ComboBox1->Text = "";
    ComboBox1->Items->Add("Unknown");
    ComboBox1->Items->Add("ALL");
    try {
    while (c != EOF) {
        // empty line
        if (c == 13) {
            iempty++;
            c = fgetc(f);
        } else
        // commented lines
        if (c == ';') {
            temp += c;
            icomments++;
            while ((c != 10) && (c != EOF)) {
                c = fgetc(f);
                temp += c;
            }
        } else {
        //line to parse
            byte temp2[20];
            temp = "";
            int i=0;
            while ((c != ' ') && (c != 9)) {
                temp2[i++] = c;
                temp += c;
                c = fgetc(f);
            }
            //Name
            map[items].Name = temp;

            //index
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            map[items].index = StrToInt(temp);
            map[items].Name = parseMon(map[items].index);
            //left
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            map[items].left = StrToInt(temp);

            //top
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            map[items].top = StrToInt(temp);

            //right
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            map[items].right = StrToInt(temp);

            //bottum
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            map[items].bottum = StrToInt(temp);

            //itemcount
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            map[items].itemcount = StrToInt(temp);
            if (map[items].itemcount != 0) {
                map[items].items = new itemKind[map[items].itemcount];
                for (int i=0;i< map[items].itemcount; i++) {
                    //item type
                    while ((c == ' ') || (c == 9)) {
                        c = fgetc(f);
                    }
                    temp = "";
                    while ((c != ' ') && (c != 9)) {
                        temp += c;
                        c = fgetc(f);
                    }
                    map[items].items[i].type = StrToInt(temp);
                    //item index
                    while ((c == ' ') || (c == 9)) {
                        c = fgetc(f);
                    }
                    temp = "";
                    while ((c != ' ') && (c != 9)) {
                        temp += c;
                        c = fgetc(f);
                    }
                    map[items].items[i].index = StrToInt(temp);
                    //item count
                    while ((c == ' ') || (c == 9)) {
                        c = fgetc(f);
                    }
                    temp = "";
                    while ((c != ' ') && (c != 9)) {
                        temp += c;
                        c = fgetc(f);
                    }
                    map[items].items[i].count = StrToInt(temp);
                }
            }
            //patern
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            map[items].patern = StrToInt(temp);

            //quantity
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            map[items].quantity = StrToInt(temp);

            //respawn
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while (((c != 13) && (c != EOF)) &&
                   ((c != ' ') && (c != 9))) {
                temp += c;
                c = fgetc(f);
            }
            map[items].respawn = StrToInt(temp);
            c = fgetc(f);
            items++;
        }
        c = fgetc(f);
    }
    } catch (...) {
//        ShowMessage("Error filenum:" + IntToStr(filenum));
    }
    //close file
    Screen->Cursor = crDefault;
    RichEdit1->Lines->Clear();
    ShowMap(filenum);
    RichEdit1->Lines->Add("           Name      Index       left        top      right     bottum    itemcnt     patern   quantity    respawn");
    RichEdit1->Lines->Add("---------------   --------   --------   --------   --------   --------   --------   --------   --------   --------");
    for (int i=0;i<items;i++) {
        char temp[200];
        sprintf(temp,"%15s %10d %10d %10d %10d %10d %10d %10d %10d %10d",
                map[i].Name, map[i].index, map[i].left,
                map[i].top,  map[i].right, map[i].bottum,
                map[i].itemcount, map[i].patern, map[i].quantity,
                map[i].respawn);
        if (RichEdit1->Lines->Count%30 == 0) {
            RichEdit1->Lines->Add("");
            RichEdit1->Lines->Add("           Name      Index       left        top      right     bottum    itemcnt     patern   quantity    respawn");
            RichEdit1->Lines->Add("---------------   --------   --------   --------   --------   --------   --------   --------   --------   --------");
        }
        RichEdit1->Lines->Add(temp);
//        for (int j=0;j<map[i].itemcount;j++) {
//            Line = "\t\t" + IntToStr(map[i].items[j].type);
//            Line = Line + "\t" + IntToStr(map[i].items[j].index);
//            Line = Line + "\t" + IntToStr(map[i].items[j].count);
//            RichEdit1->Lines->Add(Line);
//        }
    }
    RichEdit1->Lines->Add("File:" + IntToStr(filenum) + "   Comments: " + IntToStr(icomments) + "   Empty:" + IntToStr(iempty) + "   Items:" + IntToStr(items));
    filenum++;
    fclose(f);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
    RichEdit1->Lines->Clear();
    RichEdit1->Lines->Add("           Name      Index       left        top      right     bottum    itemcnt     patern   quantity    respawn");
    RichEdit1->Lines->Add("---------------   --------   --------   --------   --------   --------   --------   --------   --------   --------");
    for (int i=0;i<items;i++) {
        if (ComboBox1->Text == "Unknown") {
            if (ComboBox1->Items->IndexOf(map[i].Name) == -1) {
                char temp[200];
                sprintf(temp,"%15s %10d %10d %10d %10d %10d %10d %10d %10d %10d",
                        map[i].Name, map[i].index, map[i].left,
                        map[i].top,  map[i].right, map[i].bottum,
                        map[i].itemcount, map[i].patern, map[i].quantity,
                        map[i].respawn);
                RichEdit1->Lines->Add(temp);
                if (map[i].itemcount != 0) {
                    RichEdit1->Lines->Add("                                                                            Items");
                    for (int j=0;j<map[i].itemcount;j++) {
                        sprintf(temp,"%81s %10d",
                                itemlist[map[i].items[j].type-1][map[i].items[j].index-1],
                                map[i].items[j].count);
                        RichEdit1->Lines->Add(temp);
                    }
                    RichEdit1->Lines->Add("");
                }
            }
        } else
        if (ComboBox1->Text == "ALL") {
            char temp[200];
            sprintf(temp,"%15s %10d %10d %10d %10d %10d %10d %10d %10d %10d",
                    map[i].Name, map[i].index, map[i].left,
                    map[i].top,  map[i].right, map[i].bottum,
                    map[i].itemcount, map[i].patern, map[i].quantity,
                    map[i].respawn);
            RichEdit1->Lines->Add(temp);
            if (map[i].itemcount != 0) {
                RichEdit1->Lines->Add("                                                                            Items");
                for (int j=0;j<map[i].itemcount;j++) {
                    sprintf(temp,"%81s %10d",
                            itemlist[map[i].items[j].type-1][map[i].items[j].index-1],
                            map[i].items[j].count);
                    RichEdit1->Lines->Add(temp);
                }
                RichEdit1->Lines->Add("");
            }
        } else
        if (map[i].Name == ComboBox1->Text) {
            char temp[200];
            sprintf(temp,"%15s %10d %10d %10d %10d %10d %10d %10d %10d %10d",
                    map[i].Name, map[i].index, map[i].left,
                    map[i].top,  map[i].right, map[i].bottum,
                    map[i].itemcount, map[i].patern, map[i].quantity,
                    map[i].respawn);
            RichEdit1->Lines->Add(temp);
            if (map[i].itemcount != 0) {
                RichEdit1->Lines->Add("                                                                            Items");
//                RichEdit1->Lines->Add("Items                                                                                   type      index      count");
//                RichEdit1->Lines->Add("                                                                                    --------   --------   --------");
                for (int j=0;j<map[i].itemcount;j++) {
                    sprintf(temp,"%81s %10d",
                            itemlist[map[i].items[j].type-1][map[i].items[j].index-1],
                            map[i].items[j].count);
                    RichEdit1->Lines->Add(temp);
//                    RichEdit1->Lines->Add("\t\t\t" + itemlist[map[i].items[j].type-1][map[i].items[j].index-1] + " " + IntToStr(map[i].items[j].count));
                }
                RichEdit1->Lines->Add("");
            }
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    filenum++;    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    filenum--;
    filenum--;
}
//---------------------------------------------------------------------------

