2 * Copyright (C) 2005-2010 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 "DBCStores.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "ObjectMgr.h"
28 #include "ScriptCalls.h"
30 #include "SpellAuras.h"
32 void WorldSession::HandleUseItemOpcode(WorldPacket
& recvPacket
)
34 // TODO: add targets.read() check
35 Player
* pUser
= _player
;
37 // ignore for remote control state
38 if(pUser
->m_mover
!= pUser
)
42 uint8 unk_flags
; // flags (if 0x02 - some additional data are received)
43 uint8 cast_count
; // next cast if exists (single or not)
45 uint32 glyphIndex
; // something to do with glyphs?
46 uint32 spellid
; // casted spell id
48 recvPacket
>> bagIndex
>> slot
>> cast_count
>> spellid
>> item_guid
>> glyphIndex
>> unk_flags
;
51 if (glyphIndex
>= MAX_GLYPH_SLOT_INDEX
)
53 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, NULL
, NULL
);
57 Item
*pItem
= pUser
->GetItemByPos(bagIndex
, slot
);
60 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, NULL
, NULL
);
64 if (pItem
->GetGUID() != item_guid
)
66 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, NULL
, NULL
);
70 sLog
.outDetail("WORLD: CMSG_USE_ITEM packet, bagIndex: %u, slot: %u, cast_count: %u, spellid: %u, Item: %u, glyphIndex: %u, unk_flags: %u, data length = %i", bagIndex
, slot
, cast_count
, spellid
, pItem
->GetEntry(), glyphIndex
, unk_flags
, (uint32
)recvPacket
.size());
72 ItemPrototype
const *proto
= pItem
->GetProto();
75 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, pItem
, NULL
);
79 // some item classes can be used only in equipped state
80 if (proto
->InventoryType
!= INVTYPE_NON_EQUIP
&& !pItem
->IsEquipped())
82 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, pItem
, NULL
);
86 uint8 msg
= pUser
->CanUseItem(pItem
);
87 if (msg
!= EQUIP_ERR_OK
)
89 pUser
->SendEquipError( msg
, pItem
, NULL
);
93 // only allow conjured consumable, bandage, poisons (all should have the 2^21 item flag set in DB)
94 if (proto
->Class
== ITEM_CLASS_CONSUMABLE
&&
95 !(proto
->Flags
& ITEM_FLAGS_USEABLE_IN_ARENA
) &&
98 pUser
->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH
,pItem
,NULL
);
102 if (pUser
->isInCombat())
104 for(int i
= 0; i
< MAX_ITEM_PROTO_SPELLS
; ++i
)
106 if (SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(proto
->Spells
[i
].SpellId
))
108 if (IsNonCombatSpell(spellInfo
))
110 pUser
->SendEquipError(EQUIP_ERR_NOT_IN_COMBAT
,pItem
,NULL
);
117 // check also BIND_WHEN_PICKED_UP and BIND_QUEST_ITEM for .additem or .additemset case by GM (not binded at adding to inventory)
118 if( pItem
->GetProto()->Bonding
== BIND_WHEN_USE
|| pItem
->GetProto()->Bonding
== BIND_WHEN_PICKED_UP
|| pItem
->GetProto()->Bonding
== BIND_QUEST_ITEM
)
120 if (!pItem
->IsSoulBound())
122 pItem
->SetState(ITEM_CHANGED
, pUser
);
123 pItem
->SetBinding( true );
127 SpellCastTargets targets
;
129 recvPacket
>> targets
.ReadForCaster(pUser
);
131 targets
.Update(pUser
);
133 if (!pItem
->IsTargetValidForItemUse(targets
.getUnitTarget()))
135 // free gray item after use fail
136 pUser
->SendEquipError(EQUIP_ERR_NONE
, pItem
, NULL
);
139 if (SpellEntry
const* spellInfo
= sSpellStore
.LookupEntry(spellid
))
141 // for implicit area/coord target spells
142 if (IsPointEffectTarget(Targets(spellInfo
->EffectImplicitTargetA
[EFFECT_INDEX_0
])) ||
143 IsAreaEffectTarget(Targets(spellInfo
->EffectImplicitTargetA
[EFFECT_INDEX_0
])))
144 Spell::SendCastResult(_player
,spellInfo
,cast_count
,SPELL_FAILED_NO_VALID_TARGETS
);
145 // for explicit target spells
147 Spell::SendCastResult(_player
,spellInfo
,cast_count
,SPELL_FAILED_BAD_TARGETS
);
152 //Note: If script stop casting it must send appropriate data to client to prevent stuck item in gray state.
153 if(!Script
->ItemUse(pUser
,pItem
,targets
))
155 // no script or script not process request by self
156 pUser
->CastItemUseSpell(pItem
,targets
,cast_count
,glyphIndex
);
160 #define OPEN_CHEST 11437
161 #define OPEN_SAFE 11535
162 #define OPEN_CAGE 11792
163 #define OPEN_BOOTY_CHEST 5107
164 #define OPEN_STRONGBOX 8517
166 void WorldSession::HandleOpenItemOpcode(WorldPacket
& recvPacket
)
168 sLog
.outDetail("WORLD: CMSG_OPEN_ITEM packet, data length = %i",(uint32
)recvPacket
.size());
170 Player
* pUser
= _player
;
172 // ignore for remote control state
173 if(pUser
->m_mover
!= pUser
)
176 uint8 bagIndex
, slot
;
178 recvPacket
>> bagIndex
>> slot
;
180 sLog
.outDetail("bagIndex: %u, slot: %u",bagIndex
,slot
);
182 Item
*pItem
= pUser
->GetItemByPos(bagIndex
, slot
);
185 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, NULL
, NULL
);
189 ItemPrototype
const *proto
= pItem
->GetProto();
192 pUser
->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND
, pItem
, NULL
);
197 uint32 lockId
= proto
->LockID
;
200 LockEntry
const *lockInfo
= sLockStore
.LookupEntry(lockId
);
204 pUser
->SendEquipError(EQUIP_ERR_ITEM_LOCKED
, pItem
, NULL
);
205 sLog
.outError( "WORLD::OpenItem: item [guid = %u] has an unknown lockId: %u!", pItem
->GetGUIDLow() , lockId
);
209 // required picklocking
210 if(lockInfo
->Skill
[1] || lockInfo
->Skill
[0])
212 pUser
->SendEquipError(EQUIP_ERR_ITEM_LOCKED
, pItem
, NULL
);
217 if(pItem
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_WRAPPED
))// wrapped?
219 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT entry, flags FROM character_gifts WHERE item_guid = '%u'", pItem
->GetGUIDLow());
222 Field
*fields
= result
->Fetch();
223 uint32 entry
= fields
[0].GetUInt32();
224 uint32 flags
= fields
[1].GetUInt32();
226 pItem
->SetUInt64Value(ITEM_FIELD_GIFTCREATOR
, 0);
227 pItem
->SetEntry(entry
);
228 pItem
->SetUInt32Value(ITEM_FIELD_FLAGS
, flags
);
229 pItem
->SetState(ITEM_CHANGED
, pUser
);
234 sLog
.outError("Wrapped item %u don't have record in character_gifts table and will deleted", pItem
->GetGUIDLow());
235 pUser
->DestroyItem(pItem
->GetBagSlot(), pItem
->GetSlot(), true);
238 CharacterDatabase
.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", pItem
->GetGUIDLow());
241 pUser
->SendLoot(pItem
->GetGUID(),LOOT_CORPSE
);
244 void WorldSession::HandleGameObjectUseOpcode( WorldPacket
& recv_data
)
250 sLog
.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_USE Message [guid=%u]", GUID_LOPART(guid
));
252 // ignore for remote control state
253 if(_player
->m_mover
!= _player
)
256 GameObject
*obj
= GetPlayer()->GetMap()->GetGameObject(guid
);
264 void WorldSession::HandleGameobjectReportUse(WorldPacket
& recvPacket
)
269 sLog
.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_REPORT_USE Message [in game guid: %u]", GUID_LOPART(guid
));
271 // ignore for remote control state
272 if(_player
->m_mover
!= _player
)
275 GameObject
* go
= GetPlayer()->GetMap()->GetGameObject(guid
);
279 if(!go
->IsWithinDistInMap(_player
,INTERACTION_DISTANCE
))
282 _player
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_USE_GAMEOBJECT
, go
->GetEntry());
285 void WorldSession::HandleCastSpellOpcode(WorldPacket
& recvPacket
)
288 uint8 cast_count
, unk_flags
;
289 recvPacket
>> cast_count
;
290 recvPacket
>> spellId
;
291 recvPacket
>> unk_flags
; // flags (if 0x02 - some additional data are received)
293 // ignore for remote control state (for player case)
294 Unit
* mover
= _player
->m_mover
;
295 if(mover
!= _player
&& mover
->GetTypeId()==TYPEID_PLAYER
)
297 recvPacket
.rpos(recvPacket
.wpos()); // prevent spam at ignore packet
301 sLog
.outDebug("WORLD: got cast spell packet, spellId - %u, cast_count: %u, unk_flags %u, data length = %i",
302 spellId
, cast_count
, unk_flags
, (uint32
)recvPacket
.size());
304 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
308 sLog
.outError("WORLD: unknown spell id %u", spellId
);
309 recvPacket
.rpos(recvPacket
.wpos()); // prevent spam at ignore packet
313 if(mover
->GetTypeId()==TYPEID_PLAYER
)
315 // not have spell in spellbook or spell passive and not casted by client
316 if (!((Player
*)mover
)->HasActiveSpell (spellId
) || IsPassiveSpell(spellId
) )
318 sLog
.outError("World: Player %u casts spell %u which he shouldn't have", mover
->GetGUIDLow(), spellId
);
319 //cheater? kick? ban?
320 recvPacket
.rpos(recvPacket
.wpos()); // prevent spam at ignore packet
326 // not have spell in spellbook or spell passive and not casted by client
327 if (!((Creature
*)mover
)->HasSpell(spellId
) || IsPassiveSpell(spellId
) )
329 //cheater? kick? ban?
330 recvPacket
.rpos(recvPacket
.wpos()); // prevent spam at ignore packet
335 // client provided targets
336 SpellCastTargets targets
;
338 recvPacket
>> targets
.ReadForCaster(mover
);
340 // some spell cast packet including more data (for projectiles?)
341 if (unk_flags
& 0x02)
345 recvPacket
>> Unused
<float>(); // unk1, coords?
346 recvPacket
>> Unused
<float>(); // unk1, coords?
347 recvPacket
>> unk1
; // >> 1 or 0
350 ObjectGuid guid
; // guid - unused
351 MovementInfo movementInfo
;
353 recvPacket
>> Unused
<uint32
>(); // >> MSG_MOVE_STOP
354 recvPacket
>> guid
.ReadAsPacked();
355 recvPacket
>> movementInfo
;
359 // auto-selection buff level base at target level (in spellInfo)
360 if(targets
.getUnitTarget())
362 SpellEntry
const *actualSpellInfo
= sSpellMgr
.SelectAuraRankForPlayerLevel(spellInfo
,targets
.getUnitTarget()->getLevel());
364 // 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
366 spellInfo
= actualSpellInfo
;
369 Spell
*spell
= new Spell(mover
, spellInfo
, false);
370 spell
->m_cast_count
= cast_count
; // set count of casts
371 spell
->prepare(&targets
);
374 void WorldSession::HandleCancelCastOpcode(WorldPacket
& recvPacket
)
376 // ignore for remote control state (for player case)
377 Unit
* mover
= _player
->m_mover
;
378 if(mover
!= _player
&& mover
->GetTypeId()==TYPEID_PLAYER
)
383 recvPacket
.read_skip
<uint8
>(); // counter, increments with every CANCEL packet, don't use for now
384 recvPacket
>> spellId
;
386 //FIXME: hack, ignore unexpected client cancel Deadly Throw cast
390 if(mover
->IsNonMeleeSpellCasted(false))
391 mover
->InterruptNonMeleeSpells(false,spellId
);
394 void WorldSession::HandleCancelAuraOpcode( WorldPacket
& recvPacket
)
397 recvPacket
>> spellId
;
399 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
403 if (spellInfo
->Attributes
& SPELL_ATTR_CANT_CANCEL
)
406 if(!IsPositiveSpell(spellId
))
408 // ignore for remote control state
409 if (_player
->m_mover
!= _player
)
411 // except own aura spells
413 for(int k
= 0; k
< MAX_EFFECT_INDEX
; ++k
)
415 if (spellInfo
->EffectApplyAuraName
[k
] == SPELL_AURA_MOD_POSSESS
||
416 spellInfo
->EffectApplyAuraName
[k
] == SPELL_AURA_MOD_POSSESS_PET
)
423 // this also include case when aura not found
431 // channeled spell case (it currently casted then)
432 if (IsChanneledSpell(spellInfo
))
434 if (Spell
* curSpell
= _player
->GetCurrentSpell(CURRENT_CHANNELED_SPELL
))
435 if (curSpell
->m_spellInfo
->Id
==spellId
)
436 _player
->InterruptSpell(CURRENT_CHANNELED_SPELL
);
440 // non channeled case
441 _player
->RemoveAurasDueToSpellByCancel(spellId
);
444 void WorldSession::HandlePetCancelAuraOpcode( WorldPacket
& recvPacket
)
446 // ignore for remote control state
447 if(_player
->m_mover
!= _player
)
454 recvPacket
>> spellId
;
456 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
459 sLog
.outError("WORLD: unknown PET spell id %u", spellId
);
463 Creature
* pet
= GetPlayer()->GetMap()->GetCreatureOrPetOrVehicle(guid
);
467 sLog
.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid
)) );
471 if(pet
!= GetPlayer()->GetPet() && pet
!= GetPlayer()->GetCharm())
473 sLog
.outError( "HandlePetCancelAura.Pet %u isn't pet of player %s", uint32(GUID_LOPART(guid
)),GetPlayer()->GetName() );
479 pet
->SendPetActionFeedback(FEEDBACK_PET_DEAD
);
483 pet
->RemoveAurasDueToSpell(spellId
);
485 pet
->AddCreatureSpellCooldown(spellId
);
488 void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket
& /*recvPacket*/)
493 void WorldSession::HandleCancelAutoRepeatSpellOpcode( WorldPacket
& /*recvPacket*/)
495 // cancel and prepare for deleting
496 // do not send SMSG_CANCEL_AUTO_REPEAT! client will send this Opcode again (loop)
497 _player
->m_mover
->InterruptSpell(CURRENT_AUTOREPEAT_SPELL
, true, false);
500 void WorldSession::HandleCancelChanneling( WorldPacket
& recv_data
)
502 recv_data
.read_skip
<uint32
>(); // spellid, not used
504 // ignore for remote control state (for player case)
505 Unit
* mover
= _player
->m_mover
;
506 if(mover
!= _player
&& mover
->GetTypeId()==TYPEID_PLAYER
)
509 mover
->InterruptSpell(CURRENT_CHANNELED_SPELL
);
512 void WorldSession::HandleTotemDestroyed( WorldPacket
& recvPacket
)
514 // ignore for remote control state
515 if(_player
->m_mover
!= _player
)
520 recvPacket
>> slotId
;
522 if (int(slotId
) >= MAX_TOTEM_SLOT
)
525 if (Totem
* totem
= GetPlayer()->GetTotem(TotemSlot(slotId
)))
529 void WorldSession::HandleSelfResOpcode( WorldPacket
& /*recv_data*/ )
531 sLog
.outDebug("WORLD: CMSG_SELF_RES"); // empty opcode
533 if(_player
->GetUInt32Value(PLAYER_SELF_RES_SPELL
))
535 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(_player
->GetUInt32Value(PLAYER_SELF_RES_SPELL
));
537 _player
->CastSpell(_player
, spellInfo
, false);
539 _player
->SetUInt32Value(PLAYER_SELF_RES_SPELL
, 0);
543 void WorldSession::HandleSpellClick( WorldPacket
& recv_data
)
548 if (_player
->isInCombat()) // client prevent click and set different icon at combat state
551 Creature
*unit
= _player
->GetMap()->GetCreatureOrPetOrVehicle(guid
);
552 if (!unit
|| unit
->isInCombat()) // client prevent click and set different icon at combat state
555 SpellClickInfoMapBounds clickPair
= sObjectMgr
.GetSpellClickInfoMapBounds(unit
->GetEntry());
556 for(SpellClickInfoMap::const_iterator itr
= clickPair
.first
; itr
!= clickPair
.second
; ++itr
)
558 if (itr
->second
.IsFitToRequirements(_player
))
560 Unit
*caster
= (itr
->second
.castFlags
& 0x1) ? (Unit
*)_player
: (Unit
*)unit
;
561 Unit
*target
= (itr
->second
.castFlags
& 0x2) ? (Unit
*)_player
: (Unit
*)unit
;
563 caster
->CastSpell(target
, itr
->second
.spellId
, true);