[7719] Use all existed 4 world map overlay area ids instead 3, replace values by...
[AHbot.git] / src / game / LootHandler.cpp
blob6d3191a2723f3fe1836a1f8a2e3d4f18b6e2a529
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);
145 else
146 player->SendEquipError( msg, NULL, NULL );
149 void WorldSession::HandleLootMoneyOpcode( WorldPacket & /*recv_data*/ )
151 sLog.outDebug("WORLD: CMSG_LOOT_MONEY");
153 Player *player = GetPlayer();
154 uint64 guid = player->GetLootGUID();
155 if(!guid)
156 return;
158 Loot *pLoot = NULL;
160 switch(GUID_HIPART(guid))
162 case HIGHGUID_GAMEOBJECT:
164 GameObject *pGameObject = GetPlayer()->GetMap()->GetGameObject(guid);
166 // not check distance for GO in case owned GO (fishing bobber case, for example)
167 if( pGameObject && (pGameObject->GetOwnerGUID()==_player->GetGUID() || pGameObject->IsWithinDistInMap(_player,INTERACTION_DISTANCE)) )
168 pLoot = &pGameObject->loot;
170 break;
172 case HIGHGUID_CORPSE: // remove insignia ONLY in BG
174 Corpse *bones = ObjectAccessor::GetCorpse(*GetPlayer(), guid);
176 if (bones && bones->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
177 pLoot = &bones->loot;
179 break;
181 case HIGHGUID_ITEM:
183 if(Item *item = GetPlayer()->GetItemByGuid(guid))
184 pLoot = &item->loot;
185 break;
187 case HIGHGUID_UNIT:
189 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(guid);
191 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
193 if ( ok_loot && pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
194 pLoot = &pCreature->loot ;
196 break;
198 default:
199 return; // unlootable type
202 if( pLoot )
204 if (!IS_ITEM_GUID(guid) && player->GetGroup()) //item can be looted only single player
206 Group *group = player->GetGroup();
208 std::vector<Player*> playersNear;
209 for(GroupReference *itr = group->GetFirstMember(); itr != NULL; itr = itr->next())
211 Player* playerGroup = itr->getSource();
212 if(!playerGroup)
213 continue;
214 if (player->GetDistance2d(playerGroup) < sWorld.getConfig(CONFIG_GROUP_XP_DISTANCE))
215 playersNear.push_back(playerGroup);
218 uint32 money_per_player = uint32((pLoot->gold)/(playersNear.size()));
220 for (std::vector<Player*>::iterator i = playersNear.begin(); i != playersNear.end(); ++i)
222 (*i)->ModifyMoney( money_per_player );
223 (*i)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, money_per_player);
224 //Offset surely incorrect, but works
225 WorldPacket data( SMSG_LOOT_MONEY_NOTIFY, 4 );
226 data << uint32(money_per_player);
227 (*i)->GetSession()->SendPacket( &data );
230 else
232 player->ModifyMoney( pLoot->gold );
233 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, pLoot->gold);
235 pLoot->gold = 0;
236 pLoot->NotifyMoneyRemoved();
240 void WorldSession::HandleLootOpcode( WorldPacket & recv_data )
242 CHECK_PACKET_SIZE(recv_data,8);
244 sLog.outDebug("WORLD: CMSG_LOOT");
246 uint64 guid;
247 recv_data >> guid;
249 GetPlayer()->SendLoot(guid, LOOT_CORPSE);
252 void WorldSession::HandleLootReleaseOpcode( WorldPacket & recv_data )
254 CHECK_PACKET_SIZE(recv_data,8);
256 sLog.outDebug("WORLD: CMSG_LOOT_RELEASE");
258 // cheaters can modify lguid to prevent correct apply loot release code and re-loot
259 // use internal stored guid
260 //uint64 lguid;
261 //recv_data >> lguid;
263 if(uint64 lguid = GetPlayer()->GetLootGUID())
264 DoLootRelease(lguid);
267 void WorldSession::DoLootRelease( uint64 lguid )
269 Player *player = GetPlayer();
270 Loot *loot;
272 player->SetLootGUID(0);
273 player->SendLootRelease(lguid);
275 player->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_LOOTING);
277 if(!player->IsInWorld())
278 return;
280 if (IS_GAMEOBJECT_GUID(lguid))
282 GameObject *go = GetPlayer()->GetMap()->GetGameObject(lguid);
284 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
285 if (!go || (go->GetOwnerGUID() != _player->GetGUID() && go->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE) && !go->IsWithinDistInMap(_player,INTERACTION_DISTANCE))
286 return;
288 loot = &go->loot;
290 if (go->GetGoType() == GAMEOBJECT_TYPE_DOOR)
292 // locked doors are opened with spelleffect openlock, prevent remove its as looted
293 go->UseDoorOrButton();
295 else if (loot->isLooted() || go->GetGoType() == GAMEOBJECT_TYPE_FISHINGNODE)
297 // GO is mineral vein? so it is not removed after its looted
298 if(go->GetGoType() == GAMEOBJECT_TYPE_CHEST)
300 uint32 go_min = go->GetGOInfo()->chest.minSuccessOpens;
301 uint32 go_max = go->GetGOInfo()->chest.maxSuccessOpens;
303 // only vein pass this check
304 if(go_min != 0 && go_max > go_min)
306 float amount_rate = sWorld.getRate(RATE_MINING_AMOUNT);
307 float min_amount = go_min*amount_rate;
308 float max_amount = go_max*amount_rate;
310 go->AddUse();
311 float uses = float(go->GetUseCount());
313 if(uses < max_amount)
315 if(uses >= min_amount)
317 float chance_rate = sWorld.getRate(RATE_MINING_NEXT);
319 int32 ReqValue = 175;
320 LockEntry const *lockInfo = sLockStore.LookupEntry(go->GetGOInfo()->chest.lockId);
321 if(lockInfo)
322 ReqValue = lockInfo->Skill[0];
323 float skill = float(player->GetSkillValue(SKILL_MINING))/(ReqValue+25);
324 double chance = pow(0.8*chance_rate,4*(1/double(max_amount))*double(uses));
325 if(roll_chance_f(100*chance+skill))
327 go->SetLootState(GO_READY);
329 else // not have more uses
330 go->SetLootState(GO_JUST_DEACTIVATED);
332 else // 100% chance until min uses
333 go->SetLootState(GO_READY);
335 else // max uses already
336 go->SetLootState(GO_JUST_DEACTIVATED);
338 else // not vein
339 go->SetLootState(GO_JUST_DEACTIVATED);
341 else if (go->GetGoType() == GAMEOBJECT_TYPE_FISHINGHOLE)
342 { // The fishing hole used once more
343 go->AddUse(); // if the max usage is reached, will be despawned in next tick
344 if (go->GetUseCount()>=irand(go->GetGOInfo()->fishinghole.minSuccessOpens,go->GetGOInfo()->fishinghole.maxSuccessOpens))
346 go->SetLootState(GO_JUST_DEACTIVATED);
348 else
349 go->SetLootState(GO_READY);
351 else // not chest (or vein/herb/etc)
352 go->SetLootState(GO_JUST_DEACTIVATED);
354 loot->clear();
356 else
357 // not fully looted object
358 go->SetLootState(GO_ACTIVATED);
360 else if (IS_CORPSE_GUID(lguid)) // ONLY remove insignia at BG
362 Corpse *corpse = ObjectAccessor::GetCorpse(*player, lguid);
363 if (!corpse || !corpse->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
364 return;
366 loot = &corpse->loot;
368 if (loot->isLooted())
370 loot->clear();
371 corpse->RemoveFlag(CORPSE_FIELD_DYNAMIC_FLAGS, CORPSE_DYNFLAG_LOOTABLE);
374 else if (IS_ITEM_GUID(lguid))
376 Item *pItem = player->GetItemByGuid(lguid );
377 if(!pItem)
378 return;
380 ItemPrototype const* proto = pItem->GetProto();
382 // destroy only 5 items from stack in case prospecting and milling
383 if( (proto->BagFamily & (BAG_FAMILY_MASK_MINING_SUPP|BAG_FAMILY_MASK_HERBS)) &&
384 proto->Class == ITEM_CLASS_TRADE_GOODS)
386 pItem->m_lootGenerated = false;
387 pItem->loot.clear();
389 uint32 count = pItem->GetCount();
391 // >=5 checked in spell code, but will work for cheating cases also with removing from another stacks.
392 if(count > 5)
393 count = 5;
395 player->DestroyItemCount(pItem, count, true);
397 else
398 // 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.
399 player->DestroyItem( pItem->GetBagSlot(),pItem->GetSlot(), true);
400 return; // item can be looted only single player
402 else
404 Creature* pCreature = GetPlayer()->GetMap()->GetCreature(lguid);
406 bool ok_loot = pCreature && pCreature->isAlive() == (player->getClass()==CLASS_ROGUE && pCreature->lootForPickPocketed);
407 if ( !ok_loot || !pCreature->IsWithinDistInMap(_player,INTERACTION_DISTANCE) )
408 return;
410 loot = &pCreature->loot;
412 // update next looter
413 if(Player *recipient = pCreature->GetLootRecipient())
414 if(Group* group = recipient->GetGroup())
415 if (group->GetLooterGuid() == player->GetGUID())
416 group->UpdateLooterGuid(pCreature);
418 if (loot->isLooted())
420 // skip pickpocketing loot for speed, skinning timer redunction is no-op in fact
421 if(!pCreature->isAlive())
422 pCreature->AllLootRemovedFromCorpse();
424 pCreature->RemoveFlag(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_LOOTABLE);
425 loot->clear();
429 //Player is not looking at loot list, he doesn't need to see updates on the loot list
430 loot->RemoveLooter(player->GetGUID());
433 void WorldSession::HandleLootMasterGiveOpcode( WorldPacket & recv_data )
435 CHECK_PACKET_SIZE(recv_data,8+1+8);
437 uint8 slotid;
438 uint64 lootguid, target_playerguid;
440 recv_data >> lootguid >> slotid >> target_playerguid;
442 if(!_player->GetGroup() || _player->GetGroup()->GetLooterGuid() != _player->GetGUID())
444 _player->SendLootRelease(GetPlayer()->GetLootGUID());
445 return;
448 Player *target = ObjectAccessor::FindPlayer(MAKE_NEW_GUID(target_playerguid, 0, HIGHGUID_PLAYER));
449 if(!target)
450 return;
452 sLog.outDebug("WorldSession::HandleLootMasterGiveOpcode (CMSG_LOOT_MASTER_GIVE, 0x02A3) Target = [%s].", target->GetName());
454 if(_player->GetLootGUID() != lootguid)
455 return;
457 Loot *pLoot = NULL;
459 if(IS_CREATURE_GUID(GetPlayer()->GetLootGUID()))
461 Creature *pCreature = GetPlayer()->GetMap()->GetCreature(lootguid);
462 if(!pCreature)
463 return;
465 pLoot = &pCreature->loot;
467 else if(IS_GAMEOBJECT_GUID(GetPlayer()->GetLootGUID()))
469 GameObject *pGO = GetPlayer()->GetMap()->GetGameObject(lootguid);
470 if(!pGO)
471 return;
473 pLoot = &pGO->loot;
476 if(!pLoot)
477 return;
479 if (slotid > pLoot->items.size())
481 sLog.outDebug("AutoLootItem: Player %s might be using a hack! (slot %d, size %lu)",GetPlayer()->GetName(), slotid, (unsigned long)pLoot->items.size());
482 return;
485 LootItem& item = pLoot->items[slotid];
487 ItemPosCountVec dest;
488 uint8 msg = target->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, item.itemid, item.count );
489 if ( msg != EQUIP_ERR_OK )
491 target->SendEquipError( msg, NULL, NULL );
492 _player->SendEquipError( msg, NULL, NULL ); // send duplicate of error massage to master looter
493 return;
496 // not move item from loot to target inventory
497 Item * newitem = target->StoreNewItem( dest, item.itemid, true, item.randomPropertyId );
498 target->SendNewItem(newitem, uint32(item.count), false, false, true );
499 target->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM, item.itemid, item.count);
501 // mark as looted
502 item.count=0;
503 item.is_looted=true;
506 pLoot->NotifyItemRemoved(slotid);
507 --pLoot->unlootedCount;