UnitEvent.cpp
上传用户:garry_shen
上传日期:2015-04-15
资源大小:45647k
文件大小:147k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Filectrl.hpp>
  5. #include "Unit1.h"
  6. #include "Unit5.h"
  7. #include "UnitEvent.h"
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. #pragma resource "*.dfm"
  11. TFormEvent *FormEvent;
  12. //---------------------------------------------------------------------------
  13. __fastcall TFormEvent::TFormEvent(TComponent* Owner)
  14.     : TForm(Owner)
  15. {
  16. }
  17. //---------------------------------------------------------------------------
  18. void TFormEvent::LoadNpcEventFile(AnsiString szFilePath)
  19. {
  20.     // Load NPC Event File
  21.     FILE *pFile;
  22.     AnsiString szStr;
  23.     //char szBuf1[64], szBuf2[64], szBuf3[64], szBuf4[64];
  24.     char szBuf1[512], szBuf2[512], szBuf3[512], szBuf4[512];
  25.     int nBuf1 = 0, nBuf2 = 0, nBuf3 = 0, nBuf4 = 0;
  26.     CEventAsk*   pEventAsk = NULL;
  27.     CEventLimit* pEventLimit = NULL;
  28.     CEventRun*   pEventRun = NULL;
  29.     if(g_pNpcEvent != NULL)
  30.     {
  31.         delete g_pNpcEvent;
  32.         g_pNpcEvent = NULL;
  33.     }
  34.     g_pNpcEvent = new CNpcEvent;
  35. // initialize g_pNpcEvent start
  36.     pFile = fopen(szFilePath.c_str(),"r");
  37. if(pFile != NULL)
  38. {
  39.         while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [AskItem]
  40. {
  41.             szStr = szBuf1;
  42.             if(szStr == "[AskEnd]")
  43.             {
  44.                 continue;
  45.             }
  46.             else if(szStr == "[AskItem]")
  47.             {
  48.                 if(fscanf(pFile, "%s %s %s", szBuf1, szBuf2, szBuf3) == 3)
  49.                 {
  50.                     nBuf1 = atoi(szBuf2);
  51.                     nBuf2 = atoi(szBuf3);
  52.                     pEventAsk = new CEventAsk(szBuf1, nBuf1, nBuf2);
  53.                     // Get Default EventRun
  54.                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Default]
  55.                     {
  56.                         szStr = szBuf1;
  57.                         if(szStr == "[AskEnd]")
  58.                         {
  59.                             break;
  60.                         }
  61.                         else if(szStr == "[DefaultEnd]")
  62.                         {
  63.                             break;
  64.                         }
  65.                         else if(szStr == "[Default]")
  66.                         {
  67.                                 fscanf(pFile,"%s",szBuf1);
  68.                                 szStr = szBuf1;
  69.                                 if(szStr == "[No]")
  70.                                 {
  71.                                     fscanf(pFile,"%s",szBuf1);
  72.                                     if(strcmp(szBuf1, "NULLMSG") == 0)
  73.                                     {
  74.                                         strcpy(szBuf1, "");
  75.                                     }
  76.                                     pEventAsk->MsgDefaultWhenNo = szBuf1;
  77.                                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Default Event When No]
  78.                                     {
  79.                                         szStr = szBuf1;
  80.                                         if(szStr == "[NoEnd]")
  81.                                         {
  82.                                             break;
  83.                                         }
  84.                                         if(szStr == "[EventRun]")
  85.                                         {
  86.                                             if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  87.                                             {
  88.                                                 pEventRun = new CEventRun;
  89.                                                 nBuf1 = atoi(szBuf1);
  90.                                                 pEventRun->EventRunType = nBuf1;
  91.                                                 pEventRun->EventRunItemName = szBuf2;
  92.                                                 nBuf3 = atoi(szBuf3);
  93.                                                 pEventRun->EventRunValue = nBuf3;
  94.                                 nBuf4 = atoi(szBuf4);
  95.                                  pEventRun->EventRunGoodKind = nBuf4;
  96.                                             }
  97.                                             (pEventAsk->DefaultEventRunListWhenNo).push_back(*pEventRun);
  98.                                             delete pEventRun;
  99.                                         }
  100.                                     }    //while [Default Event When No]
  101.                                 }    //if [Default No]
  102.                                 fscanf(pFile,"%s",szBuf1);
  103.                                 szStr = szBuf1;
  104.                                 if(szStr == "[Yes]")
  105.                                 {
  106.                                     fscanf(pFile,"%s",szBuf1);
  107.                                     if(strcmp(szBuf1, "NULLMSG") == 0)
  108.                                     {
  109.                                         strcpy(szBuf1, "");
  110.                                     }
  111.                                     pEventAsk->MsgDefaultWhenYes = szBuf1;
  112.                                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Event when Yes]
  113.                                     {
  114.                                         szStr = szBuf1;
  115.                                         if(szStr == "[YesEnd]")
  116.                                         {
  117.                                             break;
  118.                                         }
  119.                                         else if(szStr == "[EventRun]")
  120.                                         {
  121.                                             if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  122.                                             {
  123.                                                 pEventRun = new CEventRun;
  124.                                                 nBuf1 = atoi(szBuf1);
  125.                                                 pEventRun->EventRunType = nBuf1;
  126.                                                 pEventRun->EventRunItemName = szBuf2;
  127.                                                 nBuf3 = atoi(szBuf3);
  128.                                                 pEventRun->EventRunValue = nBuf3;
  129.                                 nBuf4 = atoi(szBuf4);
  130.                                  pEventRun->EventRunGoodKind = nBuf4;
  131.                                             }
  132.                                             (pEventAsk->DefaultEventRunListWhenYes).push_back(*pEventRun);
  133.                                             delete pEventRun;
  134.                                         }
  135.                                     }    //while [Default Event when Yes]
  136.                                 }    // if [Default Yes]
  137.                         }
  138.                     }    //while [Default]
  139.                     // Get EventRun After Check Limits
  140.                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Limit]
  141.                     {
  142.                         szStr = szBuf1;
  143.                         if(szStr == "[AskEnd]")
  144.                         {
  145.                             break;
  146.                         }
  147.                         else if(szStr == "[LimitEnd]")
  148.                         {
  149.                             continue;
  150.                         }
  151.                         else if(szStr == "[Limit]")
  152.                         {
  153.                             if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  154.                             {
  155.                                 pEventLimit = new CEventLimit;
  156.                                 nBuf1 = atoi(szBuf1);
  157.                                 pEventLimit->LimitType = nBuf1;
  158.                                 pEventLimit->LimitItemName = szBuf2;
  159.                                 nBuf3 = atoi(szBuf3);
  160.                                 pEventLimit->LimitValue = nBuf3;
  161.                                 nBuf4 = atoi(szBuf4);
  162.                                 pEventLimit->LimitGoodKind = nBuf4;
  163.                                 fscanf(pFile,"%s",szBuf1);
  164.                                 szStr = szBuf1;
  165.                                 if(szStr == "[No]")
  166.                                 {
  167.                                     fscanf(pFile,"%s",szBuf1);
  168.                                     if(strcmp(szBuf1, "NULLMSG") == 0)
  169.                                     {
  170.                                         strcpy(szBuf1, "");
  171.                                     }
  172.                                     pEventLimit->MsgWhenNo = szBuf1;
  173.                                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [EventRun when No]
  174.                                     {
  175.                                         szStr = szBuf1;
  176.                                         if(szStr == "[NoEnd]")
  177.                                         {
  178.                                             break;
  179.                                         }
  180.                                         if(szStr == "[EventRun]")
  181.                                         {
  182.                                             if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  183.                                             {
  184.                                                 pEventRun = new CEventRun;
  185.                                                 nBuf1 = atoi(szBuf1);
  186.                                                 pEventRun->EventRunType = nBuf1;
  187.                                                 pEventRun->EventRunItemName = szBuf2;
  188.                                                 nBuf3 = atoi(szBuf3);
  189.                                                 pEventRun->EventRunValue = nBuf3;
  190.                                 nBuf4 = atoi(szBuf4);
  191.                                  pEventRun->EventRunGoodKind = nBuf4;
  192.                                             }
  193.                                             (pEventLimit->EventRunListWhenNo).push_back(*pEventRun);
  194.                                             delete pEventRun;
  195.                                         }
  196.                                     }    //while [EventRun when No]
  197.                                 }    //if [No]
  198.                                 fscanf(pFile,"%s",szBuf1);
  199.                                 szStr = szBuf1;
  200.                                 if(szStr == "[Yes]")
  201.                                 {
  202.                                     fscanf(pFile,"%s",szBuf1);
  203.                                     if(strcmp(szBuf1, "NULLMSG") == 0)
  204.                                     {
  205.                                         strcpy(szBuf1, "");
  206.                                     }
  207.                                     pEventLimit->MsgWhenYes = szBuf1;
  208.                                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [EventRun when Yes]
  209.                                     {
  210.                                         szStr = szBuf1;
  211.                                         if(szStr == "[YesEnd]")
  212.                                         {
  213.                                             break;
  214.                                         }
  215.                                         else if(szStr == "[EventRun]")
  216.                                         {
  217.                                             if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  218.                                             {
  219.                                                 pEventRun = new CEventRun;
  220.                                                 nBuf1 = atoi(szBuf1);
  221.                                                 pEventRun->EventRunType = nBuf1;
  222.                                                 pEventRun->EventRunItemName = szBuf2;
  223.                                                 nBuf3 = atoi(szBuf3);
  224.                                                 pEventRun->EventRunValue = nBuf3;
  225.                                 nBuf4 = atoi(szBuf4);
  226.                                  pEventRun->EventRunGoodKind = nBuf4;
  227.                                             }
  228.                                             (pEventLimit->EventRunListWhenYes).push_back(*pEventRun);
  229.                                             delete pEventRun;
  230.                                         }
  231.                                     }    //while [EventRun when Yes]
  232.                                 }    // if [Yes]
  233.                             }
  234.                             (pEventAsk->LimitList).push_back(*pEventLimit);
  235.                             delete pEventLimit;
  236.                         }
  237.                     }    //while [Limit]
  238.                 }
  239.                 (g_pNpcEvent->EventAskList).push_back(*pEventAsk);
  240.                 delete pEventAsk;
  241.             }
  242.         }    //while [AskItem]
  243. // initialize g_pNpcEvent end
  244.        fclose(pFile);
  245.     }
  246.     // show All Data
  247.     UpdateEventAsk(1);
  248. }
  249. //---------------------------------------------------------------------------
  250. void TFormEvent::SaveNpcEventFile(AnsiString szFilePath)
  251. {
  252.     FILE *pFile = NULL;
  253.     //char szTemp[128];
  254.     char szTemp[512];
  255.     list<CEventAsk>::iterator iterEventAsk;
  256.     list<CEventLimit>::iterator iterLimit;
  257.     list<CEventRun>::iterator iterEventRun;
  258.         AnsiString anStr;   //ANDY_NEW
  259.         int mapint,strint;
  260.     pFile = fopen(szFilePath.c_str(), "wt");
  261.     if(pFile != NULL)
  262.     {
  263.         if((g_pNpcEvent->EventAskList).empty() != true)
  264.         {
  265.             for(iterEventAsk = (g_pNpcEvent->EventAskList).begin(); iterEventAsk != (g_pNpcEvent->EventAskList).end(); iterEventAsk++)
  266.             {
  267.                 fprintf(pFile,"%s  %s  %d  %dnn", "[AskItem]",(iterEventAsk->KeyWord).c_str(), iterEventAsk->AskType, iterEventAsk->LimitChoose);
  268.                 // Write Default EventRun
  269.                 fprintf(pFile,"t%snn", "[Default]");
  270.                 if(strcmp((iterEventAsk->MsgDefaultWhenNo).c_str(), "") == 0)
  271.                 {
  272.                     strcpy(szTemp, "NULLMSG");
  273.                 }
  274.                 else
  275.                 {
  276.                     strcpy(szTemp, (iterEventAsk->MsgDefaultWhenNo).c_str());
  277.                 }
  278.                 fprintf(pFile,"tt%st%sn", "[No]",szTemp);
  279.                 if((iterEventAsk->DefaultEventRunListWhenNo).empty() != true)
  280.                 {
  281.                     for(iterEventRun = (iterEventAsk->DefaultEventRunListWhenNo).begin(); iterEventRun != (iterEventAsk->DefaultEventRunListWhenNo).end(); iterEventRun++)
  282.                     {
  283.                         anStr=(iterEventRun->EventRunItemName).c_str();  //ANDY_NEW
  284.                         strint = anStr.Length();
  285.                         mapint = anStr.AnsiPos(" ");
  286.                         anStr= anStr.SubString(mapint+1,strint-mapint);
  287.                         fprintf(pFile,"ttt%s  %d  %s  %d  %dn", "[EventRun]",iterEventRun->EventRunType, anStr.c_str(), iterEventRun->EventRunValue, iterEventRun->EventRunGoodKind);
  288.                     }
  289.                 }
  290.                 fprintf(pFile,"tt%snn", "[NoEnd]");
  291.                 if(strcmp((iterEventAsk->MsgDefaultWhenYes).c_str(), "") == 0)
  292.                 {
  293.                     strcpy(szTemp, "NULLMSG");
  294.                 }
  295.                 else
  296.                 {
  297.                     strcpy(szTemp, (iterEventAsk->MsgDefaultWhenYes).c_str());
  298.                 }
  299.                 fprintf(pFile,"tt%st%sn", "[Yes]",szTemp);
  300.                 if((iterEventAsk->DefaultEventRunListWhenYes).empty() != true)
  301.                 {
  302.                     for(iterEventRun = (iterEventAsk->DefaultEventRunListWhenYes).begin(); iterEventRun != (iterEventAsk->DefaultEventRunListWhenYes).end(); iterEventRun++)
  303.                     {
  304.                         anStr=(iterEventRun->EventRunItemName).c_str();  //ANDY_NEW
  305.                         strint = anStr.Length();
  306.                         mapint = anStr.AnsiPos(" ");
  307.                         anStr = anStr.SubString(mapint+1,strint-mapint);
  308.                         fprintf(pFile,"ttt%s  %d  %s  %d  %dn", "[EventRun]",iterEventRun->EventRunType, anStr.c_str(), iterEventRun->EventRunValue, iterEventRun->EventRunGoodKind);
  309.                     }
  310.                 }
  311.                 fprintf(pFile,"tt%snn", "[YesEnd]");
  312.                 fprintf(pFile,"t%snn", "[DefaultEnd]");
  313.                 // Write EventRun After Limit Checking
  314.                 if((iterEventAsk->LimitList).empty() != true)
  315.                 {
  316.                     for(iterLimit = (iterEventAsk->LimitList).begin(); iterLimit != (iterEventAsk->LimitList).end(); iterLimit++)
  317.                     {
  318.                         fprintf(pFile,"t%s  %d  %s  %d  %dnn", "[Limit]",iterLimit->LimitType, (iterLimit->LimitItemName).c_str(), iterLimit->LimitValue, iterLimit->LimitGoodKind);
  319.                         if(strcmp((iterLimit->MsgWhenNo).c_str(), "") == 0)
  320.                         {
  321.                             strcpy(szTemp, "NULLMSG");
  322.                         }
  323.                         else
  324.                         {
  325.                             strcpy(szTemp, (iterLimit->MsgWhenNo).c_str());
  326.                         }
  327.                         fprintf(pFile,"tt%st%sn", "[No]",szTemp);
  328.                         if((iterLimit->EventRunListWhenNo).empty() != true)
  329.                         {
  330.                             for(iterEventRun = (iterLimit->EventRunListWhenNo).begin(); iterEventRun != (iterLimit->EventRunListWhenNo).end(); iterEventRun++)
  331.                             {
  332.                                 anStr=(iterEventRun->EventRunItemName).c_str();  //ANDY_NEW
  333.                                 strint = anStr.Length();
  334.                                 mapint = anStr.AnsiPos(" ");
  335.                                 anStr = anStr.SubString(mapint+1,strint-mapint);
  336.                                 fprintf(pFile,"ttt%s  %d  %s  %d  %dn", "[EventRun]",iterEventRun->EventRunType, anStr.c_str(), iterEventRun->EventRunValue, iterEventRun->EventRunGoodKind);
  337.                             }
  338.                         }
  339.                         fprintf(pFile,"tt%snn", "[NoEnd]");
  340.                         if(strcmp((iterLimit->MsgWhenYes).c_str(), "") == 0)
  341.                         {
  342.                             strcpy(szTemp, "NULLMSG");
  343.                         }
  344.                         else
  345.                         {
  346.                             strcpy(szTemp, (iterLimit->MsgWhenYes).c_str());
  347.                         }
  348.                         fprintf(pFile,"tt%st%sn", "[Yes]",szTemp);
  349.                         if((iterLimit->EventRunListWhenYes).empty() != true)
  350.                         {
  351.                             for(iterEventRun = (iterLimit->EventRunListWhenYes).begin(); iterEventRun != (iterLimit->EventRunListWhenYes).end(); iterEventRun++)
  352.                             {
  353.                         anStr=(iterEventRun->EventRunItemName).c_str();   //ANDY_NEW
  354.                         strint = anStr.Length();
  355.                         mapint = anStr.AnsiPos(" ");
  356.                         anStr = anStr.SubString(mapint+1,strint-mapint);
  357.                                 fprintf(pFile,"ttt%s  %d  %s  %d  %dn", "[EventRun]",iterEventRun->EventRunType, anStr.c_str(), iterEventRun->EventRunValue, iterEventRun->EventRunGoodKind);
  358.                             }
  359.                         }
  360.                         fprintf(pFile,"tt%snn", "[YesEnd]");
  361.                         fprintf(pFile,"t%snn", "[LimitEnd]");
  362.                     }   //  for of LimitList
  363.                 }   //  if((iterEventAsk->LimitList).empty() != true)
  364.                 fprintf(pFile,"%snn", "[AskEnd]");
  365.             }   //  for of EventAskList
  366.         }   // if((g_pNpcEvent->EventAskList).empty() != true)
  367.         fclose(pFile);
  368.     }   // if(pFile != NULL)
  369. }
  370. //---------------------------------------------------------------------------
  371. int TFormEvent::GetSelectedEventAsk()
  372. {
  373.     int i;
  374.     for(i = 1; i < StringGridEventAsk->RowCount; i++)
  375.     {
  376.         if((StringGridEventAsk->Selection.Top == i) && (StringGridEventAsk->Selection.Bottom == i))
  377.         {
  378.             return i;
  379.         }
  380.     }
  381.     return -1;
  382. }
  383. //---------------------------------------------------------------------------
  384. int TFormEvent::GetSelectedLimit()
  385. {
  386.     int i;
  387.     for(i = 1; i < StringGridLimit->RowCount; i++)
  388.     {
  389.         if((StringGridLimit->Selection.Top == i) && (StringGridLimit->Selection.Bottom == i))
  390.         {
  391.             return i;
  392.         }
  393.     }
  394.     return -1;
  395. }
  396. //---------------------------------------------------------------------------
  397. int TFormEvent::GetSelectedEventRunWhenNo()
  398. {
  399.     int i;
  400.     for(i = 1; i < StringGridEventWhenNo->RowCount; i++)
  401.     {
  402.         if((StringGridEventWhenNo->Selection.Top == i) && (StringGridEventWhenNo->Selection.Bottom == i))
  403.         {
  404.             return i;
  405.         }
  406.     }
  407.     return -1;
  408. }
  409. //---------------------------------------------------------------------------
  410. int TFormEvent::GetSelectedEventRunWhenYes()
  411. {
  412.     int i;
  413.     for(i = 1; i < StringGridEventWhenYes->RowCount; i++)
  414.     {
  415.         if((StringGridEventWhenYes->Selection.Top == i) && (StringGridEventWhenYes->Selection.Bottom == i))
  416.         {
  417.             return i;
  418.         }
  419.     }
  420.     return -1;
  421. }
  422. //---------------------------------------------------------------------------
  423. int TFormEvent::GetSelectedDefaultEventRunWhenNo()
  424. {
  425.     int i;
  426.     for(i = 1; i < StringGridDefaultEventWhenNo->RowCount; i++)
  427.     {
  428.         if((StringGridDefaultEventWhenNo->Selection.Top == i) && (StringGridDefaultEventWhenNo->Selection.Bottom == i))
  429.         {
  430.             return i;
  431.         }
  432.     }
  433.     return -1;
  434. }
  435. //---------------------------------------------------------------------------
  436. int TFormEvent::GetSelectedDefaultEventRunWhenYes()
  437. {
  438.     int i;
  439.     for(i = 1; i < StringGridDefaultEventWhenYes->RowCount; i++)
  440.     {
  441.         if((StringGridDefaultEventWhenYes->Selection.Top == i) && (StringGridDefaultEventWhenYes->Selection.Bottom == i))
  442.         {
  443.             return i;
  444.         }
  445.     }
  446.     return -1;
  447. }
  448. //---------------------------------------------------------------------------
  449. int TFormEvent::UpdateEventAsk(int SelectedAfterUpdating)
  450. {
  451.     list<CEventAsk>::iterator iterEventAsk;
  452.     int i = 0, nAsk = 1, SelectedBeforeUpdating = 0;
  453.     if(g_pNpcEvent == NULL)
  454.     {
  455.         Application->MessageBox("UpdateEventAsk 1", "ERROR", MB_OK|MB_ICONERROR);
  456.         return -1;
  457.     }
  458.     if((SelectedBeforeUpdating = GetSelectedEventAsk()) <= 0)
  459.     {
  460.         Application->MessageBox("UpdateEventAsk 2", "ERROR", MB_OK|MB_ICONERROR);
  461.         return -1;
  462.     }
  463.     // Update [EventAsk] StringGrid
  464.     nAsk = 1;
  465.     for(iterEventAsk = (g_pNpcEvent->EventAskList).begin(); iterEventAsk != (g_pNpcEvent->EventAskList).end(); iterEventAsk++)
  466.     {
  467.         StringGridEventAsk->Cells[0][nAsk] = (iterEventAsk->KeyWord).c_str();
  468.         switch(iterEventAsk->AskType)
  469.         {
  470.             case EV_KEYWORD_None:
  471.                  StringGridEventAsk->Cells[1][nAsk] = "礚";
  472.                  break;
  473.             case EV_KEYWORD_Visible:
  474.                  StringGridEventAsk->Cells[1][nAsk] = "