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

Revision 283, 15.3 kB (checked in by yumileroy, 17 years ago)

*Alterac Valley. By Bogie and Balrok. Note: some core contents are modified. Will fix them later. Some sql are disabled because of possible conflict with offical DB. Use them at your own risk.

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