[9529] Make Player::IsValidPos const
[getmangos.git] / src / game / SpellHandler.cpp
blob6819c0b482293a0bafd966a1e94fdafd8394fb88
1 /*
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
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 // reject fake data
51 if (glyphIndex >= MAX_GLYPH_SLOT_INDEX)
53 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
54 return;
57 Item *pItem = pUser->GetItemByPos(bagIndex, slot);
58 if (!pItem)
60 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
61 return;
64 if (pItem->GetGUID() != item_guid)
66 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
67 return;
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();
73 if (!proto)
75 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
76 return;
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 );
83 return;
86 uint8 msg = pUser->CanUseItem(pItem);
87 if (msg != EQUIP_ERR_OK)
89 pUser->SendEquipError( msg, pItem, NULL );
90 return;
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) &&
96 pUser->InArena())
98 pUser->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH,pItem,NULL);
99 return;
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);
111 return;
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))
129 return;
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);
138 // send spell error
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
146 else
147 Spell::SendCastResult(_player,spellInfo,cast_count,SPELL_FAILED_BAD_TARGETS);
149 return;
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)
174 return;
176 uint8 bagIndex, slot;
178 recvPacket >> bagIndex >> slot;
180 sLog.outDetail("bagIndex: %u, slot: %u",bagIndex,slot);
182 Item *pItem = pUser->GetItemByPos(bagIndex, slot);
183 if(!pItem)
185 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
186 return;
189 ItemPrototype const *proto = pItem->GetProto();
190 if(!proto)
192 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
193 return;
196 // locked item
197 uint32 lockId = proto->LockID;
198 if(lockId)
200 LockEntry const *lockInfo = sLockStore.LookupEntry(lockId);
202 if (!lockInfo)
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);
206 return;
209 // required picklocking
210 if(lockInfo->Skill[1] || lockInfo->Skill[0])
212 pUser->SendEquipError(EQUIP_ERR_ITEM_LOCKED, pItem, NULL );
213 return;
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());
220 if (result)
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);
230 delete result;
232 else
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);
236 return;
238 CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", pItem->GetGUIDLow());
240 else
241 pUser->SendLoot(pItem->GetGUID(),LOOT_CORPSE);
244 void WorldSession::HandleGameObjectUseOpcode( WorldPacket & recv_data )
246 uint64 guid;
248 recv_data >> guid;
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)
254 return;
256 GameObject *obj = GetPlayer()->GetMap()->GetGameObject(guid);
258 if(!obj)
259 return;
261 obj->Use(_player);
264 void WorldSession::HandleGameobjectReportUse(WorldPacket& recvPacket)
266 uint64 guid;
267 recvPacket >> guid;
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)
273 return;
275 GameObject* go = GetPlayer()->GetMap()->GetGameObject(guid);
276 if(!go)
277 return;
279 if(!go->IsWithinDistInMap(_player,INTERACTION_DISTANCE))
280 return;
282 _player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_USE_GAMEOBJECT, go->GetEntry());
285 void WorldSession::HandleCastSpellOpcode(WorldPacket& recvPacket)
287 uint32 spellId;
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
298 return;
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 );
306 if(!spellInfo)
308 sLog.outError("WORLD: unknown spell id %u", spellId);
309 recvPacket.rpos(recvPacket.wpos()); // prevent spam at ignore packet
310 return;
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
321 return;
324 else
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
331 return;
335 // client provided targets
336 SpellCastTargets targets;
337 if(!targets.read(&recvPacket,mover))
339 recvPacket.rpos(recvPacket.wpos()); // prevent spam at ignore packet
340 return;
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?
348 uint8 unk1;
349 recvPacket >> unk1; // >> 1 or 0
350 if(unk1)
352 recvPacket.read_skip<uint32>(); // >> MSG_MOVE_STOP
353 uint64 guid; // guid - unused
354 if(!recvPacket.readPackGUID(guid))
355 return;
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
367 if(actualSpellInfo)
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)
381 return;
383 uint32 spellId;
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
389 if(spellId==26679)
390 return;
392 if(mover->IsNonMeleeSpellCasted(false))
393 mover->InterruptNonMeleeSpells(false,spellId);
396 void WorldSession::HandleCancelAuraOpcode( WorldPacket& recvPacket)
398 uint32 spellId;
399 recvPacket >> spellId;
401 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
402 if (!spellInfo)
403 return;
405 if (spellInfo->Attributes & SPELL_ATTR_CANT_CANCEL)
406 return;
408 if(!IsPositiveSpell(spellId))
410 // ignore for remote control state
411 if (_player->m_mover != _player)
413 // except own aura spells
414 bool allow = false;
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)
420 allow = true;
421 break;
425 // this also include case when aura not found
426 if(!allow)
427 return;
429 else
430 return;
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);
439 return;
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)
450 return;
452 uint64 guid;
453 uint32 spellId;
455 recvPacket >> guid;
456 recvPacket >> spellId;
458 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
459 if(!spellInfo)
461 sLog.outError("WORLD: unknown PET spell id %u", spellId);
462 return;
465 Creature* pet = GetPlayer()->GetMap()->GetCreatureOrPetOrVehicle(guid);
467 if(!pet)
469 sLog.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid)) );
470 return;
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() );
476 return;
479 if(!pet->isAlive())
481 pet->SendPetActionFeedback(FEEDBACK_PET_DEAD);
482 return;
485 pet->RemoveAurasDueToSpell(spellId);
487 pet->AddCreatureSpellCooldown(spellId);
490 void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket& /*recvPacket*/)
492 // nothing do
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)
509 return;
511 mover->InterruptSpell(CURRENT_CHANNELED_SPELL);
514 void WorldSession::HandleTotemDestroyed( WorldPacket& recvPacket)
516 // ignore for remote control state
517 if(_player->m_mover != _player)
518 return;
520 uint8 slotId;
522 recvPacket >> slotId;
524 if (int(slotId) >= MAX_TOTEM_SLOT)
525 return;
527 if (Totem* totem = GetPlayer()->GetTotem(TotemSlot(slotId)))
528 totem->UnSummon();
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));
538 if(spellInfo)
539 _player->CastSpell(_player, spellInfo, false);
541 _player->SetUInt32Value(PLAYER_SELF_RES_SPELL, 0);
545 void WorldSession::HandleSpellClick( WorldPacket & recv_data )
547 uint64 guid;
548 recv_data >> guid;
550 if (_player->isInCombat()) // client prevent click and set different icon at combat state
551 return;
553 Creature *unit = _player->GetMap()->GetCreatureOrPetOrVehicle(guid);
554 if (!unit || unit->isInCombat()) // client prevent click and set different icon at combat state
555 return;
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);