Factions.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "Factions.h"
  17. #include "client.h"
  18. extern MasterFactionList master_faction_list;
  19. extern ConfigReader configReader;
  20. PlayerFaction::PlayerFaction(){
  21. MFactionUpdateNeeded.SetName("PlayerFaction::MFactionUpdateNeeded");
  22. }
  23. sint32 PlayerFaction::GetMaxValue(sint8 con){
  24. if(con < 0)
  25. return con * 10000;
  26. else
  27. return (con * 10000) + 9999;
  28. }
  29. sint32 PlayerFaction::GetMinValue(sint8 con){
  30. if(con <= 0)
  31. return (con * 10000) - 9999;
  32. else
  33. return (con * 10000);
  34. }
  35. bool PlayerFaction::ShouldAttack(int32 faction_id){
  36. return (GetCon(faction_id) <= -4);
  37. }
  38. sint8 PlayerFaction::GetCon(int32 faction_id){
  39. if(faction_id <= 10){
  40. if(faction_id == 0)
  41. return 0;
  42. return (faction_id-5);
  43. }
  44. sint32 value = GetFactionValue(faction_id);
  45. if(value >= -9999 && value <= 9999)
  46. return 0;
  47. else{
  48. if(value>= 40000)
  49. return 4;
  50. else if(value <= -40000)
  51. return -4;
  52. return (sint8)(value/10000);
  53. }
  54. }
  55. int8 PlayerFaction::GetPercent(int32 faction_id){
  56. if(faction_id <= 10)
  57. return 0;
  58. sint8 con = GetCon(faction_id);
  59. sint32 value = GetFactionValue(faction_id);
  60. if(con != 0){
  61. if(value <= 0)
  62. value *= -1;
  63. if(con < 0)
  64. con *= -1;
  65. value -= con * 10000;
  66. value *= 100;
  67. return value / 10000;
  68. }
  69. else{
  70. value += 10000;
  71. value *= 100;
  72. return value / 20000;
  73. }
  74. }
  75. EQ2Packet* PlayerFaction::FactionUpdate(int16 version){
  76. EQ2Packet* ret = 0;
  77. Faction* faction = 0;
  78. PacketStruct* packet = configReader.getStruct("WS_FactionUpdate", version);
  79. MFactionUpdateNeeded.lock();
  80. if(packet){
  81. packet->setArrayLengthByName("num_factions", faction_update_needed.size());
  82. for(int32 i=0;i<faction_update_needed.size();i++){
  83. faction = master_faction_list.GetFaction(faction_update_needed[i]);
  84. if(faction){
  85. packet->setArrayDataByName("faction_id", faction->id, i);
  86. packet->setArrayDataByName("name", faction->name.c_str(), i);
  87. packet->setArrayDataByName("description", faction->description.c_str(), i);
  88. packet->setArrayDataByName("category", faction->type.c_str(), i);
  89. packet->setArrayDataByName("con", GetCon(faction->id), i);
  90. packet->setArrayDataByName("percentage", GetPercent(faction->id), i);
  91. packet->setArrayDataByName("value", GetFactionValue(faction->id), i);
  92. }
  93. }
  94. ret = packet->serialize();
  95. safe_delete(packet);
  96. }
  97. faction_update_needed.clear();
  98. MFactionUpdateNeeded.unlock();
  99. return ret;
  100. }
  101. sint32 PlayerFaction::GetFactionValue(int32 faction_id){
  102. if(faction_id <= 10)
  103. return 0;
  104. if(faction_values.count(faction_id) == 0)
  105. return master_faction_list.GetDefaultFactionValue(faction_id); //faction_values[faction_id] = master_faction_list.GetDefaultFactionValue(faction_id);
  106. return faction_values[faction_id];
  107. }
  108. bool PlayerFaction::ShouldIncrease(int32 faction_id){
  109. if(faction_id <= 10 || master_faction_list.GetIncreaseAmount(faction_id) == 0)
  110. return false;
  111. return true;
  112. }
  113. bool PlayerFaction::ShouldDecrease(int32 faction_id){
  114. if(faction_id <= 10 || master_faction_list.GetDecreaseAmount(faction_id) == 0)
  115. return false;
  116. return true;
  117. }
  118. bool PlayerFaction::IncreaseFaction(int32 faction_id, int32 amount){
  119. if(faction_id <= 10)
  120. return true;
  121. bool ret = true;
  122. if(amount == 0)
  123. amount = master_faction_list.GetIncreaseAmount(faction_id);
  124. faction_values[faction_id] += amount;
  125. if(faction_values[faction_id] >= 50000){
  126. faction_values[faction_id] = 50000;
  127. ret = false;
  128. }
  129. MFactionUpdateNeeded.lock();
  130. faction_update_needed.push_back(faction_id);
  131. MFactionUpdateNeeded.unlock();
  132. return ret;
  133. }
  134. bool PlayerFaction::DecreaseFaction(int32 faction_id, int32 amount){
  135. if(faction_id <= 10)
  136. return true;
  137. bool ret = true;
  138. if(amount == 0)
  139. amount = master_faction_list.GetDecreaseAmount(faction_id);
  140. if(amount != 0){
  141. faction_values[faction_id] -= amount;
  142. if(faction_values[faction_id] <= -50000){
  143. faction_values[faction_id] = -50000;
  144. ret = false;
  145. }
  146. }
  147. else
  148. ret = false;
  149. MFactionUpdateNeeded.lock();
  150. faction_update_needed.push_back(faction_id);
  151. MFactionUpdateNeeded.unlock();
  152. return ret;
  153. }
  154. bool PlayerFaction::SetFactionValue(int32 faction_id, sint32 value){
  155. faction_values[faction_id] = value;
  156. MFactionUpdateNeeded.lock();
  157. faction_update_needed.push_back(faction_id);
  158. MFactionUpdateNeeded.unlock();
  159. return true;
  160. }