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

模拟服务器

开发平台:

C/C++

  1. //---------------------------------------------------------------------------
  2. // Sword3 Engine (c) 1999-2000 by Kingsoft
  3. //
  4. // File: KNpcDeathCalcExp.cpp
  5. // Date: 2003.07.21
  6. // Code: 边城浪子
  7. // Desc: KNpcDeathCalcExp Class
  8. //---------------------------------------------------------------------------
  9. #include "KCore.h"
  10. #include "KNpc.h"
  11. #include "KPlayerDef.h"
  12. #include "KPlayer.h"
  13. #include "KNpcDeathCalcExp.h"
  14. void KNpcDeathCalcExp::Init(int nNpcIdx)
  15. {
  16. m_nNpcIdx = (nNpcIdx > 0 ? nNpcIdx : 0);
  17. memset(m_sCalcInfo, 0, sizeof(m_sCalcInfo));
  18. }
  19. void KNpcDeathCalcExp::Active()
  20. {
  21. if (Npc[m_nNpcIdx].m_Kind != kind_normal)
  22. return;
  23. for (int i = 0; i < defMAX_CALC_EXP_NUM; i++)
  24. {
  25. if (m_sCalcInfo[i].m_nAttackIdx <= 0)
  26. continue;
  27. m_sCalcInfo[i].m_nTime--;
  28. if (m_sCalcInfo[i].m_nTime <= 0)
  29. {
  30. m_sCalcInfo[i].m_nTime = 0;
  31. m_sCalcInfo[i].m_nAttackIdx = 0;
  32. m_sCalcInfo[i].m_nTotalDamage = 0;
  33. }
  34. }
  35. }
  36. #ifdef _SERVER
  37. void KNpcDeathCalcExp::AddDamage(int nPlayerIdx, int nDamage)
  38. {
  39. if (Npc[m_nNpcIdx].m_Kind != kind_normal)
  40. return;
  41. if (nPlayerIdx <= 0 || nPlayerIdx >= MAX_PLAYER || Player[nPlayerIdx].m_nIndex <= 0)
  42. return;
  43. if (Player[nPlayerIdx].m_cTeam.m_nFlag)
  44. {
  45. nPlayerIdx = g_Team[Player[nPlayerIdx].m_cTeam.m_nID].m_nCaptain;
  46. if (nPlayerIdx <= 0 || nPlayerIdx >= MAX_PLAYER || Player[nPlayerIdx].m_nIndex <= 0)
  47. return;
  48. }
  49. int i;
  50. for (i = 0; i < defMAX_CALC_EXP_NUM; i++)
  51. {
  52. if (m_sCalcInfo[i].m_nAttackIdx == nPlayerIdx)
  53. {
  54. this->m_sCalcInfo[i].m_nTotalDamage += nDamage;
  55. this->m_sCalcInfo[i].m_nTime = defMAX_CALC_EXP_TIME;
  56. return;
  57. }
  58. }
  59. for (i = 0; i < defMAX_CALC_EXP_NUM; i++)
  60. {
  61. if (m_sCalcInfo[i].m_nAttackIdx == 0)
  62. break;
  63. }
  64. if (i >= defMAX_CALC_EXP_NUM)
  65. return;
  66. m_sCalcInfo[i].m_nAttackIdx = nPlayerIdx;
  67. m_sCalcInfo[i].m_nTotalDamage = nDamage;
  68. m_sCalcInfo[i].m_nTime = defMAX_CALC_EXP_TIME;
  69. }
  70. int KNpcDeathCalcExp::CalcExp()
  71. {
  72. if (Npc[m_nNpcIdx].m_CurrentLifeMax <= 0)
  73. return 0;
  74. int i, j, nDamage = 0, nMaxPlayer = 0;
  75. for (i = 0; i < defMAX_CALC_EXP_NUM; i++)
  76. {
  77. if (m_sCalcInfo[i].m_nAttackIdx <= 0)
  78. continue;
  79. // 如果组队
  80. if (Player[m_sCalcInfo[i].m_nAttackIdx].m_cTeam.m_nFlag && Player[m_sCalcInfo[i].m_nAttackIdx].m_cTeam.m_nID >= 0)
  81. {
  82. int nTeam, nPlayer, nDistance, nMinDistance;
  83. nPlayer = 0;
  84. nMinDistance = PLAYER_SHARE_EXP_DISTANCE * PLAYER_SHARE_EXP_DISTANCE;
  85. nTeam = Player[m_sCalcInfo[i].m_nAttackIdx].m_cTeam.m_nID;
  86. // 队长
  87. nDistance = KNpcSet::GetDistanceSquare(m_nNpcIdx, Player[g_Team[nTeam].m_nCaptain].m_nIndex);
  88. if (nDistance >= 0 && nDistance < nMinDistance)
  89. {
  90. nMinDistance = nDistance;
  91. nPlayer = g_Team[nTeam].m_nCaptain;
  92. }
  93. // 队员
  94. for (j = 0; j < MAX_TEAM_MEMBER; j++)
  95. {
  96. if (g_Team[nTeam].m_nMember[j] <= 0)
  97. continue;
  98. nDistance = KNpcSet::GetDistanceSquare(m_nNpcIdx, Player[g_Team[nTeam].m_nMember[j]].m_nIndex);
  99. if (nDistance >= 0 && nDistance < nMinDistance)
  100. {
  101. nMinDistance = nDistance;
  102. nPlayer = g_Team[nTeam].m_nMember[j];
  103. }
  104. }
  105. if (nPlayer > 0)
  106. {
  107. if (m_sCalcInfo[i].m_nTotalDamage > nDamage)
  108. {
  109. nDamage = m_sCalcInfo[i].m_nTotalDamage;
  110. nMaxPlayer = nPlayer;
  111. }
  112. Player[nPlayer].AddExp(m_sCalcInfo[i].m_nTotalDamage * Npc[m_nNpcIdx].m_Experience / Npc[m_nNpcIdx].m_CurrentLifeMax, Npc[m_nNpcIdx].m_Level);
  113. }
  114. }
  115. // 如果没有组队
  116. else
  117. {
  118. int nDistance = KNpcSet::GetDistanceSquare(m_nNpcIdx, Player[m_sCalcInfo[i].m_nAttackIdx].m_nIndex);
  119. if (nDistance >= PLAYER_SHARE_EXP_DISTANCE * PLAYER_SHARE_EXP_DISTANCE)
  120. continue;
  121. if (m_sCalcInfo[i].m_nTotalDamage > nDamage)
  122. {
  123. nDamage = m_sCalcInfo[i].m_nTotalDamage;
  124. nMaxPlayer = m_sCalcInfo[i].m_nAttackIdx;
  125. }
  126. Player[m_sCalcInfo[i].m_nAttackIdx].AddExp(m_sCalcInfo[i].m_nTotalDamage * Npc[m_nNpcIdx].m_Experience / Npc[m_nNpcIdx].m_CurrentLifeMax, Npc[m_nNpcIdx].m_Level);
  127. }
  128. }
  129. Clear();
  130. return nMaxPlayer;
  131. }
  132. #endif
  133. void KNpcDeathCalcExp::Clear()
  134. {
  135. memset(m_sCalcInfo, 0, sizeof(m_sCalcInfo));
  136. }