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
;
128 if (!targets
.read(&recvPacket
, 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
;
337 if(!targets
.read(&recvPacket
,mover
))
339 recvPacket
.rpos(recvPacket
.wpos()); // prevent spam at ignore packet
343 // some spell cast packet including more data (for projectiles?)
344 if (unk_flags
& 0x02)
346 recvPacket
.read_skip
<float>(); // unk1, coords?
347 recvPacket
.read_skip
<float>(); // unk1, coords?
349 recvPacket
>> unk1
; // >> 1 or 0
352 recvPacket
.read_skip
<uint32
>(); // >> MSG_MOVE_STOP
353 uint64 guid
; // guid - unused
354 if(!recvPacket
.readPackGUID(guid
))
357 MovementInfo
movementInfo(recvPacket
);
361 // auto-selection buff level base at target level (in spellInfo)
362 if(targets
.getUnitTarget())
364 SpellEntry
const *actualSpellInfo
= sSpellMgr
.SelectAuraRankForPlayerLevel(spellInfo
,targets
.getUnitTarget()->getLevel());
366 // 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
368 spellInfo
= actualSpellInfo
;
371 Spell
*spell
= new Spell(mover
, spellInfo
, false);
372 spell
->m_cast_count
= cast_count
; // set count of casts
373 spell
->prepare(&targets
);
376 void WorldSession::HandleCancelCastOpcode(WorldPacket
& recvPacket
)
378 // ignore for remote control state (for player case)
379 Unit
* mover
= _player
->m_mover
;
380 if(mover
!= _player
&& mover
->GetTypeId()==TYPEID_PLAYER
)
385 recvPacket
.read_skip
<uint8
>(); // counter, increments with every CANCEL packet, don't use for now
386 recvPacket
>> spellId
;
388 //FIXME: hack, ignore unexpected client cancel Deadly Throw cast
392 if(mover
->IsNonMeleeSpellCasted(false))
393 mover
->InterruptNonMeleeSpells(false,spellId
);
396 void WorldSession::HandleCancelAuraOpcode( WorldPacket
& recvPacket
)
399 recvPacket
>> spellId
;
401 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
405 if (spellInfo
->Attributes
& SPELL_ATTR_CANT_CANCEL
)
408 if(!IsPositiveSpell(spellId
))
410 // ignore for remote control state
411 if (_player
->m_mover
!= _player
)
413 // except own aura spells
415 for(int k
= 0; k
< MAX_EFFECT_INDEX
; ++k
)
417 if (spellInfo
->EffectApplyAuraName
[k
] == SPELL_AURA_MOD_POSSESS
||
418 spellInfo
->EffectApplyAuraName
[k
] == SPELL_AURA_MOD_POSSESS_PET
)
425 // this also include case when aura not found
433 // channeled spell case (it currently casted then)
434 if (IsChanneledSpell(spellInfo
))
436 if (Spell
* curSpell
= _player
->GetCurrentSpell(CURRENT_CHANNELED_SPELL
))
437 if (curSpell
->m_spellInfo
->Id
==spellId
)
438 _player
->InterruptSpell(CURRENT_CHANNELED_SPELL
);
442 // non channeled case
443 _player
->RemoveAurasDueToSpellByCancel(spellId
);
446 void WorldSession::HandlePetCancelAuraOpcode( WorldPacket
& recvPacket
)
448 // ignore for remote control state
449 if(_player
->m_mover
!= _player
)
456 recvPacket
>> spellId
;
458 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
461 sLog
.outError("WORLD: unknown PET spell id %u", spellId
);
465 Creature
* pet
= GetPlayer()->GetMap()->GetCreatureOrPetOrVehicle(guid
);
469 sLog
.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid
)) );
473 if(pet
!= GetPlayer()->GetPet() && pet
!= GetPlayer()->GetCharm())
475 sLog
.outError( "HandlePetCancelAura.Pet %u isn't pet of player %s", uint32(GUID_LOPART(guid
)),GetPlayer()->GetName() );
481 pet
->SendPetActionFeedback(FEEDBACK_PET_DEAD
);
485 pet
->RemoveAurasDueToSpell(spellId
);
487 pet
->AddCreatureSpellCooldown(spellId
);
490 void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket
& /*recvPacket*/)
495 void WorldSession::HandleCancelAutoRepeatSpellOpcode( WorldPacket
& /*recvPacket*/)
497 // cancel and prepare for deleting
498 // do not send SMSG_CANCEL_AUTO_REPEAT! client will send this Opcode again (loop)
499 _player
->m_mover
->InterruptSpell(CURRENT_AUTOREPEAT_SPELL
, true, false);
502 void WorldSession::HandleCancelChanneling( WorldPacket
& recv_data
)
504 recv_data
.read_skip
<uint32
>(); // spellid, not used
506 // ignore for remote control state (for player case)
507 Unit
* mover
= _player
->m_mover
;
508 if(mover
!= _player
&& mover
->GetTypeId()==TYPEID_PLAYER
)
511 mover
->InterruptSpell(CURRENT_CHANNELED_SPELL
);
514 void WorldSession::HandleTotemDestroyed( WorldPacket
& recvPacket
)
516 // ignore for remote control state
517 if(_player
->m_mover
!= _player
)
522 recvPacket
>> slotId
;
524 if (int(slotId
) >= MAX_TOTEM_SLOT
)
527 if (Totem
* totem
= GetPlayer()->GetTotem(TotemSlot(slotId
)))
531 void WorldSession::HandleSelfResOpcode( WorldPacket
& /*recv_data*/ )
533 sLog
.outDebug("WORLD: CMSG_SELF_RES"); // empty opcode
535 if(_player
->GetUInt32Value(PLAYER_SELF_RES_SPELL
))
537 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(_player
->GetUInt32Value(PLAYER_SELF_RES_SPELL
));
539 _player
->CastSpell(_player
, spellInfo
, false);
541 _player
->SetUInt32Value(PLAYER_SELF_RES_SPELL
, 0);
545 void WorldSession::HandleSpellClick( WorldPacket
& recv_data
)
550 if (_player
->isInCombat()) // client prevent click and set different icon at combat state
553 Creature
*unit
= _player
->GetMap()->GetCreatureOrPetOrVehicle(guid
);
554 if (!unit
|| unit
->isInCombat()) // client prevent click and set different icon at combat state
557 SpellClickInfoMapBounds clickPair
= sObjectMgr
.GetSpellClickInfoMapBounds(unit
->GetEntry());
558 for(SpellClickInfoMap::const_iterator itr
= clickPair
.first
; itr
!= clickPair
.second
; ++itr
)
560 if (itr
->second
.IsFitToRequirements(_player
))
562 Unit
*caster
= (itr
->second
.castFlags
& 0x1) ? (Unit
*)_player
: (Unit
*)unit
;
563 Unit
*target
= (itr
->second
.castFlags
& 0x2) ? (Unit
*)_player
: (Unit
*)unit
;
565 caster
->CastSpell(target
, itr
->second
.spellId
, true);