Files
TCPlayerbotCore/src/trinitycore/WorldRunnable.cpp
T

102 lines
3.4 KiB
C++
Raw Normal View History

2009-04-06 21:14:51 +02:00
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
2010-01-16 20:19:18 +03:00
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008-10-02 16:23:55 -05:00
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/** \file
2008-10-14 11:57:03 -05:00
\ingroup Trinityd
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "Common.h"
#include "ObjectAccessor.h"
2009-06-04 21:41:07 +02:00
#include "World.h"
#include "WorldSocketMgr.h"
2008-10-02 16:23:55 -05:00
#include "Database/DatabaseEnv.h"
2009-10-17 15:51:44 -07:00
2009-06-04 21:41:07 +02:00
#include "BattleGroundMgr.h"
#include "MapManager.h"
#include "Timer.h"
2009-06-04 21:41:07 +02:00
#include "WorldRunnable.h"
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#define WORLD_SLEEP_CONST 50
2009-10-17 15:51:44 -07:00
2009-03-20 22:17:39 +01:00
#ifdef WIN32
#include "ServiceWin32.h"
extern int m_ServiceStatus;
2008-10-02 16:23:55 -05:00
#endif
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/// Heartbeat for the World
void WorldRunnable::run()
{
///- Init new SQL thread for the world database
WorldDatabase.ThreadStart(); // let thread do safe mySQL requests (one connection call enough)
2009-03-20 22:17:39 +01:00
CharacterDatabase.ThreadStart();
loginDatabase.ThreadStart();
2008-10-02 16:23:55 -05:00
sWorld.InitResultQueue();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
uint32 realCurrTime = 0;
uint32 realPrevTime = getMSTime();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
uint32 prevSleepTime = 0; // used for balanced full tick time length near WORLD_SLEEP_CONST
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
///- While we have not World::m_stopEvent, update the world
while (!World::IsStopped())
2008-10-02 16:23:55 -05:00
{
++World::m_worldLoopCounter;
realCurrTime = getMSTime();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
uint32 diff = getMSTimeDiff(realPrevTime,realCurrTime);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
sWorld.Update( diff );
realPrevTime = realCurrTime;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// diff (D0) include time of previous sleep (d0) + tick time (t0)
// we want that next d1 + t1 == WORLD_SLEEP_CONST
// we can't know next t1 and then can use (t0 + d1) == WORLD_SLEEP_CONST requirement
// d1 = WORLD_SLEEP_CONST - t0 = WORLD_SLEEP_CONST - (D0 - d0) = WORLD_SLEEP_CONST + d0 - D0
if (diff <= WORLD_SLEEP_CONST+prevSleepTime)
{
prevSleepTime = WORLD_SLEEP_CONST+prevSleepTime-diff;
ACE_Based::Thread::Sleep(prevSleepTime);
2008-10-02 16:23:55 -05:00
}
else
prevSleepTime = 0;
2009-10-17 15:51:44 -07:00
2009-03-20 22:17:39 +01:00
#ifdef WIN32
if (m_ServiceStatus == 0) World::StopNow(SHUTDOWN_EXIT_CODE);
while (m_ServiceStatus == 2) Sleep(1000);
#endif
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
sWorld.KickAll(); // save and kick all players
sWorld.UpdateSessions( 1 ); // real players unload required UpdateSessions call
2009-10-17 15:51:44 -07:00
2009-03-20 22:17:39 +01:00
// unload battleground templates before different singletons destroyed
sBattleGroundMgr.DeleteAllBattleGrounds();
2009-10-17 15:51:44 -07:00
sWorldSocketMgr->StopNetwork();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
MapManager::Instance().UnloadAll(); // unload all grids (including locked in memory)
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
///- End the database thread
WorldDatabase.ThreadEnd(); // free mySQL thread resources
2009-11-23 19:20:36 +01:00
CharacterDatabase.ThreadEnd();
loginDatabase.ThreadEnd();
2008-10-02 16:23:55 -05:00
}