root/trunk/src/trinitycore/WorldRunnable.cpp @ 2

Revision 2, 3.0 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19/** \file
20    \ingroup mangosd
21*/
22
23#include "Common.h"
24#include "World.h"
25#include "WorldRunnable.h"
26#include "Timer.h"
27#include "ObjectAccessor.h"
28#include "MapManager.h"
29
30#include "Database/DatabaseEnv.h"
31
32#ifdef WIN32
33#define WORLD_SLEEP_CONST 50
34#else
35#define WORLD_SLEEP_CONST 100                               //Is this still needed?? [On linux some time ago not working 50ms]
36#endif
37
38/// Heartbeat for the World
39void WorldRunnable::run()
40{
41    ///- Init new SQL thread for the world database
42    WorldDatabase.ThreadStart();                                // let thread do safe mySQL requests (one connection call enough)
43    sWorld.InitResultQueue();
44
45    uint32 realCurrTime = 0;
46    uint32 realPrevTime = getMSTime();
47
48    uint32 prevSleepTime = 0;                               // used for balanced full tick time length near WORLD_SLEEP_CONST
49
50    ///- While we have not World::m_stopEvent, update the world
51    while (!World::m_stopEvent)
52    {
53        ++World::m_worldLoopCounter;
54        realCurrTime = getMSTime();
55
56        uint32 diff = getMSTimeDiff(realPrevTime,realCurrTime);
57
58        sWorld.Update( diff );
59        realPrevTime = realCurrTime;
60
61        // diff (D0) include time of previous sleep (d0) + tick time (t0)
62        // we want that next d1 + t1 == WORLD_SLEEP_CONST
63        // we can't know next t1 and then can use (t0 + d1) == WORLD_SLEEP_CONST requirement
64        // d1 = WORLD_SLEEP_CONST - t0 = WORLD_SLEEP_CONST - (D0 - d0) = WORLD_SLEEP_CONST + d0 - D0
65        if (diff <= WORLD_SLEEP_CONST+prevSleepTime)
66        {
67            prevSleepTime = WORLD_SLEEP_CONST+prevSleepTime-diff;
68            ZThread::Thread::sleep(prevSleepTime);
69        }
70        else
71            prevSleepTime = 0;
72    }
73
74    sWorld.KickAllQueued();                                 // kick all queued players (and prevent its login at kick in game players)
75    sWorld.KickAll();                                       // save and kick all players
76    sWorld.UpdateSessions( 1 );                             // real players unload required UpdateSessions call
77
78    MapManager::Instance().UnloadAll();                     // unload all grids (including locked in memory)
79
80    ///- End the database thread
81    WorldDatabase.ThreadEnd();                                  // free mySQL thread resources
82}
Note: See TracBrowser for help on using the browser.