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

Revision 274, 15.6 kB (checked in by yumileroy, 17 years ago)

* Implemented new summon possessed summon type for spell 49352.
* Unsummon all summon possessed units on summoning aura cancel.

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