9
3

Loot.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 "Loot.h"
  17. #include "../client.h"
  18. #include "../../common/ConfigReader.h"
  19. #include "../classes.h"
  20. #include "../../common/debug.h"
  21. #include "../zoneserver.h"
  22. #include "../Skills.h"
  23. #include "../classes.h"
  24. #include "../World.h"
  25. #include "../LuaInterface.h"
  26. #include "../../common/Log.h"
  27. #include "../Entity.h"
  28. #include "../Rules/Rules.h"
  29. extern Classes classes;
  30. extern ConfigReader configReader;
  31. extern MasterSkillList master_skill_list;
  32. extern RuleManager rule_manager;
  33. // JA: storing loot-related functions here til I find them all :/
  34. /*
  35. void Combat::AddLooter(Entity* victim, Entity* attacker){
  36. MLooters.writelock(__FUNCTION__, __LINE__);
  37. if(attacker->IsPlayer()){
  38. PlayerGroup* group = ((Player*)attacker)->GetGroup();
  39. if(group && group->members.size() > 0){
  40. deque<GroupMemberInfo*>::iterator itr;
  41. for(itr = group->members.begin(); itr != group->members.end(); itr++)
  42. looters[victim].push_back((*itr)->client->GetPlayer());
  43. }
  44. else
  45. looters[victim].push_back(attacker);
  46. }
  47. MLooters.releasewritelock(__FUNCTION__, __LINE__);
  48. }
  49. void Combat::ClearLooters(Entity* spawn){
  50. MLooters.writelock(__FUNCTION__, __LINE__);
  51. looters.erase(spawn);
  52. MLooters.releasewritelock(__FUNCTION__, __LINE__);
  53. }
  54. bool Combat::CheckLootAllowed(Entity* dead, Entity* spawn){
  55. if(!spawn || !dead || dead->GetHP() > 0)
  56. return false;
  57. bool ret = false;
  58. MLooters.readlock(__FUNCTION__, __LINE__);
  59. if(looters.count(dead) > 0){
  60. vector<Entity*>::iterator itr;
  61. for(itr = looters[dead].begin(); itr != looters[dead].end(); itr++){
  62. if(*itr == spawn){
  63. ret = true;
  64. break;
  65. }
  66. }
  67. }
  68. MLooters.releasereadlock(__FUNCTION__, __LINE__);
  69. return ret;
  70. }
  71. bool Client::HandleLootItem(Entity* entity, Item* item){
  72. if(!item){
  73. SimpleMessage(CHANNEL_COLOR_YELLOW, "Unable to find item to loot!");
  74. return false;
  75. }
  76. if(player->item_list.HasFreeSlot() || player->item_list.CanStack(item)){
  77. if(player->item_list.AssignItemToFreeSlot(item)){
  78. int8 type = CHANNEL_COLOR_LOOT;
  79. if(GetVersion() >= 973 && GetVersion() <= 1000)
  80. type = CHANNEL_COLOR_NEW_LOOT;
  81. else if(GetVersion() >= 973)
  82. type = CHANNEL_COLOR_NEWEST_LOOT;
  83. if(entity)
  84. Message(type, "You loot \\aITEM %u 0:%s\\/a from the corpse of %s", item->details.item_id, item->name.c_str(), entity->GetName());
  85. else
  86. Message(type, "You found a \\aITEM %u 0:%s\\/a", item->details.item_id, item->name.c_str());
  87. Guild* guild = player->GetGuild();
  88. if (guild && item->details.tier >= ITEM_TAG_LEGENDARY) {
  89. char adjective[32];
  90. int8 type;
  91. memset(adjective, 0, sizeof(adjective));
  92. if (item->details.tier >= ITEM_TAG_MYTHICAL) {
  93. strncpy(adjective, "Mythical", sizeof(adjective) - 1);
  94. type = GUILD_EVENT_LOOTS_MYTHICAL_ITEM;
  95. }
  96. else if (item->details.tier >= ITEM_TAG_FABLED) {
  97. strncpy(adjective, "Fabled", sizeof(adjective) - 1);
  98. type = GUILD_EVENT_LOOTS_FABELED_ITEM;
  99. }
  100. else {
  101. strncpy(adjective, "Legendary", sizeof(adjective) - 1);
  102. type = GUILD_EVENT_LOOTS_LEGENDARY_ITEM;
  103. }
  104. guild->AddNewGuildEvent(type, "%s has looted the %s \\aITEM %u 0:%s\\/a", Timer::GetUnixTimeStamp(), true, player->GetName(), adjective, item->details.item_id, item->name.c_str());
  105. guild->SendMessageToGuild(type, "%s has looted the %s \\aITEM %u 0:%s\\/a", player->GetName(), adjective, item->details.item_id, item->name.c_str());
  106. }
  107. CheckPlayerQuestsItemUpdate(item);
  108. return true;
  109. }
  110. else
  111. SimpleMessage(CHANNEL_COLOR_RED, "Could not find free slot to place item.");
  112. }
  113. else
  114. SimpleMessage(CHANNEL_COLOR_YELLOW, "Unable to loot item: Inventory is FULL.");
  115. return false;
  116. }
  117. bool Client::HandleLootItem(Entity* entity, int32 item_id){
  118. if(!entity)
  119. return false;
  120. return HandleLootItem(entity, entity->LootItem(item_id));
  121. }
  122. void Client::HandleLoot(EQApplicationPacket* app){
  123. PacketStruct* packet = configReader.getStruct("WS_LootType", GetVersion());
  124. if(packet){
  125. packet->LoadPacketData(app->pBuffer, app->size);
  126. int32 loot_id = packet->getType_int32_ByName("loot_id");
  127. bool loot_all = (packet->getType_int8_ByName("loot_all") == 1);
  128. safe_delete(packet);
  129. int32 item_id = 0;
  130. Item* item = 0;
  131. Spawn* spawn = GetCurrentZone()->GetSpawnByID(loot_id);
  132. if(player->HasPendingLootItems(loot_id)){
  133. Item* master_item = 0;
  134. if(loot_all){
  135. vector<Item*>* items = player->GetPendingLootItems(loot_id);
  136. if(items){
  137. for(int32 i=0;loot_all && i<items->size();i++){
  138. master_item = items->at(i);
  139. if(master_item){
  140. item = new Item(master_item);
  141. if(item){
  142. loot_all = HandleLootItem(0, item);
  143. if(loot_all)
  144. player->RemovePendingLootItem(loot_id, item->details.item_id);
  145. }
  146. }
  147. }
  148. safe_delete(items);
  149. }
  150. }
  151. else{
  152. packet = configReader.getStruct("WS_LootItem", GetVersion());
  153. if(packet){
  154. packet->LoadPacketData(app->pBuffer, app->size);
  155. item_id = packet->getType_int32_ByName("item_id");
  156. vector<Item*>* items = player->GetPendingLootItems(loot_id);
  157. if(items){
  158. for(int32 i=0;i<items->size();i++){
  159. master_item = items->at(i);
  160. if(master_item && master_item->details.item_id == item_id){
  161. item = new Item(master_item);
  162. if(item && HandleLootItem(0, item))
  163. player->RemovePendingLootItem(loot_id, item->details.item_id);
  164. break;
  165. }
  166. }
  167. safe_delete(items);
  168. }
  169. safe_delete(packet);
  170. }
  171. }
  172. EQ2Packet* outapp = player->SendInventoryUpdate(GetVersion());
  173. if(outapp)
  174. QueuePacket(outapp);
  175. Loot(0, player->GetPendingLootItems(loot_id), (Entity*)spawn);
  176. }
  177. else{
  178. if(spawn && spawn->IsEntity() && GetCurrentZone()->GetCombat()->CheckLootAllowed((Entity*)spawn, player)){
  179. if(loot_all){
  180. while(loot_all && ((item_id = ((Entity*)spawn)->GetLootItemID()) > 0)){
  181. loot_all = HandleLootItem((Entity*)spawn, item_id);
  182. }
  183. }
  184. else{
  185. packet = configReader.getStruct("WS_LootItem", GetVersion());
  186. if(packet){
  187. packet->LoadPacketData(app->pBuffer, app->size);
  188. item_id = packet->getType_int32_ByName("item_id");
  189. HandleLootItem((Entity*)spawn, item_id);
  190. safe_delete(packet);
  191. }
  192. }
  193. EQ2Packet* outapp = player->SendInventoryUpdate(GetVersion());
  194. if(outapp)
  195. QueuePacket(outapp);
  196. Loot((Entity*)spawn);
  197. if(!((Entity*)spawn)->HasLoot()){
  198. CloseLoot();
  199. if(((Entity*)spawn)->IsNPC())
  200. GetCurrentZone()->RemoveDeadSpawn(spawn);
  201. }
  202. }
  203. else{
  204. if(!spawn){
  205. LogWrite(WORLD__ERROR, 0, "World", "Unknown id of %u when looting!", loot_id);
  206. SimpleMessage(CHANNEL_COLOR_YELLOW, "Unable to find spawn to loot!");
  207. }
  208. else
  209. SimpleMessage(CHANNEL_COLOR_YELLOW, "You are not unable to loot that at this time.");
  210. }
  211. }
  212. }
  213. }
  214. void Client::SendPendingLoot(int32 total_coins, Entity* entity){
  215. if(entity)
  216. Loot(total_coins, player->GetPendingLootItems(entity->GetID()), entity);
  217. }
  218. void Client::CloseLoot(){
  219. PacketStruct* packet = configReader.getStruct("WS_CloseWindow", GetVersion());
  220. packet->setDataByName("window_id", 4);
  221. EQ2Packet* outapp = packet->serialize();
  222. // DumpPacket(outapp);
  223. QueuePacket(outapp);
  224. safe_delete(packet);
  225. }
  226. string Client::GetCoinMessage(int32 total_coins){
  227. if(total_coins == 0)
  228. return " 0 Copper";
  229. char tmp[64] = {0};
  230. string message = "";
  231. int32 val = 0;
  232. if(total_coins >= 1000000){
  233. val = total_coins / 1000000;
  234. total_coins -= 1000000 * val;
  235. sprintf(tmp, " %u Platinum", val);
  236. message.append(tmp);
  237. memset(tmp, 0, 64);
  238. }
  239. if(total_coins >= 10000){
  240. val = total_coins / 10000;
  241. total_coins -= 10000 * val;
  242. sprintf(tmp, " %u Gold", val);
  243. message.append(tmp);
  244. memset(tmp, 0, 64);
  245. }
  246. if(total_coins >= 100){
  247. val = total_coins / 100;
  248. total_coins -= 100 * val;
  249. sprintf(tmp, " %u Silver", val);
  250. message.append(tmp);
  251. memset(tmp, 0, 64);
  252. }
  253. if(total_coins > 0){
  254. sprintf(tmp, " %u Copper", (int32)total_coins);
  255. message.append(tmp);
  256. }
  257. return message;
  258. }
  259. void Client::Loot(int32 total_coins, vector<Item*>* items, Entity* entity){
  260. if(!entity){
  261. CloseLoot();
  262. return;
  263. }
  264. if(total_coins > 0){
  265. player->AddCoins(total_coins);
  266. //PlaySound("coin_cha_ching");
  267. string message = "";
  268. if(entity->GetHP() == 0){
  269. message = "You loot ";
  270. entity->SetLootCoins(0);
  271. }
  272. else
  273. message = "You receive ";
  274. message.append(GetCoinMessage(total_coins));
  275. if(entity->GetHP() == 0)
  276. message.append(" from the corpse of ").append(entity->GetName());
  277. int8 type = CHANNEL_COLOR_LOOT;
  278. if(GetVersion() >= 973)
  279. type = CHANNEL_COLOR_NEW_LOOT;
  280. SimpleMessage(type, message.c_str());
  281. }
  282. if(!items || items->size() == 0)
  283. CloseLoot();
  284. PacketStruct* packet = configReader.getStruct("WS_UpdateLoot", GetVersion());
  285. if(packet){
  286. vector<Item*>::iterator itr;
  287. int32 packet_size = 0;
  288. if(items && items->size() > 0){
  289. packet->setDataByName("loot_count", items->size());
  290. packet->setDataByName("display", 1);
  291. }
  292. packet->setDataByName("unknown2", 1);
  293. packet->setDataByName("unknown3", 0x3C);
  294. packet->setDataByName("loot_id", entity->GetID());
  295. EQ2Packet* tmpPacket = packet->serialize();
  296. packet_size += tmpPacket->size;
  297. uchar* data = 0;
  298. if(items && items->size() > 0){
  299. data = new uchar[items->size()*1000 + packet_size];
  300. memset(data, 0, items->size()*1000 + packet_size);
  301. }
  302. else{
  303. data = new uchar[packet_size];
  304. memset(data, 0, packet_size);
  305. }
  306. uchar* ptr = data;
  307. memcpy(ptr, tmpPacket->pBuffer, tmpPacket->size);
  308. ptr += tmpPacket->size;
  309. safe_delete(tmpPacket);
  310. Item* item = 0;
  311. if(items && items->size() > 0){
  312. for(itr = items->begin(); itr != items->end(); itr++){
  313. item = *itr;
  314. memcpy(ptr, &item->details.item_id, sizeof(int32));
  315. ptr += sizeof(int32);
  316. packet_size += sizeof(int32);
  317. tmpPacket = item->serialize(GetVersion(), true, GetPlayer(), false, 1, 0, false, true);
  318. if(GetVersion() >= 860){
  319. memcpy(ptr, tmpPacket->pBuffer + 11, tmpPacket->size - 11);
  320. ptr += tmpPacket->size - 11;
  321. packet_size += tmpPacket->size - 11;
  322. }
  323. else{
  324. memcpy(ptr, tmpPacket->pBuffer + 10, tmpPacket->size - 10);
  325. ptr += tmpPacket->size - 10;
  326. packet_size += tmpPacket->size - 10;
  327. }
  328. safe_delete(tmpPacket);
  329. }
  330. }
  331. packet_size -= sizeof(int32);
  332. memcpy(data, &packet_size, sizeof(int32));
  333. packet_size += sizeof(int32);
  334. EQ2Packet* outapp = new EQ2Packet(OP_ClientCmdMsg, data, packet_size);
  335. //DumpPacket(outapp);
  336. QueuePacket(outapp);
  337. safe_delete_array(data);
  338. safe_delete(packet);
  339. }
  340. }
  341. void Client::Loot(Entity* entity){
  342. if(GetCurrentZone()->GetCombat()->CheckLootAllowed(entity, GetPlayer())){
  343. int32 total_coins = entity->GetLootCoins();
  344. entity->LockLoot();
  345. Loot(total_coins, entity->GetLootItems(), entity);
  346. entity->UnlockLoot();
  347. }
  348. else
  349. SimpleMessage(CHANNEL_COLOR_YELLOW, "You are not allowed to loot at this time.");
  350. }
  351. bool Player::HasPendingLootItems(int32 id){
  352. return (pending_loot_items.count(id) > 0 && pending_loot_items[id].size() > 0);
  353. }
  354. bool Player::HasPendingLootItem(int32 id, int32 item_id){
  355. return (pending_loot_items.count(id) > 0 && pending_loot_items[id].count(item_id) > 0);
  356. }
  357. vector<Item*>* Player::GetPendingLootItems(int32 id){
  358. vector<Item*>* ret = 0;
  359. if(pending_loot_items.count(id) > 0){
  360. ret = new vector<Item*>();
  361. map<int32, bool>::iterator itr;
  362. for(itr = pending_loot_items[id].begin(); itr != pending_loot_items[id].end(); itr++){
  363. if(master_item_list.GetItem(itr->first))
  364. ret->push_back(master_item_list.GetItem(itr->first));
  365. }
  366. }
  367. return ret;
  368. }
  369. void Player::RemovePendingLootItem(int32 id, int32 item_id){
  370. if(pending_loot_items.count(id) > 0){
  371. pending_loot_items[id].erase(item_id);
  372. if(pending_loot_items[id].size() == 0)
  373. pending_loot_items.erase(id);
  374. }
  375. }
  376. void Player::RemovePendingLootItems(int32 id){
  377. if(pending_loot_items.count(id) > 0)
  378. pending_loot_items.erase(id);
  379. }
  380. void Player::AddPendingLootItems(int32 id, vector<Item*>* items){
  381. if(items){
  382. Item* item = 0;
  383. for(int32 i=0;i<items->size();i++){
  384. item = items->at(i);
  385. if(item)
  386. pending_loot_items[id][item->details.item_id] = true;
  387. }
  388. }
  389. }*/
  390. NPC* Entity::DropChest() {
  391. // Check to see if treasure chests are disabled in the rules
  392. if (rule_manager.GetGlobalRule(R_World, TreasureChestDisabled)->GetBool())
  393. return 0;
  394. NPC* chest = 0;
  395. chest = new NPC();
  396. chest->SetAttackable(0);
  397. chest->SetShowLevel(0);
  398. chest->SetShowName(1);
  399. chest->SetTargetable(1);
  400. chest->SetLevel(GetLevel());
  401. // Set the brain to a blank brain so it does nothing
  402. chest->SetBrain(new BlankBrain(chest));
  403. // Set the x, y, z, heading, location (grid id) to that of the dead spawn
  404. chest->SetZone(GetZone());
  405. chest->SetX(GetX());
  406. chest->SetZ(GetZ()); // need to set X/Z BEFORE setting Y
  407. chest->SetY(GetY());
  408. // heading needs to be GetHeading() - 180 so the chest faces the proper way
  409. chest->SetHeading(GetHeading() - 180);
  410. chest->SetLocation(GetLocation());
  411. // Set the primary command to loot and the secondary to disarm
  412. chest->AddPrimaryEntityCommand("loot", rule_manager.GetGlobalRule(R_Loot, LootRadius)->GetFloat(), "loot", "", 0, 0);
  413. chest->AddSecondaryEntityCommand("Disarm", rule_manager.GetGlobalRule(R_Loot, LootRadius)->GetFloat(), "Disarm", "", 0, 0);
  414. // 32 = loot icon for the mouse
  415. chest->SetIcon(32);
  416. // 1 = show the right click menu
  417. chest->SetShowCommandIcon(1);
  418. int8 highest_tier = 0;
  419. vector<Item*>::iterator itr;
  420. for (itr = ((Spawn*)this)->GetLootItems()->begin(); itr != ((Spawn*)this)->GetLootItems()->end(); ) {
  421. if ((*itr)->details.tier >= ITEM_TAG_UNCOMMON) {
  422. if ((*itr)->details.tier > highest_tier)
  423. highest_tier = (*itr)->details.tier;
  424. // Add the item to the chest
  425. chest->AddLootItem((*itr)->details.item_id, (*itr)->details.count);
  426. // Remove the item from the corpse
  427. itr = ((Spawn*)this)->GetLootItems()->erase(itr);
  428. }
  429. else
  430. itr++;
  431. }
  432. /*4034 = small chest | 5864 = treasure chest | 5865 = ornate treasure chest | 4015 = exquisite chest*/
  433. if (highest_tier >= ITEM_TAG_FABLED) {
  434. chest->SetModelType(4015);
  435. chest->SetName("Exquisite Chest");
  436. }
  437. else if (highest_tier >= ITEM_TAG_LEGENDARY) {
  438. chest->SetModelType(5865);
  439. chest->SetName("Ornate Chest");
  440. }
  441. else if (highest_tier >= ITEM_TAG_TREASURED) {
  442. chest->SetModelType(5864);
  443. chest->SetName("Treasure Chest");
  444. }
  445. else if (highest_tier >= ITEM_TAG_UNCOMMON) {
  446. chest->SetModelType(4034);
  447. chest->SetName("Small Chest");
  448. }
  449. else
  450. safe_delete(chest);
  451. if (chest)
  452. chest->SetShowHandIcon(1);
  453. return chest;
  454. }