[7986] MaNGOS 0.13 release.
[getmangos.git] / src / game / LootHandler.cpp
blobcfba29ad3db308f5254396a2fbcc878a8f85c2b8
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "WorldPacket.h"
21 #include "Log.h"
22 #include "Corpse.h"
23 #include "GameObject.h"
24 #include "Player.h"
25 #include "ObjectAccessor.h"
26 #include "WorldSession.h"
27 #include "LootMgr.h"
28 #include "Object.h"
29 #include "Group.h"
30 #include "World.h"
31 #include "Util.h"
33 void WorldSession::HandleAutostoreLootItemOpcode( WorldPacket & recv_data )
35 CHECK_PACKET_SIZE(recv_data,1);
37 sLog.outDebug("WORLD: CMSG_AUTOSTORE_LOOT_ITEM");
38 Player *player = GetPlayer();
39 uint64 lguid = player->GetLootGUID();
40 Loot *loot;
41 uint8 lootSlot;
43 recv_data >> lootSlot;
45 if (IS_GAMEOBJECT_GUID(lguid))
47 GameObject *go = player->GetMap()->GetGameObject(lguid);
49 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
50 if (!go || (go->GetOwnerGUID() != _player->GetGUID() && go->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE) && !go->IsWithinDistInMap(_player,INTERACTION_DISTANCE))
52 player->SendLootRelease(lguid);
53 return;
56 loot = &go->loot;
58 else if (IS_ITEM_GUID(lguid))
60 Item *pItem = player->GetItemByGuid( lguid );
62 if (!pItem)
64 player->SendLootRelease(lguid);
65 return;
68 loot = &pItem->loot;
70 else
72 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(lguid);
74 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
76 if( !ok_loot || !pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
78 player->SendLootRelease(lguid);
79 return;
82 loot = &pCreature->loot;
85 QuestItem *qitem = NULL;
86 QuestItem *ffaitem = NULL;
87 QuestItem *conditem = NULL;
89 LootItem *item = loot->LootItemInSlot(lootSlot,player,&qitem,&ffaitem,&conditem);
91 if(!item)
93 player->SendEquipError( EQUIP_ERR_ALREADY_LOOTED, NULL, NULL );
94 return;
97 // questitems use the blocked field for other purposes
98 if (!qitem && item->is_blocked)
100 player->SendLootRelease(lguid);
101 return;
104 ItemPosCountVec dest;
105 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, item->itemid, item->count );
106 if ( msg == EQUIP_ERR_OK )
108 Item * newitem = player->StoreNewItem( dest, item->itemid, true, item->randomPropertyId);
110 if (qitem)
112 qitem->is_looted = true;
113 //freeforall is 1 if everyone's supposed to get the quest item.
114 if (item->freeforall || loot->GetPlayerQuestItems().size() == 1)
115 player->SendNotifyLootItemRemoved(lootSlot);
116 else
117 loot->NotifyQuestItemRemoved(qitem->index);
119 else
121 if (ffaitem)
123 //freeforall case, notify only one player of the removal
124 ffaitem->is_looted=true;
125 player->SendNotifyLootItemRemoved(lootSlot);
127 else
129 //not freeforall, notify everyone
130 if(conditem)
131 conditem->is_looted=true;
132 loot->NotifyItemRemoved(lootSlot);
136 //if only one person is supposed to loot the item, then set it to looted
137 if (!item->freeforall)
138 item->is_looted = true;
140 --loot->unlootedCount;
142 player->SendNewItem(newitem, uint32(item->count), false, false, true);
143 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM, item->itemid, item->count);
144 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_TYPE, loot->loot_type, item->count);
146 else
147 player->SendEquipError( msg, NULL, NULL );
150 void WorldSession::HandleLootMoneyOpcode( WorldPacket & /*recv_data*/ )
152 sLog.outDebug("WORLD: CMSG_LOOT_MONEY");
154 Player *player = GetPlayer();
155 uint64 guid = player->GetLootGUID();
156 if(!guid)
157 return;
159 Loot *pLoot = NULL;
161 switch(GUID_HIPART(guid))
163 case HIGHGUID_GAMEOBJECT:
165 GameObject *pGameObject = GetPlayer()->GetMap()->GetGameObject(guid);
167 // not check distance for GO in case owned GO (fishing bobber case, for example)
168 if( pGameObject && (pGameObject->GetOwnerGUID()==_player->GetGUID() || pGameObject->IsWithinDistInMap(_player,INTERACTION_DISTANCE)) )
169 pLoot = &pGameObject->loot;
171 break;
173 case HIGHGUID_CORPSE: // remove insignia ONLY in BG
175 Corpse *bones = ObjectAccessor::GetCorpse(*GetPlayer(), guid);
177 if (bones && bones->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
178 pLoot = &bones->loot;
180 break;
182 case HIGHGUID_ITEM:
184 if(Item *item = GetPlayer()->GetItemByGuid(guid))
185 pLoot = &item->loot;
186 break;
188 case HIGHGUID_UNIT:
190 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(guid);
192 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
194 if ( ok_loot && pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
195 pLoot = &pCreature->loot ;
197 break;
199 default:
200 return; // unlootable type
203 if( pLoot )
205 if (!IS_ITEM_GUID(guid) && player->GetGroup()) //item can be looted only single player
207 Group *group = player->GetGroup();
209 std::vector<Player*> playersNear;
210 for(GroupReference *itr = group->GetFirstMember(); itr != NULL; itr = itr->next())
212 Player* playerGroup = itr->getSource();
213 if(!playerGroup)
214 continue;
215 if (player->IsWithinDist(playerGroup,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
216 playersNear.push_back(playerGroup);
219 uint32 money_per_player = uint32((pLoot->gold)/(playersNear.size()));
221 for (std::vector<Player*>::const_iterator i = playersNear.begin(); i != playersNear.end(); ++i)
223 (*i)->ModifyMoney( money_per_player );
224 (*i)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, money_per_player);
225 //Offset surely incorrect, but works
226 WorldPacket data( SMSG_LOOT_MONEY_NOTIFY, 4 );
227 data << uint32(money_per_player);
228 (*i)->GetSession()->SendPacket( &data );
231 else
233 player->ModifyMoney( pLoot->gold );
234 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, pLoot->gold);
236 pLoot->gold = 0;
237 pLoot->NotifyMoneyRemoved();
241 void WorldSession::HandleLootOpcode( WorldPacket & recv_data )
243 CHECK_PACKET_SIZE(recv_data,8);
245 sLog.outDebug("WORLD: CMSG_LOOT");
247 uint64 guid;
248 recv_data >> guid;
250 GetPlayer()->SendLoot(guid, LOOT_CORPSE);
253 void WorldSession::HandleLootReleaseOpcode( WorldPacket & recv_data )
255 CHECK_PACKET_SIZE(recv_data,8);
257 sLog.outDebug("WORLD: CMSG_LOOT_RELEASE");
259 // cheaters can modify lguid to prevent correct apply loot release code and re-loot
260 // use internal stored guid
261 //uint64 lguid;
262 //recv_data >> lguid;
264 if(uint64 lguid = GetPlayer()->GetLootGUID())
265 DoLootRelease(lguid);
268 void WorldSession::DoLootRelease( uint64 lguid )
270 Player *player = GetPlayer();
271 Loot *loot;
273 player->SetLootGUID(0);
274 player->SendLootRelease(lguid);
276 player->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_LOOTING);
278 if(!player->IsInWorld())
279 return;
281 if (IS_GAMEOBJECT_GUID(lguid))
283 GameObject *go = GetPlayer()->GetMap()->GetGameObject(lguid);
285 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
286 if (!go || (go->GetOwnerGUID() != _player->GetGUID() && go->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE) && !go->IsWithinDistInMap(_player,INTERACTION_DISTANCE))
287 return;
289 loot = &go->loot;
291 if (go->GetGoType() == GAMEOBJECT_TYPE_DOOR)
293 // locked doors are opened with spelleffect openlock, prevent remove its as looted
294 go->UseDoorOrButton();
296 else if (loot->isLooted() || go->GetGoType() == GAMEOBJECT_TYPE_FISHINGNODE)
298 // GO is mineral vein? so it is not removed after its looted
299 if(go->GetGoType() == GAMEOBJECT_TYPE_CHEST)
301 uint32 go_min = go->GetGOInfo()->chest.minSuccessOpens;
302 uint32 go_max = go->GetGOInfo()->chest.maxSuccessOpens;
304 // only vein pass this check
305 if(go_min != 0 && go_max > go_min)
307 float amount_rate = sWorld.getRate(RATE_MINING_AMOUNT);
308 float min_amount = go_min*amount_rate;
309 float max_amount = go_max*amount_rate;
311 go->AddUse();
312 float uses = float(go->GetUseCount());
314 if(uses < max_amount)
316 if(uses >= min_amount)
318 float chance_rate = sWorld.getRate(RATE_MINING_NEXT);
320 int32 ReqValue = 175;
321 LockEntry const *lockInfo = sLockStore.LookupEntry(go->GetGOInfo()->chest.lockId);
322 if(lockInfo)
323 ReqValue = lockInfo->Skill[0];
324 float skill = float(player->GetSkillValue(SKILL_MINING))/(ReqValue+25);
325 double chance = pow(0.8*chance_rate,4*(1/double(max_amount))*double(uses));
326 if(roll_chance_f(100*chance+skill))
328 go->SetLootState(GO_READY);
330 else // not have more uses
331 go->SetLootState(GO_JUST_DEACTIVATED);
333 else // 100% chance until min uses
334 go->SetLootState(GO_READY);
336 else // max uses already
337 go->SetLootState(GO_JUST_DEACTIVATED);
339 else // not vein
340 go->SetLootState(GO_JUST_DEACTIVATED);
342 else if (go->GetGoType() == GAMEOBJECT_TYPE_FISHINGHOLE)
343 { // The fishing hole used once more
344 go->AddUse(); // if the max usage is reached, will be despawned in next tick
345 if (go->GetUseCount()>=irand(go->GetGOInfo()->fishinghole.minSuccessOpens,go->GetGOInfo()->fishinghole.maxSuccessOpens))
347 go->SetLootState(GO_JUST_DEACTIVATED);
349 else
350 go->SetLootState(GO_READY);
352 else // not chest (or vein/herb/etc)
353 go->SetLootState(GO_JUST_DEACTIVATED);
355 loot->clear();
357 else
358 // not fully looted object
359 go->SetLootState(GO_ACTIVATED);
361 else if (IS_CORPSE_GUID(lguid)) // ONLY remove insignia at BG
363 Corpse *corpse = ObjectAccessor::GetCorpse(*player, lguid);
364 if (!corpse || !corpse->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
365 return;
367 loot = &corpse->loot;
369 if (loot->isLooted())
371 loot->clear();
372 corpse->RemoveFlag(CORPSE_FIELD_DYNAMIC_FLAGS, CORPSE_DYNFLAG_LOOTABLE);
375 else if (IS_ITEM_GUID(lguid))
377 Item *pItem = player->GetItemByGuid(lguid );
378 if(!pItem)
379 return;
381 ItemPrototype const* proto = pItem->GetProto();
383 // destroy only 5 items from stack in case prospecting and milling
384 if( (proto->BagFamily & (BAG_FAMILY_MASK_MINING_SUPP|BAG_FAMILY_MASK_HERBS)) &&
385 proto->Class == ITEM_CLASS_TRADE_GOODS)
387 pItem->m_lootGenerated = false;
388 pItem->loot.clear();
390 uint32 count = pItem->GetCount();
392 // >=5 checked in spell code, but will work for cheating cases also with removing from another stacks.
393 if(count > 5)
394 count = 5;
396 player->DestroyItemCount(pItem, count, true);
398 else
399 // FIXME: item don't must be deleted in case not fully looted state. But this pre-request implement loot saving in DB at item save. Or checting possible.
400 player->DestroyItem( pItem->GetBagSlot(),pItem->GetSlot(), true);
401 return; // item can be looted only single player
403 else
405 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(lguid);
407 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
408 if ( !ok_loot || !pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
409 return;
411 loot = &pCreature->loot;
413 // update next looter
414 if(Player *recipient = pCreature->GetLootRecipient())
415 if(Group* group = recipient->GetGroup())
416 if (group->GetLooterGuid() == player->GetGUID())
417 group->UpdateLooterGuid(pCreature);
419 if (loot->isLooted())
421 // skip pickpocketing loot for speed, skinning timer redunction is no-op in fact
422 if(!pCreature->isAlive())
423 pCreature->AllLootRemovedFromCorpse();
425 pCreature->RemoveFlag(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_LOOTABLE);
426 loot->clear();
430 //Player is not looking at loot list, he doesn't need to see updates on the loot list
431 loot->RemoveLooter(player->GetGUID());
434 void WorldSession::HandleLootMasterGiveOpcode( WorldPacket & recv_data )
436 CHECK_PACKET_SIZE(recv_data,8+1+8);
438 uint8 slotid;
439 uint64 lootguid, target_playerguid;
441 recv_data >> lootguid >> slotid >> target_playerguid;
443 if(!_player->GetGroup() || _player->GetGroup()->GetLooterGuid() != _player->GetGUID())
445 _player->SendLootRelease(GetPlayer()->GetLootGUID());
446 return;
449 Player *target = ObjectAccessor::FindPlayer(MAKE_NEW_GUID(target_playerguid, 0, HIGHGUID_PLAYER));
450 if(!target)
451 return;
453 sLog.outDebug("WorldSession::HandleLootMasterGiveOpcode (CMSG_LOOT_MASTER_GIVE, 0x02A3) Target = [%s].", target->GetName());
455 if(_player->GetLootGUID() != lootguid)
456 return;
458 Loot *pLoot = NULL;
460 if(IS_CREATURE_GUID(GetPlayer()->GetLootGUID()))
462 Creature *pCreature = GetPlayer()->GetMap()->GetCreature(lootguid);
463 if(!pCreature)
464 return;
466 pLoot = &pCreature->loot;
468 else if(IS_GAMEOBJECT_GUID(GetPlayer()->GetLootGUID()))
470 GameObject *pGO = GetPlayer()->GetMap()->GetGameObject(lootguid);
471 if(!pGO)
472 return;
474 pLoot = &pGO->loot;
477 if(!pLoot)
478 return;
480 if (slotid > pLoot->items.size())
482 sLog.outDebug("AutoLootItem: Player %s might be using a hack! (slot %d, size %lu)",GetPlayer()->GetName(), slotid, (unsigned long)pLoot->items.size());
483 return;
486 LootItem& item = pLoot->items[slotid];
488 ItemPosCountVec dest;
489 uint8 msg = target->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, item.itemid, item.count );
490 if ( msg != EQUIP_ERR_OK )
492 target->SendEquipError( msg, NULL, NULL );
493 _player->SendEquipError( msg, NULL, NULL ); // send duplicate of error massage to master looter
494 return;
497 // not move item from loot to target inventory
498 Item * newitem = target->StoreNewItem( dest, item.itemid, true, item.randomPropertyId );
499 target->SendNewItem(newitem, uint32(item.count), false, false, true );
500 target->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM, item.itemid, item.count);
501 target->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_TYPE, pLoot->loot_type, item.count);
503 // mark as looted
504 item.count=0;
505 item.is_looted=true;
508 pLoot->NotifyItemRemoved(slotid);
509 --pLoot->unlootedCount;