[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / SpellHandler.cpp
blob70227980ecc61bd908bf8626b440f36215343e2d
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 "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+4+8+4+1);
40 Player* pUser = _player;
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, 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 < 5; ++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 //Note: If script stop casting it must send appropriate data to client to prevent stuck item in gray state.
125 if(!Script->ItemUse(pUser,pItem,targets))
127 // no script or script not process request by self
128 pUser->CastItemUseSpell(pItem,targets,cast_count,glyphIndex);
132 #define OPEN_CHEST 11437
133 #define OPEN_SAFE 11535
134 #define OPEN_CAGE 11792
135 #define OPEN_BOOTY_CHEST 5107
136 #define OPEN_STRONGBOX 8517
138 void WorldSession::HandleOpenItemOpcode(WorldPacket& recvPacket)
140 CHECK_PACKET_SIZE(recvPacket,1+1);
142 sLog.outDetail("WORLD: CMSG_OPEN_ITEM packet, data length = %i",recvPacket.size());
144 Player* pUser = _player;
145 uint8 bagIndex, slot;
147 recvPacket >> bagIndex >> slot;
149 sLog.outDetail("bagIndex: %u, slot: %u",bagIndex,slot);
151 Item *pItem = pUser->GetItemByPos(bagIndex, slot);
152 if(!pItem)
154 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
155 return;
158 ItemPrototype const *proto = pItem->GetProto();
159 if(!proto)
161 pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
162 return;
165 // locked item
166 uint32 lockId = proto->LockID;
167 if(lockId)
169 LockEntry const *lockInfo = sLockStore.LookupEntry(lockId);
171 if (!lockInfo)
173 pUser->SendEquipError(EQUIP_ERR_ITEM_LOCKED, pItem, NULL );
174 sLog.outError( "WORLD::OpenItem: item [guid = %u] has an unknown lockId: %u!", pItem->GetGUIDLow() , lockId);
175 return;
178 // required picklocking
179 if(lockInfo->Skill[1] || lockInfo->Skill[0])
181 pUser->SendEquipError(EQUIP_ERR_ITEM_LOCKED, pItem, NULL );
182 return;
186 if(pItem->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))// wrapped?
188 QueryResult *result = CharacterDatabase.PQuery("SELECT entry, flags FROM character_gifts WHERE item_guid = '%u'", pItem->GetGUIDLow());
189 if (result)
191 Field *fields = result->Fetch();
192 uint32 entry = fields[0].GetUInt32();
193 uint32 flags = fields[1].GetUInt32();
195 pItem->SetUInt64Value(ITEM_FIELD_GIFTCREATOR, 0);
196 pItem->SetEntry(entry);
197 pItem->SetUInt32Value(ITEM_FIELD_FLAGS, flags);
198 pItem->SetState(ITEM_CHANGED, pUser);
199 delete result;
201 else
203 sLog.outError("Wrapped item %u don't have record in character_gifts table and will deleted", pItem->GetGUIDLow());
204 pUser->DestroyItem(pItem->GetBagSlot(), pItem->GetSlot(), true);
205 return;
207 CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", pItem->GetGUIDLow());
209 else
210 pUser->SendLoot(pItem->GetGUID(),LOOT_CORPSE);
213 void WorldSession::HandleGameObjectUseOpcode( WorldPacket & recv_data )
215 CHECK_PACKET_SIZE(recv_data, 8);
217 uint64 guid;
219 recv_data >> guid;
221 sLog.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_USE Message [guid=%u]", GUID_LOPART(guid));
222 GameObject *obj = ObjectAccessor::GetGameObject(*_player, guid);
224 if(!obj)
225 return;
227 if (Script->GOHello(_player, obj))
228 return;
230 obj->Use(_player);
233 void WorldSession::HandleGameobjectReportUse(WorldPacket& recvPacket)
235 CHECK_PACKET_SIZE(recvPacket,8);
237 uint64 guid;
238 recvPacket >> guid;
240 sLog.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_REPORT_USE Message [in game guid: %u]", GUID_LOPART(guid));
243 void WorldSession::HandleCastSpellOpcode(WorldPacket& recvPacket)
245 CHECK_PACKET_SIZE(recvPacket,1+4+1);
247 uint32 spellId;
248 uint8 cast_count, unk_flags;
249 recvPacket >> cast_count;
250 recvPacket >> spellId;
251 recvPacket >> unk_flags; // flags (if 0x02 - some additional data are received)
253 sLog.outDebug("WORLD: got cast spell packet, spellId - %u, cast_count: %u, unk_flags %u, data length = %i",
254 spellId, cast_count, unk_flags, recvPacket.size());
256 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
258 if(!spellInfo)
260 sLog.outError("WORLD: unknown spell id %u", spellId);
261 return;
264 // not have spell in spellbook or spell passive and not casted by client
265 if ( !_player->HasActiveSpell (spellId) || IsPassiveSpell(spellId) )
267 //cheater? kick? ban?
268 return;
271 // client provided targets
272 SpellCastTargets targets;
273 if(!targets.read(&recvPacket,_player))
274 return;
276 // auto-selection buff level base at target level (in spellInfo)
277 if(targets.getUnitTarget())
279 SpellEntry const *actualSpellInfo = spellmgr.SelectAuraRankForPlayerLevel(spellInfo,targets.getUnitTarget()->getLevel());
281 // 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
282 if(actualSpellInfo)
283 spellInfo = actualSpellInfo;
286 Spell *spell = new Spell(_player, spellInfo, false);
287 spell->m_cast_count = cast_count; // set count of casts
288 spell->prepare(&targets);
291 void WorldSession::HandleCancelCastOpcode(WorldPacket& recvPacket)
293 CHECK_PACKET_SIZE(recvPacket,5);
295 // increments with every CANCEL packet, don't use for now
296 uint8 counter;
297 uint32 spellId;
298 recvPacket >> counter;
299 recvPacket >> spellId;
301 //FIXME: hack, ignore unexpected client cancel Deadly Throw cast
302 if(spellId==26679)
303 return;
305 if(_player->IsNonMeleeSpellCasted(false))
306 _player->InterruptNonMeleeSpells(false,spellId);
309 void WorldSession::HandleCancelAuraOpcode( WorldPacket& recvPacket)
311 CHECK_PACKET_SIZE(recvPacket,4);
313 uint32 spellId;
314 recvPacket >> spellId;
316 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
317 if (!spellInfo)
318 return;
320 // not allow remove non positive spells and spells with attr SPELL_ATTR_CANT_CANCEL
321 if(!IsPositiveSpell(spellId) || (spellInfo->Attributes & SPELL_ATTR_CANT_CANCEL))
322 return;
324 // channeled spell case (it currently casted then)
325 if(IsChanneledSpell(spellInfo))
327 if(Spell* spell = _player->m_currentSpells[CURRENT_CHANNELED_SPELL])
329 if(spell->m_spellInfo->Id==spellId)
331 spell->cancel();
332 spell->SetReferencedFromCurrent(false);
333 _player->m_currentSpells[CURRENT_CHANNELED_SPELL] = NULL;
336 return;
339 // non channeled case
340 _player->RemoveAurasDueToSpellByCancel(spellId);
343 void WorldSession::HandlePetCancelAuraOpcode( WorldPacket& recvPacket)
345 CHECK_PACKET_SIZE(recvPacket, 8+4);
347 uint64 guid;
348 uint32 spellId;
350 recvPacket >> guid;
351 recvPacket >> spellId;
353 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
354 if(!spellInfo)
356 sLog.outError("WORLD: unknown PET spell id %u", spellId);
357 return;
360 Creature* pet=ObjectAccessor::GetCreatureOrPetOrVehicle(*_player,guid);
362 if(!pet)
364 sLog.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid)) );
365 return;
368 if(pet != GetPlayer()->GetPet() && pet != GetPlayer()->GetCharm())
370 sLog.outError( "HandlePetCancelAura.Pet %u isn't pet of player %s", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
371 return;
374 if(!pet->isAlive())
376 pet->SendPetActionFeedback(FEEDBACK_PET_DEAD);
377 return;
380 pet->RemoveAurasDueToSpell(spellId);
382 pet->AddCreatureSpellCooldown(spellId);
385 void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket& /*recvPacket*/)
387 // nothing do
390 void WorldSession::HandleCancelAutoRepeatSpellOpcode( WorldPacket& /*recvPacket*/)
392 // may be better send SMSG_CANCEL_AUTO_REPEAT?
393 // cancel and prepare for deleting
394 _player->InterruptSpell(CURRENT_AUTOREPEAT_SPELL);
397 /// \todo Complete HandleCancelChanneling function
398 void WorldSession::HandleCancelChanneling( WorldPacket & /*recv_data */)
401 CHECK_PACKET_SIZE(recv_data, 4);
403 uint32 spellid;
404 recv_data >> spellid;
408 void WorldSession::HandleTotemDestroy( WorldPacket& recvPacket)
410 CHECK_PACKET_SIZE(recvPacket, 1);
412 uint8 slotId;
414 recvPacket >> slotId;
416 if (slotId >= MAX_TOTEM)
417 return;
419 if(!_player->m_TotemSlot[slotId])
420 return;
422 Creature* totem = ObjectAccessor::GetCreature(*_player,_player->m_TotemSlot[slotId]);
423 if(totem && totem->isTotem())
424 ((Totem*)totem)->UnSummon();
427 void WorldSession::HandleSelfResOpcode( WorldPacket & /*recv_data*/ )
429 sLog.outDebug("WORLD: CMSG_SELF_RES"); // empty opcode
431 if(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL))
433 SpellEntry const *spellInfo = sSpellStore.LookupEntry(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL));
434 if(spellInfo)
435 _player->CastSpell(_player,spellInfo,false,0);
437 _player->SetUInt32Value(PLAYER_SELF_RES_SPELL, 0);