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

#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;
int RCol=0,RRow=0;

AnsiString  attacklist[105];
AnsiString  monlist[104];

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

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    if (OpenDialog1->Execute()) {
        Screen->Cursor = crHourGlass;
        FILE *f;
        char c;
        unsigned 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<(signed)iTemp;i++) {
            c = fgetc(f);
            Value = Value + AnsiString(c);
        }
        Memo1->Lines->Clear();
        Memo1->Lines->Add(Value);

        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";
        //0,1, or 2
        //0 = Player normal skill
        //1 = Palyer DP skill
        //2 = Monster Skill?
        AdvStringGrid1->Cells[1][0]  = "";
        AdvStringGrid1->Cells[2][0]  = "";
        AdvStringGrid1->Cells[3][0]  = "";
        AdvStringGrid1->Cells[4][0]  = "TargetAnim";
        AdvStringGrid1->Cells[5][0]  = "";
        AdvStringGrid1->Cells[6][0]  = "";
        AdvStringGrid1->Cells[7][0]  = "Honor";
        AdvStringGrid1->Cells[8][0]  = "Level";
        AdvStringGrid1->Cells[9][0]  = "Char";
        AdvStringGrid1->Cells[10][0] = "MP";
        AdvStringGrid1->Cells[11][0] = "Range";
        AdvStringGrid1->Cells[12][0] = "";
        AdvStringGrid1->Cells[13][0] = "";
        AdvStringGrid1->Cells[14][0] = "";
        AdvStringGrid1->Cells[15][0] = "";
        int iRow = 1;
        iTemp=0;
        for (int item=1; item<count+1; item++) {
            //0,1, or 2
            //0 = Player normal skill
            //1 = Palyer DP skill
            //2 = Monster Skill?
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[0][iRow] = IntToStr(iTemp);
            //number of folling bytes
            //0, 1, or 2
            iTemp = fgetc(f);
            Value = "";
            for (int i=0;i<(signed)iTemp;i++) {
                c = fgetc(f);
                Value = Value + AnsiString(c);
            }
            AdvStringGrid1->Cells[1][iRow] = Value;

            //number of folling bytes
            iTemp = fgetc(f);
            //0, or 1
            Value = "";
            for (int i=0;i<(signed)iTemp;i++) {
                c = fgetc(f);
                Value = Value + AnsiString(c);
            }
            AdvStringGrid1->Cells[2][iRow] = Value;
            //
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[3][iRow] = IntToStr(iTemp);
            //Attack Animation
            iTemp = fgetc(f);
            if (AdvStringGrid1->Cells[1][iRow] != "") {
                AdvStringGrid1->Cells[4][iRow] = attacklist[iTemp];
            } else {
                AdvStringGrid1->Cells[4][iRow] = IntToStr(iTemp);
            }

            iTemp = fgetc(f);
            AdvStringGrid1->Cells[5][iRow] = IntToStr(iTemp);

            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[6][iRow] = IntToStr(iTemp);
            //Honor
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[7][iRow] = IntToStr(iTemp);
            //Level
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[8][iRow] = IntToStr(iTemp);
            //Char class
            iTemp = fgetc(f);
            if (AdvStringGrid1->Cells[1][iRow] != "") {
                AdvStringGrid1->Cells[9][iRow] = monlist[iTemp];
            } else {
                AdvStringGrid1->Cells[9][iRow] = IntToStr(iTemp);
            }
            //MP Usage
            //over 1000000000 = Percentage
            // 1000000007 = 7 %
            // 1000000050 = 50 %
            // 1000000100 = 100 %
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[10][iRow] = IntToStr(iTemp);
            //Range 0-15 + Something
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            AdvStringGrid1->Cells[11][iRow] = IntToStr(iTemp);
            
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[12][iRow] = IntToStr(iTemp);
            
            iTemp = fgetc(f);
            AdvStringGrid1->Cells[13][iRow] = IntToStr(iTemp);
            
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[14][iRow] = IntToStr(iTemp);
            iTemp = fgetc(f);
            iTemp += fgetc(f) << 8;
            iTemp += fgetc(f) << 16;
            iTemp += fgetc(f) << 24;
            AdvStringGrid1->Cells[15][iRow] = IntToStr(iTemp);
            //only display filled items
//            if (AdvStringGrid1->Cells[1][iRow] != "") {
                iRow++;
//            }
        }
        fclose(f);
        Memo1->Lines->Add("Items: " + IntToStr(iRow-1));
        AdvStringGrid1->RowCount = iRow;
        Screen->Cursor = crDefault;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    if (SaveDialog1->Execute()) {
        Screen->Cursor = crHourGlass;
        FILE *f;
        char c;
        unsigned int iTemp;
        AnsiString Value;

        f = fopen(SaveDialog1->FileName.c_str(),"wb");
        if (f == NULL) {
            ShowMessage("Error Loading File");
            Screen->Cursor = crDefault;
            return;
        }
        iTemp = 29;
        fwrite(&iTemp, 1, 1, f);
        char temp[30];
        sprintf(temp,"RedMoon Skill Info File V 1.0");
        fwrite(&temp, 29, 1, f);
        fwrite(&count, 4, 1, f);
        iTemp=0;
        for (int item=1; item<count+1; item++) {
            iTemp = StrToInt(AdvStringGrid1->Cells[0][item]);
            fwrite(&iTemp, 1, 1, f);
            Value = AdvStringGrid1->Cells[1][item];
            iTemp = strlen(Value.c_str());
            fwrite(&iTemp, 1, 1, f);
            //0, 1, or 2
            if (iTemp != 0) {
                char temp[10];
                sprintf(temp, Value.c_str());
                fwrite(&temp, iTemp, 1, f);
            }
            Value = AdvStringGrid1->Cells[2][item];
            iTemp = strlen(Value.c_str());
            fwrite(&iTemp, 1, 1, f);
            //0,1
            if (iTemp != 0) {
                char temp[10];
                sprintf(temp, Value.c_str());
                fwrite(&temp, iTemp, 1, f);
            }
            iTemp = StrToInt(AdvStringGrid1->Cells[3][item]);
            fwrite(&iTemp, 4, 1, f);

            if (AdvStringGrid1->Cells[1][item] != "") {
                Value = AdvStringGrid1->Cells[4][item];
                for (int i=0;i<105;i++) {
                    if (attacklist[i] == Value) {
                        fwrite(&i, 1, 1, f);
                    }
                }
            } else {
                iTemp = StrToInt(AdvStringGrid1->Cells[4][item]);
                fwrite(&iTemp, 1, 1, f);
            }
            iTemp = StrToInt(AdvStringGrid1->Cells[5][item]);
            fwrite(&iTemp, 1, 1, f);

            iTemp = StrToInt(AdvStringGrid1->Cells[6][item]);
            fwrite(&iTemp, 4, 1, f);

            iTemp = StrToInt(AdvStringGrid1->Cells[7][item]);
            fwrite(&iTemp, 4, 1, f);

            iTemp = StrToInt(AdvStringGrid1->Cells[8][item]);
            fwrite(&iTemp, 4, 1, f);

            if (AdvStringGrid1->Cells[1][item] != "") {
                Value = AdvStringGrid1->Cells[9][item];
                for (int i=0;i<104;i++) {
                    if (monlist[i] == Value) {
                        fwrite(&i, 1, 1, f);
                    }
                }
            } else {
                iTemp = StrToInt(AdvStringGrid1->Cells[9][item]);
                fwrite(&iTemp, 1, 1, f);
            }

            iTemp = StrToInt(AdvStringGrid1->Cells[10][item]);
            fwrite(&iTemp, 4, 1, f);

            iTemp = StrToInt(AdvStringGrid1->Cells[11][item]);
            fwrite(&iTemp, 2, 1, f);

            iTemp = StrToInt(AdvStringGrid1->Cells[12][item]);
            fwrite(&iTemp, 4, 1, f);

            iTemp = StrToInt(AdvStringGrid1->Cells[13][item]);
            fwrite(&iTemp, 1, 1, f);

            iTemp = StrToInt(AdvStringGrid1->Cells[14][item]);
            fwrite(&iTemp, 4, 1, f);

            iTemp = StrToInt(AdvStringGrid1->Cells[15][item]);
            fwrite(&iTemp, 4, 1, f);
        }
        fclose(f);
        Screen->Cursor = crDefault;
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::AdvStringGrid1GetEditText(TObject *Sender,
      int ACol, int ARow, AnsiString &Value)
{
    TRect location = AdvStringGrid1->CellRect(ACol,ARow);
    RRow = ARow;
    RCol = ACol;
    ComboBox1->Hide();
    if (RCol == 4) {
        ComboBox1->Left = location.Left;
        ComboBox1->Top  = location.Top + AdvStringGrid1->Top;
        ComboBox1->Show();
        ComboBox1->Text        = AdvStringGrid1->Cells[ACol][ARow];
        Form1->ActiveControl   = ComboBox1;
        ComboBox1->DroppedDown = true;
    } else
    if (RCol == 9) {
        ComboBox2->Left = location.Left;
        ComboBox2->Top  = location.Top + AdvStringGrid1->Top;
        ComboBox2->Show();
        ComboBox2->Text        = AdvStringGrid1->Cells[ACol][ARow];
        Form1->ActiveControl   = ComboBox2;
        ComboBox2->DroppedDown = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox1Exit(TObject *Sender)
{
    TComboBox *cbTemp;
    cbTemp = (TComboBox*)Sender;
    AnsiString temp = "";
    temp = cbTemp->Text;
    AdvStringGrid1->Cells[RCol][RRow] = temp;
    cbTemp->Hide();
    Form1->ActiveControl = AdvStringGrid1;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox1KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
    if (Key == '\r') {
        ComboBox1Exit(ComboBox1);
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormShow(TObject *Sender)
{
    for (int i=0;i<105;i++) {
        attacklist[i] = IntToStr(i);
    }
    for (int i=0;i<104;i++) {
        monlist[i] = IntToStr(i);
    }
    FILE *f;
    AnsiString temp;
    char c;
//Monsters
    f = fopen("mons.txt","rb");
    if (f == NULL) {
        ShowMessage("File not found");
        return;
    }
    c = fgetc(f);
    ComboBox2->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] = AnsiString(temp).Trim();
        ComboBox2->Items->Add(monlist[itype]);
        c = fgetc(f); // LF
        c = fgetc(f); // next item or EOF
    }
    fclose(f);
    ComboBox2->Items->EndUpdate();
//attacks
    f = fopen("attacks.txt","rb");
    if (f == NULL) {
        ShowMessage("File not found");
        return;
    }
    ComboBox2->Items->EndUpdate();
    c = fgetc(f);
    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
        }
        attacklist[itype] = AnsiString(temp).Trim();
        ComboBox1->Items->Add(attacklist[itype]);
        c = fgetc(f); // LF
        c = fgetc(f); // next item or EOF
    }
    ComboBox1->Items->EndUpdate();
    fclose(f);    
}
//---------------------------------------------------------------------------


