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
20 #include "WorldPacket.h"
23 #include "GameObject.h"
25 #include "ObjectAccessor.h"
26 #include "ObjectDefines.h"
27 #include "WorldSession.h"
34 void WorldSession::HandleAutostoreLootItemOpcode( WorldPacket
& recv_data
)
36 sLog
.outDebug("WORLD: CMSG_AUTOSTORE_LOOT_ITEM");
37 Player
*player
= GetPlayer();
38 uint64 lguid
= player
->GetLootGUID();
42 recv_data
>> lootSlot
;
44 if (IS_GAMEOBJECT_GUID(lguid
))
46 GameObject
*go
= player
->GetMap()->GetGameObject(lguid
);
48 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
49 if (!go
|| ((go
->GetOwnerGUID() != _player
->GetGUID() && go
->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE
) && !go
->IsWithinDistInMap(_player
,INTERACTION_DISTANCE
)))
51 player
->SendLootRelease(lguid
);
57 else if (IS_ITEM_GUID(lguid
))
59 Item
*pItem
= player
->GetItemByGuid( lguid
);
63 player
->SendLootRelease(lguid
);
69 else if (IS_CORPSE_GUID(lguid
))
71 Corpse
*bones
= player
->GetMap()->GetCorpse(lguid
);
74 player
->SendLootRelease(lguid
);
81 Creature
* pCreature
= GetPlayer()->GetMap()->GetCreature(lguid
);
83 bool ok_loot
= pCreature
&& pCreature
->isAlive() == (player
->getClass()==CLASS_ROGUE
&& pCreature
->lootForPickPocketed
);
85 if( !ok_loot
|| !pCreature
->IsWithinDistInMap(_player
,INTERACTION_DISTANCE
) )
87 player
->SendLootRelease(lguid
);
91 loot
= &pCreature
->loot
;
94 QuestItem
*qitem
= NULL
;
95 QuestItem
*ffaitem
= NULL
;
96 QuestItem
*conditem
= NULL
;
98 LootItem
*item
= loot
->LootItemInSlot(lootSlot
,player
,&qitem
,&ffaitem
,&conditem
);
102 player
->SendEquipError( EQUIP_ERR_ALREADY_LOOTED
, NULL
, NULL
);
106 // questitems use the blocked field for other purposes
107 if (!qitem
&& item
->is_blocked
)
109 player
->SendLootRelease(lguid
);
113 ItemPosCountVec dest
;
114 uint8 msg
= player
->CanStoreNewItem( NULL_BAG
, NULL_SLOT
, dest
, item
->itemid
, item
->count
);
115 if ( msg
== EQUIP_ERR_OK
)
117 Item
* newitem
= player
->StoreNewItem( dest
, item
->itemid
, true, item
->randomPropertyId
);
121 qitem
->is_looted
= true;
122 //freeforall is 1 if everyone's supposed to get the quest item.
123 if (item
->freeforall
|| loot
->GetPlayerQuestItems().size() == 1)
124 player
->SendNotifyLootItemRemoved(lootSlot
);
126 loot
->NotifyQuestItemRemoved(qitem
->index
);
132 //freeforall case, notify only one player of the removal
133 ffaitem
->is_looted
=true;
134 player
->SendNotifyLootItemRemoved(lootSlot
);
138 //not freeforall, notify everyone
140 conditem
->is_looted
=true;
141 loot
->NotifyItemRemoved(lootSlot
);
145 //if only one person is supposed to loot the item, then set it to looted
146 if (!item
->freeforall
)
147 item
->is_looted
= true;
149 --loot
->unlootedCount
;
151 player
->SendNewItem(newitem
, uint32(item
->count
), false, false, true);
152 player
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM
, item
->itemid
, item
->count
);
153 player
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_TYPE
, loot
->loot_type
, item
->count
);
156 player
->SendEquipError( msg
, NULL
, NULL
);
159 void WorldSession::HandleLootMoneyOpcode( WorldPacket
& /*recv_data*/ )
161 sLog
.outDebug("WORLD: CMSG_LOOT_MONEY");
163 Player
*player
= GetPlayer();
164 uint64 guid
= player
->GetLootGUID();
170 switch(GUID_HIPART(guid
))
172 case HIGHGUID_GAMEOBJECT
:
174 GameObject
*pGameObject
= GetPlayer()->GetMap()->GetGameObject(guid
);
176 // not check distance for GO in case owned GO (fishing bobber case, for example)
177 if( pGameObject
&& (pGameObject
->GetOwnerGUID()==_player
->GetGUID() || pGameObject
->IsWithinDistInMap(_player
,INTERACTION_DISTANCE
)) )
178 pLoot
= &pGameObject
->loot
;
182 case HIGHGUID_CORPSE
: // remove insignia ONLY in BG
184 Corpse
*bones
= _player
->GetMap()->GetCorpse(guid
);
186 if (bones
&& bones
->IsWithinDistInMap(_player
,INTERACTION_DISTANCE
) )
187 pLoot
= &bones
->loot
;
193 if(Item
*item
= GetPlayer()->GetItemByGuid(guid
))
199 Creature
* pCreature
= GetPlayer()->GetMap()->GetCreature(guid
);
201 bool ok_loot
= pCreature
&& pCreature
->isAlive() == (player
->getClass()==CLASS_ROGUE
&& pCreature
->lootForPickPocketed
);
203 if ( ok_loot
&& pCreature
->IsWithinDistInMap(_player
,INTERACTION_DISTANCE
) )
204 pLoot
= &pCreature
->loot
;
209 return; // unlootable type
214 if (!IS_ITEM_GUID(guid
) && player
->GetGroup()) //item can be looted only single player
216 Group
*group
= player
->GetGroup();
218 std::vector
<Player
*> playersNear
;
219 for(GroupReference
*itr
= group
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
221 Player
* playerGroup
= itr
->getSource();
224 if (player
->IsWithinDistInMap(playerGroup
,sWorld
.getConfig(CONFIG_GROUP_XP_DISTANCE
),false))
225 playersNear
.push_back(playerGroup
);
228 uint32 money_per_player
= uint32((pLoot
->gold
)/(playersNear
.size()));
230 for (std::vector
<Player
*>::const_iterator i
= playersNear
.begin(); i
!= playersNear
.end(); ++i
)
232 (*i
)->ModifyMoney( money_per_player
);
233 (*i
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY
, money_per_player
);
234 //Offset surely incorrect, but works
235 WorldPacket
data( SMSG_LOOT_MONEY_NOTIFY
, 4 );
236 data
<< uint32(money_per_player
);
237 (*i
)->GetSession()->SendPacket( &data
);
242 player
->ModifyMoney( pLoot
->gold
);
243 player
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY
, pLoot
->gold
);
246 pLoot
->NotifyMoneyRemoved();
250 void WorldSession::HandleLootOpcode( WorldPacket
& recv_data
)
252 sLog
.outDebug("WORLD: CMSG_LOOT");
257 // Check possible cheat
258 if(!_player
->isAlive())
261 GetPlayer()->SendLoot(guid
, LOOT_CORPSE
);
264 void WorldSession::HandleLootReleaseOpcode( WorldPacket
& recv_data
)
266 sLog
.outDebug("WORLD: CMSG_LOOT_RELEASE");
268 // cheaters can modify lguid to prevent correct apply loot release code and re-loot
269 // use internal stored guid
270 recv_data
.read_skip
<uint64
>(); // guid;
272 if(uint64 lguid
= GetPlayer()->GetLootGUID())
273 DoLootRelease(lguid
);
276 void WorldSession::DoLootRelease( uint64 lguid
)
278 Player
*player
= GetPlayer();
281 player
->SetLootGUID(0);
282 player
->SendLootRelease(lguid
);
284 player
->RemoveFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_LOOTING
);
286 if(!player
->IsInWorld())
289 if (IS_GAMEOBJECT_GUID(lguid
))
291 GameObject
*go
= GetPlayer()->GetMap()->GetGameObject(lguid
);
293 // not check distance for GO in case owned GO (fishing bobber case, for example) or Fishing hole GO
294 if (!go
|| ((go
->GetOwnerGUID() != _player
->GetGUID() && go
->GetGoType() != GAMEOBJECT_TYPE_FISHINGHOLE
) && !go
->IsWithinDistInMap(_player
,INTERACTION_DISTANCE
)))
299 if (go
->GetGoType() == GAMEOBJECT_TYPE_DOOR
)
301 // locked doors are opened with spelleffect openlock, prevent remove its as looted
302 go
->UseDoorOrButton();
304 else if (loot
->isLooted() || go
->GetGoType() == GAMEOBJECT_TYPE_FISHINGNODE
)
306 // GO is mineral vein? so it is not removed after its looted
307 if(go
->GetGoType() == GAMEOBJECT_TYPE_CHEST
)
309 uint32 go_min
= go
->GetGOInfo()->chest
.minSuccessOpens
;
310 uint32 go_max
= go
->GetGOInfo()->chest
.maxSuccessOpens
;
312 // only vein pass this check
313 if(go_min
!= 0 && go_max
> go_min
)
315 float amount_rate
= sWorld
.getRate(RATE_MINING_AMOUNT
);
316 float min_amount
= go_min
*amount_rate
;
317 float max_amount
= go_max
*amount_rate
;
320 float uses
= float(go
->GetUseCount());
322 if(uses
< max_amount
)
324 if(uses
>= min_amount
)
326 float chance_rate
= sWorld
.getRate(RATE_MINING_NEXT
);
328 int32 ReqValue
= 175;
329 LockEntry
const *lockInfo
= sLockStore
.LookupEntry(go
->GetGOInfo()->chest
.lockId
);
331 ReqValue
= lockInfo
->Skill
[0];
332 float skill
= float(player
->GetSkillValue(SKILL_MINING
))/(ReqValue
+25);
333 double chance
= pow(0.8*chance_rate
,4*(1/double(max_amount
))*double(uses
));
334 if(roll_chance_f(100*chance
+skill
))
336 go
->SetLootState(GO_READY
);
338 else // not have more uses
339 go
->SetLootState(GO_JUST_DEACTIVATED
);
341 else // 100% chance until min uses
342 go
->SetLootState(GO_READY
);
344 else // max uses already
345 go
->SetLootState(GO_JUST_DEACTIVATED
);
348 go
->SetLootState(GO_JUST_DEACTIVATED
);
350 else if (go
->GetGoType() == GAMEOBJECT_TYPE_FISHINGHOLE
)
351 { // The fishing hole used once more
352 go
->AddUse(); // if the max usage is reached, will be despawned in next tick
353 if (go
->GetUseCount() >= irand(go
->GetGOInfo()->fishinghole
.minSuccessOpens
,go
->GetGOInfo()->fishinghole
.maxSuccessOpens
))
355 go
->SetLootState(GO_JUST_DEACTIVATED
);
358 go
->SetLootState(GO_READY
);
360 else // not chest (or vein/herb/etc)
361 go
->SetLootState(GO_JUST_DEACTIVATED
);
366 // not fully looted object
367 go
->SetLootState(GO_ACTIVATED
);
369 else if (IS_CORPSE_GUID(lguid
)) // ONLY remove insignia at BG
371 Corpse
*corpse
= _player
->GetMap()->GetCorpse(lguid
);
372 if (!corpse
|| !corpse
->IsWithinDistInMap(_player
,INTERACTION_DISTANCE
) )
375 loot
= &corpse
->loot
;
377 if (loot
->isLooted())
380 corpse
->RemoveFlag(CORPSE_FIELD_DYNAMIC_FLAGS
, CORPSE_DYNFLAG_LOOTABLE
);
383 else if (IS_ITEM_GUID(lguid
))
385 Item
*pItem
= player
->GetItemByGuid(lguid
);
389 ItemPrototype
const* proto
= pItem
->GetProto();
391 // destroy only 5 items from stack in case prospecting and milling
392 if( (proto
->BagFamily
& (BAG_FAMILY_MASK_MINING_SUPP
|BAG_FAMILY_MASK_HERBS
)) &&
393 proto
->Class
== ITEM_CLASS_TRADE_GOODS
)
395 pItem
->m_lootGenerated
= false;
398 uint32 count
= pItem
->GetCount();
400 // >=5 checked in spell code, but will work for cheating cases also with removing from another stacks.
404 player
->DestroyItemCount(pItem
, count
, true);
407 // 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.
408 player
->DestroyItem( pItem
->GetBagSlot(),pItem
->GetSlot(), true);
409 return; // item can be looted only single player
413 Creature
* pCreature
= GetPlayer()->GetMap()->GetCreature(lguid
);
415 bool ok_loot
= pCreature
&& pCreature
->isAlive() == (player
->getClass()==CLASS_ROGUE
&& pCreature
->lootForPickPocketed
);
416 if ( !ok_loot
|| !pCreature
->IsWithinDistInMap(_player
,INTERACTION_DISTANCE
) )
419 loot
= &pCreature
->loot
;
421 // update next looter
422 if(Player
*recipient
= pCreature
->GetLootRecipient())
423 if(Group
* group
= recipient
->GetGroup())
424 if (group
->GetLooterGuid() == player
->GetGUID())
425 group
->UpdateLooterGuid(pCreature
);
427 if (loot
->isLooted())
429 // skip pickpocketing loot for speed, skinning timer redunction is no-op in fact
430 if(!pCreature
->isAlive())
431 pCreature
->AllLootRemovedFromCorpse();
433 pCreature
->RemoveFlag(UNIT_DYNAMIC_FLAGS
, UNIT_DYNFLAG_LOOTABLE
);
438 //Player is not looking at loot list, he doesn't need to see updates on the loot list
439 loot
->RemoveLooter(player
->GetGUID());
442 void WorldSession::HandleLootMasterGiveOpcode( WorldPacket
& recv_data
)
445 uint64 lootguid
, target_playerguid
;
447 recv_data
>> lootguid
>> slotid
>> target_playerguid
;
449 if(!_player
->GetGroup() || _player
->GetGroup()->GetLooterGuid() != _player
->GetGUID())
451 _player
->SendLootRelease(GetPlayer()->GetLootGUID());
455 Player
*target
= ObjectAccessor::FindPlayer(MAKE_NEW_GUID(target_playerguid
, 0, HIGHGUID_PLAYER
));
459 sLog
.outDebug("WorldSession::HandleLootMasterGiveOpcode (CMSG_LOOT_MASTER_GIVE, 0x02A3) Target = [%s].", target
->GetName());
461 if(_player
->GetLootGUID() != lootguid
)
466 if(IS_CREATURE_GUID(GetPlayer()->GetLootGUID()))
468 Creature
*pCreature
= GetPlayer()->GetMap()->GetCreature(lootguid
);
472 pLoot
= &pCreature
->loot
;
474 else if(IS_GAMEOBJECT_GUID(GetPlayer()->GetLootGUID()))
476 GameObject
*pGO
= GetPlayer()->GetMap()->GetGameObject(lootguid
);
486 if (slotid
> pLoot
->items
.size())
488 sLog
.outDebug("AutoLootItem: Player %s might be using a hack! (slot %d, size %lu)",GetPlayer()->GetName(), slotid
, (unsigned long)pLoot
->items
.size());
492 LootItem
& item
= pLoot
->items
[slotid
];
494 ItemPosCountVec dest
;
495 uint8 msg
= target
->CanStoreNewItem( NULL_BAG
, NULL_SLOT
, dest
, item
.itemid
, item
.count
);
496 if ( msg
!= EQUIP_ERR_OK
)
498 target
->SendEquipError( msg
, NULL
, NULL
);
499 _player
->SendEquipError( msg
, NULL
, NULL
); // send duplicate of error massage to master looter
503 // now move item from loot to target inventory
504 Item
* newitem
= target
->StoreNewItem( dest
, item
.itemid
, true, item
.randomPropertyId
);
505 target
->SendNewItem(newitem
, uint32(item
.count
), false, false, true );
506 target
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_ITEM
, item
.itemid
, item
.count
);
507 target
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_TYPE
, pLoot
->loot_type
, item
.count
);
513 pLoot
->NotifyItemRemoved(slotid
);
514 --pLoot
->unlootedCount
;