[8483] Implement glyph 43361.
[getmangos.git] / src / game / LootHandler.cpp
blob76008c6e960dd763e19ce0ca6c58cd9c16a77850
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 sLog.outDebug("WORLD: CMSG_AUTOSTORE_LOOT_ITEM");
36 Player *player = GetPlayer();
37 uint64 lguid = player->GetLootGUID();
38 Loot *loot;
39 uint8 lootSlot;
41 recv_data >> lootSlot;
43 if (IS_GAMEOBJECT_GUID(lguid))
45 GameObject *go = player->GetMap()->GetGameObject(lguid);
47 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
48 if (!go || ((go->GetOwnerGUID() != _player->GetGUID() && go->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE) && !go->IsWithinDistInMap(_player,INTERACTION_DISTANCE)))
50 player->SendLootRelease(lguid);
51 return;
54 loot = &go->loot;
56 else if (IS_ITEM_GUID(lguid))
58 Item *pItem = player->GetItemByGuid( lguid );
60 if (!pItem)
62 player->SendLootRelease(lguid);
63 return;
66 loot = &pItem->loot;
68 else
70 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(lguid);
72 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
74 if( !ok_loot || !pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
76 player->SendLootRelease(lguid);
77 return;
80 loot = &pCreature->loot;
83 QuestItem *qitem = NULL;
84 QuestItem *ffaitem = NULL;
85 QuestItem *conditem = NULL;
87 LootItem *item = loot->LootItemInSlot(lootSlot,player,&qitem,&ffaitem,&conditem);
89 if(!item)
91 player->SendEquipError( EQUIP_ERR_ALREADY_LOOTED, NULL, NULL );
92 return;
95 // questitems use the blocked field for other purposes
96 if (!qitem && item->is_blocked)
98 player->SendLootRelease(lguid);
99 return;
102 ItemPosCountVec dest;
103 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, item->itemid, item->count );
104 if ( msg == EQUIP_ERR_OK )
106 Item * newitem = player->StoreNewItem( dest, item->itemid, true, item->randomPropertyId);
108 if (qitem)
110 qitem->is_looted = true;
111 //freeforall is 1 if everyone's supposed to get the quest item.
112 if (item->freeforall || loot->GetPlayerQuestItems().size() == 1)
113 player->SendNotifyLootItemRemoved(lootSlot);
114 else
115 loot->NotifyQuestItemRemoved(qitem->index);
117 else
119 if (ffaitem)
121 //freeforall case, notify only one player of the removal
122 ffaitem->is_looted=true;
123 player->SendNotifyLootItemRemoved(lootSlot);
125 else
127 //not freeforall, notify everyone
128 if(conditem)
129 conditem->is_looted=true;
130 loot->NotifyItemRemoved(lootSlot);
134 //if only one person is supposed to loot the item, then set it to looted
135 if (!item->freeforall)
136 item->is_looted = true;
138 --loot->unlootedCount;
140 player->SendNewItem(newitem, uint32(item->count), false, false, true);
141 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM, item->itemid, item->count);
142 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_TYPE, loot->loot_type, item->count);
144 else
145 player->SendEquipError( msg, NULL, NULL );
148 void WorldSession::HandleLootMoneyOpcode( WorldPacket & /*recv_data*/ )
150 sLog.outDebug("WORLD: CMSG_LOOT_MONEY");
152 Player *player = GetPlayer();
153 uint64 guid = player->GetLootGUID();
154 if(!guid)
155 return;
157 Loot *pLoot = NULL;
159 switch(GUID_HIPART(guid))
161 case HIGHGUID_GAMEOBJECT:
163 GameObject *pGameObject = GetPlayer()->GetMap()->GetGameObject(guid);
165 // not check distance for GO in case owned GO (fishing bobber case, for example)
166 if( pGameObject && (pGameObject->GetOwnerGUID()==_player->GetGUID() || pGameObject->IsWithinDistInMap(_player,INTERACTION_DISTANCE)) )
167 pLoot = &pGameObject->loot;
169 break;
171 case HIGHGUID_CORPSE: // remove insignia ONLY in BG
173 Corpse *bones = ObjectAccessor::GetCorpse(*GetPlayer(), guid);
175 if (bones && bones->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
176 pLoot = &bones->loot;
178 break;
180 case HIGHGUID_ITEM:
182 if(Item *item = GetPlayer()->GetItemByGuid(guid))
183 pLoot = &item->loot;
184 break;
186 case HIGHGUID_UNIT:
188 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(guid);
190 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
192 if ( ok_loot && pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
193 pLoot = &pCreature->loot ;
195 break;
197 default:
198 return; // unlootable type
201 if( pLoot )
203 if (!IS_ITEM_GUID(guid) && player->GetGroup()) //item can be looted only single player
205 Group *group = player->GetGroup();
207 std::vector<Player*> playersNear;
208 for(GroupReference *itr = group->GetFirstMember(); itr != NULL; itr = itr->next())
210 Player* playerGroup = itr->getSource();
211 if(!playerGroup)
212 continue;
213 if (player->IsWithinDist(playerGroup,sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE),false))
214 playersNear.push_back(playerGroup);
217 uint32 money_per_player = uint32((pLoot->gold)/(playersNear.size()));
219 for (std::vector<Player*>::const_iterator i = playersNear.begin(); i != playersNear.end(); ++i)
221 (*i)->ModifyMoney( money_per_player );
222 (*i)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, money_per_player);
223 //Offset surely incorrect, but works
224 WorldPacket data( SMSG_LOOT_MONEY_NOTIFY, 4 );
225 data << uint32(money_per_player);
226 (*i)->GetSession()->SendPacket( &data );
229 else
231 player->ModifyMoney( pLoot->gold );
232 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, pLoot->gold);
234 pLoot->gold = 0;
235 pLoot->NotifyMoneyRemoved();
239 void WorldSession::HandleLootOpcode( WorldPacket & recv_data )
241 sLog.outDebug("WORLD: CMSG_LOOT");
243 uint64 guid;
244 recv_data >> guid;
246 // Check possible cheat
247 if(!_player->isAlive())
248 return;
250 GetPlayer()->SendLoot(guid, LOOT_CORPSE);
253 void WorldSession::HandleLootReleaseOpcode( WorldPacket & recv_data )
255 sLog.outDebug("WORLD: CMSG_LOOT_RELEASE");
257 // cheaters can modify lguid to prevent correct apply loot release code and re-loot
258 // use internal stored guid
259 recv_data.read_skip<uint64>(); // guid;
261 if(uint64 lguid = GetPlayer()->GetLootGUID())
262 DoLootRelease(lguid);
265 void WorldSession::DoLootRelease( uint64 lguid )
267 Player *player = GetPlayer();
268 Loot *loot;
270 player->SetLootGUID(0);
271 player->SendLootRelease(lguid);
273 player->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_LOOTING);
275 if(!player->IsInWorld())
276 return;
278 if (IS_GAMEOBJECT_GUID(lguid))
280 GameObject *go = GetPlayer()->GetMap()->GetGameObject(lguid);
282 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
283 if (!go || ((go->GetOwnerGUID() != _player->GetGUID() && go->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE) && !go->IsWithinDistInMap(_player,INTERACTION_DISTANCE)))
284 return;
286 loot = &go->loot;
288 if (go->GetGoType() == GAMEOBJECT_TYPE_DOOR)
290 // locked doors are opened with spelleffect openlock, prevent remove its as looted
291 go->UseDoorOrButton();
293 else if (loot->isLooted() || go->GetGoType() == GAMEOBJECT_TYPE_FISHINGNODE)
295 // GO is mineral vein? so it is not removed after its looted
296 if(go->GetGoType() == GAMEOBJECT_TYPE_CHEST)
298 uint32 go_min = go->GetGOInfo()->chest.minSuccessOpens;
299 uint32 go_max = go->GetGOInfo()->chest.maxSuccessOpens;
301 // only vein pass this check
302 if(go_min != 0 && go_max > go_min)
304 float amount_rate = sWorld.getRate(RATE_MINING_AMOUNT);
305 float min_amount = go_min*amount_rate;
306 float max_amount = go_max*amount_rate;
308 go->AddUse();
309 float uses = float(go->GetUseCount());
311 if(uses < max_amount)
313 if(uses >= min_amount)
315 float chance_rate = sWorld.getRate(RATE_MINING_NEXT);
317 int32 ReqValue = 175;
318 LockEntry const *lockInfo = sLockStore.LookupEntry(go->GetGOInfo()->chest.lockId);
319 if(lockInfo)
320 ReqValue = lockInfo->Skill[0];
321 float skill = float(player->GetSkillValue(SKILL_MINING))/(ReqValue+25);
322 double chance = pow(0.8*chance_rate,4*(1/double(max_amount))*double(uses));
323 if(roll_chance_f(100*chance+skill))
325 go->SetLootState(GO_READY);
327 else // not have more uses
328 go->SetLootState(GO_JUST_DEACTIVATED);
330 else // 100% chance until min uses
331 go->SetLootState(GO_READY);
333 else // max uses already
334 go->SetLootState(GO_JUST_DEACTIVATED);
336 else // not vein
337 go->SetLootState(GO_JUST_DEACTIVATED);
339 else if (go->GetGoType() == GAMEOBJECT_TYPE_FISHINGHOLE)
340 { // The fishing hole used once more
341 go->AddUse(); // if the max usage is reached, will be despawned in next tick
342 if (go->GetUseCount()>=irand(go->GetGOInfo()->fishinghole.minSuccessOpens,go->GetGOInfo()->fishinghole.maxSuccessOpens))
344 go->SetLootState(GO_JUST_DEACTIVATED);
346 else
347 go->SetLootState(GO_READY);
349 else // not chest (or vein/herb/etc)
350 go->SetLootState(GO_JUST_DEACTIVATED);
352 loot->clear();
354 else
355 // not fully looted object
356 go->SetLootState(GO_ACTIVATED);
358 else if (IS_CORPSE_GUID(lguid)) // ONLY remove insignia at BG
360 Corpse *corpse = ObjectAccessor::GetCorpse(*player, lguid);
361 if (!corpse || !corpse->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
362 return;
364 loot = &corpse->loot;
366 if (loot->isLooted())
368 loot->clear();
369 corpse->RemoveFlag(CORPSE_FIELD_DYNAMIC_FLAGS, CORPSE_DYNFLAG_LOOTABLE);
372 else if (IS_ITEM_GUID(lguid))
374 Item *pItem = player->GetItemByGuid(lguid );
375 if(!pItem)
376 return;
378 ItemPrototype const* proto = pItem->GetProto();
380 // destroy only 5 items from stack in case prospecting and milling
381 if( (proto->BagFamily & (BAG_FAMILY_MASK_MINING_SUPP|BAG_FAMILY_MASK_HERBS)) &&
382 proto->Class == ITEM_CLASS_TRADE_GOODS)
384 pItem->m_lootGenerated = false;
385 pItem->loot.clear();
387 uint32 count = pItem->GetCount();
389 // >=5 checked in spell code, but will work for cheating cases also with removing from another stacks.
390 if(count > 5)
391 count = 5;
393 player->DestroyItemCount(pItem, count, true);
395 else
396 // 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.
397 player->DestroyItem( pItem->GetBagSlot(),pItem->GetSlot(), true);
398 return; // item can be looted only single player
400 else
402 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(lguid);
404 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
405 if ( !ok_loot || !pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
406 return;
408 loot = &pCreature->loot;
410 // update next looter
411 if(Player *recipient = pCreature->GetLootRecipient())
412 if(Group* group = recipient->GetGroup())
413 if (group->GetLooterGuid() == player->GetGUID())
414 group->UpdateLooterGuid(pCreature);
416 if (loot->isLooted())
418 // skip pickpocketing loot for speed, skinning timer redunction is no-op in fact
419 if(!pCreature->isAlive())
420 pCreature->AllLootRemovedFromCorpse();
422 pCreature->RemoveFlag(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_LOOTABLE);
423 loot->clear();
427 //Player is not looking at loot list, he doesn't need to see updates on the loot list
428 loot->RemoveLooter(player->GetGUID());
431 void WorldSession::HandleLootMasterGiveOpcode( WorldPacket & recv_data )
433 uint8 slotid;
434 uint64 lootguid, target_playerguid;
436 recv_data >> lootguid >> slotid >> target_playerguid;
438 if(!_player->GetGroup() || _player->GetGroup()->GetLooterGuid() != _player->GetGUID())
440 _player->SendLootRelease(GetPlayer()->GetLootGUID());
441 return;
444 Player *target = ObjectAccessor::FindPlayer(MAKE_NEW_GUID(target_playerguid, 0, HIGHGUID_PLAYER));
445 if(!target)
446 return;
448 sLog.outDebug("WorldSession::HandleLootMasterGiveOpcode (CMSG_LOOT_MASTER_GIVE, 0x02A3) Target = [%s].", target->GetName());
450 if(_player->GetLootGUID() != lootguid)
451 return;
453 Loot *pLoot = NULL;
455 if(IS_CREATURE_GUID(GetPlayer()->GetLootGUID()))
457 Creature *pCreature = GetPlayer()->GetMap()->GetCreature(lootguid);
458 if(!pCreature)
459 return;
461 pLoot = &pCreature->loot;
463 else if(IS_GAMEOBJECT_GUID(GetPlayer()->GetLootGUID()))
465 GameObject *pGO = GetPlayer()->GetMap()->GetGameObject(lootguid);
466 if(!pGO)
467 return;
469 pLoot = &pGO->loot;
472 if(!pLoot)
473 return;
475 if (slotid > pLoot->items.size())
477 sLog.outDebug("AutoLootItem: Player %s might be using a hack! (slot %d, size %lu)",GetPlayer()->GetName(), slotid, (unsigned long)pLoot->items.size());
478 return;
481 LootItem& item = pLoot->items[slotid];
483 ItemPosCountVec dest;
484 uint8 msg = target->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, item.itemid, item.count );
485 if ( msg != EQUIP_ERR_OK )
487 target->SendEquipError( msg, NULL, NULL );
488 _player->SendEquipError( msg, NULL, NULL ); // send duplicate of error massage to master looter
489 return;
492 // not move item from loot to target inventory
493 Item * newitem = target->StoreNewItem( dest, item.itemid, true, item.randomPropertyId );
494 target->SendNewItem(newitem, uint32(item.count), false, false, true );
495 target->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM, item.itemid, item.count);
496 target->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_TYPE, pLoot->loot_type, item.count);
498 // mark as looted
499 item.count=0;
500 item.is_looted=true;
503 pLoot->NotifyItemRemoved(slotid);
504 --pLoot->unlootedCount;