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

模拟服务器

开发平台:

C/C++

  1. #ifndef KMISSION_H
  2. #define KMISSION_H
  3. #ifdef _SERVER
  4. #include "KCore.h"
  5. #include "KEngine.h"
  6. #include "KLinkArrayTemplate.h"
  7. #include "KTaskFuns.h"
  8. #include "KPlayerDef.h"
  9. #include "KSubWorld.h"
  10. #include "KSubWorldSet.h"
  11. #include "KMissionArray.h"
  12. #include "KPlayerChat.h"
  13. #include "KSortScript.h"
  14. #include "TaskDef.h"
  15. //Mission
  16. //每个Mission有自已的独立的全局变量
  17. //为每一个Mission记录玩家列表的统一的机制
  18. //包括最大人数,等等都要有
  19. //加个Chanel,有利于消息发送
  20. //有功能可以编列所有的参加者id
  21. extern int g_PlayerTimerCallBackFun(void * pOwner, char * szScriptFileName);
  22. extern int g_MissionTimerCallBackFun(void * pOwner, char * szScriptFileName);
  23. #define MAX_TIMER_PERMISSION 3
  24. typedef struct 
  25. {
  26. unsigned long m_ulPlayerIndex;
  27. unsigned long m_ulPlayerID;
  28. unsigned char m_ucPlayerGroup;
  29. unsigned long m_ulJoinTime;
  30. int   m_nParam1;
  31. int   m_nParam2;
  32. }TMissionPlayerInfo;
  33. //管理玩家集合的模板类
  34. template<class T , unsigned long ulSize>
  35. class _KMissionPlayerArray:public KLinkArrayTemplate<T , ulSize>
  36. {
  37. public:
  38. unsigned long FindSame(T *pT)
  39. {
  40. int nIdx = 0;
  41. while(1)
  42. {
  43. nIdx = m_UseIdx.GetNext(nIdx);
  44. if (!nIdx)
  45. break;
  46. if (pT->m_ulPlayerIndex == m_Data[nIdx].m_ulPlayerIndex)
  47. return nIdx;
  48. }
  49. return 0;
  50. };
  51. int GetParam1(unsigned long ulIndex) const
  52. {
  53. if (ulIndex >= ulSize) 
  54. return 0;
  55. return m_Data[ulIndex].m_nParam1;
  56. };
  57. BOOL SetParam1(unsigned long ulIndex, int nParam1)
  58. {
  59. if (ulIndex >= ulSize) 
  60. return FALSE;
  61. m_Data[ulIndex].m_nParam1 =  nParam1;
  62. return TRUE;
  63. };
  64. int GetParam2(unsigned long ulIndex) const
  65. {
  66. if (ulIndex >= ulSize) 
  67. return 0;
  68. return m_Data[ulIndex].m_nParam2;
  69. };
  70. BOOL SetParam2(unsigned long ulIndex, int nParam2)
  71. {
  72. if (ulIndex >= ulSize) 
  73. return FALSE;
  74. m_Data[ulIndex].m_nParam2 =  nParam2;
  75. return TRUE;
  76. };
  77. };
  78. typedef _KMissionPlayerArray<TMissionPlayerInfo, MAX_PLAYER> KMissionPlayerArray;
  79. //管理Mission时间触发器集合的模板类
  80. typedef KTimerFunArray <KTimerTaskFun, MAX_TIMER_PERMISSION, g_MissionTimerCallBackFun> KMissionTimerArray;
  81. #define MAX_MISSION_VALUE_COUNT 100
  82. class KMission
  83. {
  84. int m_MissionValue[MAX_MISSION_VALUE_COUNT];
  85. unsigned long m_ulMissionId;
  86. void * m_pOwner;
  87. public:
  88. KMissionTimerArray m_cTimerTaskSet;
  89. KMission()
  90. {
  91. m_pOwner = NULL;
  92. for (int i = 1; i < MAX_TIMER_PERMISSION; i ++)
  93. {
  94. KTimerTaskFun * pTimer = (KTimerTaskFun*)m_cTimerTaskSet.GetData(i);
  95. if (pTimer)
  96. pTimer->SetOwner(this);
  97. }
  98. memset(m_MissionValue, 0, sizeof(m_MissionValue));
  99. m_ulMissionId = 0;
  100. };
  101. BOOL Activate();
  102. void SetOwner(void * pOwner)
  103. {
  104. m_pOwner = pOwner;
  105. };
  106. void * GetOwner()
  107. {
  108. return m_pOwner;
  109. };
  110. void Init()
  111. {
  112. memset(m_MissionValue, 0, sizeof(m_MissionValue));
  113. m_ulMissionId = 0;
  114. };
  115. BOOL SetMissionId(unsigned long ulMissionId)
  116. {
  117. m_ulMissionId = ulMissionId;
  118. return TRUE;
  119. }
  120. unsigned long GetMissionId()
  121. {
  122. return m_ulMissionId;
  123. }
  124. unsigned long Msg2Group(const char * strMsg, unsigned char ucGroup)
  125. {
  126. if (!strMsg || !strMsg[0]) 
  127. return 0;
  128. int nCount = 0;
  129. int nIdx = 0;
  130. while(1)
  131. {
  132. nIdx = m_MissionPlayer.m_UseIdx.GetNext(nIdx);
  133. if (!nIdx)
  134. break;
  135. if (m_MissionPlayer.m_Data[nIdx].m_ucPlayerGroup == ucGroup)
  136. {
  137. KPlayerChat::SendSystemInfo(1, m_MissionPlayer.m_Data[nIdx].m_ulPlayerIndex, MESSAGE_SYSTEM_ANNOUCE_HEAD, (char *) strMsg, strlen(strMsg));
  138. nCount ++;
  139. }
  140. }
  141. return nCount;
  142. }
  143. unsigned long Msg2All(const char * strMsg)
  144. {
  145. if (!strMsg || !strMsg[0]) 
  146. return 0;
  147. int nCount = 0;
  148. int nIdx = 0;
  149. while(1)
  150. {
  151. nIdx = m_MissionPlayer.m_UseIdx.GetNext(nIdx);
  152. if (!nIdx)
  153. break;
  154. KPlayerChat::SendSystemInfo(1, m_MissionPlayer.m_Data[nIdx].m_ulPlayerIndex, MESSAGE_SYSTEM_ANNOUCE_HEAD, (char *) strMsg, strlen(strMsg));
  155. nCount ++;
  156. }
  157. return nCount;
  158. }
  159. unsigned long Msg2Player(const char * strMsg, unsigned long ulPlayerIndex)
  160. {
  161. if (!strMsg || !strMsg[0]) 
  162. return 0;
  163. KPlayerChat::SendSystemInfo(1, ulPlayerIndex, MESSAGE_SYSTEM_ANNOUCE_HEAD, (char *) strMsg, strlen(strMsg));
  164. };
  165. unsigned long GetNextPlayer(unsigned long ulIdx, unsigned char ucGroup, unsigned long &ulPlayerIndex)
  166. {
  167. ulPlayerIndex = 0;
  168. while(1)
  169. {
  170. ulIdx = m_MissionPlayer.m_UseIdx.GetNext(ulIdx);
  171. if (ulIdx == 0) 
  172. return 0;
  173. if (ucGroup)
  174. {
  175. if (m_MissionPlayer.m_Data[ulIdx].m_ucPlayerGroup == ucGroup)
  176. {
  177. ulPlayerIndex = m_MissionPlayer.m_Data[ulIdx].m_ulPlayerIndex;
  178. return ulIdx;
  179. }
  180. }
  181. else
  182. {
  183. ulPlayerIndex = m_MissionPlayer.m_Data[ulIdx].m_ulPlayerIndex;
  184. return ulIdx;
  185. }
  186. }
  187. return 0;
  188. }
  189. unsigned long GetPlayerCount() const
  190. return m_MissionPlayer.GetUsedCount();
  191. };
  192. void SetMissionValue(unsigned long ulValueId, int nValue)
  193. {
  194. if (ulValueId >= MAX_MISSION_VALUE_COUNT)
  195. return ;
  196. m_MissionValue[ulValueId] = nValue;
  197. };
  198. unsigned long GetGroupPlayerCount (unsigned char ucGroup ) const 
  199. {
  200. if (ucGroup == 0)
  201.  return GetPlayerCount();
  202. unsigned long ulGroupPlayerCount = 0;
  203. int nIdx = 0;
  204. while(1)
  205. {
  206. nIdx = m_MissionPlayer.m_UseIdx.GetNext(nIdx);
  207. if (!nIdx)
  208. break;
  209. if (m_MissionPlayer.m_Data[nIdx].m_ucPlayerGroup == ucGroup)
  210. ulGroupPlayerCount ++;
  211. }
  212. return ulGroupPlayerCount;
  213. };
  214. int GetMissionValue(unsigned long ulValueId) const
  215. {
  216. if (ulValueId >= MAX_MISSION_VALUE_COUNT)
  217. return 0;
  218. return m_MissionValue[ulValueId];
  219. };
  220. BOOL ExecuteScript(char * ScriptFileName, char * szFunName, int nParam);
  221. BOOL ExecuteScript(DWORD dwScriptId,  char * szFunName, int nParam);
  222. BOOL StartTimer(unsigned char ucTimerId, unsigned long ulTimerInterval)
  223. {
  224. KTimerTaskFun Timer;
  225. Timer.SetTimer(ulTimerInterval, ucTimerId);
  226. Timer.SetOwner(this);
  227. m_cTimerTaskSet.Add(&Timer);
  228. return TRUE;
  229. }
  230. BOOL StopTimer(unsigned char ucTimerId)
  231. {
  232. KTimerTaskFun Timer;
  233. Timer.SetTimer(1, ucTimerId);
  234. unsigned long ulIndex = 0;
  235. KTimerTaskFun * pTimer = m_cTimerTaskSet.GetData(&Timer);
  236. if (pTimer)
  237. pTimer->CloseTimer();
  238. }
  239. DWORD GetTimerRestTimer(unsigned char ucTimerId)
  240. {
  241. KTimerTaskFun Timer;
  242. Timer.SetTimer(1, ucTimerId);
  243. unsigned long ulIndex = 0;
  244. KTimerTaskFun * pTimer = m_cTimerTaskSet.GetData(&Timer);
  245. if (pTimer)
  246. return pTimer->GetRestTime();
  247. return 0;
  248. }
  249. BOOL StopMission()
  250. {
  251. m_ulMissionId = 0;
  252. Init();
  253. m_cTimerTaskSet.Clear();
  254. m_MissionPlayer.Clear();
  255. return TRUE;
  256. }
  257. KMissionPlayerArray m_MissionPlayer;
  258. /* unsigned long FindSame(unsigned long ulPlayerIndex )
  259. {
  260. if (ulPlayerIndex >= MAX_PLAYER)
  261. {
  262. return 0;
  263. }
  264. int nIdx = 0;
  265. while(1)
  266. {
  267. nIdx = m_UseIdx.GetNext(nIdx);
  268. if (!nIdx)
  269. break;
  270. if (m_MissionPlayer[nIdx].ulPlayerIndex == ulPlayerIndex)
  271. return nIdx;
  272. }
  273. return 0;
  274. };
  275. */
  276. unsigned long AddPlayer(unsigned long ulPlayerIndex, unsigned long ulPlayerID, unsigned char ucPlayerGroup, int ulJoinTime = 0, int nParam1 = 0, int nParam2 = 0)
  277. {
  278. if (ulPlayerIndex >= MAX_PLAYER|| ulPlayerID == 0)
  279. return 0;
  280. unsigned long i = 0;
  281. TMissionPlayerInfo * pInfo = m_MissionPlayer.FindFree(i);
  282. if (!i || !pInfo)
  283. return 0;
  284. pInfo->m_ulPlayerIndex = ulPlayerIndex;
  285. pInfo->m_ulPlayerID = ulPlayerID;
  286. pInfo->m_nParam1 = nParam1;
  287. pInfo->m_nParam2 = nParam2;
  288. pInfo->m_ucPlayerGroup = ucPlayerGroup;
  289. if (!ulJoinTime)
  290. pInfo->m_ulJoinTime = g_SubWorldSet.GetGameTime();
  291. else
  292. pInfo->m_ulJoinTime = ulJoinTime;
  293. m_MissionPlayer.m_FreeIdx.Remove(i);
  294. m_MissionPlayer.m_UseIdx.Insert(i);
  295. m_MissionPlayer.m_ulFreeSize --;
  296. return i;
  297. };
  298. BOOL RemovePlayer(unsigned long ulPlayerIndex, unsigned long ulPlayerID = 0)
  299. {
  300. if (ulPlayerIndex >= MAX_PLAYER)
  301. return 0;
  302. TMissionPlayerInfo Info;
  303. Info.m_ulPlayerIndex = ulPlayerIndex;
  304. Info.m_ulPlayerID = ulPlayerID;
  305. if (m_MissionPlayer.Remove(&Info))
  306. {
  307. if (m_ulMissionId)
  308. {
  309. char szScript[MAX_PATH];
  310. g_MissionTabFile.GetString(m_ulMissionId + 1, 2, "", szScript, MAX_PATH);
  311. if (szScript[0])
  312. {
  313. ExecuteScript(szScript, "OnLeave", ulPlayerIndex);
  314. }
  315. }
  316. }
  317. return TRUE;
  318. };
  319. unsigned long GetMissionPlayer_DataIndex(unsigned long ulPlayerIndex)//
  320. {
  321. if (ulPlayerIndex >= MAX_PLAYER)
  322. return 0;
  323. TMissionPlayerInfo Info;
  324. Info.m_ulPlayerIndex = ulPlayerIndex;
  325. return m_MissionPlayer.FindSame(&Info);
  326. }
  327. unsigned long GetMissionPlayer_PlayerIndex(unsigned long ulDataIndex)
  328. {
  329. if (ulDataIndex > m_MissionPlayer.GetTotalCount())
  330. return 0;
  331. return m_MissionPlayer.m_Data[ulDataIndex].m_ulPlayerIndex;
  332. }
  333. unsigned long GetMissionPlayer_GroupId(unsigned long ulDataIndex)
  334. {
  335. if (ulDataIndex > m_MissionPlayer.GetTotalCount())
  336. return 0;
  337. return m_MissionPlayer.m_Data[ulDataIndex].m_ucPlayerGroup;
  338. }
  339. };
  340. #endif
  341. #endif