root/trunk/src/game/Item.cpp @ 102

Revision 102, 29.3 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-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 "Item.h"
23#include "ObjectMgr.h"
24#include "WorldPacket.h"
25#include "Database/DatabaseEnv.h"
26#include "ItemEnchantmentMgr.h"
27
28void AddItemsSetItem(Player*player,Item *item)
29{
30    ItemPrototype const *proto = item->GetProto();
31    uint32 setid = proto->ItemSet;
32
33    ItemSetEntry const *set = sItemSetStore.LookupEntry(setid);
34
35    if(!set)
36    {
37        sLog.outErrorDb("Item set %u for item (id %u) not found, mods not applied.",setid,proto->ItemId);
38        return;
39    }
40
41    if( set->required_skill_id && player->GetSkillValue(set->required_skill_id) < set->required_skill_value )
42        return;
43
44    ItemSetEffect *eff = NULL;
45
46    for(size_t x = 0; x < player->ItemSetEff.size(); ++x)
47    {
48        if(player->ItemSetEff[x] && player->ItemSetEff[x]->setid == setid)
49        {
50            eff = player->ItemSetEff[x];
51            break;
52        }
53    }
54
55    if(!eff)
56    {
57        eff = new ItemSetEffect;
58        memset(eff,0,sizeof(ItemSetEffect));
59        eff->setid = setid;
60
61        size_t x = 0;
62        for(; x < player->ItemSetEff.size(); x++)
63            if(!player->ItemSetEff[x])
64                break;
65
66        if(x < player->ItemSetEff.size())
67            player->ItemSetEff[x]=eff;
68        else
69            player->ItemSetEff.push_back(eff);
70    }
71
72    ++eff->item_count;
73
74    for(uint32 x=0;x<8;x++)
75    {
76        if(!set->spells [x])
77            continue;
78        //not enough for  spell
79        if(set->items_to_triggerspell[x] > eff->item_count)
80            continue;
81
82        uint32 z=0;
83        for(;z<8;z++)
84            if(eff->spells[z] && eff->spells[z]->Id==set->spells[x])
85                break;
86
87        if(z < 8)
88            continue;
89
90        //new spell
91        for(uint32 y=0;y<8;y++)
92        {
93            if(!eff->spells[y])                             // free slot
94            {
95                SpellEntry const *spellInfo = sSpellStore.LookupEntry(set->spells[x]);
96                if(!spellInfo)
97                {
98                    sLog.outError("WORLD: unknown spell id %u in items set %u effects", set->spells[x],setid);
99                    break;
100                }
101
102                // spell casted only if fit form requirement, in other case will casted at form change
103                player->ApplyEquipSpell(spellInfo,NULL,true);
104                eff->spells[y] = spellInfo;
105                break;
106            }
107        }
108    }
109}
110
111void RemoveItemsSetItem(Player*player,ItemPrototype const *proto)
112{
113    uint32 setid = proto->ItemSet;
114
115    ItemSetEntry const *set = sItemSetStore.LookupEntry(setid);
116
117    if(!set)
118    {
119        sLog.outErrorDb("Item set #%u for item #%u not found, mods not removed.",setid,proto->ItemId);
120        return;
121    }
122
123    ItemSetEffect *eff = NULL;
124    size_t setindex = 0;
125    for(;setindex < player->ItemSetEff.size(); setindex++)
126    {
127        if(player->ItemSetEff[setindex] && player->ItemSetEff[setindex]->setid == setid)
128        {
129            eff = player->ItemSetEff[setindex];
130            break;
131        }
132    }
133
134    // can be in case now enough skill requirement for set appling but set has been appliend when skill requirement not enough
135    if(!eff)
136        return;
137
138    --eff->item_count;
139
140    for(uint32 x=0;x<8;x++)
141    {
142        if(!set->spells[x])
143            continue;
144
145        // enough for spell
146        if(set->items_to_triggerspell[x] <= eff->item_count)
147            continue;
148
149        for(uint32 z=0;z<8;z++)
150        {
151            if(eff->spells[z] && eff->spells[z]->Id==set->spells[x])
152            {
153                // spell can be not active if not fit form requirement
154                player->ApplyEquipSpell(eff->spells[z],NULL,false);
155                eff->spells[z]=NULL;
156                break;
157            }
158        }
159    }
160
161    if(!eff->item_count)                                    //all items of a set were removed
162    {
163        assert(eff == player->ItemSetEff[setindex]);
164        delete eff;
165        player->ItemSetEff[setindex] = NULL;
166    }
167}
168
169bool ItemCanGoIntoBag(ItemPrototype const *pProto, ItemPrototype const *pBagProto)
170{
171    if(!pProto || !pBagProto)
172        return false;
173
174    switch(pBagProto->Class)
175    {
176        case ITEM_CLASS_CONTAINER:
177            switch(pBagProto->SubClass)
178            {
179                case ITEM_SUBCLASS_CONTAINER:
180                    return true;
181                case ITEM_SUBCLASS_SOUL_CONTAINER:
182                    if(!(pProto->BagFamily & BAG_FAMILY_MASK_SHARDS))
183                        return false;
184                    return true;
185                case ITEM_SUBCLASS_HERB_CONTAINER:
186                    if(!(pProto->BagFamily & BAG_FAMILY_MASK_HERBS))
187                        return false;
188                    return true;
189                case ITEM_SUBCLASS_ENCHANTING_CONTAINER:
190                    if(!(pProto->BagFamily & BAG_FAMILY_MASK_ENCHANTING_SUPP))
191                        return false;
192                    return true;
193                case ITEM_SUBCLASS_MINING_CONTAINER:
194                    if(!(pProto->BagFamily & BAG_FAMILY_MASK_MINING_SUPP))
195                        return false;
196                    return true;
197                case ITEM_SUBCLASS_ENGINEERING_CONTAINER:
198                    if(!(pProto->BagFamily & BAG_FAMILY_MASK_ENGINEERING_SUPP))
199                        return false;
200                    return true;
201                case ITEM_SUBCLASS_GEM_CONTAINER:
202                    if(!(pProto->BagFamily & BAG_FAMILY_MASK_GEMS))
203                        return false;
204                    return true;
205                case ITEM_SUBCLASS_LEATHERWORKING_CONTAINER:
206                    if(!(pProto->BagFamily & BAG_FAMILY_MASK_LEATHERWORKING_SUPP))
207                        return false;
208                    return true;
209                default:
210                    return false;
211            }
212        case ITEM_CLASS_QUIVER:
213            switch(pBagProto->SubClass)
214            {
215                case ITEM_SUBCLASS_QUIVER:
216                    if(!(pProto->BagFamily & BAG_FAMILY_MASK_ARROWS))
217                        return false;
218                    return true;
219                case ITEM_SUBCLASS_AMMO_POUCH:
220                    if(!(pProto->BagFamily & BAG_FAMILY_MASK_BULLETS))
221                        return false;
222                    return true;
223                default:
224                    return false;
225            }
226    }
227    return false;
228}
229
230Item::Item( )
231{
232    m_objectType |= TYPEMASK_ITEM;
233    m_objectTypeId = TYPEID_ITEM;
234                                                            // 2.3.2 - 0x18
235    m_updateFlag = (UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID);
236
237    m_valuesCount = ITEM_END;
238    m_slot = 0;
239    uState = ITEM_NEW;
240    uQueuePos = -1;
241    m_container = NULL;
242    m_lootGenerated = false;
243    mb_in_trade = false;
244}
245
246bool Item::Create( uint32 guidlow, uint32 itemid, Player const* owner)
247{
248    Object::_Create( guidlow, 0, HIGHGUID_ITEM );
249
250    SetUInt32Value(OBJECT_FIELD_ENTRY, itemid);
251    SetFloatValue(OBJECT_FIELD_SCALE_X, 1.0f);
252
253    SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0);
254    SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0);
255
256    ItemPrototype const *itemProto = objmgr.GetItemPrototype(itemid);
257    if(!itemProto)
258        return false;
259
260    SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1);
261    SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability);
262    SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability);
263
264    for(int i = 0; i < 5; ++i)
265        SetSpellCharges(i,itemProto->Spells[i].SpellCharges);
266
267    SetUInt32Value(ITEM_FIELD_FLAGS, itemProto->Flags);
268    SetUInt32Value(ITEM_FIELD_DURATION, abs(itemProto->Duration));
269
270    return true;
271}
272
273void Item::UpdateDuration(Player* owner, uint32 diff)
274{
275    if (!GetUInt32Value(ITEM_FIELD_DURATION))
276        return;
277
278    sLog.outDebug("Item::UpdateDuration Item (Entry: %u Duration %u Diff %u)",GetEntry(),GetUInt32Value(ITEM_FIELD_DURATION),diff);
279
280    if (GetUInt32Value(ITEM_FIELD_DURATION)<=diff)
281    {
282        owner->DestroyItem(GetBagSlot(), GetSlot(), true);
283        return;
284    }
285
286    SetUInt32Value(ITEM_FIELD_DURATION, GetUInt32Value(ITEM_FIELD_DURATION) - diff);
287    SetState(ITEM_CHANGED);                                 // save new time in database
288}
289
290void Item::SaveToDB()
291{
292    uint32 guid = GetGUIDLow();
293    switch (uState)
294    {
295        case ITEM_NEW:
296        {
297            CharacterDatabase.PExecute( "DELETE FROM item_instance WHERE guid = '%u'", guid );
298            std::ostringstream ss;
299            ss << "INSERT INTO item_instance (guid,owner_guid,data) VALUES (" << guid << "," << GUID_LOPART(GetOwnerGUID()) << ",'";
300            for(uint16 i = 0; i < m_valuesCount; i++ )
301                ss << GetUInt32Value(i) << " ";
302            ss << "' )";
303            CharacterDatabase.Execute( ss.str().c_str() );
304        } break;
305        case ITEM_CHANGED:
306        {
307            std::ostringstream ss;
308            ss << "UPDATE item_instance SET data = '";
309            for(uint16 i = 0; i < m_valuesCount; i++ )
310                ss << GetUInt32Value(i) << " ";
311            ss << "', owner_guid = '" << GUID_LOPART(GetOwnerGUID()) << "' WHERE guid = '" << guid << "'";
312
313            CharacterDatabase.Execute( ss.str().c_str() );
314
315            if(HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
316                CharacterDatabase.PExecute("UPDATE character_gifts SET guid = '%u' WHERE item_guid = '%u'", GUID_LOPART(GetOwnerGUID()),GetGUIDLow());
317        } break;
318        case ITEM_REMOVED:
319        {
320            if (GetUInt32Value(ITEM_FIELD_ITEM_TEXT_ID) > 0 )
321                CharacterDatabase.PExecute("DELETE FROM item_text WHERE id = '%u'", GetUInt32Value(ITEM_FIELD_ITEM_TEXT_ID));
322            CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid = '%u'", guid);
323            if(HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
324                CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", GetGUIDLow());
325            delete this;
326            return;
327        }
328        case ITEM_UNCHANGED:
329            break;
330    }
331    SetState(ITEM_UNCHANGED);
332}
333
334bool Item::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result)
335{
336    // create item before any checks for store correct guid
337    // and allow use "FSetState(ITEM_REMOVED); SaveToDB();" for deleting item from DB
338    Object::_Create(guid, 0, HIGHGUID_ITEM);
339
340    bool delete_result = false;
341    if(!result)
342    {
343        result = CharacterDatabase.PQuery("SELECT data FROM item_instance WHERE guid = '%u'", guid);
344        delete_result = true;
345    }
346
347    if (!result)
348    {
349        sLog.outError("ERROR: Item (GUID: %u owner: %u) not found in table `item_instance`, can't load. ",guid,GUID_LOPART(owner_guid));
350        return false;
351    }
352
353    Field *fields = result->Fetch();
354
355    if(!LoadValues(fields[0].GetString()))
356    {
357        sLog.outError("ERROR: Item #%d have broken data in `data` field. Can't be loaded.",guid);
358        if (delete_result) delete result;
359        return false;
360    }
361
362    bool need_save = false;                                 // need explicit save data at load fixes
363
364    // overwrite possible wrong/corrupted guid
365    uint64 new_item_guid = MAKE_NEW_GUID(guid,0, HIGHGUID_ITEM);
366    if(GetUInt64Value(OBJECT_FIELD_GUID) != new_item_guid)
367    {
368        SetUInt64Value(OBJECT_FIELD_GUID, MAKE_NEW_GUID(guid,0, HIGHGUID_ITEM));
369        need_save = true;
370    }
371
372    if (delete_result) delete result;
373
374    ItemPrototype const* proto = GetProto();
375    if(!proto)
376        return false;
377
378    // recalculate suffix factor
379    if(GetItemRandomPropertyId() < 0)
380    {
381        if(UpdateItemSuffixFactor())
382            need_save = true;
383    }
384
385    // Remove bind flag for items vs NO_BIND set
386    if (IsSoulBound() && proto->Bonding == NO_BIND)
387    {
388        ApplyModFlag(ITEM_FIELD_FLAGS,ITEM_FLAGS_BINDED, false);
389        need_save = true;
390    }
391
392    // update duration if need, and remove if not need
393    if((proto->Duration==0) != (GetUInt32Value(ITEM_FIELD_DURATION)==0))
394    {
395        SetUInt32Value(ITEM_FIELD_DURATION,abs(proto->Duration));
396        need_save = true;
397    }
398
399    // set correct owner
400    if(owner_guid != 0 && GetOwnerGUID() != owner_guid)
401    {
402        SetOwnerGUID(owner_guid);
403        need_save = true;
404    }
405
406    if(need_save)                                           // normal item changed state set not work at loading
407    {
408        std::ostringstream ss;
409        ss << "UPDATE item_instance SET data = '";
410        for(uint16 i = 0; i < m_valuesCount; i++ )
411            ss << GetUInt32Value(i) << " ";
412        ss << "', owner_guid = '" << GUID_LOPART(GetOwnerGUID()) << "' WHERE guid = '" << guid << "'";
413
414        CharacterDatabase.Execute( ss.str().c_str() );
415    }
416
417    return true;
418}
419
420void Item::DeleteFromDB()
421{
422    CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid = '%u'",GetGUIDLow());
423}
424
425void Item::DeleteFromInventoryDB()
426{
427    CharacterDatabase.PExecute("DELETE FROM character_inventory WHERE item = '%u'",GetGUIDLow());
428}
429
430ItemPrototype const *Item::GetProto() const
431{
432    return objmgr.GetItemPrototype(GetUInt32Value(OBJECT_FIELD_ENTRY));
433}
434
435Player* Item::GetOwner()const
436{
437    return objmgr.GetPlayer(GetOwnerGUID());
438}
439
440uint32 Item::GetSkill()
441{
442    const static uint32 item_weapon_skills[MAX_ITEM_SUBCLASS_WEAPON] =
443    {
444        SKILL_AXES,     SKILL_2H_AXES,  SKILL_BOWS,          SKILL_GUNS,      SKILL_MACES,
445        SKILL_2H_MACES, SKILL_POLEARMS, SKILL_SWORDS,        SKILL_2H_SWORDS, 0,
446        SKILL_STAVES,   0,              0,                   SKILL_UNARMED,   0,
447        SKILL_DAGGERS,  SKILL_THROWN,   SKILL_ASSASSINATION, SKILL_CROSSBOWS, SKILL_WANDS,
448        SKILL_FISHING
449    };
450
451    const static uint32 item_armor_skills[MAX_ITEM_SUBCLASS_ARMOR] =
452    {
453        0,SKILL_CLOTH,SKILL_LEATHER,SKILL_MAIL,SKILL_PLATE_MAIL,0,SKILL_SHIELD,0,0,0
454    };
455
456    ItemPrototype const* proto = GetProto();
457
458    switch (proto->Class)
459    {
460        case ITEM_CLASS_WEAPON:
461            if( proto->SubClass >= MAX_ITEM_SUBCLASS_WEAPON )
462                return 0;
463            else
464                return item_weapon_skills[proto->SubClass];
465
466        case ITEM_CLASS_ARMOR:
467            if( proto->SubClass >= MAX_ITEM_SUBCLASS_ARMOR )
468                return 0;
469            else
470                return item_armor_skills[proto->SubClass];
471
472        default:
473            return 0;
474    }
475}
476
477uint32 Item::GetSpell()
478{
479    ItemPrototype const* proto = GetProto();
480
481    switch (proto->Class)
482    {
483        case ITEM_CLASS_WEAPON:
484            switch (proto->SubClass)
485            {
486                case ITEM_SUBCLASS_WEAPON_AXE:     return  196;
487                case ITEM_SUBCLASS_WEAPON_AXE2:    return  197;
488                case ITEM_SUBCLASS_WEAPON_BOW:     return  264;
489                case ITEM_SUBCLASS_WEAPON_GUN:     return  266;
490                case ITEM_SUBCLASS_WEAPON_MACE:    return  198;
491                case ITEM_SUBCLASS_WEAPON_MACE2:   return  199;
492                case ITEM_SUBCLASS_WEAPON_POLEARM: return  200;
493                case ITEM_SUBCLASS_WEAPON_SWORD:   return  201;
494                case ITEM_SUBCLASS_WEAPON_SWORD2:  return  202;
495                case ITEM_SUBCLASS_WEAPON_STAFF:   return  227;
496                case ITEM_SUBCLASS_WEAPON_DAGGER:  return 1180;
497                case ITEM_SUBCLASS_WEAPON_THROWN:  return 2567;
498                case ITEM_SUBCLASS_WEAPON_SPEAR:   return 3386;
499                case ITEM_SUBCLASS_WEAPON_CROSSBOW:return 5011;
500                case ITEM_SUBCLASS_WEAPON_WAND:    return 5009;
501                default: return 0;
502            }
503        case ITEM_CLASS_ARMOR:
504            switch(proto->SubClass)
505            {
506                case ITEM_SUBCLASS_ARMOR_CLOTH:    return 9078;
507                case ITEM_SUBCLASS_ARMOR_LEATHER:  return 9077;
508                case ITEM_SUBCLASS_ARMOR_MAIL:     return 8737;
509                case ITEM_SUBCLASS_ARMOR_PLATE:    return  750;
510                case ITEM_SUBCLASS_ARMOR_SHIELD:   return 9116;
511                default: return 0;
512            }
513    }
514    return 0;
515}
516
517int32 Item::GenerateItemRandomPropertyId(uint32 item_id)
518{
519    ItemPrototype const *itemProto = sItemStorage.LookupEntry<ItemPrototype>(item_id);
520
521    if(!itemProto)
522        return 0;
523
524    // item must have one from this field values not null if it can have random enchantments
525    if((!itemProto->RandomProperty) && (!itemProto->RandomSuffix))
526        return 0;
527
528    // item can have not null only one from field values
529    if((itemProto->RandomProperty) && (itemProto->RandomSuffix))
530    {
531        sLog.outErrorDb("Item template %u have RandomProperty==%u and RandomSuffix==%u, but must have one from field =0",itemProto->ItemId,itemProto->RandomProperty,itemProto->RandomSuffix);
532        return 0;
533    }
534
535    // RandomProperty case
536    if(itemProto->RandomProperty)
537    {
538        uint32 randomPropId = GetItemEnchantMod(itemProto->RandomProperty);
539        ItemRandomPropertiesEntry const *random_id = sItemRandomPropertiesStore.LookupEntry(randomPropId);
540        if(!random_id)
541        {
542            sLog.outErrorDb("Enchantment id #%u used but it doesn't have records in 'ItemRandomProperties.dbc'",randomPropId);
543            return 0;
544        }
545
546        return random_id->ID;
547    }
548    // RandomSuffix case
549    else
550    {
551        uint32 randomPropId = GetItemEnchantMod(itemProto->RandomSuffix);
552        ItemRandomSuffixEntry const *random_id = sItemRandomSuffixStore.LookupEntry(randomPropId);
553        if(!random_id)
554        {
555            sLog.outErrorDb("Enchantment id #%u used but it doesn't have records in sItemRandomSuffixStore.",randomPropId);
556            return 0;
557        }
558
559        return -int32(random_id->ID);
560    }
561}
562
563void Item::SetItemRandomProperties(int32 randomPropId)
564{
565    if(!randomPropId)
566        return;
567
568    if(randomPropId > 0)
569    {
570        ItemRandomPropertiesEntry const *item_rand = sItemRandomPropertiesStore.LookupEntry(randomPropId);
571        if(item_rand)
572        {
573            if(GetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID) != int32(item_rand->ID))
574            {
575                SetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID,item_rand->ID);
576                SetState(ITEM_CHANGED);
577            }
578            for(uint32 i = PROP_ENCHANTMENT_SLOT_2; i < PROP_ENCHANTMENT_SLOT_2 + 3; ++i)
579                SetEnchantment(EnchantmentSlot(i),item_rand->enchant_id[i - PROP_ENCHANTMENT_SLOT_2],0,0);
580        }
581    }
582    else
583    {
584        ItemRandomSuffixEntry const *item_rand = sItemRandomSuffixStore.LookupEntry(-randomPropId);
585        if(item_rand)
586        {
587            if( GetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID) != -int32(item_rand->ID) ||
588                !GetItemSuffixFactor())
589            {
590                SetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID,-int32(item_rand->ID));
591                UpdateItemSuffixFactor();
592                SetState(ITEM_CHANGED);
593            }
594
595            for(uint32 i = PROP_ENCHANTMENT_SLOT_0; i < PROP_ENCHANTMENT_SLOT_0 + 3; ++i)
596                SetEnchantment(EnchantmentSlot(i),item_rand->enchant_id[i - PROP_ENCHANTMENT_SLOT_0],0,0);
597        }
598    }
599}
600
601bool Item::UpdateItemSuffixFactor()
602{
603    uint32 suffixFactor = GenerateEnchSuffixFactor(GetEntry());
604    if(GetItemSuffixFactor()==suffixFactor)
605        return false;
606    SetUInt32Value(ITEM_FIELD_PROPERTY_SEED,suffixFactor);
607    return true;
608}
609
610void Item::SetState(ItemUpdateState state, Player *forplayer)
611{
612    if (uState == ITEM_NEW && state == ITEM_REMOVED)
613    {
614        // pretend the item never existed
615        RemoveFromUpdateQueueOf(forplayer);
616        delete this;
617        return;
618    }
619
620    if (state != ITEM_UNCHANGED)
621    {
622        // new items must stay in new state until saved
623        if (uState != ITEM_NEW) uState = state;
624        AddToUpdateQueueOf(forplayer);
625    }
626    else
627    {
628        // unset in queue
629        // the item must be removed from the queue manually
630        uQueuePos = -1;
631        uState = ITEM_UNCHANGED;
632    }
633}
634
635void Item::AddToUpdateQueueOf(Player *player)
636{
637    if (IsInUpdateQueue()) return;
638
639    if (!player)
640    {
641        player = GetOwner();
642        if (!player)
643        {
644            sLog.outError("Item::AddToUpdateQueueOf - GetPlayer didn't find a player matching owner's guid (%u)!", GUID_LOPART(GetOwnerGUID()));
645            return;
646        }
647    }
648
649    if (player->GetGUID() != GetOwnerGUID())
650    {
651        sLog.outError("Item::AddToUpdateQueueOf - Owner's guid (%u) and player's guid (%u) don't match!", GUID_LOPART(GetOwnerGUID()), player->GetGUIDLow());
652        return;
653    }
654
655    if (player->m_itemUpdateQueueBlocked) return;
656
657    player->m_itemUpdateQueue.push_back(this);
658    uQueuePos = player->m_itemUpdateQueue.size()-1;
659}
660
661void Item::RemoveFromUpdateQueueOf(Player *player)
662{
663    if (!IsInUpdateQueue()) return;
664
665    if (!player)
666    {
667        player = GetOwner();
668        if (!player)
669        {
670            sLog.outError("Item::RemoveFromUpdateQueueOf - GetPlayer didn't find a player matching owner's guid (%u)!", GUID_LOPART(GetOwnerGUID()));
671            return;
672        }
673    }
674
675    if (player->GetGUID() != GetOwnerGUID())
676    {
677        sLog.outError("Item::RemoveFromUpdateQueueOf - Owner's guid (%u) and player's guid (%u) don't match!", GUID_LOPART(GetOwnerGUID()), player->GetGUIDLow());
678        return;
679    }
680
681    if (player->m_itemUpdateQueueBlocked) return;
682
683    player->m_itemUpdateQueue[uQueuePos] = NULL;
684    uQueuePos = -1;
685}
686
687uint8 Item::GetBagSlot() const
688{
689    return m_container ? m_container->GetSlot() : uint8(INVENTORY_SLOT_BAG_0);
690}
691
692bool Item::IsEquipped() const
693{
694    return !IsInBag() && m_slot < EQUIPMENT_SLOT_END;
695}
696
697bool Item::CanBeTraded() const
698{
699    if(IsSoulBound())
700        return false;
701    if(IsBag() && (Player::IsBagPos(GetPos()) || !((Bag const*)this)->IsEmpty()) )
702        return false;
703
704    if(Player* owner = GetOwner())
705    {
706        if(owner->CanUnequipItem(GetPos(),false) !=  EQUIP_ERR_OK )
707            return false;
708        if(owner->GetLootGUID()==GetGUID())
709            return false;
710    }
711
712    if (IsBoundByEnchant())
713        return false;
714
715    return true;
716}
717
718bool Item::IsBoundByEnchant() const
719{
720    // Check all enchants for soulbound
721    for(uint32 enchant_slot = PERM_ENCHANTMENT_SLOT; enchant_slot < MAX_ENCHANTMENT_SLOT; ++enchant_slot)
722    {
723        uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
724        if(!enchant_id)
725            continue;
726
727        SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
728        if(!enchantEntry)
729            continue;
730
731        if(enchantEntry->slot & ENCHANTMENT_CAN_SOULBOUND)
732            return true;
733    }
734    return false;
735}
736
737bool Item::IsFitToSpellRequirements(SpellEntry const* spellInfo) const
738{
739    ItemPrototype const* proto = GetProto();
740
741    if (spellInfo->EquippedItemClass != -1)                 // -1 == any item class
742    {
743        if(spellInfo->EquippedItemClass != int32(proto->Class))
744            return false;                                   //  wrong item class
745
746        if(spellInfo->EquippedItemSubClassMask != 0)        // 0 == any subclass
747        {
748            if((spellInfo->EquippedItemSubClassMask & (1 << proto->SubClass)) == 0)
749                return false;                               // subclass not present in mask
750        }
751    }
752
753    if(spellInfo->EquippedItemInventoryTypeMask != 0)       // 0 == any inventory type
754    {
755        if((spellInfo->EquippedItemInventoryTypeMask  & (1 << proto->InventoryType)) == 0)
756            return false;                                   // inventory type not present in mask
757    }
758
759    return true;
760}
761
762void Item::SetEnchantment(EnchantmentSlot slot, uint32 id, uint32 duration, uint32 charges)
763{
764    // Better lost small time at check in comparison lost time at item save to DB.
765    if( GetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_ID_OFFSET)==id &&
766        GetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET)==duration &&
767        GetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_CHARGES_OFFSET)==charges )
768        return;
769
770    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_ID_OFFSET,id);
771    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET,duration);
772    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_CHARGES_OFFSET,charges);
773    SetState(ITEM_CHANGED);
774}
775
776void Item::SetEnchantmentDuration(EnchantmentSlot slot, uint32 duration)
777{
778    if(GetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET)==duration)
779        return;
780
781    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET,duration);
782    SetState(ITEM_CHANGED);
783}
784
785void Item::SetEnchantmentCharges(EnchantmentSlot slot, uint32 charges)
786{
787    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_CHARGES_OFFSET,charges);
788    SetState(ITEM_CHANGED);
789}
790
791void Item::ClearEnchantment(EnchantmentSlot slot)
792{
793    if(!GetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_ID_OFFSET))
794        return;
795
796    for(int x=0;x<3;x++)
797        SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + x,0);
798    SetState(ITEM_CHANGED);
799}
800
801bool Item::GemsFitSockets() const
802{
803    bool fits = true;
804    for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+3; ++enchant_slot)
805    {
806        uint8 SocketColor = GetProto()->Socket[enchant_slot-SOCK_ENCHANTMENT_SLOT].Color;
807
808        uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
809        if(!enchant_id)
810        {
811            if(SocketColor) fits &= false;
812            continue;
813        }
814
815        SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
816        if(!enchantEntry)
817        {
818            if(SocketColor) fits &= false;
819            continue;
820        }
821
822        uint8 GemColor = 0;
823
824        uint32 gemid = enchantEntry->GemID;
825        if(gemid)
826        {
827            ItemPrototype const* gemProto = sItemStorage.LookupEntry<ItemPrototype>(gemid);
828            if(gemProto)
829            {
830                GemPropertiesEntry const* gemProperty = sGemPropertiesStore.LookupEntry(gemProto->GemProperties);
831                if(gemProperty)
832                    GemColor = gemProperty->color;
833            }
834        }
835
836        fits &= (GemColor & SocketColor) ? true : false;
837    }
838    return fits;
839}
840
841uint8 Item::GetGemCountWithID(uint32 GemID) const
842{
843    uint8 count = 0;
844    for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+3; ++enchant_slot)
845    {
846        uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
847        if(!enchant_id)
848            continue;
849
850        SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
851        if(!enchantEntry)
852            continue;
853
854        if(GemID == enchantEntry->GemID)
855            ++count;
856    }
857    return count;
858}
859
860bool Item::IsLimitedToAnotherMapOrZone( uint32 cur_mapId, uint32 cur_zoneId) const
861{
862    ItemPrototype const* proto = GetProto();
863    return proto && (proto->Map && proto->Map != cur_mapId || proto->Area && proto->Area != cur_zoneId );
864}
865
866// Though the client has the information in the item's data field,
867// we have to send SMSG_ITEM_TIME_UPDATE to display the remaining
868// time.
869void Item::SendTimeUpdate(Player* owner)
870{
871    if (!GetUInt32Value(ITEM_FIELD_DURATION))
872        return;
873
874    WorldPacket data(SMSG_ITEM_TIME_UPDATE, (8+4));
875    data << (uint64)GetGUID();
876    data << (uint32)GetUInt32Value(ITEM_FIELD_DURATION);
877    owner->GetSession()->SendPacket(&data);
878}
879
880Item* Item::CreateItem( uint32 item, uint32 count, Player const* player )
881{
882    if ( count < 1 )
883        return NULL;                                        //don't create item at zero count
884
885    ItemPrototype const *pProto = objmgr.GetItemPrototype( item );
886    if( pProto )
887    {
888        if ( count > pProto->Stackable )
889            count = pProto->Stackable;
890
891        assert(count !=0 && "pProto->Stackable==0 but checked at loading already");
892
893        Item *pItem = NewItemOrBag( pProto );
894        if( pItem->Create(objmgr.GenerateLowGuid(HIGHGUID_ITEM), item, player) )
895        {
896            pItem->SetCount( count );
897            return pItem;
898        }
899        else
900            delete pItem;
901    }
902    return NULL;
903}
904
905Item* Item::CloneItem( uint32 count, Player const* player ) const
906{
907    Item* newItem = CreateItem( GetEntry(), count, player );
908    if(!newItem)
909        return NULL;
910
911    newItem->SetUInt32Value( ITEM_FIELD_CREATOR,      GetUInt32Value( ITEM_FIELD_CREATOR ) );
912    newItem->SetUInt32Value( ITEM_FIELD_GIFTCREATOR,  GetUInt32Value( ITEM_FIELD_GIFTCREATOR ) );
913    newItem->SetUInt32Value( ITEM_FIELD_FLAGS,        GetUInt32Value( ITEM_FIELD_FLAGS ) );
914    newItem->SetUInt32Value( ITEM_FIELD_DURATION,     GetUInt32Value( ITEM_FIELD_DURATION ) );
915    newItem->SetItemRandomProperties(GetItemRandomPropertyId());
916    return newItem;
917}
Note: See TracBrowser for help on using the browser.