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

Revision 260, 15.2 kB (checked in by yumileroy, 17 years ago)

*DB script table stucture change. Source Mangos. Also fix some bugs. Hopefully this rev will make program usable again.

Original author: megamage
Date: 2008-11-20 10:43:20-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                _player->RemoveAurasDueToSpellByCancel(spellId);
372                if (_player->GetCharm())
373                    _player->GetCharm()->RemoveAurasDueToSpellByCancel(spellId);
374                else if (_player->GetFarsightTarget()->GetTypeId() != TYPEID_DYNAMICOBJECT)
375                    ((Unit*)_player->GetFarsightTarget())->RemoveAurasDueToSpellByCancel(spellId);
376                return;
377            }
378        }
379    }
380
381    // not allow remove non positive spells and spells with attr SPELL_ATTR_CANT_CANCEL
382    if(!IsPositiveSpell(spellId) || (spellInfo->Attributes & SPELL_ATTR_CANT_CANCEL))
383        return;
384
385    _player->RemoveAurasDueToSpellByCancel(spellId);
386
387    if (spellId == 2584)                                    // Waiting to resurrect spell cancel, we must remove player from resurrect queue
388    {
389        BattleGround *bg = _player->GetBattleGround();
390        if(!bg)
391            return;
392        bg->RemovePlayerFromResurrectQueue(_player->GetGUID());
393    }
394}
395
396void WorldSession::HandlePetCancelAuraOpcode( WorldPacket& recvPacket)
397{
398    CHECK_PACKET_SIZE(recvPacket, 8+4);
399
400    uint64 guid;
401    uint32 spellId;
402
403    recvPacket >> guid;
404    recvPacket >> spellId;
405
406    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
407    if(!spellInfo)
408    {
409        sLog.outError("WORLD: unknown PET spell id %u", spellId);
410        return;
411    }
412
413    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
414
415    if(!pet)
416    {
417        sLog.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid)) );
418        return;
419    }
420
421    if(pet != GetPlayer()->GetPet() && pet != GetPlayer()->GetCharm())
422    {
423        sLog.outError( "HandlePetCancelAura.Pet %u isn't pet of player %s", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
424        return;
425    }
426
427    if(!pet->isAlive())
428    {
429        pet->SendPetActionFeedback(FEEDBACK_PET_DEAD);
430        return;
431    }
432
433    pet->RemoveAurasDueToSpell(spellId);
434
435    pet->AddCreatureSpellCooldown(spellId);
436}
437
438void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket& /*recvPacket*/)
439{
440    // nothing do
441}
442
443void WorldSession::HandleCancelAutoRepeatSpellOpcode( WorldPacket& /*recvPacket*/)
444{
445    // may be better send SMSG_CANCEL_AUTO_REPEAT?
446    // cancel and prepare for deleting
447    _player->InterruptSpell(CURRENT_AUTOREPEAT_SPELL);
448}
449
450/// \todo Complete HandleCancelChanneling function
451void WorldSession::HandleCancelChanneling( WorldPacket & /*recv_data */)
452{
453    /*
454        CHECK_PACKET_SIZE(recv_data, 4);
455
456        uint32 spellid;
457        recv_data >> spellid;
458    */
459}
460
461void WorldSession::HandleTotemDestroy( WorldPacket& recvPacket)
462{
463    CHECK_PACKET_SIZE(recvPacket, 1);
464
465    uint8 slotId;
466
467    recvPacket >> slotId;
468
469    if (slotId >= MAX_TOTEM)
470        return;
471
472    if(!_player->m_TotemSlot[slotId])
473        return;
474
475    Creature* totem = ObjectAccessor::GetCreature(*_player,_player->m_TotemSlot[slotId]);
476    // Don't unsummon sentry totem
477    if(totem && totem->isTotem() && totem->GetEntry() != SENTRY_TOTEM_ENTRY)
478        ((Totem*)totem)->UnSummon();
479}
480
481void WorldSession::HandleSelfResOpcode( WorldPacket & /*recv_data*/ )
482{
483    sLog.outDebug("WORLD: CMSG_SELF_RES");                  // empty opcode
484
485    if(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL))
486    {
487        SpellEntry const *spellInfo = sSpellStore.LookupEntry(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL));
488        if(spellInfo)
489            _player->CastSpell(_player,spellInfo,false,0);
490
491        _player->SetUInt32Value(PLAYER_SELF_RES_SPELL, 0);
492    }
493}
Note: See TracBrowser for help on using the browser.