ChestTrap.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "ChestTrap.h"
  2. #include <vector>
  3. #include <string.h>
  4. //required for c++17 compat (random_shuffle removed, replaced with shuffle)
  5. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
  6. #include <random>
  7. #else
  8. #include <algorithm> // std::random_shuffle
  9. #endif
  10. int32 ChestTrapList::Size() {
  11. MChestTrapList.readlock(__FUNCTION__, __LINE__);
  12. int32 size = chesttrap_list.size();
  13. MChestTrapList.releasereadlock(__FUNCTION__, __LINE__);
  14. return size;
  15. }
  16. void ChestTrapList::AddChestTrap(ChestTrap* trap) {
  17. if (trap->GetDBID() < 1)
  18. return;
  19. MChestTrapList.writelock(__FUNCTION__, __LINE__);
  20. if (chesttrap_list.count(trap->GetDBID()) > 0)
  21. {
  22. ChestTrap* tmpTrap = chesttrap_list[trap->GetDBID()];
  23. chesttrap_list.erase(trap->GetDBID());
  24. safe_delete(tmpTrap);
  25. }
  26. chesttrap_list[trap->GetDBID()] = trap;
  27. MChestTrapList.releasewritelock(__FUNCTION__, __LINE__);
  28. }
  29. bool ChestTrapList::GetChestTrap(int32 id, ChestTrap::ChestTrapInfo* cti) {
  30. ChestTrap* res = 0;
  31. MChestTrapList.readlock(__FUNCTION__, __LINE__);
  32. if (chesttrap_list.count(id) > 0)
  33. res = chesttrap_list[id];
  34. memset(cti, 0, sizeof(ChestTrap::ChestTrapInfo));
  35. if (res)
  36. memcpy(cti, res->GetChestTrapInfo(), sizeof(ChestTrap::ChestTrapInfo));
  37. MChestTrapList.releasereadlock(__FUNCTION__, __LINE__);
  38. return cti;
  39. }
  40. bool ChestTrapList::GetNextTrap(int32 zoneid, int32 chest_difficulty, ChestTrap::ChestTrapInfo* cti)
  41. {
  42. MChestListsInUse.writelock(__FUNCTION__, __LINE__);
  43. ChestTrapList* zoneTrapList = GetChestListByZone(zoneid);
  44. ChestTrapList* zoneDifficultyTrapList = zoneTrapList->GetChestListByDifficulty(chest_difficulty);
  45. bool ret = zoneTrapList->GetNextChestTrap(cti);
  46. MChestListsInUse.releasewritelock(__FUNCTION__, __LINE__);
  47. return ret;
  48. }
  49. void ChestTrapList::Clear() {
  50. MChestListsInUse.writelock(__FUNCTION__, __LINE__);
  51. ClearTraps();
  52. ClearTrapList();
  53. MChestListsInUse.releasewritelock(__FUNCTION__, __LINE__);
  54. }
  55. bool ChestTrapList::GetNextChestTrap(ChestTrap::ChestTrapInfo* cti) {
  56. MChestTrapList.readlock(__FUNCTION__, __LINE__);
  57. if (cycleItr == chesttrap_list.end())
  58. {
  59. MChestTrapList.releasereadlock(__FUNCTION__, __LINE__);
  60. //re-shuffle the map, we reached the end
  61. shuffleMap(this);
  62. }
  63. else
  64. MChestTrapList.releasereadlock(__FUNCTION__, __LINE__);
  65. if (cycleItr == chesttrap_list.end())
  66. return false;
  67. MChestTrapList.writelock(__FUNCTION__, __LINE__);
  68. ChestTrap* trap = cycleItr->second;
  69. memset(cti, 0, sizeof(ChestTrap::ChestTrapInfo));
  70. if (trap)
  71. memcpy(cti, trap->GetChestTrapInfo(), sizeof(ChestTrap::ChestTrapInfo));
  72. cycleItr++;
  73. MChestTrapList.releasewritelock(__FUNCTION__, __LINE__);
  74. return true;
  75. }
  76. ChestTrapList* ChestTrapList::GetChestListByDifficulty(int32 difficulty) {
  77. ChestTrapList* usedList = 0;
  78. int32 id = 0;
  79. if (ChestTrapParent)
  80. {
  81. usedList = GetChestTrapList(ChestTrapBaseList::DIFFICULTY);
  82. id = ChestTrapBaseList::DIFFICULTY;
  83. }
  84. else
  85. {
  86. usedList = GetChestTrapListByID(difficulty);
  87. id = difficulty;
  88. }
  89. if (usedList && usedList->IsListLoaded())
  90. return usedList;
  91. else if (!usedList)
  92. {
  93. usedList = new ChestTrapList();
  94. AddChestTrapList(usedList, id);
  95. }
  96. MChestTrapList.writelock(__FUNCTION__, __LINE__);
  97. map<int32, ChestTrap*>::iterator itr;
  98. for (itr = chesttrap_list.begin(); itr != chesttrap_list.end(); itr++) {
  99. ChestTrap* curTrap = itr->second;
  100. if ((curTrap->GetMinChestDifficulty() <= difficulty && difficulty <= curTrap->GetMaxChestDifficulty()) ||
  101. (curTrap->GetMinChestDifficulty() == 0 && curTrap->GetMaxChestDifficulty() == 0))
  102. usedList->AddChestTrap(curTrap);
  103. }
  104. shuffleMap(usedList);
  105. usedList->SetListLoaded(true);
  106. MChestTrapList.releasewritelock(__FUNCTION__, __LINE__);
  107. return usedList;
  108. }
  109. ChestTrapList* ChestTrapList::GetChestListByZone(int32 zoneid) {
  110. ChestTrapList* usedList = 0;
  111. int32 id = 0;
  112. if (ChestTrapParent)
  113. {
  114. usedList = GetChestTrapList(ChestTrapBaseList::ZONE);
  115. id = ChestTrapBaseList::ZONE;
  116. }
  117. else
  118. {
  119. usedList = GetChestTrapListByID(zoneid);
  120. id = zoneid;
  121. }
  122. if (usedList && usedList->IsListLoaded())
  123. return usedList;
  124. else if (!usedList)
  125. {
  126. usedList = new ChestTrapList();
  127. AddChestTrapList(usedList, id);
  128. }
  129. MChestTrapList.writelock(__FUNCTION__, __LINE__);
  130. map<int32, ChestTrap*>::iterator itr;
  131. for (itr = chesttrap_list.begin(); itr != chesttrap_list.end(); itr++) {
  132. ChestTrap* curTrap = itr->second;
  133. if (curTrap->GetApplicableZoneID() == zoneid || curTrap->GetApplicableZoneID() == -1)
  134. usedList->AddChestTrap(curTrap);
  135. }
  136. shuffleMap(usedList);
  137. usedList->SetListLoaded(true);
  138. MChestTrapList.releasewritelock(__FUNCTION__, __LINE__);
  139. return usedList;
  140. }
  141. map<int32, ChestTrap*>* ChestTrapList::GetAllChestTraps() { return &chesttrap_list; }
  142. bool ChestTrapList::IsListLoaded() { return ListLoaded; }
  143. void ChestTrapList::SetListLoaded(bool val) { ListLoaded = val; }
  144. void ChestTrapList::AddChestTrapList(ChestTrapList* traplist, int32 id) {
  145. if (chesttrap_innerlist.count(id) > 0)
  146. {
  147. ChestTrapList* tmpTrapList = chesttrap_innerlist[id];
  148. chesttrap_innerlist.erase(id);
  149. safe_delete(tmpTrapList);
  150. }
  151. chesttrap_innerlist[id] = traplist;
  152. }
  153. ChestTrapList* ChestTrapList::GetChestTrapList(ChestTrapBaseList listName) {
  154. ChestTrapList* ctl = 0;
  155. MChestTrapInnerList.readlock(__FUNCTION__, __LINE__);
  156. if (chesttrap_innerlist.count(listName) > 0)
  157. ctl = chesttrap_innerlist[listName];
  158. MChestTrapInnerList.releasereadlock(__FUNCTION__, __LINE__);
  159. return ctl;
  160. }
  161. ChestTrapList* ChestTrapList::GetChestTrapListByID(int32 id) {
  162. ChestTrapList* ctl = 0;
  163. MChestTrapInnerList.readlock(__FUNCTION__, __LINE__);
  164. if (chesttrap_innerlist.count(id) > 0)
  165. ctl = chesttrap_innerlist[id];
  166. MChestTrapInnerList.releasereadlock(__FUNCTION__, __LINE__);
  167. return ctl;
  168. }
  169. void ChestTrapList::ClearTraps() {
  170. MChestTrapList.writelock(__FUNCTION__, __LINE__);
  171. map<int32, ChestTrap*>::iterator itr;
  172. for (itr = chesttrap_list.begin(); itr != chesttrap_list.end(); itr++)
  173. safe_delete(itr->second);
  174. chesttrap_list.clear();
  175. MChestTrapList.releasewritelock(__FUNCTION__, __LINE__);
  176. }
  177. void ChestTrapList::ClearTrapList() {
  178. MChestTrapInnerList.writelock(__FUNCTION__, __LINE__);
  179. map<int32, ChestTrapList*>::iterator itr2;
  180. for (itr2 = chesttrap_innerlist.begin(); itr2 != chesttrap_innerlist.end(); itr2++)
  181. safe_delete(itr2->second);
  182. chesttrap_innerlist.clear();
  183. MChestTrapInnerList.releasewritelock(__FUNCTION__, __LINE__);
  184. // reinstantiate the base lists (zone/difficulty/etc)
  185. InstantiateLists(ChestTrapParent);
  186. }
  187. void ChestTrapList::SetupMutexes()
  188. {
  189. MChestTrapList.SetName("ChestTrapList");
  190. MChestTrapInnerList.SetName("MChestTrapInnerList");
  191. MChestListsInUse.SetName("MChestListsInUse");
  192. }
  193. void ChestTrapList::InstantiateLists(bool parent)
  194. {
  195. if (parent)
  196. {
  197. difficultyList = new ChestTrapList(false);
  198. zoneList = new ChestTrapList(false);
  199. MChestTrapInnerList.writelock(__FUNCTION__, __LINE__);
  200. chesttrap_innerlist[ChestTrapBaseList::DIFFICULTY] = difficultyList;
  201. chesttrap_innerlist[ChestTrapBaseList::ZONE] = zoneList;
  202. MChestTrapInnerList.releasewritelock(__FUNCTION__, __LINE__);
  203. }
  204. }
  205. void ChestTrapList::shuffleMap(ChestTrapList* list) {
  206. std::vector<ChestTrap*> tmp_chests;
  207. map<int32, ChestTrap*>::iterator itr;
  208. for (itr = chesttrap_list.begin(); itr != chesttrap_list.end(); itr++) {
  209. ChestTrap* curTrap = itr->second;
  210. tmp_chests.push_back(curTrap);
  211. }
  212. #ifdef WIN32
  213. //c++17/windows removed random_shuffle replaced with this ugly bullshit 9/22/22
  214. //taken right from their example.
  215. std::random_device rd;
  216. std::mt19937 g(rd());
  217. std::shuffle(tmp_chests.begin(), tmp_chests.end(),g);
  218. #else
  219. //let linux continue on, with the function as is since it still works.
  220. std::random_shuffle(tmp_chests.begin(), tmp_chests.end());
  221. #endif
  222. chesttrap_list.clear();
  223. int count = 0;
  224. for (std::vector<ChestTrap*>::iterator it = tmp_chests.begin(); it != tmp_chests.end(); ++it)
  225. {
  226. chesttrap_list[count] = *it;
  227. count++;
  228. }
  229. cycleItr = chesttrap_list.begin();
  230. }