Changeset 18 for trunk/src/game/Creature.cpp
- Timestamp:
- 11/19/08 13:23:29 (17 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/game/Creature.cpp
r14 r18 63 63 } 64 64 65 bool VendorItemData::RemoveItem( uint32 item_id ) 66 { 67 for(VendorItemList::iterator i = m_items.begin(); i != m_items.end(); ++i ) 68 { 69 if((*i)->item==item_id) 70 { 71 m_items.erase(i); 72 return true; 73 } 74 } 75 return false; 76 } 77 78 VendorItem const* VendorItemData::FindItem(uint32 item_id) const 79 { 80 for(VendorItemList::const_iterator i = m_items.begin(); i != m_items.end(); ++i ) 81 if((*i)->item==item_id) 82 return *i; 83 return NULL; 84 } 85 65 86 Creature::Creature() : 66 87 Unit(), i_AI(NULL), 67 88 lootForPickPocketed(false), lootForBody(false), m_groupLootTimer(0), lootingGroupLeaderGUID(0), 68 m_ itemsLoaded(false), m_lootMoney(0), m_lootRecipient(0),89 m_lootMoney(0), m_lootRecipient(0), 69 90 m_deathTimer(0), m_respawnTime(0), m_respawnDelay(25), m_corpseDelay(60), m_respawnradius(0.0f), 70 91 m_gossipOptionLoaded(false),m_emoteState(0), m_isPet(false), m_isTotem(false), 71 92 m_regenTimer(2000), m_defaultMovementType(IDLE_MOTION_TYPE), m_equipmentId(0), 72 93 m_AlreadyCallAssistence(false), m_regenHealth(true), m_AI_locked(false), m_isDeadByDefault(false), 73 m_meleeDamageSchoolMask(SPELL_SCHOOL_MASK_NORMAL),m_creatureInfo(NULL) 94 m_meleeDamageSchoolMask(SPELL_SCHOOL_MASK_NORMAL),m_creatureInfo(NULL), m_DBTableGuid(0) 74 95 { 75 96 m_valuesCount = UNIT_END; … … 88 109 CleanupsBeforeDelete(); 89 110 90 m_vendor _items.clear();111 m_vendorItemCounts.clear(); 91 112 92 113 delete i_AI; … … 504 525 break; 505 526 } 527 LoadCreaturesAddon(); 506 528 } 507 529 … … 674 696 break; 675 697 case GOSSIP_OPTION_VENDOR: 676 // load vendor items if not yet 677 LoadGoods(); 678 679 if(!GetItemCount()) 698 { 699 VendorItemData const* vItems = GetVendorItems(); 700 if(!vItems || vItems->Empty()) 680 701 { 681 702 sLog.outErrorDb("Creature %u (Entry: %u) have UNIT_NPC_FLAG_VENDOR but have empty trading item list.", … … 684 705 } 685 706 break; 707 } 686 708 case GOSSIP_OPTION_TRAINER: 687 709 if(!isCanTrainingOf(pPlayer,false)) … … 935 957 uint32 Creature::GetNpcTextId() 936 958 { 959 if (!m_DBTableGuid) 960 return DEFAULT_GOSSIP_MESSAGE; 961 937 962 if(uint32 pos = objmgr.GetNpcGossip(m_DBTableGuid)) 938 963 return pos; … … 1047 1072 { 1048 1073 // update in loaded data 1074 if (!m_DBTableGuid) 1075 m_DBTableGuid = GetGUIDLow(); 1049 1076 CreatureData& data = objmgr.NewOrExistCreatureData(m_DBTableGuid); 1050 1077 … … 1238 1265 Object::_Create(guidlow, Entry, HIGHGUID_UNIT); 1239 1266 1240 m_DBTableGuid = guidlow;1241 1267 if(!UpdateEntry(Entry, team, data)) 1242 1268 return false; … … 1264 1290 } 1265 1291 1266 uint32 stored_guid = guid;1292 m_DBTableGuid = guid; 1267 1293 if (map->GetInstanceId() != 0) guid = objmgr.GenerateLowGuid(HIGHGUID_UNIT); 1268 1294 … … 1278 1304 return false; 1279 1305 } 1280 1281 m_DBTableGuid = stored_guid;1282 LoadCreaturesAddon();1283 1306 1284 1307 m_respawnradius = data->spawndist; … … 1347 1370 } 1348 1371 1349 void Creature::LoadGoods()1350 {1351 // already loaded;1352 if(m_itemsLoaded)1353 return;1354 1355 m_vendor_items.clear();1356 1357 VendorItemList const* vList = objmgr.GetNpcVendorItemList(GetEntry());1358 if(!vList)1359 return;1360 1361 for (VendorItemList::const_iterator _item_iter = vList->begin(); _item_iter != vList->end(); ++_item_iter)1362 AddItem( (*_item_iter)->item, (*_item_iter)->maxcount, (*_item_iter)->incrtime, (*_item_iter)->ExtendedCost);1363 1364 m_itemsLoaded = true;1365 }1366 1367 1372 bool Creature::hasQuest(uint32 quest_id) const 1368 1373 { … … 1389 1394 void Creature::DeleteFromDB() 1390 1395 { 1396 if (!m_DBTableGuid) 1397 { 1398 sLog.outDebug("Trying to delete not saved creature!"); 1399 return; 1400 } 1401 1391 1402 objmgr.SaveCreatureRespawnTime(m_DBTableGuid,GetInstanceId(),0); 1392 1403 objmgr.DeleteCreatureData(m_DBTableGuid); … … 1495 1506 if(getDeathState()==DEAD) 1496 1507 { 1497 objmgr.SaveCreatureRespawnTime(m_DBTableGuid,GetInstanceId(),0); 1508 if (m_DBTableGuid) 1509 objmgr.SaveCreatureRespawnTime(m_DBTableGuid,GetInstanceId(),0); 1498 1510 m_respawnTime = time(NULL); // respawn at next tick 1499 1511 } … … 1717 1729 void Creature::SaveRespawnTime() 1718 1730 { 1719 if(isPet() )1731 if(isPet() || !m_DBTableGuid) 1720 1732 return; 1721 1733 … … 1753 1765 CreatureDataAddon const* Creature::GetCreatureAddon() const 1754 1766 { 1755 if(CreatureDataAddon const* addon = ObjectMgr::GetCreatureAddon(m_DBTableGuid)) 1756 return addon; 1767 if (m_DBTableGuid) 1768 { 1769 if(CreatureDataAddon const* addon = ObjectMgr::GetCreatureAddon(m_DBTableGuid)) 1770 return addon; 1771 } 1757 1772 1758 1773 // dependent from heroic mode entry … … 1957 1972 } 1958 1973 1974 1975 VendorItemData const* Creature::GetVendorItems() const 1976 { 1977 return objmgr.GetNpcVendorItemList(GetEntry()); 1978 } 1979 1980 uint32 Creature::GetVendorItemCurrentCount(VendorItem const* vItem) 1981 { 1982 if(!vItem->maxcount) 1983 return vItem->maxcount; 1984 1985 VendorItemCounts::iterator itr = m_vendorItemCounts.begin(); 1986 for(; itr != m_vendorItemCounts.end(); ++itr) 1987 if(itr->itemId==vItem->item) 1988 break; 1989 1990 if(itr == m_vendorItemCounts.end()) 1991 return vItem->maxcount; 1992 1993 VendorItemCount* vCount = &*itr; 1994 1995 time_t ptime = time(NULL); 1996 1997 if( vCount->lastIncrementTime + vItem->incrtime <= ptime ) 1998 { 1999 ItemPrototype const* pProto = objmgr.GetItemPrototype(vItem->item); 2000 2001 uint32 diff = uint32((ptime - vCount->lastIncrementTime)/vItem->incrtime); 2002 if((vCount->count + diff * pProto->BuyCount) >= vItem->maxcount ) 2003 { 2004 m_vendorItemCounts.erase(itr); 2005 return vItem->maxcount; 2006 } 2007 2008 vCount->count += diff * pProto->BuyCount; 2009 vCount->lastIncrementTime = ptime; 2010 } 2011 2012 return vCount->count; 2013 } 2014 2015 uint32 Creature::UpdateVendorItemCurrentCount(VendorItem const* vItem, uint32 used_count) 2016 { 2017 if(!vItem->maxcount) 2018 return 0; 2019 2020 VendorItemCounts::iterator itr = m_vendorItemCounts.begin(); 2021 for(; itr != m_vendorItemCounts.end(); ++itr) 2022 if(itr->itemId==vItem->item) 2023 break; 2024 2025 if(itr == m_vendorItemCounts.end()) 2026 { 2027 uint32 new_count = vItem->maxcount > used_count ? vItem->maxcount-used_count : 0; 2028 m_vendorItemCounts.push_back(VendorItemCount(vItem->item,new_count)); 2029 return new_count; 2030 } 2031 2032 VendorItemCount* vCount = &*itr; 2033 2034 time_t ptime = time(NULL); 2035 2036 if( vCount->lastIncrementTime + vItem->incrtime <= ptime ) 2037 { 2038 ItemPrototype const* pProto = objmgr.GetItemPrototype(vItem->item); 2039 2040 uint32 diff = uint32((ptime - vCount->lastIncrementTime)/vItem->incrtime); 2041 if((vCount->count + diff * pProto->BuyCount) < vItem->maxcount ) 2042 vCount->count += diff * pProto->BuyCount; 2043 else 2044 vCount->count = vItem->maxcount; 2045 } 2046 2047 vCount->count = vCount->count > used_count ? vCount->count-used_count : 0; 2048 vCount->lastIncrementTime = ptime; 2049 return vCount->count; 2050 } 2051 1959 2052 TrainerSpellData const* Creature::GetTrainerSpells() const 1960 2053 {