[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / LootHandler.cpp
blob57d1b69104eaee5c654ec5f4c245f73c1ec1823f
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 =
48 ObjectAccessor::GetGameObject(*player, 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;
59 else if (IS_ITEM_GUID(lguid))
61 Item *pItem = player->GetItemByGuid( lguid );
63 if (!pItem)
65 player->SendLootRelease(lguid);
66 return;
69 loot = &pItem->loot;
71 else
73 Creature* pCreature =
74 ObjectAccessor::GetCreature(*player, lguid);
76 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
78 if( !ok_loot || !pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
80 player->SendLootRelease(lguid);
81 return;
84 loot = &pCreature->loot;
87 QuestItem *qitem = NULL;
88 QuestItem *ffaitem = NULL;
89 QuestItem *conditem = NULL;
91 LootItem *item = loot->LootItemInSlot(lootSlot,player,&qitem,&ffaitem,&conditem);
93 if(!item)
95 player->SendEquipError( EQUIP_ERR_ALREADY_LOOTED, NULL, NULL );
96 return;
99 // questitems use the blocked field for other purposes
100 if (!qitem && item->is_blocked)
102 player->SendLootRelease(lguid);
103 return;
106 ItemPosCountVec dest;
107 uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, item->itemid, item->count );
108 if ( msg == EQUIP_ERR_OK )
110 Item * newitem = player->StoreNewItem( dest, item->itemid, true, item->randomPropertyId);
112 if (qitem)
114 qitem->is_looted = true;
115 //freeforall is 1 if everyone's supposed to get the quest item.
116 if (item->freeforall || loot->GetPlayerQuestItems().size() == 1)
117 player->SendNotifyLootItemRemoved(lootSlot);
118 else
119 loot->NotifyQuestItemRemoved(qitem->index);
121 else
123 if (ffaitem)
125 //freeforall case, notify only one player of the removal
126 ffaitem->is_looted=true;
127 player->SendNotifyLootItemRemoved(lootSlot);
129 else
131 //not freeforall, notify everyone
132 if(conditem)
133 conditem->is_looted=true;
134 loot->NotifyItemRemoved(lootSlot);
138 //if only one person is supposed to loot the item, then set it to looted
139 if (!item->freeforall)
140 item->is_looted = true;
142 --loot->unlootedCount;
144 player->SendNewItem(newitem, uint32(item->count), false, false, true);
145 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM, item->itemid, item->count);
147 else
148 player->SendEquipError( msg, NULL, NULL );
151 void WorldSession::HandleLootMoneyOpcode( WorldPacket & /*recv_data*/ )
153 sLog.outDebug("WORLD: CMSG_LOOT_MONEY");
155 Player *player = GetPlayer();
156 uint64 guid = player->GetLootGUID();
157 if(!guid)
158 return;
160 Loot *pLoot = NULL;
162 switch(GUID_HIPART(guid))
164 case HIGHGUID_GAMEOBJECT:
166 GameObject *pGameObject = ObjectAccessor::GetGameObject(*GetPlayer(), guid);
168 // not check distance for GO in case owned GO (fishing bobber case, for example)
169 if( pGameObject && (pGameObject->GetOwnerGUID()==_player->GetGUID() || pGameObject->IsWithinDistInMap(_player,INTERACTION_DISTANCE)) )
170 pLoot = &pGameObject->loot;
172 break;
174 case HIGHGUID_CORPSE: // remove insignia ONLY in BG
176 Corpse *bones = ObjectAccessor::GetCorpse(*GetPlayer(), guid);
178 if (bones && bones->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
179 pLoot = &bones->loot;
181 break;
183 case HIGHGUID_ITEM:
185 if(Item *item = GetPlayer()->GetItemByGuid(guid))
186 pLoot = &item->loot;
187 break;
189 case HIGHGUID_UNIT:
191 Creature* pCreature = ObjectAccessor::GetCreature(*GetPlayer(), guid);
193 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
195 if ( ok_loot && pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
196 pLoot = &pCreature->loot ;
198 break;
200 default:
201 return; // unlootable type
204 if( pLoot )
206 if (!IS_ITEM_GUID(guid) && player->GetGroup()) //item can be looted only single player
208 Group *group = player->GetGroup();
210 std::vector<Player*> playersNear;
211 for(GroupReference *itr = group->GetFirstMember(); itr != NULL; itr = itr->next())
213 Player* playerGroup = itr->getSource();
214 if(!playerGroup)
215 continue;
216 if (player->GetDistance2d(playerGroup) < sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE))
217 playersNear.push_back(playerGroup);
220 uint32 money_per_player = uint32((pLoot->gold)/(playersNear.size()));
222 for (std::vector<Player*>::iterator i = playersNear.begin(); i != playersNear.end(); ++i)
224 (*i)->ModifyMoney( 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
232 player->ModifyMoney( pLoot->gold );
233 pLoot->gold = 0;
234 pLoot->NotifyMoneyRemoved();
238 void WorldSession::HandleLootOpcode( WorldPacket & recv_data )
240 CHECK_PACKET_SIZE(recv_data,8);
242 sLog.outDebug("WORLD: CMSG_LOOT");
244 uint64 guid;
245 recv_data >> guid;
247 GetPlayer()->SendLoot(guid, LOOT_CORPSE);
250 void WorldSession::HandleLootReleaseOpcode( WorldPacket & recv_data )
252 CHECK_PACKET_SIZE(recv_data,8);
254 sLog.outDebug("WORLD: CMSG_LOOT_RELEASE");
256 // cheaters can modify lguid to prevent correct apply loot release code and re-loot
257 // use internal stored guid
258 //uint64 lguid;
259 //recv_data >> lguid;
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 (IS_GAMEOBJECT_GUID(lguid))
277 GameObject *go =
278 ObjectAccessor::GetGameObject(*player, lguid);
280 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
281 if (!go || (go->GetOwnerGUID() != _player->GetGUID() && go->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE) && !go->IsWithinDistInMap(_player,INTERACTION_DISTANCE))
282 return;
284 loot = &go->loot;
286 if (go->GetGoType() == GAMEOBJECT_TYPE_DOOR)
288 // locked doors are opened with spelleffect openlock, prevent remove its as looted
289 go->UseDoorOrButton();
291 else if (loot->isLooted() || go->GetGoType() == GAMEOBJECT_TYPE_FISHINGNODE)
293 // GO is mineral vein? so it is not removed after its looted
294 if(go->GetGoType() == GAMEOBJECT_TYPE_CHEST)
296 uint32 go_min = go->GetGOInfo()->chest.minSuccessOpens;
297 uint32 go_max = go->GetGOInfo()->chest.maxSuccessOpens;
299 // only vein pass this check
300 if(go_min != 0 && go_max > go_min)
302 float amount_rate = sWorld.getRate(RATE_MINING_AMOUNT);
303 float min_amount = go_min*amount_rate;
304 float max_amount = go_max*amount_rate;
306 go->AddUse();
307 float uses = float(go->GetUseCount());
309 if(uses < max_amount)
311 if(uses >= min_amount)
313 float chance_rate = sWorld.getRate(RATE_MINING_NEXT);
315 int32 ReqValue = 175;
316 LockEntry const *lockInfo = sLockStore.LookupEntry(go->GetGOInfo()->chest.lockId);
317 if(lockInfo)
318 ReqValue = lockInfo->Skill[0];
319 float skill = float(player->GetSkillValue(SKILL_MINING))/(ReqValue+25);
320 double chance = pow(0.8*chance_rate,4*(1/double(max_amount))*double(uses));
321 if(roll_chance_f(100*chance+skill))
323 go->SetLootState(GO_READY);
325 else // not have more uses
326 go->SetLootState(GO_JUST_DEACTIVATED);
328 else // 100% chance until min uses
329 go->SetLootState(GO_READY);
331 else // max uses already
332 go->SetLootState(GO_JUST_DEACTIVATED);
334 else // not vein
335 go->SetLootState(GO_JUST_DEACTIVATED);
337 else if (go->GetGoType() == GAMEOBJECT_TYPE_FISHINGHOLE)
338 { // The fishing hole used once more
339 go->AddUse(); // if the max usage is reached, will be despawned in next tick
340 if (go->GetUseCount()>=irand(go->GetGOInfo()->fishinghole.minSuccessOpens,go->GetGOInfo()->fishinghole.maxSuccessOpens))
342 go->SetLootState(GO_JUST_DEACTIVATED);
344 else
345 go->SetLootState(GO_READY);
347 else // not chest (or vein/herb/etc)
348 go->SetLootState(GO_JUST_DEACTIVATED);
350 loot->clear();
352 else
353 // not fully looted object
354 go->SetLootState(GO_ACTIVATED);
356 else if (IS_CORPSE_GUID(lguid)) // ONLY remove insignia at BG
358 Corpse *corpse = ObjectAccessor::GetCorpse(*player, lguid);
359 if (!corpse || !corpse->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
360 return;
362 loot = &corpse->loot;
364 if (loot->isLooted())
366 loot->clear();
367 corpse->RemoveFlag(CORPSE_FIELD_DYNAMIC_FLAGS, CORPSE_DYNFLAG_LOOTABLE);
370 else if (IS_ITEM_GUID(lguid))
372 Item *pItem = player->GetItemByGuid(lguid );
373 if(!pItem)
374 return;
376 ItemPrototype const* proto = pItem->GetProto();
378 // destroy only 5 items from stack in case prospecting and milling
379 if( (proto->BagFamily & (BAG_FAMILY_MASK_MINING_SUPP|BAG_FAMILY_MASK_HERBS)) &&
380 proto->Class == ITEM_CLASS_TRADE_GOODS)
382 pItem->m_lootGenerated = false;
383 pItem->loot.clear();
385 uint32 count = pItem->GetCount();
387 // >=5 checked in spell code, but will work for cheating cases also with removing from another stacks.
388 if(count > 5)
389 count = 5;
391 player->DestroyItemCount(pItem, count, true);
393 else
394 // 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.
395 player->DestroyItem( pItem->GetBagSlot(),pItem->GetSlot(), true);
396 return; // item can be looted only single player
398 else
400 Creature* pCreature = ObjectAccessor::GetCreature(*player, lguid);
402 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
403 if ( !ok_loot || !pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
404 return;
406 loot = &pCreature->loot;
408 // update next looter
409 if(Player *recipient = pCreature->GetLootRecipient())
410 if(Group* group = recipient->GetGroup())
411 if (group->GetLooterGuid() == player->GetGUID())
412 group->UpdateLooterGuid(pCreature);
414 if (loot->isLooted())
416 // skip pickpocketing loot for speed, skinning timer redunction is no-op in fact
417 if(!pCreature->isAlive())
418 pCreature->AllLootRemovedFromCorpse();
420 pCreature->RemoveFlag(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_LOOTABLE);
421 loot->clear();
425 //Player is not looking at loot list, he doesn't need to see updates on the loot list
426 loot->RemoveLooter(player->GetGUID());
429 void WorldSession::HandleLootMasterGiveOpcode( WorldPacket & recv_data )
431 CHECK_PACKET_SIZE(recv_data,8+1+8);
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 = ObjectAccessor::GetCreature(*GetPlayer(), lootguid);
458 if(!pCreature)
459 return;
461 pLoot = &pCreature->loot;
463 else if(IS_GAMEOBJECT_GUID(GetPlayer()->GetLootGUID()))
465 GameObject *pGO = ObjectAccessor::GetGameObject(*GetPlayer(), 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 %d)",GetPlayer()->GetName(), slotid, 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);
497 // mark as looted
498 item.count=0;
499 item.is_looted=true;
502 pLoot->NotifyItemRemoved(slotid);
503 --pLoot->unlootedCount;