[7474] Set correct value/maxvalue for skill with fixed skill value at skill loading.
[AHbot.git] / src / game / LootHandler.cpp
blob5c5d02699fb61da4c7dde677eb83631e7242c634
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 (*i)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, money_per_player);
226 //Offset surely incorrect, but works
227 WorldPacket data( SMSG_LOOT_MONEY_NOTIFY, 4 );
228 data << uint32(money_per_player);
229 (*i)->GetSession()->SendPacket( &data );
232 else
234 player->ModifyMoney( pLoot->gold );
235 player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY, pLoot->gold);
237 pLoot->gold = 0;
238 pLoot->NotifyMoneyRemoved();
242 void WorldSession::HandleLootOpcode( WorldPacket & recv_data )
244 CHECK_PACKET_SIZE(recv_data,8);
246 sLog.outDebug("WORLD: CMSG_LOOT");
248 uint64 guid;
249 recv_data >> guid;
251 GetPlayer()->SendLoot(guid, LOOT_CORPSE);
254 void WorldSession::HandleLootReleaseOpcode( WorldPacket & recv_data )
256 CHECK_PACKET_SIZE(recv_data,8);
258 sLog.outDebug("WORLD: CMSG_LOOT_RELEASE");
260 // cheaters can modify lguid to prevent correct apply loot release code and re-loot
261 // use internal stored guid
262 //uint64 lguid;
263 //recv_data >> lguid;
265 if(uint64 lguid = GetPlayer()->GetLootGUID())
266 DoLootRelease(lguid);
269 void WorldSession::DoLootRelease( uint64 lguid )
271 Player *player = GetPlayer();
272 Loot *loot;
274 player->SetLootGUID(0);
275 player->SendLootRelease(lguid);
277 player->RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_LOOTING);
279 if (IS_GAMEOBJECT_GUID(lguid))
281 GameObject *go =
282 ObjectAccessor::GetGameObject(*player, 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 = ObjectAccessor::GetCreature(*player, 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 = ObjectAccessor::GetCreature(*GetPlayer(), lootguid);
462 if(!pCreature)
463 return;
465 pLoot = &pCreature->loot;
467 else if(IS_GAMEOBJECT_GUID(GetPlayer()->GetLootGUID()))
469 GameObject *pGO = ObjectAccessor::GetGameObject(*GetPlayer(), 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 %d)",GetPlayer()->GetName(), slotid, 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;