[8483] Implement glyph 43361.
[getmangos.git] / src / game / SpellHandler.cpp
blob2112325f2402b0f54e0d63e40f69effd13b19234
1 /*
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
19 #include "Common.h"
20 #include "DBCStores.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "ObjectMgr.h"
24 #include "SpellMgr.h"
25 #include "Log.h"
26 #include "Opcodes.h"
27 #include "Spell.h"
28 #include "ScriptCalls.h"
29 #include "Totem.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)
39 return;
41 uint8 bagIndex, slot;
42 uint8 unk_flags; // flags (if 0x02 - some additional data are received)
43 uint8 cast_count; // next cast if exists (single or not)
44 uint64 item_guid;
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;
50 Item *pItem = pUser->GetItemByPos(bagIndex, slot);
51 if(!pItem)
53 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
54 return;
57 if(pItem->GetGUID() != item_guid)
59 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
60 return;
63 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());
65 ItemPrototype const *proto = pItem->GetProto();
66 if(!proto)
68 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
69 return;
72 // some item classes can be used only in equipped state
73 if(proto->InventoryType != INVTYPE_NON_EQUIP && !pItem->IsEquipped())
75 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
76 return;
79 uint8 msg = pUser->CanUseItem(pItem);
80 if( msg != EQUIP_ERR_OK )
82 pUser->SendEquipError( msg, pItem, NULL );
83 return;
86 // only allow conjured consumable, bandage, poisons (all should have the 2^21 item flag set in DB)
87 if( proto->Class == ITEM_CLASS_CONSUMABLE &&
88 !(proto->Flags & ITEM_FLAGS_USEABLE_IN_ARENA) &&
89 pUser->InArena())
91 pUser->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH,pItem,NULL);
92 return;
95 if (pUser->isInCombat())
97 for(int i = 0; i < MAX_ITEM_PROTO_SPELLS; ++i)
99 if (SpellEntry const *spellInfo = sSpellStore.LookupEntry(proto->Spells[i].SpellId))
101 if (IsNonCombatSpell(spellInfo))
103 pUser->SendEquipError(EQUIP_ERR_NOT_IN_COMBAT,pItem,NULL);
104 return;
110 // check also BIND_WHEN_PICKED_UP and BIND_QUEST_ITEM for .additem or .additemset case by GM (not binded at adding to inventory)
111 if( pItem->GetProto()->Bonding == BIND_WHEN_USE || pItem->GetProto()->Bonding == BIND_WHEN_PICKED_UP || pItem->GetProto()->Bonding == BIND_QUEST_ITEM )
113 if (!pItem->IsSoulBound())
115 pItem->SetState(ITEM_CHANGED, pUser);
116 pItem->SetBinding( true );
120 SpellCastTargets targets;
121 if (!targets.read(&recvPacket, pUser))
122 return;
124 targets.Update(pUser);
126 if (!pItem->IsTargetValidForItemUse(targets.getUnitTarget()))
128 // free gray item after use fail
129 pUser->SendEquipError(EQUIP_ERR_NONE, pItem, NULL);
131 // send spell error
132 if (SpellEntry const* spellInfo = sSpellStore.LookupEntry(spellid))
134 // for implicit area/coord target spells
135 if (IsPointEffectTarget(Targets(spellInfo->EffectImplicitTargetA[0])) ||
136 IsAreaEffectTarget(Targets(spellInfo->EffectImplicitTargetA[0])))
137 Spell::SendCastResult(_player,spellInfo,cast_count,SPELL_FAILED_NO_VALID_TARGETS);
138 // for explicit target spells
139 else
140 Spell::SendCastResult(_player,spellInfo,cast_count,SPELL_FAILED_BAD_TARGETS);
142 return;
145 //Note: If script stop casting it must send appropriate data to client to prevent stuck item in gray state.
146 if(!Script->ItemUse(pUser,pItem,targets))
148 // no script or script not process request by self
149 pUser->CastItemUseSpell(pItem,targets,cast_count,glyphIndex);
153 #define OPEN_CHEST 11437
154 #define OPEN_SAFE 11535
155 #define OPEN_CAGE 11792
156 #define OPEN_BOOTY_CHEST 5107
157 #define OPEN_STRONGBOX 8517
159 void WorldSession::HandleOpenItemOpcode(WorldPacket& recvPacket)
161 sLog.outDetail("WORLD: CMSG_OPEN_ITEM packet, data length = %i",(uint32)recvPacket.size());
163 Player* pUser = _player;
165 // ignore for remote control state
166 if(pUser->m_mover != pUser)
167 return;
169 uint8 bagIndex, slot;
171 recvPacket >> bagIndex >> slot;
173 sLog.outDetail("bagIndex: %u, slot: %u",bagIndex,slot);
175 Item *pItem = pUser->GetItemByPos(bagIndex, slot);
176 if(!pItem)
178 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
179 return;
182 ItemPrototype const *proto = pItem->GetProto();
183 if(!proto)
185 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
186 return;
189 // locked item
190 uint32 lockId = proto->LockID;
191 if(lockId)
193 LockEntry const *lockInfo = sLockStore.LookupEntry(lockId);
195 if (!lockInfo)
197 pUser->SendEquipError(EQUIP_ERR_ITEM_LOCKED, pItem, NULL );
198 sLog.outError( "WORLD::OpenItem: item [guid = %u] has an unknown lockId: %u!", pItem->GetGUIDLow() , lockId);
199 return;
202 // required picklocking
203 if(lockInfo->Skill[1] || lockInfo->Skill[0])
205 pUser->SendEquipError(EQUIP_ERR_ITEM_LOCKED, pItem, NULL );
206 return;
210 if(pItem->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))// wrapped?
212 QueryResult *result = CharacterDatabase.PQuery("SELECT entry, flags FROM character_gifts WHERE item_guid = '%u'", pItem->GetGUIDLow());
213 if (result)
215 Field *fields = result->Fetch();
216 uint32 entry = fields[0].GetUInt32();
217 uint32 flags = fields[1].GetUInt32();
219 pItem->SetUInt64Value(ITEM_FIELD_GIFTCREATOR, 0);
220 pItem->SetEntry(entry);
221 pItem->SetUInt32Value(ITEM_FIELD_FLAGS, flags);
222 pItem->SetState(ITEM_CHANGED, pUser);
223 delete result;
225 else
227 sLog.outError("Wrapped item %u don't have record in character_gifts table and will deleted", pItem->GetGUIDLow());
228 pUser->DestroyItem(pItem->GetBagSlot(), pItem->GetSlot(), true);
229 return;
231 CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", pItem->GetGUIDLow());
233 else
234 pUser->SendLoot(pItem->GetGUID(),LOOT_CORPSE);
237 void WorldSession::HandleGameObjectUseOpcode( WorldPacket & recv_data )
239 uint64 guid;
241 recv_data >> guid;
243 sLog.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_USE Message [guid=%u]", GUID_LOPART(guid));
245 // ignore for remote control state
246 if(_player->m_mover != _player)
247 return;
249 GameObject *obj = GetPlayer()->GetMap()->GetGameObject(guid);
251 if(!obj)
252 return;
254 if (Script->GOHello(_player, obj))
255 return;
257 obj->Use(_player);
260 void WorldSession::HandleGameobjectReportUse(WorldPacket& recvPacket)
262 uint64 guid;
263 recvPacket >> guid;
265 sLog.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_REPORT_USE Message [in game guid: %u]", GUID_LOPART(guid));
267 // ignore for remote control state
268 if(_player->m_mover != _player)
269 return;
271 GameObject* go = GetPlayer()->GetMap()->GetGameObject(guid);
272 if(!go)
273 return;
275 if(!go->IsWithinDistInMap(_player,INTERACTION_DISTANCE))
276 return;
278 _player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_USE_GAMEOBJECT, go->GetEntry());
281 void WorldSession::HandleCastSpellOpcode(WorldPacket& recvPacket)
283 uint32 spellId;
284 uint8 cast_count, unk_flags;
285 recvPacket >> cast_count;
286 recvPacket >> spellId;
287 recvPacket >> unk_flags; // flags (if 0x02 - some additional data are received)
289 // ignore for remote control state (for player case)
290 Unit* mover = _player->m_mover;
291 if(mover != _player && mover->GetTypeId()==TYPEID_PLAYER)
293 recvPacket.rpos(recvPacket.wpos()); // prevent spam at ignore packet
294 return;
297 sLog.outDebug("WORLD: got cast spell packet, spellId - %u, cast_count: %u, unk_flags %u, data length = %i",
298 spellId, cast_count, unk_flags, (uint32)recvPacket.size());
300 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
302 if(!spellInfo)
304 sLog.outError("WORLD: unknown spell id %u", spellId);
305 recvPacket.rpos(recvPacket.wpos()); // prevent spam at ignore packet
306 return;
309 if(mover->GetTypeId()==TYPEID_PLAYER)
311 // not have spell in spellbook or spell passive and not casted by client
312 if (!((Player*)mover)->HasActiveSpell (spellId) || IsPassiveSpell(spellId) )
314 //cheater? kick? ban?
315 recvPacket.rpos(recvPacket.wpos()); // prevent spam at ignore packet
316 return;
319 else
321 // not have spell in spellbook or spell passive and not casted by client
322 if (!((Creature*)mover)->HasSpell(spellId) || IsPassiveSpell(spellId) )
324 //cheater? kick? ban?
325 recvPacket.rpos(recvPacket.wpos()); // prevent spam at ignore packet
326 return;
330 // client provided targets
331 SpellCastTargets targets;
332 if(!targets.read(&recvPacket,mover))
334 recvPacket.rpos(recvPacket.wpos()); // prevent spam at ignore packet
335 return;
338 // some spell cast packet including more data (for projectiles?)
339 if (unk_flags & 0x02)
341 recvPacket.read_skip<float>(); // unk1, coords?
342 recvPacket.read_skip<float>(); // unk1, coords?
343 recvPacket.read_skip<uint8>(); // >> 1
344 recvPacket.read_skip<uint32>(); // >> MSG_MOVE_STOP
345 MovementInfo movementInfo;
346 ReadMovementInfo(recvPacket, &movementInfo);
349 // auto-selection buff level base at target level (in spellInfo)
350 if(targets.getUnitTarget())
352 SpellEntry const *actualSpellInfo = spellmgr.SelectAuraRankForPlayerLevel(spellInfo,targets.getUnitTarget()->getLevel());
354 // 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
355 if(actualSpellInfo)
356 spellInfo = actualSpellInfo;
359 Spell *spell = new Spell(mover, spellInfo, false);
360 spell->m_cast_count = cast_count; // set count of casts
361 spell->prepare(&targets);
364 void WorldSession::HandleCancelCastOpcode(WorldPacket& recvPacket)
366 // ignore for remote control state (for player case)
367 Unit* mover = _player->m_mover;
368 if(mover != _player && mover->GetTypeId()==TYPEID_PLAYER)
369 return;
371 uint32 spellId;
373 recvPacket.read_skip<uint8>(); // counter, increments with every CANCEL packet, don't use for now
374 recvPacket >> spellId;
376 //FIXME: hack, ignore unexpected client cancel Deadly Throw cast
377 if(spellId==26679)
378 return;
380 if(mover->IsNonMeleeSpellCasted(false))
381 mover->InterruptNonMeleeSpells(false,spellId);
384 void WorldSession::HandleCancelAuraOpcode( WorldPacket& recvPacket)
386 uint32 spellId;
387 recvPacket >> spellId;
389 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
390 if (!spellInfo)
391 return;
393 if (spellInfo->Attributes & SPELL_ATTR_CANT_CANCEL)
394 return;
396 if(!IsPositiveSpell(spellId))
398 // ignore for remote control state
399 if (_player->m_mover != _player)
401 // except own aura spells
402 bool allow = false;
403 for(int k = 0; k < 3; ++k)
405 if (spellInfo->EffectApplyAuraName[k] == SPELL_AURA_MOD_POSSESS ||
406 spellInfo->EffectApplyAuraName[k] == SPELL_AURA_MOD_POSSESS_PET)
408 allow = true;
409 break;
413 // this also include case when aura not found
414 if(!allow)
415 return;
417 else
418 return;
421 // channeled spell case (it currently casted then)
422 if (IsChanneledSpell(spellInfo))
424 if (Spell* curSpell = _player->GetCurrentSpell(CURRENT_CHANNELED_SPELL))
425 if (curSpell->m_spellInfo->Id==spellId)
426 _player->InterruptSpell(CURRENT_CHANNELED_SPELL);
427 return;
430 // non channeled case
431 _player->RemoveAurasDueToSpellByCancel(spellId);
434 void WorldSession::HandlePetCancelAuraOpcode( WorldPacket& recvPacket)
436 // ignore for remote control state
437 if(_player->m_mover != _player)
438 return;
440 uint64 guid;
441 uint32 spellId;
443 recvPacket >> guid;
444 recvPacket >> spellId;
446 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
447 if(!spellInfo)
449 sLog.outError("WORLD: unknown PET spell id %u", spellId);
450 return;
453 Creature* pet=ObjectAccessor::GetCreatureOrPetOrVehicle(*_player,guid);
455 if(!pet)
457 sLog.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid)) );
458 return;
461 if(pet != GetPlayer()->GetPet() && pet != GetPlayer()->GetCharm())
463 sLog.outError( "HandlePetCancelAura.Pet %u isn't pet of player %s", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
464 return;
467 if(!pet->isAlive())
469 pet->SendPetActionFeedback(FEEDBACK_PET_DEAD);
470 return;
473 pet->RemoveAurasDueToSpell(spellId);
475 pet->AddCreatureSpellCooldown(spellId);
478 void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket& /*recvPacket*/)
480 // nothing do
483 void WorldSession::HandleCancelAutoRepeatSpellOpcode( WorldPacket& /*recvPacket*/)
485 // may be better send SMSG_CANCEL_AUTO_REPEAT?
486 // cancel and prepare for deleting
487 _player->m_mover->InterruptSpell(CURRENT_AUTOREPEAT_SPELL);
490 void WorldSession::HandleCancelChanneling( WorldPacket & recv_data)
492 recv_data.read_skip<uint32>(); // spellid, not used
494 // ignore for remote control state (for player case)
495 Unit* mover = _player->m_mover;
496 if(mover != _player && mover->GetTypeId()==TYPEID_PLAYER)
497 return;
499 mover->InterruptSpell(CURRENT_CHANNELED_SPELL);
502 void WorldSession::HandleTotemDestroyed( WorldPacket& recvPacket)
504 // ignore for remote control state
505 if(_player->m_mover != _player)
506 return;
508 uint8 slotId;
510 recvPacket >> slotId;
512 if (slotId >= MAX_TOTEM)
513 return;
515 if(!_player->m_TotemSlot[slotId])
516 return;
518 Creature* totem = GetPlayer()->GetMap()->GetCreature(_player->m_TotemSlot[slotId]);
519 if(totem && totem->isTotem())
520 ((Totem*)totem)->UnSummon();
523 void WorldSession::HandleSelfResOpcode( WorldPacket & /*recv_data*/ )
525 sLog.outDebug("WORLD: CMSG_SELF_RES"); // empty opcode
527 if(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL))
529 SpellEntry const *spellInfo = sSpellStore.LookupEntry(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL));
530 if(spellInfo)
531 _player->CastSpell(_player,spellInfo,false,0);
533 _player->SetUInt32Value(PLAYER_SELF_RES_SPELL, 0);
537 void WorldSession::HandleSpellClick( WorldPacket & recv_data )
539 uint64 guid;
540 recv_data >> guid;
542 if (_player->isInCombat()) // client prevent click and set different icon at combat state
543 return;
545 Creature *unit = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, guid);
546 if (!unit || unit->isInCombat()) // client prevent click and set different icon at combat state
547 return;
549 SpellClickInfoMapBounds clickPair = objmgr.GetSpellClickInfoMapBounds(unit->GetEntry());
550 for(SpellClickInfoMap::const_iterator itr = clickPair.first; itr != clickPair.second; ++itr)
552 if (itr->second.IsFitToRequirements(_player))
554 Unit *caster = (itr->second.castFlags & 0x1) ? (Unit*)_player : (Unit*)unit;
555 Unit *target = (itr->second.castFlags & 0x2) ? (Unit*)_player : (Unit*)unit;
557 caster->CastSpell(target, itr->second.spellId, true);