root/trunk/src/game/ObjectMgr.cpp @ 78

Revision 78, 248.8 kB (checked in by yumileroy, 17 years ago)

[svn] * fixed help for subcommands - source mangos
* Renamed accounts column tbc to expansion and it only took a little over 4 hours o.O

Original author: KingPin?
Date: 2008-10-20 12:23:56-05:00

Line 
1/*
2 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
3 *
4 * Thanks to the original authors: MaNGOS <http://www.mangosproject.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 "Database/DatabaseEnv.h"
23#include "Database/SQLStorage.h"
24
25#include "Log.h"
26#include "MapManager.h"
27#include "ObjectMgr.h"
28#include "SpellMgr.h"
29#include "UpdateMask.h"
30#include "World.h"
31#include "WorldSession.h"
32#include "Group.h"
33#include "Guild.h"
34#include "ArenaTeam.h"
35#include "Transports.h"
36#include "ProgressBar.h"
37#include "Policies/SingletonImp.h"
38#include "Language.h"
39#include "GameEvent.h"
40#include "Spell.h"
41#include "Chat.h"
42#include "InstanceSaveMgr.h"
43#include "SpellAuras.h"
44#include "Util.h"
45
46INSTANTIATE_SINGLETON_1(ObjectMgr);
47
48ScriptMapMap sQuestEndScripts;
49ScriptMapMap sQuestStartScripts;
50ScriptMapMap sSpellScripts;
51ScriptMapMap sGameObjectScripts;
52ScriptMapMap sEventScripts;
53
54bool normalizePlayerName(std::string& name)
55{
56    if(name.empty())
57        return false;
58
59    wchar_t wstr_buf[MAX_INTERNAL_PLAYER_NAME+1];
60    size_t wstr_len = MAX_INTERNAL_PLAYER_NAME;
61
62    if(!Utf8toWStr(name,&wstr_buf[0],wstr_len))
63        return false;
64
65    wstr_buf[0] = wcharToUpper(wstr_buf[0]);
66    for(size_t i = 1; i < wstr_len; ++i)
67        wstr_buf[i] = wcharToLower(wstr_buf[i]);
68
69    if(!WStrToUtf8(wstr_buf,wstr_len,name))
70        return false;
71
72    return true;
73}
74
75LanguageDesc lang_description[LANGUAGES_COUNT] =
76{
77    { LANG_ADDON,           0, 0                       },
78    { LANG_UNIVERSAL,       0, 0                       },
79    { LANG_ORCISH,        669, SKILL_LANG_ORCISH       },
80    { LANG_DARNASSIAN,    671, SKILL_LANG_DARNASSIAN   },
81    { LANG_TAURAHE,       670, SKILL_LANG_TAURAHE      },
82    { LANG_DWARVISH,      672, SKILL_LANG_DWARVEN      },
83    { LANG_COMMON,        668, SKILL_LANG_COMMON       },
84    { LANG_DEMONIC,       815, SKILL_LANG_DEMON_TONGUE },
85    { LANG_TITAN,         816, SKILL_LANG_TITAN        },
86    { LANG_THALASSIAN,    813, SKILL_LANG_THALASSIAN   },
87    { LANG_DRACONIC,      814, SKILL_LANG_DRACONIC     },
88    { LANG_KALIMAG,       817, SKILL_LANG_OLD_TONGUE   },
89    { LANG_GNOMISH,      7340, SKILL_LANG_GNOMISH      },
90    { LANG_TROLL,        7341, SKILL_LANG_TROLL        },
91    { LANG_GUTTERSPEAK, 17737, SKILL_LANG_GUTTERSPEAK  },
92    { LANG_DRAENEI,     29932, SKILL_LANG_DRAENEI      },
93    { LANG_ZOMBIE,          0, 0                       },
94    { LANG_GNOMISH_BINARY,  0, 0                       },
95    { LANG_GOBLIN_BINARY,   0, 0                       }
96};
97
98LanguageDesc const* GetLanguageDescByID(uint32 lang)
99{
100    for(int i = 0; i < LANGUAGES_COUNT; ++i)
101    {
102        if(uint32(lang_description[i].lang_id) == lang)
103            return &lang_description[i];
104    }
105
106    return NULL;
107}
108
109ObjectMgr::ObjectMgr()
110{
111    m_hiCharGuid        = 1;
112    m_hiCreatureGuid    = 1;
113    m_hiPetGuid         = 1;
114    m_hiItemGuid        = 1;
115    m_hiGoGuid          = 1;
116    m_hiDoGuid          = 1;
117    m_hiCorpseGuid      = 1;
118
119    m_hiPetNumber       = 1;
120
121    mGuildBankTabPrice.resize(GUILD_BANK_MAX_TABS);
122    mGuildBankTabPrice[0] = 100;
123    mGuildBankTabPrice[1] = 250;
124    mGuildBankTabPrice[2] = 500;
125    mGuildBankTabPrice[3] = 1000;
126    mGuildBankTabPrice[4] = 2500;
127    mGuildBankTabPrice[5] = 5000;
128
129    // Only zero condition left, others will be added while loading DB tables
130    mConditions.resize(1);
131}
132
133ObjectMgr::~ObjectMgr()
134{
135    for( QuestMap::iterator i = mQuestTemplates.begin( ); i != mQuestTemplates.end( ); ++ i )
136    {
137        delete i->second;
138    }
139    mQuestTemplates.clear( );
140
141    for( GossipTextMap::iterator i = mGossipText.begin( ); i != mGossipText.end( ); ++ i )
142    {
143        delete i->second;
144    }
145    mGossipText.clear( );
146
147    mAreaTriggers.clear();
148
149    for(PetLevelInfoMap::iterator i = petInfo.begin( ); i != petInfo.end( ); ++ i )
150    {
151        delete[] i->second;
152    }
153    petInfo.clear();
154
155    // free only if loaded
156    for (int class_ = 0; class_ < MAX_CLASSES; ++class_)
157        delete[] playerClassInfo[class_].levelInfo;
158
159    for (int race = 0; race < MAX_RACES; ++race)
160        for (int class_ = 0; class_ < MAX_CLASSES; ++class_)
161            delete[] playerInfo[race][class_].levelInfo;
162
163    // free group and guild objects
164    for (GroupSet::iterator itr = mGroupSet.begin(); itr != mGroupSet.end(); ++itr)
165        delete (*itr);
166    for (GuildSet::iterator itr = mGuildSet.begin(); itr != mGuildSet.end(); ++itr)
167        delete (*itr);
168
169    for(ItemMap::iterator itr = mAitems.begin(); itr != mAitems.end(); ++itr)
170        delete itr->second;
171
172    for (CacheVendorItemMap::iterator itr = m_mCacheVendorItemMap.begin(); itr != m_mCacheVendorItemMap.end(); ++itr)
173        itr->second.Clear();
174
175    for (CacheTrainerSpellMap::iterator itr = m_mCacheTrainerSpellMap.begin(); itr != m_mCacheTrainerSpellMap.end(); ++itr)
176        itr->second.Clear();
177}
178
179Group * ObjectMgr::GetGroupByLeader(const uint64 &guid) const
180{
181    for(GroupSet::const_iterator itr = mGroupSet.begin(); itr != mGroupSet.end(); ++itr)
182        if ((*itr)->GetLeaderGUID() == guid)
183            return *itr;
184
185    return NULL;
186}
187
188Guild * ObjectMgr::GetGuildById(const uint32 GuildId) const
189{
190    for(GuildSet::const_iterator itr = mGuildSet.begin(); itr != mGuildSet.end(); itr++)
191        if ((*itr)->GetId() == GuildId)
192            return *itr;
193
194    return NULL;
195}
196
197Guild * ObjectMgr::GetGuildByName(std::string guildname) const
198{
199    for(GuildSet::const_iterator itr = mGuildSet.begin(); itr != mGuildSet.end(); itr++)
200        if ((*itr)->GetName() == guildname)
201            return *itr;
202
203    return NULL;
204}
205
206std::string ObjectMgr::GetGuildNameById(const uint32 GuildId) const
207{
208    for(GuildSet::const_iterator itr = mGuildSet.begin(); itr != mGuildSet.end(); itr++)
209        if ((*itr)->GetId() == GuildId)
210            return (*itr)->GetName();
211
212    return "";
213}
214
215Guild* ObjectMgr::GetGuildByLeader(const uint64 &guid) const
216{
217    for(GuildSet::const_iterator itr = mGuildSet.begin(); itr != mGuildSet.end(); ++itr)
218        if( (*itr)->GetLeader() == guid)
219            return *itr;
220
221    return NULL;
222}
223
224ArenaTeam* ObjectMgr::GetArenaTeamById(const uint32 ArenaTeamId) const
225{
226    for(ArenaTeamSet::const_iterator itr = mArenaTeamSet.begin(); itr != mArenaTeamSet.end(); itr++)
227        if ((*itr)->GetId() == ArenaTeamId)
228            return *itr;
229
230    return NULL;
231}
232
233ArenaTeam* ObjectMgr::GetArenaTeamByName(std::string arenateamname) const
234{
235    for(ArenaTeamSet::const_iterator itr = mArenaTeamSet.begin(); itr != mArenaTeamSet.end(); itr++)
236        if ((*itr)->GetName() == arenateamname)
237            return *itr;
238
239    return NULL;
240}
241
242ArenaTeam* ObjectMgr::GetArenaTeamByCapitan(uint64 const& guid) const
243{
244    for(ArenaTeamSet::const_iterator itr = mArenaTeamSet.begin(); itr != mArenaTeamSet.end(); itr++)
245        if ((*itr)->GetCaptain() == guid)
246            return *itr;
247
248    return NULL;
249}
250
251AuctionHouseObject * ObjectMgr::GetAuctionsMap( uint32 location )
252{
253    switch ( location )
254    {
255        case 6:                                             //horde
256            return & mHordeAuctions;
257            break;
258        case 2:                                             //alliance
259            return & mAllianceAuctions;
260            break;
261        default:                                            //neutral
262            return & mNeutralAuctions;
263    }
264}
265
266uint32 ObjectMgr::GetAuctionCut(uint32 location, uint32 highBid)
267{
268    if (location == 7 && !sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_AUCTION))
269        return (uint32) (0.15f * highBid * sWorld.getRate(RATE_AUCTION_CUT));
270    else
271        return (uint32) (0.05f * highBid * sWorld.getRate(RATE_AUCTION_CUT));
272}
273
274uint32 ObjectMgr::GetAuctionDeposit(uint32 location, uint32 time, Item *pItem)
275{
276    float percentance;                                      // in 0..1
277    if ( location == 7 && !sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_AUCTION))
278        percentance = 0.75f;
279    else
280        percentance = 0.15f;
281
282    percentance *= sWorld.getRate(RATE_AUCTION_DEPOSIT);
283
284    return uint32( percentance * pItem->GetProto()->SellPrice * pItem->GetCount() * (time / MIN_AUCTION_TIME ) );
285}
286
287/// the sum of outbid is (1% from current bid)*5, if bid is very small, it is 1c
288uint32 ObjectMgr::GetAuctionOutBid(uint32 currentBid)
289{
290    uint32 outbid = (currentBid / 100) * 5;
291    if (!outbid)
292        outbid = 1;
293    return outbid;
294}
295
296//does not clear ram
297void ObjectMgr::SendAuctionWonMail( AuctionEntry *auction )
298{
299    Item *pItem = GetAItem(auction->item_guidlow);
300    if(!pItem)
301        return;
302
303    uint64 bidder_guid = MAKE_NEW_GUID(auction->bidder, 0, HIGHGUID_PLAYER);
304    Player *bidder = GetPlayer(bidder_guid);
305
306    uint32 bidder_accId = 0;
307
308    // data for gm.log
309    if( sWorld.getConfig(CONFIG_GM_LOG_TRADE) )
310    {
311        uint32 bidder_security = 0;
312        std::string bidder_name;
313        if (bidder)
314        {
315            bidder_accId = bidder->GetSession()->GetAccountId();
316            bidder_security = bidder->GetSession()->GetSecurity();
317            bidder_name = bidder->GetName();
318        }
319        else
320        {
321            bidder_accId = GetPlayerAccountIdByGUID(bidder_guid);
322            bidder_security = GetSecurityByAccount(bidder_accId);
323
324            if(bidder_security > SEC_PLAYER )               // not do redundant DB requests
325            {
326                if(!GetPlayerNameByGUID(bidder_guid,bidder_name))
327                    bidder_name = GetTrinityStringForDBCLocale(LANG_UNKNOWN);
328            }
329        }
330
331        if( bidder_security > SEC_PLAYER )
332        {
333            std::string owner_name;
334            if(!GetPlayerNameByGUID(auction->owner,owner_name))
335                owner_name = GetTrinityStringForDBCLocale(LANG_UNKNOWN);
336
337            uint32 owner_accid = GetPlayerAccountIdByGUID(auction->owner);
338
339            sLog.outCommand("GM %s (Account: %u) won item in auction: %s (Entry: %u Count: %u) and pay money: %u. Original owner %s (Account: %u)",
340                bidder_name.c_str(),bidder_accId,pItem->GetProto()->Name1,pItem->GetEntry(),pItem->GetCount(),auction->bid,owner_name.c_str(),owner_accid);
341        }
342    }
343    else if(!bidder)
344        bidder_accId = GetPlayerAccountIdByGUID(bidder_guid);
345
346    // receiver exist
347    if(bidder || bidder_accId)
348    {
349        std::ostringstream msgAuctionWonSubject;
350        msgAuctionWonSubject << auction->item_template << ":0:" << AUCTION_WON;
351
352        std::ostringstream msgAuctionWonBody;
353        msgAuctionWonBody.width(16);
354        msgAuctionWonBody << std::right << std::hex << auction->owner;
355        msgAuctionWonBody << std::dec << ":" << auction->bid << ":" << auction->buyout;
356        sLog.outDebug( "AuctionWon body string : %s", msgAuctionWonBody.str().c_str() );
357
358        //prepare mail data... :
359        uint32 itemTextId = this->CreateItemText( msgAuctionWonBody.str() );
360
361        // set owner to bidder (to prevent delete item with sender char deleting)
362        // owner in `data` will set at mail receive and item extracting
363        CharacterDatabase.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'",auction->bidder,pItem->GetGUIDLow());
364        CharacterDatabase.CommitTransaction();
365
366        MailItemsInfo mi;
367        mi.AddItem(auction->item_guidlow, auction->item_template, pItem);
368
369        if (bidder)
370            bidder->GetSession()->SendAuctionBidderNotification( auction->location, auction->Id, bidder_guid, 0, 0, auction->item_template);
371        else
372            RemoveAItem(pItem->GetGUIDLow());               // we have to remove the item, before we delete it !!
373
374        // will delete item or place to receiver mail list
375        WorldSession::SendMailTo(bidder, MAIL_AUCTION, MAIL_STATIONERY_AUCTION, auction->location, auction->bidder, msgAuctionWonSubject.str(), itemTextId, &mi, 0, 0, MAIL_CHECK_MASK_AUCTION);
376    }
377    // receiver not exist
378    else
379    {
380        CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid='%u'", pItem->GetGUIDLow());
381        RemoveAItem(pItem->GetGUIDLow());                   // we have to remove the item, before we delete it !!
382        delete pItem;
383    }
384}
385
386void ObjectMgr::SendAuctionSalePendingMail( AuctionEntry * auction )
387{
388    uint64 owner_guid = MAKE_NEW_GUID(auction->owner, 0, HIGHGUID_PLAYER);
389    Player *owner = GetPlayer(owner_guid);
390
391    // owner exist (online or offline)
392    if(owner || GetPlayerAccountIdByGUID(owner_guid))
393    {
394        std::ostringstream msgAuctionSalePendingSubject;
395        msgAuctionSalePendingSubject << auction->item_template << ":0:" << AUCTION_SALE_PENDING;
396
397        std::ostringstream msgAuctionSalePendingBody;
398        uint32 auctionCut = GetAuctionCut(auction->location, auction->bid);
399
400        time_t distrTime = time(NULL) + HOUR;
401
402        msgAuctionSalePendingBody.width(16);
403        msgAuctionSalePendingBody << std::right << std::hex << auction->bidder;
404        msgAuctionSalePendingBody << std::dec << ":" << auction->bid << ":" << auction->buyout;
405        msgAuctionSalePendingBody << ":" << auction->deposit << ":" << auctionCut << ":0:";
406        msgAuctionSalePendingBody << secsToTimeBitFields(distrTime);
407
408        sLog.outDebug("AuctionSalePending body string : %s", msgAuctionSalePendingBody.str().c_str());
409
410        uint32 itemTextId = this->CreateItemText( msgAuctionSalePendingBody.str() );
411
412        WorldSession::SendMailTo(owner, MAIL_AUCTION, MAIL_STATIONERY_AUCTION, auction->location, auction->owner, msgAuctionSalePendingSubject.str(), itemTextId, NULL, 0, 0, MAIL_CHECK_MASK_AUCTION);
413    }
414}
415
416//call this method to send mail to auction owner, when auction is successful, it does not clear ram
417void ObjectMgr::SendAuctionSuccessfulMail( AuctionEntry * auction )
418{
419    uint64 owner_guid = MAKE_NEW_GUID(auction->owner, 0, HIGHGUID_PLAYER);
420    Player *owner = GetPlayer(owner_guid);
421
422    uint32 owner_accId = 0;
423    if(!owner)
424        owner_accId = GetPlayerAccountIdByGUID(owner_guid);
425
426    // owner exist
427    if(owner || owner_accId)
428    {
429        std::ostringstream msgAuctionSuccessfulSubject;
430        msgAuctionSuccessfulSubject << auction->item_template << ":0:" << AUCTION_SUCCESSFUL;
431
432        std::ostringstream auctionSuccessfulBody;
433        uint32 auctionCut = GetAuctionCut(auction->location, auction->bid);
434
435        auctionSuccessfulBody.width(16);
436        auctionSuccessfulBody << std::right << std::hex << auction->bidder;
437        auctionSuccessfulBody << std::dec << ":" << auction->bid << ":" << auction->buyout;
438        auctionSuccessfulBody << ":" << auction->deposit << ":" << auctionCut;
439
440        sLog.outDebug("AuctionSuccessful body string : %s", auctionSuccessfulBody.str().c_str());
441
442        uint32 itemTextId = this->CreateItemText( auctionSuccessfulBody.str() );
443
444        uint32 profit = auction->bid + auction->deposit - auctionCut;
445
446        if (owner)
447        {
448            //send auction owner notification, bidder must be current!
449            owner->GetSession()->SendAuctionOwnerNotification( auction );
450        }
451
452        WorldSession::SendMailTo(owner, MAIL_AUCTION, MAIL_STATIONERY_AUCTION, auction->location, auction->owner, msgAuctionSuccessfulSubject.str(), itemTextId, NULL, profit, 0, MAIL_CHECK_MASK_AUCTION, HOUR);
453    }
454}
455
456//does not clear ram
457void ObjectMgr::SendAuctionExpiredMail( AuctionEntry * auction )
458{                                                           //return an item in auction to its owner by mail
459    Item *pItem = GetAItem(auction->item_guidlow);
460    if(!pItem)
461    {
462        sLog.outError("Auction item (GUID: %u) not found, and lost.",auction->item_guidlow);
463        return;
464    }
465
466    uint64 owner_guid = MAKE_NEW_GUID(auction->owner, 0, HIGHGUID_PLAYER);
467    Player *owner = GetPlayer(owner_guid);
468
469    uint32 owner_accId = 0;
470    if(!owner)
471        owner_accId = GetPlayerAccountIdByGUID(owner_guid);
472
473    // owner exist
474    if(owner || owner_accId)
475    {
476        std::ostringstream subject;
477        subject << auction->item_template << ":0:" << AUCTION_EXPIRED;
478
479        if ( owner )
480            owner->GetSession()->SendAuctionOwnerNotification( auction );
481        else
482            RemoveAItem(pItem->GetGUIDLow());               // we have to remove the item, before we delete it !!
483
484        MailItemsInfo mi;
485        mi.AddItem(auction->item_guidlow, auction->item_template, pItem);
486
487        // will delete item or place to receiver mail list
488        WorldSession::SendMailTo(owner, MAIL_AUCTION, MAIL_STATIONERY_AUCTION, auction->location, GUID_LOPART(owner_guid), subject.str(), 0, &mi, 0, 0, MAIL_CHECK_MASK_NONE);
489
490    }
491    // owner not found
492    else
493    {
494        CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid='%u'",pItem->GetGUIDLow());
495        RemoveAItem(pItem->GetGUIDLow());                   // we have to remove the item, before we delete it !!
496        delete pItem;
497    }
498}
499
500CreatureInfo const* ObjectMgr::GetCreatureTemplate(uint32 id)
501{
502    return sCreatureStorage.LookupEntry<CreatureInfo>(id);
503}
504
505void ObjectMgr::LoadCreatureLocales()
506{
507    QueryResult *result = WorldDatabase.Query("SELECT entry,name_loc1,subname_loc1,name_loc2,subname_loc2,name_loc3,subname_loc3,name_loc4,subname_loc4,name_loc5,subname_loc5,name_loc6,subname_loc6,name_loc7,subname_loc7,name_loc8,subname_loc8 FROM locales_creature");
508
509    if(!result)
510    {
511        barGoLink bar(1);
512
513        bar.step();
514
515        sLog.outString("");
516        sLog.outString(">> Loaded 0 creature locale strings. DB table `locales_creature` is empty.");
517        return;
518    }
519
520    barGoLink bar(result->GetRowCount());
521
522    do
523    {
524        Field *fields = result->Fetch();
525        bar.step();
526
527        uint32 entry = fields[0].GetUInt32();
528
529        CreatureLocale& data = mCreatureLocaleMap[entry];
530
531        for(int i = 1; i < MAX_LOCALE; ++i)
532        {
533            std::string str = fields[1+2*(i-1)].GetCppString();
534            if(!str.empty())
535            {
536                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
537                if(idx >= 0)
538                {
539                    if(data.Name.size() <= idx)
540                        data.Name.resize(idx+1);
541
542                    data.Name[idx] = str;
543                }
544            }
545            str = fields[1+2*(i-1)+1].GetCppString();
546            if(!str.empty())
547            {
548                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
549                if(idx >= 0)
550                {
551                    if(data.SubName.size() <= idx)
552                        data.SubName.resize(idx+1);
553
554                    data.SubName[idx] = str;
555                }
556            }
557        }
558    } while (result->NextRow());
559
560    delete result;
561
562    sLog.outString();
563    sLog.outString( ">> Loaded %u creature locale strings", mCreatureLocaleMap.size() );
564}
565
566void ObjectMgr::LoadCreatureTemplates()
567{
568    sCreatureStorage.Load();
569
570    sLog.outString( ">> Loaded %u creature definitions", sCreatureStorage.RecordCount );
571    sLog.outString();
572
573    std::set<uint32> heroicEntries;                         // already loaded heroic value in creatures
574    std::set<uint32> hasHeroicEntries;                      // already loaded creatures with heroic entry values
575
576    // check data correctness
577    for(uint32 i = 1; i < sCreatureStorage.MaxEntry; ++i)
578    {
579        CreatureInfo const* cInfo = sCreatureStorage.LookupEntry<CreatureInfo>(i);
580        if(!cInfo)
581            continue;
582
583        if(cInfo->HeroicEntry)
584        {
585            CreatureInfo const* heroicInfo = GetCreatureTemplate(cInfo->HeroicEntry);
586            if(!heroicInfo)
587            {
588                sLog.outErrorDb("Creature (Entry: %u) have `heroic_entry`=%u but creature entry %u not exist.",cInfo->HeroicEntry,cInfo->HeroicEntry);
589                continue;
590            }
591
592            if(heroicEntries.find(i)!=heroicEntries.end())
593            {
594                sLog.outErrorDb("Creature (Entry: %u) listed as heroic but have value in `heroic_entry`.",i);
595                continue;
596            }
597
598            if(heroicEntries.find(cInfo->HeroicEntry)!=heroicEntries.end())
599            {
600                sLog.outErrorDb("Creature (Entry: %u) already listed as heroic for another entry.",cInfo->HeroicEntry);
601                continue;
602            }
603
604            if(hasHeroicEntries.find(cInfo->HeroicEntry)!=hasHeroicEntries.end())
605            {
606                sLog.outErrorDb("Creature (Entry: %u) have `heroic_entry`=%u but creature entry %u have heroic entry also.",i,cInfo->HeroicEntry,cInfo->HeroicEntry);
607                continue;
608            }
609
610            if(cInfo->npcflag != heroicInfo->npcflag)
611            {
612                sLog.outErrorDb("Creature (Entry: %u) listed in `creature_template_substitution` has different `npcflag` in heroic mode.",i);
613                continue;
614            }
615
616            if(cInfo->classNum != heroicInfo->classNum)
617            {
618                sLog.outErrorDb("Creature (Entry: %u) listed in `creature_template_substitution` has different `classNum` in heroic mode.",i);
619                continue;
620            }
621
622            if(cInfo->race != heroicInfo->race)
623            {
624                sLog.outErrorDb("Creature (Entry: %u) listed in `creature_template_substitution` has different `race` in heroic mode.",i);
625                continue;
626            }
627
628            if(cInfo->trainer_type != heroicInfo->trainer_type)
629            {
630                sLog.outErrorDb("Creature (Entry: %u) listed in `creature_template_substitution` has different `trainer_type` in heroic mode.",i);
631                continue;
632            }
633
634            if(cInfo->trainer_spell != heroicInfo->trainer_spell)
635            {
636                sLog.outErrorDb("Creature (Entry: %u) listed in `creature_template_substitution` has different `trainer_spell` in heroic mode.",i);
637                continue;
638            }
639
640            hasHeroicEntries.insert(i);
641            heroicEntries.insert(cInfo->HeroicEntry);
642        }
643
644        FactionTemplateEntry const* factionTemplate = sFactionTemplateStore.LookupEntry(cInfo->faction_A);
645        if(!factionTemplate)
646            sLog.outErrorDb("Creature (Entry: %u) has non-existing faction_A template (%u)", cInfo->Entry, cInfo->faction_A);
647
648        factionTemplate = sFactionTemplateStore.LookupEntry(cInfo->faction_H);
649        if(!factionTemplate)
650            sLog.outErrorDb("Creature (Entry: %u) has non-existing faction_H template (%u)", cInfo->Entry, cInfo->faction_H);
651
652        CreatureModelInfo const* minfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(cInfo->DisplayID_A);
653        if (!minfo)
654            sLog.outErrorDb("Creature (Entry: %u) has non-existing modelId_A (%u)", cInfo->Entry, cInfo->DisplayID_A);
655        minfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(cInfo->DisplayID_H);
656        if (!minfo)
657            sLog.outErrorDb("Creature (Entry: %u) has non-existing modelId_H (%u)", cInfo->Entry, cInfo->DisplayID_H);
658
659        if(cInfo->dmgschool >= MAX_SPELL_SCHOOL)
660        {
661            sLog.outErrorDb("Creature (Entry: %u) has invalid spell school value (%u) in `dmgschool`",cInfo->Entry,cInfo->dmgschool);
662            const_cast<CreatureInfo*>(cInfo)->dmgschool = SPELL_SCHOOL_NORMAL;
663        }
664
665        if(cInfo->baseattacktime == 0)
666            const_cast<CreatureInfo*>(cInfo)->baseattacktime  = BASE_ATTACK_TIME;
667
668        if(cInfo->rangeattacktime == 0)
669            const_cast<CreatureInfo*>(cInfo)->rangeattacktime = BASE_ATTACK_TIME;
670
671        if((cInfo->npcflag & UNIT_NPC_FLAG_TRAINER) && cInfo->trainer_type >= MAX_TRAINER_TYPE)
672            sLog.outErrorDb("Creature (Entry: %u) has wrong trainer type %u",cInfo->Entry,cInfo->trainer_type);
673
674        if(cInfo->InhabitType <= 0 || cInfo->InhabitType > INHABIT_ANYWHERE)
675        {
676            sLog.outErrorDb("Creature (Entry: %u) has wrong value (%u) in `InhabitType`, creature will not correctly walk/swim/fly",cInfo->Entry,cInfo->InhabitType);
677            const_cast<CreatureInfo*>(cInfo)->InhabitType = INHABIT_ANYWHERE;
678        }
679
680        if(cInfo->PetSpellDataId)
681        {
682            CreatureSpellDataEntry const* spellDataId = sCreatureSpellDataStore.LookupEntry(cInfo->PetSpellDataId);
683            if(!spellDataId)
684                sLog.outErrorDb("Creature (Entry: %u) has non-existing PetSpellDataId (%u)", cInfo->Entry, cInfo->PetSpellDataId);
685        }
686
687        if(cInfo->MovementType >= MAX_DB_MOTION_TYPE)
688        {
689            sLog.outErrorDb("Creature (Entry: %u) has wrong movement generator type (%u), ignore and set to IDLE.",cInfo->Entry,cInfo->MovementType);
690            const_cast<CreatureInfo*>(cInfo)->MovementType = IDLE_MOTION_TYPE;
691        }
692
693        if(cInfo->equipmentId > 0)                          // 0 no equipment
694        {
695            if(!GetEquipmentInfo(cInfo->equipmentId))
696            {
697                sLog.outErrorDb("Table `creature_template` have creature (Entry: %u) with equipment_id %u not found in table `creature_equip_template`, set to no equipment.", cInfo->Entry, cInfo->equipmentId);
698                const_cast<CreatureInfo*>(cInfo)->equipmentId = 0;
699            }
700        }
701
702        /// if not set custom creature scale then load scale from CreatureDisplayInfo.dbc
703        if(cInfo->scale <= 0.0f)
704        {
705            CreatureDisplayInfoEntry const* ScaleEntry = sCreatureDisplayInfoStore.LookupEntry(cInfo->DisplayID_A);
706            const_cast<CreatureInfo*>(cInfo)->scale = ScaleEntry ? ScaleEntry->scale : 1.0f;
707        }
708    }
709}
710
711void ObjectMgr::ConvertCreatureAddonAuras(CreatureDataAddon* addon, char const* table, char const* guidEntryStr)
712{
713    // Now add the auras, format "spellid effectindex spellid effectindex..."
714    char *p,*s;
715    std::vector<int> val;
716    s=p=(char*)reinterpret_cast<char const*>(addon->auras);
717    if(p)
718    {
719        while (p[0]!=0)
720        {
721            ++p;
722            if (p[0]==' ')
723            {
724                val.push_back(atoi(s));
725                s=++p;
726            }
727        }
728        if (p!=s)
729            val.push_back(atoi(s));
730
731        // free char* loaded memory
732        delete[] (char*)reinterpret_cast<char const*>(addon->auras);
733
734        // wrong list
735        if (val.size()%2)
736        {
737            addon->auras = NULL;
738            sLog.outErrorDb("Creature (%s: %u) has wrong `auras` data in `%s`.",guidEntryStr,addon->guidOrEntry,table);
739            return;
740        }
741    }
742
743    // empty list
744    if(val.empty())
745    {
746        addon->auras = NULL;
747        return;
748    }
749
750    // replace by new strucutres array
751    const_cast<CreatureDataAddonAura*&>(addon->auras) = new CreatureDataAddonAura[val.size()/2+1];
752
753    int i=0;
754    for(int j=0;j<val.size()/2;++j)
755    {
756        CreatureDataAddonAura& cAura = const_cast<CreatureDataAddonAura&>(addon->auras[i]);
757        cAura.spell_id = (uint32)val[2*j+0];
758        cAura.effect_idx  = (uint32)val[2*j+1];
759        if ( cAura.effect_idx > 2 )
760        {
761            sLog.outErrorDb("Creature (%s: %u) has wrong effect %u for spell %u in `auras` field in `%s`.",guidEntryStr,addon->guidOrEntry,cAura.effect_idx,cAura.spell_id,table);
762            continue;
763        }
764        SpellEntry const *AdditionalSpellInfo = sSpellStore.LookupEntry(cAura.spell_id);
765        if (!AdditionalSpellInfo)
766        {
767            sLog.outErrorDb("Creature (%s: %u) has wrong spell %u defined in `auras` field in `%s`.",guidEntryStr,addon->guidOrEntry,cAura.spell_id,table);
768            continue;
769        }
770
771        if (!AdditionalSpellInfo->Effect[cAura.effect_idx] || !AdditionalSpellInfo->EffectApplyAuraName[cAura.effect_idx])
772        {
773            sLog.outErrorDb("Creature (%s: %u) has not aura effect %u of spell %u defined in `auras` field in `%s`.",guidEntryStr,addon->guidOrEntry,cAura.effect_idx,cAura.spell_id,table);
774            continue;
775        }
776
777        ++i;
778    }
779
780    // fill terminator element (after last added)
781    CreatureDataAddonAura& endAura = const_cast<CreatureDataAddonAura&>(addon->auras[i]);
782    endAura.spell_id   = 0;
783    endAura.effect_idx = 0;
784}
785
786void ObjectMgr::LoadCreatureAddons()
787{
788    sCreatureInfoAddonStorage.Load();
789
790    sLog.outString( ">> Loaded %u creature template addons", sCreatureInfoAddonStorage.RecordCount );
791    sLog.outString();
792
793    // check data correctness and convert 'auras'
794    for(uint32 i = 1; i < sCreatureInfoAddonStorage.MaxEntry; ++i)
795    {
796        CreatureDataAddon const* addon = sCreatureInfoAddonStorage.LookupEntry<CreatureDataAddon>(i);
797        if(!addon)
798            continue;
799
800        ConvertCreatureAddonAuras(const_cast<CreatureDataAddon*>(addon), "creature_template_addon", "Entry");
801
802        if(!sCreatureStorage.LookupEntry<CreatureInfo>(addon->guidOrEntry))
803            sLog.outErrorDb("Creature (Entry: %u) does not exist but has a record in `creature_template_addon`",addon->guidOrEntry);
804    }
805
806    sCreatureDataAddonStorage.Load();
807
808    sLog.outString( ">> Loaded %u creature addons", sCreatureDataAddonStorage.RecordCount );
809    sLog.outString();
810
811    // check data correctness and convert 'auras'
812    for(uint32 i = 1; i < sCreatureDataAddonStorage.MaxEntry; ++i)
813    {
814        CreatureDataAddon const* addon = sCreatureDataAddonStorage.LookupEntry<CreatureDataAddon>(i);
815        if(!addon)
816            continue;
817
818        ConvertCreatureAddonAuras(const_cast<CreatureDataAddon*>(addon), "creature_addon", "GUIDLow");
819
820        if(mCreatureDataMap.find(addon->guidOrEntry)==mCreatureDataMap.end())
821            sLog.outErrorDb("Creature (GUID: %u) does not exist but has a record in `creature_addon`",addon->guidOrEntry);
822    }
823}
824
825EquipmentInfo const* ObjectMgr::GetEquipmentInfo(uint32 entry)
826{
827    return sEquipmentStorage.LookupEntry<EquipmentInfo>(entry);
828}
829
830void ObjectMgr::LoadEquipmentTemplates()
831{
832    sEquipmentStorage.Load();
833
834    sLog.outString( ">> Loaded %u equipment template", sEquipmentStorage.RecordCount );
835    sLog.outString();
836}
837
838CreatureModelInfo const* ObjectMgr::GetCreatureModelInfo(uint32 modelid)
839{
840    return sCreatureModelStorage.LookupEntry<CreatureModelInfo>(modelid);
841}
842
843uint32 ObjectMgr::ChooseDisplayId(uint32 team, const CreatureInfo *cinfo, const CreatureData *data)
844{
845    // Load creature model (display id)
846    uint32 display_id;
847    if (!data || data->displayid == 0)                      // use defaults from the template
848    {
849        // DisplayID_A is used if no team is given
850        if (team == HORDE)
851            display_id = (cinfo->DisplayID_H2 != 0 && urand(0,1) == 0) ? cinfo->DisplayID_H2 : cinfo->DisplayID_H;
852        else
853            display_id = (cinfo->DisplayID_A2 != 0 && urand(0,1) == 0) ? cinfo->DisplayID_A2 : cinfo->DisplayID_A;
854    }
855    else                                                    // overriden in creature data
856        display_id = data->displayid;
857
858    return display_id;
859}
860
861CreatureModelInfo const* ObjectMgr::GetCreatureModelRandomGender(uint32 display_id)
862{
863    CreatureModelInfo const *minfo = GetCreatureModelInfo(display_id);
864    if(!minfo)
865        return NULL;
866
867    // If a model for another gender exists, 50% chance to use it
868    if(minfo->modelid_other_gender != 0 && urand(0,1) == 0)
869    {
870        CreatureModelInfo const *minfo_tmp = GetCreatureModelInfo(minfo->modelid_other_gender);
871        if(!minfo_tmp)
872        {
873            sLog.outErrorDb("Model (Entry: %u) has modelid_other_gender %u not found in table `creature_model_info`. ", minfo->modelid, minfo->modelid_other_gender);
874            return minfo;                                   // not fatal, just use the previous one
875        }
876        else
877            return minfo_tmp;
878    }
879    else
880        return minfo;
881}
882
883void ObjectMgr::LoadCreatureModelInfo()
884{
885    sCreatureModelStorage.Load();
886
887    sLog.outString( ">> Loaded %u creature model based info", sCreatureModelStorage.RecordCount );
888    sLog.outString();
889}
890
891void ObjectMgr::LoadCreatures()
892{
893    uint32 count = 0;
894    //                                                0              1   2    3
895    QueryResult *result = WorldDatabase.Query("SELECT creature.guid, id, map, modelid,"
896    //   4             5           6           7           8            9              10         11
897        "equipment_id, position_x, position_y, position_z, orientation, spawntimesecs, spawndist, currentwaypoint,"
898    //   12         13       14          15            16         17
899        "curhealth, curmana, DeathState, MovementType, spawnMask, event "
900        "FROM creature LEFT OUTER JOIN game_event_creature ON creature.guid = game_event_creature.guid");
901
902    if(!result)
903    {
904        barGoLink bar(1);
905
906        bar.step();
907
908        sLog.outString("");
909        sLog.outErrorDb(">> Loaded 0 creature. DB table `creature` is empty.");
910        return;
911    }
912
913    // build single time for check creature data
914    std::set<uint32> heroicCreatures;
915    for(uint32 i = 0; i < sCreatureStorage.MaxEntry; ++i)
916        if(CreatureInfo const* cInfo = sCreatureStorage.LookupEntry<CreatureInfo>(i))
917            if(cInfo->HeroicEntry)
918                heroicCreatures.insert(cInfo->HeroicEntry);
919
920    barGoLink bar(result->GetRowCount());
921
922    do
923    {
924        Field *fields = result->Fetch();
925        bar.step();
926
927        uint32 guid = fields[0].GetUInt32();
928
929        CreatureData& data = mCreatureDataMap[guid];
930
931        data.id             = fields[ 1].GetUInt32();
932        data.mapid          = fields[ 2].GetUInt32();
933        data.displayid      = fields[ 3].GetUInt32();
934        data.equipmentId    = fields[ 4].GetUInt32();
935        data.posX           = fields[ 5].GetFloat();
936        data.posY           = fields[ 6].GetFloat();
937        data.posZ           = fields[ 7].GetFloat();
938        data.orientation    = fields[ 8].GetFloat();
939        data.spawntimesecs  = fields[ 9].GetUInt32();
940        data.spawndist      = fields[10].GetFloat();
941        data.currentwaypoint= fields[11].GetUInt32();
942        data.curhealth      = fields[12].GetUInt32();
943        data.curmana        = fields[13].GetUInt32();
944        data.is_dead        = fields[14].GetBool();
945        data.movementType   = fields[15].GetUInt8();
946        data.spawnMask      = fields[16].GetUInt8();
947        int16 gameEvent     = fields[17].GetInt16();
948
949        CreatureInfo const* cInfo = GetCreatureTemplate(data.id);
950        if(!cInfo)
951        {
952            sLog.outErrorDb("Table `creature` have creature (GUID: %u) with not existed creature entry %u, skipped.",guid,data.id );
953            continue;
954        }
955
956        if(heroicCreatures.find(data.id)!=heroicCreatures.end())
957        {
958            sLog.outErrorDb("Table `creature` have creature (GUID: %u) that listed as heroic template in `creature_template_substitution`, skipped.",guid,data.id );
959            continue;
960        }
961
962        if(data.equipmentId > 0)                            // -1 no equipment, 0 use default
963        {
964            if(!GetEquipmentInfo(data.equipmentId))
965            {
966                sLog.outErrorDb("Table `creature` have creature (Entry: %u) with equipment_id %u not found in table `creature_equip_template`, set to no equipment.", data.id, data.equipmentId);
967                data.equipmentId = -1;
968            }
969        }
970
971        if(cInfo->RegenHealth && data.curhealth < cInfo->minhealth)
972        {
973            sLog.outErrorDb("Table `creature` have creature (GUID: %u Entry: %u) with `creature_template`.`RegenHealth`=1 and low current health (%u), `creature_template`.`minhealth`=%u.",guid,data.id,data.curhealth, cInfo->minhealth );
974            data.curhealth = cInfo->minhealth;
975        }
976
977        if(data.curmana < cInfo->minmana)
978        {
979            sLog.outErrorDb("Table `creature` have creature (GUID: %u Entry: %u) with low current mana (%u), `creature_template`.`minmana`=%u.",guid,data.id,data.curmana, cInfo->minmana );
980            data.curmana = cInfo->minmana;
981        }
982
983        if(data.spawndist < 0.0f)
984        {
985            sLog.outErrorDb("Table `creature` have creature (GUID: %u Entry: %u) with `spawndist`< 0, set to 0.",guid,data.id );
986            data.spawndist = 0.0f;
987        }
988        else if(data.movementType == RANDOM_MOTION_TYPE)
989        {
990            if(data.spawndist == 0.0f)
991            {
992                sLog.outErrorDb("Table `creature` have creature (GUID: %u Entry: %u) with `MovementType`=1 (random movement) but with `spawndist`=0, replace by idle movement type (0).",guid,data.id );
993                data.movementType = IDLE_MOTION_TYPE;
994            }
995        }
996        else if(data.movementType == IDLE_MOTION_TYPE)
997        {
998            if(data.spawndist != 0.0f)
999            {
1000                sLog.outErrorDb("Table `creature` have creature (GUID: %u Entry: %u) with `MovementType`=0 (idle) have `spawndist`<>0, set to 0.",guid,data.id );
1001                data.spawndist = 0.0f;
1002            }
1003        }
1004
1005        if (gameEvent==0)                                   // if not this is to be managed by GameEvent System
1006            AddCreatureToGrid(guid, &data);
1007        ++count;
1008
1009    } while (result->NextRow());
1010
1011    delete result;
1012
1013    sLog.outString();
1014    sLog.outString( ">> Loaded %u creatures", mCreatureDataMap.size() );
1015}
1016
1017void ObjectMgr::AddCreatureToGrid(uint32 guid, CreatureData const* data)
1018{
1019    uint8 mask = data->spawnMask;
1020    for(uint8 i = 0; mask != 0; i++, mask >>= 1)
1021    {
1022        if(mask & 1)
1023        {
1024            CellPair cell_pair = Trinity::ComputeCellPair(data->posX, data->posY);
1025            uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
1026
1027            CellObjectGuids& cell_guids = mMapObjectGuids[MAKE_PAIR32(data->mapid,i)][cell_id];
1028            cell_guids.creatures.insert(guid);
1029        }
1030    }
1031}
1032
1033void ObjectMgr::RemoveCreatureFromGrid(uint32 guid, CreatureData const* data)
1034{
1035    uint8 mask = data->spawnMask;
1036    for(uint8 i = 0; mask != 0; i++, mask >>= 1)
1037    {
1038        if(mask & 1)
1039        {
1040            CellPair cell_pair = Trinity::ComputeCellPair(data->posX, data->posY);
1041            uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
1042
1043            CellObjectGuids& cell_guids = mMapObjectGuids[MAKE_PAIR32(data->mapid,i)][cell_id];
1044            cell_guids.creatures.erase(guid);
1045        }
1046    }
1047}
1048
1049void ObjectMgr::LoadGameobjects()
1050{
1051    uint32 count = 0;
1052
1053    //                                                0                1   2    3           4           5           6
1054    QueryResult *result = WorldDatabase.Query("SELECT gameobject.guid, id, map, position_x, position_y, position_z, orientation,"
1055    //   7          8          9          10         11             12            13     14         15
1056        "rotation0, rotation1, rotation2, rotation3, spawntimesecs, animprogress, state, spawnMask, event "
1057        "FROM gameobject LEFT OUTER JOIN game_event_gameobject ON gameobject.guid = game_event_gameobject.guid");
1058
1059    if(!result)
1060    {
1061        barGoLink bar(1);
1062
1063        bar.step();
1064
1065        sLog.outString();
1066        sLog.outErrorDb(">> Loaded 0 gameobjects. DB table `gameobject` is empty.");
1067        return;
1068    }
1069
1070    barGoLink bar(result->GetRowCount());
1071
1072    do
1073    {
1074        Field *fields = result->Fetch();
1075        bar.step();
1076
1077        uint32 guid = fields[0].GetUInt32();
1078
1079        GameObjectData& data = mGameObjectDataMap[guid];
1080
1081        data.id             = fields[ 1].GetUInt32();
1082        data.mapid          = fields[ 2].GetUInt32();
1083        data.posX           = fields[ 3].GetFloat();
1084        data.posY           = fields[ 4].GetFloat();
1085        data.posZ           = fields[ 5].GetFloat();
1086        data.orientation    = fields[ 6].GetFloat();
1087        data.rotation0      = fields[ 7].GetFloat();
1088        data.rotation1      = fields[ 8].GetFloat();
1089        data.rotation2      = fields[ 9].GetFloat();
1090        data.rotation3      = fields[10].GetFloat();
1091        data.spawntimesecs  = fields[11].GetInt32();
1092        data.animprogress   = fields[12].GetUInt32();
1093        data.go_state       = fields[13].GetUInt32();
1094        data.ArtKit         = 0;
1095        data.spawnMask      = fields[14].GetUInt8();
1096        int16 gameEvent     = fields[15].GetInt16();
1097
1098        GameObjectInfo const* gInfo = GetGameObjectInfo(data.id);
1099        if(!gInfo)
1100        {
1101            sLog.outErrorDb("Table `gameobject` have gameobject (GUID: %u) with not existed gameobject entry %u, skipped.",guid,data.id );
1102            continue;
1103        }
1104
1105        if (gameEvent==0)                                   // if not this is to be managed by GameEvent System
1106            AddGameobjectToGrid(guid, &data);
1107        ++count;
1108
1109    } while (result->NextRow());
1110
1111    delete result;
1112
1113    sLog.outString();
1114    sLog.outString( ">> Loaded %u gameobjects", mGameObjectDataMap.size());
1115}
1116
1117void ObjectMgr::AddGameobjectToGrid(uint32 guid, GameObjectData const* data)
1118{
1119    uint8 mask = data->spawnMask;
1120    for(uint8 i = 0; mask != 0; i++, mask >>= 1)
1121    {
1122        if(mask & 1)
1123        {
1124            CellPair cell_pair = Trinity::ComputeCellPair(data->posX, data->posY);
1125            uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
1126
1127            CellObjectGuids& cell_guids = mMapObjectGuids[MAKE_PAIR32(data->mapid,i)][cell_id];
1128            cell_guids.gameobjects.insert(guid);
1129        }
1130    }
1131}
1132
1133void ObjectMgr::RemoveGameobjectFromGrid(uint32 guid, GameObjectData const* data)
1134{
1135    uint8 mask = data->spawnMask;
1136    for(uint8 i = 0; mask != 0; i++, mask >>= 1)
1137    {
1138        if(mask & 1)
1139        {
1140            CellPair cell_pair = Trinity::ComputeCellPair(data->posX, data->posY);
1141            uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
1142
1143            CellObjectGuids& cell_guids = mMapObjectGuids[MAKE_PAIR32(data->mapid,i)][cell_id];
1144            cell_guids.gameobjects.erase(guid);
1145        }
1146    }
1147}
1148
1149void ObjectMgr::LoadCreatureRespawnTimes()
1150{
1151    // remove outdated data
1152    WorldDatabase.DirectExecute("DELETE FROM creature_respawn WHERE respawntime <= UNIX_TIMESTAMP(NOW())");
1153
1154    uint32 count = 0;
1155
1156    QueryResult *result = WorldDatabase.Query("SELECT guid,respawntime,instance FROM creature_respawn");
1157
1158    if(!result)
1159    {
1160        barGoLink bar(1);
1161
1162        bar.step();
1163
1164        sLog.outString();
1165        sLog.outString(">> Loaded 0 creature respawn time.");
1166        return;
1167    }
1168
1169    barGoLink bar(result->GetRowCount());
1170
1171    do
1172    {
1173        Field *fields = result->Fetch();
1174        bar.step();
1175
1176        uint32 loguid       = fields[0].GetUInt32();
1177        uint64 respawn_time = fields[1].GetUInt64();
1178        uint32 instance     = fields[2].GetUInt32();
1179
1180        mCreatureRespawnTimes[MAKE_PAIR64(loguid,instance)] = time_t(respawn_time);
1181
1182        ++count;
1183    } while (result->NextRow());
1184
1185    delete result;
1186
1187    sLog.outString( ">> Loaded %u creature respawn times", mCreatureRespawnTimes.size() );
1188    sLog.outString();
1189}
1190
1191void ObjectMgr::LoadGameobjectRespawnTimes()
1192{
1193    // remove outdated data
1194    WorldDatabase.DirectExecute("DELETE FROM gameobject_respawn WHERE respawntime <= UNIX_TIMESTAMP(NOW())");
1195
1196    uint32 count = 0;
1197
1198    QueryResult *result = WorldDatabase.Query("SELECT guid,respawntime,instance FROM gameobject_respawn");
1199
1200    if(!result)
1201    {
1202        barGoLink bar(1);
1203
1204        bar.step();
1205
1206        sLog.outString();
1207        sLog.outString(">> Loaded 0 gameobject respawn time.");
1208        return;
1209    }
1210
1211    barGoLink bar(result->GetRowCount());
1212
1213    do
1214    {
1215        Field *fields = result->Fetch();
1216        bar.step();
1217
1218        uint32 loguid       = fields[0].GetUInt32();
1219        uint64 respawn_time = fields[1].GetUInt64();
1220        uint32 instance     = fields[2].GetUInt32();
1221
1222        mGORespawnTimes[MAKE_PAIR64(loguid,instance)] = time_t(respawn_time);
1223
1224        ++count;
1225    } while (result->NextRow());
1226
1227    delete result;
1228
1229    sLog.outString( ">> Loaded %u gameobject respawn times", mGORespawnTimes.size() );
1230    sLog.outString();
1231}
1232
1233// name must be checked to correctness (if received) before call this function
1234uint64 ObjectMgr::GetPlayerGUIDByName(std::string name) const
1235{
1236    uint64 guid = 0;
1237
1238    CharacterDatabase.escape_string(name);
1239
1240    // Player name safe to sending to DB (checked at login) and this function using
1241    QueryResult *result = CharacterDatabase.PQuery("SELECT guid FROM characters WHERE name = '%s'", name.c_str());
1242    if(result)
1243    {
1244        guid = MAKE_NEW_GUID((*result)[0].GetUInt32(), 0, HIGHGUID_PLAYER);
1245
1246        delete result;
1247    }
1248
1249    return guid;
1250}
1251
1252bool ObjectMgr::GetPlayerNameByGUID(const uint64 &guid, std::string &name) const
1253{
1254    // prevent DB access for online player
1255    if(Player* player = GetPlayer(guid))
1256    {
1257        name = player->GetName();
1258        return true;
1259    }
1260
1261    QueryResult *result = CharacterDatabase.PQuery("SELECT name FROM characters WHERE guid = '%u'", GUID_LOPART(guid));
1262
1263    if(result)
1264    {
1265        name = (*result)[0].GetCppString();
1266        delete result;
1267        return true;
1268    }
1269
1270    return false;
1271}
1272
1273uint32 ObjectMgr::GetPlayerTeamByGUID(const uint64 &guid) const
1274{
1275    QueryResult *result = CharacterDatabase.PQuery("SELECT race FROM characters WHERE guid = '%u'", GUID_LOPART(guid));
1276
1277    if(result)
1278    {
1279        uint8 race = (*result)[0].GetUInt8();
1280        delete result;
1281        return Player::TeamForRace(race);
1282    }
1283
1284    return 0;
1285}
1286
1287uint32 ObjectMgr::GetPlayerAccountIdByGUID(const uint64 &guid) const
1288{
1289    QueryResult *result = CharacterDatabase.PQuery("SELECT account FROM characters WHERE guid = '%u'", GUID_LOPART(guid));
1290    if(result)
1291    {
1292        uint32 acc = (*result)[0].GetUInt32();
1293        delete result;
1294        return acc;
1295    }
1296
1297    return 0;
1298}
1299
1300uint32 ObjectMgr::GetSecurityByAccount(uint32 acc_id) const
1301{
1302    QueryResult *result = loginDatabase.PQuery("SELECT gmlevel FROM account WHERE id = '%u'", acc_id);
1303    if(result)
1304    {
1305        uint32 sec = (*result)[0].GetUInt32();
1306        delete result;
1307        return sec;
1308    }
1309
1310    return 0;
1311}
1312
1313bool ObjectMgr::GetAccountNameByAccount(uint32 acc_id, std::string &name) const
1314{
1315    QueryResult *result = loginDatabase.PQuery("SELECT username FROM account WHERE id = '%u'", acc_id);
1316    if(result)
1317    {
1318        name = (*result)[0].GetCppString();
1319        delete result;
1320        return true;
1321    }
1322
1323    return false;
1324}
1325
1326uint32 ObjectMgr::GetAccountByAccountName(std::string name) const
1327{
1328    loginDatabase.escape_string(name);
1329    QueryResult *result = loginDatabase.PQuery("SELECT id FROM account WHERE username = '%s'", name.c_str());
1330    if(result)
1331    {
1332        uint32 id = (*result)[0].GetUInt32();
1333        delete result;
1334        return id;
1335    }
1336
1337    return 0;
1338}
1339
1340void ObjectMgr::LoadAuctions()
1341{
1342    QueryResult *result = CharacterDatabase.Query("SELECT COUNT(*) FROM auctionhouse");
1343    if( !result )
1344        return;
1345
1346    Field *fields = result->Fetch();
1347    uint32 AuctionCount=fields[0].GetUInt32();
1348    delete result;
1349
1350    if(!AuctionCount)
1351        return;
1352
1353    result = CharacterDatabase.Query( "SELECT id,auctioneerguid,itemguid,item_template,itemowner,buyoutprice,time,buyguid,lastbid,startbid,deposit,location FROM auctionhouse" );
1354    if( !result )
1355        return;
1356
1357    barGoLink bar( AuctionCount );
1358
1359    AuctionEntry *aItem;
1360
1361    do
1362    {
1363        fields = result->Fetch();
1364
1365        bar.step();
1366
1367        aItem = new AuctionEntry;
1368        aItem->Id = fields[0].GetUInt32();
1369        aItem->auctioneer = fields[1].GetUInt32();
1370        aItem->item_guidlow = fields[2].GetUInt32();
1371        aItem->item_template = fields[3].GetUInt32();
1372        aItem->owner = fields[4].GetUInt32();
1373        aItem->buyout = fields[5].GetUInt32();
1374        aItem->time = fields[6].GetUInt32();
1375        aItem->bidder = fields[7].GetUInt32();
1376        aItem->bid = fields[8].GetUInt32();
1377        aItem->startbid = fields[9].GetUInt32();
1378        aItem->deposit = fields[10].GetUInt32();
1379        aItem->location = fields[11].GetUInt8();
1380        //check if sold item exists
1381        if ( this->GetAItem( aItem->item_guidlow ) )
1382        {
1383            GetAuctionsMap( aItem->location )->AddAuction(aItem);
1384        }
1385        else
1386        {
1387            CharacterDatabase.PExecute("DELETE FROM auctionhouse WHERE id = '%u'",aItem->Id);
1388            sLog.outError("Auction %u has not a existing item : %u", aItem->Id, aItem->item_guidlow);
1389            delete aItem;
1390        }
1391    } while (result->NextRow());
1392    delete result;
1393
1394    sLog.outString();
1395    sLog.outString( ">> Loaded %u auctions", AuctionCount );
1396    sLog.outString();
1397}
1398
1399void ObjectMgr::LoadItemLocales()
1400{
1401    QueryResult *result = WorldDatabase.Query("SELECT entry,name_loc1,description_loc1,name_loc2,description_loc2,name_loc3,description_loc3,name_loc4,description_loc4,name_loc5,description_loc5,name_loc6,description_loc6,name_loc7,description_loc7,name_loc8,description_loc8 FROM locales_item");
1402
1403    if(!result)
1404    {
1405        barGoLink bar(1);
1406
1407        bar.step();
1408
1409        sLog.outString("");
1410        sLog.outString(">> Loaded 0 Item locale strings. DB table `locales_item` is empty.");
1411        return;
1412    }
1413
1414    barGoLink bar(result->GetRowCount());
1415
1416    do
1417    {
1418        Field *fields = result->Fetch();
1419        bar.step();
1420
1421        uint32 entry = fields[0].GetUInt32();
1422
1423        ItemLocale& data = mItemLocaleMap[entry];
1424
1425        for(int i = 1; i < MAX_LOCALE; ++i)
1426        {
1427            std::string str = fields[1+2*(i-1)].GetCppString();
1428            if(!str.empty())
1429            {
1430                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
1431                if(idx >= 0)
1432                {
1433                    if(data.Name.size() <= idx)
1434                        data.Name.resize(idx+1);
1435
1436                    data.Name[idx] = str;
1437                }
1438            }
1439
1440            str = fields[1+2*(i-1)+1].GetCppString();
1441            if(!str.empty())
1442            {
1443                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
1444                if(idx >= 0)
1445                {
1446                    if(data.Description.size() <= idx)
1447                        data.Description.resize(idx+1);
1448
1449                    data.Description[idx] = str;
1450                }
1451            }
1452        }
1453    } while (result->NextRow());
1454
1455    delete result;
1456
1457    sLog.outString();
1458    sLog.outString( ">> Loaded %u Item locale strings", mItemLocaleMap.size() );
1459}
1460
1461void ObjectMgr::LoadItemPrototypes()
1462{
1463    sItemStorage.Load ();
1464    sLog.outString( ">> Loaded %u item prototypes", sItemStorage.RecordCount );
1465    sLog.outString();
1466
1467    // check data correctness
1468    for(uint32 i = 1; i < sItemStorage.MaxEntry; ++i)
1469    {
1470        ItemPrototype const* proto = sItemStorage.LookupEntry<ItemPrototype >(i);
1471        ItemEntry const *dbcitem = sItemStore.LookupEntry(i);
1472        if(!proto)
1473        {
1474            /* to many errors, and possible not all items really used in game
1475            if (dbcitem)
1476                sLog.outErrorDb("Item (Entry: %u) doesn't exists in DB, but must exist.",i);
1477            */
1478            continue;
1479        }
1480
1481        if(dbcitem)
1482        {
1483            if(proto->InventoryType != dbcitem->InventoryType)
1484            {
1485                sLog.outErrorDb("Item (Entry: %u) not correct %u inventory type, must be %u (still using DB value).",i,proto->InventoryType,dbcitem->InventoryType);
1486                // It safe let use InventoryType from DB
1487            }
1488
1489            if(proto->DisplayInfoID != dbcitem->DisplayId)
1490            {
1491                sLog.outErrorDb("Item (Entry: %u) not correct %u display id, must be %u (using it).",i,proto->DisplayInfoID,dbcitem->DisplayId);
1492                const_cast<ItemPrototype*>(proto)->DisplayInfoID = dbcitem->DisplayId;
1493            }
1494            if(proto->Sheath != dbcitem->Sheath)
1495            {
1496                sLog.outErrorDb("Item (Entry: %u) not correct %u sheath, must be %u  (using it).",i,proto->Sheath,dbcitem->Sheath);
1497                const_cast<ItemPrototype*>(proto)->Sheath = dbcitem->Sheath;
1498            }
1499        }
1500        else
1501        {
1502            sLog.outErrorDb("Item (Entry: %u) not correct (not listed in list of existed items).",i);
1503        }
1504
1505        if(proto->Class >= MAX_ITEM_CLASS)
1506        {
1507            sLog.outErrorDb("Item (Entry: %u) has wrong Class value (%u)",i,proto->Class);
1508            const_cast<ItemPrototype*>(proto)->Class = ITEM_CLASS_JUNK;
1509        }
1510
1511        if(proto->SubClass >= MaxItemSubclassValues[proto->Class])
1512        {
1513            sLog.outErrorDb("Item (Entry: %u) has wrong Subclass value (%u) for class %u",i,proto->SubClass,proto->Class);
1514            const_cast<ItemPrototype*>(proto)->SubClass = 0;// exist for all item classes
1515        }
1516
1517        if(proto->Quality >= MAX_ITEM_QUALITY)
1518        {
1519            sLog.outErrorDb("Item (Entry: %u) has wrong Quality value (%u)",i,proto->Quality);
1520            const_cast<ItemPrototype*>(proto)->Quality = ITEM_QUALITY_NORMAL;
1521        }
1522
1523        if(proto->BuyCount <= 0)
1524        {
1525            sLog.outErrorDb("Item (Entry: %u) has wrong BuyCount value (%u), set to default(1).",i,proto->BuyCount);
1526            const_cast<ItemPrototype*>(proto)->BuyCount = 1;
1527        }
1528
1529        if(proto->InventoryType >= MAX_INVTYPE)
1530        {
1531            sLog.outErrorDb("Item (Entry: %u) has wrong InventoryType value (%u)",i,proto->InventoryType);
1532            const_cast<ItemPrototype*>(proto)->InventoryType = INVTYPE_NON_EQUIP;
1533        }
1534
1535        if(proto->RequiredSkill >= MAX_SKILL_TYPE)
1536        {
1537            sLog.outErrorDb("Item (Entry: %u) has wrong RequiredSkill value (%u)",i,proto->RequiredSkill);
1538            const_cast<ItemPrototype*>(proto)->RequiredSkill = 0;
1539        }
1540
1541        if(!(proto->AllowableClass & CLASSMASK_ALL_PLAYABLE))
1542        {
1543            sLog.outErrorDb("Item (Entry: %u) not have in `AllowableClass` any playable classes (%u) and can't be equipped.",i,proto->AllowableClass);
1544        }
1545
1546        if(!(proto->AllowableRace & RACEMASK_ALL_PLAYABLE))
1547        {
1548            sLog.outErrorDb("Item (Entry: %u) not have in `AllowableRace` any playable races (%u) and can't be equipped.",i,proto->AllowableRace);
1549        }
1550
1551        if(proto->RequiredSpell && !sSpellStore.LookupEntry(proto->RequiredSpell))
1552        {
1553            sLog.outErrorDb("Item (Entry: %u) have wrong (non-existed) spell in RequiredSpell (%u)",i,proto->RequiredSpell);
1554            const_cast<ItemPrototype*>(proto)->RequiredSpell = 0;
1555        }
1556
1557        if(proto->RequiredReputationRank >= MAX_REPUTATION_RANK)
1558            sLog.outErrorDb("Item (Entry: %u) has wrong reputation rank in RequiredReputationRank (%u), item can't be used.",i,proto->RequiredReputationRank);
1559
1560        if(proto->RequiredReputationFaction)
1561        {
1562            if(!sFactionStore.LookupEntry(proto->RequiredReputationFaction))
1563            {
1564                sLog.outErrorDb("Item (Entry: %u) has wrong (not existing) faction in RequiredReputationFaction (%u)",i,proto->RequiredReputationFaction);
1565                const_cast<ItemPrototype*>(proto)->RequiredReputationFaction = 0;
1566            }
1567
1568            if(proto->RequiredReputationRank == MIN_REPUTATION_RANK)
1569                sLog.outErrorDb("Item (Entry: %u) has min. reputation rank in RequiredReputationRank (0) but RequiredReputationFaction > 0, faction setting is useless.",i);
1570        }
1571        else if(proto->RequiredReputationRank > MIN_REPUTATION_RANK)
1572            sLog.outErrorDb("Item (Entry: %u) has RequiredReputationFaction ==0 but RequiredReputationRank > 0, rank setting is useless.",i);
1573
1574        if(proto->Stackable==0)
1575        {
1576            sLog.outErrorDb("Item (Entry: %u) has wrong value in stackable (%u), replace by default 1.",i,proto->Stackable);
1577            const_cast<ItemPrototype*>(proto)->Stackable = 1;
1578        }
1579        else if(proto->Stackable > 255)
1580        {
1581            sLog.outErrorDb("Item (Entry: %u) has too large value in stackable (%u), replace by hardcoded upper limit (255).",i,proto->Stackable);
1582            const_cast<ItemPrototype*>(proto)->Stackable = 255;
1583        }
1584
1585        for (int j = 0; j < 10; j++)
1586        {
1587            // for ItemStatValue != 0
1588            if(proto->ItemStat[j].ItemStatValue && proto->ItemStat[j].ItemStatType >= MAX_ITEM_MOD)
1589            {
1590                sLog.outErrorDb("Item (Entry: %u) has wrong stat_type%d (%u)",i,j+1,proto->ItemStat[j].ItemStatType);
1591                const_cast<ItemPrototype*>(proto)->ItemStat[j].ItemStatType = 0;
1592            }
1593        }
1594
1595        for (int j = 0; j < 5; j++)
1596        {
1597            if(proto->Damage[j].DamageType >= MAX_SPELL_SCHOOL)
1598            {
1599                sLog.outErrorDb("Item (Entry: %u) has wrong dmg_type%d (%u)",i,j+1,proto->Damage[j].DamageType);
1600                const_cast<ItemPrototype*>(proto)->Damage[j].DamageType = 0;
1601            }
1602        }
1603
1604        // special format
1605        if(proto->Spells[0].SpellId == SPELL_ID_GENERIC_LEARN)
1606        {
1607            // spell_1
1608            if(proto->Spells[0].SpellTrigger != ITEM_SPELLTRIGGER_ON_USE)
1609            {
1610                sLog.outErrorDb("Item (Entry: %u) has wrong item spell trigger value in spelltrigger_%d (%u) for special learning format",i,0+1,proto->Spells[0].SpellTrigger);
1611                const_cast<ItemPrototype*>(proto)->Spells[0].SpellId = 0;
1612                const_cast<ItemPrototype*>(proto)->Spells[0].SpellTrigger = ITEM_SPELLTRIGGER_ON_USE;
1613                const_cast<ItemPrototype*>(proto)->Spells[1].SpellId = 0;
1614                const_cast<ItemPrototype*>(proto)->Spells[1].SpellTrigger = ITEM_SPELLTRIGGER_ON_USE;
1615            }
1616
1617            // spell_2 have learning spell
1618            if(proto->Spells[1].SpellTrigger != ITEM_SPELLTRIGGER_LEARN_SPELL_ID)
1619            {
1620                sLog.outErrorDb("Item (Entry: %u) has wrong item spell trigger value in spelltrigger_%d (%u) for special learning format.",i,1+1,proto->Spells[1].SpellTrigger);
1621                const_cast<ItemPrototype*>(proto)->Spells[0].SpellId = 0;
1622                const_cast<ItemPrototype*>(proto)->Spells[1].SpellId = 0;
1623                const_cast<ItemPrototype*>(proto)->Spells[1].SpellTrigger = ITEM_SPELLTRIGGER_ON_USE;
1624            }
1625            else if(!proto->Spells[1].SpellId)
1626            {
1627                sLog.outErrorDb("Item (Entry: %u) not has expected spell in spellid_%d in special learning format.",i,1+1);
1628                const_cast<ItemPrototype*>(proto)->Spells[0].SpellId = 0;
1629                const_cast<ItemPrototype*>(proto)->Spells[1].SpellTrigger = ITEM_SPELLTRIGGER_ON_USE;
1630            }
1631            else
1632            {
1633                SpellEntry const* spellInfo = sSpellStore.LookupEntry(proto->Spells[1].SpellId);
1634                if(!spellInfo)
1635                {
1636                    sLog.outErrorDb("Item (Entry: %u) has wrong (not existing) spell in spellid_%d (%u)",i,1+1,proto->Spells[1].SpellId);
1637                    const_cast<ItemPrototype*>(proto)->Spells[0].SpellId = 0;
1638                    const_cast<ItemPrototype*>(proto)->Spells[1].SpellId = 0;
1639                    const_cast<ItemPrototype*>(proto)->Spells[1].SpellTrigger = ITEM_SPELLTRIGGER_ON_USE;
1640                }
1641                // allowed only in special format
1642                else if(proto->Spells[1].SpellId==SPELL_ID_GENERIC_LEARN)
1643                {
1644                    sLog.outErrorDb("Item (Entry: %u) has broken spell in spellid_%d (%u)",i,1+1,proto->Spells[1].SpellId);
1645                    const_cast<ItemPrototype*>(proto)->Spells[0].SpellId = 0;
1646                    const_cast<ItemPrototype*>(proto)->Spells[1].SpellId = 0;
1647                    const_cast<ItemPrototype*>(proto)->Spells[1].SpellTrigger = ITEM_SPELLTRIGGER_ON_USE;
1648                }
1649            }
1650
1651            // spell_3*,spell_4*,spell_5* is empty
1652            for (int j = 2; j < 5; j++)
1653            {
1654                if(proto->Spells[j].SpellTrigger != ITEM_SPELLTRIGGER_ON_USE)
1655                {
1656                    sLog.outErrorDb("Item (Entry: %u) has wrong item spell trigger value in spelltrigger_%d (%u)",i,j+1,proto->Spells[j].SpellTrigger);
1657                    const_cast<ItemPrototype*>(proto)->Spells[j].SpellId = 0;
1658                    const_cast<ItemPrototype*>(proto)->Spells[j].SpellTrigger = ITEM_SPELLTRIGGER_ON_USE;
1659                }
1660                else if(proto->Spells[j].SpellId != 0)
1661                {
1662                    sLog.outErrorDb("Item (Entry: %u) has wrong spell in spellid_%d (%u) for learning special format",i,j+1,proto->Spells[j].SpellId);
1663                    const_cast<ItemPrototype*>(proto)->Spells[j].SpellId = 0;
1664                }
1665            }
1666        }
1667        // normal spell list
1668        else
1669        {
1670            for (int j = 0; j < 5; j++)
1671            {
1672                if(proto->Spells[j].SpellTrigger >= MAX_ITEM_SPELLTRIGGER || proto->Spells[j].SpellTrigger == ITEM_SPELLTRIGGER_LEARN_SPELL_ID)
1673                {
1674                    sLog.outErrorDb("Item (Entry: %u) has wrong item spell trigger value in spelltrigger_%d (%u)",i,j+1,proto->Spells[j].SpellTrigger);
1675                    const_cast<ItemPrototype*>(proto)->Spells[j].SpellId = 0;
1676                    const_cast<ItemPrototype*>(proto)->Spells[j].SpellTrigger = ITEM_SPELLTRIGGER_ON_USE;
1677                }
1678
1679                if(proto->Spells[j].SpellId)
1680                {
1681                    SpellEntry const* spellInfo = sSpellStore.LookupEntry(proto->Spells[j].SpellId);
1682                    if(!spellInfo)
1683                    {
1684                        sLog.outErrorDb("Item (Entry: %u) has wrong (not existing) spell in spellid_%d (%u)",i,j+1,proto->Spells[j].SpellId);
1685                        const_cast<ItemPrototype*>(proto)->Spells[j].SpellId = 0;
1686                    }
1687                    // allowed only in special format
1688                    else if(proto->Spells[j].SpellId==SPELL_ID_GENERIC_LEARN)
1689                    {
1690                        sLog.outErrorDb("Item (Entry: %u) has broken spell in spellid_%d (%u)",i,j+1,proto->Spells[j].SpellId);
1691                        const_cast<ItemPrototype*>(proto)->Spells[j].SpellId = 0;
1692                    }
1693                }
1694            }
1695        }
1696
1697        if(proto->Bonding >= MAX_BIND_TYPE)
1698            sLog.outErrorDb("Item (Entry: %u) has wrong Bonding value (%u)",i,proto->Bonding);
1699
1700        if(proto->PageText && !sPageTextStore.LookupEntry<PageText>(proto->PageText))
1701            sLog.outErrorDb("Item (Entry: %u) has non existing first page (Id:%u)", i,proto->PageText);
1702
1703        if(proto->LockID && !sLockStore.LookupEntry(proto->LockID))
1704            sLog.outErrorDb("Item (Entry: %u) has wrong LockID (%u)",i,proto->LockID);
1705
1706        if(proto->Sheath >= MAX_SHEATHETYPE)
1707        {
1708            sLog.outErrorDb("Item (Entry: %u) has wrong Sheath (%u)",i,proto->Sheath);
1709            const_cast<ItemPrototype*>(proto)->Sheath = SHEATHETYPE_NONE;
1710        }
1711
1712        if(proto->RandomProperty && !sItemRandomPropertiesStore.LookupEntry(GetItemEnchantMod(proto->RandomProperty)))
1713        {
1714            sLog.outErrorDb("Item (Entry: %u) has unknown (wrong or not listed in `item_enchantment_template`) RandomProperty (%u)",i,proto->RandomProperty);
1715            const_cast<ItemPrototype*>(proto)->RandomProperty = 0;
1716        }
1717
1718        if(proto->RandomSuffix && !sItemRandomSuffixStore.LookupEntry(GetItemEnchantMod(proto->RandomSuffix)))
1719        {
1720            sLog.outErrorDb("Item (Entry: %u) has wrong RandomSuffix (%u)",i,proto->RandomSuffix);
1721            const_cast<ItemPrototype*>(proto)->RandomSuffix = 0;
1722        }
1723
1724        if(proto->ItemSet && !sItemSetStore.LookupEntry(proto->ItemSet))
1725        {
1726            sLog.outErrorDb("Item (Entry: %u) have wrong ItemSet (%u)",i,proto->ItemSet);
1727            const_cast<ItemPrototype*>(proto)->ItemSet = 0;
1728        }
1729
1730        if(proto->Area && !GetAreaEntryByAreaID(proto->Area))
1731            sLog.outErrorDb("Item (Entry: %u) has wrong Area (%u)",i,proto->Area);
1732
1733        if(proto->Map && !sMapStore.LookupEntry(proto->Map))
1734            sLog.outErrorDb("Item (Entry: %u) has wrong Map (%u)",i,proto->Map);
1735
1736        if(proto->TotemCategory && !sTotemCategoryStore.LookupEntry(proto->TotemCategory))
1737            sLog.outErrorDb("Item (Entry: %u) has wrong TotemCategory (%u)",i,proto->TotemCategory);
1738
1739        for (int j = 0; j < 3; j++)
1740        {
1741            if(proto->Socket[j].Color && (proto->Socket[j].Color & SOCKET_COLOR_ALL) != proto->Socket[j].Color)
1742            {
1743                sLog.outErrorDb("Item (Entry: %u) has wrong socketColor_%d (%u)",i,j+1,proto->Socket[j].Color);
1744                const_cast<ItemPrototype*>(proto)->Socket[j].Color = 0;
1745            }
1746        }
1747
1748        if(proto->GemProperties && !sGemPropertiesStore.LookupEntry(proto->GemProperties))
1749            sLog.outErrorDb("Item (Entry: %u) has wrong GemProperties (%u)",i,proto->GemProperties);
1750
1751        if(proto->FoodType >= MAX_PET_DIET)
1752        {
1753            sLog.outErrorDb("Item (Entry: %u) has wrong FoodType value (%u)",i,proto->FoodType);
1754            const_cast<ItemPrototype*>(proto)->FoodType = 0;
1755        }
1756    }
1757
1758    // this DBC used currently only for check item templates in DB.
1759    sItemStore.Clear();
1760}
1761
1762void ObjectMgr::LoadAuctionItems()
1763{
1764    QueryResult *result = CharacterDatabase.Query( "SELECT itemguid,item_template FROM auctionhouse" );
1765
1766    if( !result )
1767        return;
1768
1769    barGoLink bar( result->GetRowCount() );
1770
1771    uint32 count = 0;
1772
1773    Field *fields;
1774    do
1775    {
1776        bar.step();
1777
1778        fields = result->Fetch();
1779        uint32 item_guid        = fields[0].GetUInt32();
1780        uint32 item_template    = fields[1].GetUInt32();
1781
1782        ItemPrototype const *proto = GetItemPrototype(item_template);
1783
1784        if(!proto)
1785        {
1786            sLog.outError( "ObjectMgr::LoadAuctionItems: Unknown item (GUID: %u id: #%u) in auction, skipped.", item_guid,item_template);
1787            continue;
1788        }
1789
1790        Item *item = NewItemOrBag(proto);
1791
1792        if(!item->LoadFromDB(item_guid,0))
1793        {
1794            delete item;
1795            continue;
1796        }
1797        AddAItem(item);
1798
1799        ++count;
1800    }
1801    while( result->NextRow() );
1802
1803    delete result;
1804
1805    sLog.outString();
1806    sLog.outString( ">> Loaded %u auction items", count );
1807}
1808
1809void ObjectMgr::LoadPetLevelInfo()
1810{
1811    // Loading levels data
1812    {
1813        //                                                 0               1      2   3     4    5    6    7     8    9
1814        QueryResult *result  = WorldDatabase.Query("SELECT creature_entry, level, hp, mana, str, agi, sta, inte, spi, armor FROM pet_levelstats");
1815
1816        uint32 count = 0;
1817
1818        if (!result)
1819        {
1820            barGoLink bar( 1 );
1821
1822            sLog.outString();
1823            sLog.outString( ">> Loaded %u level pet stats definitions", count );
1824            sLog.outErrorDb( "Error loading `pet_levelstats` table or empty table.");
1825            return;
1826        }
1827
1828        barGoLink bar( result->GetRowCount() );
1829
1830        do
1831        {
1832            Field* fields = result->Fetch();
1833
1834            uint32 creature_id = fields[0].GetUInt32();
1835            if(!sCreatureStorage.LookupEntry<CreatureInfo>(creature_id))
1836            {
1837                sLog.outErrorDb("Wrong creature id %u in `pet_levelstats` table, ignoring.",creature_id);
1838                continue;
1839            }
1840
1841            uint32 current_level = fields[1].GetUInt32();
1842            if(current_level > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
1843            {
1844                if(current_level > 255)                     // hardcoded level maximum
1845                    sLog.outErrorDb("Wrong (> 255) level %u in `pet_levelstats` table, ignoring.",current_level);
1846                else
1847                    sLog.outDetail("Unused (> MaxPlayerLevel in Trinityd.conf) level %u in `pet_levelstats` table, ignoring.",current_level);
1848                continue;
1849            }
1850            else if(current_level < 1)
1851            {
1852                sLog.outErrorDb("Wrong (<1) level %u in `pet_levelstats` table, ignoring.",current_level);
1853                continue;
1854            }
1855
1856            PetLevelInfo*& pInfoMapEntry = petInfo[creature_id];
1857
1858            if(pInfoMapEntry==NULL)
1859                pInfoMapEntry =  new PetLevelInfo[sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL)];
1860
1861            // data for level 1 stored in [0] array element, ...
1862            PetLevelInfo* pLevelInfo = &pInfoMapEntry[current_level-1];
1863
1864            pLevelInfo->health = fields[2].GetUInt16();
1865            pLevelInfo->mana   = fields[3].GetUInt16();
1866            pLevelInfo->armor  = fields[9].GetUInt16();
1867
1868            for (int i = 0; i < MAX_STATS; i++)
1869            {
1870                pLevelInfo->stats[i] = fields[i+4].GetUInt16();
1871            }
1872
1873            bar.step();
1874            ++count;
1875        }
1876        while (result->NextRow());
1877
1878        delete result;
1879
1880        sLog.outString();
1881        sLog.outString( ">> Loaded %u level pet stats definitions", count );
1882    }
1883
1884    // Fill gaps and check integrity
1885    for (PetLevelInfoMap::iterator itr = petInfo.begin(); itr != petInfo.end(); ++itr)
1886    {
1887        PetLevelInfo* pInfo = itr->second;
1888
1889        // fatal error if no level 1 data
1890        if(!pInfo || pInfo[0].health == 0 )
1891        {
1892            sLog.outErrorDb("Creature %u does not have pet stats data for Level 1!",itr->first);
1893            exit(1);
1894        }
1895
1896        // fill level gaps
1897        for (uint32 level = 1; level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL); ++level)
1898        {
1899            if(pInfo[level].health == 0)
1900            {
1901                sLog.outErrorDb("Creature %u has no data for Level %i pet stats data, using data of Level %i.",itr->first,level+1, level);
1902                pInfo[level] = pInfo[level-1];
1903            }
1904        }
1905    }
1906}
1907
1908PetLevelInfo const* ObjectMgr::GetPetLevelInfo(uint32 creature_id, uint32 level) const
1909{
1910    if(level > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
1911        level = sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL);
1912
1913    PetLevelInfoMap::const_iterator itr = petInfo.find(creature_id);
1914    if(itr == petInfo.end())
1915        return NULL;
1916
1917    return &itr->second[level-1];                           // data for level 1 stored in [0] array element, ...
1918}
1919
1920void ObjectMgr::LoadPlayerInfo()
1921{
1922    // Load playercreate
1923    {
1924        //                                                0     1      2    3     4           5           6
1925        QueryResult *result = WorldDatabase.Query("SELECT race, class, map, zone, position_x, position_y, position_z FROM playercreateinfo");
1926
1927        uint32 count = 0;
1928
1929        if (!result)
1930        {
1931            barGoLink bar( 1 );
1932
1933            sLog.outString();
1934            sLog.outString( ">> Loaded %u player create definitions", count );
1935            sLog.outErrorDb( "Error loading `playercreateinfo` table or empty table.");
1936            exit(1);
1937        }
1938
1939        barGoLink bar( result->GetRowCount() );
1940
1941        do
1942        {
1943            Field* fields = result->Fetch();
1944
1945            uint32 current_race = fields[0].GetUInt32();
1946            uint32 current_class = fields[1].GetUInt32();
1947            uint32 mapId     = fields[2].GetUInt32();
1948            uint32 zoneId    = fields[3].GetUInt32();
1949            float  positionX = fields[4].GetFloat();
1950            float  positionY = fields[5].GetFloat();
1951            float  positionZ = fields[6].GetFloat();
1952
1953            if(current_race >= MAX_RACES)
1954            {
1955                sLog.outErrorDb("Wrong race %u in `playercreateinfo` table, ignoring.",current_race);
1956                continue;
1957            }
1958
1959            ChrRacesEntry const* rEntry = sChrRacesStore.LookupEntry(current_race);
1960            if(!rEntry)
1961            {
1962                sLog.outErrorDb("Wrong race %u in `playercreateinfo` table, ignoring.",current_race);
1963                continue;
1964            }
1965
1966            if(current_class >= MAX_CLASSES)
1967            {
1968                sLog.outErrorDb("Wrong class %u in `playercreateinfo` table, ignoring.",current_class);
1969                continue;
1970            }
1971
1972            if(!sChrClassesStore.LookupEntry(current_class))
1973            {
1974                sLog.outErrorDb("Wrong class %u in `playercreateinfo` table, ignoring.",current_class);
1975                continue;
1976            }
1977
1978            // accept DB data only for valid position (and non instanceable)
1979            if( !MapManager::IsValidMapCoord(mapId,positionX,positionY,positionZ) )
1980            {
1981                sLog.outErrorDb("Wrong home position for class %u race %u pair in `playercreateinfo` table, ignoring.",current_class,current_race);
1982                continue;
1983            }
1984
1985            if( sMapStore.LookupEntry(mapId)->Instanceable() )
1986            {
1987                sLog.outErrorDb("Home position in instanceable map for class %u race %u pair in `playercreateinfo` table, ignoring.",current_class,current_race);
1988                continue;
1989            }
1990
1991            PlayerInfo* pInfo = &playerInfo[current_race][current_class];
1992
1993            pInfo->mapId     = mapId;
1994            pInfo->zoneId    = zoneId;
1995            pInfo->positionX = positionX;
1996            pInfo->positionY = positionY;
1997            pInfo->positionZ = positionZ;
1998
1999            pInfo->displayId_m = rEntry->model_m;
2000            pInfo->displayId_f = rEntry->model_f;
2001
2002            bar.step();
2003            ++count;
2004        }
2005        while (result->NextRow());
2006
2007        delete result;
2008
2009        sLog.outString();
2010        sLog.outString( ">> Loaded %u player create definitions", count );
2011    }
2012
2013    // Load playercreate items
2014    {
2015        //                                                0     1      2       3
2016        QueryResult *result = WorldDatabase.Query("SELECT race, class, itemid, amount FROM playercreateinfo_item");
2017
2018        uint32 count = 0;
2019
2020        if (!result)
2021        {
2022            barGoLink bar( 1 );
2023
2024            sLog.outString();
2025            sLog.outString( ">> Loaded %u player create items", count );
2026            sLog.outErrorDb( "Error loading `playercreateinfo_item` table or empty table.");
2027        }
2028        else
2029        {
2030            barGoLink bar( result->GetRowCount() );
2031
2032            do
2033            {
2034                Field* fields = result->Fetch();
2035
2036                uint32 current_race = fields[0].GetUInt32();
2037                if(current_race >= MAX_RACES)
2038                {
2039                    sLog.outErrorDb("Wrong race %u in `playercreateinfo_item` table, ignoring.",current_race);
2040                    continue;
2041                }
2042
2043                uint32 current_class = fields[1].GetUInt32();
2044                if(current_class >= MAX_CLASSES)
2045                {
2046                    sLog.outErrorDb("Wrong class %u in `playercreateinfo_item` table, ignoring.",current_class);
2047                    continue;
2048                }
2049
2050                PlayerInfo* pInfo = &playerInfo[current_race][current_class];
2051
2052                uint32 item_id = fields[2].GetUInt32();
2053
2054                if(!GetItemPrototype(item_id))
2055                {
2056                    sLog.outErrorDb("Item id %u (race %u class %u) in `playercreateinfo_item` table but not listed in `item_template`, ignoring.",item_id,current_race,current_class);
2057                    continue;
2058                }
2059
2060                uint32 amount  = fields[3].GetUInt32();
2061
2062                if(!amount)
2063                {
2064                    sLog.outErrorDb("Item id %u (class %u race %u) have amount==0 in `playercreateinfo_item` table, ignoring.",item_id,current_race,current_class);
2065                    continue;
2066                }
2067
2068                pInfo->item.push_back(PlayerCreateInfoItem( item_id, amount));
2069
2070                bar.step();
2071                ++count;
2072            }
2073            while(result->NextRow());
2074
2075            delete result;
2076
2077            sLog.outString();
2078            sLog.outString( ">> Loaded %u player create items", count );
2079        }
2080    }
2081
2082    // Load playercreate spells
2083    {
2084
2085        QueryResult *result = NULL;
2086        if(sWorld.getConfig(CONFIG_START_ALL_SPELLS))
2087            result = WorldDatabase.Query("SELECT race, class, Spell, Active FROM playercreateinfo_spell_custom");
2088        else
2089            result = WorldDatabase.Query("SELECT race, class, Spell, Active FROM playercreateinfo_spell");
2090
2091        uint32 count = 0;
2092
2093        if (!result)
2094        {
2095            barGoLink bar( 1 );
2096
2097            sLog.outString();
2098            sLog.outString( ">> Loaded %u player create spells", count );
2099            sLog.outErrorDb( "Error loading player starting spells or empty table.");
2100        }
2101        else
2102        {
2103            barGoLink bar( result->GetRowCount() );
2104
2105            do
2106            {
2107                Field* fields = result->Fetch();
2108
2109                uint32 current_race = fields[0].GetUInt32();
2110                if(current_race >= MAX_RACES)
2111                {
2112                    sLog.outErrorDb("Wrong race %u in `playercreateinfo_spell` table, ignoring.",current_race);
2113                    continue;
2114                }
2115
2116                uint32 current_class = fields[1].GetUInt32();
2117                if(current_class >= MAX_CLASSES)
2118                {
2119                    sLog.outErrorDb("Wrong class %u in `playercreateinfo_spell` table, ignoring.",current_class);
2120                    continue;
2121                }
2122
2123                PlayerInfo* pInfo = &playerInfo[current_race][current_class];
2124                pInfo->spell.push_back(CreateSpellPair(fields[2].GetUInt16(), fields[3].GetUInt8()));
2125
2126                bar.step();
2127                ++count;
2128            }
2129            while( result->NextRow() );
2130
2131            delete result;
2132
2133            sLog.outString();
2134            sLog.outString( ">> Loaded %u player create spells", count );
2135        }
2136    }
2137
2138    // Load playercreate actions
2139    {
2140        //                                                0     1      2       3       4     5
2141        QueryResult *result = WorldDatabase.Query("SELECT race, class, button, action, type, misc FROM playercreateinfo_action");
2142
2143        uint32 count = 0;
2144
2145        if (!result)
2146        {
2147            barGoLink bar( 1 );
2148
2149            sLog.outString();
2150            sLog.outString( ">> Loaded %u player create actions", count );
2151            sLog.outErrorDb( "Error loading `playercreateinfo_action` table or empty table.");
2152        }
2153        else
2154        {
2155            barGoLink bar( result->GetRowCount() );
2156
2157            do
2158            {
2159                Field* fields = result->Fetch();
2160
2161                uint32 current_race = fields[0].GetUInt32();
2162                if(current_race >= MAX_RACES)
2163                {
2164                    sLog.outErrorDb("Wrong race %u in `playercreateinfo_action` table, ignoring.",current_race);
2165                    continue;
2166                }
2167
2168                uint32 current_class = fields[1].GetUInt32();
2169                if(current_class >= MAX_CLASSES)
2170                {
2171                    sLog.outErrorDb("Wrong class %u in `playercreateinfo_action` table, ignoring.",current_class);
2172                    continue;
2173                }
2174
2175                PlayerInfo* pInfo = &playerInfo[current_race][current_class];
2176                pInfo->action[0].push_back(fields[2].GetUInt16());
2177                pInfo->action[1].push_back(fields[3].GetUInt16());
2178                pInfo->action[2].push_back(fields[4].GetUInt16());
2179                pInfo->action[3].push_back(fields[5].GetUInt16());
2180
2181                bar.step();
2182                ++count;
2183            }
2184            while( result->NextRow() );
2185
2186            delete result;
2187
2188            sLog.outString();
2189            sLog.outString( ">> Loaded %u player create actions", count );
2190        }
2191    }
2192
2193    // Loading levels data (class only dependent)
2194    {
2195        //                                                 0      1      2       3
2196        QueryResult *result  = WorldDatabase.Query("SELECT class, level, basehp, basemana FROM player_classlevelstats");
2197
2198        uint32 count = 0;
2199
2200        if (!result)
2201        {
2202            barGoLink bar( 1 );
2203
2204            sLog.outString();
2205            sLog.outString( ">> Loaded %u level health/mana definitions", count );
2206            sLog.outErrorDb( "Error loading `player_classlevelstats` table or empty table.");
2207            exit(1);
2208        }
2209
2210        barGoLink bar( result->GetRowCount() );
2211
2212        do
2213        {
2214            Field* fields = result->Fetch();
2215
2216            uint32 current_class = fields[0].GetUInt32();
2217            if(current_class >= MAX_CLASSES)
2218            {
2219                sLog.outErrorDb("Wrong class %u in `player_classlevelstats` table, ignoring.",current_class);
2220                continue;
2221            }
2222
2223            uint32 current_level = fields[1].GetUInt32();
2224            if(current_level > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
2225            {
2226                if(current_level > 255)                     // hardcoded level maximum
2227                    sLog.outErrorDb("Wrong (> 255) level %u in `player_classlevelstats` table, ignoring.",current_level);
2228                else
2229                    sLog.outDetail("Unused (> MaxPlayerLevel in Trinityd.conf) level %u in `player_classlevelstats` table, ignoring.",current_level);
2230                continue;
2231            }
2232
2233            PlayerClassInfo* pClassInfo = &playerClassInfo[current_class];
2234
2235            if(!pClassInfo->levelInfo)
2236                pClassInfo->levelInfo = new PlayerClassLevelInfo[sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL)];
2237
2238            PlayerClassLevelInfo* pClassLevelInfo = &pClassInfo->levelInfo[current_level-1];
2239
2240            pClassLevelInfo->basehealth = fields[2].GetUInt16();
2241            pClassLevelInfo->basemana   = fields[3].GetUInt16();
2242
2243            bar.step();
2244            ++count;
2245        }
2246        while (result->NextRow());
2247
2248        delete result;
2249
2250        sLog.outString();
2251        sLog.outString( ">> Loaded %u level health/mana definitions", count );
2252    }
2253
2254    // Fill gaps and check integrity
2255    for (int class_ = 0; class_ < MAX_CLASSES; ++class_)
2256    {
2257        // skip non existed classes
2258        if(!sChrClassesStore.LookupEntry(class_))
2259            continue;
2260
2261        PlayerClassInfo* pClassInfo = &playerClassInfo[class_];
2262
2263        // fatal error if no level 1 data
2264        if(!pClassInfo->levelInfo || pClassInfo->levelInfo[0].basehealth == 0 )
2265        {
2266            sLog.outErrorDb("Class %i Level 1 does not have health/mana data!",class_);
2267            exit(1);
2268        }
2269
2270        // fill level gaps
2271        for (uint32 level = 1; level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL); ++level)
2272        {
2273            if(pClassInfo->levelInfo[level].basehealth == 0)
2274            {
2275                sLog.outErrorDb("Class %i Level %i does not have health/mana data. Using stats data of level %i.",class_,level+1, level);
2276                pClassInfo->levelInfo[level] = pClassInfo->levelInfo[level-1];
2277            }
2278        }
2279    }
2280
2281    // Loading levels data (class/race dependent)
2282    {
2283        //                                                 0     1      2      3    4    5    6    7
2284        QueryResult *result  = WorldDatabase.Query("SELECT race, class, level, str, agi, sta, inte, spi FROM player_levelstats");
2285
2286        uint32 count = 0;
2287
2288        if (!result)
2289        {
2290            barGoLink bar( 1 );
2291
2292            sLog.outString();
2293            sLog.outString( ">> Loaded %u level stats definitions", count );
2294            sLog.outErrorDb( "Error loading `player_levelstats` table or empty table.");
2295            exit(1);
2296        }
2297
2298        barGoLink bar( result->GetRowCount() );
2299
2300        do
2301        {
2302            Field* fields = result->Fetch();
2303
2304            uint32 current_race = fields[0].GetUInt32();
2305            if(current_race >= MAX_RACES)
2306            {
2307                sLog.outErrorDb("Wrong race %u in `player_levelstats` table, ignoring.",current_race);
2308                continue;
2309            }
2310
2311            uint32 current_class = fields[1].GetUInt32();
2312            if(current_class >= MAX_CLASSES)
2313            {
2314                sLog.outErrorDb("Wrong class %u in `player_levelstats` table, ignoring.",current_class);
2315                continue;
2316            }
2317
2318            uint32 current_level = fields[2].GetUInt32();
2319            if(current_level > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
2320            {
2321                if(current_level > 255)                     // hardcoded level maximum
2322                    sLog.outErrorDb("Wrong (> 255) level %u in `player_levelstats` table, ignoring.",current_level);
2323                else
2324                    sLog.outDetail("Unused (> MaxPlayerLevel in Trinityd.conf) level %u in `player_levelstats` table, ignoring.",current_level);
2325                continue;
2326            }
2327
2328            PlayerInfo* pInfo = &playerInfo[current_race][current_class];
2329
2330            if(!pInfo->levelInfo)
2331                pInfo->levelInfo = new PlayerLevelInfo[sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL)];
2332
2333            PlayerLevelInfo* pLevelInfo = &pInfo->levelInfo[current_level-1];
2334
2335            for (int i = 0; i < MAX_STATS; i++)
2336            {
2337                pLevelInfo->stats[i] = fields[i+3].GetUInt8();
2338            }
2339
2340            bar.step();
2341            ++count;
2342        }
2343        while (result->NextRow());
2344
2345        delete result;
2346
2347        sLog.outString();
2348        sLog.outString( ">> Loaded %u level stats definitions", count );
2349    }
2350
2351    // Fill gaps and check integrity
2352    for (int race = 0; race < MAX_RACES; ++race)
2353    {
2354        // skip non existed races
2355        if(!sChrRacesStore.LookupEntry(race))
2356            continue;
2357
2358        for (int class_ = 0; class_ < MAX_CLASSES; ++class_)
2359        {
2360            // skip non existed classes
2361            if(!sChrClassesStore.LookupEntry(class_))
2362                continue;
2363
2364            PlayerInfo* pInfo = &playerInfo[race][class_];
2365
2366            // skip non loaded combinations
2367            if(!pInfo->displayId_m || !pInfo->displayId_f)
2368                continue;
2369
2370            // skip expansion races if not playing with expansion
2371            if (sWorld.getConfig(CONFIG_EXPANSION) < 1 && (race == RACE_BLOODELF || race == RACE_DRAENEI))
2372                continue;
2373               
2374            // skip expansion classes if not playing with expansion
2375            if (sWorld.getConfig(CONFIG_EXPANSION) < 2 && class_ == CLASS_DEATH_KNIGHT)
2376                continue;
2377
2378            // fatal error if no level 1 data
2379            if(!pInfo->levelInfo || pInfo->levelInfo[0].stats[0] == 0 )
2380            {
2381                sLog.outErrorDb("Race %i Class %i Level 1 does not have stats data!",race,class_);
2382                exit(1);
2383            }
2384
2385            // fill level gaps
2386            for (uint32 level = 1; level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL); ++level)
2387            {
2388                if(pInfo->levelInfo[level].stats[0] == 0)
2389                {
2390                    sLog.outErrorDb("Race %i Class %i Level %i does not have stats data. Using stats data of level %i.",race,class_,level+1, level);
2391                    pInfo->levelInfo[level] = pInfo->levelInfo[level-1];
2392                }
2393            }
2394        }
2395    }
2396}
2397
2398void ObjectMgr::GetPlayerClassLevelInfo(uint32 class_, uint32 level, PlayerClassLevelInfo* info) const
2399{
2400    if(level < 1 || class_ >= MAX_CLASSES)
2401        return;
2402
2403    PlayerClassInfo const* pInfo = &playerClassInfo[class_];
2404
2405    if(level > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
2406        level = sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL);
2407
2408    *info = pInfo->levelInfo[level-1];
2409}
2410
2411void ObjectMgr::GetPlayerLevelInfo(uint32 race, uint32 class_, uint32 level, PlayerLevelInfo* info) const
2412{
2413    if(level < 1 || race   >= MAX_RACES || class_ >= MAX_CLASSES)
2414        return;
2415
2416    PlayerInfo const* pInfo = &playerInfo[race][class_];
2417    if(pInfo->displayId_m==0 || pInfo->displayId_f==0)
2418        return;
2419
2420    if(level <= sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
2421        *info = pInfo->levelInfo[level-1];
2422    else
2423        BuildPlayerLevelInfo(race,class_,level,info);
2424}
2425
2426void ObjectMgr::BuildPlayerLevelInfo(uint8 race, uint8 _class, uint8 level, PlayerLevelInfo* info) const
2427{
2428    // base data (last known level)
2429    *info = playerInfo[race][_class].levelInfo[sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL)-1];
2430
2431    for(int lvl = sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL)-1; lvl < level; ++lvl)
2432    {
2433        switch(_class)
2434        {
2435            case CLASS_WARRIOR:
2436                info->stats[STAT_STRENGTH]  += (lvl > 23 ? 2: (lvl > 1  ? 1: 0));
2437                info->stats[STAT_STAMINA]   += (lvl > 23 ? 2: (lvl > 1  ? 1: 0));
2438                info->stats[STAT_AGILITY]   += (lvl > 36 ? 1: (lvl > 6 && (lvl%2) ? 1: 0));
2439                info->stats[STAT_INTELLECT] += (lvl > 9 && !(lvl%2) ? 1: 0);
2440                info->stats[STAT_SPIRIT]    += (lvl > 9 && !(lvl%2) ? 1: 0);
2441                break;
2442            case CLASS_PALADIN:
2443                info->stats[STAT_STRENGTH]  += (lvl > 3  ? 1: 0);
2444                info->stats[STAT_STAMINA]   += (lvl > 33 ? 2: (lvl > 1 ? 1: 0));
2445                info->stats[STAT_AGILITY]   += (lvl > 38 ? 1: (lvl > 7 && !(lvl%2) ? 1: 0));
2446                info->stats[STAT_INTELLECT] += (lvl > 6 && (lvl%2) ? 1: 0);
2447                info->stats[STAT_SPIRIT]    += (lvl > 7 ? 1: 0);
2448                break;
2449            case CLASS_HUNTER:
2450                info->stats[STAT_STRENGTH]  += (lvl > 4  ? 1: 0);
2451                info->stats[STAT_STAMINA]   += (lvl > 4  ? 1: 0);
2452                info->stats[STAT_AGILITY]   += (lvl > 33 ? 2: (lvl > 1 ? 1: 0));
2453                info->stats[STAT_INTELLECT] += (lvl > 8 && (lvl%2) ? 1: 0);
2454                info->stats[STAT_SPIRIT]    += (lvl > 38 ? 1: (lvl > 9 && !(lvl%2) ? 1: 0));
2455                break;
2456            case CLASS_ROGUE:
2457                info->stats[STAT_STRENGTH]  += (lvl > 5  ? 1: 0);
2458                info->stats[STAT_STAMINA]   += (lvl > 4  ? 1: 0);
2459                info->stats[STAT_AGILITY]   += (lvl > 16 ? 2: (lvl > 1 ? 1: 0));
2460                info->stats[STAT_INTELLECT] += (lvl > 8 && !(lvl%2) ? 1: 0);
2461                info->stats[STAT_SPIRIT]    += (lvl > 38 ? 1: (lvl > 9 && !(lvl%2) ? 1: 0));
2462                break;
2463            case CLASS_PRIEST:
2464                info->stats[STAT_STRENGTH]  += (lvl > 9 && !(lvl%2) ? 1: 0);
2465                info->stats[STAT_STAMINA]   += (lvl > 5  ? 1: 0);
2466                info->stats[STAT_AGILITY]   += (lvl > 38 ? 1: (lvl > 8 && (lvl%2) ? 1: 0));
2467                info->stats[STAT_INTELLECT] += (lvl > 22 ? 2: (lvl > 1 ? 1: 0));
2468                info->stats[STAT_SPIRIT]    += (lvl > 3  ? 1: 0);
2469                break;
2470            case CLASS_SHAMAN:
2471                info->stats[STAT_STRENGTH]  += (lvl > 34 ? 1: (lvl > 6 && (lvl%2) ? 1: 0));
2472                info->stats[STAT_STAMINA]   += (lvl > 4 ? 1: 0);
2473                info->stats[STAT_AGILITY]   += (lvl > 7 && !(lvl%2) ? 1: 0);
2474                info->stats[STAT_INTELLECT] += (lvl > 5 ? 1: 0);
2475                info->stats[STAT_SPIRIT]    += (lvl > 4 ? 1: 0);
2476                break;
2477            case CLASS_MAGE:
2478                info->stats[STAT_STRENGTH]  += (lvl > 9 && !(lvl%2) ? 1: 0);
2479                info->stats[STAT_STAMINA]   += (lvl > 5  ? 1: 0);
2480                info->stats[STAT_AGILITY]   += (lvl > 9 && !(lvl%2) ? 1: 0);
2481                info->stats[STAT_INTELLECT] += (lvl > 24 ? 2: (lvl > 1 ? 1: 0));
2482                info->stats[STAT_SPIRIT]    += (lvl > 33 ? 2: (lvl > 2 ? 1: 0));
2483                break;
2484            case CLASS_WARLOCK:
2485                info->stats[STAT_STRENGTH]  += (lvl > 9 && !(lvl%2) ? 1: 0);
2486                info->stats[STAT_STAMINA]   += (lvl > 38 ? 2: (lvl > 3 ? 1: 0));
2487                info->stats[STAT_AGILITY]   += (lvl > 9 && !(lvl%2) ? 1: 0);
2488                info->stats[STAT_INTELLECT] += (lvl > 33 ? 2: (lvl > 2 ? 1: 0));
2489                info->stats[STAT_SPIRIT]    += (lvl > 38 ? 2: (lvl > 3 ? 1: 0));
2490                break;
2491            case CLASS_DRUID:
2492                info->stats[STAT_STRENGTH]  += (lvl > 38 ? 2: (lvl > 6 && (lvl%2) ? 1: 0));
2493                info->stats[STAT_STAMINA]   += (lvl > 32 ? 2: (lvl > 4 ? 1: 0));
2494                info->stats[STAT_AGILITY]   += (lvl > 38 ? 2: (lvl > 8 && (lvl%2) ? 1: 0));
2495                info->stats[STAT_INTELLECT] += (lvl > 38 ? 3: (lvl > 4 ? 1: 0));
2496                info->stats[STAT_SPIRIT]    += (lvl > 38 ? 3: (lvl > 5 ? 1: 0));
2497        }
2498    }
2499}
2500
2501void ObjectMgr::LoadGuilds()
2502{
2503    Guild *newguild;
2504    uint32 count = 0;
2505
2506    QueryResult *result = CharacterDatabase.Query( "SELECT guildid FROM guild" );
2507
2508    if( !result )
2509    {
2510
2511        barGoLink bar( 1 );
2512
2513        bar.step();
2514
2515        sLog.outString();
2516        sLog.outString( ">> Loaded %u guild definitions", count );
2517        return;
2518    }
2519
2520    barGoLink bar( result->GetRowCount() );
2521
2522    do
2523    {
2524        Field *fields = result->Fetch();
2525
2526        bar.step();
2527        ++count;
2528
2529        newguild = new Guild;
2530        if(!newguild->LoadGuildFromDB(fields[0].GetUInt32()))
2531        {
2532            newguild->Disband();
2533            delete newguild;
2534            continue;
2535        }
2536        AddGuild(newguild);
2537
2538    }while( result->NextRow() );
2539
2540    delete result;
2541
2542    sLog.outString();
2543    sLog.outString( ">> Loaded %u guild definitions", count );
2544}
2545
2546void ObjectMgr::LoadArenaTeams()
2547{
2548    uint32 count = 0;
2549
2550    QueryResult *result = CharacterDatabase.Query( "SELECT arenateamid FROM arena_team" );
2551
2552    if( !result )
2553    {
2554
2555        barGoLink bar( 1 );
2556
2557        bar.step();
2558
2559        sLog.outString();
2560        sLog.outString( ">> Loaded %u arenateam definitions", count );
2561        return;
2562    }
2563
2564    barGoLink bar( result->GetRowCount() );
2565
2566    do
2567    {
2568        Field *fields = result->Fetch();
2569
2570        bar.step();
2571        ++count;
2572
2573        ArenaTeam *newarenateam = new ArenaTeam;
2574        if(!newarenateam->LoadArenaTeamFromDB(fields[0].GetUInt32()))
2575        {
2576            delete newarenateam;
2577            continue;
2578        }
2579        AddArenaTeam(newarenateam);
2580    }while( result->NextRow() );
2581
2582    delete result;
2583
2584    sLog.outString();
2585    sLog.outString( ">> Loaded %u arenateam definitions", count );
2586}
2587
2588void ObjectMgr::LoadGroups()
2589{
2590    // -- loading groups --
2591    Group *group = NULL;
2592    uint64 leaderGuid = 0;
2593    uint32 count = 0;
2594    //                                                     0         1              2           3           4              5      6      7      8      9      10     11     12     13      14          15
2595    QueryResult *result = CharacterDatabase.PQuery("SELECT mainTank, mainAssistant, lootMethod, looterGuid, lootThreshold, icon1, icon2, icon3, icon4, icon5, icon6, icon7, icon8, isRaid, difficulty, leaderGuid FROM groups");
2596
2597    if( !result )
2598    {
2599        barGoLink bar( 1 );
2600
2601        bar.step();
2602
2603        sLog.outString();
2604        sLog.outString( ">> Loaded %u group definitions", count );
2605        return;
2606    }
2607
2608    barGoLink bar( result->GetRowCount() );
2609
2610    do
2611    {
2612        bar.step();
2613        Field *fields = result->Fetch();
2614        ++count;
2615        leaderGuid = MAKE_NEW_GUID(fields[15].GetUInt32(),0,HIGHGUID_PLAYER);
2616
2617        group = new Group;
2618        if(!group->LoadGroupFromDB(leaderGuid, result, false))
2619        {
2620            group->Disband();
2621            delete group;
2622            continue;
2623        }
2624        AddGroup(group);
2625    }while( result->NextRow() );
2626
2627    delete result;
2628
2629    sLog.outString();
2630    sLog.outString( ">> Loaded %u group definitions", count );
2631
2632    // -- loading members --
2633    count = 0;
2634    group = NULL;
2635    leaderGuid = 0;
2636    //                                        0           1          2         3
2637    result = CharacterDatabase.PQuery("SELECT memberGuid, assistant, subgroup, leaderGuid FROM group_member ORDER BY leaderGuid");
2638    if(!result)
2639    {
2640        barGoLink bar( 1 );
2641        bar.step();
2642    }
2643    else
2644    {
2645        barGoLink bar( result->GetRowCount() );
2646        do
2647        {
2648            bar.step();
2649            Field *fields = result->Fetch();
2650            count++;
2651            leaderGuid = MAKE_NEW_GUID(fields[3].GetUInt32(), 0, HIGHGUID_PLAYER);
2652            if(!group || group->GetLeaderGUID() != leaderGuid)
2653            {
2654                group = GetGroupByLeader(leaderGuid);
2655                if(!group)
2656                {
2657                    sLog.outErrorDb("Incorrect entry in group_member table : no group with leader %d for member %d!", fields[3].GetUInt32(), fields[0].GetUInt32());
2658                    CharacterDatabase.PExecute("DELETE FROM group_member WHERE memberGuid = '%d'", fields[0].GetUInt32());
2659                    continue;
2660                }
2661            }
2662
2663            if(!group->LoadMemberFromDB(fields[0].GetUInt32(), fields[2].GetUInt8(), fields[1].GetBool()))
2664            {
2665                sLog.outErrorDb("Incorrect entry in group_member table : member %d cannot be added to player %d's group!", fields[0].GetUInt32(), fields[3].GetUInt32());
2666                CharacterDatabase.PExecute("DELETE FROM group_member WHERE memberGuid = '%d'", fields[0].GetUInt32());
2667            }
2668        }while( result->NextRow() );
2669        delete result;
2670    }
2671
2672    // clean groups
2673    // TODO: maybe delete from the DB before loading in this case
2674    for(GroupSet::iterator itr = mGroupSet.begin(); itr != mGroupSet.end();)
2675    {
2676        if((*itr)->GetMembersCount() < 2)
2677        {
2678            (*itr)->Disband();
2679            delete *itr;
2680            mGroupSet.erase(itr++);
2681        }
2682        else
2683            ++itr;
2684    }
2685
2686    // -- loading instances --
2687    count = 0;
2688    group = NULL;
2689    leaderGuid = 0;
2690    result = CharacterDatabase.PQuery(
2691        //      0           1    2         3          4           5
2692        "SELECT leaderGuid, map, instance, permanent, difficulty, resettime, "
2693        // 6
2694        "(SELECT COUNT(*) FROM character_instance WHERE guid = leaderGuid AND instance = group_instance.instance AND permanent = 1 LIMIT 1) "
2695        "FROM group_instance LEFT JOIN instance ON instance = id ORDER BY leaderGuid"
2696    );
2697
2698    if(!result)
2699    {
2700        barGoLink bar( 1 );
2701        bar.step();
2702    }
2703    else
2704    {
2705        barGoLink bar( result->GetRowCount() );
2706        do
2707        {
2708            bar.step();
2709            Field *fields = result->Fetch();
2710            count++;
2711            leaderGuid = MAKE_NEW_GUID(fields[0].GetUInt32(), 0, HIGHGUID_PLAYER);
2712            if(!group || group->GetLeaderGUID() != leaderGuid)
2713            {
2714                group = GetGroupByLeader(leaderGuid);
2715                if(!group)
2716                {
2717                    sLog.outErrorDb("Incorrect entry in group_instance table : no group with leader %d", fields[0].GetUInt32());
2718                    continue;
2719                }
2720            }
2721
2722            InstanceSave *save = sInstanceSaveManager.AddInstanceSave(fields[1].GetUInt32(), fields[2].GetUInt32(), fields[4].GetUInt8(), (time_t)fields[5].GetUInt64(), (fields[6].GetUInt32() == 0), true);
2723            group->BindToInstance(save, fields[3].GetBool(), true);
2724        }while( result->NextRow() );
2725        delete result;
2726    }
2727
2728    sLog.outString();
2729    sLog.outString( ">> Loaded %u group-instance binds total", count );
2730
2731    sLog.outString();
2732    sLog.outString( ">> Loaded %u group members total", count );
2733}
2734
2735void ObjectMgr::LoadQuests()
2736{
2737    // For reload case
2738    for(QuestMap::const_iterator itr=mQuestTemplates.begin(); itr != mQuestTemplates.end(); ++itr)
2739        delete itr->second;
2740    mQuestTemplates.clear();
2741
2742    mExclusiveQuestGroups.clear();
2743
2744    //                                                0      1       2           3             4         5           6     7              8
2745    QueryResult *result = WorldDatabase.Query("SELECT entry, Method, ZoneOrSort, SkillOrClass, MinLevel, QuestLevel, Type, RequiredRaces, RequiredSkillValue,"
2746    //   9                    10                 11                     12                   13                     14                   15                16
2747        "RepObjectiveFaction, RepObjectiveValue, RequiredMinRepFaction, RequiredMinRepValue, RequiredMaxRepFaction, RequiredMaxRepValue, SuggestedPlayers, LimitTime,"
2748    //   17          18            19           20           21           22              23                24         25            26
2749        "QuestFlags, SpecialFlags, CharTitleId, PrevQuestId, NextQuestId, ExclusiveGroup, NextQuestInChain, SrcItemId, SrcItemCount, SrcSpell,"
2750    //   27     28       29          30               31                32       33              34              35              36
2751        "Title, Details, Objectives, OfferRewardText, RequestItemsText, EndText, ObjectiveText1, ObjectiveText2, ObjectiveText3, ObjectiveText4,"
2752    //   37          38          39          40          41             42             43             44
2753        "ReqItemId1, ReqItemId2, ReqItemId3, ReqItemId4, ReqItemCount1, ReqItemCount2, ReqItemCount3, ReqItemCount4,"
2754    //   45            46            47            48            49               50               51               52               53             54             54             55
2755        "ReqSourceId1, ReqSourceId2, ReqSourceId3, ReqSourceId4, ReqSourceCount1, ReqSourceCount2, ReqSourceCount3, ReqSourceCount4, ReqSourceRef1, ReqSourceRef2, ReqSourceRef3, ReqSourceRef4,"
2756    //   57                  58                  59                  60                  61                     62                     63                     64
2757        "ReqCreatureOrGOId1, ReqCreatureOrGOId2, ReqCreatureOrGOId3, ReqCreatureOrGOId4, ReqCreatureOrGOCount1, ReqCreatureOrGOCount2, ReqCreatureOrGOCount3, ReqCreatureOrGOCount4,"
2758    //   65             66             67             68
2759        "ReqSpellCast1, ReqSpellCast2, ReqSpellCast3, ReqSpellCast4,"
2760    //   69                70                71                72                73                74
2761        "RewChoiceItemId1, RewChoiceItemId2, RewChoiceItemId3, RewChoiceItemId4, RewChoiceItemId5, RewChoiceItemId6,"
2762    //   75                   76                   77                   78                   79                   80
2763        "RewChoiceItemCount1, RewChoiceItemCount2, RewChoiceItemCount3, RewChoiceItemCount4, RewChoiceItemCount5, RewChoiceItemCount6,"
2764    //   81          82          83          84          85             86             87             88
2765        "RewItemId1, RewItemId2, RewItemId3, RewItemId4, RewItemCount1, RewItemCount2, RewItemCount3, RewItemCount4,"
2766    //   89              90              91              92              93              94            95            96            97            98
2767        "RewRepFaction1, RewRepFaction2, RewRepFaction3, RewRepFaction4, RewRepFaction5, RewRepValue1, RewRepValue2, RewRepValue3, RewRepValue4, RewRepValue5,"
2768    //   99             100               101       102           103                104               105         106     107     108
2769        "RewOrReqMoney, RewMoneyMaxLevel, RewSpell, RewSpellCast, RewMailTemplateId, RewMailDelaySecs, PointMapId, PointX, PointY, PointOpt,"
2770    //   109            110            111            112           113              114            115                116                117                118
2771        "DetailsEmote1, DetailsEmote2, DetailsEmote3, DetailsEmote4,IncompleteEmote, CompleteEmote, OfferRewardEmote1, OfferRewardEmote2, OfferRewardEmote3, OfferRewardEmote4,"
2772    //   119          120
2773        "StartScript, CompleteScript"
2774        " FROM quest_template");
2775    if(result == NULL)
2776    {
2777        barGoLink bar( 1 );
2778        bar.step();
2779
2780        sLog.outString();
2781        sLog.outString( ">> Loaded 0 quests definitions" );
2782        sLog.outErrorDb("`quest_template` table is empty!");
2783        return;
2784    }
2785
2786    // create multimap previous quest for each existed quest
2787    // some quests can have many previous maps set by NextQuestId in previous quest
2788    // for example set of race quests can lead to single not race specific quest
2789    barGoLink bar( result->GetRowCount() );
2790    do
2791    {
2792        bar.step();
2793        Field *fields = result->Fetch();
2794
2795        Quest * newQuest = new Quest(fields);
2796        mQuestTemplates[newQuest->GetQuestId()] = newQuest;
2797    } while( result->NextRow() );
2798
2799    delete result;
2800
2801    // Post processing
2802    for (QuestMap::iterator iter = mQuestTemplates.begin(); iter != mQuestTemplates.end(); iter++)
2803    {
2804        Quest * qinfo = iter->second;
2805
2806        // additional quest integrity checks (GO, creature_template and item_template must be loaded already)
2807
2808        if( qinfo->GetQuestMethod() >= 3 )
2809        {
2810            sLog.outErrorDb("Quest %u has `Method` = %u, expected values are 0, 1 or 2.",qinfo->GetQuestId(),qinfo->GetQuestMethod());
2811        }
2812
2813        if (qinfo->QuestFlags & ~QUEST_TRINITY_FLAGS_DB_ALLOWED)
2814        {
2815            sLog.outErrorDb("Quest %u has `SpecialFlags` = %u > max allowed value. Correct `SpecialFlags` to value <= %u",
2816                qinfo->GetQuestId(),qinfo->QuestFlags,QUEST_TRINITY_FLAGS_DB_ALLOWED >> 16);
2817            qinfo->QuestFlags &= QUEST_TRINITY_FLAGS_DB_ALLOWED;
2818        }
2819
2820        if(qinfo->QuestFlags & QUEST_FLAGS_DAILY)
2821        {
2822            if(!(qinfo->QuestFlags & QUEST_TRINITY_FLAGS_REPEATABLE))
2823            {
2824                sLog.outErrorDb("Daily Quest %u not marked as repeatable in `SpecialFlags`, added.",qinfo->GetQuestId());
2825                qinfo->QuestFlags |= QUEST_TRINITY_FLAGS_REPEATABLE;
2826            }
2827        }
2828
2829        if(qinfo->QuestFlags & QUEST_FLAGS_AUTO_REWARDED)
2830        {
2831            // at auto-reward can be rewarded only RewChoiceItemId[0]
2832            for(int j = 1; j < QUEST_REWARD_CHOICES_COUNT; ++j )
2833            {
2834                if(uint32 id = qinfo->RewChoiceItemId[j])
2835                {
2836                    sLog.outErrorDb("Quest %u has `RewChoiceItemId%d` = %u but item from `RewChoiceItemId%d` can't be rewarded with quest flag QUEST_FLAGS_AUTO_REWARDED.",
2837                        qinfo->GetQuestId(),j+1,id,j+1);
2838                    // no changes, quest ignore this data
2839                }
2840            }
2841        }
2842
2843        // client quest log visual (area case)
2844        if( qinfo->ZoneOrSort > 0 )
2845        {
2846            if(!GetAreaEntryByAreaID(qinfo->ZoneOrSort))
2847            {
2848                sLog.outErrorDb("Quest %u has `ZoneOrSort` = %u (zone case) but zone with this id does not exist.",
2849                    qinfo->GetQuestId(),qinfo->ZoneOrSort);
2850                // no changes, quest not dependent from this value but can have problems at client
2851            }
2852        }
2853        // client quest log visual (sort case)
2854        if( qinfo->ZoneOrSort < 0 )
2855        {
2856            QuestSortEntry const* qSort = sQuestSortStore.LookupEntry(-int32(qinfo->ZoneOrSort));
2857            if( !qSort )
2858            {
2859                sLog.outErrorDb("Quest %u has `ZoneOrSort` = %i (sort case) but quest sort with this id does not exist.",
2860                    qinfo->GetQuestId(),qinfo->ZoneOrSort);
2861                // no changes, quest not dependent from this value but can have problems at client (note some may be 0, we must allow this so no check)
2862            }
2863            //check SkillOrClass value (class case).
2864            if( ClassByQuestSort(-int32(qinfo->ZoneOrSort)) )
2865            {
2866                // SkillOrClass should not have class case when class case already set in ZoneOrSort.
2867                if(qinfo->SkillOrClass < 0)
2868                {
2869                    sLog.outErrorDb("Quest %u has `ZoneOrSort` = %i (class sort case) and `SkillOrClass` = %i (class case), redundant.",
2870                        qinfo->GetQuestId(),qinfo->ZoneOrSort,qinfo->SkillOrClass);
2871                }
2872            }
2873            //check for proper SkillOrClass value (skill case)
2874            if(int32 skill_id =  SkillByQuestSort(-int32(qinfo->ZoneOrSort)))
2875            {
2876                // skill is positive value in SkillOrClass
2877                if(qinfo->SkillOrClass != skill_id )
2878                {
2879                    sLog.outErrorDb("Quest %u has `ZoneOrSort` = %i (skill sort case) but `SkillOrClass` does not have a corresponding value (%i).",
2880                        qinfo->GetQuestId(),qinfo->ZoneOrSort,skill_id);
2881                    //override, and force proper value here?
2882                }
2883            }
2884        }
2885
2886        // SkillOrClass (class case)
2887        if( qinfo->SkillOrClass < 0 )
2888        {
2889            if( !sChrClassesStore.LookupEntry(-int32(qinfo->SkillOrClass)) )
2890            {
2891                sLog.outErrorDb("Quest %u has `SkillOrClass` = %i (class case) but class (%i) does not exist",
2892                    qinfo->GetQuestId(),qinfo->SkillOrClass,-qinfo->SkillOrClass);
2893            }
2894        }
2895        // SkillOrClass (skill case)
2896        if( qinfo->SkillOrClass > 0 )
2897        {
2898            if( !sSkillLineStore.LookupEntry(qinfo->SkillOrClass) )
2899            {
2900                sLog.outErrorDb("Quest %u has `SkillOrClass` = %u (skill case) but skill (%i) does not exist",
2901                    qinfo->GetQuestId(),qinfo->SkillOrClass,qinfo->SkillOrClass);
2902            }
2903        }
2904
2905        if( qinfo->RequiredSkillValue )
2906        {
2907            if( qinfo->RequiredSkillValue > sWorld.GetConfigMaxSkillValue() )
2908            {
2909                sLog.outErrorDb("Quest %u has `RequiredSkillValue` = %u but max possible skill is %u, quest can't be done.",
2910                    qinfo->GetQuestId(),qinfo->RequiredSkillValue,sWorld.GetConfigMaxSkillValue());
2911                // no changes, quest can't be done for this requirement
2912            }
2913
2914            if( qinfo->SkillOrClass <= 0 )
2915            {
2916                sLog.outErrorDb("Quest %u has `RequiredSkillValue` = %u but `SkillOrClass` = %i (class case), value ignored.",
2917                    qinfo->GetQuestId(),qinfo->RequiredSkillValue,qinfo->SkillOrClass);
2918                // no changes, quest can't be done for this requirement (fail at wrong skill id)
2919            }
2920        }
2921        // else Skill quests can have 0 skill level, this is ok
2922
2923        if(qinfo->RepObjectiveFaction && !sFactionStore.LookupEntry(qinfo->RepObjectiveFaction))
2924        {
2925            sLog.outErrorDb("Quest %u has `RepObjectiveFaction` = %u but faction template %u does not exist, quest can't be done.",
2926                qinfo->GetQuestId(),qinfo->RepObjectiveFaction,qinfo->RepObjectiveFaction);
2927            // no changes, quest can't be done for this requirement
2928        }
2929
2930        if(qinfo->RequiredMinRepFaction && !sFactionStore.LookupEntry(qinfo->RequiredMinRepFaction))
2931        {
2932            sLog.outErrorDb("Quest %u has `RequiredMinRepFaction` = %u but faction template %u does not exist, quest can't be done.",
2933                qinfo->GetQuestId(),qinfo->RequiredMinRepFaction,qinfo->RequiredMinRepFaction);
2934            // no changes, quest can't be done for this requirement
2935        }
2936
2937        if(qinfo->RequiredMaxRepFaction && !sFactionStore.LookupEntry(qinfo->RequiredMaxRepFaction))
2938        {
2939            sLog.outErrorDb("Quest %u has `RequiredMaxRepFaction` = %u but faction template %u does not exist, quest can't be done.",
2940                qinfo->GetQuestId(),qinfo->RequiredMaxRepFaction,qinfo->RequiredMaxRepFaction);
2941            // no changes, quest can't be done for this requirement
2942        }
2943
2944        if(qinfo->RequiredMinRepValue && qinfo->RequiredMinRepValue > Player::Reputation_Cap)
2945        {
2946            sLog.outErrorDb("Quest %u has `RequiredMinRepValue` = %d but max reputation is %u, quest can't be done.",
2947                qinfo->GetQuestId(),qinfo->RequiredMinRepValue,Player::Reputation_Cap);
2948            // no changes, quest can't be done for this requirement
2949        }
2950
2951        if(qinfo->RequiredMinRepValue && qinfo->RequiredMaxRepValue && qinfo->RequiredMaxRepValue <= qinfo->RequiredMinRepValue)
2952        {
2953            sLog.outErrorDb("Quest %u has `RequiredMaxRepValue` = %d and `RequiredMinRepValue` = %d, quest can't be done.",
2954                qinfo->GetQuestId(),qinfo->RequiredMaxRepValue,qinfo->RequiredMinRepValue);
2955            // no changes, quest can't be done for this requirement
2956        }
2957
2958        if(!qinfo->RepObjectiveFaction && qinfo->RepObjectiveValue > 0 )
2959        {
2960            sLog.outErrorDb("Quest %u has `RepObjectiveValue` = %d but `RepObjectiveFaction` is 0, value has no effect",
2961                qinfo->GetQuestId(),qinfo->RepObjectiveValue);
2962            // warning
2963        }
2964
2965        if(!qinfo->RequiredMinRepFaction && qinfo->RequiredMinRepValue > 0 )
2966        {
2967            sLog.outErrorDb("Quest %u has `RequiredMinRepValue` = %d but `RequiredMinRepFaction` is 0, value has no effect",
2968                qinfo->GetQuestId(),qinfo->RequiredMinRepValue);
2969            // warning
2970        }
2971
2972        if(!qinfo->RequiredMaxRepFaction && qinfo->RequiredMaxRepValue > 0 )
2973        {
2974            sLog.outErrorDb("Quest %u has `RequiredMaxRepValue` = %d but `RequiredMaxRepFaction` is 0, value has no effect",
2975                qinfo->GetQuestId(),qinfo->RequiredMaxRepValue);
2976            // warning
2977        }
2978
2979        if(qinfo->CharTitleId && !sCharTitlesStore.LookupEntry(qinfo->CharTitleId))
2980        {
2981            sLog.outErrorDb("Quest %u has `CharTitleId` = %u but CharTitle Id %u does not exist, quest can't be rewarded with title.",
2982                qinfo->GetQuestId(),qinfo->GetCharTitleId(),qinfo->GetCharTitleId());
2983            qinfo->CharTitleId = 0;
2984            // quest can't reward this title
2985        }
2986
2987        if(qinfo->SrcItemId)
2988        {
2989            if(!sItemStorage.LookupEntry<ItemPrototype>(qinfo->SrcItemId))
2990            {
2991                sLog.outErrorDb("Quest %u has `SrcItemId` = %u but item with entry %u does not exist, quest can't be done.",
2992                    qinfo->GetQuestId(),qinfo->SrcItemId,qinfo->SrcItemId);
2993                qinfo->SrcItemId = 0;                       // quest can't be done for this requirement
2994            }
2995            else if(qinfo->SrcItemCount==0)
2996            {
2997                sLog.outErrorDb("Quest %u has `SrcItemId` = %u but `SrcItemCount` = 0, set to 1 but need fix in DB.",
2998                    qinfo->GetQuestId(),qinfo->SrcItemId);
2999                qinfo->SrcItemCount = 1;                    // update to 1 for allow quest work for backward comptibility with DB
3000            }
3001        }
3002        else if(qinfo->SrcItemCount>0)
3003        {
3004            sLog.outErrorDb("Quest %u has `SrcItemId` = 0 but `SrcItemCount` = %u, useless value.",
3005                qinfo->GetQuestId(),qinfo->SrcItemCount);
3006            qinfo->SrcItemCount=0;                          // no quest work changes in fact
3007        }
3008
3009        if(qinfo->SrcSpell)
3010        {
3011            SpellEntry const* spellInfo = sSpellStore.LookupEntry(qinfo->SrcSpell);
3012            if(!spellInfo)
3013            {
3014                sLog.outErrorDb("Quest %u has `SrcSpell` = %u but spell %u doesn't exist, quest can't be done.",
3015                    qinfo->GetQuestId(),qinfo->SrcSpell,qinfo->SrcSpell);
3016                qinfo->SrcSpell = 0;                        // quest can't be done for this requirement
3017            }
3018            else if(!SpellMgr::IsSpellValid(spellInfo))
3019            {
3020                sLog.outErrorDb("Quest %u has `SrcSpell` = %u but spell %u is broken, quest can't be done.",
3021                    qinfo->GetQuestId(),qinfo->SrcSpell,qinfo->SrcSpell);
3022                qinfo->SrcSpell = 0;                        // quest can't be done for this requirement
3023            }
3024        }
3025
3026        for(int j = 0; j < QUEST_OBJECTIVES_COUNT; ++j )
3027        {
3028            uint32 id = qinfo->ReqItemId[j];
3029            if(id)
3030            {
3031                if(qinfo->ReqItemCount[j]==0)
3032                {
3033                    sLog.outErrorDb("Quest %u has `ReqItemId%d` = %u but `ReqItemCount%d` = 0, quest can't be done.",
3034                        qinfo->GetQuestId(),j+1,id,j+1);
3035                    // no changes, quest can't be done for this requirement
3036                }
3037
3038                qinfo->SetFlag(QUEST_TRINITY_FLAGS_DELIVER);
3039
3040                if(!sItemStorage.LookupEntry<ItemPrototype>(id))
3041                {
3042                    sLog.outErrorDb("Quest %u has `ReqItemId%d` = %u but item with entry %u does not exist, quest can't be done.",
3043                        qinfo->GetQuestId(),j+1,id,id);
3044                    qinfo->ReqItemCount[j] = 0;             // prevent incorrect work of quest
3045                }
3046            }
3047            else if(qinfo->ReqItemCount[j]>0)
3048            {
3049                sLog.outErrorDb("Quest %u has `ReqItemId%d` = 0 but `ReqItemCount%d` = %u, quest can't be done.",
3050                    qinfo->GetQuestId(),j+1,j+1,qinfo->ReqItemCount[j]);
3051                qinfo->ReqItemCount[j] = 0;                 // prevent incorrect work of quest
3052            }
3053        }
3054
3055        for(int j = 0; j < QUEST_SOURCE_ITEM_IDS_COUNT; ++j )
3056        {
3057            uint32 id = qinfo->ReqSourceId[j];
3058            if(id)
3059            {
3060                if(!sItemStorage.LookupEntry<ItemPrototype>(id))
3061                {
3062                    sLog.outErrorDb("Quest %u has `ReqSourceId%d` = %u but item with entry %u does not exist, quest can't be done.",
3063                        qinfo->GetQuestId(),j+1,id,id);
3064                    // no changes, quest can't be done for this requirement
3065                }
3066
3067                if(!qinfo->ReqSourceCount[j])
3068                {
3069                    sLog.outErrorDb("Quest %u has `ReqSourceId%d` = %u but `ReqSourceCount%d` = 0, quest can't be done.",
3070                        qinfo->GetQuestId(),j+1,id,j+1);
3071                    qinfo->ReqSourceId[j] = 0;              // prevent incorrect work of quest
3072                }
3073
3074                if(!qinfo->ReqSourceRef[j])
3075                {
3076                    sLog.outErrorDb("Quest %u has `ReqSourceId%d` = %u but `ReqSourceRef%d` = 0, quest can't be done.",
3077                        qinfo->GetQuestId(),j+1,id,j+1);
3078                    qinfo->ReqSourceId[j] = 0;              // prevent incorrect work of quest
3079                }
3080            }
3081            else
3082            {
3083                if(qinfo->ReqSourceCount[j]>0)
3084                {
3085                    sLog.outErrorDb("Quest %u has `ReqSourceId%d` = 0 but `ReqSourceCount%d` = %u.",
3086                        qinfo->GetQuestId(),j+1,j+1,qinfo->ReqSourceCount[j]);
3087                    // no changes, quest ignore this data
3088                }
3089
3090                if(qinfo->ReqSourceRef[j]>0)
3091                {
3092                    sLog.outErrorDb("Quest %u has `ReqSourceId%d` = 0 but `ReqSourceRef%d` = %u.",
3093                        qinfo->GetQuestId(),j+1,j+1,qinfo->ReqSourceRef[j]);
3094                    // no changes, quest ignore this data
3095                }
3096            }
3097        }
3098
3099        for(int j = 0; j < QUEST_SOURCE_ITEM_IDS_COUNT; ++j )
3100        {
3101            uint32 ref = qinfo->ReqSourceRef[j];
3102            if(ref)
3103            {
3104                if(ref > QUEST_OBJECTIVES_COUNT)
3105                {
3106                    sLog.outErrorDb("Quest %u has `ReqSourceRef%d` = %u but max value in `ReqSourceRef%d` is %u, quest can't be done.",
3107                        qinfo->GetQuestId(),j+1,ref,j+1,QUEST_OBJECTIVES_COUNT);
3108                    // no changes, quest can't be done for this requirement
3109                }
3110                else
3111                if(!qinfo->ReqItemId[ref-1] && !qinfo->ReqSpell[ref-1])
3112                {
3113                    sLog.outErrorDb("Quest %u has `ReqSourceRef%d` = %u but `ReqItemId%u` = 0 and `ReqSpellCast%u` = 0, quest can't be done.",
3114                        qinfo->GetQuestId(),j+1,ref,ref,ref);
3115                    // no changes, quest can't be done for this requirement
3116                }
3117                else if(qinfo->ReqItemId[ref-1] && qinfo->ReqSpell[ref-1])
3118                {
3119                    sLog.outErrorDb("Quest %u has `ReqItemId%u` = %u and `ReqSpellCast%u` = %u, quest can't have both fields <> 0, then can't be done.",
3120                        qinfo->GetQuestId(),ref,qinfo->ReqItemId[ref-1],ref,qinfo->ReqSpell[ref-1]);
3121                    // no changes, quest can't be done for this requirement
3122                    qinfo->ReqSourceId[j] = 0;              // prevent incorrect work of quest
3123                }
3124            }
3125        }
3126
3127        for(int j = 0; j < QUEST_OBJECTIVES_COUNT; ++j )
3128        {
3129            uint32 id = qinfo->ReqSpell[j];
3130            if(id)
3131            {
3132                SpellEntry const* spellInfo = sSpellStore.LookupEntry(id);
3133                if(!spellInfo)
3134                {
3135                    sLog.outErrorDb("Quest %u has `ReqSpellCast%d` = %u but spell %u does not exist, quest can't be done.",
3136                        qinfo->GetQuestId(),j+1,id,id);
3137                    // no changes, quest can't be done for this requirement
3138                }
3139
3140                if(!qinfo->ReqCreatureOrGOId[j])
3141                {
3142                    bool found = false;
3143                    for(int k = 0; k < 3; ++k)
3144                    {
3145                        if( spellInfo->Effect[k]==SPELL_EFFECT_QUEST_COMPLETE && uint32(spellInfo->EffectMiscValue[k])==qinfo->QuestId ||
3146                            spellInfo->Effect[k]==SPELL_EFFECT_SEND_EVENT)
3147                        {
3148                            found = true;
3149                            break;
3150                        }
3151                    }
3152
3153                    if(found)
3154                    {
3155                        if(!qinfo->HasFlag(QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT))
3156                        {
3157                            sLog.outErrorDb("Spell (id: %u) have SPELL_EFFECT_QUEST_COMPLETE or SPELL_EFFECT_SEND_EVENT for quest %u and ReqCreatureOrGOId%d = 0, but quest not have flag QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT. Quest flags or ReqCreatureOrGOId%d must be fixed, quest modified to enable objective.",spellInfo->Id,qinfo->QuestId,j+1,j+1);
3158
3159                            // this will prevent quest completing without objective
3160                            const_cast<Quest*>(qinfo)->SetFlag(QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT);
3161                        }
3162                    }
3163                    else
3164                    {
3165                        sLog.outErrorDb("Quest %u has `ReqSpellCast%d` = %u and ReqCreatureOrGOId%d = 0 but spell %u does not have SPELL_EFFECT_QUEST_COMPLETE or SPELL_EFFECT_SEND_EVENT effect for this quest, quest can't be done.",
3166                            qinfo->GetQuestId(),j+1,id,j+1,id);
3167                        // no changes, quest can't be done for this requirement
3168                    }
3169                }
3170            }
3171        }
3172
3173        for(int j = 0; j < QUEST_OBJECTIVES_COUNT; ++j )
3174        {
3175            int32 id = qinfo->ReqCreatureOrGOId[j];
3176            if(id < 0 && !sGOStorage.LookupEntry<GameObjectInfo>(-id))
3177            {
3178                sLog.outErrorDb("Quest %u has `ReqCreatureOrGOId%d` = %i but gameobject %u does not exist, quest can't be done.",
3179                    qinfo->GetQuestId(),j+1,id,uint32(-id));
3180                qinfo->ReqCreatureOrGOId[j] = 0;            // quest can't be done for this requirement
3181            }
3182
3183            if(id > 0 && !sCreatureStorage.LookupEntry<CreatureInfo>(id))
3184            {
3185                sLog.outErrorDb("Quest %u has `ReqCreatureOrGOId%d` = %i but creature with entry %u does not exist, quest can't be done.",
3186                    qinfo->GetQuestId(),j+1,id,uint32(id));
3187                qinfo->ReqCreatureOrGOId[j] = 0;            // quest can't be done for this requirement
3188            }
3189
3190            if(id)
3191            {
3192                // In fact SpeakTo and Kill are quite same: either you can speak to mob:SpeakTo or you can't:Kill/Cast
3193
3194                qinfo->SetFlag(QUEST_TRINITY_FLAGS_KILL_OR_CAST | QUEST_TRINITY_FLAGS_SPEAKTO);
3195
3196                if(!qinfo->ReqCreatureOrGOCount[j])
3197                {
3198                    sLog.outErrorDb("Quest %u has `ReqCreatureOrGOId%d` = %u but `ReqCreatureOrGOCount%d` = 0, quest can't be done.",
3199                        qinfo->GetQuestId(),j+1,id,j+1);
3200                    // no changes, quest can be incorrectly done, but we already report this
3201                }
3202            }
3203            else if(qinfo->ReqCreatureOrGOCount[j]>0)
3204            {
3205                sLog.outErrorDb("Quest %u has `ReqCreatureOrGOId%d` = 0 but `ReqCreatureOrGOCount%d` = %u.",
3206                    qinfo->GetQuestId(),j+1,j+1,qinfo->ReqCreatureOrGOCount[j]);
3207                // no changes, quest ignore this data
3208            }
3209        }
3210
3211        for(int j = 0; j < QUEST_REWARD_CHOICES_COUNT; ++j )
3212        {
3213            uint32 id = qinfo->RewChoiceItemId[j];
3214            if(id)
3215            {
3216                if(!sItemStorage.LookupEntry<ItemPrototype>(id))
3217                {
3218                    sLog.outErrorDb("Quest %u has `RewChoiceItemId%d` = %u but item with entry %u does not exist, quest will not reward this item.",
3219                        qinfo->GetQuestId(),j+1,id,id);
3220                    qinfo->RewChoiceItemId[j] = 0;          // no changes, quest will not reward this
3221                }
3222
3223                if(!qinfo->RewChoiceItemCount[j])
3224                {
3225                    sLog.outErrorDb("Quest %u has `RewChoiceItemId%d` = %u but `RewChoiceItemCount%d` = 0, quest can't be done.",
3226                        qinfo->GetQuestId(),j+1,id,j+1);
3227                    // no changes, quest can't be done
3228                }
3229            }
3230            else if(qinfo->RewChoiceItemCount[j]>0)
3231            {
3232                sLog.outErrorDb("Quest %u has `RewChoiceItemId%d` = 0 but `RewChoiceItemCount%d` = %u.",
3233                    qinfo->GetQuestId(),j+1,j+1,qinfo->RewChoiceItemCount[j]);
3234                // no changes, quest ignore this data
3235            }
3236        }
3237
3238        for(int j = 0; j < QUEST_REWARDS_COUNT; ++j )
3239        {
3240            uint32 id = qinfo->RewItemId[j];
3241            if(id)
3242            {
3243                if(!sItemStorage.LookupEntry<ItemPrototype>(id))
3244                {
3245                    sLog.outErrorDb("Quest %u has `RewItemId%d` = %u but item with entry %u does not exist, quest will not reward this item.",
3246                        qinfo->GetQuestId(),j+1,id,id);
3247                    qinfo->RewItemId[j] = 0;                // no changes, quest will not reward this item
3248                }
3249
3250                if(!qinfo->RewItemCount[j])
3251                {
3252                    sLog.outErrorDb("Quest %u has `RewItemId%d` = %u but `RewItemCount%d` = 0, quest will not reward this item.",
3253                        qinfo->GetQuestId(),j+1,id,j+1);
3254                    // no changes
3255                }
3256            }
3257            else if(qinfo->RewItemCount[j]>0)
3258            {
3259                sLog.outErrorDb("Quest %u has `RewItemId%d` = 0 but `RewItemCount%d` = %u.",
3260                    qinfo->GetQuestId(),j+1,j+1,qinfo->RewItemCount[j]);
3261                // no changes, quest ignore this data
3262            }
3263        }
3264
3265        for(int j = 0; j < QUEST_REPUTATIONS_COUNT; ++j)
3266        {
3267            if(qinfo->RewRepFaction[j])
3268            {
3269                if(!qinfo->RewRepValue[j])
3270                {
3271                    sLog.outErrorDb("Quest %u has `RewRepFaction%d` = %u but `RewRepValue%d` = 0, quest will not reward this reputation.",
3272                        qinfo->GetQuestId(),j+1,qinfo->RewRepValue[j],j+1);
3273                    // no changes
3274                }
3275
3276                if(!sFactionStore.LookupEntry(qinfo->RewRepFaction[j]))
3277                {
3278                    sLog.outErrorDb("Quest %u has `RewRepFaction%d` = %u but raw faction (faction.dbc) %u does not exist, quest will not reward reputation for this faction.",
3279                        qinfo->GetQuestId(),j+1,qinfo->RewRepFaction[j] ,qinfo->RewRepFaction[j] );
3280                    qinfo->RewRepFaction[j] = 0;            // quest will not reward this
3281                }
3282            }
3283            else if(qinfo->RewRepValue[j]!=0)
3284            {
3285                sLog.outErrorDb("Quest %u has `RewRepFaction%d` = 0 but `RewRepValue%d` = %u.",
3286                    qinfo->GetQuestId(),j+1,j+1,qinfo->RewRepValue[j]);
3287                // no changes, quest ignore this data
3288            }
3289        }
3290
3291        if(qinfo->RewSpell)
3292        {
3293            SpellEntry const* spellInfo = sSpellStore.LookupEntry(qinfo->RewSpell);
3294
3295            if(!spellInfo)
3296            {
3297                sLog.outErrorDb("Quest %u has `RewSpell` = %u but spell %u does not exist, spell removed as display reward.",
3298                    qinfo->GetQuestId(),qinfo->RewSpell,qinfo->RewSpell);
3299                qinfo->RewSpell = 0;                        // no spell reward will display for this quest
3300            }
3301
3302            else if(!SpellMgr::IsSpellValid(spellInfo))
3303            {
3304                sLog.outErrorDb("Quest %u has `RewSpell` = %u but spell %u is broken, quest can't be done.",
3305                    qinfo->GetQuestId(),qinfo->RewSpell,qinfo->RewSpell);
3306                qinfo->RewSpell = 0;                        // no spell reward will display for this quest
3307            }
3308
3309        }
3310
3311        if(qinfo->RewSpellCast)
3312        {
3313            SpellEntry const* spellInfo = sSpellStore.LookupEntry(qinfo->RewSpellCast);
3314
3315            if(!spellInfo)
3316            {
3317                sLog.outErrorDb("Quest %u has `RewSpellCast` = %u but spell %u does not exist, quest will not have a spell reward.",
3318                    qinfo->GetQuestId(),qinfo->RewSpellCast,qinfo->RewSpellCast);
3319                qinfo->RewSpellCast = 0;                    // no spell will be casted on player
3320            }
3321
3322            else if(!SpellMgr::IsSpellValid(spellInfo))
3323            {
3324                sLog.outErrorDb("Quest %u has `RewSpellCast` = %u but spell %u is broken, quest can't be done.",
3325                    qinfo->GetQuestId(),qinfo->RewSpellCast,qinfo->RewSpellCast);
3326                qinfo->RewSpellCast = 0;                    // no spell will be casted on player
3327            }
3328
3329        }
3330
3331        if(qinfo->RewMailTemplateId)
3332        {
3333            if(!sMailTemplateStore.LookupEntry(qinfo->RewMailTemplateId))
3334            {
3335                sLog.outErrorDb("Quest %u has `RewMailTemplateId` = %u but mail template  %u does not exist, quest will not have a mail reward.",
3336                    qinfo->GetQuestId(),qinfo->RewMailTemplateId,qinfo->RewMailTemplateId);
3337                qinfo->RewMailTemplateId = 0;               // no mail will send to player
3338                qinfo->RewMailDelaySecs = 0;                // no mail will send to player
3339            }
3340        }
3341
3342        if(qinfo->NextQuestInChain)
3343        {
3344            if(mQuestTemplates.find(qinfo->NextQuestInChain) == mQuestTemplates.end())
3345            {
3346                sLog.outErrorDb("Quest %u has `NextQuestInChain` = %u but quest %u does not exist, quest chain will not work.",
3347                    qinfo->GetQuestId(),qinfo->NextQuestInChain ,qinfo->NextQuestInChain );
3348                qinfo->NextQuestInChain = 0;
3349            }
3350            else
3351                mQuestTemplates[qinfo->NextQuestInChain]->prevChainQuests.push_back(qinfo->GetQuestId());
3352        }
3353
3354        // fill additional data stores
3355        if(qinfo->PrevQuestId)
3356        {
3357            if (mQuestTemplates.find(abs(qinfo->GetPrevQuestId())) == mQuestTemplates.end())
3358            {
3359                sLog.outErrorDb("Quest %d has PrevQuestId %i, but no such quest", qinfo->GetQuestId(), qinfo->GetPrevQuestId());
3360            }
3361            else
3362            {
3363                qinfo->prevQuests.push_back(qinfo->PrevQuestId);
3364            }
3365        }
3366
3367        if(qinfo->NextQuestId)
3368        {
3369            if (mQuestTemplates.find(abs(qinfo->GetNextQuestId())) == mQuestTemplates.end())
3370            {
3371                sLog.outErrorDb("Quest %d has NextQuestId %i, but no such quest", qinfo->GetQuestId(), qinfo->GetNextQuestId());
3372            }
3373            else
3374            {
3375                int32 signedQuestId = qinfo->NextQuestId < 0 ? -int32(qinfo->GetQuestId()) : int32(qinfo->GetQuestId());
3376                mQuestTemplates[abs(qinfo->GetNextQuestId())]->prevQuests.push_back(signedQuestId);
3377            }
3378        }
3379
3380        if(qinfo->ExclusiveGroup)
3381            mExclusiveQuestGroups.insert(std::pair<int32, uint32>(qinfo->ExclusiveGroup, qinfo->GetQuestId()));
3382        if(qinfo->LimitTime)
3383            qinfo->SetFlag(QUEST_TRINITY_FLAGS_TIMED);
3384    }
3385
3386    // check QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT for spell with SPELL_EFFECT_QUEST_COMPLETE
3387    for (uint32 i = 0; i < sSpellStore.GetNumRows(); ++i)
3388    {
3389        SpellEntry const *spellInfo = sSpellStore.LookupEntry(i);
3390        if(!spellInfo)
3391            continue;
3392
3393        for(int j = 0; j < 3; ++j)
3394        {
3395            if(spellInfo->Effect[j] != SPELL_EFFECT_QUEST_COMPLETE)
3396                continue;
3397
3398            uint32 quest_id = spellInfo->EffectMiscValue[j];
3399
3400            Quest const* quest = GetQuestTemplate(quest_id);
3401
3402            // some quest referenced in spells not exist (outdataed spells)
3403            if(!quest)
3404                continue;
3405
3406            if(!quest->HasFlag(QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT))
3407            {
3408                sLog.outErrorDb("Spell (id: %u) have SPELL_EFFECT_QUEST_COMPLETE for quest %u , but quest not have flag QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT. Quest flags must be fixed, quest modified to enable objective.",spellInfo->Id,quest_id);
3409
3410                // this will prevent quest completing without objective
3411                const_cast<Quest*>(quest)->SetFlag(QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT);
3412            }
3413        }
3414    }
3415
3416    sLog.outString();
3417    sLog.outString( ">> Loaded %u quests definitions", mQuestTemplates.size() );
3418}
3419
3420void ObjectMgr::LoadQuestLocales()
3421{
3422    QueryResult *result = WorldDatabase.Query("SELECT entry,"
3423        "Title_loc1,Details_loc1,Objectives_loc1,OfferRewardText_loc1,RequestItemsText_loc1,EndText_loc1,ObjectiveText1_loc1,ObjectiveText2_loc1,ObjectiveText3_loc1,ObjectiveText4_loc1,"
3424        "Title_loc2,Details_loc2,Objectives_loc2,OfferRewardText_loc2,RequestItemsText_loc2,EndText_loc2,ObjectiveText1_loc2,ObjectiveText2_loc2,ObjectiveText3_loc2,ObjectiveText4_loc2,"
3425        "Title_loc3,Details_loc3,Objectives_loc3,OfferRewardText_loc3,RequestItemsText_loc3,EndText_loc3,ObjectiveText1_loc3,ObjectiveText2_loc3,ObjectiveText3_loc3,ObjectiveText4_loc3,"
3426        "Title_loc4,Details_loc4,Objectives_loc4,OfferRewardText_loc4,RequestItemsText_loc4,EndText_loc4,ObjectiveText1_loc4,ObjectiveText2_loc4,ObjectiveText3_loc4,ObjectiveText4_loc4,"
3427        "Title_loc5,Details_loc5,Objectives_loc5,OfferRewardText_loc5,RequestItemsText_loc5,EndText_loc5,ObjectiveText1_loc5,ObjectiveText2_loc5,ObjectiveText3_loc5,ObjectiveText4_loc5,"
3428        "Title_loc6,Details_loc6,Objectives_loc6,OfferRewardText_loc6,RequestItemsText_loc6,EndText_loc6,ObjectiveText1_loc6,ObjectiveText2_loc6,ObjectiveText3_loc6,ObjectiveText4_loc6,"
3429        "Title_loc7,Details_loc7,Objectives_loc7,OfferRewardText_loc7,RequestItemsText_loc7,EndText_loc7,ObjectiveText1_loc7,ObjectiveText2_loc7,ObjectiveText3_loc7,ObjectiveText4_loc7,"
3430        "Title_loc8,Details_loc8,Objectives_loc8,OfferRewardText_loc8,RequestItemsText_loc8,EndText_loc8,ObjectiveText1_loc8,ObjectiveText2_loc8,ObjectiveText3_loc8,ObjectiveText4_loc8"
3431        " FROM locales_quest"
3432        );
3433
3434    if(!result)
3435    {
3436        barGoLink bar(1);
3437
3438        bar.step();
3439
3440        sLog.outString("");
3441        sLog.outString(">> Loaded 0 Quest locale strings. DB table `locales_quest` is empty.");
3442        return;
3443    }
3444
3445    barGoLink bar(result->GetRowCount());
3446
3447    do
3448    {
3449        Field *fields = result->Fetch();
3450        bar.step();
3451
3452        uint32 entry = fields[0].GetUInt32();
3453
3454        QuestLocale& data = mQuestLocaleMap[entry];
3455
3456        for(int i = 1; i < MAX_LOCALE; ++i)
3457        {
3458            std::string str = fields[1+10*(i-1)].GetCppString();
3459            if(!str.empty())
3460            {
3461                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
3462                if(idx >= 0)
3463                {
3464                    if(data.Title.size() <= idx)
3465                        data.Title.resize(idx+1);
3466
3467                    data.Title[idx] = str;
3468                }
3469            }
3470            str = fields[1+10*(i-1)+1].GetCppString();
3471            if(!str.empty())
3472            {
3473                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
3474                if(idx >= 0)
3475                {
3476                    if(data.Details.size() <= idx)
3477                        data.Details.resize(idx+1);
3478
3479                    data.Details[idx] = str;
3480                }
3481            }
3482            str = fields[1+10*(i-1)+2].GetCppString();
3483            if(!str.empty())
3484            {
3485                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
3486                if(idx >= 0)
3487                {
3488                    if(data.Objectives.size() <= idx)
3489                        data.Objectives.resize(idx+1);
3490
3491                    data.Objectives[idx] = str;
3492                }
3493            }
3494            str = fields[1+10*(i-1)+3].GetCppString();
3495            if(!str.empty())
3496            {
3497                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
3498                if(idx >= 0)
3499                {
3500                    if(data.OfferRewardText.size() <= idx)
3501                        data.OfferRewardText.resize(idx+1);
3502
3503                    data.OfferRewardText[idx] = str;
3504                }
3505            }
3506            str = fields[1+10*(i-1)+4].GetCppString();
3507            if(!str.empty())
3508            {
3509                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
3510                if(idx >= 0)
3511                {
3512                    if(data.RequestItemsText.size() <= idx)
3513                        data.RequestItemsText.resize(idx+1);
3514
3515                    data.RequestItemsText[idx] = str;
3516                }
3517            }
3518            str = fields[1+10*(i-1)+5].GetCppString();
3519            if(!str.empty())
3520            {
3521                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
3522                if(idx >= 0)
3523                {
3524                    if(data.EndText.size() <= idx)
3525                        data.EndText.resize(idx+1);
3526
3527                    data.EndText[idx] = str;
3528                }
3529            }
3530            for(int k = 0; k < 4; ++k)
3531            {
3532                str = fields[1+10*(i-1)+6+k].GetCppString();
3533                if(!str.empty())
3534                {
3535                    int idx = GetOrNewIndexForLocale(LocaleConstant(i));
3536                    if(idx >= 0)
3537                    {
3538                        if(data.ObjectiveText[k].size() <= idx)
3539                            data.ObjectiveText[k].resize(idx+1);
3540
3541                        data.ObjectiveText[k][idx] = str;
3542                    }
3543                }
3544            }
3545        }
3546    } while (result->NextRow());
3547
3548    delete result;
3549
3550    sLog.outString();
3551    sLog.outString( ">> Loaded %u Quest locale strings", mQuestLocaleMap.size() );
3552}
3553
3554void ObjectMgr::LoadPetCreateSpells()
3555{
3556    QueryResult *result = WorldDatabase.PQuery("SELECT entry, Spell1, Spell2, Spell3, Spell4 FROM petcreateinfo_spell");
3557    if(!result)
3558    {
3559        barGoLink bar( 1 );
3560        bar.step();
3561
3562        sLog.outString();
3563        sLog.outString( ">> Loaded 0 pet create spells" );
3564        sLog.outErrorDb("`petcreateinfo_spell` table is empty!");
3565        return;
3566    }
3567
3568    uint32 count = 0;
3569
3570    barGoLink bar( result->GetRowCount() );
3571
3572    mPetCreateSpell.clear();
3573
3574    do
3575    {
3576        Field *fields = result->Fetch();
3577        bar.step();
3578
3579        uint32 creature_id = fields[0].GetUInt32();
3580
3581        if(!creature_id || !sCreatureStorage.LookupEntry<CreatureInfo>(creature_id))
3582            continue;
3583
3584        PetCreateSpellEntry PetCreateSpell;
3585        for(int i = 0; i < 4; i++)
3586        {
3587            PetCreateSpell.spellid[i] = fields[i + 1].GetUInt32();
3588
3589            if(PetCreateSpell.spellid[i] && !sSpellStore.LookupEntry(PetCreateSpell.spellid[i]))
3590                sLog.outErrorDb("Spell %u listed in `petcreateinfo_spell` does not exist",PetCreateSpell.spellid[i]);
3591        }
3592
3593        mPetCreateSpell[creature_id] = PetCreateSpell;
3594
3595        ++count;
3596    }
3597    while (result->NextRow());
3598
3599    delete result;
3600
3601    sLog.outString();
3602    sLog.outString( ">> Loaded %u pet create spells", count );
3603}
3604
3605void ObjectMgr::LoadScripts(ScriptMapMap& scripts, char const* tablename)
3606{
3607    if(sWorld.IsScriptScheduled())                          // function don't must be called in time scripts use.
3608        return;
3609
3610    sLog.outString( "%s :", tablename);
3611
3612    scripts.clear();                                        // need for reload support
3613
3614    QueryResult *result = WorldDatabase.PQuery( "SELECT id,delay,command,datalong,datalong2,datatext, x, y, z, o FROM %s", tablename );
3615
3616    uint32 count = 0;
3617
3618    if( !result )
3619    {
3620        barGoLink bar( 1 );
3621        bar.step();
3622
3623        sLog.outString();
3624        sLog.outString( ">> Loaded %u script definitions", count );
3625        return;
3626    }
3627
3628    barGoLink bar( result->GetRowCount() );
3629
3630    do
3631    {
3632        bar.step();
3633
3634        Field *fields = result->Fetch();
3635        ScriptInfo tmp;
3636        tmp.id = fields[0].GetUInt32();
3637        tmp.delay = fields[1].GetUInt32();
3638        tmp.command = fields[2].GetUInt32();
3639        tmp.datalong = fields[3].GetUInt32();
3640        tmp.datalong2 = fields[4].GetUInt32();
3641        tmp.datatext = fields[5].GetCppString();
3642        tmp.x = fields[6].GetFloat();
3643        tmp.y = fields[7].GetFloat();
3644        tmp.z = fields[8].GetFloat();
3645        tmp.o = fields[9].GetFloat();
3646
3647        // generic command args check
3648        switch(tmp.command)
3649        {
3650            case SCRIPT_COMMAND_TALK:
3651            {
3652                if(tmp.datalong > 3)
3653                {
3654                    sLog.outErrorDb("Table `%s` has invalid talk type (datalong = %u) in SCRIPT_COMMAND_TALK for script id %u",tablename,tmp.datalong,tmp.id);
3655                    continue;
3656                }
3657                break;
3658            }
3659
3660            case SCRIPT_COMMAND_TELEPORT_TO:
3661            {
3662                if(!sMapStore.LookupEntry(tmp.datalong))
3663                {
3664                    sLog.outErrorDb("Table `%s` has invalid map (Id: %u) in SCRIPT_COMMAND_TELEPORT_TO for script id %u",tablename,tmp.datalong,tmp.id);
3665                    continue;
3666                }
3667
3668                if(!Trinity::IsValidMapCoord(tmp.x,tmp.y,tmp.z,tmp.o))
3669                {
3670                    sLog.outErrorDb("Table `%s` has invalid coordinates (X: %f Y: %f) in SCRIPT_COMMAND_TELEPORT_TO for script id %u",tablename,tmp.x,tmp.y,tmp.id);
3671                    continue;
3672                }
3673                break;
3674            }
3675
3676            case SCRIPT_COMMAND_TEMP_SUMMON_CREATURE:
3677            {
3678                if(!Trinity::IsValidMapCoord(tmp.x,tmp.y,tmp.z,tmp.o))
3679                {
3680                    sLog.outErrorDb("Table `%s` has invalid coordinates (X: %f Y: %f) in SCRIPT_COMMAND_TEMP_SUMMON_CREATURE for script id %u",tablename,tmp.x,tmp.y,tmp.id);
3681                    continue;
3682                }
3683
3684                if(!GetCreatureTemplate(tmp.datalong))
3685                {
3686                    sLog.outErrorDb("Table `%s` has invalid creature (Entry: %u) in SCRIPT_COMMAND_TEMP_SUMMON_CREATURE for script id %u",tablename,tmp.datalong,tmp.id);
3687                    continue;
3688                }
3689                break;
3690            }
3691
3692            case SCRIPT_COMMAND_RESPAWN_GAMEOBJECT:
3693            {
3694                GameObjectData const* data = GetGOData(tmp.datalong);
3695                if(!data)
3696                {
3697                    sLog.outErrorDb("Table `%s` has invalid gameobject (GUID: %u) in SCRIPT_COMMAND_RESPAWN_GAMEOBJECT for script id %u",tablename,tmp.datalong,tmp.id);
3698                    continue;
3699                }
3700
3701                GameObjectInfo const* info = GetGameObjectInfo(data->id);
3702                if(!info)
3703                {
3704                    sLog.outErrorDb("Table `%s` has gameobject with invalid entry (GUID: %u Entry: %u) in SCRIPT_COMMAND_RESPAWN_GAMEOBJECT for script id %u",tablename,tmp.datalong,data->id,tmp.id);
3705                    continue;
3706                }
3707
3708                if( info->type==GAMEOBJECT_TYPE_FISHINGNODE ||
3709                    info->type==GAMEOBJECT_TYPE_FISHINGHOLE ||
3710                    info->type==GAMEOBJECT_TYPE_DOOR        ||
3711                    info->type==GAMEOBJECT_TYPE_BUTTON      ||
3712                    info->type==GAMEOBJECT_TYPE_TRAP )
3713                {
3714                    sLog.outErrorDb("Table `%s` have gameobject type (%u) unsupported by command SCRIPT_COMMAND_RESPAWN_GAMEOBJECT for script id %u",tablename,info->id,tmp.id);
3715                    continue;
3716                }
3717                break;
3718            }
3719            case SCRIPT_COMMAND_OPEN_DOOR:
3720            case SCRIPT_COMMAND_CLOSE_DOOR:
3721            {
3722                GameObjectData const* data = GetGOData(tmp.datalong);
3723                if(!data)
3724                {
3725                    sLog.outErrorDb("Table `%s` has invalid gameobject (GUID: %u) in %s for script id %u",tablename,tmp.datalong,(tmp.command==SCRIPT_COMMAND_OPEN_DOOR ? "SCRIPT_COMMAND_OPEN_DOOR" : "SCRIPT_COMMAND_CLOSE_DOOR"),tmp.id);
3726                    continue;
3727                }
3728
3729                GameObjectInfo const* info = GetGameObjectInfo(data->id);
3730                if(!info)
3731                {
3732                    sLog.outErrorDb("Table `%s` has gameobject with invalid entry (GUID: %u Entry: %u) in %s for script id %u",tablename,tmp.datalong,data->id,(tmp.command==SCRIPT_COMMAND_OPEN_DOOR ? "SCRIPT_COMMAND_OPEN_DOOR" : "SCRIPT_COMMAND_CLOSE_DOOR"),tmp.id);
3733                    continue;
3734                }
3735
3736                if( info->type!=GAMEOBJECT_TYPE_DOOR)
3737                {
3738                    sLog.outErrorDb("Table `%s` has gameobject type (%u) non supported by command %s for script id %u",tablename,info->id,(tmp.command==SCRIPT_COMMAND_OPEN_DOOR ? "SCRIPT_COMMAND_OPEN_DOOR" : "SCRIPT_COMMAND_CLOSE_DOOR"),tmp.id);
3739                    continue;
3740                }
3741
3742                break;
3743            }
3744            case SCRIPT_COMMAND_QUEST_EXPLORED:
3745            {
3746                Quest const* quest = GetQuestTemplate(tmp.datalong);
3747                if(!quest)
3748                {
3749                    sLog.outErrorDb("Table `%s` has invalid quest (ID: %u) in SCRIPT_COMMAND_QUEST_EXPLORED in `datalong` for script id %u",tablename,tmp.datalong,tmp.id);
3750                    continue;
3751                }
3752
3753                if(!quest->HasFlag(QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT))
3754                {
3755                    sLog.outErrorDb("Table `%s` has quest (ID: %u) in SCRIPT_COMMAND_QUEST_EXPLORED in `datalong` for script id %u, but quest not have flag QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT in quest flags. Script command or quest flags wrong. Quest modified to require objective.",tablename,tmp.datalong,tmp.id);
3756
3757                    // this will prevent quest completing without objective
3758                    const_cast<Quest*>(quest)->SetFlag(QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT);
3759
3760                    // continue; - quest objective requiremet set and command can be allowed
3761                }
3762
3763                if(float(tmp.datalong2) > DEFAULT_VISIBILITY_DISTANCE)
3764                {
3765                    sLog.outErrorDb("Table `%s` has too large distance (%u) for exploring objective complete in `datalong2` in SCRIPT_COMMAND_QUEST_EXPLORED in `datalong` for script id %u",tablename,tmp.datalong2,tmp.id);
3766                    continue;
3767                }
3768
3769                if(tmp.datalong2 && float(tmp.datalong2) > DEFAULT_VISIBILITY_DISTANCE)
3770                {
3771                    sLog.outErrorDb("Table `%s` has too large distance (%u) for exploring objective complete in `datalong2` in SCRIPT_COMMAND_QUEST_EXPLORED in `datalong` for script id %u, max distance is %u or 0 for disable distance check",tablename,tmp.datalong2,tmp.id,uint32(DEFAULT_VISIBILITY_DISTANCE));
3772                    continue;
3773                }
3774
3775                if(tmp.datalong2 && float(tmp.datalong2) < INTERACTION_DISTANCE)
3776                {
3777                    sLog.outErrorDb("Table `%s` has too small distance (%u) for exploring objective complete in `datalong2` in SCRIPT_COMMAND_QUEST_EXPLORED in `datalong` for script id %u, min distance is %u or 0 for disable distance check",tablename,tmp.datalong2,tmp.id,uint32(INTERACTION_DISTANCE));
3778                    continue;
3779                }
3780
3781                break;
3782            }
3783
3784            case SCRIPT_COMMAND_REMOVE_AURA:
3785            case SCRIPT_COMMAND_CAST_SPELL:
3786            {
3787                if(!sSpellStore.LookupEntry(tmp.datalong))
3788                {
3789                    sLog.outErrorDb("Table `%s` using non-existent spell (id: %u) in SCRIPT_COMMAND_REMOVE_AURA or SCRIPT_COMMAND_CAST_SPELL for script id %u",tablename,tmp.datalong,tmp.id);
3790                    continue;
3791                }
3792                break;
3793            }
3794        }
3795
3796        if (scripts.find(tmp.id) == scripts.end())
3797        {
3798            ScriptMap emptyMap;
3799            scripts[tmp.id] = emptyMap;
3800        }
3801        scripts[tmp.id].insert(std::pair<uint32, ScriptInfo>(tmp.delay, tmp));
3802
3803        ++count;
3804    } while( result->NextRow() );
3805
3806    delete result;
3807
3808    sLog.outString();
3809    sLog.outString( ">> Loaded %u script definitions", count );
3810}
3811
3812void ObjectMgr::LoadGameObjectScripts()
3813{
3814    LoadScripts(sGameObjectScripts,    "gameobject_scripts");
3815
3816    // check ids
3817    for(ScriptMapMap::const_iterator itr = sGameObjectScripts.begin(); itr != sGameObjectScripts.end(); ++itr)
3818    {
3819        if(!GetGOData(itr->first))
3820            sLog.outErrorDb("Table `gameobject_scripts` has not existing gameobject (GUID: %u) as script id",itr->first);
3821    }
3822}
3823
3824void ObjectMgr::LoadQuestEndScripts()
3825{
3826    LoadScripts(sQuestEndScripts,  "quest_end_scripts");
3827
3828    // check ids
3829    for(ScriptMapMap::const_iterator itr = sQuestEndScripts.begin(); itr != sQuestEndScripts.end(); ++itr)
3830    {
3831        if(!GetQuestTemplate(itr->first))
3832            sLog.outErrorDb("Table `quest_end_scripts` has not existing quest (Id: %u) as script id",itr->first);
3833    }
3834}
3835
3836void ObjectMgr::LoadQuestStartScripts()
3837{
3838    LoadScripts(sQuestStartScripts,"quest_start_scripts");
3839
3840    // check ids
3841    for(ScriptMapMap::const_iterator itr = sQuestStartScripts.begin(); itr != sQuestStartScripts.end(); ++itr)
3842    {
3843        if(!GetQuestTemplate(itr->first))
3844            sLog.outErrorDb("Table `quest_start_scripts` has not existing quest (Id: %u) as script id",itr->first);
3845    }
3846}
3847
3848void ObjectMgr::LoadSpellScripts()
3849{
3850    LoadScripts(sSpellScripts, "spell_scripts");
3851
3852    // check ids
3853    for(ScriptMapMap::const_iterator itr = sSpellScripts.begin(); itr != sSpellScripts.end(); ++itr)
3854    {
3855        SpellEntry const* spellInfo = sSpellStore.LookupEntry(itr->first);
3856
3857        if(!spellInfo)
3858        {
3859            sLog.outErrorDb("Table `spell_scripts` has not existing spell (Id: %u) as script id",itr->first);
3860            continue;
3861        }
3862
3863        //check for correct spellEffect
3864        bool found = false;
3865        for(int i=0; i<3; ++i)
3866        {
3867            // skip empty effects
3868            if( !spellInfo->Effect[i] )
3869                continue;
3870
3871            if( spellInfo->Effect[i] == SPELL_EFFECT_SCRIPT_EFFECT )
3872            {
3873                found =  true;
3874                break;
3875            }
3876        }
3877
3878        if(!found)
3879            sLog.outErrorDb("Table `spell_scripts` has unsupported spell (Id: %u) without SPELL_EFFECT_SCRIPT_EFFECT (%u) spell effect",itr->first,SPELL_EFFECT_SCRIPT_EFFECT);
3880    }
3881}
3882
3883void ObjectMgr::LoadEventScripts()
3884{
3885    LoadScripts(sEventScripts, "event_scripts");
3886
3887    std::set<uint32> evt_scripts;
3888    // Load all possible script entries from gameobjects
3889    for(uint32 i = 1; i < sGOStorage.MaxEntry; ++i)
3890    {
3891        GameObjectInfo const * goInfo = sGOStorage.LookupEntry<GameObjectInfo>(i);
3892        if (goInfo)
3893        {
3894            switch(goInfo->type)
3895            {
3896                case GAMEOBJECT_TYPE_GOOBER:
3897                    if(goInfo->goober.eventId)
3898                        evt_scripts.insert(goInfo->goober.eventId);
3899                    break;
3900                case GAMEOBJECT_TYPE_CHEST:
3901                    if(goInfo->chest.eventId)
3902                        evt_scripts.insert(goInfo->chest.eventId);
3903                    break;
3904                default:
3905                    break;
3906            }
3907        }
3908    }
3909    // Load all possible script entries from spells
3910    for(uint32 i = 1; i < sSpellStore.GetNumRows(); ++i)
3911    {
3912        SpellEntry const * spell = sSpellStore.LookupEntry(i);
3913        if (spell)
3914        {
3915            for(int j=0; j<3; ++j)
3916            {
3917                if( spell->Effect[j] == SPELL_EFFECT_SEND_EVENT )
3918                {
3919                    if (spell->EffectMiscValue[j])
3920                        evt_scripts.insert(spell->EffectMiscValue[j]);
3921                }
3922            }
3923        }
3924    }
3925    // Then check if all scripts are in above list of possible script entries
3926    for(ScriptMapMap::const_iterator itr = sEventScripts.begin(); itr != sEventScripts.end(); ++itr)
3927    {
3928        std::set<uint32>::const_iterator itr2 = evt_scripts.find(itr->first);
3929        if (itr2 == evt_scripts.end())
3930            sLog.outErrorDb("Table `event_scripts` has script (Id: %u) not refering to any gameobject_template type 10 data2 field or type 3 data6 field or any spell effect %u", itr->first, SPELL_EFFECT_SEND_EVENT);
3931    }
3932}
3933
3934void ObjectMgr::LoadItemTexts()
3935{
3936    QueryResult *result = CharacterDatabase.PQuery("SELECT id, text FROM item_text");
3937
3938    uint32 count = 0;
3939
3940    if( !result )
3941    {
3942        barGoLink bar( 1 );
3943        bar.step();
3944
3945        sLog.outString();
3946        sLog.outString( ">> Loaded %u item pages", count );
3947        return;
3948    }
3949
3950    barGoLink bar( result->GetRowCount() );
3951
3952    Field* fields;
3953    do
3954    {
3955        bar.step();
3956
3957        fields = result->Fetch();
3958
3959        mItemTexts[ fields[0].GetUInt32() ] = fields[1].GetCppString();
3960
3961        ++count;
3962
3963    } while ( result->NextRow() );
3964
3965    delete result;
3966
3967    sLog.outString();
3968    sLog.outString( ">> Loaded %u item texts", count );
3969}
3970
3971void ObjectMgr::LoadPageTexts()
3972{
3973    sPageTextStore.Free();                                  // for reload case
3974
3975    sPageTextStore.Load();
3976    sLog.outString( ">> Loaded %u page texts", sPageTextStore.RecordCount );
3977    sLog.outString();
3978
3979    for(uint32 i = 1; i < sPageTextStore.MaxEntry; ++i)
3980    {
3981        // check data correctness
3982        PageText const* page = sPageTextStore.LookupEntry<PageText>(i);
3983        if(!page)
3984            continue;
3985
3986        if(page->Next_Page && !sPageTextStore.LookupEntry<PageText>(page->Next_Page))
3987        {
3988            sLog.outErrorDb("Page text (Id: %u) has not existing next page (Id:%u)", i,page->Next_Page);
3989            continue;
3990        }
3991
3992        // detect circular reference
3993        std::set<uint32> checkedPages;
3994        for(PageText const* pageItr = page; pageItr; pageItr = sPageTextStore.LookupEntry<PageText>(pageItr->Next_Page))
3995        {
3996            if(!pageItr->Next_Page)
3997                break;
3998            checkedPages.insert(pageItr->Page_ID);
3999            if(checkedPages.find(pageItr->Next_Page)!=checkedPages.end())
4000            {
4001                std::ostringstream ss;
4002                ss<< "The text page(s) ";
4003                for (std::set<uint32>::iterator itr= checkedPages.begin();itr!=checkedPages.end(); itr++)
4004                    ss << *itr << " ";
4005                ss << "create(s) a circular reference, which can cause the server to freeze. Changing Next_Page of page "
4006                    << pageItr->Page_ID <<" to 0";
4007                sLog.outErrorDb(ss.str().c_str());
4008                const_cast<PageText*>(pageItr)->Next_Page = 0;
4009                break;
4010            }
4011        }
4012    }
4013}
4014
4015void ObjectMgr::LoadPageTextLocales()
4016{
4017    QueryResult *result = WorldDatabase.PQuery("SELECT entry,text_loc1,text_loc2,text_loc3,text_loc4,text_loc5,text_loc6,text_loc7,text_loc8 FROM locales_page_text");
4018
4019    if(!result)
4020    {
4021        barGoLink bar(1);
4022
4023        bar.step();
4024
4025        sLog.outString("");
4026        sLog.outString(">> Loaded 0 PageText locale strings. DB table `locales_page_text` is empty.");
4027        return;
4028    }
4029
4030    barGoLink bar(result->GetRowCount());
4031
4032    do
4033    {
4034        Field *fields = result->Fetch();
4035        bar.step();
4036
4037        uint32 entry = fields[0].GetUInt32();
4038
4039        PageTextLocale& data = mPageTextLocaleMap[entry];
4040
4041        for(int i = 1; i < MAX_LOCALE; ++i)
4042        {
4043            std::string str = fields[i].GetCppString();
4044            if(str.empty())
4045                continue;
4046
4047            int idx = GetOrNewIndexForLocale(LocaleConstant(i));
4048            if(idx >= 0)
4049            {
4050                if(data.Text.size() <= idx)
4051                    data.Text.resize(idx+1);
4052
4053                data.Text[idx] = str;
4054            }
4055        }
4056
4057    } while (result->NextRow());
4058
4059    delete result;
4060
4061    sLog.outString();
4062    sLog.outString( ">> Loaded %u PageText locale strings", mPageTextLocaleMap.size() );
4063}
4064
4065void ObjectMgr::LoadInstanceTemplate()
4066{
4067    sInstanceTemplate.Load();
4068
4069    for(uint32 i = 0; i < sInstanceTemplate.MaxEntry; i++)
4070    {
4071        InstanceTemplate* temp = (InstanceTemplate*)GetInstanceTemplate(i);
4072        if(!temp) continue;
4073        const MapEntry* entry = sMapStore.LookupEntry(temp->map);
4074        if(!entry)
4075        {
4076            sLog.outErrorDb("ObjectMgr::LoadInstanceTemplate: bad mapid %d for template!", temp->map);
4077            continue;
4078        }
4079        else if(!entry->HasResetTime())
4080            continue;
4081
4082        if(temp->reset_delay == 0)
4083        {
4084            // use defaults from the DBC
4085            if(entry->SupportsHeroicMode())
4086            {
4087                temp->reset_delay = entry->resetTimeHeroic / DAY;
4088            }
4089            else if (entry->resetTimeRaid && entry->map_type == MAP_RAID)
4090            {
4091                temp->reset_delay = entry->resetTimeRaid / DAY;
4092            }
4093        }
4094
4095        // the reset_delay must be atleast one day
4096        temp->reset_delay = std::max((uint32)1, (uint32)(temp->reset_delay * sWorld.getRate(RATE_INSTANCE_RESET_TIME)));
4097    }
4098
4099    sLog.outString( ">> Loaded %u Instance Template definitions", sInstanceTemplate.RecordCount );
4100    sLog.outString();
4101}
4102
4103void ObjectMgr::AddGossipText(GossipText *pGText)
4104{
4105    ASSERT( pGText->Text_ID );
4106    ASSERT( mGossipText.find(pGText->Text_ID) == mGossipText.end() );
4107    mGossipText[pGText->Text_ID] = pGText;
4108}
4109
4110GossipText *ObjectMgr::GetGossipText(uint32 Text_ID)
4111{
4112    GossipTextMap::const_iterator itr;
4113    for (itr = mGossipText.begin(); itr != mGossipText.end(); itr++)
4114    {
4115        if(itr->second->Text_ID == Text_ID)
4116            return itr->second;
4117    }
4118    return NULL;
4119}
4120
4121void ObjectMgr::LoadGossipText()
4122{
4123    GossipText *pGText;
4124    QueryResult *result = WorldDatabase.Query( "SELECT * FROM npc_text" );
4125
4126    int count = 0;
4127    if( !result )
4128    {
4129        barGoLink bar( 1 );
4130        bar.step();
4131
4132        sLog.outString();
4133        sLog.outString( ">> Loaded %u npc texts", count );
4134        return;
4135    }
4136
4137    int cic;
4138
4139    barGoLink bar( result->GetRowCount() );
4140
4141    do
4142    {
4143        ++count;
4144        cic = 0;
4145
4146        Field *fields = result->Fetch();
4147
4148        bar.step();
4149
4150        pGText = new GossipText;
4151        pGText->Text_ID    = fields[cic++].GetUInt32();
4152
4153        for (int i=0; i< 8; i++)
4154        {
4155            pGText->Options[i].Text_0           = fields[cic++].GetCppString();
4156            pGText->Options[i].Text_1           = fields[cic++].GetCppString();
4157
4158            pGText->Options[i].Language         = fields[cic++].GetUInt32();
4159            pGText->Options[i].Probability      = fields[cic++].GetFloat();
4160
4161            pGText->Options[i].Emotes[0]._Delay  = fields[cic++].GetUInt32();
4162            pGText->Options[i].Emotes[0]._Emote  = fields[cic++].GetUInt32();
4163
4164            pGText->Options[i].Emotes[1]._Delay  = fields[cic++].GetUInt32();
4165            pGText->Options[i].Emotes[1]._Emote  = fields[cic++].GetUInt32();
4166
4167            pGText->Options[i].Emotes[2]._Delay  = fields[cic++].GetUInt32();
4168            pGText->Options[i].Emotes[2]._Emote  = fields[cic++].GetUInt32();
4169        }
4170
4171        if ( !pGText->Text_ID ) continue;
4172        AddGossipText( pGText );
4173
4174    } while( result->NextRow() );
4175
4176    sLog.outString();
4177    sLog.outString( ">> Loaded %u npc texts", count );
4178    delete result;
4179}
4180
4181void ObjectMgr::LoadNpcTextLocales()
4182{
4183    QueryResult *result = WorldDatabase.Query("SELECT entry,"
4184        "Text0_0_loc1,Text0_1_loc1,Text1_0_loc1,Text1_1_loc1,Text2_0_loc1,Text2_1_loc1,Text3_0_loc1,Text3_1_loc1,Text4_0_loc1,Text4_1_loc1,Text5_0_loc1,Text5_1_loc1,Text6_0_loc1,Text6_1_loc1,Text7_0_loc1,Text7_1_loc1,"
4185        "Text0_0_loc2,Text0_1_loc2,Text1_0_loc2,Text1_1_loc2,Text2_0_loc2,Text2_1_loc2,Text3_0_loc2,Text3_1_loc1,Text4_0_loc2,Text4_1_loc2,Text5_0_loc2,Text5_1_loc2,Text6_0_loc2,Text6_1_loc2,Text7_0_loc2,Text7_1_loc2,"
4186        "Text0_0_loc3,Text0_1_loc3,Text1_0_loc3,Text1_1_loc3,Text2_0_loc3,Text2_1_loc3,Text3_0_loc3,Text3_1_loc1,Text4_0_loc3,Text4_1_loc3,Text5_0_loc3,Text5_1_loc3,Text6_0_loc3,Text6_1_loc3,Text7_0_loc3,Text7_1_loc3,"
4187        "Text0_0_loc4,Text0_1_loc4,Text1_0_loc4,Text1_1_loc4,Text2_0_loc4,Text2_1_loc4,Text3_0_loc4,Text3_1_loc1,Text4_0_loc4,Text4_1_loc4,Text5_0_loc4,Text5_1_loc4,Text6_0_loc4,Text6_1_loc4,Text7_0_loc4,Text7_1_loc4,"
4188        "Text0_0_loc5,Text0_1_loc5,Text1_0_loc5,Text1_1_loc5,Text2_0_loc5,Text2_1_loc5,Text3_0_loc5,Text3_1_loc1,Text4_0_loc5,Text4_1_loc5,Text5_0_loc5,Text5_1_loc5,Text6_0_loc5,Text6_1_loc5,Text7_0_loc5,Text7_1_loc5,"
4189        "Text0_0_loc6,Text0_1_loc6,Text1_0_loc6,Text1_1_loc6,Text2_0_loc6,Text2_1_loc6,Text3_0_loc6,Text3_1_loc1,Text4_0_loc6,Text4_1_loc6,Text5_0_loc6,Text5_1_loc6,Text6_0_loc6,Text6_1_loc6,Text7_0_loc6,Text7_1_loc6,"
4190        "Text0_0_loc7,Text0_1_loc7,Text1_0_loc7,Text1_1_loc7,Text2_0_loc7,Text2_1_loc7,Text3_0_loc7,Text3_1_loc1,Text4_0_loc7,Text4_1_loc7,Text5_0_loc7,Text5_1_loc7,Text6_0_loc7,Text6_1_loc7,Text7_0_loc7,Text7_1_loc7, "
4191        "Text0_0_loc8,Text0_1_loc8,Text1_0_loc8,Text1_1_loc8,Text2_0_loc8,Text2_1_loc8,Text3_0_loc8,Text3_1_loc1,Text4_0_loc8,Text4_1_loc8,Text5_0_loc8,Text5_1_loc8,Text6_0_loc8,Text6_1_loc8,Text7_0_loc8,Text7_1_loc8 "
4192        " FROM locales_npc_text");
4193
4194    if(!result)
4195    {
4196        barGoLink bar(1);
4197
4198        bar.step();
4199
4200        sLog.outString("");
4201        sLog.outString(">> Loaded 0 Quest locale strings. DB table `locales_npc_text` is empty.");
4202        return;
4203    }
4204
4205    barGoLink bar(result->GetRowCount());
4206
4207    do
4208    {
4209        Field *fields = result->Fetch();
4210        bar.step();
4211
4212        uint32 entry = fields[0].GetUInt32();
4213
4214        NpcTextLocale& data = mNpcTextLocaleMap[entry];
4215
4216        for(int i=1; i<MAX_LOCALE; ++i)
4217        {
4218            for(int j=0; j<8; ++j)
4219            {
4220                std::string str0 = fields[1+8*2*(i-1)+2*j].GetCppString();
4221                if(!str0.empty())
4222                {
4223                    int idx = GetOrNewIndexForLocale(LocaleConstant(i));
4224                    if(idx >= 0)
4225                    {
4226                        if(data.Text_0[j].size() <= idx)
4227                            data.Text_0[j].resize(idx+1);
4228
4229                        data.Text_0[j][idx] = str0;
4230                    }
4231                }
4232                std::string str1 = fields[1+8*2*(i-1)+2*j+1].GetCppString();
4233                if(!str1.empty())
4234                {
4235                    int idx = GetOrNewIndexForLocale(LocaleConstant(i));
4236                    if(idx >= 0)
4237                    {
4238                        if(data.Text_1[j].size() <= idx)
4239                            data.Text_1[j].resize(idx+1);
4240
4241                        data.Text_1[j][idx] = str1;
4242                    }
4243                }
4244            }
4245        }
4246    } while (result->NextRow());
4247
4248    delete result;
4249
4250    sLog.outString();
4251    sLog.outString( ">> Loaded %u NpcText locale strings", mNpcTextLocaleMap.size() );
4252}
4253
4254//not very fast function but it is called only once a day, or on starting-up
4255void ObjectMgr::ReturnOrDeleteOldMails(bool serverUp)
4256{
4257    time_t basetime = time(NULL);
4258    sLog.outDebug("Returning mails current time: hour: %d, minute: %d, second: %d ", localtime(&basetime)->tm_hour, localtime(&basetime)->tm_min, localtime(&basetime)->tm_sec);
4259    //delete all old mails without item and without body immediately, if starting server
4260    if (!serverUp)
4261        CharacterDatabase.PExecute("DELETE FROM mail WHERE expire_time < '" I64FMTD "' AND has_items = '0' AND itemTextId = 0", (uint64)basetime);
4262    //                                                     0  1           2      3        4          5         6           7   8       9
4263    QueryResult* result = CharacterDatabase.PQuery("SELECT id,messageType,sender,receiver,itemTextId,has_items,expire_time,cod,checked,mailTemplateId FROM mail WHERE expire_time < '" I64FMTD "'", (uint64)basetime);
4264    if ( !result )
4265        return;                                             // any mails need to be returned or deleted
4266    Field *fields;
4267    //std::ostringstream delitems, delmails; //will be here for optimization
4268    //bool deletemail = false, deleteitem = false;
4269    //delitems << "DELETE FROM item_instance WHERE guid IN ( ";
4270    //delmails << "DELETE FROM mail WHERE id IN ( "
4271    do
4272    {
4273        fields = result->Fetch();
4274        Mail *m = new Mail;
4275        m->messageID = fields[0].GetUInt32();
4276        m->messageType = fields[1].GetUInt8();
4277        m->sender = fields[2].GetUInt32();
4278        m->receiver = fields[3].GetUInt32();
4279        m->itemTextId = fields[4].GetUInt32();
4280        bool has_items = fields[5].GetBool();
4281        m->expire_time = (time_t)fields[6].GetUInt64();
4282        m->deliver_time = 0;
4283        m->COD = fields[7].GetUInt32();
4284        m->checked = fields[8].GetUInt32();
4285        m->mailTemplateId = fields[9].GetInt16();
4286
4287        Player *pl = 0;
4288        if (serverUp)
4289            pl = GetPlayer((uint64)m->receiver);
4290        if (pl && pl->m_mailsLoaded)
4291        {                                                   //this code will run very improbably (the time is between 4 and 5 am, in game is online a player, who has old mail
4292            //his in mailbox and he has already listed his mails )
4293            delete m;
4294            continue;
4295        }
4296        //delete or return mail:
4297        if (has_items)
4298        {
4299            QueryResult *resultItems = CharacterDatabase.PQuery("SELECT item_guid,item_template FROM mail_items WHERE mail_id='%u'", m->messageID);
4300            if(resultItems)
4301            {
4302                do
4303                {
4304                    Field *fields2 = resultItems->Fetch();
4305
4306                    uint32 item_guid_low = fields2[0].GetUInt32();
4307                    uint32 item_template = fields2[1].GetUInt32();
4308
4309                    m->AddItem(item_guid_low, item_template);
4310                }
4311                while (resultItems->NextRow());
4312
4313                delete resultItems;
4314            }
4315            //if it is mail from AH, it shouldn't be returned, but deleted
4316            if (m->messageType != MAIL_NORMAL || (m->checked & (MAIL_CHECK_MASK_AUCTION | MAIL_CHECK_MASK_COD_PAYMENT | MAIL_CHECK_MASK_RETURNED)))
4317            {
4318                // mail open and then not returned
4319                for(std::vector<MailItemInfo>::iterator itr2 = m->items.begin(); itr2 != m->items.end(); ++itr2)
4320                    CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid = '%u'", itr2->item_guid);
4321            }
4322            else
4323            {
4324                //mail will be returned:
4325                CharacterDatabase.PExecute("UPDATE mail SET sender = '%u', receiver = '%u', expire_time = '" I64FMTD "', deliver_time = '" I64FMTD "',cod = '0', checked = '%u' WHERE id = '%u'", m->receiver, m->sender, (uint64)(basetime + 30*DAY), (uint64)basetime, MAIL_CHECK_MASK_RETURNED, m->messageID);
4326                delete m;
4327                continue;
4328            }
4329        }
4330
4331        if (m->itemTextId)
4332            CharacterDatabase.PExecute("DELETE FROM item_text WHERE id = '%u'", m->itemTextId);
4333
4334        //deletemail = true;
4335        //delmails << m->messageID << ", ";
4336        CharacterDatabase.PExecute("DELETE FROM mail WHERE id = '%u'", m->messageID);
4337        delete m;
4338    } while (result->NextRow());
4339    delete result;
4340}
4341
4342void ObjectMgr::LoadQuestAreaTriggers()
4343{
4344    mQuestAreaTriggerMap.clear();                           // need for reload case
4345
4346    QueryResult *result = WorldDatabase.Query( "SELECT id,quest FROM areatrigger_involvedrelation" );
4347
4348    uint32 count = 0;
4349
4350    if( !result )
4351    {
4352        barGoLink bar( 1 );
4353        bar.step();
4354
4355        sLog.outString();
4356        sLog.outString( ">> Loaded %u quest trigger points", count );
4357        return;
4358    }
4359
4360    barGoLink bar( result->GetRowCount() );
4361
4362    do
4363    {
4364        ++count;
4365        bar.step();
4366
4367        Field *fields = result->Fetch();
4368
4369        uint32 trigger_ID = fields[0].GetUInt32();
4370        uint32 quest_ID   = fields[1].GetUInt32();
4371
4372        AreaTriggerEntry const* atEntry = sAreaTriggerStore.LookupEntry(trigger_ID);
4373        if(!atEntry)
4374        {
4375            sLog.outErrorDb("Area trigger (ID:%u) does not exist in `AreaTrigger.dbc`.",trigger_ID);
4376            continue;
4377        }
4378
4379        Quest const* quest = GetQuestTemplate(quest_ID);
4380
4381        if(!quest)
4382        {
4383            sLog.outErrorDb("Table `areatrigger_involvedrelation` has record (id: %u) for not existing quest %u",trigger_ID,quest_ID);
4384            continue;
4385        }
4386
4387        if(!quest->HasFlag(QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT))
4388        {
4389            sLog.outErrorDb("Table `areatrigger_involvedrelation` has record (id: %u) for not quest %u, but quest not have flag QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT. Trigger or quest flags must be fixed, quest modified to require objective.",trigger_ID,quest_ID);
4390
4391            // this will prevent quest completing without objective
4392            const_cast<Quest*>(quest)->SetFlag(QUEST_TRINITY_FLAGS_EXPLORATION_OR_EVENT);
4393
4394            // continue; - quest modified to required obkective and trigger can be allowed.
4395        }
4396
4397        mQuestAreaTriggerMap[trigger_ID] = quest_ID;
4398
4399    } while( result->NextRow() );
4400
4401    delete result;
4402
4403    sLog.outString();
4404    sLog.outString( ">> Loaded %u quest trigger points", count );
4405}
4406
4407void ObjectMgr::LoadTavernAreaTriggers()
4408{
4409    mTavernAreaTriggerSet.clear();                          // need for reload case
4410
4411    QueryResult *result = WorldDatabase.Query("SELECT id FROM areatrigger_tavern");
4412
4413    uint32 count = 0;
4414
4415    if( !result )
4416    {
4417        barGoLink bar( 1 );
4418        bar.step();
4419
4420        sLog.outString();
4421        sLog.outString( ">> Loaded %u tavern triggers", count );
4422        return;
4423    }
4424
4425    barGoLink bar( result->GetRowCount() );
4426
4427    do
4428    {
4429        ++count;
4430        bar.step();
4431
4432        Field *fields = result->Fetch();
4433
4434        uint32 Trigger_ID      = fields[0].GetUInt32();
4435
4436        AreaTriggerEntry const* atEntry = sAreaTriggerStore.LookupEntry(Trigger_ID);
4437        if(!atEntry)
4438        {
4439            sLog.outErrorDb("Area trigger (ID:%u) does not exist in `AreaTrigger.dbc`.",Trigger_ID);
4440            continue;
4441        }
4442
4443        mTavernAreaTriggerSet.insert(Trigger_ID);
4444    } while( result->NextRow() );
4445
4446    delete result;
4447
4448    sLog.outString();
4449    sLog.outString( ">> Loaded %u tavern triggers", count );
4450}
4451
4452void ObjectMgr::LoadAreaTriggerScripts()
4453{
4454    mAreaTriggerScripts.clear();                            // need for reload case
4455    QueryResult *result = WorldDatabase.Query("SELECT entry, ScriptName FROM areatrigger_scripts");
4456
4457    uint32 count = 0;
4458
4459    if( !result )
4460    {
4461        barGoLink bar( 1 );
4462        bar.step();
4463
4464        sLog.outString();
4465        sLog.outString( ">> Loaded %u areatrigger scripts", count );
4466        return;
4467    }
4468
4469    barGoLink bar( result->GetRowCount() );
4470
4471    do
4472    {
4473        ++count;
4474        bar.step();
4475
4476        Field *fields = result->Fetch();
4477
4478        uint32 Trigger_ID      = fields[0].GetUInt32();
4479        std::string scriptName = fields[1].GetCppString();
4480
4481        AreaTriggerEntry const* atEntry = sAreaTriggerStore.LookupEntry(Trigger_ID);
4482        if(!atEntry)
4483        {
4484            sLog.outErrorDb("Area trigger (ID:%u) does not exist in `AreaTrigger.dbc`.",Trigger_ID);
4485            continue;
4486        }
4487        mAreaTriggerScripts[Trigger_ID] = scriptName;
4488    } while( result->NextRow() );
4489
4490    delete result;
4491
4492    sLog.outString();
4493    sLog.outString( ">> Loaded %u areatrigger scripts", count );
4494}
4495uint32 ObjectMgr::GetNearestTaxiNode( float x, float y, float z, uint32 mapid )
4496{
4497    bool found = false;
4498    float dist;
4499    uint32 id = 0;
4500
4501    for(uint32 i = 1; i < sTaxiNodesStore.GetNumRows(); ++i)
4502    {
4503        TaxiNodesEntry const* node = sTaxiNodesStore.LookupEntry(i);
4504        if(node && node->map_id == mapid)
4505        {
4506            float dist2 = (node->x - x)*(node->x - x)+(node->y - y)*(node->y - y)+(node->z - z)*(node->z - z);
4507            if(found)
4508            {
4509                if(dist2 < dist)
4510                {
4511                    dist = dist2;
4512                    id = i;
4513                }
4514            }
4515            else
4516            {
4517                found = true;
4518                dist = dist2;
4519                id = i;
4520            }
4521        }
4522    }
4523
4524    return id;
4525}
4526
4527void ObjectMgr::GetTaxiPath( uint32 source, uint32 destination, uint32 &path, uint32 &cost)
4528{
4529    TaxiPathSetBySource::iterator src_i = sTaxiPathSetBySource.find(source);
4530    if(src_i==sTaxiPathSetBySource.end())
4531    {
4532        path = 0;
4533        cost = 0;
4534        return;
4535    }
4536
4537    TaxiPathSetForSource& pathSet = src_i->second;
4538
4539    TaxiPathSetForSource::iterator dest_i = pathSet.find(destination);
4540    if(dest_i==pathSet.end())
4541    {
4542        path = 0;
4543        cost = 0;
4544        return;
4545    }
4546
4547    cost = dest_i->second.price;
4548    path = dest_i->second.ID;
4549}
4550
4551uint16 ObjectMgr::GetTaxiMount( uint32 id, uint32 team )
4552{
4553    uint16 mount_entry = 0;
4554    uint16 mount_id = 0;
4555
4556    TaxiNodesEntry const* node = sTaxiNodesStore.LookupEntry(id);
4557    if(node)
4558    {
4559        if (team == ALLIANCE)
4560        {
4561            mount_entry = node->alliance_mount_type;
4562            CreatureInfo const *ci = GetCreatureTemplate(mount_entry);
4563            if(ci)
4564                mount_id = ci->DisplayID_A;
4565        }
4566        if (team == HORDE)
4567        {
4568            mount_entry = node->horde_mount_type;
4569            CreatureInfo const *ci = GetCreatureTemplate(mount_entry);
4570            if(ci)
4571                mount_id = ci->DisplayID_H;
4572        }
4573    }
4574
4575    CreatureModelInfo const *minfo = GetCreatureModelInfo(mount_id);
4576    if(!minfo)
4577    {
4578        sLog.outErrorDb("Taxi mount (Entry: %u) for taxi node (Id: %u) for team %u has model %u not found in table `creature_model_info`, can't load. ",
4579            mount_entry,id,team,mount_id);
4580
4581        return false;
4582    }
4583    if(minfo->modelid_other_gender!=0)
4584        mount_id = urand(0,1) ? mount_id : minfo->modelid_other_gender;
4585
4586    return mount_id;
4587}
4588
4589void ObjectMgr::GetTaxiPathNodes( uint32 path, Path &pathnodes, std::vector<uint32>& mapIds)
4590{
4591    if(path >= sTaxiPathNodesByPath.size())
4592        return;
4593
4594    TaxiPathNodeList& nodeList = sTaxiPathNodesByPath[path];
4595
4596    pathnodes.Resize(nodeList.size());
4597    mapIds.resize(nodeList.size());
4598
4599    for(size_t i = 0; i < nodeList.size(); ++i)
4600    {
4601        pathnodes[ i ].x = nodeList[i].x;
4602        pathnodes[ i ].y = nodeList[i].y;
4603        pathnodes[ i ].z = nodeList[i].z;
4604
4605        mapIds[i] = nodeList[i].mapid;
4606    }
4607}
4608
4609void ObjectMgr::GetTransportPathNodes( uint32 path, TransportPath &pathnodes )
4610{
4611    if(path >= sTaxiPathNodesByPath.size())
4612        return;
4613
4614    TaxiPathNodeList& nodeList = sTaxiPathNodesByPath[path];
4615
4616    pathnodes.Resize(nodeList.size());
4617
4618    for(size_t i = 0; i < nodeList.size(); ++i)
4619    {
4620        pathnodes[ i ].mapid = nodeList[i].mapid;
4621        pathnodes[ i ].x = nodeList[i].x;
4622        pathnodes[ i ].y = nodeList[i].y;
4623        pathnodes[ i ].z = nodeList[i].z;
4624        pathnodes[ i ].actionFlag = nodeList[i].actionFlag;
4625        pathnodes[ i ].delay = nodeList[i].delay;
4626    }
4627}
4628
4629void ObjectMgr::LoadGraveyardZones()
4630{
4631    mGraveYardMap.clear();                                  // need for reload case
4632
4633    QueryResult *result = WorldDatabase.Query("SELECT id,ghost_zone,faction FROM game_graveyard_zone");
4634
4635    uint32 count = 0;
4636
4637    if( !result )
4638    {
4639        barGoLink bar( 1 );
4640        bar.step();
4641
4642        sLog.outString();
4643        sLog.outString( ">> Loaded %u graveyard-zone links", count );
4644        return;
4645    }
4646
4647    barGoLink bar( result->GetRowCount() );
4648
4649    do
4650    {
4651        ++count;
4652        bar.step();
4653
4654        Field *fields = result->Fetch();
4655
4656        uint32 safeLocId = fields[0].GetUInt32();
4657        uint32 zoneId = fields[1].GetUInt32();
4658        uint32 team   = fields[2].GetUInt32();
4659
4660        WorldSafeLocsEntry const* entry = sWorldSafeLocsStore.LookupEntry(safeLocId);
4661        if(!entry)
4662        {
4663            sLog.outErrorDb("Table `game_graveyard_zone` has record for not existing graveyard (WorldSafeLocs.dbc id) %u, skipped.",safeLocId);
4664            continue;
4665        }
4666
4667        AreaTableEntry const *areaEntry = GetAreaEntryByAreaID(zoneId);
4668        if(!areaEntry)
4669        {
4670            sLog.outErrorDb("Table `game_graveyard_zone` has record for not existing zone id (%u), skipped.",zoneId);
4671            continue;
4672        }
4673
4674        if(areaEntry->zone != 0)
4675        {
4676            sLog.outErrorDb("Table `game_graveyard_zone` has record subzone id (%u) instead of zone, skipped.",zoneId);
4677            continue;
4678        }
4679
4680        if(team!=0 && team!=HORDE && team!=ALLIANCE)
4681        {
4682            sLog.outErrorDb("Table `game_graveyard_zone` has record for non player faction (%u), skipped.",team);
4683            continue;
4684        }
4685
4686        if(entry->map_id != areaEntry->mapid && team != 0)
4687        {
4688            sLog.outErrorDb("Table `game_graveyard_zone` has record for ghost zone (%u) at map %u and graveyard (%u) at map %u for team %u, but in case maps are different, player faction setting is ignored. Use faction 0 instead.",zoneId,areaEntry->mapid, safeLocId, entry->map_id, team);
4689            team = 0;
4690        }
4691
4692        if(!AddGraveYardLink(safeLocId,zoneId,team,false))
4693            sLog.outErrorDb("Table `game_graveyard_zone` has a duplicate record for Garveyard (ID: %u) and Zone (ID: %u), skipped.",safeLocId,zoneId);
4694    } while( result->NextRow() );
4695
4696    delete result;
4697
4698    sLog.outString();
4699    sLog.outString( ">> Loaded %u graveyard-zone links", count );
4700}
4701
4702WorldSafeLocsEntry const *ObjectMgr::GetClosestGraveYard(float x, float y, float z, uint32 MapId, uint32 team)
4703{
4704    // search for zone associated closest graveyard
4705    uint32 zoneId = MapManager::Instance().GetZoneId(MapId,x,y);
4706
4707    // Simulate std. algorithm:
4708    //   found some graveyard associated to (ghost_zone,ghost_map)
4709    //
4710    //   if mapId == graveyard.mapId (ghost in plain zone or city or battleground) and search graveyard at same map
4711    //     then check faction
4712    //   if mapId != graveyard.mapId (ghost in instance) and search any graveyard associated
4713    //     then skip check faction
4714    GraveYardMap::const_iterator graveLow  = mGraveYardMap.lower_bound(zoneId);
4715    GraveYardMap::const_iterator graveUp   = mGraveYardMap.upper_bound(zoneId);
4716    if(graveLow==graveUp)
4717    {
4718        sLog.outErrorDb("Table `game_graveyard_zone` incomplete: Zone %u Team %u does not have a linked graveyard.",zoneId,team);
4719        return NULL;
4720    }
4721
4722    bool foundNear = false;
4723    float distNear;
4724    WorldSafeLocsEntry const* entryNear = NULL;
4725    WorldSafeLocsEntry const* entryFar = NULL;
4726
4727    for(GraveYardMap::const_iterator itr = graveLow; itr != graveUp; ++itr)
4728    {
4729        GraveYardData const& data = itr->second;
4730
4731        WorldSafeLocsEntry const* entry = sWorldSafeLocsStore.LookupEntry(data.safeLocId);
4732        if(!entry)
4733        {
4734            sLog.outErrorDb("Table `game_graveyard_zone` has record for not existing graveyard (WorldSafeLocs.dbc id) %u, skipped.",data.safeLocId);
4735            continue;
4736        }
4737
4738        // remember first graveyard at another map and ignore other
4739        if(MapId != entry->map_id)
4740        {
4741            if(!entryFar)
4742                entryFar = entry;
4743            continue;
4744        }
4745
4746        // skip enemy faction graveyard at same map (normal area, city, or battleground)
4747        // team == 0 case can be at call from .neargrave
4748        if(data.team != 0 && team != 0 && data.team != team)
4749            continue;
4750
4751        // find now nearest graveyard at same map
4752        float dist2 = (entry->x - x)*(entry->x - x)+(entry->y - y)*(entry->y - y)+(entry->z - z)*(entry->z - z);
4753        if(foundNear)
4754        {
4755            if(dist2 < distNear)
4756            {
4757                distNear = dist2;
4758                entryNear = entry;
4759            }
4760        }
4761        else
4762        {
4763            foundNear = true;
4764            distNear = dist2;
4765            entryNear = entry;
4766        }
4767    }
4768
4769    if(entryNear)
4770        return entryNear;
4771
4772    return entryFar;
4773}
4774
4775GraveYardData const* ObjectMgr::FindGraveYardData(uint32 id, uint32 zoneId)
4776{
4777    GraveYardMap::const_iterator graveLow  = mGraveYardMap.lower_bound(zoneId);
4778    GraveYardMap::const_iterator graveUp   = mGraveYardMap.upper_bound(zoneId);
4779
4780    for(GraveYardMap::const_iterator itr = graveLow; itr != graveUp; ++itr)
4781    {
4782        if(itr->second.safeLocId==id)
4783            return &itr->second;
4784    }
4785
4786    return NULL;
4787}
4788
4789bool ObjectMgr::AddGraveYardLink(uint32 id, uint32 zoneId, uint32 team, bool inDB)
4790{
4791    if(FindGraveYardData(id,zoneId))
4792        return false;
4793
4794    // add link to loaded data
4795    GraveYardData data;
4796    data.safeLocId = id;
4797    data.team = team;
4798
4799    mGraveYardMap.insert(GraveYardMap::value_type(zoneId,data));
4800
4801    // add link to DB
4802    if(inDB)
4803    {
4804        WorldDatabase.PExecuteLog("INSERT INTO game_graveyard_zone ( id,ghost_zone,faction) "
4805            "VALUES ('%u', '%u','%u')",id,zoneId,team);
4806    }
4807
4808    return true;
4809}
4810
4811void ObjectMgr::RemoveGraveYardLink(uint32 id, uint32 zoneId, uint32 team, bool inDB)
4812{
4813    GraveYardMap::iterator graveLow  = mGraveYardMap.lower_bound(zoneId);
4814    GraveYardMap::iterator graveUp   = mGraveYardMap.upper_bound(zoneId);
4815    if(graveLow==graveUp)
4816    {
4817        //sLog.outErrorDb("Table `game_graveyard_zone` incomplete: Zone %u Team %u does not have a linked graveyard.",zoneId,team);
4818        return;
4819    }
4820
4821    bool found = false;
4822
4823    GraveYardMap::iterator itr;
4824
4825    for(itr = graveLow; itr != graveUp; ++itr)
4826    {
4827        GraveYardData & data = itr->second;
4828
4829        // skip not matching safezone id
4830        if(data.safeLocId != id)
4831            continue;
4832
4833        // skip enemy faction graveyard at same map (normal area, city, or battleground)
4834        // team == 0 case can be at call from .neargrave
4835        if(data.team != 0 && team != 0 && data.team != team)
4836            continue;
4837
4838        found = true;
4839        break;
4840    }
4841
4842    // no match, return
4843    if(!found)
4844        return;
4845
4846    // remove from links
4847    mGraveYardMap.erase(itr);
4848
4849    // remove link from DB
4850    if(inDB)
4851    {
4852        WorldDatabase.PExecute("DELETE FROM game_graveyard_zone WHERE id = '%u' AND ghost_zone = '%u' AND faction = '%u'",id,zoneId,team);
4853    }
4854
4855    return;
4856}
4857
4858
4859void ObjectMgr::LoadAreaTriggerTeleports()
4860{
4861    mAreaTriggers.clear();                                  // need for reload case
4862
4863    uint32 count = 0;
4864
4865    //                                                0   1               2              3               4           5            6                    7                     8           9                  10                 11                 12
4866    QueryResult *result = WorldDatabase.Query("SELECT id, required_level, required_item, required_item2, heroic_key, heroic_key2, required_quest_done, required_failed_text, target_map, target_position_x, target_position_y, target_position_z, target_orientation FROM areatrigger_teleport");
4867    if( !result )
4868    {
4869
4870        barGoLink bar( 1 );
4871
4872        bar.step();
4873
4874        sLog.outString();
4875        sLog.outString( ">> Loaded %u area trigger teleport definitions", count );
4876        return;
4877    }
4878
4879    barGoLink bar( result->GetRowCount() );
4880
4881    do
4882    {
4883        Field *fields = result->Fetch();
4884
4885        bar.step();
4886
4887        ++count;
4888
4889        uint32 Trigger_ID = fields[0].GetUInt32();
4890
4891        AreaTrigger at;
4892
4893        at.requiredLevel      = fields[1].GetUInt8();
4894        at.requiredItem       = fields[2].GetUInt32();
4895        at.requiredItem2      = fields[3].GetUInt32();
4896        at.heroicKey          = fields[4].GetUInt32();
4897        at.heroicKey2         = fields[5].GetUInt32();
4898        at.requiredQuest      = fields[6].GetUInt32();
4899        at.requiredFailedText = fields[7].GetCppString();
4900        at.target_mapId       = fields[8].GetUInt32();
4901        at.target_X           = fields[9].GetFloat();
4902        at.target_Y           = fields[10].GetFloat();
4903        at.target_Z           = fields[11].GetFloat();
4904        at.target_Orientation = fields[12].GetFloat();
4905
4906        AreaTriggerEntry const* atEntry = sAreaTriggerStore.LookupEntry(Trigger_ID);
4907        if(!atEntry)
4908        {
4909            sLog.outErrorDb("Area trigger (ID:%u) does not exist in `AreaTrigger.dbc`.",Trigger_ID);
4910            continue;
4911        }
4912
4913        if(at.requiredItem)
4914        {
4915            ItemPrototype const *pProto = GetItemPrototype(at.requiredItem);
4916            if(!pProto)
4917            {
4918                sLog.outError("Key item %u does not exist for trigger %u, removing key requirement.", at.requiredItem, Trigger_ID);
4919                at.requiredItem = 0;
4920            }
4921        }
4922        if(at.requiredItem2)
4923        {
4924            ItemPrototype const *pProto = GetItemPrototype(at.requiredItem2);
4925            if(!pProto)
4926            {
4927                sLog.outError("Second item %u not exist for trigger %u, remove key requirement.", at.requiredItem2, Trigger_ID);
4928                at.requiredItem2 = 0;
4929            }
4930        }
4931
4932        if(at.heroicKey)
4933        {
4934            ItemPrototype const *pProto = GetItemPrototype(at.heroicKey);
4935            if(!pProto)
4936            {
4937                sLog.outError("Heroic key item %u not exist for trigger %u, remove key requirement.", at.heroicKey, Trigger_ID);
4938                at.heroicKey = 0;
4939            }
4940        }
4941
4942        if(at.heroicKey2)
4943        {
4944            ItemPrototype const *pProto = GetItemPrototype(at.heroicKey2);
4945            if(!pProto)
4946            {
4947                sLog.outError("Heroic second key item %u not exist for trigger %u, remove key requirement.", at.heroicKey2, Trigger_ID);
4948                at.heroicKey2 = 0;
4949            }
4950        }
4951
4952        if(at.requiredQuest)
4953        {
4954            if(!mQuestTemplates[at.requiredQuest])
4955            {
4956                sLog.outErrorDb("Required Quest %u not exist for trigger %u, remove quest done requirement.",at.requiredQuest,Trigger_ID);
4957                at.requiredQuest = 0;
4958            }
4959        }
4960
4961        MapEntry const* mapEntry = sMapStore.LookupEntry(at.target_mapId);
4962        if(!mapEntry)
4963        {
4964            sLog.outErrorDb("Area trigger (ID:%u) target map (ID: %u) does not exist in `Map.dbc`.",Trigger_ID,at.target_mapId);
4965            continue;
4966        }
4967
4968        if(at.target_X==0 && at.target_Y==0 && at.target_Z==0)
4969        {
4970            sLog.outErrorDb("Area trigger (ID:%u) target coordinates not provided.",Trigger_ID);
4971            continue;
4972        }
4973
4974        mAreaTriggers[Trigger_ID] = at;
4975
4976    } while( result->NextRow() );
4977
4978    delete result;
4979
4980    sLog.outString();
4981    sLog.outString( ">> Loaded %u area trigger teleport definitions", count );
4982}
4983
4984AreaTrigger const* ObjectMgr::GetGoBackTrigger(uint32 Map) const
4985{
4986    const MapEntry *mapEntry = sMapStore.LookupEntry(Map);
4987    if(!mapEntry) return NULL;
4988    for (AreaTriggerMap::const_iterator itr = mAreaTriggers.begin(); itr != mAreaTriggers.end(); itr++)
4989    {
4990        if(itr->second.target_mapId == mapEntry->parent_map)
4991        {
4992            AreaTriggerEntry const* atEntry = sAreaTriggerStore.LookupEntry(itr->first);
4993            if(atEntry && atEntry->mapid == Map)
4994                return &itr->second;
4995        }
4996    }
4997    return NULL;
4998}
4999
5000void ObjectMgr::SetHighestGuids()
5001{
5002    QueryResult *result = CharacterDatabase.Query( "SELECT MAX(guid) FROM characters" );
5003    if( result )
5004    {
5005        m_hiCharGuid = (*result)[0].GetUInt32()+1;
5006
5007        delete result;
5008    }
5009
5010    result = WorldDatabase.Query( "SELECT MAX(guid) FROM creature" );
5011    if( result )
5012    {
5013        m_hiCreatureGuid = (*result)[0].GetUInt32()+1;
5014
5015        delete result;
5016    }
5017
5018    result = CharacterDatabase.Query( "SELECT MAX(id) FROM character_pet" );
5019    if( result )
5020    {
5021        m_hiPetGuid = (*result)[0].GetUInt32()+1;
5022
5023        delete result;
5024    }
5025
5026    result = CharacterDatabase.Query( "SELECT MAX(guid) FROM item_instance" );
5027    if( result )
5028    {
5029        m_hiItemGuid = (*result)[0].GetUInt32()+1;
5030
5031        delete result;
5032    }
5033
5034    // Cleanup other tables from not existed guids (>=m_hiItemGuid)
5035    CharacterDatabase.PExecute("DELETE FROM character_inventory WHERE item >= '%u'", m_hiItemGuid);
5036    CharacterDatabase.PExecute("DELETE FROM mail_items WHERE item_guid >= '%u'", m_hiItemGuid);
5037    CharacterDatabase.PExecute("DELETE FROM auctionhouse WHERE itemguid >= '%u'", m_hiItemGuid);
5038    CharacterDatabase.PExecute("DELETE FROM guild_bank_item WHERE item_guid >= '%u'", m_hiItemGuid);
5039
5040    result = WorldDatabase.Query("SELECT MAX(guid) FROM gameobject" );
5041    if( result )
5042    {
5043        m_hiGoGuid = (*result)[0].GetUInt32()+1;
5044
5045        delete result;
5046    }
5047
5048    result = CharacterDatabase.Query("SELECT MAX(id) FROM auctionhouse" );
5049    if( result )
5050    {
5051        m_auctionid = (*result)[0].GetUInt32()+1;
5052
5053        delete result;
5054    }
5055    else
5056    {
5057        m_auctionid = 0;
5058    }
5059    result = CharacterDatabase.Query( "SELECT MAX(id) FROM mail" );
5060    if( result )
5061    {
5062        m_mailid = (*result)[0].GetUInt32()+1;
5063
5064        delete result;
5065    }
5066    else
5067    {
5068        m_mailid = 0;
5069    }
5070    result = CharacterDatabase.Query( "SELECT MAX(id) FROM item_text" );
5071    if( result )
5072    {
5073        m_ItemTextId = (*result)[0].GetUInt32();
5074
5075        delete result;
5076    }
5077    else
5078        m_ItemTextId = 0;
5079
5080    result = CharacterDatabase.Query( "SELECT MAX(guid) FROM corpse" );
5081    if( result )
5082    {
5083        m_hiCorpseGuid = (*result)[0].GetUInt32()+1;
5084
5085        delete result;
5086    }
5087}
5088
5089uint32 ObjectMgr::GenerateAuctionID()
5090{
5091    ++m_auctionid;
5092    if(m_auctionid>=0xFFFFFFFF)
5093    {
5094        sLog.outError("Auctions ids overflow!! Can't continue, shuting down server. ");
5095        sWorld.m_stopEvent = true;
5096    }
5097    return m_auctionid;
5098}
5099
5100uint32 ObjectMgr::GenerateMailID()
5101{
5102    ++m_mailid;
5103    if(m_mailid>=0xFFFFFFFF)
5104    {
5105        sLog.outError("Mail ids overflow!! Can't continue, shuting down server. ");
5106        sWorld.m_stopEvent = true;
5107    }
5108    return m_mailid;
5109}
5110
5111uint32 ObjectMgr::GenerateItemTextID()
5112{
5113    ++m_ItemTextId;
5114    if(m_ItemTextId>=0xFFFFFFFF)
5115    {
5116        sLog.outError("Item text ids overflow!! Can't continue, shuting down server. ");
5117        sWorld.m_stopEvent = true;
5118    }
5119    return m_ItemTextId;
5120}
5121
5122uint32 ObjectMgr::CreateItemText(std::string text)
5123{
5124    uint32 newItemTextId = GenerateItemTextID();
5125    //insert new itempage to container
5126    mItemTexts[ newItemTextId ] = text;
5127    //save new itempage
5128    CharacterDatabase.escape_string(text);
5129    //any Delete query needed, itemTextId is maximum of all ids
5130    std::ostringstream query;
5131    query << "INSERT INTO item_text (id,text) VALUES ( '" << newItemTextId << "', '" << text << "')";
5132    CharacterDatabase.Execute(query.str().c_str());         //needs to be run this way, because mail body may be more than 1024 characters
5133    return newItemTextId;
5134}
5135
5136uint32 ObjectMgr::GenerateLowGuid(HighGuid guidhigh)
5137{
5138    switch(guidhigh)
5139    {
5140        case HIGHGUID_ITEM:
5141            ++m_hiItemGuid;
5142            if(m_hiItemGuid>=0xFFFFFFFF)
5143            {
5144                sLog.outError("Item guid overflow!! Can't continue, shuting down server. ");
5145                sWorld.m_stopEvent = true;
5146            }
5147            return m_hiItemGuid;
5148        case HIGHGUID_UNIT:
5149            ++m_hiCreatureGuid;
5150            if(m_hiCreatureGuid>=0x00FFFFFF)
5151            {
5152                sLog.outError("Creature guid overflow!! Can't continue, shuting down server. ");
5153                sWorld.m_stopEvent = true;
5154            }
5155            return m_hiCreatureGuid;
5156        case HIGHGUID_PET:
5157            ++m_hiPetGuid;
5158            if(m_hiPetGuid>=0x00FFFFFF)
5159            {
5160                sLog.outError("Pet guid overflow!! Can't continue, shuting down server. ");
5161                sWorld.m_stopEvent = true;
5162            }
5163            return m_hiPetGuid;
5164        case HIGHGUID_PLAYER:
5165            ++m_hiCharGuid;
5166            if(m_hiCharGuid>=0xFFFFFFFF)
5167            {
5168                sLog.outError("Players guid overflow!! Can't continue, shuting down server. ");
5169                sWorld.m_stopEvent = true;
5170            }
5171            return m_hiCharGuid;
5172        case HIGHGUID_GAMEOBJECT:
5173            ++m_hiGoGuid;
5174            if(m_hiGoGuid>=0x00FFFFFF)
5175            {
5176                sLog.outError("Gameobject guid overflow!! Can't continue, shuting down server. ");
5177                sWorld.m_stopEvent = true;
5178            }
5179            return m_hiGoGuid;
5180        case HIGHGUID_CORPSE:
5181            ++m_hiCorpseGuid;
5182            if(m_hiCorpseGuid>=0xFFFFFFFF)
5183            {
5184                sLog.outError("Corpse guid overflow!! Can't continue, shuting down server. ");
5185                sWorld.m_stopEvent = true;
5186            }
5187            return m_hiCorpseGuid;
5188        case HIGHGUID_DYNAMICOBJECT:
5189            ++m_hiDoGuid;
5190            if(m_hiDoGuid>=0xFFFFFFFF)
5191            {
5192                sLog.outError("DynamicObject guid overflow!! Can't continue, shuting down server. ");
5193                sWorld.m_stopEvent = true;
5194            }
5195            return m_hiDoGuid;
5196        default:
5197            ASSERT(0);
5198    }
5199
5200    ASSERT(0);
5201    return 0;
5202}
5203
5204void ObjectMgr::LoadGameObjectLocales()
5205{
5206    QueryResult *result = WorldDatabase.Query("SELECT entry,"
5207        "name_loc1,name_loc2,name_loc3,name_loc4,name_loc5,name_loc6,name_loc7,name_loc8,"
5208        "castbarcaption_loc1,castbarcaption_loc2,castbarcaption_loc3,castbarcaption_loc4,"
5209        "castbarcaption_loc5,castbarcaption_loc6,castbarcaption_loc7,castbarcaption_loc8 FROM locales_gameobject");
5210
5211    if(!result)
5212    {
5213        barGoLink bar(1);
5214
5215        bar.step();
5216
5217        sLog.outString("");
5218        sLog.outString(">> Loaded 0 gameobject locale strings. DB table `locales_gameobject` is empty.");
5219        return;
5220    }
5221
5222    barGoLink bar(result->GetRowCount());
5223
5224    do
5225    {
5226        Field *fields = result->Fetch();
5227        bar.step();
5228
5229        uint32 entry = fields[0].GetUInt32();
5230
5231        GameObjectLocale& data = mGameObjectLocaleMap[entry];
5232
5233        for(int i = 1; i < MAX_LOCALE; ++i)
5234        {
5235            std::string str = fields[i].GetCppString();
5236            if(!str.empty())
5237            {
5238                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
5239                if(idx >= 0)
5240                {
5241                    if(data.Name.size() <= idx)
5242                        data.Name.resize(idx+1);
5243
5244                    data.Name[idx] = str;
5245                }
5246            }
5247        }
5248
5249        for(int i = MAX_LOCALE; i < MAX_LOCALE*2-1; ++i)
5250        {
5251            std::string str = fields[i].GetCppString();
5252            if(!str.empty())
5253            {
5254                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
5255                if(idx >= 0)
5256                {
5257                    if(data.CastBarCaption.size() <= idx)
5258                        data.CastBarCaption.resize(idx+1);
5259
5260                    data.CastBarCaption[idx] = str;
5261                }
5262            }
5263        }
5264
5265    } while (result->NextRow());
5266
5267    delete result;
5268
5269    sLog.outString();
5270    sLog.outString( ">> Loaded %u gameobject locale strings", mGameObjectLocaleMap.size() );
5271}
5272
5273void ObjectMgr::LoadGameobjectInfo()
5274{
5275    sGOStorage.Load();
5276
5277    // some checks
5278    for(uint32 id = 1; id < sGOStorage.MaxEntry; id++)
5279    {
5280        GameObjectInfo const* goInfo = sGOStorage.LookupEntry<GameObjectInfo>(id);
5281        if(!goInfo)
5282            continue;
5283
5284        switch(goInfo->type)
5285        {
5286            case GAMEOBJECT_TYPE_DOOR:                      //0
5287            {
5288                if(goInfo->door.lockId)
5289                {
5290                    if(!sLockStore.LookupEntry(goInfo->door.lockId))
5291                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data1=%u but lock (Id: %u) not found.",
5292                            id,goInfo->type,goInfo->door.lockId,goInfo->door.lockId);
5293                }
5294                break;
5295            }
5296            case GAMEOBJECT_TYPE_BUTTON:                    //1
5297            {
5298                if(goInfo->button.lockId)
5299                {
5300                    if(!sLockStore.LookupEntry(goInfo->button.lockId))
5301                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data1=%u but lock (Id: %u) not found.",
5302                            id,goInfo->type,goInfo->button.lockId,goInfo->button.lockId);
5303                }
5304                break;
5305            }
5306            case GAMEOBJECT_TYPE_CHEST:                     //3
5307            {
5308                if(goInfo->chest.lockId)
5309                {
5310                    if(!sLockStore.LookupEntry(goInfo->chest.lockId))
5311                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data0=%u but lock (Id: %u) not found.",
5312                            id,goInfo->type,goInfo->chest.lockId,goInfo->chest.lockId);
5313                }
5314                if(goInfo->chest.linkedTrapId)              // linked trap
5315                {
5316                    if(GameObjectInfo const* trapInfo = sGOStorage.LookupEntry<GameObjectInfo>(goInfo->chest.linkedTrapId))
5317                    {
5318                        if(trapInfo->type!=GAMEOBJECT_TYPE_TRAP)
5319                            sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data7=%u but GO (Entry %u) have not GAMEOBJECT_TYPE_TRAP (%u) type.",
5320                                id,goInfo->type,goInfo->chest.linkedTrapId,goInfo->chest.linkedTrapId,GAMEOBJECT_TYPE_TRAP);
5321                    }
5322                    /* disable check for while
5323                    else
5324                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data2=%u but trap GO (Entry %u) not exist in `gameobject_template`.",
5325                            id,goInfo->type,goInfo->chest.linkedTrapId,goInfo->chest.linkedTrapId);
5326                    */
5327                }
5328                break;
5329            }
5330            case GAMEOBJECT_TYPE_TRAP:                      //6
5331            {
5332                /* disable check for while
5333                if(goInfo->trap.spellId)                    // spell
5334                {
5335                    if(!sSpellStore.LookupEntry(goInfo->trap.spellId))
5336                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data3=%u but Spell (Entry %u) not exist.",
5337                            id,goInfo->type,goInfo->trap.spellId,goInfo->trap.spellId);
5338                }
5339                */
5340                break;
5341            }
5342            case GAMEOBJECT_TYPE_CHAIR:                     //7
5343                if(goInfo->chair.height > 2)
5344                {
5345                    sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data1=%u but correct chair height in range 0..2.",
5346                        id,goInfo->type,goInfo->chair.height);
5347
5348                    // prevent client and server unexpected work
5349                    const_cast<GameObjectInfo*>(goInfo)->chair.height = 0;
5350                }
5351                break;
5352            case GAMEOBJECT_TYPE_SPELL_FOCUS:               //8
5353            {
5354                if(goInfo->spellFocus.focusId)
5355                {
5356                    if(!sSpellFocusObjectStore.LookupEntry(goInfo->spellFocus.focusId))
5357                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data0=%u but SpellFocus (Id: %u) not exist.",
5358                            id,goInfo->type,goInfo->spellFocus.focusId,goInfo->spellFocus.focusId);
5359                }
5360
5361                if(goInfo->spellFocus.linkedTrapId)         // linked trap
5362                {
5363                    if(GameObjectInfo const* trapInfo = sGOStorage.LookupEntry<GameObjectInfo>(goInfo->spellFocus.linkedTrapId))
5364                    {
5365                        if(trapInfo->type!=GAMEOBJECT_TYPE_TRAP)
5366                            sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data2=%u but GO (Entry %u) have not GAMEOBJECT_TYPE_TRAP (%u) type.",
5367                                id,goInfo->type,goInfo->spellFocus.linkedTrapId,goInfo->spellFocus.linkedTrapId,GAMEOBJECT_TYPE_TRAP);
5368                    }
5369                    /* disable check for while
5370                    else
5371                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data2=%u but trap GO (Entry %u) not exist in `gameobject_template`.",
5372                            id,goInfo->type,goInfo->spellFocus.linkedTrapId,goInfo->spellFocus.linkedTrapId);
5373                    */
5374                }
5375                break;
5376            }
5377            case GAMEOBJECT_TYPE_GOOBER:                    //10
5378            {
5379                if(goInfo->goober.pageId)                   // pageId
5380                {
5381                    if(!sPageTextStore.LookupEntry<PageText>(goInfo->goober.pageId))
5382                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data7=%u but PageText (Entry %u) not exist.",
5383                            id,goInfo->type,goInfo->goober.pageId,goInfo->goober.pageId);
5384                }
5385                /* disable check for while
5386                if(goInfo->goober.spellId)                  // spell
5387                {
5388                    if(!sSpellStore.LookupEntry(goInfo->goober.spellId))
5389                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data2=%u but Spell (Entry %u) not exist.",
5390                            id,goInfo->type,goInfo->goober.spellId,goInfo->goober.spellId);
5391                }
5392                */
5393                if(goInfo->goober.linkedTrapId)             // linked trap
5394                {
5395                    if(GameObjectInfo const* trapInfo = sGOStorage.LookupEntry<GameObjectInfo>(goInfo->goober.linkedTrapId))
5396                    {
5397                        if(trapInfo->type!=GAMEOBJECT_TYPE_TRAP)
5398                            sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data12=%u but GO (Entry %u) have not GAMEOBJECT_TYPE_TRAP (%u) type.",
5399                                id,goInfo->type,goInfo->goober.linkedTrapId,goInfo->goober.linkedTrapId,GAMEOBJECT_TYPE_TRAP);
5400                    }
5401                    /* disable check for while
5402                    else
5403                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data12=%u but trap GO (Entry %u) not exist in `gameobject_template`.",
5404                            id,goInfo->type,goInfo->goober.linkedTrapId,goInfo->goober.linkedTrapId);
5405                    */
5406                }
5407                break;
5408            }
5409            case GAMEOBJECT_TYPE_MO_TRANSPORT:              //15
5410            {
5411                if(goInfo->moTransport.taxiPathId)
5412                {
5413                    if(goInfo->moTransport.taxiPathId >= sTaxiPathNodesByPath.size() || sTaxiPathNodesByPath[goInfo->moTransport.taxiPathId].empty())
5414                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data0=%u but TaxiPath (Id: %u) not exist.",
5415                            id,goInfo->type,goInfo->moTransport.taxiPathId,goInfo->moTransport.taxiPathId);
5416                }
5417                break;
5418            }
5419            case GAMEOBJECT_TYPE_SUMMONING_RITUAL:          //18
5420            {
5421                /* disabled
5422                if(goInfo->summoningRitual.spellId)
5423                {
5424                    if(!sSpellStore.LookupEntry(goInfo->summoningRitual.spellId))
5425                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data1=%u but Spell (Entry %u) not exist.",
5426                            id,goInfo->type,goInfo->summoningRitual.spellId,goInfo->summoningRitual.spellId);
5427                }
5428                */
5429                break;
5430            }
5431            case GAMEOBJECT_TYPE_SPELLCASTER:               //22
5432            {
5433                if(goInfo->spellcaster.spellId)             // spell
5434                {
5435                    if(!sSpellStore.LookupEntry(goInfo->spellcaster.spellId))
5436                        sLog.outErrorDb("Gameobject (Entry: %u GoType: %u) have data3=%u but Spell (Entry %u) not exist.",
5437                            id,goInfo->type,goInfo->spellcaster.spellId,goInfo->spellcaster.spellId);
5438                }
5439                break;
5440            }
5441        }
5442    }
5443
5444    sLog.outString( ">> Loaded %u game object templates", sGOStorage.RecordCount );
5445    sLog.outString();
5446}
5447
5448void ObjectMgr::LoadExplorationBaseXP()
5449{
5450    uint32 count = 0;
5451    QueryResult *result = WorldDatabase.Query("SELECT level,basexp FROM exploration_basexp");
5452
5453    if( !result )
5454    {
5455        barGoLink bar( 1 );
5456
5457        bar.step();
5458
5459        sLog.outString();
5460        sLog.outString( ">> Loaded %u BaseXP definitions", count );
5461        return;
5462    }
5463
5464    barGoLink bar( result->GetRowCount() );
5465
5466    do
5467    {
5468        bar.step();
5469
5470        Field *fields = result->Fetch();
5471        uint32 level  = fields[0].GetUInt32();
5472        uint32 basexp = fields[1].GetUInt32();
5473        mBaseXPTable[level] = basexp;
5474        ++count;
5475    }
5476    while (result->NextRow());
5477
5478    delete result;
5479
5480    sLog.outString();
5481    sLog.outString( ">> Loaded %u BaseXP definitions", count );
5482}
5483
5484uint32 ObjectMgr::GetBaseXP(uint32 level)
5485{
5486    return mBaseXPTable[level] ? mBaseXPTable[level] : 0;
5487}
5488
5489void ObjectMgr::LoadPetNames()
5490{
5491    uint32 count = 0;
5492    QueryResult *result = WorldDatabase.Query("SELECT word,entry,half FROM pet_name_generation");
5493
5494    if( !result )
5495    {
5496        barGoLink bar( 1 );
5497
5498        bar.step();
5499
5500        sLog.outString();
5501        sLog.outString( ">> Loaded %u pet name parts", count );
5502        return;
5503    }
5504
5505    barGoLink bar( result->GetRowCount() );
5506
5507    do
5508    {
5509        bar.step();
5510
5511        Field *fields = result->Fetch();
5512        std::string word = fields[0].GetString();
5513        uint32 entry     = fields[1].GetUInt32();
5514        bool   half      = fields[2].GetBool();
5515        if(half)
5516            PetHalfName1[entry].push_back(word);
5517        else
5518            PetHalfName0[entry].push_back(word);
5519        ++count;
5520    }
5521    while (result->NextRow());
5522    delete result;
5523
5524    sLog.outString();
5525    sLog.outString( ">> Loaded %u pet name parts", count );
5526}
5527
5528void ObjectMgr::LoadPetNumber()
5529{
5530    QueryResult* result = CharacterDatabase.Query("SELECT MAX(id) FROM character_pet");
5531    if(result)
5532    {
5533        Field *fields = result->Fetch();
5534        m_hiPetNumber = fields[0].GetUInt32()+1;
5535        delete result;
5536    }
5537
5538    barGoLink bar( 1 );
5539    bar.step();
5540
5541    sLog.outString();
5542    sLog.outString( ">> Loaded the max pet number: %d", m_hiPetNumber-1);
5543}
5544
5545std::string ObjectMgr::GeneratePetName(uint32 entry)
5546{
5547    std::vector<std::string> & list0 = PetHalfName0[entry];
5548    std::vector<std::string> & list1 = PetHalfName1[entry];
5549
5550    if(list0.empty() || list1.empty())
5551    {
5552        CreatureInfo const *cinfo = GetCreatureTemplate(entry);
5553        char* petname = GetPetName(cinfo->family, sWorld.GetDefaultDbcLocale());
5554        if(!petname)
5555            petname = cinfo->Name;
5556        return std::string(petname);
5557    }
5558
5559    return *(list0.begin()+urand(0, list0.size()-1)) + *(list1.begin()+urand(0, list1.size()-1));
5560}
5561
5562uint32 ObjectMgr::GeneratePetNumber()
5563{
5564    return ++m_hiPetNumber;
5565}
5566
5567void ObjectMgr::LoadCorpses()
5568{
5569    uint32 count = 0;
5570    //                                                     0           1           2           3            4    5     6     7            8         10
5571    QueryResult *result = CharacterDatabase.PQuery("SELECT position_x, position_y, position_z, orientation, map, data, time, corpse_type, instance, guid FROM corpse WHERE corpse_type <> 0");
5572
5573    if( !result )
5574    {
5575        barGoLink bar( 1 );
5576
5577        bar.step();
5578
5579        sLog.outString();
5580        sLog.outString( ">> Loaded %u corpses", count );
5581        return;
5582    }
5583
5584    barGoLink bar( result->GetRowCount() );
5585
5586    do
5587    {
5588        bar.step();
5589
5590        Field *fields = result->Fetch();
5591
5592        uint32 guid = fields[result->GetFieldCount()-1].GetUInt32();
5593
5594        Corpse *corpse = new Corpse;
5595        if(!corpse->LoadFromDB(guid,fields))
5596        {
5597            delete corpse;
5598            continue;
5599        }
5600
5601        ObjectAccessor::Instance().AddCorpse(corpse);
5602
5603        ++count;
5604    }
5605    while (result->NextRow());
5606    delete result;
5607
5608    sLog.outString();
5609    sLog.outString( ">> Loaded %u corpses", count );
5610}
5611
5612void ObjectMgr::LoadReputationOnKill()
5613{
5614    uint32 count = 0;
5615
5616    //                                                0            1                     2
5617    QueryResult *result = WorldDatabase.Query("SELECT creature_id, RewOnKillRepFaction1, RewOnKillRepFaction2,"
5618    //   3             4             5                   6             7             8                   9
5619        "IsTeamAward1, MaxStanding1, RewOnKillRepValue1, IsTeamAward2, MaxStanding2, RewOnKillRepValue2, TeamDependent "
5620        "FROM creature_onkill_reputation");
5621
5622    if(!result)
5623    {
5624        barGoLink bar(1);
5625
5626        bar.step();
5627
5628        sLog.outString();
5629        sLog.outErrorDb(">> Loaded 0 creature award reputation definitions. DB table `creature_onkill_reputation` is empty.");
5630        return;
5631    }
5632
5633    barGoLink bar(result->GetRowCount());
5634
5635    do
5636    {
5637        Field *fields = result->Fetch();
5638        bar.step();
5639
5640        uint32 creature_id = fields[0].GetUInt32();
5641
5642        ReputationOnKillEntry repOnKill;
5643        repOnKill.repfaction1          = fields[1].GetUInt32();
5644        repOnKill.repfaction2          = fields[2].GetUInt32();
5645        repOnKill.is_teamaward1        = fields[3].GetBool();
5646        repOnKill.reputation_max_cap1  = fields[4].GetUInt32();
5647        repOnKill.repvalue1            = fields[5].GetInt32();
5648        repOnKill.is_teamaward2        = fields[6].GetBool();
5649        repOnKill.reputation_max_cap2  = fields[7].GetUInt32();
5650        repOnKill.repvalue2            = fields[8].GetInt32();
5651        repOnKill.team_dependent       = fields[9].GetUInt8();
5652
5653        if(!GetCreatureTemplate(creature_id))
5654        {
5655            sLog.outErrorDb("Table `creature_onkill_reputation` have data for not existed creature entry (%u), skipped",creature_id);
5656            continue;
5657        }
5658
5659        if(repOnKill.repfaction1)
5660        {
5661            FactionEntry const *factionEntry1 = sFactionStore.LookupEntry(repOnKill.repfaction1);
5662            if(!factionEntry1)
5663            {
5664                sLog.outErrorDb("Faction (faction.dbc) %u does not exist but is used in `creature_onkill_reputation`",repOnKill.repfaction1);
5665                continue;
5666            }
5667        }
5668
5669        if(repOnKill.repfaction2)
5670        {
5671            FactionEntry const *factionEntry2 = sFactionStore.LookupEntry(repOnKill.repfaction2);
5672            if(!factionEntry2)
5673            {
5674                sLog.outErrorDb("Faction (faction.dbc) %u does not exist but is used in `creature_onkill_reputation`",repOnKill.repfaction2);
5675                continue;
5676            }
5677        }
5678
5679        mRepOnKill[creature_id] = repOnKill;
5680
5681        ++count;
5682    } while (result->NextRow());
5683
5684    delete result;
5685
5686    sLog.outString();
5687    sLog.outString(">> Loaded %u creature award reputation definitions", count);
5688}
5689
5690void ObjectMgr::LoadWeatherZoneChances()
5691{
5692    uint32 count = 0;
5693
5694    //                                                0     1                   2                   3                    4                   5                   6                    7                 8                 9                  10                  11                  12
5695    QueryResult *result = WorldDatabase.Query("SELECT zone, spring_rain_chance, spring_snow_chance, spring_storm_chance, summer_rain_chance, summer_snow_chance, summer_storm_chance, fall_rain_chance, fall_snow_chance, fall_storm_chance, winter_rain_chance, winter_snow_chance, winter_storm_chance FROM game_weather");
5696
5697    if(!result)
5698    {
5699        barGoLink bar(1);
5700
5701        bar.step();
5702
5703        sLog.outString();
5704        sLog.outErrorDb(">> Loaded 0 weather definitions. DB table `game_weather` is empty.");
5705        return;
5706    }
5707
5708    barGoLink bar(result->GetRowCount());
5709
5710    do
5711    {
5712        Field *fields = result->Fetch();
5713        bar.step();
5714
5715        uint32 zone_id = fields[0].GetUInt32();
5716
5717        WeatherZoneChances& wzc = mWeatherZoneMap[zone_id];
5718
5719        for(int season = 0; season < WEATHER_SEASONS; ++season)
5720        {
5721            wzc.data[season].rainChance  = fields[season * (MAX_WEATHER_TYPE-1) + 1].GetUInt32();
5722            wzc.data[season].snowChance  = fields[season * (MAX_WEATHER_TYPE-1) + 2].GetUInt32();
5723            wzc.data[season].stormChance = fields[season * (MAX_WEATHER_TYPE-1) + 3].GetUInt32();
5724
5725            if(wzc.data[season].rainChance > 100)
5726            {
5727                wzc.data[season].rainChance = 25;
5728                sLog.outErrorDb("Weather for zone %u season %u has wrong rain chance > 100%",zone_id,season);
5729            }
5730
5731            if(wzc.data[season].snowChance > 100)
5732            {
5733                wzc.data[season].snowChance = 25;
5734                sLog.outErrorDb("Weather for zone %u season %u has wrong snow chance > 100%",zone_id,season);
5735            }
5736
5737            if(wzc.data[season].stormChance > 100)
5738            {
5739                wzc.data[season].stormChance = 25;
5740                sLog.outErrorDb("Weather for zone %u season %u has wrong storm chance > 100%",zone_id,season);
5741            }
5742        }
5743
5744        ++count;
5745    } while (result->NextRow());
5746
5747    delete result;
5748
5749    sLog.outString();
5750    sLog.outString(">> Loaded %u weather definitions", count);
5751}
5752
5753void ObjectMgr::SaveCreatureRespawnTime(uint32 loguid, uint32 instance, time_t t)
5754{
5755    mCreatureRespawnTimes[MAKE_PAIR64(loguid,instance)] = t;
5756    WorldDatabase.PExecute("DELETE FROM creature_respawn WHERE guid = '%u' AND instance = '%u'", loguid, instance);
5757    if(t)
5758        WorldDatabase.PExecute("INSERT INTO creature_respawn VALUES ( '%u', '" I64FMTD "', '%u' )", loguid, uint64(t), instance);
5759}
5760
5761void ObjectMgr::DeleteCreatureData(uint32 guid)
5762{
5763    // remove mapid*cellid -> guid_set map
5764    CreatureData const* data = GetCreatureData(guid);
5765    if(data)
5766        RemoveCreatureFromGrid(guid, data);
5767
5768    mCreatureDataMap.erase(guid);
5769}
5770
5771void ObjectMgr::SaveGORespawnTime(uint32 loguid, uint32 instance, time_t t)
5772{
5773    mGORespawnTimes[MAKE_PAIR64(loguid,instance)] = t;
5774    WorldDatabase.PExecute("DELETE FROM gameobject_respawn WHERE guid = '%u' AND instance = '%u'", loguid, instance);
5775    if(t)
5776        WorldDatabase.PExecute("INSERT INTO gameobject_respawn VALUES ( '%u', '" I64FMTD "', '%u' )", loguid, uint64(t), instance);
5777}
5778
5779void ObjectMgr::DeleteRespawnTimeForInstance(uint32 instance)
5780{
5781    RespawnTimes::iterator next;
5782
5783    for(RespawnTimes::iterator itr = mGORespawnTimes.begin(); itr != mGORespawnTimes.end(); itr = next)
5784    {
5785        next = itr;
5786        ++next;
5787
5788        if(GUID_HIPART(itr->first)==instance)
5789            mGORespawnTimes.erase(itr);
5790    }
5791
5792    for(RespawnTimes::iterator itr = mCreatureRespawnTimes.begin(); itr != mCreatureRespawnTimes.end(); itr = next)
5793    {
5794        next = itr;
5795        ++next;
5796
5797        if(GUID_HIPART(itr->first)==instance)
5798            mCreatureRespawnTimes.erase(itr);
5799    }
5800
5801    WorldDatabase.PExecute("DELETE FROM creature_respawn WHERE instance = '%u'", instance);
5802    WorldDatabase.PExecute("DELETE FROM gameobject_respawn WHERE instance = '%u'", instance);
5803}
5804
5805void ObjectMgr::DeleteGOData(uint32 guid)
5806{
5807    // remove mapid*cellid -> guid_set map
5808    GameObjectData const* data = GetGOData(guid);
5809    if(data)
5810        RemoveGameobjectFromGrid(guid, data);
5811
5812    mGameObjectDataMap.erase(guid);
5813}
5814
5815void ObjectMgr::AddCorpseCellData(uint32 mapid, uint32 cellid, uint32 player_guid, uint32 instance)
5816{
5817    // corpses are always added to spawn mode 0 and they are spawned by their instance id
5818    CellObjectGuids& cell_guids = mMapObjectGuids[MAKE_PAIR32(mapid,0)][cellid];
5819    cell_guids.corpses[player_guid] = instance;
5820}
5821
5822void ObjectMgr::DeleteCorpseCellData(uint32 mapid, uint32 cellid, uint32 player_guid)
5823{
5824    // corpses are always added to spawn mode 0 and they are spawned by their instance id
5825    CellObjectGuids& cell_guids = mMapObjectGuids[MAKE_PAIR32(mapid,0)][cellid];
5826    cell_guids.corpses.erase(player_guid);
5827}
5828
5829void ObjectMgr::LoadQuestRelationsHelper(QuestRelations& map,char const* table)
5830{
5831    map.clear();                                            // need for reload case
5832
5833    uint32 count = 0;
5834
5835    QueryResult *result = WorldDatabase.PQuery("SELECT id,quest FROM %s",table);
5836
5837    if(!result)
5838    {
5839        barGoLink bar(1);
5840
5841        bar.step();
5842
5843        sLog.outString();
5844        sLog.outErrorDb(">> Loaded 0 quest relations from %s. DB table `%s` is empty.",table,table);
5845        return;
5846    }
5847
5848    barGoLink bar(result->GetRowCount());
5849
5850    do
5851    {
5852        Field *fields = result->Fetch();
5853        bar.step();
5854
5855        uint32 id    = fields[0].GetUInt32();
5856        uint32 quest = fields[1].GetUInt32();
5857
5858        if(mQuestTemplates.find(quest) == mQuestTemplates.end())
5859        {
5860            sLog.outErrorDb("Table `%s: Quest %u listed for entry %u does not exist.",table,quest,id);
5861            continue;
5862        }
5863
5864        map.insert(QuestRelations::value_type(id,quest));
5865
5866        ++count;
5867    } while (result->NextRow());
5868
5869    delete result;
5870
5871    sLog.outString();
5872    sLog.outString(">> Loaded %u quest relations from %s", count,table);
5873}
5874
5875void ObjectMgr::LoadGameobjectQuestRelations()
5876{
5877    LoadQuestRelationsHelper(mGOQuestRelations,"gameobject_questrelation");
5878
5879    for(QuestRelations::iterator itr = mGOQuestRelations.begin(); itr != mGOQuestRelations.end(); ++itr)
5880    {
5881        GameObjectInfo const* goInfo = GetGameObjectInfo(itr->first);
5882        if(!goInfo)
5883            sLog.outErrorDb("Table `gameobject_questrelation` have data for not existed gameobject entry (%u) and existed quest %u",itr->first,itr->second);
5884        else if(goInfo->type != GAMEOBJECT_TYPE_QUESTGIVER)
5885            sLog.outErrorDb("Table `gameobject_questrelation` have data gameobject entry (%u) for quest %u, but GO is not GAMEOBJECT_TYPE_QUESTGIVER",itr->first,itr->second);
5886    }
5887}
5888
5889void ObjectMgr::LoadGameobjectInvolvedRelations()
5890{
5891    LoadQuestRelationsHelper(mGOQuestInvolvedRelations,"gameobject_involvedrelation");
5892
5893    for(QuestRelations::iterator itr = mGOQuestInvolvedRelations.begin(); itr != mGOQuestInvolvedRelations.end(); ++itr)
5894    {
5895        GameObjectInfo const* goInfo = GetGameObjectInfo(itr->first);
5896        if(!goInfo)
5897            sLog.outErrorDb("Table `gameobject_involvedrelation` have data for not existed gameobject entry (%u) and existed quest %u",itr->first,itr->second);
5898        else if(goInfo->type != GAMEOBJECT_TYPE_QUESTGIVER)
5899            sLog.outErrorDb("Table `gameobject_involvedrelation` have data gameobject entry (%u) for quest %u, but GO is not GAMEOBJECT_TYPE_QUESTGIVER",itr->first,itr->second);
5900    }
5901}
5902
5903void ObjectMgr::LoadCreatureQuestRelations()
5904{
5905    LoadQuestRelationsHelper(mCreatureQuestRelations,"creature_questrelation");
5906
5907    for(QuestRelations::iterator itr = mCreatureQuestRelations.begin(); itr != mCreatureQuestRelations.end(); ++itr)
5908    {
5909        CreatureInfo const* cInfo = GetCreatureTemplate(itr->first);
5910        if(!cInfo)
5911            sLog.outErrorDb("Table `creature_questrelation` have data for not existed creature entry (%u) and existed quest %u",itr->first,itr->second);
5912        else if(!(cInfo->npcflag & UNIT_NPC_FLAG_QUESTGIVER))
5913            sLog.outErrorDb("Table `creature_questrelation` has creature entry (%u) for quest %u, but npcflag does not include UNIT_NPC_FLAG_QUESTGIVER",itr->first,itr->second);
5914    }
5915}
5916
5917void ObjectMgr::LoadCreatureInvolvedRelations()
5918{
5919    LoadQuestRelationsHelper(mCreatureQuestInvolvedRelations,"creature_involvedrelation");
5920
5921    for(QuestRelations::iterator itr = mCreatureQuestInvolvedRelations.begin(); itr != mCreatureQuestInvolvedRelations.end(); ++itr)
5922    {
5923        CreatureInfo const* cInfo = GetCreatureTemplate(itr->first);
5924        if(!cInfo)
5925            sLog.outErrorDb("Table `creature_involvedrelation` have data for not existed creature entry (%u) and existed quest %u",itr->first,itr->second);
5926        else if(!(cInfo->npcflag & UNIT_NPC_FLAG_QUESTGIVER))
5927            sLog.outErrorDb("Table `creature_involvedrelation` has creature entry (%u) for quest %u, but npcflag does not include UNIT_NPC_FLAG_QUESTGIVER",itr->first,itr->second);
5928    }
5929}
5930
5931void ObjectMgr::LoadReservedPlayersNames()
5932{
5933    m_ReservedNames.clear();                                // need for reload case
5934
5935    QueryResult *result = WorldDatabase.PQuery("SELECT name FROM reserved_name");
5936
5937    uint32 count = 0;
5938
5939    if( !result )
5940    {
5941        barGoLink bar( 1 );
5942        bar.step();
5943
5944        sLog.outString();
5945        sLog.outString( ">> Loaded %u reserved player names", count );
5946        return;
5947    }
5948
5949    barGoLink bar( result->GetRowCount() );
5950
5951    Field* fields;
5952    do
5953    {
5954        bar.step();
5955        fields = result->Fetch();
5956        std::string name= fields[0].GetCppString();
5957        if(normalizePlayerName(name))
5958        {
5959            m_ReservedNames.insert(name);
5960            ++count;
5961        }
5962    } while ( result->NextRow() );
5963
5964    delete result;
5965
5966    sLog.outString();
5967    sLog.outString( ">> Loaded %u reserved player names", count );
5968}
5969
5970enum LanguageType
5971{
5972    LT_BASIC_LATIN    = 0x0000,
5973    LT_EXTENDEN_LATIN = 0x0001,
5974    LT_CYRILLIC       = 0x0002,
5975    LT_EAST_ASIA      = 0x0004,
5976    LT_ANY            = 0xFFFF
5977};
5978
5979static LanguageType GetRealmLanguageType(bool create)
5980{
5981    switch(sWorld.getConfig(CONFIG_REALM_ZONE))
5982    {
5983        case REALM_ZONE_UNKNOWN:                            // any language
5984        case REALM_ZONE_DEVELOPMENT:
5985        case REALM_ZONE_TEST_SERVER:
5986        case REALM_ZONE_QA_SERVER:
5987            return LT_ANY;
5988        case REALM_ZONE_UNITED_STATES:                      // extended-Latin
5989        case REALM_ZONE_OCEANIC:
5990        case REALM_ZONE_LATIN_AMERICA:
5991        case REALM_ZONE_ENGLISH:
5992        case REALM_ZONE_GERMAN:
5993        case REALM_ZONE_FRENCH:
5994        case REALM_ZONE_SPANISH:
5995            return LT_EXTENDEN_LATIN;
5996        case REALM_ZONE_KOREA:                              // East-Asian
5997        case REALM_ZONE_TAIWAN:
5998        case REALM_ZONE_CHINA:
5999            return LT_EAST_ASIA;
6000        case REALM_ZONE_RUSSIAN:                            // Cyrillic
6001            return LT_CYRILLIC;
6002        default:
6003            return create ? LT_BASIC_LATIN : LT_ANY;        // basic-Latin at create, any at login
6004    }
6005}
6006
6007bool isValidString(std::wstring wstr, uint32 strictMask, bool numericOrSpace, bool create = false)
6008{
6009    if(strictMask==0)                                       // any language, ignore realm
6010    {
6011        if(isExtendedLatinString(wstr,numericOrSpace))
6012            return true;
6013        if(isCyrillicString(wstr,numericOrSpace))
6014            return true;
6015        if(isEastAsianString(wstr,numericOrSpace))
6016            return true;
6017        return false;
6018    }
6019
6020    if(strictMask & 0x2)                                    // realm zone specific
6021    {
6022        LanguageType lt = GetRealmLanguageType(create);
6023        if(lt & LT_EXTENDEN_LATIN)
6024            if(isExtendedLatinString(wstr,numericOrSpace))
6025                return true;
6026        if(lt & LT_CYRILLIC)
6027            if(isCyrillicString(wstr,numericOrSpace))
6028                return true;
6029        if(lt & LT_EAST_ASIA)
6030            if(isEastAsianString(wstr,numericOrSpace))
6031                return true;
6032    }
6033
6034    if(strictMask & 0x1)                                    // basic latin
6035    {
6036        if(isBasicLatinString(wstr,numericOrSpace))
6037            return true;
6038    }
6039
6040    return false;
6041}
6042
6043bool ObjectMgr::IsValidName( std::string name, bool create )
6044{
6045    std::wstring wname;
6046    if(!Utf8toWStr(name,wname))
6047        return false;
6048
6049    if(wname.size() < 1 || wname.size() > MAX_PLAYER_NAME)
6050        return false;
6051
6052    uint32 strictMask = sWorld.getConfig(CONFIG_STRICT_PLAYER_NAMES);
6053
6054    return isValidString(wname,strictMask,false,create);
6055}
6056
6057bool ObjectMgr::IsValidCharterName( std::string name )
6058{
6059    std::wstring wname;
6060    if(!Utf8toWStr(name,wname))
6061        return false;
6062
6063    if(wname.size() < 1)
6064        return false;
6065
6066    uint32 strictMask = sWorld.getConfig(CONFIG_STRICT_CHARTER_NAMES);
6067
6068    return isValidString(wname,strictMask,true);
6069}
6070
6071bool ObjectMgr::IsValidPetName( std::string name )
6072{
6073    std::wstring wname;
6074    if(!Utf8toWStr(name,wname))
6075        return false;
6076
6077    if(wname.size() < 1)
6078        return false;
6079
6080    uint32 strictMask = sWorld.getConfig(CONFIG_STRICT_PET_NAMES);
6081
6082    return isValidString(wname,strictMask,false);
6083}
6084
6085int ObjectMgr::GetIndexForLocale( LocaleConstant loc )
6086{
6087    if(loc==LOCALE_enUS)
6088        return -1;
6089
6090    for(size_t i=0;i < m_LocalForIndex.size(); ++i)
6091        if(m_LocalForIndex[i]==loc)
6092            return i;
6093
6094    return -1;
6095}
6096
6097LocaleConstant ObjectMgr::GetLocaleForIndex(int i)
6098{
6099    if (i<0 || i>=m_LocalForIndex.size())
6100        return LOCALE_enUS;
6101
6102    return m_LocalForIndex[i];
6103}
6104
6105int ObjectMgr::GetOrNewIndexForLocale( LocaleConstant loc )
6106{
6107    if(loc==LOCALE_enUS)
6108        return -1;
6109
6110    for(size_t i=0;i < m_LocalForIndex.size(); ++i)
6111        if(m_LocalForIndex[i]==loc)
6112            return i;
6113
6114    m_LocalForIndex.push_back(loc);
6115    return m_LocalForIndex.size()-1;
6116}
6117
6118void ObjectMgr::LoadBattleMastersEntry()
6119{
6120    mBattleMastersMap.clear();                              // need for reload case
6121
6122    QueryResult *result = WorldDatabase.Query( "SELECT entry,bg_template FROM battlemaster_entry" );
6123
6124    uint32 count = 0;
6125
6126    if( !result )
6127    {
6128        barGoLink bar( 1 );
6129        bar.step();
6130
6131        sLog.outString();
6132        sLog.outString( ">> Loaded 0 battlemaster entries - table is empty!" );
6133        return;
6134    }
6135
6136    barGoLink bar( result->GetRowCount() );
6137
6138    do
6139    {
6140        ++count;
6141        bar.step();
6142
6143        Field *fields = result->Fetch();
6144
6145        uint32 entry = fields[0].GetUInt32();
6146        uint32 bgTypeId  = fields[1].GetUInt32();
6147
6148        mBattleMastersMap[entry] = bgTypeId;
6149
6150    } while( result->NextRow() );
6151
6152    delete result;
6153
6154    sLog.outString();
6155    sLog.outString( ">> Loaded %u battlemaster entries", count );
6156}
6157
6158void ObjectMgr::LoadGameObjectForQuests()
6159{
6160    mGameObjectForQuestSet.clear();                         // need for reload case
6161
6162    uint32 count = 0;
6163
6164    // collect GO entries for GO that must activated
6165    for(uint32 go_entry = 1; go_entry < sGOStorage.MaxEntry; ++go_entry)
6166    {
6167        GameObjectInfo const* goInfo = sGOStorage.LookupEntry<GameObjectInfo>(go_entry);
6168        if(!goInfo)
6169            continue;
6170
6171        switch(goInfo->type)
6172        {
6173            // scan GO chest with loot including quest items
6174            case GAMEOBJECT_TYPE_CHEST:
6175            {
6176                uint32 loot_id = GameObject::GetLootId(goInfo);
6177
6178                // find quest loot for GO
6179                if(LootTemplates_Gameobject.HaveQuestLootFor(loot_id))
6180                {
6181                    mGameObjectForQuestSet.insert(go_entry);
6182                    ++count;
6183                }
6184                break;
6185            }
6186            case GAMEOBJECT_TYPE_GOOBER:
6187            {
6188                if(goInfo->goober.questId)                  //quests objects
6189                {
6190                    mGameObjectForQuestSet.insert(go_entry);
6191                    count++;
6192                }
6193                break;
6194            }
6195            default:
6196                break;
6197        }
6198    }
6199
6200    sLog.outString();
6201    sLog.outString( ">> Loaded %u GameObject for quests", count );
6202}
6203
6204bool ObjectMgr::LoadTrinityStrings(DatabaseType& db, char const* table, int32 min_value, int32 max_value)
6205{
6206    // cleanup affected map part for reloading case
6207    for(TrinityStringLocaleMap::iterator itr = mTrinityStringLocaleMap.begin(); itr != mTrinityStringLocaleMap.end();)
6208    {
6209        if(itr->first >= min_value && itr->first <= max_value)
6210        {
6211            TrinityStringLocaleMap::iterator itr2 = itr;
6212            ++itr;
6213            mTrinityStringLocaleMap.erase(itr2);
6214        }
6215        else
6216            ++itr;
6217    }
6218
6219    QueryResult *result = db.PQuery("SELECT entry,content_default,content_loc1,content_loc2,content_loc3,content_loc4,content_loc5,content_loc6,content_loc7,content_loc8 FROM %s",table);
6220
6221    if(!result)
6222    {
6223        barGoLink bar(1);
6224
6225        bar.step();
6226
6227        sLog.outString("");
6228        if(min_value > 0)                                   // error only in case internal strings
6229            sLog.outErrorDb(">> Loaded 0 trinity strings. DB table `%s` is empty. Cannot continue.",table);
6230        else
6231            sLog.outString(">> Loaded 0 string templates. DB table `%s` is empty.",table);
6232        return false;
6233    }
6234
6235    uint32 count = 0;
6236
6237    barGoLink bar(result->GetRowCount());
6238
6239    do
6240    {
6241        Field *fields = result->Fetch();
6242        bar.step();
6243
6244        int32 entry = fields[0].GetInt32();
6245
6246        if(entry==0)
6247        {
6248            sLog.outErrorDb("Table `%s` contain reserved entry 0, ignored.",table);
6249            continue;
6250        }
6251        else if(entry < min_value || entry > max_value)
6252        {
6253            int32 start = min_value > 0 ? min_value : max_value;
6254            int32 end   = min_value > 0 ? max_value : min_value;
6255            sLog.outErrorDb("Table `%s` contain entry %i out of allowed range (%d - %d), ignored.",table,entry,start,end);
6256            continue;
6257        }
6258
6259        TrinityStringLocale& data = mTrinityStringLocaleMap[entry];
6260
6261        if(data.Content.size() > 0)
6262        {
6263            sLog.outErrorDb("Table `%s` contain data for already loaded entry  %i (from another table?), ignored.",table,entry);
6264            continue;
6265        }
6266
6267        data.Content.resize(1);
6268        ++count;
6269
6270        // 0 -> default, idx in to idx+1
6271        data.Content[0] = fields[1].GetCppString();
6272
6273        for(int i = 1; i < MAX_LOCALE; ++i)
6274        {
6275            std::string str = fields[i+1].GetCppString();
6276            if(!str.empty())
6277            {
6278                int idx = GetOrNewIndexForLocale(LocaleConstant(i));
6279                if(idx >= 0)
6280                {
6281                    // 0 -> default, idx in to idx+1
6282                    if(data.Content.size() <= idx+1)
6283                        data.Content.resize(idx+2);
6284
6285                    data.Content[idx+1] = str;
6286                }
6287            }
6288        }
6289    } while (result->NextRow());
6290
6291    delete result;
6292
6293    sLog.outString();
6294    if(min_value > 0)                                       // internal Trinity strings
6295        sLog.outString( ">> Loaded %u Trinity strings from table %s", count,table);
6296    else
6297        sLog.outString( ">> Loaded %u string templates from %s", count,table);
6298
6299    return true;
6300}
6301
6302const char *ObjectMgr::GetTrinityString(int32 entry, int locale_idx) const
6303{
6304    // locale_idx==-1 -> default, locale_idx >= 0 in to idx+1
6305    // Content[0] always exist if exist TrinityStringLocale
6306    if(TrinityStringLocale const *msl = GetTrinityStringLocale(entry))
6307    {
6308        if(msl->Content.size() > locale_idx+1 && !msl->Content[locale_idx+1].empty())
6309            return msl->Content[locale_idx+1].c_str();
6310        else
6311            return msl->Content[0].c_str();
6312    }
6313
6314    if(entry > 0)
6315        sLog.outErrorDb("Entry %i not found in `trinity_string` table.",entry);
6316    else
6317        sLog.outErrorDb("Trinity string entry %i not found in DB.",entry);
6318    return "<error>";
6319}
6320
6321void ObjectMgr::LoadFishingBaseSkillLevel()
6322{
6323    mFishingBaseForArea.clear();                            // for relaod case
6324
6325    uint32 count = 0;
6326    QueryResult *result = WorldDatabase.Query("SELECT entry,skill FROM skill_fishing_base_level");
6327
6328    if( !result )
6329    {
6330        barGoLink bar( 1 );
6331
6332        bar.step();
6333
6334        sLog.outString();
6335        sLog.outErrorDb(">> Loaded `skill_fishing_base_level`, table is empty!");
6336        return;
6337    }
6338
6339    barGoLink bar( result->GetRowCount() );
6340
6341    do
6342    {
6343        bar.step();
6344
6345        Field *fields = result->Fetch();
6346        uint32 entry  = fields[0].GetUInt32();
6347        int32 skill   = fields[1].GetInt32();
6348
6349        AreaTableEntry const* fArea = GetAreaEntryByAreaID(entry);
6350        if(!fArea)
6351        {
6352            sLog.outErrorDb("AreaId %u defined in `skill_fishing_base_level` does not exist",entry);
6353            continue;
6354        }
6355
6356        mFishingBaseForArea[entry] = skill;
6357        ++count;
6358    }
6359    while (result->NextRow());
6360
6361    delete result;
6362
6363    sLog.outString();
6364    sLog.outString( ">> Loaded %u areas for fishing base skill level", count );
6365}
6366
6367// Searches for the same condition already in Conditions store
6368// Returns Id if found, else adds it to Conditions and returns Id
6369uint16 ObjectMgr::GetConditionId( ConditionType condition, uint32 value1, uint32 value2 )
6370{
6371    PlayerCondition lc = PlayerCondition(condition, value1, value2);
6372    for (uint16 i=0; i < mConditions.size(); ++i)
6373    {
6374        if (lc == mConditions[i])
6375            return i;
6376    }
6377
6378    mConditions.push_back(lc);
6379
6380    if(mConditions.size() > 0xFFFF)
6381    {
6382        sLog.outError("Conditions store overflow! Current and later loaded conditions will ignored!");
6383        return 0;
6384    }
6385
6386    return mConditions.size() - 1;
6387}
6388
6389bool ObjectMgr::CheckDeclinedNames( std::wstring mainpart, DeclinedName const& names )
6390{
6391    for(int i =0; i < MAX_DECLINED_NAME_CASES; ++i)
6392    {
6393        std::wstring wname;
6394        if(!Utf8toWStr(names.name[i],wname))
6395            return false;
6396
6397        if(mainpart!=GetMainPartOfName(wname,i+1))
6398            return false;
6399    }
6400    return true;
6401}
6402
6403const char* ObjectMgr::GetAreaTriggerScriptName(uint32 id)
6404{
6405    AreaTriggerScriptMap::const_iterator i = mAreaTriggerScripts.find(id);
6406    if(i!= mAreaTriggerScripts.end())
6407        return i->second.c_str();
6408    return "";
6409}
6410
6411// Checks if player meets the condition
6412bool PlayerCondition::Meets(Player const * player) const
6413{
6414    if( !player )
6415        return false;                                       // player not present, return false
6416
6417    switch (condition)
6418    {
6419        case CONDITION_NONE:
6420            return true;                                    // empty condition, always met
6421        case CONDITION_AURA:
6422            return player->HasAura(value1, value2);
6423        case CONDITION_ITEM:
6424            return player->HasItemCount(value1, value2);
6425        case CONDITION_ITEM_EQUIPPED:
6426            return player->GetItemOrItemWithGemEquipped(value1) != NULL;
6427        case CONDITION_ZONEID:
6428            return player->GetZoneId() == value1;
6429        case CONDITION_REPUTATION_RANK:
6430        {
6431            FactionEntry const* faction = sFactionStore.LookupEntry(value1);
6432            return faction && player->GetReputationRank(faction) >= value2;
6433        }
6434        case CONDITION_TEAM:
6435            return player->GetTeam() == value1;
6436        case CONDITION_SKILL:
6437            return player->HasSkill(value1) && player->GetBaseSkillValue(value1) >= value2;
6438        case CONDITION_QUESTREWARDED:
6439            return player->GetQuestRewardStatus(value1);
6440        case CONDITION_QUESTTAKEN:
6441        {
6442            QuestStatus status = player->GetQuestStatus(value1);
6443            return (status == QUEST_STATUS_INCOMPLETE);
6444        }
6445        case CONDITION_AD_COMMISSION_AURA:
6446        {
6447            Unit::AuraMap const& auras = player->GetAuras();
6448            for(Unit::AuraMap::const_iterator itr = auras.begin(); itr != auras.end(); ++itr)
6449                if((itr->second->GetSpellProto()->Attributes & 0x1000010) && itr->second->GetSpellProto()->SpellVisual==3580)
6450                    return true;
6451            return false;
6452        }
6453        default:
6454            return false;
6455    }
6456}
6457
6458// Verification of condition values validity
6459bool PlayerCondition::IsValid(ConditionType condition, uint32 value1, uint32 value2)
6460{
6461    if( condition >= MAX_CONDITION)                         // Wrong condition type
6462    {
6463        sLog.outErrorDb("Condition has bad type of %u, skipped ", condition );
6464        return false;
6465    }
6466
6467    switch (condition)
6468    {
6469        case CONDITION_AURA:
6470        {
6471            if(!sSpellStore.LookupEntry(value1))
6472            {
6473                sLog.outErrorDb("Aura condition requires to have non existing spell (Id: %d), skipped", value1);
6474                return false;
6475            }
6476            if(value2 > 2)
6477            {
6478                sLog.outErrorDb("Aura condition requires to have non existing effect index (%u) (must be 0..2), skipped", value2);
6479                return false;
6480            }
6481            break;
6482        }
6483        case CONDITION_ITEM:
6484        {
6485            ItemPrototype const *proto = objmgr.GetItemPrototype(value1);
6486            if(!proto)
6487            {
6488                sLog.outErrorDb("Item condition requires to have non existing item (%u), skipped", value1);
6489                return false;
6490            }
6491            break;
6492        }
6493        case CONDITION_ITEM_EQUIPPED:
6494        {
6495            ItemPrototype const *proto = objmgr.GetItemPrototype(value1);
6496            if(!proto)
6497            {
6498                sLog.outErrorDb("ItemEquipped condition requires to have non existing item (%u) equipped, skipped", value1);
6499                return false;
6500            }
6501            break;
6502        }
6503        case CONDITION_ZONEID:
6504        {
6505            AreaTableEntry const* areaEntry = GetAreaEntryByAreaID(value1);
6506            if(!areaEntry)
6507            {
6508                sLog.outErrorDb("Zone condition requires to be in non existing area (%u), skipped", value1);
6509                return false;
6510            }
6511            if(areaEntry->zone != 0)
6512            {
6513                sLog.outErrorDb("Zone condition requires to be in area (%u) which is a subzone but zone expected, skipped", value1);
6514                return false;
6515            }
6516            break;
6517        }
6518        case CONDITION_REPUTATION_RANK:
6519        {
6520            FactionEntry const* factionEntry = sFactionStore.LookupEntry(value1);
6521            if(!factionEntry)
6522            {
6523                sLog.outErrorDb("Reputation condition requires to have reputation non existing faction (%u), skipped", value1);
6524                return false;
6525            }
6526            break;
6527        }
6528        case CONDITION_TEAM:
6529        {
6530            if (value1 != ALLIANCE && value1 != HORDE)
6531            {
6532                sLog.outErrorDb("Team condition specifies unknown team (%u), skipped", value1);
6533                return false;
6534            }
6535            break;
6536        }
6537        case CONDITION_SKILL:
6538        {
6539            SkillLineEntry const *pSkill = sSkillLineStore.LookupEntry(value1);
6540            if (!pSkill)
6541            {
6542                sLog.outErrorDb("Skill condition specifies non-existing skill (%u), skipped", value1);
6543                return false;
6544            }
6545            if (value2 < 1 || value2 > sWorld.GetConfigMaxSkillValue() )
6546            {
6547                sLog.outErrorDb("Skill condition specifies invalid skill value (%u), skipped", value2);
6548                return false;
6549            }
6550            break;
6551        }
6552        case CONDITION_QUESTREWARDED:
6553        case CONDITION_QUESTTAKEN:
6554        {
6555            Quest const *Quest = objmgr.GetQuestTemplate(value1);
6556            if (!Quest)
6557            {
6558                sLog.outErrorDb("Quest condition specifies non-existing quest (%u), skipped", value1);
6559                return false;
6560            }
6561            if(value2)
6562                sLog.outErrorDb("Quest condition has useless data in value2 (%u)!", value2);
6563            break;
6564        }
6565        case CONDITION_AD_COMMISSION_AURA:
6566        {
6567            if(value1)
6568                sLog.outErrorDb("Quest condition has useless data in value1 (%u)!", value1);
6569            if(value2)
6570                sLog.outErrorDb("Quest condition has useless data in value2 (%u)!", value2);
6571            break;
6572        }
6573    }
6574    return true;
6575}
6576
6577SkillRangeType GetSkillRangeType(SkillLineEntry const *pSkill, bool racial)
6578{
6579    switch(pSkill->categoryId)
6580    {
6581        case SKILL_CATEGORY_LANGUAGES: return SKILL_RANGE_LANGUAGE;
6582        case SKILL_CATEGORY_WEAPON:
6583            if(pSkill->id!=SKILL_FIST_WEAPONS)
6584                return SKILL_RANGE_LEVEL;
6585            else
6586                return SKILL_RANGE_MONO;
6587        case SKILL_CATEGORY_ARMOR:
6588        case SKILL_CATEGORY_CLASS:
6589            if(pSkill->id != SKILL_POISONS && pSkill->id != SKILL_LOCKPICKING)
6590                return SKILL_RANGE_MONO;
6591            else
6592                return SKILL_RANGE_LEVEL;
6593        case SKILL_CATEGORY_SECONDARY:
6594        case SKILL_CATEGORY_PROFESSION:
6595            // not set skills for professions and racial abilities
6596            if(IsProfessionSkill(pSkill->id))
6597                return SKILL_RANGE_RANK;
6598            else if(racial)
6599                return SKILL_RANGE_NONE;
6600            else
6601                return SKILL_RANGE_MONO;
6602        default:
6603        case SKILL_CATEGORY_ATTRIBUTES:                     //not found in dbc
6604        case SKILL_CATEGORY_NOT_DISPLAYED:                  //only GENEREC(DND)
6605            return SKILL_RANGE_NONE;
6606    }
6607}
6608
6609void ObjectMgr::LoadGameTele()
6610{
6611    m_GameTeleMap.clear();                                  // for relaod case
6612
6613    uint32 count = 0;
6614    QueryResult *result = WorldDatabase.Query("SELECT id, position_x, position_y, position_z, orientation, map, name FROM game_tele");
6615
6616    if( !result )
6617    {
6618        barGoLink bar( 1 );
6619
6620        bar.step();
6621
6622        sLog.outString();
6623        sLog.outErrorDb(">> Loaded `game_tele`, table is empty!");
6624        return;
6625    }
6626
6627    barGoLink bar( result->GetRowCount() );
6628
6629    do
6630    {
6631        bar.step();
6632
6633        Field *fields = result->Fetch();
6634
6635        uint32 id         = fields[0].GetUInt32();
6636
6637        GameTele gt;
6638
6639        gt.position_x     = fields[1].GetFloat();
6640        gt.position_y     = fields[2].GetFloat();
6641        gt.position_z     = fields[3].GetFloat();
6642        gt.orientation    = fields[4].GetFloat();
6643        gt.mapId          = fields[5].GetUInt32();
6644        gt.name           = fields[6].GetCppString();
6645
6646        if(!MapManager::IsValidMapCoord(gt.mapId,gt.position_x,gt.position_y,gt.position_z,gt.orientation))
6647        {
6648            sLog.outErrorDb("Wrong position for id %u (name: %s) in `game_tele` table, ignoring.",id,gt.name.c_str());
6649            continue;
6650        }
6651
6652        if(!Utf8toWStr(gt.name,gt.wnameLow))
6653        {
6654            sLog.outErrorDb("Wrong UTF8 name for id %u in `game_tele` table, ignoring.",id);
6655            continue;
6656        }
6657
6658        wstrToLower( gt.wnameLow );
6659
6660        m_GameTeleMap[id] = gt;
6661
6662        ++count;
6663    }
6664    while (result->NextRow());
6665
6666    delete result;
6667
6668    sLog.outString();
6669    sLog.outString( ">> Loaded %u game tele's", count );
6670}
6671
6672GameTele const* ObjectMgr::GetGameTele(std::string name) const
6673{
6674    // explicit name case
6675    std::wstring wname;
6676    if(!Utf8toWStr(name,wname))
6677        return false;
6678
6679    // converting string that we try to find to lower case
6680    wstrToLower( wname );
6681
6682    for(GameTeleMap::const_iterator itr = m_GameTeleMap.begin(); itr != m_GameTeleMap.end(); ++itr)
6683        if(itr->second.wnameLow == wname)
6684            return &itr->second;
6685
6686    return NULL;
6687}
6688
6689bool ObjectMgr::AddGameTele(GameTele& tele)
6690{
6691    // find max id
6692    uint32 new_id = 0;
6693    for(GameTeleMap::const_iterator itr = m_GameTeleMap.begin(); itr != m_GameTeleMap.end(); ++itr)
6694        if(itr->first > new_id)
6695            new_id = itr->first;
6696   
6697    // use next
6698    ++new_id;
6699
6700    if(!Utf8toWStr(tele.name,tele.wnameLow))
6701        return false;
6702
6703    wstrToLower( tele.wnameLow );
6704
6705    m_GameTeleMap[new_id] = tele;
6706
6707    return WorldDatabase.PExecuteLog("INSERT INTO game_tele (id,position_x,position_y,position_z,orientation,map,name) VALUES (%u,%f,%f,%f,%f,%d,'%s')",
6708        new_id,tele.position_x,tele.position_y,tele.position_z,tele.orientation,tele.mapId,tele.name.c_str());
6709}
6710
6711bool ObjectMgr::DeleteGameTele(std::string name)
6712{
6713    // explicit name case
6714    std::wstring wname;
6715    if(!Utf8toWStr(name,wname))
6716        return false;
6717
6718    // converting string that we try to find to lower case
6719    wstrToLower( wname );
6720
6721    for(GameTeleMap::iterator itr = m_GameTeleMap.begin(); itr != m_GameTeleMap.end(); ++itr)
6722    {
6723        if(itr->second.wnameLow == wname)
6724        {
6725            WorldDatabase.PExecuteLog("DELETE FROM game_tele WHERE name = '%s'",itr->second.name.c_str());
6726            m_GameTeleMap.erase(itr);
6727            return true;
6728        }
6729    }
6730
6731    return false;
6732}
6733
6734void ObjectMgr::LoadTrainerSpell()
6735{
6736    // For reload case
6737    for (CacheTrainerSpellMap::iterator itr = m_mCacheTrainerSpellMap.begin(); itr != m_mCacheTrainerSpellMap.end(); ++itr)
6738        itr->second.Clear();
6739    m_mCacheTrainerSpellMap.clear();
6740
6741    std::set<uint32> skip_trainers;
6742
6743    QueryResult *result = WorldDatabase.PQuery("SELECT entry, spell,spellcost,reqskill,reqskillvalue,reqlevel FROM npc_trainer");
6744
6745    if( !result )
6746    {
6747        barGoLink bar( 1 );
6748
6749        bar.step();
6750
6751        sLog.outString();
6752        sLog.outErrorDb(">> Loaded `npc_trainer`, table is empty!");
6753        return;
6754    }
6755
6756    barGoLink bar( result->GetRowCount() );
6757
6758    uint32 count = 0;
6759    do
6760    {
6761        bar.step();
6762
6763        Field* fields = result->Fetch();
6764
6765        uint32 entry  = fields[0].GetUInt32();
6766        uint32 spell  = fields[1].GetUInt32();
6767
6768        CreatureInfo const* cInfo = GetCreatureTemplate(entry);
6769
6770        if(!cInfo)
6771        {
6772            sLog.outErrorDb("Table `npc_trainer` have entry for not existed creature template (Entry: %u), ignore", entry);
6773            continue;
6774        }
6775
6776        if(!(cInfo->npcflag & UNIT_NPC_FLAG_TRAINER))
6777        {
6778            if(skip_trainers.count(entry) == 0)
6779            {
6780                sLog.outErrorDb("Table `npc_trainer` have data for not creature template (Entry: %u) without trainer flag, ignore", entry);
6781                skip_trainers.insert(entry);
6782            }
6783            continue;
6784        }
6785
6786        SpellEntry const *spellinfo = sSpellStore.LookupEntry(spell);
6787        if(!spellinfo)
6788        {
6789            sLog.outErrorDb("Table `npc_trainer` for Trainer (Entry: %u ) has non existing spell %u, ignore", entry,spell);
6790            continue;
6791        }
6792
6793        if(!SpellMgr::IsSpellValid(spellinfo))
6794        {
6795            sLog.outErrorDb("Table `npc_trainer` for Trainer (Entry: %u) has broken learning spell %u, ignore", entry, spell);
6796            continue;
6797        }
6798
6799        TrainerSpell* pTrainerSpell = new TrainerSpell();
6800        pTrainerSpell->spell         = spell;
6801        pTrainerSpell->spellcost     = fields[2].GetUInt32();
6802        pTrainerSpell->reqskill      = fields[3].GetUInt32();
6803        pTrainerSpell->reqskillvalue = fields[4].GetUInt32();
6804        pTrainerSpell->reqlevel      = fields[5].GetUInt32();
6805
6806        if(!pTrainerSpell->reqlevel)
6807            pTrainerSpell->reqlevel = spellinfo->spellLevel;
6808
6809
6810        TrainerSpellData& data = m_mCacheTrainerSpellMap[entry];
6811
6812        if(SpellMgr::IsProfessionSpell(spell))
6813            data.trainerType = 2;
6814
6815        data.spellList.push_back(pTrainerSpell);
6816        ++count;
6817
6818    } while (result->NextRow());
6819    delete result;
6820
6821    sLog.outString();
6822    sLog.outString( ">> Loaded Trainers %d", count );
6823}
6824
6825void ObjectMgr::LoadVendors()
6826{
6827    // For reload case
6828    for (CacheVendorItemMap::iterator itr = m_mCacheVendorItemMap.begin(); itr != m_mCacheVendorItemMap.end(); ++itr)
6829        itr->second.Clear();
6830    m_mCacheVendorItemMap.clear();
6831
6832    std::set<uint32> skip_vendors;
6833
6834    QueryResult *result = WorldDatabase.PQuery("SELECT entry, item, maxcount, incrtime, ExtendedCost FROM npc_vendor");
6835    if( !result )
6836    {
6837        barGoLink bar( 1 );
6838
6839        bar.step();
6840
6841        sLog.outString();
6842        sLog.outErrorDb(">> Loaded `npc_vendor`, table is empty!");
6843        return;
6844    }
6845
6846    barGoLink bar( result->GetRowCount() );
6847
6848    uint32 count = 0;
6849    do
6850    {
6851        bar.step();
6852        Field* fields = result->Fetch();
6853
6854        uint32 entry        = fields[0].GetUInt32();
6855        uint32 item_id      = fields[1].GetUInt32();
6856        uint32 maxcount     = fields[2].GetUInt32();
6857        uint32 incrtime     = fields[3].GetUInt32();
6858        uint32 ExtendedCost = fields[4].GetUInt32();
6859
6860        if(!IsVendorItemValid(entry,item_id,maxcount,incrtime,ExtendedCost,NULL,&skip_vendors))
6861            continue;
6862
6863        VendorItemData& vList = m_mCacheVendorItemMap[entry];
6864
6865        vList.AddItem(item_id,maxcount,incrtime,ExtendedCost);
6866        ++count;
6867
6868    } while (result->NextRow());
6869    delete result;
6870
6871    sLog.outString();
6872    sLog.outString( ">> Loaded %d Vendors ", count );
6873}
6874
6875void ObjectMgr::LoadNpcTextId()
6876{
6877
6878    m_mCacheNpcTextIdMap.clear();
6879
6880    QueryResult* result = WorldDatabase.PQuery("SELECT npc_guid, textid FROM npc_gossip");
6881    if( !result )
6882    {
6883        barGoLink bar( 1 );
6884
6885        bar.step();
6886
6887        sLog.outString();
6888        sLog.outErrorDb(">> Loaded `npc_gossip`, table is empty!");
6889        return;
6890    }
6891
6892    barGoLink bar( result->GetRowCount() );
6893
6894    uint32 count = 0;
6895    uint32 guid,textid;
6896    do
6897    {
6898        bar.step();
6899
6900        Field* fields = result->Fetch();
6901
6902        guid   = fields[0].GetUInt32();
6903        textid = fields[1].GetUInt32();
6904
6905        if (!GetCreatureData(guid))
6906        {
6907            sLog.outErrorDb("Table `npc_gossip` have not existed creature (GUID: %u) entry, ignore. ",guid);
6908            continue;
6909        }
6910        if (!GetGossipText(textid))
6911        {
6912            sLog.outErrorDb("Table `npc_gossip` for creature (GUID: %u) have wrong Textid (%u), ignore. ", guid, textid);
6913            continue;
6914        }
6915
6916        m_mCacheNpcTextIdMap[guid] = textid ;
6917        ++count;
6918
6919    } while (result->NextRow());
6920    delete result;
6921
6922    sLog.outString();
6923    sLog.outString( ">> Loaded %d NpcTextId ", count );
6924}
6925
6926void ObjectMgr::AddVendorItem( uint32 entry,uint32 item, uint32 maxcount, uint32 incrtime, uint32 extendedcost, bool savetodb)
6927{
6928    VendorItemData& vList = m_mCacheVendorItemMap[entry];
6929    vList.AddItem(item,maxcount,incrtime,extendedcost);
6930
6931    if(savetodb) WorldDatabase.PExecuteLog("INSERT INTO npc_vendor (entry,item,maxcount,incrtime,extendedcost) VALUES('%u','%u','%u','%u','%u')",entry, item, maxcount,incrtime,extendedcost);
6932}
6933
6934bool ObjectMgr::RemoveVendorItem( uint32 entry,uint32 item, bool savetodb)
6935{
6936    CacheVendorItemMap::iterator  iter = m_mCacheVendorItemMap.find(entry);
6937    if(iter == m_mCacheVendorItemMap.end())
6938        return false;
6939
6940    if(!iter->second.FindItem(item))
6941        return false;
6942
6943    iter->second.RemoveItem(item);
6944    if(savetodb) WorldDatabase.PExecuteLog("DELETE FROM npc_vendor WHERE entry='%u' AND item='%u'",entry, item);
6945    return true;
6946}
6947
6948bool ObjectMgr::IsVendorItemValid( uint32 vendor_entry, uint32 item_id, uint32 maxcount, uint32 incrtime, uint32 ExtendedCost, Player* pl, std::set<uint32>* skip_vendors, uint32 ORnpcflag ) const
6949{
6950    CreatureInfo const* cInfo = GetCreatureTemplate(vendor_entry);
6951    if(!cInfo)
6952    {
6953        if(pl)
6954            ChatHandler(pl).SendSysMessage(LANG_COMMAND_VENDORSELECTION);
6955        else
6956            sLog.outErrorDb("Table `(game_event_)npc_vendor` have data for not existed creature template (Entry: %u), ignore", vendor_entry);
6957        return false;
6958    }
6959
6960    if(!((cInfo->npcflag | ORnpcflag) & UNIT_NPC_FLAG_VENDOR))
6961    {
6962        if(!skip_vendors || skip_vendors->count(vendor_entry)==0)
6963        {
6964            if(pl)
6965                ChatHandler(pl).SendSysMessage(LANG_COMMAND_VENDORSELECTION);
6966            else
6967                sLog.outErrorDb("Table `(game_event_)npc_vendor` have data for not creature template (Entry: %u) without vendor flag, ignore", vendor_entry);
6968
6969            if(skip_vendors)
6970                skip_vendors->insert(vendor_entry);
6971        }
6972        return false;
6973    }
6974
6975    if(!GetItemPrototype(item_id))
6976    {
6977        if(pl)
6978            ChatHandler(pl).PSendSysMessage(LANG_ITEM_NOT_FOUND, item_id);
6979        else
6980            sLog.outErrorDb("Table `(game_event_)npc_vendor` for Vendor (Entry: %u) have in item list non-existed item (%u), ignore",vendor_entry,item_id);
6981        return false;
6982    }
6983
6984    if(ExtendedCost && !sItemExtendedCostStore.LookupEntry(ExtendedCost))
6985    {
6986        if(pl)
6987            ChatHandler(pl).PSendSysMessage(LANG_EXTENDED_COST_NOT_EXIST,ExtendedCost);
6988        else
6989            sLog.outErrorDb("Table `(game_event_)npc_vendor` have Item (Entry: %u) with wrong ExtendedCost (%u) for vendor (%u), ignore",item_id,ExtendedCost,vendor_entry);
6990        return false;
6991    }
6992
6993    if(maxcount > 0 && incrtime == 0)
6994    {
6995        if(pl)
6996            ChatHandler(pl).PSendSysMessage("MaxCount!=0 (%u) but IncrTime==0", maxcount);
6997        else
6998            sLog.outErrorDb( "Table `(game_event_)npc_vendor` has `maxcount` (%u) for item %u of vendor (Entry: %u) but `incrtime`=0, ignore", maxcount, item_id, vendor_entry);
6999        return false;
7000    }
7001    else if(maxcount==0 && incrtime > 0)
7002    {
7003        if(pl)
7004            ChatHandler(pl).PSendSysMessage("MaxCount==0 but IncrTime<>=0");
7005        else
7006            sLog.outErrorDb( "Table `(game_event_)npc_vendor` has `maxcount`=0 for item %u of vendor (Entry: %u) but `incrtime`<>0, ignore", item_id, vendor_entry);
7007        return false;
7008    }
7009
7010    VendorItemData const* vItems = GetNpcVendorItemList(vendor_entry);
7011    if(!vItems)
7012        return true;                                        // later checks for non-empty lists
7013
7014    if(vItems->FindItem(item_id))
7015    {
7016        if(pl)
7017            ChatHandler(pl).PSendSysMessage(LANG_ITEM_ALREADY_IN_LIST,item_id);
7018        else
7019            sLog.outErrorDb( "Table `(game_event_)npc_vendor` has duplicate items %u for vendor (Entry: %u), ignore", item_id, vendor_entry);
7020        return false;
7021    }
7022
7023    if(vItems->GetItemCount() >= MAX_VENDOR_ITEMS)
7024    {
7025        if(pl)
7026            ChatHandler(pl).SendSysMessage(LANG_COMMAND_ADDVENDORITEMITEMS);
7027        else
7028            sLog.outErrorDb( "Table `npc_vendor` has too many items (%u >= %i) for vendor (Entry: %u), ignore", vItems->GetItemCount(), MAX_VENDOR_ITEMS, vendor_entry);
7029        return false;
7030    }
7031
7032    return true;
7033}
7034
7035// Functions for scripting access
7036const char* GetAreaTriggerScriptNameById(uint32 id)
7037{
7038    return objmgr.GetAreaTriggerScriptName(id);
7039}
7040
7041bool LoadTrinityStrings(DatabaseType& db, char const* table,int32 start_value, int32 end_value)
7042{
7043    if(start_value >= 0 || start_value <= end_value)        // start/end reversed for negative values
7044    {
7045        sLog.outErrorDb("Table '%s' attempt loaded with invalid range (%d - %d), use (%d - %d) instead.",table,start_value,end_value,-1,std::numeric_limits<int32>::min());
7046        start_value = -1;
7047        end_value = std::numeric_limits<int32>::min();
7048    }
7049
7050    // for scripting localized strings allowed use _only_ negative entries
7051    return objmgr.LoadTrinityStrings(db,table,end_value,start_value);
7052}
Note: See TracBrowser for help on using the browser.