[9636] Move item real-time/in-game duration counting flag to new extraflags field.
[getmangos.git] / src / game / Item.cpp
blob94f344cf9bd176b314fccd9f702f403d38a737db
1 /*
2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Item.h"
21 #include "ObjectMgr.h"
22 #include "ObjectGuid.h"
23 #include "WorldPacket.h"
24 #include "Database/DatabaseEnv.h"
25 #include "ItemEnchantmentMgr.h"
27 void AddItemsSetItem(Player*player,Item *item)
29 ItemPrototype const *proto = item->GetProto();
30 uint32 setid = proto->ItemSet;
32 ItemSetEntry const *set = sItemSetStore.LookupEntry(setid);
34 if(!set)
36 sLog.outErrorDb("Item set %u for item (id %u) not found, mods not applied.",setid,proto->ItemId);
37 return;
40 if( set->required_skill_id && player->GetSkillValue(set->required_skill_id) < set->required_skill_value )
41 return;
43 ItemSetEffect *eff = NULL;
45 for(size_t x = 0; x < player->ItemSetEff.size(); ++x)
47 if(player->ItemSetEff[x] && player->ItemSetEff[x]->setid == setid)
49 eff = player->ItemSetEff[x];
50 break;
54 if(!eff)
56 eff = new ItemSetEffect;
57 memset(eff,0,sizeof(ItemSetEffect));
58 eff->setid = setid;
60 size_t x = 0;
61 for(; x < player->ItemSetEff.size(); x++)
62 if(!player->ItemSetEff[x])
63 break;
65 if(x < player->ItemSetEff.size())
66 player->ItemSetEff[x]=eff;
67 else
68 player->ItemSetEff.push_back(eff);
71 ++eff->item_count;
73 for(uint32 x=0;x<8;x++)
75 if(!set->spells [x])
76 continue;
77 //not enough for spell
78 if(set->items_to_triggerspell[x] > eff->item_count)
79 continue;
81 uint32 z=0;
82 for(;z<8;z++)
83 if(eff->spells[z] && eff->spells[z]->Id==set->spells[x])
84 break;
86 if(z < 8)
87 continue;
89 //new spell
90 for(uint32 y=0;y<8;y++)
92 if(!eff->spells[y]) // free slot
94 SpellEntry const *spellInfo = sSpellStore.LookupEntry(set->spells[x]);
95 if(!spellInfo)
97 sLog.outError("WORLD: unknown spell id %u in items set %u effects", set->spells[x],setid);
98 break;
101 // spell casted only if fit form requirement, in other case will casted at form change
102 player->ApplyEquipSpell(spellInfo,NULL,true);
103 eff->spells[y] = spellInfo;
104 break;
110 void RemoveItemsSetItem(Player*player,ItemPrototype const *proto)
112 uint32 setid = proto->ItemSet;
114 ItemSetEntry const *set = sItemSetStore.LookupEntry(setid);
116 if(!set)
118 sLog.outErrorDb("Item set #%u for item #%u not found, mods not removed.",setid,proto->ItemId);
119 return;
122 ItemSetEffect *eff = NULL;
123 size_t setindex = 0;
124 for(;setindex < player->ItemSetEff.size(); setindex++)
126 if(player->ItemSetEff[setindex] && player->ItemSetEff[setindex]->setid == setid)
128 eff = player->ItemSetEff[setindex];
129 break;
133 // can be in case now enough skill requirement for set appling but set has been appliend when skill requirement not enough
134 if(!eff)
135 return;
137 --eff->item_count;
139 for(uint32 x=0;x<8;x++)
141 if(!set->spells[x])
142 continue;
144 // enough for spell
145 if(set->items_to_triggerspell[x] <= eff->item_count)
146 continue;
148 for(uint32 z=0;z<8;z++)
150 if(eff->spells[z] && eff->spells[z]->Id==set->spells[x])
152 // spell can be not active if not fit form requirement
153 player->ApplyEquipSpell(eff->spells[z],NULL,false);
154 eff->spells[z]=NULL;
155 break;
160 if(!eff->item_count) //all items of a set were removed
162 assert(eff == player->ItemSetEff[setindex]);
163 delete eff;
164 player->ItemSetEff[setindex] = NULL;
168 bool ItemCanGoIntoBag(ItemPrototype const *pProto, ItemPrototype const *pBagProto)
170 if(!pProto || !pBagProto)
171 return false;
173 switch(pBagProto->Class)
175 case ITEM_CLASS_CONTAINER:
176 switch(pBagProto->SubClass)
178 case ITEM_SUBCLASS_CONTAINER:
179 return true;
180 case ITEM_SUBCLASS_SOUL_CONTAINER:
181 if(!(pProto->BagFamily & BAG_FAMILY_MASK_SOUL_SHARDS))
182 return false;
183 return true;
184 case ITEM_SUBCLASS_HERB_CONTAINER:
185 if(!(pProto->BagFamily & BAG_FAMILY_MASK_HERBS))
186 return false;
187 return true;
188 case ITEM_SUBCLASS_ENCHANTING_CONTAINER:
189 if(!(pProto->BagFamily & BAG_FAMILY_MASK_ENCHANTING_SUPP))
190 return false;
191 return true;
192 case ITEM_SUBCLASS_MINING_CONTAINER:
193 if(!(pProto->BagFamily & BAG_FAMILY_MASK_MINING_SUPP))
194 return false;
195 return true;
196 case ITEM_SUBCLASS_ENGINEERING_CONTAINER:
197 if(!(pProto->BagFamily & BAG_FAMILY_MASK_ENGINEERING_SUPP))
198 return false;
199 return true;
200 case ITEM_SUBCLASS_GEM_CONTAINER:
201 if(!(pProto->BagFamily & BAG_FAMILY_MASK_GEMS))
202 return false;
203 return true;
204 case ITEM_SUBCLASS_LEATHERWORKING_CONTAINER:
205 if(!(pProto->BagFamily & BAG_FAMILY_MASK_LEATHERWORKING_SUPP))
206 return false;
207 return true;
208 case ITEM_SUBCLASS_INSCRIPTION_CONTAINER:
209 if(!(pProto->BagFamily & BAG_FAMILY_MASK_INSCRIPTION_SUPP))
210 return false;
211 return true;
212 default:
213 return false;
215 case ITEM_CLASS_QUIVER:
216 switch(pBagProto->SubClass)
218 case ITEM_SUBCLASS_QUIVER:
219 if(!(pProto->BagFamily & BAG_FAMILY_MASK_ARROWS))
220 return false;
221 return true;
222 case ITEM_SUBCLASS_AMMO_POUCH:
223 if(!(pProto->BagFamily & BAG_FAMILY_MASK_BULLETS))
224 return false;
225 return true;
226 default:
227 return false;
230 return false;
233 Item::Item( )
235 m_objectType |= TYPEMASK_ITEM;
236 m_objectTypeId = TYPEID_ITEM;
238 m_updateFlag = UPDATEFLAG_HIGHGUID;
240 m_valuesCount = ITEM_END;
241 m_slot = 0;
242 uState = ITEM_NEW;
243 uQueuePos = -1;
244 m_container = NULL;
245 m_lootGenerated = false;
246 mb_in_trade = false;
249 bool Item::Create( uint32 guidlow, uint32 itemid, Player const* owner)
251 Object::_Create( guidlow, 0, HIGHGUID_ITEM );
253 SetEntry(itemid);
254 SetFloatValue(OBJECT_FIELD_SCALE_X, 1.0f);
256 SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0);
257 SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0);
259 ItemPrototype const *itemProto = ObjectMgr::GetItemPrototype(itemid);
260 if(!itemProto)
261 return false;
263 SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1);
264 SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability);
265 SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability);
267 for(int i = 0; i < MAX_ITEM_PROTO_SPELLS; ++i)
268 SetSpellCharges(i,itemProto->Spells[i].SpellCharges);
270 SetUInt32Value(ITEM_FIELD_FLAGS, itemProto->Flags);
271 SetUInt32Value(ITEM_FIELD_DURATION, itemProto->Duration);
273 return true;
276 void Item::UpdateDuration(Player* owner, uint32 diff)
278 if (!GetUInt32Value(ITEM_FIELD_DURATION))
279 return;
281 sLog.outDebug("Item::UpdateDuration Item (Entry: %u Duration %u Diff %u)",GetEntry(),GetUInt32Value(ITEM_FIELD_DURATION),diff);
283 if (GetUInt32Value(ITEM_FIELD_DURATION)<=diff)
285 owner->DestroyItem(GetBagSlot(), GetSlot(), true);
286 return;
289 SetUInt32Value(ITEM_FIELD_DURATION, GetUInt32Value(ITEM_FIELD_DURATION) - diff);
290 SetState(ITEM_CHANGED, owner); // save new time in database
293 void Item::SaveToDB()
295 uint32 guid = GetGUIDLow();
296 switch (uState)
298 case ITEM_NEW:
300 CharacterDatabase.PExecute( "DELETE FROM item_instance WHERE guid = '%u'", guid );
301 std::ostringstream ss;
302 ss << "INSERT INTO item_instance (guid,owner_guid,data) VALUES (" << guid << "," << GUID_LOPART(GetOwnerGUID()) << ",'";
303 for(uint16 i = 0; i < m_valuesCount; ++i )
304 ss << GetUInt32Value(i) << " ";
305 ss << "' )";
306 CharacterDatabase.Execute( ss.str().c_str() );
307 } break;
308 case ITEM_CHANGED:
310 std::ostringstream ss;
311 ss << "UPDATE item_instance SET data = '";
312 for(uint16 i = 0; i < m_valuesCount; ++i )
313 ss << GetUInt32Value(i) << " ";
314 ss << "', owner_guid = '" << GUID_LOPART(GetOwnerGUID()) << "' WHERE guid = '" << guid << "'";
316 CharacterDatabase.Execute( ss.str().c_str() );
318 if(HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
319 CharacterDatabase.PExecute("UPDATE character_gifts SET guid = '%u' WHERE item_guid = '%u'", GUID_LOPART(GetOwnerGUID()),GetGUIDLow());
320 } break;
321 case ITEM_REMOVED:
323 if (GetUInt32Value(ITEM_FIELD_ITEM_TEXT_ID) > 0 )
324 CharacterDatabase.PExecute("DELETE FROM item_text WHERE id = '%u'", GetUInt32Value(ITEM_FIELD_ITEM_TEXT_ID));
325 CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid = '%u'", guid);
326 if(HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
327 CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", GetGUIDLow());
328 delete this;
329 return;
331 case ITEM_UNCHANGED:
332 break;
334 SetState(ITEM_UNCHANGED);
337 bool Item::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result)
339 // create item before any checks for store correct guid
340 // and allow use "FSetState(ITEM_REMOVED); SaveToDB();" for deleting item from DB
341 Object::_Create(guid, 0, HIGHGUID_ITEM);
343 bool delete_result = false;
344 if(!result)
346 result = CharacterDatabase.PQuery("SELECT data FROM item_instance WHERE guid = '%u'", guid);
347 delete_result = true;
350 if (!result)
352 sLog.outError("Item (GUID: %u owner: %u) not found in table `item_instance`, can't load. ",guid,GUID_LOPART(owner_guid));
353 return false;
356 Field *fields = result->Fetch();
358 if(!LoadValues(fields[0].GetString()))
360 sLog.outError("Item #%d have broken data in `data` field. Can't be loaded.",guid);
361 if (delete_result) delete result;
362 return false;
365 bool need_save = false; // need explicit save data at load fixes
367 // overwrite possible wrong/corrupted guid
368 uint64 new_item_guid = MAKE_NEW_GUID(guid,0, HIGHGUID_ITEM);
369 if(GetUInt64Value(OBJECT_FIELD_GUID) != new_item_guid)
371 SetUInt64Value(OBJECT_FIELD_GUID, MAKE_NEW_GUID(guid,0, HIGHGUID_ITEM));
372 need_save = true;
375 if (delete_result) delete result;
377 ItemPrototype const* proto = GetProto();
378 if(!proto)
379 return false;
381 // update max durability (and durability) if need
382 if(proto->MaxDurability!= GetUInt32Value(ITEM_FIELD_MAXDURABILITY))
384 SetUInt32Value(ITEM_FIELD_MAXDURABILITY,proto->MaxDurability);
385 if(GetUInt32Value(ITEM_FIELD_DURABILITY) > proto->MaxDurability)
386 SetUInt32Value(ITEM_FIELD_DURABILITY,proto->MaxDurability);
388 need_save = true;
391 // recalculate suffix factor
392 if(GetItemRandomPropertyId() < 0)
394 if(UpdateItemSuffixFactor())
395 need_save = true;
398 // Remove bind flag for items vs NO_BIND set
399 if (IsSoulBound() && proto->Bonding == NO_BIND)
401 ApplyModFlag(ITEM_FIELD_FLAGS,ITEM_FLAGS_BINDED, false);
402 need_save = true;
405 // update duration if need, and remove if not need
406 if ((proto->Duration == 0) != (GetUInt32Value(ITEM_FIELD_DURATION) == 0))
408 SetUInt32Value(ITEM_FIELD_DURATION, proto->Duration);
409 need_save = true;
412 // set correct owner
413 if (owner_guid != 0 && GetOwnerGUID() != owner_guid)
415 SetOwnerGUID(owner_guid);
416 need_save = true;
419 if (need_save) // normal item changed state set not work at loading
421 std::ostringstream ss;
422 ss << "UPDATE item_instance SET data = '";
423 for(uint16 i = 0; i < m_valuesCount; ++i )
424 ss << GetUInt32Value(i) << " ";
425 ss << "', owner_guid = '" << GUID_LOPART(GetOwnerGUID()) << "' WHERE guid = '" << guid << "'";
427 CharacterDatabase.Execute( ss.str().c_str() );
430 return true;
433 void Item::DeleteFromDB()
435 CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid = '%u'",GetGUIDLow());
438 void Item::DeleteFromInventoryDB()
440 CharacterDatabase.PExecute("DELETE FROM character_inventory WHERE item = '%u'",GetGUIDLow());
443 ItemPrototype const *Item::GetProto() const
445 return ObjectMgr::GetItemPrototype(GetEntry());
448 Player* Item::GetOwner()const
450 return sObjectMgr.GetPlayer(GetOwnerGUID());
453 uint32 Item::GetSkill()
455 const static uint32 item_weapon_skills[MAX_ITEM_SUBCLASS_WEAPON] =
457 SKILL_AXES, SKILL_2H_AXES, SKILL_BOWS, SKILL_GUNS, SKILL_MACES,
458 SKILL_2H_MACES, SKILL_POLEARMS, SKILL_SWORDS, SKILL_2H_SWORDS, 0,
459 SKILL_STAVES, 0, 0, SKILL_UNARMED, 0,
460 SKILL_DAGGERS, SKILL_THROWN, SKILL_ASSASSINATION, SKILL_CROSSBOWS, SKILL_WANDS,
461 SKILL_FISHING
464 const static uint32 item_armor_skills[MAX_ITEM_SUBCLASS_ARMOR] =
466 0,SKILL_CLOTH,SKILL_LEATHER,SKILL_MAIL,SKILL_PLATE_MAIL,0,SKILL_SHIELD,0,0,0,0
469 ItemPrototype const* proto = GetProto();
471 switch (proto->Class)
473 case ITEM_CLASS_WEAPON:
474 if( proto->SubClass >= MAX_ITEM_SUBCLASS_WEAPON )
475 return 0;
476 else
477 return item_weapon_skills[proto->SubClass];
479 case ITEM_CLASS_ARMOR:
480 if( proto->SubClass >= MAX_ITEM_SUBCLASS_ARMOR )
481 return 0;
482 else
483 return item_armor_skills[proto->SubClass];
485 default:
486 return 0;
490 uint32 Item::GetSpell()
492 ItemPrototype const* proto = GetProto();
494 switch (proto->Class)
496 case ITEM_CLASS_WEAPON:
497 switch (proto->SubClass)
499 case ITEM_SUBCLASS_WEAPON_AXE: return 196;
500 case ITEM_SUBCLASS_WEAPON_AXE2: return 197;
501 case ITEM_SUBCLASS_WEAPON_BOW: return 264;
502 case ITEM_SUBCLASS_WEAPON_GUN: return 266;
503 case ITEM_SUBCLASS_WEAPON_MACE: return 198;
504 case ITEM_SUBCLASS_WEAPON_MACE2: return 199;
505 case ITEM_SUBCLASS_WEAPON_POLEARM: return 200;
506 case ITEM_SUBCLASS_WEAPON_SWORD: return 201;
507 case ITEM_SUBCLASS_WEAPON_SWORD2: return 202;
508 case ITEM_SUBCLASS_WEAPON_STAFF: return 227;
509 case ITEM_SUBCLASS_WEAPON_DAGGER: return 1180;
510 case ITEM_SUBCLASS_WEAPON_THROWN: return 2567;
511 case ITEM_SUBCLASS_WEAPON_SPEAR: return 3386;
512 case ITEM_SUBCLASS_WEAPON_CROSSBOW:return 5011;
513 case ITEM_SUBCLASS_WEAPON_WAND: return 5009;
514 default: return 0;
516 case ITEM_CLASS_ARMOR:
517 switch(proto->SubClass)
519 case ITEM_SUBCLASS_ARMOR_CLOTH: return 9078;
520 case ITEM_SUBCLASS_ARMOR_LEATHER: return 9077;
521 case ITEM_SUBCLASS_ARMOR_MAIL: return 8737;
522 case ITEM_SUBCLASS_ARMOR_PLATE: return 750;
523 case ITEM_SUBCLASS_ARMOR_SHIELD: return 9116;
524 default: return 0;
527 return 0;
530 int32 Item::GenerateItemRandomPropertyId(uint32 item_id)
532 ItemPrototype const *itemProto = sItemStorage.LookupEntry<ItemPrototype>(item_id);
534 if(!itemProto)
535 return 0;
537 // item must have one from this field values not null if it can have random enchantments
538 if((!itemProto->RandomProperty) && (!itemProto->RandomSuffix))
539 return 0;
541 // item can have not null only one from field values
542 if((itemProto->RandomProperty) && (itemProto->RandomSuffix))
544 sLog.outErrorDb("Item template %u have RandomProperty==%u and RandomSuffix==%u, but must have one from field =0",itemProto->ItemId,itemProto->RandomProperty,itemProto->RandomSuffix);
545 return 0;
548 // RandomProperty case
549 if(itemProto->RandomProperty)
551 uint32 randomPropId = GetItemEnchantMod(itemProto->RandomProperty);
552 ItemRandomPropertiesEntry const *random_id = sItemRandomPropertiesStore.LookupEntry(randomPropId);
553 if(!random_id)
555 sLog.outErrorDb("Enchantment id #%u used but it doesn't have records in 'ItemRandomProperties.dbc'",randomPropId);
556 return 0;
559 return random_id->ID;
561 // RandomSuffix case
562 else
564 uint32 randomPropId = GetItemEnchantMod(itemProto->RandomSuffix);
565 ItemRandomSuffixEntry const *random_id = sItemRandomSuffixStore.LookupEntry(randomPropId);
566 if(!random_id)
568 sLog.outErrorDb("Enchantment id #%u used but it doesn't have records in sItemRandomSuffixStore.",randomPropId);
569 return 0;
572 return -int32(random_id->ID);
576 void Item::SetItemRandomProperties(int32 randomPropId)
578 if(!randomPropId)
579 return;
581 if(randomPropId > 0)
583 ItemRandomPropertiesEntry const *item_rand = sItemRandomPropertiesStore.LookupEntry(randomPropId);
584 if(item_rand)
586 if(GetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID) != int32(item_rand->ID))
588 SetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID,item_rand->ID);
589 SetState(ITEM_CHANGED);
591 for(uint32 i = PROP_ENCHANTMENT_SLOT_2; i < PROP_ENCHANTMENT_SLOT_2 + 3; ++i)
592 SetEnchantment(EnchantmentSlot(i),item_rand->enchant_id[i - PROP_ENCHANTMENT_SLOT_2],0,0);
595 else
597 ItemRandomSuffixEntry const *item_rand = sItemRandomSuffixStore.LookupEntry(-randomPropId);
598 if(item_rand)
600 if( GetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID) != -int32(item_rand->ID) ||
601 !GetItemSuffixFactor())
603 SetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID,-int32(item_rand->ID));
604 UpdateItemSuffixFactor();
605 SetState(ITEM_CHANGED);
608 for(uint32 i = PROP_ENCHANTMENT_SLOT_0; i < PROP_ENCHANTMENT_SLOT_0 + 3; ++i)
609 SetEnchantment(EnchantmentSlot(i),item_rand->enchant_id[i - PROP_ENCHANTMENT_SLOT_0],0,0);
614 bool Item::UpdateItemSuffixFactor()
616 uint32 suffixFactor = GenerateEnchSuffixFactor(GetEntry());
617 if(GetItemSuffixFactor()==suffixFactor)
618 return false;
619 SetUInt32Value(ITEM_FIELD_PROPERTY_SEED,suffixFactor);
620 return true;
623 void Item::SetState(ItemUpdateState state, Player *forplayer)
625 if (uState == ITEM_NEW && state == ITEM_REMOVED)
627 // pretend the item never existed
628 RemoveFromUpdateQueueOf(forplayer);
629 delete this;
630 return;
633 if (state != ITEM_UNCHANGED)
635 // new items must stay in new state until saved
636 if (uState != ITEM_NEW) uState = state;
637 AddToUpdateQueueOf(forplayer);
639 else
641 // unset in queue
642 // the item must be removed from the queue manually
643 uQueuePos = -1;
644 uState = ITEM_UNCHANGED;
648 void Item::AddToUpdateQueueOf(Player *player)
650 if (IsInUpdateQueue()) return;
652 if (!player)
654 player = GetOwner();
655 if (!player)
657 sLog.outError("Item::AddToUpdateQueueOf - GetPlayer didn't find a player matching owner's guid (%u)!", GUID_LOPART(GetOwnerGUID()));
658 return;
662 if (player->GetGUID() != GetOwnerGUID())
664 sLog.outError("Item::AddToUpdateQueueOf - Owner's guid (%u) and player's guid (%u) don't match!", GUID_LOPART(GetOwnerGUID()), player->GetGUIDLow());
665 return;
668 if (player->m_itemUpdateQueueBlocked) return;
670 player->m_itemUpdateQueue.push_back(this);
671 uQueuePos = player->m_itemUpdateQueue.size()-1;
674 void Item::RemoveFromUpdateQueueOf(Player *player)
676 if (!IsInUpdateQueue()) return;
678 if (!player)
680 player = GetOwner();
681 if (!player)
683 sLog.outError("Item::RemoveFromUpdateQueueOf - GetPlayer didn't find a player matching owner's guid (%u)!", GUID_LOPART(GetOwnerGUID()));
684 return;
688 if (player->GetGUID() != GetOwnerGUID())
690 sLog.outError("Item::RemoveFromUpdateQueueOf - Owner's guid (%u) and player's guid (%u) don't match!", GUID_LOPART(GetOwnerGUID()), player->GetGUIDLow());
691 return;
694 if (player->m_itemUpdateQueueBlocked) return;
696 player->m_itemUpdateQueue[uQueuePos] = NULL;
697 uQueuePos = -1;
700 uint8 Item::GetBagSlot() const
702 return m_container ? m_container->GetSlot() : uint8(INVENTORY_SLOT_BAG_0);
705 bool Item::IsEquipped() const
707 return !IsInBag() && m_slot < EQUIPMENT_SLOT_END;
710 bool Item::CanBeTraded(bool mail) const
712 if (m_lootGenerated)
713 return false;
715 if ((!mail || !IsBoundAccountWide()) && IsSoulBound())
716 return false;
718 if (IsBag() && (Player::IsBagPos(GetPos()) || !((Bag const*)this)->IsEmpty()) )
719 return false;
721 if (Player* owner = GetOwner())
723 if (owner->CanUnequipItem(GetPos(),false) != EQUIP_ERR_OK )
724 return false;
725 if (owner->GetLootGUID()==GetGUID())
726 return false;
729 if (IsBoundByEnchant())
730 return false;
732 return true;
735 bool Item::IsBoundByEnchant() const
737 // Check all enchants for soulbound
738 for(uint32 enchant_slot = PERM_ENCHANTMENT_SLOT; enchant_slot < MAX_ENCHANTMENT_SLOT; ++enchant_slot)
740 uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
741 if(!enchant_id)
742 continue;
744 SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
745 if(!enchantEntry)
746 continue;
748 if(enchantEntry->slot & ENCHANTMENT_CAN_SOULBOUND)
749 return true;
751 return false;
754 bool Item::IsFitToSpellRequirements(SpellEntry const* spellInfo) const
756 ItemPrototype const* proto = GetProto();
758 if (spellInfo->EquippedItemClass != -1) // -1 == any item class
760 if(spellInfo->EquippedItemClass != int32(proto->Class))
761 return false; // wrong item class
763 if(spellInfo->EquippedItemSubClassMask != 0) // 0 == any subclass
765 if((spellInfo->EquippedItemSubClassMask & (1 << proto->SubClass)) == 0)
766 return false; // subclass not present in mask
770 if(spellInfo->EquippedItemInventoryTypeMask != 0) // 0 == any inventory type
772 if((spellInfo->EquippedItemInventoryTypeMask & (1 << proto->InventoryType)) == 0)
773 return false; // inventory type not present in mask
776 return true;
779 bool Item::IsTargetValidForItemUse(Unit* pUnitTarget)
781 ItemRequiredTargetMapBounds bounds = sObjectMgr.GetItemRequiredTargetMapBounds(GetProto()->ItemId);
783 if (bounds.first == bounds.second)
784 return true;
786 if (!pUnitTarget)
787 return false;
789 for(ItemRequiredTargetMap::const_iterator itr = bounds.first; itr != bounds.second; ++itr)
790 if(itr->second.IsFitToRequirements(pUnitTarget))
791 return true;
793 return false;
796 void Item::SetEnchantment(EnchantmentSlot slot, uint32 id, uint32 duration, uint32 charges)
798 // Better lost small time at check in comparison lost time at item save to DB.
799 if((GetEnchantmentId(slot) == id) && (GetEnchantmentDuration(slot) == duration) && (GetEnchantmentCharges(slot) == charges))
800 return;
802 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_ID_OFFSET,id);
803 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET,duration);
804 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_CHARGES_OFFSET,charges);
805 SetState(ITEM_CHANGED);
808 void Item::SetEnchantmentDuration(EnchantmentSlot slot, uint32 duration)
810 if(GetEnchantmentDuration(slot) == duration)
811 return;
813 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET,duration);
814 SetState(ITEM_CHANGED);
817 void Item::SetEnchantmentCharges(EnchantmentSlot slot, uint32 charges)
819 if(GetEnchantmentCharges(slot) == charges)
820 return;
822 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_CHARGES_OFFSET,charges);
823 SetState(ITEM_CHANGED);
826 void Item::ClearEnchantment(EnchantmentSlot slot)
828 if(!GetEnchantmentId(slot))
829 return;
831 for(uint8 x = 0; x < 3; ++x)
832 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + x, 0);
833 SetState(ITEM_CHANGED);
836 bool Item::GemsFitSockets() const
838 bool fits = true;
839 for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+MAX_GEM_SOCKETS; ++enchant_slot)
841 uint8 SocketColor = GetProto()->Socket[enchant_slot-SOCK_ENCHANTMENT_SLOT].Color;
843 uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
844 if(!enchant_id)
846 if(SocketColor) fits &= false;
847 continue;
850 SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
851 if(!enchantEntry)
853 if(SocketColor) fits &= false;
854 continue;
857 uint8 GemColor = 0;
859 uint32 gemid = enchantEntry->GemID;
860 if(gemid)
862 ItemPrototype const* gemProto = sItemStorage.LookupEntry<ItemPrototype>(gemid);
863 if(gemProto)
865 GemPropertiesEntry const* gemProperty = sGemPropertiesStore.LookupEntry(gemProto->GemProperties);
866 if(gemProperty)
867 GemColor = gemProperty->color;
871 fits &= (GemColor & SocketColor) ? true : false;
873 return fits;
876 uint8 Item::GetGemCountWithID(uint32 GemID) const
878 uint8 count = 0;
879 for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+MAX_GEM_SOCKETS; ++enchant_slot)
881 uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
882 if(!enchant_id)
883 continue;
885 SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
886 if(!enchantEntry)
887 continue;
889 if(GemID == enchantEntry->GemID)
890 ++count;
892 return count;
895 uint8 Item::GetGemCountWithLimitCategory(uint32 limitCategory) const
897 uint8 count = 0;
898 for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+MAX_GEM_SOCKETS; ++enchant_slot)
900 uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
901 if(!enchant_id)
902 continue;
904 SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
905 if(!enchantEntry)
906 continue;
908 ItemPrototype const* gemProto = ObjectMgr::GetItemPrototype(enchantEntry->GemID);
909 if(!gemProto)
910 continue;
912 if(gemProto->ItemLimitCategory==limitCategory)
913 ++count;
915 return count;
918 bool Item::IsLimitedToAnotherMapOrZone( uint32 cur_mapId, uint32 cur_zoneId) const
920 ItemPrototype const* proto = GetProto();
921 return proto && (proto->Map && proto->Map != cur_mapId || proto->Area && proto->Area != cur_zoneId );
924 // Though the client has the information in the item's data field,
925 // we have to send SMSG_ITEM_TIME_UPDATE to display the remaining
926 // time.
927 void Item::SendTimeUpdate(Player* owner)
929 if (!GetUInt32Value(ITEM_FIELD_DURATION))
930 return;
932 WorldPacket data(SMSG_ITEM_TIME_UPDATE, (8+4));
933 data << (uint64)GetGUID();
934 data << (uint32)GetUInt32Value(ITEM_FIELD_DURATION);
935 owner->GetSession()->SendPacket(&data);
938 Item* Item::CreateItem( uint32 item, uint32 count, Player const* player )
940 if ( count < 1 )
941 return NULL; //don't create item at zero count
943 ItemPrototype const *pProto = ObjectMgr::GetItemPrototype( item );
944 if( pProto )
946 if ( count > pProto->GetMaxStackSize())
947 count = pProto->GetMaxStackSize();
949 assert(count !=0 && "pProto->Stackable==0 but checked at loading already");
951 Item *pItem = NewItemOrBag( pProto );
952 if( pItem->Create(sObjectMgr.GenerateLowGuid(HIGHGUID_ITEM), item, player) )
954 pItem->SetCount( count );
955 return pItem;
957 else
958 delete pItem;
960 return NULL;
963 Item* Item::CloneItem( uint32 count, Player const* player ) const
965 Item* newItem = CreateItem( GetEntry(), count, player );
966 if(!newItem)
967 return NULL;
969 newItem->SetUInt32Value( ITEM_FIELD_CREATOR, GetUInt32Value( ITEM_FIELD_CREATOR ) );
970 newItem->SetUInt32Value( ITEM_FIELD_GIFTCREATOR, GetUInt32Value( ITEM_FIELD_GIFTCREATOR ) );
971 newItem->SetUInt32Value( ITEM_FIELD_FLAGS, GetUInt32Value( ITEM_FIELD_FLAGS ) );
972 newItem->SetUInt32Value( ITEM_FIELD_DURATION, GetUInt32Value( ITEM_FIELD_DURATION ) );
973 newItem->SetItemRandomProperties(GetItemRandomPropertyId());
974 return newItem;
977 bool Item::IsBindedNotWith( Player const* player ) const
979 // not binded item
980 if(!IsSoulBound())
981 return false;
983 // own item
984 if(GetOwnerGUID()== player->GetGUID())
985 return false;
987 // not BOA item case
988 if(!IsBoundAccountWide())
989 return true;
991 // online
992 if(Player* owner = sObjectMgr.GetPlayer(GetOwnerGUID()))
994 return owner->GetSession()->GetAccountId() != player->GetSession()->GetAccountId();
996 // offline slow case
997 else
999 return sObjectMgr.GetPlayerAccountIdByGUID(GetOwnerGUID()) != player->GetSession()->GetAccountId();
1003 void Item::AddToClientUpdateList()
1005 if (Player* pl = GetOwner())
1006 pl->GetMap()->AddUpdateObject(this);
1009 void Item::RemoveFromClientUpdateList()
1011 if (Player* pl = GetOwner())
1012 pl->GetMap()->RemoveUpdateObject(this);
1015 void Item::BuildUpdateData(UpdateDataMapType& update_players)
1017 if (Player* pl = GetOwner())
1018 BuildUpdateDataForPlayer(pl, update_players);
1020 ClearUpdateMask(false);
1023 uint8 Item::CanBeMergedPartlyWith( ItemPrototype const* proto ) const
1025 // check item type
1026 if (GetEntry() != proto->ItemId)
1027 return EQUIP_ERR_ITEM_CANT_STACK;
1029 // check free space (full stacks can't be target of merge
1030 if (GetCount() >= proto->GetMaxStackSize())
1031 return EQUIP_ERR_ITEM_CANT_STACK;
1033 // not allow merge looting currently items
1034 if (m_lootGenerated)
1035 return EQUIP_ERR_ALREADY_LOOTED;
1037 return EQUIP_ERR_OK;
1040 bool ItemRequiredTarget::IsFitToRequirements( Unit* pUnitTarget ) const
1042 if(pUnitTarget->GetTypeId() != TYPEID_UNIT)
1043 return false;
1045 if(pUnitTarget->GetEntry() != m_uiTargetEntry)
1046 return false;
1048 switch(m_uiType)
1050 case ITEM_TARGET_TYPE_CREATURE:
1051 return pUnitTarget->isAlive();
1052 case ITEM_TARGET_TYPE_DEAD:
1053 return !pUnitTarget->isAlive();
1054 default:
1055 return false;
1059 bool Item::HasMaxCharges() const
1061 ItemPrototype const* itemProto = GetProto();
1063 for(int i = 0; i < MAX_ITEM_PROTO_SPELLS; ++i)
1064 if (GetSpellCharges(i) != itemProto->Spells[i].SpellCharges)
1065 return false;
1067 return true;
1070 void Item::RestoreCharges()
1072 ItemPrototype const* itemProto = GetProto();
1074 for(int i = 0; i < MAX_ITEM_PROTO_SPELLS; ++i)
1076 if (GetSpellCharges(i) != itemProto->Spells[i].SpellCharges)
1078 SetSpellCharges(i, itemProto->Spells[i].SpellCharges);
1079 SetState(ITEM_CHANGED);