1
0
Fork 0

Added the real tick duration to the OnWorldTick hook.

This commit is contained in:
madmaxoft 2013-11-30 14:22:26 +01:00
parent 463de118a0
commit 2383016b1d
8 changed files with 34 additions and 17 deletions

View File

@ -224,6 +224,22 @@ public:
return CallFunction(0);
}
/// Call any 3-param 0-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
Push(a_Arg3);
return CallFunction(0);
}
/// Call any 1-param 1-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename RetT1

View File

@ -94,7 +94,7 @@ public:
virtual bool OnUpdatingSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, cPlayer * a_Player) = 0;
virtual bool OnWeatherChanged (cWorld & a_World) = 0;
virtual bool OnWeatherChanging (cWorld & a_World, eWeather & a_NewWeather) = 0;
virtual bool OnWorldTick (cWorld & a_World, float a_Dt) = 0;
virtual bool OnWorldTick (cWorld & a_World, float a_Dt, int a_LastTickDurationMSec) = 0;
/** Handles the command split into a_Split, issued by player a_Player.
Command permissions have already been checked.

View File

@ -1143,13 +1143,13 @@ bool cPluginLua::OnWeatherChanging(cWorld & a_World, eWeather & a_NewWeather)
bool cPluginLua::OnWorldTick(cWorld & a_World, float a_Dt)
bool cPluginLua::OnWorldTick(cWorld & a_World, float a_Dt, int a_LastTickDurationMSec)
{
cCSLock Lock(m_CriticalSection);
cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_WORLD_TICK];
for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
{
m_LuaState.Call((int)(**itr), &a_World, a_Dt);
m_LuaState.Call((int)(**itr), &a_World, a_Dt, a_LastTickDurationMSec);
}
return false;
}

View File

@ -90,7 +90,7 @@ public:
virtual bool OnUpdatingSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, cPlayer * a_Player) override;
virtual bool OnWeatherChanged (cWorld & a_World) override;
virtual bool OnWeatherChanging (cWorld & a_World, eWeather & a_NewWeather) override;
virtual bool OnWorldTick (cWorld & a_World, float a_Dt) override;
virtual bool OnWorldTick (cWorld & a_World, float a_Dt, int a_LastTickDurationMSec) override;
virtual bool HandleCommand(const AStringVector & a_Split, cPlayer * a_Player) override;

View File

@ -1210,7 +1210,7 @@ bool cPluginManager::CallHookWeatherChanging(cWorld & a_World, eWeather & a_NewW
bool cPluginManager::CallHookWorldTick(cWorld & a_World, float a_Dt)
bool cPluginManager::CallHookWorldTick(cWorld & a_World, float a_Dt, int a_LastTickDurationMSec)
{
HookMap::iterator Plugins = m_Hooks.find(HOOK_WORLD_TICK);
if (Plugins == m_Hooks.end())
@ -1219,7 +1219,7 @@ bool cPluginManager::CallHookWorldTick(cWorld & a_World, float a_Dt)
}
for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr)
{
if ((*itr)->OnWorldTick(a_World, a_Dt))
if ((*itr)->OnWorldTick(a_World, a_Dt, a_LastTickDurationMSec))
{
return true;
}

View File

@ -191,7 +191,7 @@ public: // tolua_export
bool CallHookUpdatingSign (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, AString & a_Line1, AString & a_Line2, AString & a_Line3, AString & a_Line4, cPlayer * a_Player);
bool CallHookWeatherChanged (cWorld & a_World);
bool CallHookWeatherChanging (cWorld & a_World, eWeather & a_NewWeather);
bool CallHookWorldTick (cWorld & a_World, float a_Dt);
bool CallHookWorldTick (cWorld & a_World, float a_Dt, int a_LastTickDurationMSec);
bool DisablePlugin(const AString & a_PluginName); // tolua_export
bool LoadPlugin (const AString & a_PluginName); // tolua_export

View File

@ -197,20 +197,21 @@ void cWorld::cTickThread::Execute(void)
{
cTimer Timer;
long long msPerTick = 50;
long long LastTime = Timer.GetNowTime();
const Int64 msPerTick = 50;
Int64 LastTime = Timer.GetNowTime();
Int64 TickDuration = 50;
while (!m_ShouldTerminate)
{
long long NowTime = Timer.GetNowTime();
Int64 NowTime = Timer.GetNowTime();
float DeltaTime = (float)(NowTime - LastTime);
m_World.Tick(DeltaTime);
long long TickTime = Timer.GetNowTime() - NowTime;
m_World.Tick(DeltaTime, (int)TickDuration);
TickDuration = Timer.GetNowTime() - NowTime;
if (TickTime < msPerTick)
if (TickDuration < msPerTick)
{
// Stretch tick time until it's at least msPerTick
cSleep::MilliSleep((unsigned int)(msPerTick - TickTime));
cSleep::MilliSleep((unsigned int)(msPerTick - TickDuration));
}
LastTime = NowTime;
@ -660,10 +661,10 @@ void cWorld::Stop(void)
void cWorld::Tick(float a_Dt)
void cWorld::Tick(float a_Dt, int a_LastTickDurationMSec)
{
// Call the plugins
cPluginManager::Get()->CallHookWorldTick(*this, a_Dt);
cPluginManager::Get()->CallHookWorldTick(*this, a_Dt, a_LastTickDurationMSec);
// We need sub-tick precision here, that's why we store the time in seconds and calculate ticks off of it
m_WorldAgeSecs += (double)a_Dt / 1000.0;

View File

@ -729,7 +729,7 @@ private:
cWorld(const AString & a_WorldName);
~cWorld();
void Tick(float a_Dt);
void Tick(float a_Dt, int a_LastTickDurationMSec);
/// Handles the weather in each tick
void TickWeather(float a_Dt);