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

Revision 260, 29.0 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 "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_SOUL_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    SetEntry(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(GetEntry());
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((GetEnchantmentId(slot) == id) && (GetEnchantmentDuration(slot) == duration) && (GetEnchantmentCharges(slot) == charges))
766        return;
767
768    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_ID_OFFSET,id);
769    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET,duration);
770    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_CHARGES_OFFSET,charges);
771    SetState(ITEM_CHANGED);
772}
773
774void Item::SetEnchantmentDuration(EnchantmentSlot slot, uint32 duration)
775{
776    if(GetEnchantmentDuration(slot) == duration)
777        return;
778
779    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET,duration);
780    SetState(ITEM_CHANGED);
781}
782
783void Item::SetEnchantmentCharges(EnchantmentSlot slot, uint32 charges)
784{
785    if(GetEnchantmentCharges(slot) == charges)
786        return;
787
788    SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_CHARGES_OFFSET,charges);
789    SetState(ITEM_CHANGED);
790}
791
792void Item::ClearEnchantment(EnchantmentSlot slot)
793{
794    if(!GetEnchantmentId(slot))
795        return;
796
797    for(uint8 x = 0; x < 3; ++x)
798        SetUInt32Value(ITEM_FIELD_ENCHANTMENT + slot*MAX_ENCHANTMENT_OFFSET + x, 0);
799    SetState(ITEM_CHANGED);
800}
801
802bool Item::GemsFitSockets() const
803{
804    bool fits = true;
805    for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+3; ++enchant_slot)
806    {
807        uint8 SocketColor = GetProto()->Socket[enchant_slot-SOCK_ENCHANTMENT_SLOT].Color;
808
809        uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
810        if(!enchant_id)
811        {
812            if(SocketColor) fits &= false;
813            continue;
814        }
815
816        SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
817        if(!enchantEntry)
818        {
819            if(SocketColor) fits &= false;
820            continue;
821        }
822
823        uint8 GemColor = 0;
824
825        uint32 gemid = enchantEntry->GemID;
826        if(gemid)
827        {
828            ItemPrototype const* gemProto = sItemStorage.LookupEntry<ItemPrototype>(gemid);
829            if(gemProto)
830            {
831                GemPropertiesEntry const* gemProperty = sGemPropertiesStore.LookupEntry(gemProto->GemProperties);
832                if(gemProperty)
833                    GemColor = gemProperty->color;
834            }
835        }
836
837        fits &= (GemColor & SocketColor) ? true : false;
838    }
839    return fits;
840}
841
842uint8 Item::GetGemCountWithID(uint32 GemID) const
843{
844    uint8 count = 0;
845    for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+3; ++enchant_slot)
846    {
847        uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
848        if(!enchant_id)
849            continue;
850
851        SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
852        if(!enchantEntry)
853            continue;
854
855        if(GemID == enchantEntry->GemID)
856            ++count;
857    }
858    return count;
859}
860
861bool Item::IsLimitedToAnotherMapOrZone( uint32 cur_mapId, uint32 cur_zoneId) const
862{
863    ItemPrototype const* proto = GetProto();
864    return proto && (proto->Map && proto->Map != cur_mapId || proto->Area && proto->Area != cur_zoneId );
865}
866
867// Though the client has the information in the item's data field,
868// we have to send SMSG_ITEM_TIME_UPDATE to display the remaining
869// time.
870void Item::SendTimeUpdate(Player* owner)
871{
872    if (!GetUInt32Value(ITEM_FIELD_DURATION))
873        return;
874
875    WorldPacket data(SMSG_ITEM_TIME_UPDATE, (8+4));
876    data << (uint64)GetGUID();
877    data << (uint32)GetUInt32Value(ITEM_FIELD_DURATION);
878    owner->GetSession()->SendPacket(&data);
879}
880
881Item* Item::CreateItem( uint32 item, uint32 count, Player const* player )
882{
883    if ( count < 1 )
884        return NULL;                                        //don't create item at zero count
885
886    ItemPrototype const *pProto = objmgr.GetItemPrototype( item );
887    if( pProto )
888    {
889        if ( count > pProto->Stackable )
890            count = pProto->Stackable;
891
892        assert(count !=0 && "pProto->Stackable==0 but checked at loading already");
893
894        Item *pItem = NewItemOrBag( pProto );
895        if( pItem->Create(objmgr.GenerateLowGuid(HIGHGUID_ITEM), item, player) )
896        {
897            pItem->SetCount( count );
898            return pItem;
899        }
900        else
901            delete pItem;
902    }
903    return NULL;
904}
905
906Item* Item::CloneItem( uint32 count, Player const* player ) const
907{
908    Item* newItem = CreateItem( GetEntry(), count, player );
909    if(!newItem)
910        return NULL;
911
912    newItem->SetUInt32Value( ITEM_FIELD_CREATOR,      GetUInt32Value( ITEM_FIELD_CREATOR ) );
913    newItem->SetUInt32Value( ITEM_FIELD_GIFTCREATOR,  GetUInt32Value( ITEM_FIELD_GIFTCREATOR ) );
914    newItem->SetUInt32Value( ITEM_FIELD_FLAGS,        GetUInt32Value( ITEM_FIELD_FLAGS ) );
915    newItem->SetUInt32Value( ITEM_FIELD_DURATION,     GetUInt32Value( ITEM_FIELD_DURATION ) );
916    newItem->SetItemRandomProperties(GetItemRandomPropertyId());
917    return newItem;
918}
Note: See TracBrowser for help on using the browser.