More fixes for wrong format arg/value pairs.
[auctionmangos.git] / src / game / SpellHandler.cpp
blob3f4a6a846868bed751b3bbb0ae03b3f54677981a
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;
266 uint32 spellId = OPEN_CHEST;
268 recv_data >> guid;
270 sLog.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_USE Message [guid=%u]", GUID_LOPART(guid));
271 GameObject *obj = ObjectAccessor::GetGameObject(*_player, guid);
273 if(!obj)
274 return;
276 if (Script->GOHello(_player, obj))
277 return;
279 obj->Use(_player);
282 void WorldSession::HandleCastSpellOpcode(WorldPacket& recvPacket)
284 CHECK_PACKET_SIZE(recvPacket,4+1+2);
286 uint32 spellId;
287 uint8 cast_count;
288 recvPacket >> spellId;
289 recvPacket >> cast_count;
291 sLog.outDebug("WORLD: got cast spell packet, spellId - %u, cast_count: %u data length = %i",
292 spellId, cast_count, recvPacket.size());
294 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
296 if(!spellInfo)
298 sLog.outError("WORLD: unknown spell id %u", spellId);
299 return;
302 // not have spell or spell passive and not casted by client
303 if ( !_player->HasSpell (spellId) || IsPassiveSpell(spellId) )
305 //cheater? kick? ban?
306 return;
309 // client provided targets
310 SpellCastTargets targets;
311 if(!targets.read(&recvPacket,_player))
312 return;
314 // auto-selection buff level base at target level (in spellInfo)
315 if(targets.getUnitTarget())
317 SpellEntry const *actualSpellInfo = spellmgr.SelectAuraRankForPlayerLevel(spellInfo,targets.getUnitTarget()->getLevel());
319 // 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
320 if(actualSpellInfo)
321 spellInfo = actualSpellInfo;
324 Spell *spell = new Spell(_player, spellInfo, false);
325 spell->m_cast_count = cast_count; // set count of casts
326 spell->prepare(&targets);
329 void WorldSession::HandleCancelCastOpcode(WorldPacket& recvPacket)
331 CHECK_PACKET_SIZE(recvPacket,4);
333 uint32 spellId;
334 recvPacket >> spellId;
336 //FIXME: hack, ignore unexpected client cancel Deadly Throw cast
337 if(spellId==26679)
338 return;
340 if(_player->IsNonMeleeSpellCasted(false))
341 _player->InterruptNonMeleeSpells(false,spellId);
344 void WorldSession::HandleCancelAuraOpcode( WorldPacket& recvPacket)
346 CHECK_PACKET_SIZE(recvPacket,4);
348 uint32 spellId;
349 recvPacket >> spellId;
351 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
352 if (!spellInfo)
353 return;
355 // not allow remove non positive spells and spells with attr SPELL_ATTR_CANT_CANCEL
356 if(!IsPositiveSpell(spellId) || (spellInfo->Attributes & SPELL_ATTR_CANT_CANCEL))
357 return;
359 _player->RemoveAurasDueToSpellByCancel(spellId);
361 if (spellId == 2584) // Waiting to resurrect spell cancel, we must remove player from resurrect queue
363 BattleGround *bg = _player->GetBattleGround();
364 if(!bg)
365 return;
366 bg->RemovePlayerFromResurrectQueue(_player->GetGUID());
370 void WorldSession::HandlePetCancelAuraOpcode( WorldPacket& recvPacket)
372 CHECK_PACKET_SIZE(recvPacket, 8+4);
374 uint64 guid;
375 uint32 spellId;
377 recvPacket >> guid;
378 recvPacket >> spellId;
380 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
381 if(!spellInfo)
383 sLog.outError("WORLD: unknown PET spell id %u", spellId);
384 return;
387 Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
389 if(!pet)
391 sLog.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid)) );
392 return;
395 if(pet != GetPlayer()->GetPet() && pet != GetPlayer()->GetCharm())
397 sLog.outError( "HandlePetCancelAura.Pet %u isn't pet of player %s", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
398 return;
401 if(!pet->isAlive())
403 pet->SendPetActionFeedback(FEEDBACK_PET_DEAD);
404 return;
407 pet->RemoveAurasDueToSpell(spellId);
409 pet->AddCreatureSpellCooldown(spellId);
412 void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket& /*recvPacket*/)
414 // nothing do
417 void WorldSession::HandleCancelAutoRepeatSpellOpcode( WorldPacket& /*recvPacket*/)
419 // may be better send SMSG_CANCEL_AUTO_REPEAT?
420 // cancel and prepare for deleting
421 _player->InterruptSpell(CURRENT_AUTOREPEAT_SPELL);
424 /// \todo Complete HandleCancelChanneling function
425 void WorldSession::HandleCancelChanneling( WorldPacket & /*recv_data */)
428 CHECK_PACKET_SIZE(recv_data, 4);
430 uint32 spellid;
431 recv_data >> spellid;
435 void WorldSession::HandleTotemDestroy( WorldPacket& recvPacket)
437 CHECK_PACKET_SIZE(recvPacket, 1);
439 uint8 slotId;
441 recvPacket >> slotId;
443 if (slotId >= MAX_TOTEM)
444 return;
446 if(!_player->m_TotemSlot[slotId])
447 return;
449 Creature* totem = ObjectAccessor::GetCreature(*_player,_player->m_TotemSlot[slotId]);
450 if(totem && totem->isTotem())
451 ((Totem*)totem)->UnSummon();
454 void WorldSession::HandleSelfResOpcode( WorldPacket & /*recv_data*/ )
456 sLog.outDebug("WORLD: CMSG_SELF_RES"); // empty opcode
458 if(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL))
460 SpellEntry const *spellInfo = sSpellStore.LookupEntry(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL));
461 if(spellInfo)
462 _player->CastSpell(_player,spellInfo,false,0);
464 _player->SetUInt32Value(PLAYER_SELF_RES_SPELL, 0);