[6922] Whitespace and newline fixes
[getmangos.git] / src / game / SpellHandler.cpp
blobde379d91e62cad415ab9d53886dde7caf9651c39
1 /*
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
19 #include "Common.h"
20 #include "Database/DBCStores.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "World.h"
24 #include "ObjectMgr.h"
25 #include "SpellMgr.h"
26 #include "Log.h"
27 #include "Opcodes.h"
28 #include "Spell.h"
29 #include "SpellAuras.h"
30 #include "BattleGround.h"
31 #include "MapManager.h"
32 #include "ScriptCalls.h"
33 #include "Totem.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;
41 uint8 bagIndex, slot;
42 uint8 spell_count; // number of spells at item, not used
43 uint8 cast_count; // next cast if exists (single or not)
44 uint64 item_guid;
46 recvPacket >> bagIndex >> slot >> spell_count >> cast_count >> item_guid;
48 Item *pItem = pUser->GetItemByPos(bagIndex, slot);
49 if(!pItem)
51 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
52 return;
55 if(pItem->GetGUID() != item_guid)
57 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
58 return;
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();
64 if(!proto)
66 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
67 return;
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 );
74 return;
77 uint8 msg = pUser->CanUseItem(pItem);
78 if( msg != EQUIP_ERR_OK )
80 pUser->SendEquipError( msg, pItem, NULL );
81 return;
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) &&
87 pUser->InArena())
89 pUser->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH,pItem,NULL);
90 return;
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);
102 return;
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))
120 return;
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);
133 if(!spellInfo)
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);
137 return;
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);
145 return;
148 // use triggered flag only for items with many spell casts and for not first cast
149 int count = 0;
151 for(int i = 0; i < 5; ++i)
153 _Spell const& spellData = pItem->GetProto()->Spells[i];
155 // no spell
156 if(!spellData.SpellId)
157 continue;
159 // wrong triggering type
160 if( spellData.SpellTrigger != ITEM_SPELLTRIGGER_ON_USE && spellData.SpellTrigger != ITEM_SPELLTRIGGER_ON_NO_DELAY_USE)
161 continue;
163 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellData.SpellId);
164 if(!spellInfo)
166 sLog.outError("Item (Entry: %u) in have wrong spell id %u, ignoring ",proto->ItemId, spellData.SpellId);
167 continue;
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);
175 ++count;
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);
200 if(!pItem)
202 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
203 return;
206 ItemPrototype const *proto = pItem->GetProto();
207 if(!proto)
209 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
210 return;
213 // locked item
214 uint32 lockId = proto->LockID;
215 if(lockId)
217 LockEntry const *lockInfo = sLockStore.LookupEntry(lockId);
219 if (!lockInfo)
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);
223 return;
226 // required picklocking
227 if(lockInfo->requiredlockskill || lockInfo->requiredminingskill)
229 pUser->SendEquipError(EQUIP_ERR_ITEM_LOCKED, pItem, NULL );
230 return;
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());
237 if (result)
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);
247 delete result;
249 else
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);
253 return;
255 CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", pItem->GetGUIDLow());
257 else
258 pUser->SendLoot(pItem->GetGUID(),LOOT_CORPSE);
261 void WorldSession::HandleGameObjectUseOpcode( WorldPacket & recv_data )
263 CHECK_PACKET_SIZE(recv_data, 8);
265 uint64 guid;
267 recv_data >> guid;
269 sLog.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_USE Message [guid=%u]", GUID_LOPART(guid));
270 GameObject *obj = ObjectAccessor::GetGameObject(*_player, guid);
272 if(!obj)
273 return;
275 if (Script->GOHello(_player, obj))
276 return;
278 obj->Use(_player);
281 void WorldSession::HandleCastSpellOpcode(WorldPacket& recvPacket)
283 CHECK_PACKET_SIZE(recvPacket,4+1+2);
285 uint32 spellId;
286 uint8 cast_count;
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 );
295 if(!spellInfo)
297 sLog.outError("WORLD: unknown spell id %u", spellId);
298 return;
301 // not have spell or spell passive and not casted by client
302 if ( !_player->HasSpell (spellId) || IsPassiveSpell(spellId) )
304 //cheater? kick? ban?
305 return;
308 // client provided targets
309 SpellCastTargets targets;
310 if(!targets.read(&recvPacket,_player))
311 return;
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
319 if(actualSpellInfo)
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);
332 uint32 spellId;
333 recvPacket >> spellId;
335 //FIXME: hack, ignore unexpected client cancel Deadly Throw cast
336 if(spellId==26679)
337 return;
339 if(_player->IsNonMeleeSpellCasted(false))
340 _player->InterruptNonMeleeSpells(false,spellId);
343 void WorldSession::HandleCancelAuraOpcode( WorldPacket& recvPacket)
345 CHECK_PACKET_SIZE(recvPacket,4);
347 uint32 spellId;
348 recvPacket >> spellId;
350 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
351 if (!spellInfo)
352 return;
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))
356 return;
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)
365 spell->cancel();
366 spell->SetReferencedFromCurrent(false);
367 _player->m_currentSpells[CURRENT_CHANNELED_SPELL] = NULL;
370 return;
373 // non channeled case
374 _player->RemoveAurasDueToSpellByCancel(spellId);
377 void WorldSession::HandlePetCancelAuraOpcode( WorldPacket& recvPacket)
379 CHECK_PACKET_SIZE(recvPacket, 8+4);
381 uint64 guid;
382 uint32 spellId;
384 recvPacket >> guid;
385 recvPacket >> spellId;
387 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
388 if(!spellInfo)
390 sLog.outError("WORLD: unknown PET spell id %u", spellId);
391 return;
394 Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
396 if(!pet)
398 sLog.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid)) );
399 return;
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() );
405 return;
408 if(!pet->isAlive())
410 pet->SendPetActionFeedback(FEEDBACK_PET_DEAD);
411 return;
414 pet->RemoveAurasDueToSpell(spellId);
416 pet->AddCreatureSpellCooldown(spellId);
419 void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket& /*recvPacket*/)
421 // nothing do
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);
437 uint32 spellid;
438 recv_data >> spellid;
442 void WorldSession::HandleTotemDestroy( WorldPacket& recvPacket)
444 CHECK_PACKET_SIZE(recvPacket, 1);
446 uint8 slotId;
448 recvPacket >> slotId;
450 if (slotId >= MAX_TOTEM)
451 return;
453 if(!_player->m_TotemSlot[slotId])
454 return;
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));
468 if(spellInfo)
469 _player->CastSpell(_player,spellInfo,false,0);
471 _player->SetUInt32Value(PLAYER_SELF_RES_SPELL, 0);