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

Revision 284, 15.8 kB (checked in by yumileroy, 17 years ago)

Merge with 284 (54b0e67d97fe).

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