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 "WorldSession.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();
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
);
56 else if (IS_ITEM_GUID(lguid
))
58 Item
*pItem
= player
->GetItemByGuid( lguid
);
62 player
->SendLootRelease(lguid
);
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
);
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
);
91 player
->SendEquipError( EQUIP_ERR_ALREADY_LOOTED
, NULL
, NULL
);
95 // questitems use the blocked field for other purposes
96 if (!qitem
&& item
->is_blocked
)
98 player
->SendLootRelease(lguid
);
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
);
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
);
115 loot
->NotifyQuestItemRemoved(qitem
->index
);
121 //freeforall case, notify only one player of the removal
122 ffaitem
->is_looted
=true;
123 player
->SendNotifyLootItemRemoved(lootSlot
);
127 //not freeforall, notify everyone
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
);
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();
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
;
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
;
182 if(Item
*item
= GetPlayer()->GetItemByGuid(guid
))
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
;
198 return; // unlootable type
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();
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
);
231 player
->ModifyMoney( pLoot
->gold
);
232 player
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_LOOT_MONEY
, pLoot
->gold
);
235 pLoot
->NotifyMoneyRemoved();
239 void WorldSession::HandleLootOpcode( WorldPacket
& recv_data
)
241 sLog
.outDebug("WORLD: CMSG_LOOT");
246 // Check possible cheat
247 if(!_player
->isAlive())
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();
270 player
->SetLootGUID(0);
271 player
->SendLootRelease(lguid
);
273 player
->RemoveFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_LOOTING
);
275 if(!player
->IsInWorld())
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
)))
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
;
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
);
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
);
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
);
347 go
->SetLootState(GO_READY
);
349 else // not chest (or vein/herb/etc)
350 go
->SetLootState(GO_JUST_DEACTIVATED
);
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
) )
364 loot
= &corpse
->loot
;
366 if (loot
->isLooted())
369 corpse
->RemoveFlag(CORPSE_FIELD_DYNAMIC_FLAGS
, CORPSE_DYNFLAG_LOOTABLE
);
372 else if (IS_ITEM_GUID(lguid
))
374 Item
*pItem
= player
->GetItemByGuid(lguid
);
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;
387 uint32 count
= pItem
->GetCount();
389 // >=5 checked in spell code, but will work for cheating cases also with removing from another stacks.
393 player
->DestroyItemCount(pItem
, count
, true);
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
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
) )
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
);
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
)
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());
444 Player
*target
= ObjectAccessor::FindPlayer(MAKE_NEW_GUID(target_playerguid
, 0, HIGHGUID_PLAYER
));
448 sLog
.outDebug("WorldSession::HandleLootMasterGiveOpcode (CMSG_LOOT_MASTER_GIVE, 0x02A3) Target = [%s].", target
->GetName());
450 if(_player
->GetLootGUID() != lootguid
)
455 if(IS_CREATURE_GUID(GetPlayer()->GetLootGUID()))
457 Creature
*pCreature
= GetPlayer()->GetMap()->GetCreature(lootguid
);
461 pLoot
= &pCreature
->loot
;
463 else if(IS_GAMEOBJECT_GUID(GetPlayer()->GetLootGUID()))
465 GameObject
*pGO
= GetPlayer()->GetMap()->GetGameObject(lootguid
);
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());
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
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
);
503 pLoot
->NotifyItemRemoved(slotid
);
504 --pLoot
->unlootedCount
;