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

游戏引擎

开发平台:

Visual C++

  1. // N_EventNpc.cpp: implementation of the CN_EventNpc class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "......goodsGoodsGoods.h"
  6. #include "....Npc.h"
  7. #include "......Common.h"
  8. #include "N_EventNpc.h"
  9. #include "......config.h"
  10. extern char g_tszPathName[256];
  11. extern rmfullglobals myglobs;
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CN_EventNpc::CN_EventNpc(char *szPath, char *szNpcFile):CN_Disciple(szPath, szNpcFile)
  16. {
  17. FILE *pFile = NULL;
  18. char szFileName[512], szBuf1[512], szBuf2[512];
  19. int nAskType = 0;
  20. g_HasEvent = 0;
  21. g_pNpcEvent = NULL;
  22. list<CEventAsk>::iterator iterEventAsk;
  23. char szKeyWord[128] = "NULL";
  24. nLimitCount = 0;
  25. strcpy(szFileName, szPath);
  26. strcat(szFileName, "\");
  27. strcat(szFileName, szNpcFile);
  28.     pFile = fopen(szFileName,"r");
  29. if(pFile != NULL)
  30. {
  31.         while(fscanf(pFile,"%s",szBuf1)!=EOF)
  32. {
  33.             if(strcmp(szBuf1,"ㄆン") == 0)
  34. {
  35. fscanf(pFile,"%s",szBuf1);
  36. if(strcmp(szBuf1,"礚") != 0)
  37. {
  38. strcpy(szBuf2, g_tszPathName);
  39. strcat(szBuf2, "\Event\");
  40. strcat(szBuf2, szBuf1);
  41. LoadNpcEventFile(szBuf2);
  42. for(iterEventAsk = (g_pNpcEvent->EventAskList).begin(); iterEventAsk != (g_pNpcEvent->EventAskList).end(); iterEventAsk++)
  43. {
  44. switch(iterEventAsk->AskType)
  45. {
  46. case EV_KEYWORD_Visible :
  47. nAskType = NPCASK_visible;
  48. break;
  49. case EV_KEYWORD_Invisible :
  50. nAskType = 0;
  51. break;
  52. default ://AddRecordMsg("AskType = default");
  53. nAskType = 0;
  54. break;
  55. }
  56. strcpy(szKeyWord,(iterEventAsk->KeyWord).c_str());
  57. AddAsk(szKeyWord, nAskType, "");
  58. }
  59. }
  60. }
  61. } // while
  62. fclose(pFile);
  63. }
  64. else
  65. {
  66. //AddRecordMsg("Cannot Open Event File");
  67. }
  68. }
  69. CN_EventNpc::~CN_EventNpc()
  70. {
  71. }
  72. void CN_EventNpc::LoadNpcEventFile(char *szNpcEventFile)
  73. {
  74.     // Load NPC Event File
  75.     FILE *pFile;
  76.     CString szStr;
  77.     char szBuf1[64], szBuf2[64], szBuf3[64], szBuf4[64];
  78.     int nBuf1 = 0, nBuf2 = 0, nBuf3 = 0, nBuf4 = 0;
  79.     CEventAsk*   pEventAsk = NULL;
  80.     CEventLimit* pEventLimit = NULL;
  81.     CEventRun*   pEventRun = NULL;
  82. // initialize g_pNpcEvent start
  83.     pFile = fopen(szNpcEventFile,"r");
  84. if(pFile != NULL)
  85. {
  86. g_pNpcEvent = new CNpcEvent;
  87. g_HasEvent = 1;
  88.         (g_pNpcEvent->EventAskList).clear();
  89.         while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [AskItem]
  90. {
  91.             szStr = szBuf1;
  92.             if(szStr == "[AskEnd]")
  93.             {
  94.                 continue;
  95.             }
  96.             else if(szStr == "[AskItem]")
  97.             {
  98.                 if(fscanf(pFile, "%s %s %s", szBuf1, szBuf2, szBuf3) == 3)
  99.                 {
  100.                     nBuf1 = atoi(szBuf2);
  101.                     nBuf2 = atoi(szBuf3);
  102.                     pEventAsk = new CEventAsk(szBuf1, nBuf1, nBuf2);
  103.                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Default]
  104.                     {
  105.                         szStr = szBuf1;
  106.                         if(szStr == "[AskEnd]")
  107.                         {
  108.                             break;
  109.                         }
  110.                         else if(szStr == "[DefaultEnd]")
  111.                         {
  112.                             break;
  113.                         }
  114.                         else if(szStr == "[Default]")
  115.                         {
  116. // Read Default EventRun data
  117.                             fscanf(pFile,"%s",szBuf1);
  118.                             szStr = szBuf1;
  119.                             if(szStr == "[No]")
  120.                             {
  121.                                 fscanf(pFile,"%s",szBuf1);
  122.                                 if(strcmp(szBuf1, "NULLMSG") == 0)
  123.                                 {
  124.                                     strcpy(szBuf1, "");
  125.                                 }
  126.                                 pEventAsk->MsgDefaultWhenNo = szBuf1;
  127.                                 while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Event] when [No]
  128.                                 {
  129.                                     szStr = szBuf1;
  130.                                     if(szStr == "[NoEnd]")
  131.                                     {
  132.                                         break;
  133.                                     }
  134.                                     if(szStr == "[EventRun]")
  135.                                     {
  136.                                         if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  137.                                         {
  138.                                             pEventRun = new CEventRun;
  139.                                             nBuf1 = atoi(szBuf1);
  140.                                             pEventRun->EventRunType = nBuf1;
  141.                                             pEventRun->EventRunItemName = szBuf2;
  142.                                              nBuf3 = atoi(szBuf3);
  143.                                             pEventRun->EventRunValue = nBuf3;
  144.                                      nBuf4 = atoi(szBuf4);
  145.                                pEventRun->EventRunGoodKind = nBuf4;
  146.                                         }
  147.                                         (pEventAsk->DefaultEventRunListWhenNo).push_back(*pEventRun);
  148.                                         delete pEventRun;
  149.                                     }
  150.                                 }    //while [Default Event] when [No]
  151.                             }    //if [Default No]
  152.                             fscanf(pFile,"%s",szBuf1);
  153.                             szStr = szBuf1;
  154.                             if(szStr == "[Yes]")
  155.                             {
  156.                                 fscanf(pFile,"%s",szBuf1);
  157.                                 if(strcmp(szBuf1, "NULLMSG") == 0)
  158.                                 {
  159.                                     strcpy(szBuf1, "");
  160.                                 }
  161.                                 pEventAsk->MsgDefaultWhenYes = szBuf1;
  162.                                 while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Event] when [Yes]
  163.                                 {
  164.                                     szStr = szBuf1;
  165.                                     if(szStr == "[YesEnd]")
  166.                                     {
  167.                                         break;
  168.                                     }
  169.                                     else if(szStr == "[EventRun]")
  170.                                     {
  171.                                         if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  172.                                         {
  173.                                             pEventRun = new CEventRun;
  174.                                             nBuf1 = atoi(szBuf1);
  175.                                             pEventRun->EventRunType = nBuf1;
  176.                                             pEventRun->EventRunItemName = szBuf2;
  177.                                             nBuf3 = atoi(szBuf3);   
  178.                                             pEventRun->EventRunValue = nBuf3;
  179.                                 nBuf4 = atoi(szBuf4);
  180.                                pEventRun->EventRunGoodKind = nBuf4;
  181.                                         }
  182.                                         (pEventAsk->DefaultEventRunListWhenYes).push_back(*pEventRun);
  183.                                         delete pEventRun;
  184.                                     }
  185.                                 }    //while [Default Event] when [Yes]
  186.                             }    // if [DefaultYes]
  187.                         }
  188.                     }    //while [Default]
  189. // Read EventRun after checking limits
  190.                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Limit]
  191.                     {
  192.                         szStr = szBuf1;
  193.                         if(szStr == "[AskEnd]")
  194.                         {
  195.                             break;
  196.                         }
  197.                         else if(szStr == "[LimitEnd]")
  198.                         {
  199.                             continue;
  200.                         }
  201.                         else if(szStr == "[Limit]")
  202.                         {
  203.                             if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  204.                             {
  205.                                 pEventLimit = new CEventLimit;
  206.                                 nBuf1 = atoi(szBuf1);
  207.                                 pEventLimit->LimitType = nBuf1;
  208.                                 pEventLimit->LimitItemName = szBuf2;
  209.                                 nBuf3 = atoi(szBuf3);
  210.                                 pEventLimit->LimitValue = nBuf3;
  211.                                 nBuf4 = atoi(szBuf4);
  212.                                 pEventLimit->LimitGoodKind = nBuf4;
  213.                                 fscanf(pFile,"%s",szBuf1);
  214.                                 szStr = szBuf1;
  215.                                 if(szStr == "[No]")
  216.                                 {
  217.                                     fscanf(pFile,"%s",szBuf1);
  218.                                     if(strcmp(szBuf1, "NULLMSG") == 0)
  219.                                     {
  220.                                         strcpy(szBuf1, "");
  221.                                     }
  222.                                     pEventLimit->MsgWhenNo = szBuf1;
  223.                                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Event] when [No]
  224.                                     {
  225.                                         szStr = szBuf1;
  226.                                         if(szStr == "[NoEnd]")
  227.                                         {
  228.                                             break;
  229.                                         }
  230.                                         if(szStr == "[EventRun]")
  231.                                         {
  232.                                             if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  233.                                             {
  234.                                                 pEventRun = new CEventRun;
  235.                                                 nBuf1 = atoi(szBuf1);
  236.                                                 pEventRun->EventRunType = nBuf1;
  237.                                                 pEventRun->EventRunItemName = szBuf2;
  238.                                                 nBuf3 = atoi(szBuf3);
  239.                                                 pEventRun->EventRunValue = nBuf3;
  240.                                 nBuf4 = atoi(szBuf4);
  241.                                  pEventRun->EventRunGoodKind = nBuf4;
  242.                                             }
  243.                                             (pEventLimit->EventRunListWhenNo).push_back(*pEventRun);
  244.                                             delete pEventRun;
  245.                                         }
  246.                                     }    //while [Event] when [No]
  247.                                 }    //if [No]
  248.                                 fscanf(pFile,"%s",szBuf1);
  249.                                 szStr = szBuf1;
  250.                                 if(szStr == "[Yes]")
  251.                                 {
  252.                                     fscanf(pFile,"%s",szBuf1);
  253.                                     if(strcmp(szBuf1, "NULLMSG") == 0)
  254.                                     {
  255.                                         strcpy(szBuf1, "");
  256.                                     }
  257.                                     pEventLimit->MsgWhenYes = szBuf1;
  258.                                     while(fscanf(pFile,"%s",szBuf1)!=EOF)     //while [Event] when [Yes]
  259.                                     {
  260.                                         szStr = szBuf1;
  261.                                         if(szStr == "[YesEnd]")
  262.                                         {
  263.                                             break;
  264.                                         }
  265.                                         else if(szStr == "[EventRun]")
  266.                                         {
  267.                                             if(fscanf(pFile, "%s %s %s %s", szBuf1, szBuf2, szBuf3, szBuf4) == 4)
  268.                                             {
  269.                                                 pEventRun = new CEventRun;
  270.                                                 nBuf1 = atoi(szBuf1);
  271.                                                 pEventRun->EventRunType = nBuf1;
  272.                                                 pEventRun->EventRunItemName = szBuf2;
  273.                                                 nBuf3 = atoi(szBuf3);
  274.                                                 pEventRun->EventRunValue = nBuf3;
  275.                                 nBuf4 = atoi(szBuf4);
  276.                                  pEventRun->EventRunGoodKind = nBuf4;
  277.                                             }
  278.                                             (pEventLimit->EventRunListWhenYes).push_back(*pEventRun);
  279.                                             delete pEventRun;
  280.                                         }
  281.                                     }    //while [Event] when [Yes]
  282.                                 }    // if [Yes]
  283.                             }
  284.                             (pEventAsk->LimitList).push_back(*pEventLimit);
  285.                             delete pEventLimit;
  286.                         }
  287.                     }    //while [Limit]
  288.                 }
  289.                 (g_pNpcEvent->EventAskList).push_back(*pEventAsk);
  290.                 delete pEventAsk;
  291.             }
  292.         }    //while [AskItem]
  293.         fclose(pFile);
  294.     }
  295. }
  296. TRetInfo CN_EventNpc::DoAsk(CPlayerInfo *who, CString szName)
  297. {
  298. TRetInfo ri;
  299. list<CEventAsk>::iterator iterEventAsk;
  300. ri = CNpc::DoAsk(who, szName,"$NONE$");
  301. if (!ri.bRet) return ri;
  302.     if(g_HasEvent == 1)
  303. {
  304. for(iterEventAsk = (g_pNpcEvent->EventAskList).begin(); iterEventAsk != (g_pNpcEvent->EventAskList).end(); iterEventAsk++)
  305. {
  306. if ( !strcmp(szName, (iterEventAsk->KeyWord).c_str() ) ) 
  307. {
  308. //AddRecordMsg("KeyWord_run");
  309. ri = DoEventLimit(who, iterEventAsk);
  310. return ri;
  311. }
  312. }
  313. }
  314. ri.bRet = TRUE;
  315. return ri;
  316. }
  317. TRetInfo CN_EventNpc::DoEventLimit(CPlayerInfo *who, list<CEventAsk>::iterator iterEventAsk)
  318. {
  319. int IsYes = -1;
  320. TRetInfo ri;
  321.     list<CEventLimit>::iterator iterLimit, iterEndLimit;
  322. char szLimitItemName[64] = "NULL";
  323. CGoods *pGood = NULL;
  324. switch(iterEventAsk->LimitChoose)
  325. {
  326. case EV_LIMITCHOOSE_All : //AddRecordMsg("LIMITCHOOSE_All");
  327. IsYes = 1;
  328. for(iterLimit = (iterEventAsk->LimitList).begin(); iterLimit != (iterEventAsk->LimitList).end(); iterLimit++)
  329.          {
  330. strcpy(szLimitItemName, (iterLimit->LimitItemName).c_str());
  331. switch(iterLimit->LimitType)
  332. {
  333.                         case EV_LIMIT_NotDefine:        //AddRecordMsg("LIMIT_NoDefine");
  334. ri.szRet = "iterEventAsk->LimitType=LIMIT_NotDefine";
  335.                                 break;
  336.                         case EV_LIMIT_GoodHasGot:       //AddRecordMsg("LIMIT_GoodHasGot");
  337. if(who->GetGoods(szLimitItemName) == NULL)
  338. {
  339. IsYes = 0;
  340. }
  341.                                 break;
  342.                         case EV_LIMIT_GoodHasNotGot:    //AddRecordMsg("LIMIT_GoodHasNotGot");
  343. if(who->GetGoods(szLimitItemName) != NULL)
  344. {
  345. IsYes = 0;
  346. }
  347.                              break;
  348.                         case EV_LIMIT_GoodCountMore:    //AddRecordMsg("LIMIT_GoodCountMore");
  349. if(who->GetGoodCount(szLimitItemName) <= iterLimit->LimitValue)
  350. {
  351. IsYes = 0;
  352. }
  353.                     break;
  354.                         case EV_LIMIT_GoodCountEqual:   //AddRecordMsg("LIMIT_GoodCountEqual");
  355. if(who->GetGoodCount(szLimitItemName) != iterLimit->LimitValue)
  356. {
  357. IsYes = 0;
  358. }
  359.                                 break;
  360.                         case EV_LIMIT_GoodCountLess:    //AddRecordMsg("LIMIT_GoodCountLess");
  361. if(who->GetGoodCount(szLimitItemName) >= iterLimit->LimitValue)
  362. {
  363. IsYes = 0;
  364. }
  365.                                 break;
  366.                         case EV_LIMIT_MoneyMore:       //AddRecordMsg("LIMIT_MoneyMore");
  367. if(who->nMoney <= iterLimit->LimitValue)
  368. {
  369. IsYes = 0;
  370. }
  371.                                 break;
  372.                         case EV_LIMIT_MoneyLess:       //AddRecordMsg("LIMIT_MoneyLess");
  373. if(who->nMoney >= iterLimit->LimitValue)
  374. {
  375. IsYes = 0;
  376. }
  377.                                 break;
  378.                         case EV_LIMIT_StautsPointMore: //AddRecordMsg("LIMIT_StautsPointMore");
  379. if( strcmp("