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

Revision 44, 13.9 kB (checked in by yumileroy, 17 years ago)

[svn] * Merge Temp dev SVN with Assembla.
* Changes include:

  • Implementation of w12x's Outdoor PvP and Game Event Systems.
  • Temporary removal of IRC Chat Bot (until infinite loop when disabled is fixed).
  • All mangos -> trinity (to convert your mangos_string table, please run mangos_string_to_trinity_string.sql).
  • Improved Config cleanup.
  • And many more changes.

Original author: Seline
Date: 2008-10-14 11:57:03-05:00

Line 
1/*
2 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
3 *
4 * Thanks to the original authors: MaNGOS <http://www.mangosproject.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);
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    // client provided targets
306    SpellCastTargets targets;
307    if(!targets.read(&recvPacket,_player))
308        return;
309
310    // auto-selection buff level base at target level (in spellInfo)
311    if(targets.getUnitTarget())
312    {
313        SpellEntry const *actualSpellInfo = spellmgr.SelectAuraRankForPlayerLevel(spellInfo,targets.getUnitTarget()->getLevel());
314
315        // 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
316        if(actualSpellInfo)
317            spellInfo = actualSpellInfo;
318    }
319
320    Spell *spell = new Spell(_player, spellInfo, false);
321    spell->m_cast_count = cast_count;                       //set count of casts
322    spell->prepare(&targets);
323}
324
325void WorldSession::HandleCancelCastOpcode(WorldPacket& recvPacket)
326{
327    CHECK_PACKET_SIZE(recvPacket,4);
328
329    uint32 spellId;
330    recvPacket >> spellId;
331
332    //FIXME: hack, ignore unexpected client cancel Deadly Throw cast
333    if(spellId==26679)
334        return;
335
336    if(_player->IsNonMeleeSpellCasted(false))
337        _player->InterruptNonMeleeSpells(false,spellId);
338}
339
340void WorldSession::HandleCancelAuraOpcode( WorldPacket& recvPacket)
341{
342    CHECK_PACKET_SIZE(recvPacket,4);
343
344    uint32 spellId;
345    recvPacket >> spellId;
346
347    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
348    if (!spellInfo)
349        return;
350
351    // not allow remove non positive spells and spells with attr SPELL_ATTR_CANT_CANCEL
352    if(!IsPositiveSpell(spellId) || (spellInfo->Attributes & SPELL_ATTR_CANT_CANCEL))
353        return;
354
355    _player->RemoveAurasDueToSpellByCancel(spellId);
356
357    if (spellId == 2584)                                    // Waiting to resurrect spell cancel, we must remove player from resurrect queue
358    {
359        BattleGround *bg = _player->GetBattleGround();
360        if(!bg)
361            return;
362        bg->RemovePlayerFromResurrectQueue(_player->GetGUID());
363    }
364}
365
366void WorldSession::HandlePetCancelAuraOpcode( WorldPacket& recvPacket)
367{
368    CHECK_PACKET_SIZE(recvPacket, 8+4);
369
370    uint64 guid;
371    uint32 spellId;
372
373    recvPacket >> guid;
374    recvPacket >> spellId;
375
376    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId );
377    if(!spellInfo)
378    {
379        sLog.outError("WORLD: unknown PET spell id %u", spellId);
380        return;
381    }
382
383    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
384
385    if(!pet)
386    {
387        sLog.outError( "Pet %u not exist.", uint32(GUID_LOPART(guid)) );
388        return;
389    }
390
391    if(pet != GetPlayer()->GetPet() && pet != GetPlayer()->GetCharm())
392    {
393        sLog.outError( "HandlePetCancelAura.Pet %u isn't pet of player %s", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
394        return;
395    }
396
397    if(!pet->isAlive())
398    {
399        pet->SendPetActionFeedback(FEEDBACK_PET_DEAD);
400        return;
401    }
402
403    pet->RemoveAurasDueToSpell(spellId);
404
405    pet->AddCreatureSpellCooldown(spellId);
406}
407
408void WorldSession::HandleCancelGrowthAuraOpcode( WorldPacket& /*recvPacket*/)
409{
410    // nothing do
411}
412
413void WorldSession::HandleCancelAutoRepeatSpellOpcode( WorldPacket& /*recvPacket*/)
414{
415    // may be better send SMSG_CANCEL_AUTO_REPEAT?
416    // cancel and prepare for deleting
417    _player->InterruptSpell(CURRENT_AUTOREPEAT_SPELL);
418}
419
420/// \todo Complete HandleCancelChanneling function
421void WorldSession::HandleCancelChanneling( WorldPacket & /*recv_data */)
422{
423    /*
424        CHECK_PACKET_SIZE(recv_data, 4);
425
426        uint32 spellid;
427        recv_data >> spellid;
428    */
429}
430
431void WorldSession::HandleTotemDestroy( WorldPacket& recvPacket)
432{
433    CHECK_PACKET_SIZE(recvPacket, 1);
434
435    uint8 slotId;
436
437    recvPacket >> slotId;
438
439    if (slotId >= MAX_TOTEM)
440        return;
441
442    if(!_player->m_TotemSlot[slotId])
443        return;
444
445    Creature* totem = ObjectAccessor::GetCreature(*_player,_player->m_TotemSlot[slotId]);
446    if(totem && totem->isTotem())
447        ((Totem*)totem)->UnSummon();
448}
449
450void WorldSession::HandleSelfResOpcode( WorldPacket & /*recv_data*/ )
451{
452    sLog.outDebug("WORLD: CMSG_SELF_RES");                  // empty opcode
453
454    if(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL))
455    {
456        SpellEntry const *spellInfo = sSpellStore.LookupEntry(_player->GetUInt32Value(PLAYER_SELF_RES_SPELL));
457        if(spellInfo)
458            _player->CastSpell(_player,spellInfo,false,0);
459
460        _player->SetUInt32Value(PLAYER_SELF_RES_SPELL, 0);
461    }
462}
Note: See TracBrowser for help on using the browser.