9
3

Collections.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "Collections.h"
  17. #include "../../common/Log.h"
  18. #include <assert.h>
  19. extern MasterCollectionList master_collection_list;
  20. Collection::Collection() {
  21. id = 0;
  22. memset(name, 0, sizeof(name));
  23. memset(category, 0, sizeof(category));
  24. level = 0;
  25. reward_coin = 0;
  26. reward_xp = 0;
  27. completed = false;
  28. save_needed = false;
  29. }
  30. Collection::Collection(Collection *in) {
  31. vector<struct CollectionItem *> *collection_items_in;
  32. vector<struct CollectionRewardItem *> *reward_items_in;
  33. vector<struct CollectionItem *>::iterator itr;
  34. vector<struct CollectionRewardItem *>::iterator itr2;
  35. struct CollectionItem *collection_item;
  36. struct CollectionRewardItem *reward_item;
  37. assert(in);
  38. id = in->GetID();
  39. strncpy(name, in->GetName(), sizeof(name));
  40. strncpy(category, in->GetCategory(), sizeof(category));
  41. level = in->GetLevel();
  42. reward_coin = in->GetRewardCoin();
  43. reward_xp = in->GetRewardXP();
  44. completed = in->GetCompleted();
  45. save_needed = in->GetSaveNeeded();
  46. collection_items_in = in->GetCollectionItems();
  47. for (itr = collection_items_in->begin(); itr != collection_items_in->end(); itr++) {
  48. collection_item = new struct CollectionItem;
  49. collection_item->item = (*itr)->item;
  50. collection_item->index = (*itr)->index;
  51. collection_item->found = (*itr)->found;
  52. collection_items.push_back(collection_item);
  53. }
  54. reward_items_in = in->GetRewardItems();
  55. for (itr2 = reward_items_in->begin(); itr2 != reward_items_in->end(); itr2++) {
  56. reward_item = new struct CollectionRewardItem;
  57. reward_item->item = (*itr2)->item;
  58. reward_item->quantity = (*itr2)->quantity;
  59. reward_items.push_back(reward_item);
  60. }
  61. reward_items_in = in->GetSelectableRewardItems();
  62. for (itr2 = reward_items_in->begin(); itr2 != reward_items_in->end(); itr2++) {
  63. reward_item = new struct CollectionRewardItem;
  64. reward_item->item = (*itr2)->item;
  65. reward_item->quantity = (*itr2)->quantity;
  66. selectable_reward_items.push_back(reward_item);
  67. }
  68. }
  69. Collection::~Collection() {
  70. vector<struct CollectionItem *>::iterator itr;
  71. vector<struct CollectionRewardItem *>::iterator itr2;
  72. for (itr = collection_items.begin(); itr != collection_items.end(); itr++)
  73. safe_delete(*itr);
  74. for (itr2 = reward_items.begin(); itr2 != reward_items.end(); itr2++)
  75. safe_delete(*itr2);
  76. for (itr2 = selectable_reward_items.begin(); itr2 != selectable_reward_items.end(); itr2++)
  77. safe_delete(*itr2);
  78. }
  79. void Collection::AddCollectionItem(struct CollectionItem *collection_item) {
  80. assert(collection_item);
  81. collection_items.push_back(collection_item);
  82. }
  83. void Collection::AddRewardItem(struct CollectionRewardItem *reward_item) {
  84. assert(reward_item);
  85. reward_items.push_back(reward_item);
  86. }
  87. void Collection::AddSelectableRewardItem(struct CollectionRewardItem *reward_item) {
  88. assert(reward_item);
  89. selectable_reward_items.push_back(reward_item);
  90. }
  91. bool Collection::NeedsItem(Item *item) {
  92. vector<struct CollectionItem *>::iterator itr;
  93. struct CollectionItem *collection_item;
  94. assert(item);
  95. if (completed)
  96. return false;
  97. for (itr = collection_items.begin(); itr != collection_items.end(); itr++) {
  98. collection_item = *itr;
  99. if (collection_item->item == item->details.item_id) {
  100. if (collection_item->found)
  101. return false;
  102. else
  103. return true;
  104. }
  105. }
  106. /* item is not required by this collection at all */
  107. return false;
  108. }
  109. struct CollectionItem * Collection::GetCollectionItemByItemID(int32 item_id) {
  110. vector<struct CollectionItem *>::iterator itr;
  111. struct CollectionItem *collection_item;
  112. for (itr = collection_items.begin(); itr != collection_items.end(); itr++) {
  113. collection_item = *itr;
  114. if (collection_item->item == item_id)
  115. return collection_item;
  116. }
  117. return 0;
  118. }
  119. bool Collection::GetIsReadyToTurnIn() {
  120. vector<struct CollectionItem *>::iterator itr;
  121. if (completed)
  122. return false;
  123. for (itr = collection_items.begin(); itr != collection_items.end(); itr++) {
  124. if (!(*itr)->found)
  125. return false;
  126. }
  127. return true;
  128. }
  129. MasterCollectionList::MasterCollectionList() {
  130. mutex_collections.SetName("MasterCollectionList::collections");
  131. }
  132. MasterCollectionList::~MasterCollectionList() {
  133. ClearCollections();
  134. }
  135. bool MasterCollectionList::AddCollection(Collection *collection) {
  136. bool ret = false;
  137. assert(collection);
  138. mutex_collections.writelock(__FUNCTION__, __LINE__);
  139. if (collections.count(collection->GetID()) == 0) {
  140. collections[collection->GetID()] = collection;
  141. ret = true;
  142. }
  143. mutex_collections.releasewritelock(__FUNCTION__, __LINE__);
  144. return ret;
  145. }
  146. Collection * MasterCollectionList::GetCollection(int32 collection_id) {
  147. Collection *collection = 0;
  148. mutex_collections.readlock(__FUNCTION__, __LINE__);
  149. if (collections.count(collection_id) > 0)
  150. collection = collections[collection_id];
  151. mutex_collections.releasereadlock(__FUNCTION__, __LINE__);
  152. return collection;
  153. }
  154. void MasterCollectionList::ClearCollections() {
  155. map<int32, Collection *>::iterator itr;
  156. mutex_collections.writelock(__FUNCTION__, __LINE__);
  157. for (itr = collections.begin(); itr != collections.end(); itr++)
  158. safe_delete(itr->second);
  159. collections.clear();
  160. mutex_collections.releasewritelock(__FUNCTION__, __LINE__);
  161. }
  162. int32 MasterCollectionList::Size() {
  163. int32 size;
  164. mutex_collections.readlock(__FUNCTION__, __LINE__);
  165. size = collections.size();
  166. mutex_collections.releasereadlock(__FUNCTION__, __LINE__);
  167. return size;
  168. }
  169. bool MasterCollectionList::NeedsItem(Item *item) {
  170. map<int32, Collection *>::iterator itr;
  171. bool ret = false;
  172. assert(item);
  173. mutex_collections.readlock(__FUNCTION__, __LINE__);
  174. for (itr = collections.begin(); itr != collections.end(); itr++) {
  175. if (itr->second->NeedsItem(item)) {
  176. ret = true;
  177. break;
  178. }
  179. }
  180. mutex_collections.releasereadlock(__FUNCTION__, __LINE__);
  181. return ret;
  182. }
  183. PlayerCollectionList::PlayerCollectionList() {
  184. }
  185. PlayerCollectionList::~PlayerCollectionList() {
  186. ClearCollections();
  187. }
  188. bool PlayerCollectionList::AddCollection(Collection *collection) {
  189. assert(collection);
  190. if (collections.count(collection->GetID()) == 0) {
  191. collections[collection->GetID()] = collection;
  192. return true;
  193. }
  194. return false;
  195. }
  196. Collection * PlayerCollectionList::GetCollection(int32 collection_id) {
  197. if (collections.count(collection_id) > 0)
  198. return collections[collection_id];
  199. return 0;
  200. }
  201. void PlayerCollectionList::ClearCollections() {
  202. map<int32, Collection *>::iterator itr;
  203. for (itr = collections.begin(); itr != collections.end(); itr++)
  204. safe_delete(itr->second);
  205. collections.clear();
  206. }
  207. int32 PlayerCollectionList::Size() {
  208. return collections.size();
  209. }
  210. bool PlayerCollectionList::NeedsItem(Item *item) {
  211. map<int32, Collection *> *master_collections;
  212. map<int32, Collection *>::iterator itr;
  213. Collection *collection;
  214. Mutex *master_mutex;
  215. bool ret = false;
  216. assert(item);
  217. for (itr = collections.begin(); itr != collections.end(); itr++) {
  218. if (itr->second->NeedsItem(item)) {
  219. ret = true;
  220. break;
  221. }
  222. }
  223. /* if the player doesnt have a collection that needs the item, check the master collection list to see if there's a collection
  224. * in there that needs the item that the player does not have yet */
  225. if (!ret) {
  226. master_mutex = master_collection_list.GetMutex();
  227. master_collections = master_collection_list.GetCollections();
  228. master_mutex->readlock(__FUNCTION__, __LINE__);
  229. for (itr = master_collections->begin(); itr != master_collections->end(); itr++) {
  230. collection = itr->second;
  231. if (collection->NeedsItem(item) && !GetCollection(collection->GetID())) {
  232. ret = true;
  233. break;
  234. }
  235. }
  236. master_mutex->releasereadlock(__FUNCTION__, __LINE__);
  237. }
  238. return ret;
  239. }
  240. bool PlayerCollectionList::HasCollectionsToHandIn() {
  241. map<int32, Collection *>::iterator itr;
  242. for (itr = collections.begin(); itr != collections.end(); itr++) {
  243. if (itr->second->GetIsReadyToTurnIn())
  244. return true;
  245. }
  246. return false;
  247. }