[9581] Fixed apply damage reduction to melee/ranged damage.
[getmangos.git] / src / game / LootHandler.cpp
blob944e9e821e740f762df19e56437abeb8eff25ef0
1 /*
2 * Copyright (C) 2005-2010 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 "ObjectGuid.h"
27 #include "WorldSession.h"
28 #include "LootMgr.h"
29 #include "Object.h"
30 #include "Group.h"
31 #include "World.h"
32 #include "Util.h"
34 void WorldSession::HandleAutostoreLootItemOpcode( WorldPacket & recv_data )
36 sLog.outDebug("WORLD: CMSG_AUTOSTORE_LOOT_ITEM");
37 Player *player = GetPlayer();
38 ObjectGuid lguid = player->GetLootGUID();
39 Loot *loot;
40 uint8 lootSlot;
42 recv_data >> lootSlot;
44 switch( lguid.GetHigh())
46 case HIGHGUID_GAMEOBJECT:
48 GameObject *go = player->GetMap()->GetGameObject(lguid);
50 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
51 if (!go || ((go->GetOwnerGUID() != _player->GetGUID() && go->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE) && !go->IsWithinDistInMap(_player,INTERACTION_DISTANCE)))
53 player->SendLootRelease(lguid);
54 return;
57 loot = &go->loot;
58 break;
60 case HIGHGUID_ITEM:
62 Item *pItem = player->GetItemByGuid( lguid );
64 if (!pItem)
66 player->SendLootRelease(lguid);
67 return;
70 loot = &pItem->loot;
71 break;
73 case HIGHGUID_CORPSE:
75 Corpse *bones = player->GetMap()->GetCorpse(lguid);
76 if (!bones)
78 player->SendLootRelease(lguid);
79 return;
81 loot = &bones->loot;
82 break;
84 case HIGHGUID_UNIT:
86 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(lguid);
88 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
90 if( !ok_loot || !pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
92 player->SendLootRelease(lguid);
93 return;
96 loot = &pCreature->loot;
97 break;
99 default:
101 sLog.outError("%s is unsupported for looting.",lguid.GetString().c_str());
102 return;
106 QuestItem *qitem = NULL;
107 QuestItem *ffaitem = NULL;
108 QuestItem *conditem = NULL;
110 LootItem *item = loot->LootItemInSlot(lootSlot,player,&qitem,&ffaitem,&conditem);
112 if(!item)
114 player->SendEquipError( EQUIP_ERR_ALREADY_LOOTED, NULL, NULL );
115 return;
118 // questitems use the blocked field for other purposes
119 if (!qitem && item->is_blocked)
121 player->SendLootRelease(lguid);
122 return;
125 ItemPosCountVec dest;
126 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, item->itemid, item->count );
127 if ( msg == EQUIP_ERR_OK )
129 Item * newitem = player->StoreNewItem( dest, item->itemid, true, item->randomPropertyId);
131 if (qitem)
133 qitem->is_looted = true;
134 //freeforall is 1 if everyone's supposed to get the quest item.
135 if (item->freeforall || loot->GetPlayerQuestItems().size() == 1)
136 player->SendNotifyLootItemRemoved(lootSlot);
137 else
138 loot->NotifyQuestItemRemoved(qitem->index);
140 else
142 if (ffaitem)
144 //freeforall case, notify only one player of the removal
145 ffaitem->is_looted=true;
146 player->SendNotifyLootItemRemoved(lootSlot);
148 else
150 //not freeforall, notify everyone
151 if(conditem)
152 conditem->is_looted=true;
153 loot->NotifyItemRemoved(lootSlot);
157 //if only one person is supposed to loot the item, then set it to looted
158 if (!item->freeforall)
159 item->is_looted = true;
161 --loot->unlootedCount;
163 player->SendNewItem(newitem, uint32(item->count), false, false, true);
164 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM, item->itemid, item->count);
165 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_TYPE, loot->loot_type, item->count);
166 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_EPIC_ITEM, item->itemid, item->count);
168 else
169 player->SendEquipError( msg, NULL, NULL, item->itemid );
172 void WorldSession::HandleLootMoneyOpcode( WorldPacket & /*recv_data*/ )
174 sLog.outDebug("WORLD: CMSG_LOOT_MONEY");
176 Player *player = GetPlayer();
177 ObjectGuid guid = player->GetLootGUID();
178 if (guid.IsEmpty())
179 return;
181 Loot *pLoot = NULL;
183 switch(guid.GetHigh())
185 case HIGHGUID_GAMEOBJECT:
187 GameObject *pGameObject = GetPlayer()->GetMap()->GetGameObject(guid);
189 // not check distance for GO in case owned GO (fishing bobber case, for example)
190 if( pGameObject && (pGameObject->GetOwnerGUID()==_player->GetGUID() || pGameObject->IsWithinDistInMap(_player,INTERACTION_DISTANCE)) )
191 pLoot = &pGameObject->loot;
193 break;
195 case HIGHGUID_CORPSE: // remove insignia ONLY in BG
197 Corpse *bones = _player->GetMap()->GetCorpse(guid);
199 if (bones && bones->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
200 pLoot = &bones->loot;
202 break;
204 case HIGHGUID_ITEM:
206 if(Item *item = GetPlayer()->GetItemByGuid(guid))
207 pLoot = &item->loot;
208 break;
210 case HIGHGUID_UNIT:
212 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(guid);
214 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
216 if ( ok_loot && pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
217 pLoot = &pCreature->loot ;
219 break;
221 default:
222 return; // unlootable type
225 if (pLoot)
227 if (!guid.IsItem() && player->GetGroup()) //item can be looted only single player
229 Group *group = player->GetGroup();
231 std::vector<Player*> playersNear;
232 for(GroupReference *itr = group->GetFirstMember(); itr != NULL; itr = itr->next())
234 Player* playerGroup = itr->getSource();
235 if(!playerGroup)
236 continue;
237 if (player->IsWithinDistInMap(playerGroup,sWorld.getConfig(CONFIG_FLOAT_GROUP_XP_DISTANCE),false))
238 playersNear.push_back(playerGroup);
241 uint32 money_per_player = uint32((pLoot->gold)/(playersNear.size()));
243 for (std::vector<Player*>::const_iterator i = playersNear.begin(); i != playersNear.end(); ++i)
245 (*i)->ModifyMoney( money_per_player );
246 (*i)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, money_per_player);
247 //Offset surely incorrect, but works
248 WorldPacket data( SMSG_LOOT_MONEY_NOTIFY, 4 );
249 data << uint32(money_per_player);
250 (*i)->GetSession()->SendPacket( &data );
253 else
255 player->ModifyMoney( pLoot->gold );
256 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, pLoot->gold);
258 pLoot->gold = 0;
259 pLoot->NotifyMoneyRemoved();
263 void WorldSession::HandleLootOpcode( WorldPacket & recv_data )
265 sLog.outDebug("WORLD: CMSG_LOOT");
267 uint64 guid;
268 recv_data >> guid;
270 // Check possible cheat
271 if(!_player->isAlive())
272 return;
274 GetPlayer()->SendLoot(guid, LOOT_CORPSE);
277 void WorldSession::HandleLootReleaseOpcode( WorldPacket & recv_data )
279 sLog.outDebug("WORLD: CMSG_LOOT_RELEASE");
281 // cheaters can modify lguid to prevent correct apply loot release code and re-loot
282 // use internal stored guid
283 recv_data.read_skip<uint64>(); // guid;
285 if(uint64 lguid = GetPlayer()->GetLootGUID())
286 DoLootRelease(lguid);
289 void WorldSession::DoLootRelease(ObjectGuid lguid)
291 Player *player = GetPlayer();
292 Loot *loot;
294 player->SetLootGUID(0);
295 player->SendLootRelease(lguid);
297 player->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_LOOTING);
299 if(!player->IsInWorld())
300 return;
302 switch(lguid.GetHigh())
304 case HIGHGUID_GAMEOBJECT:
306 GameObject *go = GetPlayer()->GetMap()->GetGameObject(lguid);
308 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
309 if (!go || ((go->GetOwnerGUID() != _player->GetGUID() && go->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE) && !go->IsWithinDistInMap(_player,INTERACTION_DISTANCE)))
310 return;
312 loot = &go->loot;
314 if (go->GetGoType() == GAMEOBJECT_TYPE_DOOR)
316 // locked doors are opened with spelleffect openlock, prevent remove its as looted
317 go->UseDoorOrButton();
319 else if (loot->isLooted() || go->GetGoType() == GAMEOBJECT_TYPE_FISHINGNODE)
321 // GO is mineral vein? so it is not removed after its looted
322 if(go->GetGoType() == GAMEOBJECT_TYPE_CHEST)
324 uint32 go_min = go->GetGOInfo()->chest.minSuccessOpens;
325 uint32 go_max = go->GetGOInfo()->chest.maxSuccessOpens;
327 // only vein pass this check
328 if(go_min != 0 && go_max > go_min)
330 float amount_rate = sWorld.getConfig(CONFIG_FLOAT_RATE_MINING_AMOUNT);
331 float min_amount = go_min*amount_rate;
332 float max_amount = go_max*amount_rate;
334 go->AddUse();
335 float uses = float(go->GetUseCount());
337 if(uses < max_amount)
339 if(uses >= min_amount)
341 float chance_rate = sWorld.getConfig(CONFIG_FLOAT_RATE_MINING_NEXT);
343 int32 ReqValue = 175;
344 LockEntry const *lockInfo = sLockStore.LookupEntry(go->GetGOInfo()->chest.lockId);
345 if(lockInfo)
346 ReqValue = lockInfo->Skill[0];
347 float skill = float(player->GetSkillValue(SKILL_MINING))/(ReqValue+25);
348 double chance = pow(0.8*chance_rate,4*(1/double(max_amount))*double(uses));
349 if(roll_chance_f(float(100.0f*chance+skill)))
351 go->SetLootState(GO_READY);
353 else // not have more uses
354 go->SetLootState(GO_JUST_DEACTIVATED);
356 else // 100% chance until min uses
357 go->SetLootState(GO_READY);
359 else // max uses already
360 go->SetLootState(GO_JUST_DEACTIVATED);
362 else // not vein
363 go->SetLootState(GO_JUST_DEACTIVATED);
365 else if (go->GetGoType() == GAMEOBJECT_TYPE_FISHINGHOLE)
366 { // The fishing hole used once more
367 go->AddUse(); // if the max usage is reached, will be despawned in next tick
368 if (go->GetUseCount() >= urand(go->GetGOInfo()->fishinghole.minSuccessOpens,go->GetGOInfo()->fishinghole.maxSuccessOpens))
370 go->SetLootState(GO_JUST_DEACTIVATED);
372 else
373 go->SetLootState(GO_READY);
375 else // not chest (or vein/herb/etc)
376 go->SetLootState(GO_JUST_DEACTIVATED);
378 loot->clear();
380 else
381 // not fully looted object
382 go->SetLootState(GO_ACTIVATED);
383 break;
385 case HIGHGUID_CORPSE: // ONLY remove insignia at BG
387 Corpse *corpse = _player->GetMap()->GetCorpse(lguid);
388 if (!corpse || !corpse->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
389 return;
391 loot = &corpse->loot;
393 if (loot->isLooted())
395 loot->clear();
396 corpse->RemoveFlag(CORPSE_FIELD_DYNAMIC_FLAGS, CORPSE_DYNFLAG_LOOTABLE);
398 break;
400 case HIGHGUID_ITEM:
402 Item *pItem = player->GetItemByGuid(lguid );
403 if(!pItem)
404 return;
406 ItemPrototype const* proto = pItem->GetProto();
408 // destroy only 5 items from stack in case prospecting and milling
409 if( (proto->BagFamily & (BAG_FAMILY_MASK_MINING_SUPP|BAG_FAMILY_MASK_HERBS)) &&
410 proto->Class == ITEM_CLASS_TRADE_GOODS)
412 pItem->m_lootGenerated = false;
413 pItem->loot.clear();
415 uint32 count = pItem->GetCount();
417 // >=5 checked in spell code, but will work for cheating cases also with removing from another stacks.
418 if(count > 5)
419 count = 5;
421 player->DestroyItemCount(pItem, count, true);
423 else
424 // 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.
425 player->DestroyItem( pItem->GetBagSlot(),pItem->GetSlot(), true);
426 return; // item can be looted only single player
428 case HIGHGUID_UNIT:
430 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(lguid);
432 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
433 if ( !ok_loot || !pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
434 return;
436 loot = &pCreature->loot;
438 // update next looter
439 if(Player *recipient = pCreature->GetLootRecipient())
440 if(Group* group = recipient->GetGroup())
441 if (group->GetLooterGuid() == player->GetGUID())
442 group->UpdateLooterGuid(pCreature);
444 if (loot->isLooted())
446 // skip pickpocketing loot for speed, skinning timer redunction is no-op in fact
447 if(!pCreature->isAlive())
448 pCreature->AllLootRemovedFromCorpse();
450 pCreature->RemoveFlag(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_LOOTABLE);
451 loot->clear();
453 break;
455 default:
457 sLog.outError("%s is unsupported for looting.", lguid.GetString().c_str());
458 return;
462 //Player is not looking at loot list, he doesn't need to see updates on the loot list
463 loot->RemoveLooter(player->GetGUID());
466 void WorldSession::HandleLootMasterGiveOpcode( WorldPacket & recv_data )
468 uint8 slotid;
469 ObjectGuid lootguid;
470 ObjectGuid target_playerguid;
472 recv_data >> lootguid >> slotid >> target_playerguid;
474 if(!_player->GetGroup() || _player->GetGroup()->GetLooterGuid() != _player->GetGUID())
476 _player->SendLootRelease(GetPlayer()->GetLootGUID());
477 return;
480 Player *target = ObjectAccessor::FindPlayer(target_playerguid);
481 if(!target)
482 return;
484 sLog.outDebug("WorldSession::HandleLootMasterGiveOpcode (CMSG_LOOT_MASTER_GIVE, 0x02A3) Target = %s [%s].", target_playerguid.GetString().c_str(), target->GetName());
486 if(_player->GetLootGUID() != lootguid.GetRawValue())
487 return;
489 Loot *pLoot = NULL;
491 if(lootguid.IsCreature())
493 Creature *pCreature = GetPlayer()->GetMap()->GetCreature(lootguid);
494 if(!pCreature)
495 return;
497 pLoot = &pCreature->loot;
499 else if(lootguid.IsGameobject())
501 GameObject *pGO = GetPlayer()->GetMap()->GetGameObject(lootguid);
502 if(!pGO)
503 return;
505 pLoot = &pGO->loot;
507 else
508 return;
510 if (slotid > pLoot->items.size())
512 sLog.outDebug("AutoLootItem: Player %s might be using a hack! (slot %d, size %lu)",GetPlayer()->GetName(), slotid, (unsigned long)pLoot->items.size());
513 return;
516 LootItem& item = pLoot->items[slotid];
518 ItemPosCountVec dest;
519 uint8 msg = target->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, item.itemid, item.count );
520 if ( msg != EQUIP_ERR_OK )
522 target->SendEquipError( msg, NULL, NULL, item.itemid );
524 // send duplicate of error massage to master looter
525 _player->SendEquipError( msg, NULL, NULL, item.itemid );
526 return;
529 // now move item from loot to target inventory
530 Item * newitem = target->StoreNewItem( dest, item.itemid, true, item.randomPropertyId );
531 target->SendNewItem(newitem, uint32(item.count), false, false, true );
532 target->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM, item.itemid, item.count);
533 target->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_TYPE, pLoot->loot_type, item.count);
534 target->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_EPIC_ITEM, item.itemid, item.count);
536 // mark as looted
537 item.count=0;
538 item.is_looted=true;
540 pLoot->NotifyItemRemoved(slotid);
541 --pLoot->unlootedCount;