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

模拟服务器

开发平台:

C/C++

  1. //#include <objbase.h>
  2. //#include <crtdbg.h>
  3. #include "KCore.h"
  4. #include "KPlayer.h"
  5. #include "KNpc.h"
  6. #include "KNpcSet.h"
  7. #include "KSubWorld.h"
  8. #include "GameDataDef.h"
  9. #include "KProtocolProcess.h"
  10. #ifdef _SERVER
  11. //#include "KNetServer.h"
  12. //#include "../MultiServer/Heaven/Interface/iServer.h"
  13. #include "KSubWorldSet.h"
  14. #include "CoreServerShell.h"
  15. #endif
  16. #include "KPlayerSet.h"
  17. #include "Text.h"
  18. #include "CoreUseNameDef.h"
  19. #define PLAYER_FIRST_LUCKY 0
  20. KPlayerSet PlayerSet;
  21. KPlayerSet::KPlayerSet()
  22. {
  23. #ifdef _SERVER
  24. m_pWelcomeMsg = NULL;
  25. m_ulNextSaveTime = 0;
  26. m_ulDelayTimePerSave = 0;
  27. m_ulMaxSaveTimePerPlayer = 0;
  28. #endif
  29. }
  30. BOOL KPlayerSet::Init()
  31. {
  32. int i;
  33. #ifdef _SERVER
  34. m_nNumPlayer = 0;
  35. m_ulNextSaveTime = 0;
  36. m_ulMaxSaveTimePerPlayer = 60 * 20 * 30;
  37. m_ulDelayTimePerSave = m_ulMaxSaveTimePerPlayer / MAX_PLAYER;
  38. #endif
  39. // 优化查找表
  40. m_FreeIdx.Init(MAX_PLAYER);
  41. m_UseIdx.Init(MAX_PLAYER);
  42. // 开始时所有的数组元素都为空
  43. for (i = MAX_PLAYER - 1; i > 0; i--)
  44. {
  45. m_FreeIdx.Insert(i);
  46. }
  47. if ( !m_cLevelAdd.Init() )
  48. return FALSE;
  49. if ( !m_cLeadExp.Init() )
  50. return FALSE;
  51. for (i = 0; i < MAX_PLAYER; i++)
  52. {
  53. Player[i].Release();
  54. Player[i].SetPlayerIndex(i);
  55. #ifdef _SERVER
  56. Player[i].m_cPK.Init(i);
  57. #endif
  58. Player[i].m_cTong.Init(i);
  59. Player[i].m_ItemList.Init(i);
  60. Player[i].m_Node.m_nIndex = i;
  61. }
  62. if ( !m_cNewPlayerAttribute.Init() )
  63. return FALSE;
  64. #ifdef _SERVER
  65. KTabFile cPKParam;
  66. memset(m_sPKPunishParam, 0, sizeof(m_sPKPunishParam));
  67. // g_SetFilePath("\");
  68. if (!cPKParam.Load(defPK_PUNISH_FILE))
  69. return FALSE;
  70. for (i = 0; i < MAX_DEATH_PUNISH_PK_VALUE + 1; i++)
  71. {
  72. cPKParam.GetInteger(i + 2, 2, 1, &m_sPKPunishParam[i].m_nExp);
  73. cPKParam.GetInteger(i + 2, 3, 1, &m_sPKPunishParam[i].m_nMoney);
  74. cPKParam.GetInteger(i + 2, 4, 1, &m_sPKPunishParam[i].m_nItem);
  75. cPKParam.GetInteger(i + 2, 5, 1, &m_sPKPunishParam[i].m_nEquip);
  76. }
  77. #endif
  78. // 帮会参数
  79. KIniFile cTongFile;
  80. if (cTongFile.Load(defPLAYER_TONG_PARAM_FILE))
  81. {
  82. cTongFile.GetInteger("TongCreate", "Level", 60, &m_sTongParam.m_nLevel);
  83. cTongFile.GetInteger("TongCreate", "LeadLevel", 10, &m_sTongParam.m_nLeadLevel);
  84. cTongFile.GetInteger("TongCreate", "Money", 500000, &m_sTongParam.m_nMoney);
  85. }
  86. else
  87. {
  88. m_sTongParam.m_nLevel = 60;
  89. m_sTongParam.m_nLeadLevel = 10;
  90. m_sTongParam.m_nMoney = 500000;
  91. }
  92. return TRUE;
  93. }
  94. int KPlayerSet::FindFree()
  95. {
  96. return m_FreeIdx.GetNext(0);
  97. }
  98. int KPlayerSet::FindSame(DWORD dwID)
  99. {
  100. int nUseIdx = 0;
  101. nUseIdx = m_UseIdx.GetNext(0);
  102. while(nUseIdx)
  103. {
  104. if (Player[nUseIdx].m_dwID == dwID)
  105. return nUseIdx;
  106. nUseIdx = m_UseIdx.GetNext(nUseIdx);
  107. }
  108. return 0;
  109. }
  110. int KPlayerSet::GetFirstPlayer()
  111. {
  112. m_nListCurIdx = m_UseIdx.GetNext(0);
  113. return m_nListCurIdx;
  114. }
  115. int KPlayerSet::GetNextPlayer()
  116. {
  117. if ( !m_nListCurIdx )
  118. return 0;
  119. m_nListCurIdx = m_UseIdx.GetNext(m_nListCurIdx);
  120. return m_nListCurIdx;
  121. }
  122. #ifdef _SERVER
  123. int KPlayerSet::Add(LPSTR szPlayerID, void* pGuid)
  124. {
  125. if (!pGuid || !szPlayerID || !szPlayerID[0])
  126. return 0;
  127. int i;
  128. /*
  129. DWORD dwID = g_FileName2Id(szPlayerID);
  130. i = FindSame(dwID);
  131. if (i)
  132. return 0;
  133. */
  134. DWORD dwID = g_FileName2Id(szPlayerID);
  135. i = FindFree();
  136. if (i)
  137. {
  138. Player[i].m_dwID = dwID;
  139. Player[i].m_nNetConnectIdx = -1;
  140. Player[i].m_dwLoginTime = g_SubWorldSet.GetGameTime();
  141. memcpy(&Player[i].m_Guid, pGuid, sizeof(GUID));
  142. Player[i].SetPlayerIndex(i);
  143. m_FreeIdx.Remove(i);
  144. m_UseIdx.Insert(i);
  145. m_nNumPlayer ++;
  146. return i;
  147. }
  148. return 0;
  149. }
  150. int KPlayerSet::Broadcasting(char* pMessage, int nLen)
  151. {
  152. if ( !pMessage || nLen <= 0 || nLen >= MAX_SENTENCE_LENGTH)
  153. return 0;
  154. if (!g_pServer)
  155. return 0;
  156. g_pServer->PreparePackSink();
  157. KPlayerChat::SendSystemInfo(0, 0, MESSAGE_SYSTEM_ANNOUCE_HEAD, pMessage, nLen);
  158. g_pServer->SendPackToClient(-1);
  159. return 1;
  160. }
  161. void KPlayerSet::SendWelcomeMsg(int nPlayerIndex)
  162. {
  163. //====通知有人加入====
  164. char szBuffer[MAX_SENTENCE_LENGTH];
  165. int nLength;
  166. static int nMsg = 0;
  167. const char* Msgs[3] =
  168. { "我<color=green>%s<color=restore>来也,你等快来朝拜。",
  169. "<color=green>%s<color=restore>脚踏凌波微步,飘然而至。",
  170. "一道闪光,<color=green>%s<color=restore>从天而降。" };
  171. memset(szBuffer, 0, sizeof(szBuffer));
  172. sprintf(szBuffer, Msgs[nMsg], Player[nPlayerIndex].m_PlayerName);
  173. nMsg++;
  174. nMsg %= 3;
  175. nLength = TEncodeText(szBuffer, strlen(szBuffer));
  176. KPlayerChat::SendSystemInfo(0, 0, MESSAGE_SYSTEM_ANNOUCE_HEAD, szBuffer, nLength);
  177. //====发送欢迎消息
  178. if (m_pWelcomeMsg)
  179. {
  180. int *pAMsg = (int*)m_pWelcomeMsg;
  181. int nCount =  *pAMsg;
  182. pAMsg ++;
  183. for (int i = 0; i < nCount; i++)
  184. {
  185. int nLen = *pAMsg;
  186. KPlayerChat::SendSystemInfo(1, nPlayerIndex, "吴府管家", (((char*)pAMsg) + sizeof(int)), nLen);
  187. pAMsg = (int*)(((char*)pAMsg) + nLen + sizeof(int));
  188. }
  189. }
  190. }
  191. void KPlayerSet::ReloadWelcomeMsg()
  192. {
  193. return;
  194. /*    欢迎信息文本文件格式要求:
  195. 每行为一条信息。  Msg:信息内容
  196. 控制信息内容的长度,使每条信息经过编码後长度不大于MAX_SENTENCE_LENGTH
  197. */
  198. if (m_pWelcomeMsg)
  199. {
  200. free (m_pWelcomeMsg);
  201. m_pWelcomeMsg = NULL;
  202. }
  203. KFile File;
  204. char Head[8];
  205. if (File.Open("\Msg\WelcomeMsg.txt") == FALSE)
  206. return;
  207. int nLen = File.Size();
  208. m_pWelcomeMsg = malloc(nLen + sizeof(int));
  209. if (m_pWelcomeMsg == NULL)
  210. return;
  211. *(int*)m_pWelcomeMsg = 0;
  212. char* pSearchBegin = (char*)m_pWelcomeMsg + sizeof(int);
  213. int* pLineHeader = (int*)pSearchBegin;
  214. File.Read(pLineHeader, nLen);
  215. File.Close();
  216. char* pMsgHeader;
  217. while(pMsgHeader = (char*)memchr(pSearchBegin, ':', nLen))
  218. {
  219. pMsgHeader++; //跳过那个冒号
  220. nLen -= pMsgHeader -pSearchBegin; //冒号后的剩余长度
  221. if (nLen <= 0)
  222. break;
  223. int nSkipLen = pMsgHeader - (char*)pLineHeader - sizeof(int);
  224. pSearchBegin = pMsgHeader;
  225. if (nSkipLen < 0)
  226. continue;
  227. if (nSkipLen > 0)
  228. memset(&pLineHeader[1], 0, nSkipLen);
  229. char* pMsgTail = (char*)memchr(pSearchBegin, 0x0d, nLen); //查找行结尾
  230. int nMsgLen;
  231. if (pMsgTail)
  232. {
  233. *pMsgTail = 0;
  234. pMsgTail++;
  235. if ((pMsgTail < pSearchBegin + nLen) && *pMsgTail == 0x0a)
  236. {
  237. *pMsgTail = 0;
  238. pMsgTail++;
  239. }
  240. nMsgLen = pMsgTail - pSearchBegin;
  241. pSearchBegin = pMsgTail;
  242. }
  243. else
  244. {
  245. nMsgLen = nLen;
  246. }
  247. nLen -= nMsgLen;
  248. nMsgLen = TEncodeText((char*)&pLineHeader[1], nMsgLen + nSkipLen);
  249. if (nMsgLen > 0)
  250. {
  251. *pLineHeader = nMsgLen;
  252. pLineHeader = (int*)(((char*)pLineHeader) + sizeof(int) + (*pLineHeader));
  253. (*(int*)m_pWelcomeMsg) ++;
  254. }
  255. if (pMsgTail == NULL || nLen <= 0)
  256. break;
  257. }
  258. if ((int*)m_pWelcomeMsg == 0)
  259. {
  260. free(m_pWelcomeMsg);
  261. m_pWelcomeMsg = NULL;
  262. }
  263. }
  264. void KPlayerSet::PrepareRemove(int nIndex)
  265. {
  266. if (nIndex <= 0 || nIndex >= MAX_PLAYER)
  267. return;
  268. g_DebugLog("SERVER:Player[%s] has been removed!", Npc[Player[nIndex].m_nIndex].Name);
  269. // 通知聊天好友自己下线了
  270. Player[nIndex].m_cChat.OffLine(Player[nIndex].m_dwID);
  271. // 如果组队,离开队伍
  272. PLAYER_APPLY_LEAVE_TEAM sLeaveTeam;
  273. sLeaveTeam.ProtocolType = c2s_teamapplyleave;
  274. Player[nIndex].LeaveTeam((BYTE*)&sLeaveTeam);
  275. // 如果交易正在进行,取消交易
  276. TRADE_DECISION_COMMAND sTrade;
  277. sTrade.ProtocolType = c2s_tradedecision;
  278. sTrade.m_btDecision = 0;
  279. Player[nIndex].TradeDecision((BYTE*)&sTrade);
  280. // PK状态取消
  281. Player[nIndex].m_cPK.CloseAll();
  282. // Player[nIndex].Save();
  283. Player[nIndex].WaitForRemove();
  284. }
  285. void KPlayerSet::PrepareLoginFailed(int nIndex)
  286. {
  287. if (nIndex <= 0 || nIndex >= MAX_PLAYER)
  288. return;
  289. g_DebugLog("SERVER:Player[%s] has been removed!", Npc[Player[nIndex].m_nIndex].Name);
  290. // 通知聊天好友自己下线了
  291. Player[nIndex].m_cChat.OffLine(Player[nIndex].m_dwID);
  292. // 如果组队,离开队伍
  293. PLAYER_APPLY_LEAVE_TEAM sLeaveTeam;
  294. sLeaveTeam.ProtocolType = c2s_teamapplyleave;
  295. Player[nIndex].LeaveTeam((BYTE*)&sLeaveTeam);
  296. // 如果交易正在进行,取消交易
  297. TRADE_DECISION_COMMAND sTrade;
  298. sTrade.ProtocolType = c2s_tradedecision;
  299. sTrade.m_btDecision = 0;
  300. Player[nIndex].TradeDecision((BYTE*)&sTrade);
  301. // PK状态取消
  302. Player[nIndex].m_cPK.CloseAll();
  303. // 帮会状态处理
  304. Player[nIndex].m_cTong.Clear();
  305. // 利用TimeOut来清Player
  306. Player[nIndex].m_nNetConnectIdx = -1;
  307. Player[nIndex].m_dwLoginTime = 0;
  308. }
  309. void KPlayerSet::PrepareExchange(int i)
  310. {
  311. if (!Player[i].m_bExchangeServer || !Npc[Player[i].m_nIndex].m_bExchangeServer)
  312. return;
  313. // 清除PK状态
  314. Player[i].m_cPK.CloseAll();
  315. // 离队
  316. PLAYER_APPLY_LEAVE_TEAM sLeaveTeam;
  317. sLeaveTeam.ProtocolType = c2s_teamapplyleave;
  318. Player[i].LeaveTeam((BYTE*)&sLeaveTeam);
  319. // 如果交易正在进行,取消交易
  320. TRADE_DECISION_COMMAND sTrade;
  321. sTrade.ProtocolType = c2s_tradedecision;
  322. sTrade.m_btDecision = 0;
  323. Player[i].TradeDecision((BYTE*)&sTrade);
  324. Player[i].Save();
  325. // Player[i].UpdateEnterGamePos(Player[i].m_sExchangePos.m_dwMapID,
  326. // Player[i].m_sExchangePos.m_nX,
  327. // Player[i].m_sExchangePos.m_nY,
  328. // Npc[Player[i].m_nIndex].m_FightMode);
  329. }
  330. void KPlayerSet::RemoveQuiting(int nIndex)
  331. {
  332. if (Player[nIndex].m_nNetConnectIdx == -1 || Player[nIndex].m_dwID == 0)
  333. return;
  334. if (Player[nIndex].IsWaitingRemove())
  335. {
  336. if (Player[nIndex].m_nIndex > 0) // have npc
  337. {
  338. int nRegion = Npc[Player[nIndex].m_nIndex].m_RegionIndex;
  339. int nSubWorld = Npc[Player[nIndex].m_nIndex].m_SubWorldIndex;
  340. if (nSubWorld >= 0 && nRegion >= 0)
  341. {
  342. SubWorld[nSubWorld].RemovePlayer(nRegion, nIndex);
  343. SubWorld[nSubWorld].m_MissionArray.RemovePlayer(nIndex);
  344. SubWorld[nSubWorld].m_Region[nRegion].RemoveNpc(Player[nIndex].m_nIndex);
  345. SubWorld[nSubWorld].m_Region[nRegion].DecRef(Npc[Player[nIndex].m_nIndex].m_MapX, Npc[Player[nIndex].m_nIndex].m_MapY, obj_npc);
  346. }
  347. NpcSet.Remove(Player[nIndex].m_nIndex);
  348. }
  349. // 帮会状态处理
  350. Player[nIndex].m_cTong.Clear();
  351. Player[nIndex].m_ItemList.RemoveAll();
  352. Player[nIndex].m_dwID = 0;
  353. Player[nIndex].m_nIndex = 0;
  354. Player[nIndex].m_nNetConnectIdx = -1;
  355. Player[nIndex].Release();
  356. m_FreeIdx.Insert(nIndex);
  357. m_UseIdx.Remove(nIndex);
  358. m_nNumPlayer --;
  359. }
  360. }
  361. void KPlayerSet::RemoveLoginTimeOut(int nIndex)
  362. {
  363. if (Player[nIndex].IsLoginTimeOut())
  364. {
  365. if (Player[nIndex].m_nIndex > 0)
  366. {
  367. int nRegion = Npc[Player[nIndex].m_nIndex].m_RegionIndex;
  368. int nSubWorld = Npc[Player[nIndex].m_nIndex].m_SubWorldIndex;
  369. /* if (nSubWorld >= 0)
  370. SubWorld[nSubWorld].RemovePlayer(nRegion, nIndex);*/
  371. if (nSubWorld >= 0 && nRegion >= 0)
  372. {
  373. SubWorld[nSubWorld].m_Region[nRegion].RemoveNpc(Player[nIndex].m_nIndex);
  374. SubWorld[nSubWorld].m_Region[nRegion].DecRef(Npc[Player[nIndex].m_nIndex].m_MapX, Npc[Player[nIndex].m_nIndex].m_MapY, obj_npc);
  375. }
  376. NpcSet.Remove(Player[nIndex].m_nIndex);
  377. }
  378. Player[nIndex].m_ItemList.RemoveAll();
  379. Player[nIndex].m_dwID = 0;
  380. Player[nIndex].m_nIndex = 0;
  381. Player[nIndex].m_nNetConnectIdx = -1;
  382. Player[nIndex].LoginTimeOut();
  383. m_FreeIdx.Insert(nIndex);
  384. m_UseIdx.Remove(nIndex);
  385. m_nNumPlayer --;
  386. }
  387. }
  388. void KPlayerSet::RemoveExchanging(int nIndex)
  389. {
  390. if (Player[nIndex].m_nNetConnectIdx == -1)
  391. return;
  392. if (Player[nIndex].IsExchangingServer())
  393. {
  394. int nRegion = Npc[Player[nIndex].m_nIndex].m_RegionIndex;
  395. int nSubWorld = Npc[Player[nIndex].m_nIndex].m_SubWorldIndex;
  396. if (nSubWorld >= 0 && nRegion >= 0)
  397. {
  398. SubWorld[nSubWorld].RemovePlayer(nRegion, nIndex);
  399. SubWorld[nSubWorld].m_MissionArray.RemovePlayer(nIndex);
  400. SubWorld[nSubWorld].m_Region[nRegion].RemoveNpc(Player[nIndex].m_nIndex);
  401. SubWorld[nSubWorld].m_Region[nRegion].DecRef(Npc[Player[nIndex].m_nIndex].m_MapX, Npc[Player[nIndex].m_nIndex].m_MapY, obj_npc);
  402. }
  403. if (Player[nIndex].m_nIndex > 0)
  404. NpcSet.Remove(Player[nIndex].m_nIndex);
  405. Player[nIndex].m_ItemList.RemoveAll();
  406. Player[nIndex].m_dwID = 0;
  407. Player[nIndex].m_nIndex = 0;
  408. Player[nIndex].m_nNetConnectIdx = -1;
  409. Player[nIndex].Release();
  410. m_FreeIdx.Insert(nIndex);
  411. m_UseIdx.Remove(nIndex);
  412. m_nNumPlayer --;
  413. }
  414. }
  415. #endif
  416. /*
  417. int KPlayerSet::FindClient(int nClient)
  418. {
  419. int i;
  420. i = m_UseIdx.GetNext(0);
  421. while(i)
  422. {
  423. if (Player[i].m_nNetConnectIdx == nClient)
  424. return i;
  425. i = m_UseIdx.GetNext(i);
  426. }
  427. return 0;
  428. }
  429. */
  430. #ifdef _SERVER
  431. void KPlayerSet::ProcessClientMessage(int nIndex, const char* pChar, int nSize)
  432. {
  433. if (nIndex <= 0 && nIndex >= MAX_PLAYER)
  434. return;
  435. int i = Player[nIndex].m_nNetConnectIdx;
  436. if (i >= 0)
  437. {
  438. // _ASSERT(g_CoreServerShell.CheckProtocolSize(pChar, nSize));
  439. if (Player[nIndex].m_dwID && !Player[nIndex].m_bExchangeServer)
  440. g_ProtocolProcess.ProcessNetMsg(nIndex, (BYTE*)pChar);
  441. }
  442. }
  443. #endif
  444. //---------------------------------------------------------------------------
  445. // 功能:构造函数
  446. //---------------------------------------------------------------------------
  447. KLevelAdd::KLevelAdd()
  448. {
  449. memset(m_nLevelExp, 0, sizeof(int) * MAX_LEVEL);
  450. memset(m_nLifePerLevel, 0, sizeof(int) * series_num);
  451. memset(m_nManaPerLevel, 0, sizeof(int) * series_num);
  452. memset(m_nStaminaPerLevel, 0, sizeof(int) * series_num);
  453. memset(m_nLifePerVitality, 0, sizeof(int) * series_num);
  454. memset(m_nStaminaPerVitality, 0, sizeof(int) * series_num);
  455. memset(m_nManaPerEnergy, 0, sizeof(int) * series_num);
  456. }
  457. //---------------------------------------------------------------------------
  458. // 功能:初始化
  459. //---------------------------------------------------------------------------
  460. BOOL KLevelAdd::Init()
  461. {
  462. int i;
  463. KTabFile LevelExp;
  464. if ( !LevelExp.Load(PLAYER_LEVEL_EXP_FILE) )
  465. return FALSE;
  466. for (i = 0; i < MAX_LEVEL; i++)
  467. {
  468. LevelExp.GetInteger(i + 2, 2, 0, &m_nLevelExp[i]);
  469. }
  470. KTabFile LevelAdd;
  471. if ( !LevelAdd.Load(PLAYER_LEVEL_ADD_FILE) )
  472. return FALSE;
  473. for (i = 0; i < series_num; i++)
  474. {
  475. LevelAdd.GetInteger(i + 2, 2, 0, &m_nLifePerLevel[i]);
  476. LevelAdd.GetInteger(i + 2, 3, 0, &m_nStaminaPerLevel[i]);
  477. LevelAdd.GetInteger(i + 2, 4, 0, &m_nManaPerLevel[i]);
  478. LevelAdd.GetInteger(i + 2, 5, 0, &m_nLifePerVitality[i]);
  479. LevelAdd.GetInteger(i + 2, 6, 0, &m_nStaminaPerVitality[i]);
  480. LevelAdd.GetInteger(i + 2, 7, 0, &m_nManaPerEnergy[i]);
  481. LevelAdd.GetInteger(i + 2, 8, 0, &m_nLeadExpShare[i]);
  482. LevelAdd.GetInteger(i + 2, 9, 0, &m_nFireResistPerLevel[i]);
  483. LevelAdd.GetInteger(i + 2, 10, 0, &m_nColdResistPerLevel[i]);
  484. LevelAdd.GetInteger(i + 2, 11, 0, &m_nPoisonResistPerLevel[i]);
  485. LevelAdd.GetInteger(i + 2, 12, 0, &m_nLightResistPerLevel[i]);
  486. LevelAdd.GetInteger(i + 2, 13, 0, &m_nPhysicsResistPerLevel[i]);
  487. }
  488. return TRUE;
  489. }
  490. //---------------------------------------------------------------------------
  491. // 功能:获得某等级的升级经验
  492. // 参数:nLevel  目标等级
  493. //---------------------------------------------------------------------------
  494. int KLevelAdd::GetLevelExp(int nLevel)
  495. {
  496. if (nLevel < 1 || nLevel > MAX_LEVEL)
  497. return 0;
  498. return m_nLevelExp[nLevel - 1];
  499. }
  500. //---------------------------------------------------------------------------
  501. // 功能:获得每个系升级加生命点
  502. //---------------------------------------------------------------------------
  503. int KLevelAdd::GetLifePerLevel(int nSeries)
  504. {
  505. if (nSeries < 0 || nSeries >= series_num)
  506. return 0;
  507. return m_nLifePerLevel[nSeries];
  508. }
  509. //---------------------------------------------------------------------------
  510. // 功能:获得每个系升级加体力点
  511. //---------------------------------------------------------------------------
  512. int KLevelAdd::GetStaminaPerLevel(int nSeries)
  513. {
  514. if (nSeries < 0 || nSeries >= series_num)
  515. return 0;
  516. return m_nStaminaPerLevel[nSeries];
  517. }
  518. //---------------------------------------------------------------------------
  519. // 功能:获得每个系升级加内力点
  520. //---------------------------------------------------------------------------
  521. int KLevelAdd::GetManaPerLevel(int nSeries)
  522. {
  523. if (nSeries < 0 || nSeries >= series_num)
  524. return 0;
  525. return m_nManaPerLevel[nSeries];
  526. }
  527. //---------------------------------------------------------------------------
  528. // 功能:每个系活力点增加一点后生命点增长
  529. //---------------------------------------------------------------------------
  530. int KLevelAdd::GetLifePerVitality(int nSeries)
  531. {
  532. if (nSeries < 0 || nSeries >= series_num)
  533. return 0;
  534. return m_nLifePerVitality[nSeries];
  535. }
  536. //---------------------------------------------------------------------------
  537. // 功能:每个系活力点增加一点后体力点增长
  538. //---------------------------------------------------------------------------
  539. int KLevelAdd::GetStaminaPerVitality(int nSeries)
  540. {
  541. if (nSeries < 0 || nSeries >= series_num)
  542. return 0;
  543. return m_nStaminaPerVitality[nSeries];
  544. }
  545. //---------------------------------------------------------------------------
  546. // 功能:每个系精力点增加一点后内力点增长
  547. //---------------------------------------------------------------------------
  548. int KLevelAdd::GetManaPerEnergy(int nSeries)
  549. {
  550. if (nSeries < 0 || nSeries >= series_num)
  551. return 0;
  552. return m_nManaPerEnergy[nSeries];
  553. }
  554. //---------------------------------------------------------------------------
  555. // 功能:每个系精力点增加一点后内力点增长
  556. //---------------------------------------------------------------------------
  557. int KLevelAdd::GetLeadExpShare(int nSeries)
  558. {
  559. if (nSeries < 0 || nSeries >= series_num)
  560. return 0;
  561. return m_nLeadExpShare[nSeries];
  562. }
  563. //---------------------------------------------------------------------------
  564. // 功能:每个系某个等级的基本火抗性
  565. //---------------------------------------------------------------------------
  566. int KLevelAdd::GetFireResist(int nSeries, int nLevel)
  567. {
  568. if (nSeries < 0 || nSeries >= series_num)
  569. return 0;
  570. if (nLevel <= 0 || nLevel >= MAX_LEVEL)
  571. return 0;
  572. return (m_nFireResistPerLevel[nSeries] * nLevel / 100);
  573. }
  574. //---------------------------------------------------------------------------
  575. // 功能:每个系某个等级的基本冰抗性
  576. //---------------------------------------------------------------------------
  577. int KLevelAdd::GetColdResist(int nSeries, int nLevel)
  578. {
  579. if (nSeries < 0 || nSeries >= series_num)
  580. return 0;
  581. if (nLevel <= 0 || nLevel >= MAX_LEVEL)
  582. return 0;
  583. return (m_nColdResistPerLevel[nSeries] * nLevel / 100);
  584. }
  585. //---------------------------------------------------------------------------
  586. // 功能:每个系某个等级的基本毒抗性
  587. //---------------------------------------------------------------------------
  588. int KLevelAdd::GetPoisonResist(int nSeries, int nLevel)
  589. {
  590. if (nSeries < 0 || nSeries >= series_num)
  591. return 0;
  592. if (nLevel <= 0 || nLevel >= MAX_LEVEL)
  593. return 0;
  594. return (m_nPoisonResistPerLevel[nSeries] * nLevel / 100);
  595. }
  596. //---------------------------------------------------------------------------
  597. // 功能:每个系某个等级的基本电抗性
  598. //---------------------------------------------------------------------------
  599. int KLevelAdd::GetLightResist(int nSeries, int nLevel)
  600. {
  601. if (nSeries < 0 || nSeries >= series_num)
  602. return 0;
  603. if (nLevel <= 0 || nLevel >= MAX_LEVEL)
  604. return 0;
  605. return (m_nLightResistPerLevel[nSeries] * nLevel / 100);
  606. }
  607. //---------------------------------------------------------------------------
  608. // 功能:每个系某个等级的基本物理抗性
  609. //---------------------------------------------------------------------------
  610. int KLevelAdd::GetPhysicsResist(int nSeries, int nLevel)
  611. {
  612. if (nSeries < 0 || nSeries >= series_num)
  613. return 0;
  614. if (nLevel <= 0 || nLevel >= MAX_LEVEL)
  615. return 0;
  616. return (m_nPhysicsResistPerLevel[nSeries] * nLevel / 100);
  617. }
  618. //---------------------------------------------------------------------------
  619. // 功能:构造函数
  620. //---------------------------------------------------------------------------
  621. KTeamLeadExp::KTeamLeadExp()
  622. {
  623. for (int i = 0; i < MAX_LEAD_LEVEL; i++)
  624. {
  625. m_sPerLevel[i].m_dwExp = 0;
  626. m_sPerLevel[i].m_dwMemNum = 1;
  627. }
  628. }
  629. //---------------------------------------------------------------------------
  630. // 功能:初始化,读取统率力数据
  631. //---------------------------------------------------------------------------
  632. BOOL KTeamLeadExp::Init()
  633. {
  634. int i;
  635. KTabFile LevelExp;
  636. if ( !LevelExp.Load(PLAYER_LEVEL_LEAD_EXP_FILE) )
  637. return FALSE;
  638. for (i = 0; i < MAX_LEAD_LEVEL; i++)
  639. {
  640. LevelExp.GetInteger(i + 2, 2, 0, (int*)&m_sPerLevel[i].m_dwExp);
  641. LevelExp.GetInteger(i + 2, 3, 1, (int*)&m_sPerLevel[i].m_dwMemNum);
  642. }
  643. return TRUE;
  644. }
  645. //---------------------------------------------------------------------------
  646. // 功能:传入经验获得等级数
  647. //---------------------------------------------------------------------------
  648. int KTeamLeadExp::GetLevel(DWORD dwExp, int nCurLeadLevel)
  649. {
  650. if (dwExp <= 0)
  651. return 1;
  652. if (nCurLeadLevel > 0)
  653. {
  654. if (nCurLeadLevel >= MAX_LEAD_LEVEL)
  655. return MAX_LEAD_LEVEL;
  656. if (dwExp < m_sPerLevel[nCurLeadLevel - 1].m_dwExp)
  657. return nCurLeadLevel;
  658. if (dwExp < m_sPerLevel[nCurLeadLevel].m_dwExp)
  659. return nCurLeadLevel + 1;
  660. }
  661. for (int i = 0; i < MAX_LEAD_LEVEL; i++)
  662. {
  663. if (dwExp < m_sPerLevel[i].m_dwExp)
  664. {
  665. return i + 1;
  666. }
  667. }
  668. return MAX_LEAD_LEVEL;
  669. }
  670. //---------------------------------------------------------------------------
  671. // 功能:传入经验获得可带队员数
  672. //---------------------------------------------------------------------------
  673. int KTeamLeadExp::GetMemNumFromExp(DWORD dwExp)
  674. {
  675. int nGetLevel;
  676. nGetLevel = GetLevel(dwExp);
  677. return m_sPerLevel[nGetLevel - 1].m_dwMemNum;
  678. }
  679. //---------------------------------------------------------------------------
  680. // 功能:传入等级获得可带队员数
  681. //---------------------------------------------------------------------------
  682. int KTeamLeadExp::GetMemNumFromLevel(int nLevel)
  683. {
  684. if (1 <= nLevel && nLevel <= MAX_LEAD_LEVEL)
  685. return m_sPerLevel[nLevel - 1].m_dwMemNum;
  686. return 1;
  687. }
  688. //---------------------------------------------------------------------------
  689. // 功能:传入等级获得升级所需经验值
  690. //---------------------------------------------------------------------------
  691. int KTeamLeadExp::GetLevelExp(int nLevel)
  692. {
  693. if (1 <= nLevel && nLevel <= MAX_LEAD_LEVEL)
  694. return this->m_sPerLevel[nLevel - 1].m_dwExp;
  695. return 0;
  696. }
  697. #ifdef _SERVER
  698. void KPlayerSet::Activate()
  699. {
  700. }
  701. #endif
  702. KNewPlayerAttribute::KNewPlayerAttribute()
  703. {
  704. memset(m_nStrength, 0, sizeof(m_nStrength));
  705. memset(m_nDexterity, 0, sizeof(m_nDexterity));
  706. memset(m_nVitality, 0, sizeof(m_nVitality));
  707. memset(m_nEngergy, 0, sizeof(m_nEngergy));
  708. memset(m_nLucky, 0, sizeof(m_nLucky));
  709. }
  710. BOOL KNewPlayerAttribute::Init()
  711. {
  712. char szSeries[5][16] = {"metal", "wood", "water", "fire", "earth"};
  713. KIniFile AttributeFile;
  714. if ( !AttributeFile.Load(BASE_ATTRIBUTE_FILE_NAME) )
  715. return FALSE;
  716. for (int i = 0; i < series_num; i++)
  717. {
  718. AttributeFile.GetInteger(szSeries[i], "Strength", 0, &m_nStrength[i]);
  719. AttributeFile.GetInteger(szSeries[i], "Dexterity", 0, &m_nDexterity[i]);
  720. AttributeFile.GetInteger(szSeries[i], "Vitality", 0, &m_nVitality[i]);
  721. AttributeFile.GetInteger(szSeries[i], "Engergy", 0, &m_nEngergy[i]);
  722. AttributeFile.GetInteger(szSeries[i], "Lucky", PLAYER_FIRST_LUCKY, &m_nLucky[i]);
  723. }
  724. return TRUE;
  725. }
  726. #ifdef _SERVER
  727. BOOL KPlayerSet::GetPlayerName(int nIndex, char* szName)
  728. {
  729. // int i = FindClient(nClient);
  730. int i = nIndex;
  731. if (!szName)
  732. return FALSE;
  733. if (i <= 0 || i >= MAX_PLAYER)
  734. {
  735. szName[0] = 0;
  736. return FALSE;
  737. }
  738. strcpy(szName, Player[i].m_PlayerName);
  739. return TRUE;
  740. }
  741. #endif
  742. #ifdef _SERVER
  743. BOOL KPlayerSet::GetPlayerAccount(int i, char* szName)
  744. {
  745. // int i = FindClient(nClient);
  746. if (!szName)
  747. return FALSE;
  748. if (i <= 0 || i >= MAX_PLAYER)
  749. {
  750. szName[0] = 0;
  751. return FALSE;
  752. }
  753. strcpy(szName, Player[i].m_AccoutName);
  754. return TRUE;
  755. }
  756. #endif
  757. #ifdef _SERVER
  758. int KPlayerSet::AttachPlayer(const unsigned long lnID, GUID* pGuid)
  759. {
  760. if (lnID >= MAX_PLAYER || NULL == pGuid)
  761. return 0;
  762. int nUseIdx = m_UseIdx.GetPrev(0);
  763. while(nUseIdx)
  764. {
  765. if (Player[nUseIdx].m_nNetConnectIdx == -1)
  766. {
  767. if (0 == memcmp(&Player[nUseIdx].m_Guid, pGuid, sizeof(GUID)))
  768. {
  769. Player[nUseIdx].m_nNetConnectIdx = lnID;
  770. Player[nUseIdx].m_ulLastSaveTime = g_SubWorldSet.m_nLoopRate;
  771. /// Player[nUseIdx].m_dwLoginTime = -1;
  772. return nUseIdx;
  773. }
  774. // else
  775. // {
  776. // g_DebugLog("[error]Attach to a connected player named %s", Player[nUseIdx].m_PlayerName);
  777. // return 0;
  778. // }
  779. }
  780. nUseIdx = m_UseIdx.GetPrev(nUseIdx);
  781. }
  782. return 0;
  783. }
  784. #endif
  785. #ifdef _SERVER
  786. int KPlayerSet::GetPlayerIndexByGuid(GUID* pGuid)
  787. {
  788. int nUseIdx = m_UseIdx.GetNext(0);
  789. while(nUseIdx)
  790. {
  791. if (0 == memcmp(&Player[nUseIdx].m_Guid, pGuid, sizeof(GUID)))
  792. {
  793. if (Player[nUseIdx].m_nNetConnectIdx != -1)
  794. {
  795. return nUseIdx;
  796. }
  797. else
  798. {
  799. g_DebugLog("[error]Find Guid to a disconnect player");
  800. return 0;
  801. }
  802. }
  803. nUseIdx = m_UseIdx.GetNext(nUseIdx);
  804. }
  805. return 0;
  806. }
  807. #endif
  808. #ifdef _SERVER
  809. void KPlayerSet::AutoSave()
  810. {
  811. unsigned long uTime = g_SubWorldSet.GetGameTime();
  812. if (uTime >= m_ulNextSaveTime)
  813. {
  814. int nUseIdx = m_UseIdx.GetNext(0);
  815. while(nUseIdx)
  816. {
  817. if (Player[nUseIdx].CanSave() && uTime - Player[nUseIdx].m_ulLastSaveTime >= m_ulMaxSaveTimePerPlayer)
  818. {
  819. if (Player[nUseIdx].Save())
  820. {
  821. Player[nUseIdx].m_uMustSave = SAVE_REQUEST;
  822. // Player[nUseIdx].m_ulLastSaveTime = uTime;
  823. m_ulNextSaveTime += m_ulDelayTimePerSave;
  824. }
  825. break;
  826. }
  827. nUseIdx = m_UseIdx.GetNext(nUseIdx);
  828. }
  829. }
  830. }
  831. #endif