root/trunk/src/game/SpellHandler.cpp @ 219

Revision 207, 14.7 kB (checked in by yumileroy, 17 years ago)

[svn] * Improve some arena team related DB access
* Cache GM tickets on server startup.
* Remove unused src/game/HateMatrix.h and references.
* Better check client inventory pos data received in some client packets to
skip invalid cases

Original author: KingPin?
Date: 2008-11-10 09:04:23-06:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Common.h"
22#include "Database/DBCStores.h"
23#include "WorldPacket.h"
24#include "WorldSession.h"
25#include "World.h"
26#include "ObjectMgr.h"
27#include "SpellMgr.h"
28#include "Log.h"
29#include "Opcodes.h"
30#include "Spell.h"
31#include "SpellAuras.h"
32#include "BattleGround.h"
33#include "MapManager.h"
34#include "ScriptCalls.h"
35#include "Totem.h"
36
37void WorldSession::HandleUseItemOpcode(WorldPacket& recvPacket)
38{
39    // TODO: add targets.read() check
40    CHECK_PACKET_SIZE(recvPacket,1+1+1+1+8);
41
42    Player* pUser = _player;
43    uint8 bagIndex, slot;
44    uint8 spell_count;                                      // number of spells at item, not used
45    uint8 cast_count;                                       // next cast if exists (single or not)
46    uint64 item_guid;
47
48    recvPacket >> bagIndex >> slot >> spell_count >> cast_count >> item_guid;
49
50    Item *pItem = pUser->GetItemByPos(bagIndex, slot);
51    if(!pItem)
52    {
53        pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
54        return;
55    }
56
57    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());
58
59    ItemPrototype const *proto = pItem->GetProto();
60    if(!proto)
61    {
62        pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
63        return;
64    }
65
66    // some item classes can be used only in equipped state
67    if(proto->InventoryType != INVTYPE_NON_EQUIP && !pItem->IsEquipped())
68    {
69        pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
70        return;
71    }
72
73    uint8 msg = pUser->CanUseItem(pItem);
74    if( msg != EQUIP_ERR_OK )
75    {
76        pUser->SendEquipError( msg, pItem, NULL );
77        return;
78    }
79
80    // only allow conjured consumable, bandage, poisons (all should have the 2^21 item flag set in DB)
81    if( proto->Class == ITEM_CLASS_CONSUMABLE &&
82        !(proto->Flags & ITEM_FLAGS_USEABLE_IN_ARENA) &&
83        pUser->InArena())
84    {
85        pUser->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH,pItem,NULL);
86        return;
87    }
88
89    if (pUser->isInCombat())
90    {
91        for(int i = 0; i < 5; ++i)
92        {
93            if (SpellEntry const *spellInfo = sSpellStore.LookupEntry(proto->Spells[i].SpellId))
94            {
95                if (IsNonCombatSpell(spellInfo))
96                {
97                    pUser->SendEquipError(EQUIP_ERR_NOT_IN_COMBAT,pItem,NULL);
98                    return;
99                }
100            }
101        }
102    }
103
104    // check also  BIND_WHEN_PICKED_UP and BIND_QUEST_ITEM for .additem or .additemset case by GM (not binded at adding to inventory)
105    if( pItem->GetProto()->Bonding == BIND_WHEN_USE || pItem->GetProto()->Bonding == BIND_WHEN_PICKED_UP || pItem->GetProto()->Bonding == BIND_QUEST_ITEM )
106    {
107        if (!pItem->IsSoulBound())
108        {
109            pItem->SetState(ITEM_CHANGED, pUser);
110            pItem->SetBinding( true );
111        }
112    }
113
114    SpellCastTargets targets;
115    if(!targets.read(&recvPacket, pUser))
116        return;
117
118    //Note: If script stop casting it must send appropriate data to client to prevent stuck item in gray state.
119    if(!Script->ItemUse(pUser,pItem,targets))
120    {
121        // no script or script not process request by self
122
123        // special learning case
124        if(pItem->GetProto()->Spells[0].SpellId==SPELL_ID_GENERIC_LEARN)
125        {
126            uint32 learning_spell_id = pItem->GetProto()->Spells[1].SpellId;
127
128            SpellEntry const *spellInfo = sSpellStore.LookupEntry(SPELL_ID_GENERIC_LEARN);
129            if(!spellInfo)
130            {
131                sLog.outError("Item (Entry: %u) in have wrong spell id %u, ignoring ",proto->ItemId, SPELL_ID_GENERIC_LEARN);
132                pUser->SendEquipError(EQUIP_ERR_NONE,pItem,NULL);
133                return;
134            }
135
136            Spell *spell = new Spell(pUser, spellInfo, false);
137            spell->m_CastItem = pItem;
138            spell->m_cast_count = cast_count;               //set count of casts
139            spell->m_currentBasePoints[0] = learning_spell_id;
140            spell->prepare(&targets);
141            return;
142        }
143
144        // use triggered flag only for items with many spell casts and for not first cast
145        int count = 0;
146
147        for(int i = 0; i < 5; ++i)
148        {
149            _Spell const& spellData = pItem->GetProto()->Spells[i];
150
151            // no spell
152            if(!spellData.SpellId)
153                continue;
154
155            // wrong triggering type
156            if( spellData.SpellTrigger != ITEM_SPELLTRIGGER_ON_USE && spellData.SpellTrigger != ITEM_SPELLTRIGGER_ON_NO_DELAY_USE)
157                continue;
158
159            SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellData.SpellId);
160            if(!spellInfo)
161            {
162                sLog.outError("Item (Entry: %u) in have wrong spell id %u, ignoring ",proto->ItemId, spellData.SpellId);
163                continue;
164            }
165
166            Spell *spell = new Spell(pUser, spellInfo, (count > 0));
167            spell->m_CastItem = pItem;
168            spell->m_cast_count = cast_count;               //set count of casts
169            spell->prepare(&targets);
170
171            ++count;
172        }
173    }
174}
175
176#define OPEN_CHEST 11437
177#define OPEN_SAFE 11535
178#define OPEN_CAGE 11792
179#define OPEN_BOOTY_CHEST 5107
180#define OPEN_STRONGBOX 8517
181
182void WorldSession::HandleOpenItemOpcode(WorldPacket& recvPacket)
183{
184    CHECK_PACKET_SIZE(recvPacket,1+1);
185
186    sLog.outDetail("WORLD: CMSG_OPEN_ITEM packet, data length = %i",recvPacket.size());
187
188    Player* pUser = _player;
189    uint8 bagIndex, slot;
190
191    recvPacket >> bagIndex >> slot;
192
193    sLog.outDetail("bagIndex: %u, slot: %u",bagIndex,slot);
194
195    Item *pItem = pUser->GetItemByPos(bagIndex, slot);
196    if(!pItem)
197    {
198        pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, NULL, NULL );
199        return;
200    }
201
202    ItemPrototype const *proto = pItem->GetProto();
203    if(!proto)
204    {
205        pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, NULL );
206        return;
207    }
208
209    // locked item
210    uint32 lockId = proto->LockID;
211    if(lockId)
212    {
213        LockEntry const *lockInfo = sLockStore.LookupEntry(lockId);
214
215        if (!lockInfo)
216        {
217            pUser->SendEquipError(EQUIP_ERR_ITEM_LOCKED, pItem, NULL );
218            sLog.outError( "WORLD::OpenItem: item [guid = %u] has an unknown lockId: %u!", pItem->GetGUIDLow() , lockId);
219            return;
220        }
221
222        // required picklocking
223        if(lockInfo->requiredlockskill || lockInfo->requiredminingskill)
224        {
225            pUser->SendEquipError(EQUIP_ERR_ITEM_LOCKED, pItem, NULL );
226            return;
227        }
228    }
229
230    if(pItem->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))// wrapped?
231    {
232        QueryResult *result = CharacterDatabase.PQuery("SELECT entry, flags FROM character_gifts WHERE item_guid = '%u'", pItem->GetGUIDLow());
233        if (result)
234        {
235            Field *fields = result->Fetch();
236            uint32 entry = fields[0].GetUInt32();
237            uint32 flags = fields[1].GetUInt32();
238
239            pItem->SetUInt64Value(ITEM_FIELD_GIFTCREATOR, 0);
240            pItem->SetUInt32Value(OBJECT_FIELD_ENTRY, entry);
241            pItem->SetUInt32Value(ITEM_FIELD_FLAGS, flags);
242            pItem->SetState(ITEM_CHANGED, pUser);
243            delete result;
244        }
245        else
246        {
247            sLog.outError("Wrapped item %u don't have record in character_gifts table and will deleted", pItem->GetGUIDLow());
248            pUser->DestroyItem(pItem->GetBagSlot(), pItem->GetSlot(), true);
249            return;
250        }
251        CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", pItem->GetGUIDLow());
252    }
253    else
254        pUser->SendLoot(pItem->GetGUID(),LOOT_CORPSE);
255}
256
257void WorldSession::HandleGameObjectUseOpcode( WorldPacket & recv_data )
258{
259    CHECK_PACKET_SIZE(recv_data,8);
260
261    uint64 guid;
262    uint32 spellId = OPEN_CHEST;
263
264    recv_data >> guid;
265
266    sLog.outDebug( "WORLD: Recvd CMSG_GAMEOBJ_USE Message [guid=%u]", GUID_LOPART(guid));
267    GameObject *obj = ObjectAccessor::GetGameObject(*_player, guid);
268
269    if(!obj)
270        return;
271
272    if (Script->GOHello(_player, obj))
273        return;
274
275    obj->Use(_player);
276}
277
278void WorldSession::HandleCastSpellOpcode(WorldPacket& recvPacket)
279{
280    CHECK_PACKET_SIZE(recvPacket,4+1+2);
281
282    uint32 spellId;
283    uint8  cast_count;
284    recvPacket >> spellId;
285    recvPacket >> cast_count;
286
287    sLog.outDebug("WORLD: got cast spell packet, spellId - %u, cast_count: %u data length = %i",
288        spellId, cast_count, recvPacket.size());
289
290    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
291
292    if(!spellInfo)
293    {
294        sLog.outError("WORLD: unknown spell id %u", spellId);
295        return;
296    }
297
298    // not have spell or spell passive and not casted by client
299    if ( !_player->HasSpell (spellId) || IsPassiveSpell(spellId) )
300    {
301        //cheater? kick? ban?
302        return;
303    }
304
305    // can't use our own spells when we're in possession of another unit,
306    if(_player->isPossessing())
307        return;
308
309    // client provided targets
310    SpellCastTargets targets;
311    if(!targets.read(&recvPacket,_player))
312        return;
313
314    // auto-selection buff level base at target level (in spellInfo)
315    if(targets.getUnitTarget())
316    {
317        SpellEntry const *actualSpellInfo = spellmgr.SelectAuraRankForPlayerLevel(spellInfo,targets.getUnitTarget()->getLevel());
318
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;
322    }
323
324    Spell *spell = new Spell(_player, spellInfo, false);
325    spell->m_cast_count = cast_count;                       //set count of casts
326    spell->prepare(&targets);
327}
328
329void WorldSession::HandleCancelCastOpcode(WorldPacket& recvPacket)
330{
331    CHECK_PACKET_SIZE(recvPacket,4);
332
333    uint32 spellId;
334    recvPacket >> spellId;
335
336    //FIXME: hack, ignore unexpected client cancel Deadly Throw cast
337    if(spellId==26679)
338        return;
339
340    if(_player->IsNonMeleeSpellCasted(false))
341        _player->InterruptNonMeleeSpells(false,spellId);
342}
343
344void WorldSession::HandleCancelAuraOpcode( WorldPacket& recvPacket)
345{
346    CHECK_PACKET_SIZE(recvPacket,4);
347
348    uint32 spellId;
349    recvPacket >> spellId;
350
351    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
352    if (!spellInfo)
353        return;
354
355    // Remove possess/charm aura from the possessed/charmed as well
356    // TODO: Remove this once the ability to cancel aura sets at once is implemented
357    if(_player->GetCharm())
358    {
359        for (int i = 0; i < 3; ++i)
360        {
361            if (spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_POSSESS ||
362                spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_POSSESS_PET ||
363                spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_CHARM)
364            {
365                _player->RemoveAurasDueToSpellByCancel(spellId);
366                _player->GetCharm()->RemoveAurasDueToSpellByCancel(spellId);
367                return;
368            }
369        }
370    }
371
372    // not allow remove non positive spells and spells with attr SPELL_ATTR_CANT_CANCEL
373    if(!IsPositiveSpell(spellId) || (spellInfo->Attributes & SPELL_ATTR_CANT_CANCEL))
374        return;
375
376    _player->RemoveAurasDueToSpellByCancel(spellId);
377
378    if (spellId == 2584)                                    // Waiting to resurrect spell cancel, we must remove player from resurrect queue
379    {
380        BattleGround *bg = _player->GetBattleGround();
381        if(!bg)
382            return;
383        bg->RemovePlayerFromResurrectQueue(_player->GetGUID());
384    }
385}
386
387void WorldSession::HandlePetCancelAuraOpcode( WorldPacket& recvPacket)
388{
389    CHECK_PACKET_SIZE(recvPacket, 8+4);
390
391    uint64 guid;
392    uint32 spellId;
393
394    recvPacket >> guid;
395    recvPacket >> spellId;
396
397    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
398    if(!spellInfo)
399    {
400        sLog.outError("WORLD: unknown PET spell id %u", spellId);
401        return;
402    }
403
404    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
405
406    if(!pet)
407    {
408        sLog.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid)) );
409        return;
410    }
411
412    if(pet != GetPlayer()->GetPet() && pet != GetPlayer()->GetCharm())
413    {
414        sLog.outError( "HandlePetCancelAura.Pet %u isn't pet of player %s", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
415        return;
416    }
417
418    if(!pet->isAlive())
419    {
420        pet->SendPetActionFeedback(FEEDBACK_PET_DEAD);
421        return;
422    }
423
424    pet->RemoveAurasDueToSpell(spellId);
425
426    pet->AddCreatureSpellCooldown(spellId);
427}
428
429void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket& /*recvPacket*/)
430{
431    // nothing do
432}
433
434void WorldSession::HandleCancelAutoRepeatSpellOpcode( WorldPacket& /*recvPacket*/)
435{
436    // may be better send SMSG_CANCEL_AUTO_REPEAT?
437    // cancel and prepare for deleting
438    _player->InterruptSpell(CURRENT_AUTOREPEAT_SPELL);
439}
440
441/// \todo Complete HandleCancelChanneling function
442void WorldSession::HandleCancelChanneling( WorldPacket & /*recv_data */)
443{
444    /*
445        CHECK_PACKET_SIZE(recv_data, 4);
446
447        uint32 spellid;
448        recv_data >> spellid;
449    */
450}
451
452void WorldSession::HandleTotemDestroy( WorldPacket& recvPacket)
453{
454    CHECK_PACKET_SIZE(recvPacket, 1);
455
456    uint8 slotId;
457
458    recvPacket >> slotId;
459
460    if (slotId >= MAX_TOTEM)
461        return;
462
463    if(!_player->m_TotemSlot[slotId])
464        return;
465
466    Creature* totem = ObjectAccessor::GetCreature(*_player,_player->m_TotemSlot[slotId]);
467    if(totem && totem->isTotem())
468        ((Totem*)totem)->UnSummon();
469}
470
471void WorldSession::HandleSelfResOpcode( WorldPacket & /*recv_data*/ )
472{
473    sLog.outDebug("WORLD: CMSG_SELF_RES");                  // empty opcode
474
475    if(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL))
476    {
477        SpellEntry const *spellInfo = sSpellStore.LookupEntry(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL));
478        if(spellInfo)
479            _player->CastSpell(_player,spellInfo,false,0);
480
481        _player->SetUInt32Value(PLAYER_SELF_RES_SPELL, 0);
482    }
483}
Note: See TracBrowser for help on using the browser.