ChestTrap.cpp 7.2 KB

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