Factions.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include "Player.h"
  19. extern MasterFactionList master_faction_list;
  20. extern ConfigReader configReader;
  21. PlayerFaction::PlayerFaction(){
  22. MFactionUpdateNeeded.SetName("PlayerFaction::MFactionUpdateNeeded");
  23. }
  24. sint32 PlayerFaction::GetMaxValue(sint8 con){
  25. if(con < 0)
  26. return con * 10000;
  27. else
  28. return (con * 10000) + 9999;
  29. }
  30. sint32 PlayerFaction::GetMinValue(sint8 con){
  31. if(con <= 0)
  32. return (con * 10000) - 9999;
  33. else
  34. return (con * 10000);
  35. }
  36. bool PlayerFaction::ShouldAttack(int32 faction_id){
  37. return (GetCon(faction_id) <= -4);
  38. }
  39. sint8 PlayerFaction::GetCon(int32 faction_id){
  40. if(faction_id <= 10){
  41. if(faction_id == 0)
  42. return 0;
  43. return (faction_id-5);
  44. }
  45. sint32 value = GetFactionValue(faction_id);
  46. if(value >= -9999 && value <= 9999)
  47. return 0;
  48. else{
  49. if(value>= 40000)
  50. return 4;
  51. else if(value <= -40000)
  52. return -4;
  53. return (sint8)(value/10000);
  54. }
  55. }
  56. int8 PlayerFaction::GetPercent(int32 faction_id){
  57. if(faction_id <= 10)
  58. return 0;
  59. sint8 con = GetCon(faction_id);
  60. sint32 value = GetFactionValue(faction_id);
  61. if(con != 0){
  62. if(value <= 0)
  63. value *= -1;
  64. if(con < 0)
  65. con *= -1;
  66. value -= con * 10000;
  67. value *= 100;
  68. return value / 10000;
  69. }
  70. else{
  71. value += 10000;
  72. value *= 100;
  73. return value / 20000;
  74. }
  75. }
  76. EQ2Packet* PlayerFaction::FactionUpdate(int16 version){
  77. EQ2Packet* ret = 0;
  78. Faction* faction = 0;
  79. PacketStruct* packet = configReader.getStruct("WS_FactionUpdate", version);
  80. MFactionUpdateNeeded.lock();
  81. if(packet){
  82. packet->setArrayLengthByName("num_factions", faction_update_needed.size());
  83. for(int32 i=0;i<faction_update_needed.size();i++){
  84. faction = master_faction_list.GetFaction(faction_update_needed[i]);
  85. if(faction){
  86. packet->setArrayDataByName("faction_id", faction->id, i);
  87. packet->setArrayDataByName("name", faction->name.c_str(), i);
  88. packet->setArrayDataByName("description", faction->description.c_str(), i);
  89. packet->setArrayDataByName("category", faction->type.c_str(), i);
  90. packet->setArrayDataByName("con", GetCon(faction->id), i);
  91. packet->setArrayDataByName("percentage", GetPercent(faction->id), i);
  92. packet->setArrayDataByName("value", GetFactionValue(faction->id), i);
  93. }
  94. }
  95. ret = packet->serialize();
  96. safe_delete(packet);
  97. }
  98. faction_update_needed.clear();
  99. MFactionUpdateNeeded.unlock();
  100. return ret;
  101. }
  102. sint32 PlayerFaction::GetFactionValue(int32 faction_id){
  103. if(faction_id <= 10)
  104. return 0;
  105. //devn00b: This always seems to return 1, even if the player infact has no faction. since we handle this via a check in zoneserver.cpp (processfaction)
  106. //if(faction_values.count(faction_id) == 0)
  107. //return master_faction_list.GetDefaultFactionValue(faction_id); //faction_values[faction_id] = master_faction_list.GetDefaultFactionValue(faction_id);
  108. return faction_values[faction_id];
  109. }
  110. bool PlayerFaction::ShouldIncrease(int32 faction_id){
  111. if(faction_id <= 10 || master_faction_list.GetIncreaseAmount(faction_id) == 0)
  112. return false;
  113. return true;
  114. }
  115. bool PlayerFaction::ShouldDecrease(int32 faction_id){
  116. if(faction_id <= 10 || master_faction_list.GetDecreaseAmount(faction_id) == 0)
  117. return false;
  118. return true;
  119. }
  120. bool PlayerFaction::IncreaseFaction(int32 faction_id, int32 amount){
  121. if(faction_id <= 10)
  122. return true;
  123. bool ret = true;
  124. if(amount == 0)
  125. amount = master_faction_list.GetIncreaseAmount(faction_id);
  126. faction_values[faction_id] += amount;
  127. if(faction_values[faction_id] >= 50000){
  128. faction_values[faction_id] = 50000;
  129. ret = false;
  130. }
  131. MFactionUpdateNeeded.lock();
  132. faction_update_needed.push_back(faction_id);
  133. MFactionUpdateNeeded.unlock();
  134. return ret;
  135. }
  136. bool PlayerFaction::DecreaseFaction(int32 faction_id, int32 amount){
  137. if(faction_id <= 10)
  138. return true;
  139. bool ret = true;
  140. if(amount == 0)
  141. amount = master_faction_list.GetDecreaseAmount(faction_id);
  142. if(amount != 0){
  143. faction_values[faction_id] -= amount;
  144. if(faction_values[faction_id] <= -50000){
  145. faction_values[faction_id] = -50000;
  146. ret = false;
  147. }
  148. }
  149. else
  150. ret = false;
  151. MFactionUpdateNeeded.lock();
  152. faction_update_needed.push_back(faction_id);
  153. MFactionUpdateNeeded.unlock();
  154. return ret;
  155. }
  156. bool PlayerFaction::SetFactionValue(int32 faction_id, sint32 value){
  157. faction_values[faction_id] = value;
  158. MFactionUpdateNeeded.lock();
  159. faction_update_needed.push_back(faction_id);
  160. MFactionUpdateNeeded.unlock();
  161. return true;
  162. }