2 * Copyright (C) 2005-2008 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 "Database/DBCStores.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
24 #include "ObjectMgr.h"
29 #include "SpellAuras.h"
30 #include "BattleGround.h"
31 #include "MapManager.h"
32 #include "ScriptCalls.h"
35 void WorldSession::HandleUseItemOpcode(WorldPacket
& recvPacket
)
37 // TODO: add targets.read() check
38 CHECK_PACKET_SIZE(recvPacket
,1+1+1+1+8);
40 Player
* pUser
= _player
;
42 uint8 spell_count
; // number of spells at item, not used
43 uint8 cast_count
; // next cast if exists (single or not)
46 recvPacket
>> bagIndex
>> slot
>> spell_count
>> cast_count
>> item_guid
;
48 Item
*pItem
= pUser
->GetItemByPos(bagIndex
, slot
);
51 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, NULL
, NULL
);
55 if(pItem
->GetGUID() != item_guid
)
57 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, NULL
, NULL
);
61 sLog
.outDetail("WORLD: CMSG_USE_ITEM packet, bagIndex: %u, slot: %u, spell_count: %u , cast_count: %u, Item: %u, data length = %i", bagIndex
, slot
, spell_count
, cast_count
, pItem
->GetEntry(), recvPacket
.size());
63 ItemPrototype
const *proto
= pItem
->GetProto();
66 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, pItem
, NULL
);
70 // some item classes can be used only in equipped state
71 if(proto
->InventoryType
!= INVTYPE_NON_EQUIP
&& !pItem
->IsEquipped())
73 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, pItem
, NULL
);
77 uint8 msg
= pUser
->CanUseItem(pItem
);
78 if( msg
!= EQUIP_ERR_OK
)
80 pUser
->SendEquipError( msg
, pItem
, NULL
);
84 // only allow conjured consumable, bandage, poisons (all should have the 2^21 item flag set in DB)
85 if( proto
->Class
== ITEM_CLASS_CONSUMABLE
&&
86 !(proto
->Flags
& ITEM_FLAGS_USEABLE_IN_ARENA
) &&
89 pUser
->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH
,pItem
,NULL
);
93 if (pUser
->isInCombat())
95 for(int i
= 0; i
< 5; ++i
)
97 if (SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(proto
->Spells
[i
].SpellId
))
99 if (IsNonCombatSpell(spellInfo
))
101 pUser
->SendEquipError(EQUIP_ERR_NOT_IN_COMBAT
,pItem
,NULL
);
108 // check also BIND_WHEN_PICKED_UP and BIND_QUEST_ITEM for .additem or .additemset case by GM (not binded at adding to inventory)
109 if( pItem
->GetProto()->Bonding
== BIND_WHEN_USE
|| pItem
->GetProto()->Bonding
== BIND_WHEN_PICKED_UP
|| pItem
->GetProto()->Bonding
== BIND_QUEST_ITEM
)
111 if (!pItem
->IsSoulBound())
113 pItem
->SetState(ITEM_CHANGED
, pUser
);
114 pItem
->SetBinding( true );
118 SpellCastTargets targets
;
119 if(!targets
.read(&recvPacket
, pUser
))
122 //Note: If script stop casting it must send appropriate data to client to prevent stuck item in gray state.
123 if(!Script
->ItemUse(pUser
,pItem
,targets
))
125 // no script or script not process request by self
127 // special learning case
128 if(pItem
->GetProto()->Spells
[0].SpellId
==SPELL_ID_GENERIC_LEARN
)
130 uint32 learning_spell_id
= pItem
->GetProto()->Spells
[1].SpellId
;
132 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(SPELL_ID_GENERIC_LEARN
);
135 sLog
.outError("Item (Entry: %u) in have wrong spell id %u, ignoring ",proto
->ItemId
, SPELL_ID_GENERIC_LEARN
);
136 pUser
->SendEquipError(EQUIP_ERR_NONE
,pItem
,NULL
);
140 Spell
*spell
= new Spell(pUser
, spellInfo
, false);
141 spell
->m_CastItem
= pItem
;
142 spell
->m_cast_count
= cast_count
; //set count of casts
143 spell
->m_currentBasePoints
[0] = learning_spell_id
;
144 spell
->prepare(&targets
);
148 // use triggered flag only for items with many spell casts and for not first cast
151 for(int i
= 0; i
< 5; ++i
)
153 _Spell
const& spellData
= pItem
->GetProto()->Spells
[i
];
156 if(!spellData
.SpellId
)
159 // wrong triggering type
160 if( spellData
.SpellTrigger
!= ITEM_SPELLTRIGGER_ON_USE
&& spellData
.SpellTrigger
!= ITEM_SPELLTRIGGER_ON_NO_DELAY_USE
)
163 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellData
.SpellId
);
166 sLog
.outError("Item (Entry: %u) in have wrong spell id %u, ignoring ",proto
->ItemId
, spellData
.SpellId
);
170 Spell
*spell
= new Spell(pUser
, spellInfo
, (count
> 0));
171 spell
->m_CastItem
= pItem
;
172 spell
->m_cast_count
= cast_count
; //set count of casts
173 spell
->prepare(&targets
);
180 #define OPEN_CHEST 11437
181 #define OPEN_SAFE 11535
182 #define OPEN_CAGE 11792
183 #define OPEN_BOOTY_CHEST 5107
184 #define OPEN_STRONGBOX 8517
186 void WorldSession::HandleOpenItemOpcode(WorldPacket
& recvPacket
)
188 CHECK_PACKET_SIZE(recvPacket
,1+1);
190 sLog
.outDetail("WORLD: CMSG_OPEN_ITEM packet, data length = %i",recvPacket
.size());
192 Player
* pUser
= _player
;
193 uint8 bagIndex
, slot
;
195 recvPacket
>> bagIndex
>> slot
;
197 sLog
.outDetail("bagIndex: %u, slot: %u",bagIndex
,slot
);
199 Item
*pItem
= pUser
->GetItemByPos(bagIndex
, slot
);
202 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, NULL
, NULL
);
206 ItemPrototype
const *proto
= pItem
->GetProto();
209 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, pItem
, NULL
);
214 uint32 lockId
= proto
->LockID
;
217 LockEntry
const *lockInfo
= sLockStore
.LookupEntry(lockId
);
221 pUser
->SendEquipError(EQUIP_ERR_ITEM_LOCKED
, pItem
, NULL
);
222 sLog
.outError( "WORLD::OpenItem: item [guid = %u] has an unknown lockId: %u!", pItem
->GetGUIDLow() , lockId
);
226 // required picklocking
227 if(lockInfo
->requiredlockskill
|| lockInfo
->requiredminingskill
)
229 pUser
->SendEquipError(EQUIP_ERR_ITEM_LOCKED
, pItem
, NULL
);
234 if(pItem
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_WRAPPED
))// wrapped?
236 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT entry, flags FROM character_gifts WHERE item_guid = '%u'", pItem
->GetGUIDLow());
239 Field
*fields
= result
->Fetch();
240 uint32 entry
= fields
[0].GetUInt32();
241 uint32 flags
= fields
[1].GetUInt32();
243 pItem
->SetUInt64Value(ITEM_FIELD_GIFTCREATOR
, 0);
244 pItem
->SetEntry(entry
);
245 pItem
->SetUInt32Value(ITEM_FIELD_FLAGS
, flags
);
246 pItem
->SetState(ITEM_CHANGED
, pUser
);
251 sLog
.outError("Wrapped item %u don't have record in character_gifts table and will deleted", pItem
->GetGUIDLow());
252 pUser
->DestroyItem(pItem
->GetBagSlot(), pItem
->GetSlot(), true);
255 CharacterDatabase
.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", pItem
->GetGUIDLow());
258 pUser
->SendLoot(pItem
->GetGUID(),LOOT_CORPSE
);
261 void WorldSession::HandleGameObjectUseOpcode( WorldPacket
& recv_data
)
263 CHECK_PACKET_SIZE(recv_data
,8);
269 sLog
.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_USE Message [guid=%u]", GUID_LOPART(guid
));
270 GameObject
*obj
= ObjectAccessor::GetGameObject(*_player
, guid
);
275 if (Script
->GOHello(_player
, obj
))
281 void WorldSession::HandleCastSpellOpcode(WorldPacket
& recvPacket
)
283 CHECK_PACKET_SIZE(recvPacket
,4+1+2);
287 recvPacket
>> spellId
;
288 recvPacket
>> cast_count
;
290 sLog
.outDebug("WORLD: got cast spell packet, spellId - %u, cast_count: %u data length = %i",
291 spellId
, cast_count
, recvPacket
.size());
293 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
297 sLog
.outError("WORLD: unknown spell id %u", spellId
);
301 // not have spell or spell passive and not casted by client
302 if ( !_player
->HasSpell (spellId
) || IsPassiveSpell(spellId
) )
304 //cheater? kick? ban?
308 // client provided targets
309 SpellCastTargets targets
;
310 if(!targets
.read(&recvPacket
,_player
))
313 // auto-selection buff level base at target level (in spellInfo)
314 if(targets
.getUnitTarget())
316 SpellEntry
const *actualSpellInfo
= spellmgr
.SelectAuraRankForPlayerLevel(spellInfo
,targets
.getUnitTarget()->getLevel());
318 // if rank not found then function return NULL but in explicit cast case original spell can be casted and later failed with appropriate error message
320 spellInfo
= actualSpellInfo
;
323 Spell
*spell
= new Spell(_player
, spellInfo
, false);
324 spell
->m_cast_count
= cast_count
; // set count of casts
325 spell
->prepare(&targets
);
328 void WorldSession::HandleCancelCastOpcode(WorldPacket
& recvPacket
)
330 CHECK_PACKET_SIZE(recvPacket
,4);
333 recvPacket
>> spellId
;
335 //FIXME: hack, ignore unexpected client cancel Deadly Throw cast
339 if(_player
->IsNonMeleeSpellCasted(false))
340 _player
->InterruptNonMeleeSpells(false,spellId
);
343 void WorldSession::HandleCancelAuraOpcode( WorldPacket
& recvPacket
)
345 CHECK_PACKET_SIZE(recvPacket
,4);
348 recvPacket
>> spellId
;
350 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
354 // not allow remove non positive spells and spells with attr SPELL_ATTR_CANT_CANCEL
355 if(!IsPositiveSpell(spellId
) || (spellInfo
->Attributes
& SPELL_ATTR_CANT_CANCEL
))
358 // channeled spell case (it currently casted then)
359 if(IsChanneledSpell(spellInfo
))
361 if(Spell
* spell
= _player
->m_currentSpells
[CURRENT_CHANNELED_SPELL
])
363 if(spell
->m_spellInfo
->Id
==spellId
)
366 spell
->SetReferencedFromCurrent(false);
367 _player
->m_currentSpells
[CURRENT_CHANNELED_SPELL
] = NULL
;
373 // non channeled case
374 _player
->RemoveAurasDueToSpellByCancel(spellId
);
377 void WorldSession::HandlePetCancelAuraOpcode( WorldPacket
& recvPacket
)
379 CHECK_PACKET_SIZE(recvPacket
, 8+4);
385 recvPacket
>> spellId
;
387 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
390 sLog
.outError("WORLD: unknown PET spell id %u", spellId
);
394 Creature
* pet
=ObjectAccessor::GetCreatureOrPet(*_player
,guid
);
398 sLog
.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid
)) );
402 if(pet
!= GetPlayer()->GetPet() && pet
!= GetPlayer()->GetCharm())
404 sLog
.outError( "HandlePetCancelAura.Pet %u isn't pet of player %s", uint32(GUID_LOPART(guid
)),GetPlayer()->GetName() );
410 pet
->SendPetActionFeedback(FEEDBACK_PET_DEAD
);
414 pet
->RemoveAurasDueToSpell(spellId
);
416 pet
->AddCreatureSpellCooldown(spellId
);
419 void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket
& /*recvPacket*/)
424 void WorldSession::HandleCancelAutoRepeatSpellOpcode( WorldPacket
& /*recvPacket*/)
426 // may be better send SMSG_CANCEL_AUTO_REPEAT?
427 // cancel and prepare for deleting
428 _player
->InterruptSpell(CURRENT_AUTOREPEAT_SPELL
);
431 /// \todo Complete HandleCancelChanneling function
432 void WorldSession::HandleCancelChanneling( WorldPacket
& /*recv_data */)
435 CHECK_PACKET_SIZE(recv_data, 4);
438 recv_data >> spellid;
442 void WorldSession::HandleTotemDestroy( WorldPacket
& recvPacket
)
444 CHECK_PACKET_SIZE(recvPacket
, 1);
448 recvPacket
>> slotId
;
450 if (slotId
>= MAX_TOTEM
)
453 if(!_player
->m_TotemSlot
[slotId
])
456 Creature
* totem
= ObjectAccessor::GetCreature(*_player
,_player
->m_TotemSlot
[slotId
]);
457 if(totem
&& totem
->isTotem())
458 ((Totem
*)totem
)->UnSummon();
461 void WorldSession::HandleSelfResOpcode( WorldPacket
& /*recv_data*/ )
463 sLog
.outDebug("WORLD: CMSG_SELF_RES"); // empty opcode
465 if(_player
->GetUInt32Value(PLAYER_SELF_RES_SPELL
))
467 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(_player
->GetUInt32Value(PLAYER_SELF_RES_SPELL
));
469 _player
->CastSpell(_player
,spellInfo
,false,0);
471 _player
->SetUInt32Value(PLAYER_SELF_RES_SPELL
, 0);