[8449] Deprecate healing/damage item mods and merge internal data in to spell power.
[getmangos.git] / src / game / Item.cpp
blobeb755b0221d9d6d5adce84533f86ac0435f4e658
1 /*
2 * Copyright (C) 2005-2009 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 "WorldPacket.h"
23 #include "Database/DatabaseEnv.h"
24 #include "ItemEnchantmentMgr.h"
26 void AddItemsSetItem(Player*player,Item *item)
28 ItemPrototype const *proto = item->GetProto();
29 uint32 setid = proto->ItemSet;
31 ItemSetEntry const *set = sItemSetStore.LookupEntry(setid);
33 if(!set)
35 sLog.outErrorDb("Item set %u for item (id %u) not found, mods not applied.",setid,proto->ItemId);
36 return;
39 if( set->required_skill_id && player->GetSkillValue(set->required_skill_id) < set->required_skill_value )
40 return;
42 ItemSetEffect *eff = NULL;
44 for(size_t x = 0; x < player->ItemSetEff.size(); ++x)
46 if(player->ItemSetEff[x] && player->ItemSetEff[x]->setid == setid)
48 eff = player->ItemSetEff[x];
49 break;
53 if(!eff)
55 eff = new ItemSetEffect;
56 memset(eff,0,sizeof(ItemSetEffect));
57 eff->setid = setid;
59 size_t x = 0;
60 for(; x < player->ItemSetEff.size(); x++)
61 if(!player->ItemSetEff[x])
62 break;
64 if(x < player->ItemSetEff.size())
65 player->ItemSetEff[x]=eff;
66 else
67 player->ItemSetEff.push_back(eff);
70 ++eff->item_count;
72 for(uint32 x=0;x<8;x++)
74 if(!set->spells [x])
75 continue;
76 //not enough for spell
77 if(set->items_to_triggerspell[x] > eff->item_count)
78 continue;
80 uint32 z=0;
81 for(;z<8;z++)
82 if(eff->spells[z] && eff->spells[z]->Id==set->spells[x])
83 break;
85 if(z < 8)
86 continue;
88 //new spell
89 for(uint32 y=0;y<8;y++)
91 if(!eff->spells[y]) // free slot
93 SpellEntry const *spellInfo = sSpellStore.LookupEntry(set->spells[x]);
94 if(!spellInfo)
96 sLog.outError("WORLD: unknown spell id %u in items set %u effects", set->spells[x],setid);
97 break;
100 // spell casted only if fit form requirement, in other case will casted at form change
101 player->ApplyEquipSpell(spellInfo,NULL,true);
102 eff->spells[y] = spellInfo;
103 break;
109 void RemoveItemsSetItem(Player*player,ItemPrototype const *proto)
111 uint32 setid = proto->ItemSet;
113 ItemSetEntry const *set = sItemSetStore.LookupEntry(setid);
115 if(!set)
117 sLog.outErrorDb("Item set #%u for item #%u not found, mods not removed.",setid,proto->ItemId);
118 return;
121 ItemSetEffect *eff = NULL;
122 size_t setindex = 0;
123 for(;setindex < player->ItemSetEff.size(); setindex++)
125 if(player->ItemSetEff[setindex] && player->ItemSetEff[setindex]->setid == setid)
127 eff = player->ItemSetEff[setindex];
128 break;
132 // can be in case now enough skill requirement for set appling but set has been appliend when skill requirement not enough
133 if(!eff)
134 return;
136 --eff->item_count;
138 for(uint32 x=0;x<8;x++)
140 if(!set->spells[x])
141 continue;
143 // enough for spell
144 if(set->items_to_triggerspell[x] <= eff->item_count)
145 continue;
147 for(uint32 z=0;z<8;z++)
149 if(eff->spells[z] && eff->spells[z]->Id==set->spells[x])
151 // spell can be not active if not fit form requirement
152 player->ApplyEquipSpell(eff->spells[z],NULL,false);
153 eff->spells[z]=NULL;
154 break;
159 if(!eff->item_count) //all items of a set were removed
161 assert(eff == player->ItemSetEff[setindex]);
162 delete eff;
163 player->ItemSetEff[setindex] = NULL;
167 bool ItemCanGoIntoBag(ItemPrototype const *pProto, ItemPrototype const *pBagProto)
169 if(!pProto || !pBagProto)
170 return false;
172 switch(pBagProto->Class)
174 case ITEM_CLASS_CONTAINER:
175 switch(pBagProto->SubClass)
177 case ITEM_SUBCLASS_CONTAINER:
178 return true;
179 case ITEM_SUBCLASS_SOUL_CONTAINER:
180 if(!(pProto->BagFamily & BAG_FAMILY_MASK_SOUL_SHARDS))
181 return false;
182 return true;
183 case ITEM_SUBCLASS_HERB_CONTAINER:
184 if(!(pProto->BagFamily & BAG_FAMILY_MASK_HERBS))
185 return false;
186 return true;
187 case ITEM_SUBCLASS_ENCHANTING_CONTAINER:
188 if(!(pProto->BagFamily & BAG_FAMILY_MASK_ENCHANTING_SUPP))
189 return false;
190 return true;
191 case ITEM_SUBCLASS_MINING_CONTAINER:
192 if(!(pProto->BagFamily & BAG_FAMILY_MASK_MINING_SUPP))
193 return false;
194 return true;
195 case ITEM_SUBCLASS_ENGINEERING_CONTAINER:
196 if(!(pProto->BagFamily & BAG_FAMILY_MASK_ENGINEERING_SUPP))
197 return false;
198 return true;
199 case ITEM_SUBCLASS_GEM_CONTAINER:
200 if(!(pProto->BagFamily & BAG_FAMILY_MASK_GEMS))
201 return false;
202 return true;
203 case ITEM_SUBCLASS_LEATHERWORKING_CONTAINER:
204 if(!(pProto->BagFamily & BAG_FAMILY_MASK_LEATHERWORKING_SUPP))
205 return false;
206 return true;
207 case ITEM_SUBCLASS_INSCRIPTION_CONTAINER:
208 if(!(pProto->BagFamily & BAG_FAMILY_MASK_INSCRIPTION_SUPP))
209 return false;
210 return true;
211 default:
212 return false;
214 case ITEM_CLASS_QUIVER:
215 switch(pBagProto->SubClass)
217 case ITEM_SUBCLASS_QUIVER:
218 if(!(pProto->BagFamily & BAG_FAMILY_MASK_ARROWS))
219 return false;
220 return true;
221 case ITEM_SUBCLASS_AMMO_POUCH:
222 if(!(pProto->BagFamily & BAG_FAMILY_MASK_BULLETS))
223 return false;
224 return true;
225 default:
226 return false;
229 return false;
232 Item::Item( )
234 m_objectType |= TYPEMASK_ITEM;
235 m_objectTypeId = TYPEID_ITEM;
237 m_updateFlag = UPDATEFLAG_HIGHGUID;
239 m_valuesCount = ITEM_END;
240 m_slot = 0;
241 uState = ITEM_NEW;
242 uQueuePos = -1;
243 m_container = NULL;
244 m_lootGenerated = false;
245 mb_in_trade = false;
248 bool Item::Create( uint32 guidlow, uint32 itemid, Player const* owner)
250 Object::_Create( guidlow, 0, HIGHGUID_ITEM );
252 SetEntry(itemid);
253 SetFloatValue(OBJECT_FIELD_SCALE_X, 1.0f);
255 SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0);
256 SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0);
258 ItemPrototype const *itemProto = objmgr.GetItemPrototype(itemid);
259 if(!itemProto)
260 return false;
262 SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1);
263 SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability);
264 SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability);
266 for(int i = 0; i < MAX_ITEM_PROTO_SPELLS; ++i)
267 SetSpellCharges(i,itemProto->Spells[i].SpellCharges);
269 SetUInt32Value(ITEM_FIELD_FLAGS, itemProto->Flags);
270 SetUInt32Value(ITEM_FIELD_DURATION, abs(itemProto->Duration));
272 return true;
275 void Item::UpdateDuration(Player* owner, uint32 diff)
277 if (!GetUInt32Value(ITEM_FIELD_DURATION))
278 return;
280 sLog.outDebug("Item::UpdateDuration Item (Entry: %u Duration %u Diff %u)",GetEntry(),GetUInt32Value(ITEM_FIELD_DURATION),diff);
282 if (GetUInt32Value(ITEM_FIELD_DURATION)<=diff)
284 owner->DestroyItem(GetBagSlot(), GetSlot(), true);
285 return;
288 SetUInt32Value(ITEM_FIELD_DURATION, GetUInt32Value(ITEM_FIELD_DURATION) - diff);
289 SetState(ITEM_CHANGED, owner); // save new time in database
292 void Item::SaveToDB()
294 uint32 guid = GetGUIDLow();
295 switch (uState)
297 case ITEM_NEW:
299 CharacterDatabase.PExecute( "DELETE FROM item_instance WHERE guid = '%u'", guid );
300 std::ostringstream ss;
301 ss << "INSERT INTO item_instance (guid,owner_guid,data) VALUES (" << guid << "," << GUID_LOPART(GetOwnerGUID()) << ",'";
302 for(uint16 i = 0; i < m_valuesCount; ++i )
303 ss << GetUInt32Value(i) << " ";
304 ss << "' )";
305 CharacterDatabase.Execute( ss.str().c_str() );
306 } break;
307 case ITEM_CHANGED:
309 std::ostringstream ss;
310 ss << "UPDATE item_instance SET data = '";
311 for(uint16 i = 0; i < m_valuesCount; ++i )
312 ss << GetUInt32Value(i) << " ";
313 ss << "', owner_guid = '" << GUID_LOPART(GetOwnerGUID()) << "' WHERE guid = '" << guid << "'";
315 CharacterDatabase.Execute( ss.str().c_str() );
317 if(HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
318 CharacterDatabase.PExecute("UPDATE character_gifts SET guid = '%u' WHERE item_guid = '%u'", GUID_LOPART(GetOwnerGUID()),GetGUIDLow());
319 } break;
320 case ITEM_REMOVED:
322 if (GetUInt32Value(ITEM_FIELD_ITEM_TEXT_ID) > 0 )
323 CharacterDatabase.PExecute("DELETE FROM item_text WHERE id = '%u'", GetUInt32Value(ITEM_FIELD_ITEM_TEXT_ID));
324 CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid = '%u'", guid);
325 if(HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
326 CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", GetGUIDLow());
327 delete this;
328 return;
330 case ITEM_UNCHANGED:
331 break;
333 SetState(ITEM_UNCHANGED);
336 bool Item::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result)
338 // create item before any checks for store correct guid
339 // and allow use "FSetState(ITEM_REMOVED); SaveToDB();" for deleting item from DB
340 Object::_Create(guid, 0, HIGHGUID_ITEM);
342 bool delete_result = false;
343 if(!result)
345 result = CharacterDatabase.PQuery("SELECT data FROM item_instance WHERE guid = '%u'", guid);
346 delete_result = true;
349 if (!result)
351 sLog.outError("Item (GUID: %u owner: %u) not found in table `item_instance`, can't load. ",guid,GUID_LOPART(owner_guid));
352 return false;
355 Field *fields = result->Fetch();
357 if(!LoadValues(fields[0].GetString()))
359 sLog.outError("Item #%d have broken data in `data` field. Can't be loaded.",guid);
360 if (delete_result) delete result;
361 return false;
364 bool need_save = false; // need explicit save data at load fixes
366 // overwrite possible wrong/corrupted guid
367 uint64 new_item_guid = MAKE_NEW_GUID(guid,0, HIGHGUID_ITEM);
368 if(GetUInt64Value(OBJECT_FIELD_GUID) != new_item_guid)
370 SetUInt64Value(OBJECT_FIELD_GUID, MAKE_NEW_GUID(guid,0, HIGHGUID_ITEM));
371 need_save = true;
374 if (delete_result) delete result;
376 ItemPrototype const* proto = GetProto();
377 if(!proto)
378 return false;
380 // update max durability (and durability) if need
381 if(proto->MaxDurability!= GetUInt32Value(ITEM_FIELD_MAXDURABILITY))
383 SetUInt32Value(ITEM_FIELD_MAXDURABILITY,proto->MaxDurability);
384 if(GetUInt32Value(ITEM_FIELD_DURABILITY) > proto->MaxDurability)
385 SetUInt32Value(ITEM_FIELD_DURABILITY,proto->MaxDurability);
387 need_save = true;
390 // recalculate suffix factor
391 if(GetItemRandomPropertyId() < 0)
393 if(UpdateItemSuffixFactor())
394 need_save = true;
397 // Remove bind flag for items vs NO_BIND set
398 if (IsSoulBound() && proto->Bonding == NO_BIND)
400 ApplyModFlag(ITEM_FIELD_FLAGS,ITEM_FLAGS_BINDED, false);
401 need_save = true;
404 // update duration if need, and remove if not need
405 if((proto->Duration==0) != (GetUInt32Value(ITEM_FIELD_DURATION)==0))
407 SetUInt32Value(ITEM_FIELD_DURATION,abs(proto->Duration));
408 need_save = true;
411 // set correct owner
412 if(owner_guid != 0 && GetOwnerGUID() != owner_guid)
414 SetOwnerGUID(owner_guid);
415 need_save = true;
418 if(need_save) // normal item changed state set not work at loading
420 std::ostringstream ss;
421 ss << "UPDATE item_instance SET data = '";
422 for(uint16 i = 0; i < m_valuesCount; ++i )
423 ss << GetUInt32Value(i) << " ";
424 ss << "', owner_guid = '" << GUID_LOPART(GetOwnerGUID()) << "' WHERE guid = '" << guid << "'";
426 CharacterDatabase.Execute( ss.str().c_str() );
429 return true;
432 void Item::DeleteFromDB()
434 CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid = '%u'",GetGUIDLow());
437 void Item::DeleteFromInventoryDB()
439 CharacterDatabase.PExecute("DELETE FROM character_inventory WHERE item = '%u'",GetGUIDLow());
442 ItemPrototype const *Item::GetProto() const
444 return objmgr.GetItemPrototype(GetEntry());
447 Player* Item::GetOwner()const
449 return objmgr.GetPlayer(GetOwnerGUID());
452 uint32 Item::GetSkill()
454 const static uint32 item_weapon_skills[MAX_ITEM_SUBCLASS_WEAPON] =
456 SKILL_AXES, SKILL_2H_AXES, SKILL_BOWS, SKILL_GUNS, SKILL_MACES,
457 SKILL_2H_MACES, SKILL_POLEARMS, SKILL_SWORDS, SKILL_2H_SWORDS, 0,
458 SKILL_STAVES, 0, 0, SKILL_UNARMED, 0,
459 SKILL_DAGGERS, SKILL_THROWN, SKILL_ASSASSINATION, SKILL_CROSSBOWS, SKILL_WANDS,
460 SKILL_FISHING
463 const static uint32 item_armor_skills[MAX_ITEM_SUBCLASS_ARMOR] =
465 0,SKILL_CLOTH,SKILL_LEATHER,SKILL_MAIL,SKILL_PLATE_MAIL,0,SKILL_SHIELD,0,0,0,0
468 ItemPrototype const* proto = GetProto();
470 switch (proto->Class)
472 case ITEM_CLASS_WEAPON:
473 if( proto->SubClass >= MAX_ITEM_SUBCLASS_WEAPON )
474 return 0;
475 else
476 return item_weapon_skills[proto->SubClass];
478 case ITEM_CLASS_ARMOR:
479 if( proto->SubClass >= MAX_ITEM_SUBCLASS_ARMOR )
480 return 0;
481 else
482 return item_armor_skills[proto->SubClass];
484 default:
485 return 0;
489 uint32 Item::GetSpell()
491 ItemPrototype const* proto = GetProto();
493 switch (proto->Class)
495 case ITEM_CLASS_WEAPON:
496 switch (proto->SubClass)
498 case ITEM_SUBCLASS_WEAPON_AXE: return 196;
499 case ITEM_SUBCLASS_WEAPON_AXE2: return 197;
500 case ITEM_SUBCLASS_WEAPON_BOW: return 264;
501 case ITEM_SUBCLASS_WEAPON_GUN: return 266;
502 case ITEM_SUBCLASS_WEAPON_MACE: return 198;
503 case ITEM_SUBCLASS_WEAPON_MACE2: return 199;
504 case ITEM_SUBCLASS_WEAPON_POLEARM: return 200;
505 case ITEM_SUBCLASS_WEAPON_SWORD: return 201;
506 case ITEM_SUBCLASS_WEAPON_SWORD2: return 202;
507 case ITEM_SUBCLASS_WEAPON_STAFF: return 227;
508 case ITEM_SUBCLASS_WEAPON_DAGGER: return 1180;
509 case ITEM_SUBCLASS_WEAPON_THROWN: return 2567;
510 case ITEM_SUBCLASS_WEAPON_SPEAR: return 3386;
511 case ITEM_SUBCLASS_WEAPON_CROSSBOW:return 5011;
512 case ITEM_SUBCLASS_WEAPON_WAND: return 5009;
513 default: return 0;
515 case ITEM_CLASS_ARMOR:
516 switch(proto->SubClass)
518 case ITEM_SUBCLASS_ARMOR_CLOTH: return 9078;
519 case ITEM_SUBCLASS_ARMOR_LEATHER: return 9077;
520 case ITEM_SUBCLASS_ARMOR_MAIL: return 8737;
521 case ITEM_SUBCLASS_ARMOR_PLATE: return 750;
522 case ITEM_SUBCLASS_ARMOR_SHIELD: return 9116;
523 default: return 0;
526 return 0;
529 int32 Item::GenerateItemRandomPropertyId(uint32 item_id)
531 ItemPrototype const *itemProto = sItemStorage.LookupEntry<ItemPrototype>(item_id);
533 if(!itemProto)
534 return 0;
536 // item must have one from this field values not null if it can have random enchantments
537 if((!itemProto->RandomProperty) && (!itemProto->RandomSuffix))
538 return 0;
540 // item can have not null only one from field values
541 if((itemProto->RandomProperty) && (itemProto->RandomSuffix))
543 sLog.outErrorDb("Item template %u have RandomProperty==%u and RandomSuffix==%u, but must have one from field =0",itemProto->ItemId,itemProto->RandomProperty,itemProto->RandomSuffix);
544 return 0;
547 // RandomProperty case
548 if(itemProto->RandomProperty)
550 uint32 randomPropId = GetItemEnchantMod(itemProto->RandomProperty);
551 ItemRandomPropertiesEntry const *random_id = sItemRandomPropertiesStore.LookupEntry(randomPropId);
552 if(!random_id)
554 sLog.outErrorDb("Enchantment id #%u used but it doesn't have records in 'ItemRandomProperties.dbc'",randomPropId);
555 return 0;
558 return random_id->ID;
560 // RandomSuffix case
561 else
563 uint32 randomPropId = GetItemEnchantMod(itemProto->RandomSuffix);
564 ItemRandomSuffixEntry const *random_id = sItemRandomSuffixStore.LookupEntry(randomPropId);
565 if(!random_id)
567 sLog.outErrorDb("Enchantment id #%u used but it doesn't have records in sItemRandomSuffixStore.",randomPropId);
568 return 0;
571 return -int32(random_id->ID);
575 void Item::SetItemRandomProperties(int32 randomPropId)
577 if(!randomPropId)
578 return;
580 if(randomPropId > 0)
582 ItemRandomPropertiesEntry const *item_rand = sItemRandomPropertiesStore.LookupEntry(randomPropId);
583 if(item_rand)
585 if(GetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID) != int32(item_rand->ID))
587 SetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID,item_rand->ID);
588 SetState(ITEM_CHANGED);
590 for(uint32 i = PROP_ENCHANTMENT_SLOT_2; i < PROP_ENCHANTMENT_SLOT_2 + 3; ++i)
591 SetEnchantment(EnchantmentSlot(i),item_rand->enchant_id[i - PROP_ENCHANTMENT_SLOT_2],0,0);
594 else
596 ItemRandomSuffixEntry const *item_rand = sItemRandomSuffixStore.LookupEntry(-randomPropId);
597 if(item_rand)
599 if( GetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID) != -int32(item_rand->ID) ||
600 !GetItemSuffixFactor())
602 SetInt32Value(ITEM_FIELD_RANDOM_PROPERTIES_ID,-int32(item_rand->ID));
603 UpdateItemSuffixFactor();
604 SetState(ITEM_CHANGED);
607 for(uint32 i = PROP_ENCHANTMENT_SLOT_0; i < PROP_ENCHANTMENT_SLOT_0 + 3; ++i)
608 SetEnchantment(EnchantmentSlot(i),item_rand->enchant_id[i - PROP_ENCHANTMENT_SLOT_0],0,0);
613 bool Item::UpdateItemSuffixFactor()
615 uint32 suffixFactor = GenerateEnchSuffixFactor(GetEntry());
616 if(GetItemSuffixFactor()==suffixFactor)
617 return false;
618 SetUInt32Value(ITEM_FIELD_PROPERTY_SEED,suffixFactor);
619 return true;
622 void Item::SetState(ItemUpdateState state, Player *forplayer)
624 if (uState == ITEM_NEW && state == ITEM_REMOVED)
626 // pretend the item never existed
627 RemoveFromUpdateQueueOf(forplayer);
628 delete this;
629 return;
632 if (state != ITEM_UNCHANGED)
634 // new items must stay in new state until saved
635 if (uState != ITEM_NEW) uState = state;
636 AddToUpdateQueueOf(forplayer);
638 else
640 // unset in queue
641 // the item must be removed from the queue manually
642 uQueuePos = -1;
643 uState = ITEM_UNCHANGED;
647 void Item::AddToUpdateQueueOf(Player *player)
649 if (IsInUpdateQueue()) return;
651 if (!player)
653 player = GetOwner();
654 if (!player)
656 sLog.outError("Item::AddToUpdateQueueOf - GetPlayer didn't find a player matching owner's guid (%u)!", GUID_LOPART(GetOwnerGUID()));
657 return;
661 if (player->GetGUID() != GetOwnerGUID())
663 sLog.outError("Item::AddToUpdateQueueOf - Owner's guid (%u) and player's guid (%u) don't match!", GUID_LOPART(GetOwnerGUID()), player->GetGUIDLow());
664 return;
667 if (player->m_itemUpdateQueueBlocked) return;
669 player->m_itemUpdateQueue.push_back(this);
670 uQueuePos = player->m_itemUpdateQueue.size()-1;
673 void Item::RemoveFromUpdateQueueOf(Player *player)
675 if (!IsInUpdateQueue()) return;
677 if (!player)
679 player = GetOwner();
680 if (!player)
682 sLog.outError("Item::RemoveFromUpdateQueueOf - GetPlayer didn't find a player matching owner's guid (%u)!", GUID_LOPART(GetOwnerGUID()));
683 return;
687 if (player->GetGUID() != GetOwnerGUID())
689 sLog.outError("Item::RemoveFromUpdateQueueOf - Owner's guid (%u) and player's guid (%u) don't match!", GUID_LOPART(GetOwnerGUID()), player->GetGUIDLow());
690 return;
693 if (player->m_itemUpdateQueueBlocked) return;
695 player->m_itemUpdateQueue[uQueuePos] = NULL;
696 uQueuePos = -1;
699 uint8 Item::GetBagSlot() const
701 return m_container ? m_container->GetSlot() : uint8(INVENTORY_SLOT_BAG_0);
704 bool Item::IsEquipped() const
706 return !IsInBag() && m_slot < EQUIPMENT_SLOT_END;
709 bool Item::CanBeTraded(bool mail) const
711 if ((!mail || !IsBoundAccountWide()) && IsSoulBound())
712 return false;
714 if (IsBag() && (Player::IsBagPos(GetPos()) || !((Bag const*)this)->IsEmpty()) )
715 return false;
717 if (Player* owner = GetOwner())
719 if (owner->CanUnequipItem(GetPos(),false) != EQUIP_ERR_OK )
720 return false;
721 if (owner->GetLootGUID()==GetGUID())
722 return false;
725 if (IsBoundByEnchant())
726 return false;
728 return true;
731 bool Item::IsBoundByEnchant() const
733 // Check all enchants for soulbound
734 for(uint32 enchant_slot = PERM_ENCHANTMENT_SLOT; enchant_slot < MAX_ENCHANTMENT_SLOT; ++enchant_slot)
736 uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
737 if(!enchant_id)
738 continue;
740 SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
741 if(!enchantEntry)
742 continue;
744 if(enchantEntry->slot & ENCHANTMENT_CAN_SOULBOUND)
745 return true;
747 return false;
750 bool Item::IsFitToSpellRequirements(SpellEntry const* spellInfo) const
752 ItemPrototype const* proto = GetProto();
754 if (spellInfo->EquippedItemClass != -1) // -1 == any item class
756 if(spellInfo->EquippedItemClass != int32(proto->Class))
757 return false; // wrong item class
759 if(spellInfo->EquippedItemSubClassMask != 0) // 0 == any subclass
761 if((spellInfo->EquippedItemSubClassMask & (1 << proto->SubClass)) == 0)
762 return false; // subclass not present in mask
766 if(spellInfo->EquippedItemInventoryTypeMask != 0) // 0 == any inventory type
768 if((spellInfo->EquippedItemInventoryTypeMask & (1 << proto->InventoryType)) == 0)
769 return false; // inventory type not present in mask
772 return true;
775 bool Item::IsTargetValidForItemUse(Unit* pUnitTarget)
777 ItemRequiredTargetMapBounds bounds = objmgr.GetItemRequiredTargetMapBounds(GetProto()->ItemId);
779 if (bounds.first == bounds.second)
780 return true;
782 if (!pUnitTarget)
783 return false;
785 for(ItemRequiredTargetMap::const_iterator itr = bounds.first; itr != bounds.second; ++itr)
786 if(itr->second.IsFitToRequirements(pUnitTarget))
787 return true;
789 return false;
792 void Item::SetEnchantment(EnchantmentSlot slot, uint32 id, uint32 duration, uint32 charges)
794 // Better lost small time at check in comparison lost time at item save to DB.
795 if((GetEnchantmentId(slot) == id) && (GetEnchantmentDuration(slot) == duration) && (GetEnchantmentCharges(slot) == charges))
796 return;
798 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_ID_OFFSET,id);
799 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET,duration);
800 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_CHARGES_OFFSET,charges);
801 SetState(ITEM_CHANGED);
804 void Item::SetEnchantmentDuration(EnchantmentSlot slot, uint32 duration)
806 if(GetEnchantmentDuration(slot) == duration)
807 return;
809 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_DURATION_OFFSET,duration);
810 SetState(ITEM_CHANGED);
813 void Item::SetEnchantmentCharges(EnchantmentSlot slot, uint32 charges)
815 if(GetEnchantmentCharges(slot) == charges)
816 return;
818 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + ENCHANTMENT_CHARGES_OFFSET,charges);
819 SetState(ITEM_CHANGED);
822 void Item::ClearEnchantment(EnchantmentSlot slot)
824 if(!GetEnchantmentId(slot))
825 return;
827 for(uint8 x = 0; x < 3; ++x)
828 SetUInt32Value(ITEM_FIELD_ENCHANTMENT_1_1 + slot*MAX_ENCHANTMENT_OFFSET + x, 0);
829 SetState(ITEM_CHANGED);
832 bool Item::GemsFitSockets() const
834 bool fits = true;
835 for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+MAX_GEM_SOCKETS; ++enchant_slot)
837 uint8 SocketColor = GetProto()->Socket[enchant_slot-SOCK_ENCHANTMENT_SLOT].Color;
839 uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
840 if(!enchant_id)
842 if(SocketColor) fits &= false;
843 continue;
846 SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
847 if(!enchantEntry)
849 if(SocketColor) fits &= false;
850 continue;
853 uint8 GemColor = 0;
855 uint32 gemid = enchantEntry->GemID;
856 if(gemid)
858 ItemPrototype const* gemProto = sItemStorage.LookupEntry<ItemPrototype>(gemid);
859 if(gemProto)
861 GemPropertiesEntry const* gemProperty = sGemPropertiesStore.LookupEntry(gemProto->GemProperties);
862 if(gemProperty)
863 GemColor = gemProperty->color;
867 fits &= (GemColor & SocketColor) ? true : false;
869 return fits;
872 uint8 Item::GetGemCountWithID(uint32 GemID) const
874 uint8 count = 0;
875 for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+MAX_GEM_SOCKETS; ++enchant_slot)
877 uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
878 if(!enchant_id)
879 continue;
881 SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
882 if(!enchantEntry)
883 continue;
885 if(GemID == enchantEntry->GemID)
886 ++count;
888 return count;
891 uint8 Item::GetGemCountWithLimitCategory(uint32 limitCategory) const
893 uint8 count = 0;
894 for(uint32 enchant_slot = SOCK_ENCHANTMENT_SLOT; enchant_slot < SOCK_ENCHANTMENT_SLOT+MAX_GEM_SOCKETS; ++enchant_slot)
896 uint32 enchant_id = GetEnchantmentId(EnchantmentSlot(enchant_slot));
897 if(!enchant_id)
898 continue;
900 SpellItemEnchantmentEntry const* enchantEntry = sSpellItemEnchantmentStore.LookupEntry(enchant_id);
901 if(!enchantEntry)
902 continue;
904 ItemPrototype const* gemProto = ObjectMgr::GetItemPrototype(enchantEntry->GemID);
905 if(!gemProto)
906 continue;
908 if(gemProto->ItemLimitCategory==limitCategory)
909 ++count;
911 return count;
914 bool Item::IsLimitedToAnotherMapOrZone( uint32 cur_mapId, uint32 cur_zoneId) const
916 ItemPrototype const* proto = GetProto();
917 return proto && (proto->Map && proto->Map != cur_mapId || proto->Area && proto->Area != cur_zoneId );
920 // Though the client has the information in the item's data field,
921 // we have to send SMSG_ITEM_TIME_UPDATE to display the remaining
922 // time.
923 void Item::SendTimeUpdate(Player* owner)
925 if (!GetUInt32Value(ITEM_FIELD_DURATION))
926 return;
928 WorldPacket data(SMSG_ITEM_TIME_UPDATE, (8+4));
929 data << (uint64)GetGUID();
930 data << (uint32)GetUInt32Value(ITEM_FIELD_DURATION);
931 owner->GetSession()->SendPacket(&data);
934 Item* Item::CreateItem( uint32 item, uint32 count, Player const* player )
936 if ( count < 1 )
937 return NULL; //don't create item at zero count
939 ItemPrototype const *pProto = objmgr.GetItemPrototype( item );
940 if( pProto )
942 if ( count > pProto->GetMaxStackSize())
943 count = pProto->GetMaxStackSize();
945 assert(count !=0 && "pProto->Stackable==0 but checked at loading already");
947 Item *pItem = NewItemOrBag( pProto );
948 if( pItem->Create(objmgr.GenerateLowGuid(HIGHGUID_ITEM), item, player) )
950 pItem->SetCount( count );
951 return pItem;
953 else
954 delete pItem;
956 return NULL;
959 Item* Item::CloneItem( uint32 count, Player const* player ) const
961 Item* newItem = CreateItem( GetEntry(), count, player );
962 if(!newItem)
963 return NULL;
965 newItem->SetUInt32Value( ITEM_FIELD_CREATOR, GetUInt32Value( ITEM_FIELD_CREATOR ) );
966 newItem->SetUInt32Value( ITEM_FIELD_GIFTCREATOR, GetUInt32Value( ITEM_FIELD_GIFTCREATOR ) );
967 newItem->SetUInt32Value( ITEM_FIELD_FLAGS, GetUInt32Value( ITEM_FIELD_FLAGS ) );
968 newItem->SetUInt32Value( ITEM_FIELD_DURATION, GetUInt32Value( ITEM_FIELD_DURATION ) );
969 newItem->SetItemRandomProperties(GetItemRandomPropertyId());
970 return newItem;
973 bool Item::IsBindedNotWith( Player const* player ) const
975 // not binded item
976 if(!IsSoulBound())
977 return false;
979 // own item
980 if(GetOwnerGUID()== player->GetGUID())
981 return false;
983 // not BOA item case
984 if(!IsBoundAccountWide())
985 return true;
987 // online
988 if(Player* owner = objmgr.GetPlayer(GetOwnerGUID()))
990 return owner->GetSession()->GetAccountId() != player->GetSession()->GetAccountId();
992 // offline slow case
993 else
995 return objmgr.GetPlayerAccountIdByGUID(GetOwnerGUID()) != player->GetSession()->GetAccountId();
999 bool ItemRequiredTarget::IsFitToRequirements( Unit* pUnitTarget ) const
1001 if(pUnitTarget->GetTypeId() != TYPEID_UNIT)
1002 return false;
1004 if(pUnitTarget->GetEntry() != m_uiTargetEntry)
1005 return false;
1007 switch(m_uiType)
1009 case ITEM_TARGET_TYPE_CREATURE:
1010 return pUnitTarget->isAlive();
1011 case ITEM_TARGET_TYPE_DEAD:
1012 return !pUnitTarget->isAlive();
1013 default:
1014 return false;