Futher pet spell work
[getmangos.git] / src / mangosd / WorldRunnable.cpp
blobe812e23ec996009e15c6962a61ce8bef5f480f76
1 /*
2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /** \file
20 \ingroup mangosd
23 #include "WorldSocketMgr.h"
24 #include "Common.h"
25 #include "World.h"
26 #include "WorldRunnable.h"
27 #include "Timer.h"
28 #include "ObjectAccessor.h"
29 #include "MapManager.h"
31 #include "Database/DatabaseEnv.h"
33 #ifdef WIN32
34 #define WORLD_SLEEP_CONST 50
35 #else
36 #define WORLD_SLEEP_CONST 100 //Is this still needed?? [On linux some time ago not working 50ms]
37 #endif
39 /// Heartbeat for the World
40 void WorldRunnable::run()
42 ///- Init new SQL thread for the world database
43 WorldDatabase.ThreadStart(); // let thread do safe mySQL requests (one connection call enough)
44 sWorld.InitResultQueue();
46 uint32 realCurrTime = 0;
47 uint32 realPrevTime = getMSTime();
49 uint32 prevSleepTime = 0; // used for balanced full tick time length near WORLD_SLEEP_CONST
51 ///- While we have not World::m_stopEvent, update the world
52 while (!World::m_stopEvent)
54 ++World::m_worldLoopCounter;
55 realCurrTime = getMSTime();
57 uint32 diff = getMSTimeDiff(realPrevTime,realCurrTime);
59 sWorld.Update( diff );
60 realPrevTime = realCurrTime;
62 // diff (D0) include time of previous sleep (d0) + tick time (t0)
63 // we want that next d1 + t1 == WORLD_SLEEP_CONST
64 // we can't know next t1 and then can use (t0 + d1) == WORLD_SLEEP_CONST requirement
65 // d1 = WORLD_SLEEP_CONST - t0 = WORLD_SLEEP_CONST - (D0 - d0) = WORLD_SLEEP_CONST + d0 - D0
66 if (diff <= WORLD_SLEEP_CONST+prevSleepTime)
68 prevSleepTime = WORLD_SLEEP_CONST+prevSleepTime-diff;
69 ZThread::Thread::sleep(prevSleepTime);
71 else
72 prevSleepTime = 0;
75 sWorld.KickAllQueued(); // kick all queued players (and prevent its login at kick in game players)
76 sWorld.KickAll(); // save and kick all players
77 sWorld.UpdateSessions( 1 ); // real players unload required UpdateSessions call
79 sWorldSocketMgr->StopNetwork();
81 MapManager::Instance().UnloadAll(); // unload all grids (including locked in memory)
83 ///- End the database thread
84 WorldDatabase.ThreadEnd(); // free mySQL thread resources