KPlayerMenuState.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:6k
源码类别:

模拟服务器

开发平台:

C/C++

  1. //---------------------------------------------------------------------------
  2. // Sword3 Engine (c) 2002 by Kingsoft
  3. //
  4. // File: KPlayerMenuState.cpp
  5. // Date: 2002.12.10
  6. // Code: 边城浪子
  7. // Desc: PlayerMenuState Class
  8. //---------------------------------------------------------------------------
  9. #include "KCore.h"
  10. #include "MyAssert.H"
  11. #include "KPlayer.h"
  12. #ifdef _SERVER
  13. //#include "KNetServer.h"
  14. //#include "../MultiServer/Heaven/Interface/iServer.h"
  15. #endif
  16. #include "KPlayerMenuState.h"
  17. #include "CoreUseNameDef.h"
  18. KPlayerMenuState::KPlayerMenuState()
  19. {
  20. Release();
  21. }
  22. #ifdef _SERVER
  23. void KPlayerMenuState::Release()
  24. {
  25. m_nState = PLAYER_MENU_STATE_NORMAL;
  26. m_nTradeDest = -1;
  27. m_nTradeState = 0;
  28. memset(m_szSentence, 0, MAX_SENTENCE_LENGTH);
  29. m_nBackState = PLAYER_MENU_STATE_NORMAL;
  30. memset(m_szBackSentence, 0, MAX_SENTENCE_LENGTH);
  31. }
  32. #endif
  33. #ifndef _SERVER
  34. void KPlayerMenuState::Release()
  35. {
  36. m_nState = PLAYER_MENU_STATE_NORMAL;
  37. m_nTradeDest = -1;
  38. m_nTradeState = 0;
  39. m_nTradeDestState = 0;
  40. memset(m_szSentence, 0, MAX_SENTENCE_LENGTH);
  41. }
  42. #endif
  43. #ifdef _SERVER
  44. void KPlayerMenuState::BackupState()
  45. {
  46. m_nBackState = m_nState;
  47. memcpy(m_szBackSentence, m_szSentence, sizeof(m_szSentence));
  48. };
  49. #endif
  50. #ifdef _SERVER
  51. void KPlayerMenuState::RestoreState()
  52. {
  53. m_nState = m_nBackState;
  54. memcpy(m_szSentence, m_szBackSentence, sizeof(m_szSentence));
  55. };
  56. #endif
  57. #ifdef _SERVER
  58. void KPlayerMenuState::SetState(int nPlayerIdx, int nState, char *lpszSentence/*=NULL*/, int nSentenceLength/*=0*/, DWORD dwDestNpcID/*=0*/)
  59. {
  60. if (nState < PLAYER_MENU_STATE_NORMAL || nState >= PLAYER_MENU_STATE_NUM)
  61. return;
  62. if (m_nState != nState)
  63. {
  64. m_nBackState = m_nState;
  65. m_nState = nState;
  66. memcpy(m_szBackSentence, m_szSentence, sizeof(m_szSentence));
  67. }
  68. if (lpszSentence == NULL || nSentenceLength <= 0)
  69. {
  70. m_szSentence[0] = 0;
  71. }
  72. else
  73. {
  74. if (nSentenceLength >= MAX_SENTENCE_LENGTH)
  75. nSentenceLength = MAX_SENTENCE_LENGTH - 1;
  76. memcpy(m_szSentence, lpszSentence, nSentenceLength);
  77. m_szSentence[nSentenceLength] = 0;
  78. }
  79. switch (nState)
  80. {
  81. case PLAYER_MENU_STATE_NORMAL:
  82. {
  83. if (m_nBackState == PLAYER_MENU_STATE_TEAMOPEN)
  84. {
  85. if (Player[nPlayerIdx].m_cTeam.m_nFlag && g_Team[Player[nPlayerIdx].m_cTeam.m_nID].IsOpen())
  86. g_Team[Player[nPlayerIdx].m_cTeam.m_nID].SetTeamClose();
  87. }
  88. TRADE_CHANGE_STATE_SYNC sTrade;
  89. sTrade.ProtocolType = s2c_tradechangestate;
  90. sTrade.m_btState = 0;
  91. g_pServer->PackDataToClient(Player[nPlayerIdx].m_nNetConnectIdx, (BYTE*)&sTrade, sizeof(TRADE_CHANGE_STATE_SYNC));
  92. NPC_SET_MENU_STATE_SYNC sSync;
  93. sSync.ProtocolType = s2c_npcsetmenustate;
  94. sSync.m_dwID = Npc[Player[nPlayerIdx].m_nIndex].m_dwID;
  95. sSync.m_btState = PLAYER_MENU_STATE_NORMAL;
  96. sSync.m_wLength = sizeof(NPC_SET_MENU_STATE_SYNC) - 1 - sizeof(sSync.m_szSentence);
  97. Npc[Player[nPlayerIdx].m_nIndex].SendDataToNearRegion((LPVOID)&sSync, sSync.m_wLength + 1);
  98. }
  99. break;
  100. case PLAYER_MENU_STATE_TRADEOPEN:
  101. {
  102. TRADE_CHANGE_STATE_SYNC sTrade;
  103. sTrade.ProtocolType = s2c_tradechangestate;
  104. sTrade.m_btState = 1;
  105. g_pServer->PackDataToClient(Player[nPlayerIdx].m_nNetConnectIdx, (BYTE*)&sTrade, sizeof(TRADE_CHANGE_STATE_SYNC));
  106. NPC_SET_MENU_STATE_SYNC sSync;
  107. sSync.ProtocolType = s2c_npcsetmenustate;
  108. sSync.m_dwID = Npc[Player[nPlayerIdx].m_nIndex].m_dwID;
  109. sSync.m_btState = PLAYER_MENU_STATE_TRADEOPEN;
  110. memset(sSync.m_szSentence, 0, sizeof(sSync.m_szSentence));
  111. memcpy(sSync.m_szSentence, lpszSentence, nSentenceLength);
  112. sSync.m_wLength = sizeof(NPC_SET_MENU_STATE_SYNC) - 1 - sizeof(sSync.m_szSentence) + nSentenceLength;
  113. Npc[Player[nPlayerIdx].m_nIndex].SendDataToNearRegion((LPVOID)&sSync, sSync.m_wLength + 1);
  114. }
  115. break;
  116. case PLAYER_MENU_STATE_TRADING:
  117. {
  118. // 给自己发消息
  119. TRADE_CHANGE_STATE_SYNC sState;
  120. sState.ProtocolType = s2c_tradechangestate;
  121. sState.m_btState = 2;
  122. sState.m_dwNpcID = dwDestNpcID;
  123. g_pServer->PackDataToClient(Player[nPlayerIdx].m_nNetConnectIdx, (BYTE*)&sState, sizeof(TRADE_CHANGE_STATE_SYNC));
  124. // 给附近玩家发消息
  125. NPC_SET_MENU_STATE_SYNC sSync;
  126. sSync.ProtocolType = s2c_npcsetmenustate;
  127. sSync.m_btState = PLAYER_MENU_STATE_TRADING;
  128. sSync.m_wLength = sizeof(NPC_SET_MENU_STATE_SYNC) - 1 - sizeof(sSync.m_szSentence);
  129. sSync.m_dwID = Npc[Player[nPlayerIdx].m_nIndex].m_dwID;
  130. Npc[Player[nPlayerIdx].m_nIndex].SendDataToNearRegion((LPVOID)&sSync, sSync.m_wLength + 1);
  131. }
  132. break;
  133. }
  134. }
  135. #endif
  136. #ifdef _SERVER
  137. void KPlayerMenuState::RestoreBackupState(int nPlayerIdx)
  138. {
  139. _ASSERT(m_nBackState != PLAYER_MENU_STATE_TRADING);
  140. if (this->m_nState != PLAYER_MENU_STATE_TRADING)
  141. return;
  142. switch (this->m_nBackState)
  143. {
  144. case PLAYER_MENU_STATE_NORMAL:
  145. case PLAYER_MENU_STATE_TEAMOPEN:
  146. this->m_nBackState = PLAYER_MENU_STATE_NORMAL;
  147. this->m_nState = PLAYER_MENU_STATE_NORMAL;
  148. this->m_szBackSentence[0] = 0;
  149. this->m_szSentence[0] = 0;
  150. this->SetState(nPlayerIdx, PLAYER_MENU_STATE_NORMAL);
  151. break;
  152. case PLAYER_MENU_STATE_TRADEOPEN:
  153. {
  154. char szBuffer[MAX_SENTENCE_LENGTH];
  155. memcpy(szBuffer, m_szBackSentence, sizeof(szBuffer));
  156. this->m_nState = PLAYER_MENU_STATE_NORMAL;
  157. this->m_szSentence[0] = 0;
  158. this->SetState(nPlayerIdx, PLAYER_MENU_STATE_TRADEOPEN, szBuffer, strlen(szBuffer));
  159. }
  160. break;
  161. }
  162. }
  163. #endif
  164. #ifndef _SERVER
  165. void KPlayerMenuState::SetState(int nState)
  166. {
  167. if (nState < PLAYER_MENU_STATE_NORMAL || nState >= PLAYER_MENU_STATE_NUM)
  168. return;
  169. m_nState = nState;
  170. }
  171. #endif
  172. BOOL KPlayerMenuStateGraph::Init()
  173. {
  174. KTabFile cFile;
  175. // g_SetFilePath("\");
  176. if( !cFile.Load(PLAYER_MENU_STATE_RES_FILE) )
  177. return FALSE;
  178. m_szName[0][0] = 0;
  179. for (int i = 1; i < PLAYER_MENU_STATE_NUM; i++)
  180. {
  181. cFile.GetString(i + 1, 2, "", m_szName[i], sizeof(m_szName[i]));
  182. }
  183. return TRUE;
  184. }
  185. void KPlayerMenuStateGraph::GetStateSpr(int nState, char *lpszGetName)
  186. {
  187. if ( !lpszGetName )
  188. return;
  189. if (nState < 0 || nState >= PLAYER_MENU_STATE_NUM)
  190. {
  191. lpszGetName[0] = 0;
  192. return;
  193. }
  194. g_StrCpy(lpszGetName, m_szName[nState]);
  195. }