UnitEvent.cpp
上传用户:garry_shen
上传日期:2015-04-15
资源大小:45647k
文件大小:147k
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include <Filectrl.hpp>
- #include "Unit1.h"
- #include "Unit5.h"
- #include "UnitEvent.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TFormEvent *FormEvent;
- //---------------------------------------------------------------------------
- __fastcall TFormEvent::TFormEvent(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void TFormEvent::LoadNpcEventFile(AnsiString szFilePath)
- {
- // Load NPC Event File
- FILE *pFile;
- AnsiString szStr;
- //char szBuf1[64], szBuf2[64], szBuf3[64], szBuf4[64];
- char szBuf1[512], szBuf2[512], szBuf3[512], szBuf4[512];
- int nBuf1 = 0, nBuf2 = 0, nBuf3 = 0, nBuf4 = 0;
- CEventAsk* pEventAsk = NULL;
- CEventLimit* pEventLimit = NULL;
- CEventRun* pEventRun = NULL;
- if(g_pNpcEvent != NULL)
- {
- delete g_pNpcEvent;
- g_pNpcEvent = NULL;
- }
- g_pNpcEvent = new CNpcEvent;
- // initialize g_pNpcEvent start
- pFile = fopen(szFilePath.c_str(),"r");
- if(pFile != NULL)
- {
- while(fscanf(pFile,"%s",szBuf1)!=EOF) //while [AskItem]
- {
- szStr = szBuf1;
- if(szStr == "[AskEnd]")
- {
- continue;
- }
- else if(szStr == "[AskItem]")
- {
- if(fscanf(pFile, "%s %s %s", szBuf1, szBuf2, szBuf3) == 3)
- {
- nBuf1 = atoi(szBuf2);
- nBuf2 = atoi(szBuf3);
- pEventAsk = new CEventAsk(szBuf1, nBuf1, nBuf2);
- // Get Default EventRun
- while(fscanf(pFile,"%s",szBuf1)!=EOF) //while [Default]
- {
- szStr = szBuf1;
- if(szStr == "[AskEnd]")
- {
- break;
- }
- else if(szStr == "[DefaultEnd]")
- {
- break;
- }
- else if(szStr == "[Default]")
- {
- fscanf(pFile,"%s",szBuf1);
- szStr = szBuf1;
- if(szStr == "[No]")
- {
- fscanf(pFile,"%s",szBuf1);
- if(strcmp(szBuf1, "NULLMSG") == 0)
- {
- strcpy(szBuf1, "");
- }
- pEventAsk->MsgDefaultWhenNo = szBuf1;
- while(fscanf(pFile,"%s",szBuf1)!=EOF) //while [Default Event When No]
- {
- szStr = szBuf1;
- if(szStr == "[NoEnd]")
- {
- break;
- }
- if(szStr == "[EventRun]")
- {
- if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
- {
- pEventRun = new CEventRun;
- nBuf1 = atoi(szBuf1);
- pEventRun->EventRunType = nBuf1;
- pEventRun->EventRunItemName = szBuf2;
- nBuf3 = atoi(szBuf3);
- pEventRun->EventRunValue = nBuf3;
- nBuf4 = atoi(szBuf4);
- pEventRun->EventRunGoodKind = nBuf4;
- }
- (pEventAsk->DefaultEventRunListWhenNo).push_back(*pEventRun);
- delete pEventRun;
- }
- } //while [Default Event When No]
- } //if [Default No]
- fscanf(pFile,"%s",szBuf1);
- szStr = szBuf1;
- if(szStr == "[Yes]")
- {
- fscanf(pFile,"%s",szBuf1);
- if(strcmp(szBuf1, "NULLMSG") == 0)
- {
- strcpy(szBuf1, "");
- }
- pEventAsk->MsgDefaultWhenYes = szBuf1;
- while(fscanf(pFile,"%s",szBuf1)!=EOF) //while [Event when Yes]
- {
- szStr = szBuf1;
- if(szStr == "[YesEnd]")
- {
- break;
- }
- else if(szStr == "[EventRun]")
- {
- if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
- {
- pEventRun = new CEventRun;
- nBuf1 = atoi(szBuf1);
- pEventRun->EventRunType = nBuf1;
- pEventRun->EventRunItemName = szBuf2;
- nBuf3 = atoi(szBuf3);
- pEventRun->EventRunValue = nBuf3;
- nBuf4 = atoi(szBuf4);
- pEventRun->EventRunGoodKind = nBuf4;
- }
- (pEventAsk->DefaultEventRunListWhenYes).push_back(*pEventRun);
- delete pEventRun;
- }
- } //while [Default Event when Yes]
- } // if [Default Yes]
- }
- } //while [Default]
- // Get EventRun After Check Limits
- while(fscanf(pFile,"%s",szBuf1)!=EOF) //while [Limit]
- {
- szStr = szBuf1;
- if(szStr == "[AskEnd]")
- {
- break;
- }
- else if(szStr == "[LimitEnd]")
- {
- continue;
- }
- else if(szStr == "[Limit]")
- {
- if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
- {
- pEventLimit = new CEventLimit;
- nBuf1 = atoi(szBuf1);
- pEventLimit->LimitType = nBuf1;
- pEventLimit->LimitItemName = szBuf2;
- nBuf3 = atoi(szBuf3);
- pEventLimit->LimitValue = nBuf3;
- nBuf4 = atoi(szBuf4);
- pEventLimit->LimitGoodKind = nBuf4;
- fscanf(pFile,"%s",szBuf1);
- szStr = szBuf1;
- if(szStr == "[No]")
- {
- fscanf(pFile,"%s",szBuf1);
- if(strcmp(szBuf1, "NULLMSG") == 0)
- {
- strcpy(szBuf1, "");
- }
- pEventLimit->MsgWhenNo = szBuf1;
- while(fscanf(pFile,"%s",szBuf1)!=EOF) //while [EventRun when No]
- {
- szStr = szBuf1;
- if(szStr == "[NoEnd]")
- {
- break;
- }
- if(szStr == "[EventRun]")
- {
- if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
- {
- pEventRun = new CEventRun;
- nBuf1 = atoi(szBuf1);
- pEventRun->EventRunType = nBuf1;
- pEventRun->EventRunItemName = szBuf2;
- nBuf3 = atoi(szBuf3);
- pEventRun->EventRunValue = nBuf3;
- nBuf4 = atoi(szBuf4);
- pEventRun->EventRunGoodKind = nBuf4;
- }
- (pEventLimit->EventRunListWhenNo).push_back(*pEventRun);
- delete pEventRun;
- }
- } //while [EventRun when No]
- } //if [No]
- fscanf(pFile,"%s",szBuf1);
- szStr = szBuf1;
- if(szStr == "[Yes]")
- {
- fscanf(pFile,"%s",szBuf1);
- if(strcmp(szBuf1, "NULLMSG") == 0)
- {
- strcpy(szBuf1, "");
- }
- pEventLimit->MsgWhenYes = szBuf1;
- while(fscanf(pFile,"%s",szBuf1)!=EOF) //while [EventRun when Yes]
- {
- szStr = szBuf1;
- if(szStr == "[YesEnd]")
- {
- break;
- }
- else if(szStr == "[EventRun]")
- {
- if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
- {
- pEventRun = new CEventRun;
- nBuf1 = atoi(szBuf1);
- pEventRun->EventRunType = nBuf1;
- pEventRun->EventRunItemName = szBuf2;
- nBuf3 = atoi(szBuf3);
- pEventRun->EventRunValue = nBuf3;
- nBuf4 = atoi(szBuf4);
- pEventRun->EventRunGoodKind = nBuf4;
- }
- (pEventLimit->EventRunListWhenYes).push_back(*pEventRun);
- delete pEventRun;
- }
- } //while [EventRun when Yes]
- } // if [Yes]
- }
- (pEventAsk->LimitList).push_back(*pEventLimit);
- delete pEventLimit;
- }
- } //while [Limit]
- }
- (g_pNpcEvent->EventAskList).push_back(*pEventAsk);
- delete pEventAsk;
- }
- } //while [AskItem]
- // initialize g_pNpcEvent end
- fclose(pFile);
- }
- // show All Data
- UpdateEventAsk(1);
- }
- //---------------------------------------------------------------------------
- void TFormEvent::SaveNpcEventFile(AnsiString szFilePath)
- {
- FILE *pFile = NULL;
- //char szTemp[128];
- char szTemp[512];
- list<CEventAsk>::iterator iterEventAsk;
- list<CEventLimit>::iterator iterLimit;
- list<CEventRun>::iterator iterEventRun;
- AnsiString anStr; //ANDY_NEW
- int mapint,strint;
- pFile = fopen(szFilePath.c_str(), "wt");
- if(pFile != NULL)
- {
- if((g_pNpcEvent->EventAskList).empty() != true)
- {
- for(iterEventAsk = (g_pNpcEvent->EventAskList).begin(); iterEventAsk != (g_pNpcEvent->EventAskList).end(); iterEventAsk++)
- {
- fprintf(pFile,"%s %s %d %dnn", "[AskItem]",(iterEventAsk->KeyWord).c_str(), iterEventAsk->AskType, iterEventAsk->LimitChoose);
- // Write Default EventRun
- fprintf(pFile,"t%snn", "[Default]");
- if(strcmp((iterEventAsk->MsgDefaultWhenNo).c_str(), "") == 0)
- {
- strcpy(szTemp, "NULLMSG");
- }
- else
- {
- strcpy(szTemp, (iterEventAsk->MsgDefaultWhenNo).c_str());
- }
- fprintf(pFile,"tt%st%sn", "[No]",szTemp);
- if((iterEventAsk->DefaultEventRunListWhenNo).empty() != true)
- {
- for(iterEventRun = (iterEventAsk->DefaultEventRunListWhenNo).begin(); iterEventRun != (iterEventAsk->DefaultEventRunListWhenNo).end(); iterEventRun++)
- {
- anStr=(iterEventRun->EventRunItemName).c_str(); //ANDY_NEW
- strint = anStr.Length();
- mapint = anStr.AnsiPos(" ");
- anStr= anStr.SubString(mapint+1,strint-mapint);
- fprintf(pFile,"ttt%s %d %s %d %dn", "[EventRun]",iterEventRun->EventRunType, anStr.c_str(), iterEventRun->EventRunValue, iterEventRun->EventRunGoodKind);
- }
- }
- fprintf(pFile,"tt%snn", "[NoEnd]");
- if(strcmp((iterEventAsk->MsgDefaultWhenYes).c_str(), "") == 0)
- {
- strcpy(szTemp, "NULLMSG");
- }
- else
- {
- strcpy(szTemp, (iterEventAsk->MsgDefaultWhenYes).c_str());
- }
- fprintf(pFile,"tt%st%sn", "[Yes]",szTemp);
- if((iterEventAsk->DefaultEventRunListWhenYes).empty() != true)
- {
- for(iterEventRun = (iterEventAsk->DefaultEventRunListWhenYes).begin(); iterEventRun != (iterEventAsk->DefaultEventRunListWhenYes).end(); iterEventRun++)
- {
- anStr=(iterEventRun->EventRunItemName).c_str(); //ANDY_NEW
- strint = anStr.Length();
- mapint = anStr.AnsiPos(" ");
- anStr = anStr.SubString(mapint+1,strint-mapint);
- fprintf(pFile,"ttt%s %d %s %d %dn", "[EventRun]",iterEventRun->EventRunType, anStr.c_str(), iterEventRun->EventRunValue, iterEventRun->EventRunGoodKind);
- }
- }
- fprintf(pFile,"tt%snn", "[YesEnd]");
- fprintf(pFile,"t%snn", "[DefaultEnd]");
- // Write EventRun After Limit Checking
- if((iterEventAsk->LimitList).empty() != true)
- {
- for(iterLimit = (iterEventAsk->LimitList).begin(); iterLimit != (iterEventAsk->LimitList).end(); iterLimit++)
- {
- fprintf(pFile,"t%s %d %s %d %dnn", "[Limit]",iterLimit->LimitType, (iterLimit->LimitItemName).c_str(), iterLimit->LimitValue, iterLimit->LimitGoodKind);
- if(strcmp((iterLimit->MsgWhenNo).c_str(), "") == 0)
- {
- strcpy(szTemp, "NULLMSG");
- }
- else
- {
- strcpy(szTemp, (iterLimit->MsgWhenNo).c_str());
- }
- fprintf(pFile,"tt%st%sn", "[No]",szTemp);
- if((iterLimit->EventRunListWhenNo).empty() != true)
- {
- for(iterEventRun = (iterLimit->EventRunListWhenNo).begin(); iterEventRun != (iterLimit->EventRunListWhenNo).end(); iterEventRun++)
- {
- anStr=(iterEventRun->EventRunItemName).c_str(); //ANDY_NEW
- strint = anStr.Length();
- mapint = anStr.AnsiPos(" ");
- anStr = anStr.SubString(mapint+1,strint-mapint);
- fprintf(pFile,"ttt%s %d %s %d %dn", "[EventRun]",iterEventRun->EventRunType, anStr.c_str(), iterEventRun->EventRunValue, iterEventRun->EventRunGoodKind);
- }
- }
- fprintf(pFile,"tt%snn", "[NoEnd]");
- if(strcmp((iterLimit->MsgWhenYes).c_str(), "") == 0)
- {
- strcpy(szTemp, "NULLMSG");
- }
- else
- {
- strcpy(szTemp, (iterLimit->MsgWhenYes).c_str());
- }
- fprintf(pFile,"tt%st%sn", "[Yes]",szTemp);
- if((iterLimit->EventRunListWhenYes).empty() != true)
- {
- for(iterEventRun = (iterLimit->EventRunListWhenYes).begin(); iterEventRun != (iterLimit->EventRunListWhenYes).end(); iterEventRun++)
- {
- anStr=(iterEventRun->EventRunItemName).c_str(); //ANDY_NEW
- strint = anStr.Length();
- mapint = anStr.AnsiPos(" ");
- anStr = anStr.SubString(mapint+1,strint-mapint);
- fprintf(pFile,"ttt%s %d %s %d %dn", "[EventRun]",iterEventRun->EventRunType, anStr.c_str(), iterEventRun->EventRunValue, iterEventRun->EventRunGoodKind);
- }
- }
- fprintf(pFile,"tt%snn", "[YesEnd]");
- fprintf(pFile,"t%snn", "[LimitEnd]");
- } // for of LimitList
- } // if((iterEventAsk->LimitList).empty() != true)
- fprintf(pFile,"%snn", "[AskEnd]");
- } // for of EventAskList
- } // if((g_pNpcEvent->EventAskList).empty() != true)
- fclose(pFile);
- } // if(pFile != NULL)
- }
- //---------------------------------------------------------------------------
- int TFormEvent::GetSelectedEventAsk()
- {
- int i;
- for(i = 1; i < StringGridEventAsk->RowCount; i++)
- {
- if((StringGridEventAsk->Selection.Top == i) && (StringGridEventAsk->Selection.Bottom == i))
- {
- return i;
- }
- }
- return -1;
- }
- //---------------------------------------------------------------------------
- int TFormEvent::GetSelectedLimit()
- {
- int i;
- for(i = 1; i < StringGridLimit->RowCount; i++)
- {
- if((StringGridLimit->Selection.Top == i) && (StringGridLimit->Selection.Bottom == i))
- {
- return i;
- }
- }
- return -1;
- }
- //---------------------------------------------------------------------------
- int TFormEvent::GetSelectedEventRunWhenNo()
- {
- int i;
- for(i = 1; i < StringGridEventWhenNo->RowCount; i++)
- {
- if((StringGridEventWhenNo->Selection.Top == i) && (StringGridEventWhenNo->Selection.Bottom == i))
- {
- return i;
- }
- }
- return -1;
- }
- //---------------------------------------------------------------------------
- int TFormEvent::GetSelectedEventRunWhenYes()
- {
- int i;
- for(i = 1; i < StringGridEventWhenYes->RowCount; i++)
- {
- if((StringGridEventWhenYes->Selection.Top == i) && (StringGridEventWhenYes->Selection.Bottom == i))
- {
- return i;
- }
- }
- return -1;
- }
- //---------------------------------------------------------------------------
- int TFormEvent::GetSelectedDefaultEventRunWhenNo()
- {
- int i;
- for(i = 1; i < StringGridDefaultEventWhenNo->RowCount; i++)
- {
- if((StringGridDefaultEventWhenNo->Selection.Top == i) && (StringGridDefaultEventWhenNo->Selection.Bottom == i))
- {
- return i;
- }
- }
- return -1;
- }
- //---------------------------------------------------------------------------
- int TFormEvent::GetSelectedDefaultEventRunWhenYes()
- {
- int i;
- for(i = 1; i < StringGridDefaultEventWhenYes->RowCount; i++)
- {
- if((StringGridDefaultEventWhenYes->Selection.Top == i) && (StringGridDefaultEventWhenYes->Selection.Bottom == i))
- {
- return i;
- }
- }
- return -1;
- }
- //---------------------------------------------------------------------------
- int TFormEvent::UpdateEventAsk(int SelectedAfterUpdating)
- {
- list<CEventAsk>::iterator iterEventAsk;
- int i = 0, nAsk = 1, SelectedBeforeUpdating = 0;
- if(g_pNpcEvent == NULL)
- {
- Application->MessageBox("UpdateEventAsk 1", "ERROR", MB_OK|MB_ICONERROR);
- return -1;
- }
- if((SelectedBeforeUpdating = GetSelectedEventAsk()) <= 0)
- {
- Application->MessageBox("UpdateEventAsk 2", "ERROR", MB_OK|MB_ICONERROR);
- return -1;
- }
- // Update [EventAsk] StringGrid
- nAsk = 1;
- for(iterEventAsk = (g_pNpcEvent->EventAskList).begin(); iterEventAsk != (g_pNpcEvent->EventAskList).end(); iterEventAsk++)
- {
- StringGridEventAsk->Cells[0][nAsk] = (iterEventAsk->KeyWord).c_str();
- switch(iterEventAsk->AskType)
- {
- case EV_KEYWORD_None:
- StringGridEventAsk->Cells[1][nAsk] = "礚";
- break;
- case EV_KEYWORD_Visible:
- StringGridEventAsk->Cells[1][nAsk] = "