ChestTrap.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. bool ret = zoneTrapList->GetNextChestTrap(cti);
  41. MChestListsInUse.releasewritelock(__FUNCTION__, __LINE__);
  42. return ret;
  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. if (cycleItr == chesttrap_list.end())
  61. return false;
  62. MChestTrapList.writelock(__FUNCTION__, __LINE__);
  63. ChestTrap* trap = cycleItr->second;
  64. memset(cti, 0, sizeof(ChestTrap::ChestTrapInfo));
  65. if (trap)
  66. memcpy(cti, trap->GetChestTrapInfo(), sizeof(ChestTrap::ChestTrapInfo));
  67. cycleItr++;
  68. MChestTrapList.releasewritelock(__FUNCTION__, __LINE__);
  69. return true;
  70. }
  71. ChestTrapList* ChestTrapList::GetChestListByDifficulty(int32 difficulty) {
  72. ChestTrapList* usedList = 0;
  73. int32 id = 0;
  74. if (ChestTrapParent)
  75. {
  76. usedList = GetChestTrapList(ChestTrapBaseList::DIFFICULTY);
  77. id = ChestTrapBaseList::DIFFICULTY;
  78. }
  79. else
  80. {
  81. usedList = GetChestTrapListByID(difficulty);
  82. id = difficulty;
  83. }
  84. if (usedList && usedList->IsListLoaded())
  85. return usedList;
  86. else if (!usedList)
  87. {
  88. usedList = new ChestTrapList();
  89. AddChestTrapList(usedList, id);
  90. }
  91. MChestTrapList.writelock(__FUNCTION__, __LINE__);
  92. map<int32, ChestTrap*>::iterator itr;
  93. for (itr = chesttrap_list.begin(); itr != chesttrap_list.end(); itr++) {
  94. ChestTrap* curTrap = itr->second;
  95. if ((curTrap->GetMinChestDifficulty() <= difficulty && difficulty <= curTrap->GetMaxChestDifficulty()) ||
  96. (curTrap->GetMinChestDifficulty() == 0 && curTrap->GetMaxChestDifficulty() == 0))
  97. usedList->AddChestTrap(curTrap);
  98. }
  99. shuffleMap(usedList);
  100. usedList->SetListLoaded(true);
  101. MChestTrapList.releasewritelock(__FUNCTION__, __LINE__);
  102. return usedList;
  103. }
  104. ChestTrapList* ChestTrapList::GetChestListByZone(int32 zoneid) {
  105. ChestTrapList* usedList = 0;
  106. int32 id = 0;
  107. if (ChestTrapParent)
  108. {
  109. usedList = GetChestTrapList(ChestTrapBaseList::ZONE);
  110. id = ChestTrapBaseList::ZONE;
  111. }
  112. else
  113. {
  114. usedList = GetChestTrapListByID(zoneid);
  115. id = zoneid;
  116. }
  117. if (usedList && usedList->IsListLoaded())
  118. return usedList;
  119. else if (!usedList)
  120. {
  121. usedList = new ChestTrapList();
  122. AddChestTrapList(usedList, id);
  123. }
  124. MChestTrapList.writelock(__FUNCTION__, __LINE__);
  125. map<int32, ChestTrap*>::iterator itr;
  126. for (itr = chesttrap_list.begin(); itr != chesttrap_list.end(); itr++) {
  127. ChestTrap* curTrap = itr->second;
  128. if (curTrap->GetApplicableZoneID() == zoneid || curTrap->GetApplicableZoneID() == -1)
  129. usedList->AddChestTrap(curTrap);
  130. }
  131. shuffleMap(usedList);
  132. usedList->SetListLoaded(true);
  133. MChestTrapList.releasewritelock(__FUNCTION__, __LINE__);
  134. return usedList;
  135. }
  136. map<int32, ChestTrap*>* ChestTrapList::GetAllChestTraps() { return &chesttrap_list; }
  137. bool ChestTrapList::IsListLoaded() { return ListLoaded; }
  138. void ChestTrapList::SetListLoaded(bool val) { ListLoaded = val; }
  139. void ChestTrapList::AddChestTrapList(ChestTrapList* traplist, int32 id) {
  140. if (chesttrap_innerlist.count(id) > 0)
  141. {
  142. ChestTrapList* tmpTrapList = chesttrap_innerlist[id];
  143. chesttrap_innerlist.erase(id);
  144. safe_delete(tmpTrapList);
  145. }
  146. chesttrap_innerlist[id] = traplist;
  147. }
  148. ChestTrapList* ChestTrapList::GetChestTrapList(ChestTrapBaseList listName) {
  149. ChestTrapList* ctl = 0;
  150. MChestTrapInnerList.readlock(__FUNCTION__, __LINE__);
  151. if (chesttrap_innerlist.count(listName) > 0)
  152. ctl = chesttrap_innerlist[listName];
  153. MChestTrapInnerList.releasereadlock(__FUNCTION__, __LINE__);
  154. return ctl;
  155. }
  156. ChestTrapList* ChestTrapList::GetChestTrapListByID(int32 id) {
  157. ChestTrapList* ctl = 0;
  158. MChestTrapInnerList.readlock(__FUNCTION__, __LINE__);
  159. if (chesttrap_innerlist.count(id) > 0)
  160. ctl = chesttrap_innerlist[id];
  161. MChestTrapInnerList.releasereadlock(__FUNCTION__, __LINE__);
  162. return ctl;
  163. }
  164. void ChestTrapList::ClearTraps() {
  165. MChestTrapList.writelock(__FUNCTION__, __LINE__);
  166. map<int32, ChestTrap*>::iterator itr;
  167. for (itr = chesttrap_list.begin(); itr != chesttrap_list.end(); itr++)
  168. safe_delete(itr->second);
  169. chesttrap_list.clear();
  170. MChestTrapList.releasewritelock(__FUNCTION__, __LINE__);
  171. }
  172. void ChestTrapList::ClearTrapList() {
  173. MChestTrapInnerList.writelock(__FUNCTION__, __LINE__);
  174. map<int32, ChestTrapList*>::iterator itr2;
  175. for (itr2 = chesttrap_innerlist.begin(); itr2 != chesttrap_innerlist.end(); itr2++)
  176. safe_delete(itr2->second);
  177. chesttrap_innerlist.clear();
  178. MChestTrapInnerList.releasewritelock(__FUNCTION__, __LINE__);
  179. // reinstantiate the base lists (zone/difficulty/etc)
  180. InstantiateLists(ChestTrapParent);
  181. }
  182. void ChestTrapList::SetupMutexes()
  183. {
  184. MChestTrapList.SetName("ChestTrapList");
  185. MChestTrapInnerList.SetName("MChestTrapInnerList");
  186. MChestListsInUse.SetName("MChestListsInUse");
  187. }
  188. void ChestTrapList::InstantiateLists(bool parent)
  189. {
  190. if (parent)
  191. {
  192. difficultyList = new ChestTrapList(false);
  193. zoneList = new ChestTrapList(false);
  194. MChestTrapInnerList.writelock(__FUNCTION__, __LINE__);
  195. chesttrap_innerlist[ChestTrapBaseList::DIFFICULTY] = difficultyList;
  196. chesttrap_innerlist[ChestTrapBaseList::ZONE] = zoneList;
  197. MChestTrapInnerList.releasewritelock(__FUNCTION__, __LINE__);
  198. }
  199. }
  200. void ChestTrapList::shuffleMap(ChestTrapList* list) {
  201. std::vector<ChestTrap*> tmp_chests;
  202. map<int32, ChestTrap*>::iterator itr;
  203. for (itr = chesttrap_list.begin(); itr != chesttrap_list.end(); itr++) {
  204. ChestTrap* curTrap = itr->second;
  205. tmp_chests.push_back(curTrap);
  206. }
  207. std::random_shuffle(tmp_chests.begin(), tmp_chests.end());
  208. chesttrap_list.clear();
  209. int count = 0;
  210. for (std::vector<ChestTrap*>::iterator it = tmp_chests.begin(); it != tmp_chests.end(); ++it)
  211. {
  212. chesttrap_list[count] = *it;
  213. count++;
  214. }
  215. cycleItr = chesttrap_list.begin();
  216. }