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

#include <vcl.h>
#pragma hdrstop

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

struct rsmInfo {
    signed int Index, Skin, Los, Wspeed, ATT1Ran, ATT2Ran, Att1int, Att2Int,
        Att1Ani, Att2Ani, Skill1, Skill2, Skill1Int, Skill2Int,
        Str, Spr, Dex, Pow, Exp, Dir1, Dir2, Honor, Fly;
};

rsmInfo info[500];
int items;
int citem=0;

AnsiString  monlist[104];
AnsiString  skilllist[382];
AnsiString  attacklist[95];

void loadInfo();

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

void DispayCurrent(void)
{
    Form1->Edit1->Text = info[citem].Index;
    Form1->ComboBox1->Text = monlist[info[citem].Skin];
    Form1->Edit2->Text = info[citem].Los;
    Form1->Edit3->Text = info[citem].Wspeed;
    Form1->Edit4->Text = info[citem].ATT1Ran;
    Form1->Edit5->Text = info[citem].ATT2Ran;
    Form1->Edit6->Text = info[citem].Att1int;
    Form1->Edit7->Text = info[citem].Att2Int;
    Form1->ComboBox4->Text = attacklist[info[citem].Att1Ani];
    Form1->ComboBox5->Text = attacklist[info[citem].Att2Ani];
    Form1->ComboBox2->Text = skilllist[info[citem].Skill1];
    Form1->ComboBox3->Text = skilllist[info[citem].Skill2];
    Form1->Edit12->Text = info[citem].Skill1Int;
    Form1->Edit13->Text = info[citem].Skill2Int;
    Form1->Edit14->Text = info[citem].Str;
    Form1->Edit8->Text  = info[citem].Spr;
    Form1->Edit9->Text  = info[citem].Dex;
    Form1->Edit10->Text = info[citem].Pow;
    Form1->Edit11->Text = info[citem].Exp;
    Form1->Edit15->Text = info[citem].Dir1;
    Form1->Edit16->Text = info[citem].Dir2;
    Form1->Edit17->Text = info[citem].Honor;
    Form1->Edit18->Text = info[citem].Fly;

    Form1->Label24->Caption = "Item " + IntToStr(citem) + " of " + IntToStr(items);
}
//---------------------------------------------------------------------------

void loadInfo()
{
//Monsters
    FILE *f;
    AnsiString temp;
    char c;
    f = fopen("mons.txt","rb");
    if (f == NULL) {
        ShowMessage("File not found");
        return;
    }
    c = fgetc(f);
    Form1->ComboBox1->Items->BeginUpdate();
    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;
        Form1->ComboBox1->Items->Add(temp);
        c = fgetc(f); // LF
        c = fgetc(f); // next item or EOF
    }
    fclose(f);
    Form1->ComboBox1->Items->EndUpdate();

//Skills
    f = fopen("skills.txt","rb");
    if (f == NULL) {
        ShowMessage("File not found");
        return;
    }
    c = fgetc(f);
    Form1->ComboBox2->Items->BeginUpdate();
    Form1->ComboBox3->Items->BeginUpdate();
    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
        }
        skilllist[itype] = temp;
        Form1->ComboBox2->Items->Add(temp);
        Form1->ComboBox3->Items->Add(temp);
        c = fgetc(f); // LF
        c = fgetc(f); // next item or EOF
    }
    fclose(f);
    Form1->ComboBox2->Items->EndUpdate();
    Form1->ComboBox3->Items->EndUpdate();

//attacks
    f = fopen("attacks.txt","rb");
    if (f == NULL) {
        ShowMessage("File not found");
        return;
    }
    c = fgetc(f);
    Form1->ComboBox4->Items->BeginUpdate();
    Form1->ComboBox5->Items->BeginUpdate();
    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
        }
        attacklist[itype] = temp;
        Form1->ComboBox4->Items->Add(temp);
        Form1->ComboBox5->Items->Add(temp);
        c = fgetc(f); // LF
        c = fgetc(f); // next item or EOF
    }
    fclose(f);
    Form1->ComboBox4->Items->EndUpdate();
    Form1->ComboBox5->Items->EndUpdate();
}
//---------------------------------------------------------------------------

void ShowMap(int 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\\mopinfo.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->Text = "";
    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
            temp = "";
            //index
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            info[items].Index = StrToInt(temp);
            //skin
            while ((c == ' ') || (c == 9)) {
                c = fgetc(f);
            }
            temp = "";
            while ((c != ' ') && (c != 9)) {
                temp += c;
                c = fgetc(f);
            }
            info[items].Skin = StrToInt(temp);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            //rest
            while ((c != 13) && (c != EOF)) {
                c = fgetc(f);
            }
            c = fgetc(f);
            items++;
        }
        c = fgetc(f);
    }
    } catch (...) {
//        ShowMessage("Error filenum:" + IntToStr(filenum));
    }
    //close file
    Screen->Cursor = crDefault;
    fclose(f);
    Button1->Enabled = false;
    items--;
    DispayCurrent();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    if (citem < items) {
        citem++;
        DispayCurrent();
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    if (citem > 0) {
        citem--;
        DispayCurrent();
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit1Enter(TObject *Sender)
{
    TEdit *tmp;
    tmp = (TEdit*)Sender;
    StatusBar1->SimpleText = tmp->Hint;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox1Enter(TObject *Sender)
{
    TComboBox *tmp;
    tmp = (TComboBox*)Sender;
    StatusBar1->SimpleText = tmp->Hint;
}
//---------------------------------------------------------------------------

