Added the real tick duration to the OnWorldTick hook.
This commit is contained in:
parent
463de118a0
commit
2383016b1d
@ -224,6 +224,22 @@ public:
|
|||||||
return CallFunction(0);
|
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:
|
/// Call any 1-param 1-return Lua function in a single line:
|
||||||
template<
|
template<
|
||||||
typename FnT, typename ArgT1, typename RetT1
|
typename FnT, typename ArgT1, typename RetT1
|
||||||
|
@ -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 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 OnWeatherChanged (cWorld & a_World) = 0;
|
||||||
virtual bool OnWeatherChanging (cWorld & a_World, eWeather & a_NewWeather) = 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.
|
/** Handles the command split into a_Split, issued by player a_Player.
|
||||||
Command permissions have already been checked.
|
Command permissions have already been checked.
|
||||||
|
@ -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);
|
cCSLock Lock(m_CriticalSection);
|
||||||
cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_WORLD_TICK];
|
cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_WORLD_TICK];
|
||||||
for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -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 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 OnWeatherChanged (cWorld & a_World) override;
|
||||||
virtual bool OnWeatherChanging (cWorld & a_World, eWeather & a_NewWeather) 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;
|
virtual bool HandleCommand(const AStringVector & a_Split, cPlayer * a_Player) override;
|
||||||
|
|
||||||
|
@ -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);
|
HookMap::iterator Plugins = m_Hooks.find(HOOK_WORLD_TICK);
|
||||||
if (Plugins == m_Hooks.end())
|
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)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -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 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 CallHookWeatherChanged (cWorld & a_World);
|
||||||
bool CallHookWeatherChanging (cWorld & a_World, eWeather & a_NewWeather);
|
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 DisablePlugin(const AString & a_PluginName); // tolua_export
|
||||||
bool LoadPlugin (const AString & a_PluginName); // tolua_export
|
bool LoadPlugin (const AString & a_PluginName); // tolua_export
|
||||||
|
@ -197,20 +197,21 @@ void cWorld::cTickThread::Execute(void)
|
|||||||
{
|
{
|
||||||
cTimer Timer;
|
cTimer Timer;
|
||||||
|
|
||||||
long long msPerTick = 50;
|
const Int64 msPerTick = 50;
|
||||||
long long LastTime = Timer.GetNowTime();
|
Int64 LastTime = Timer.GetNowTime();
|
||||||
|
|
||||||
|
Int64 TickDuration = 50;
|
||||||
while (!m_ShouldTerminate)
|
while (!m_ShouldTerminate)
|
||||||
{
|
{
|
||||||
long long NowTime = Timer.GetNowTime();
|
Int64 NowTime = Timer.GetNowTime();
|
||||||
float DeltaTime = (float)(NowTime - LastTime);
|
float DeltaTime = (float)(NowTime - LastTime);
|
||||||
m_World.Tick(DeltaTime);
|
m_World.Tick(DeltaTime, (int)TickDuration);
|
||||||
long long TickTime = Timer.GetNowTime() - NowTime;
|
TickDuration = Timer.GetNowTime() - NowTime;
|
||||||
|
|
||||||
if (TickTime < msPerTick)
|
if (TickDuration < msPerTick)
|
||||||
{
|
{
|
||||||
// Stretch tick time until it's at least msPerTick
|
// Stretch tick time until it's at least msPerTick
|
||||||
cSleep::MilliSleep((unsigned int)(msPerTick - TickTime));
|
cSleep::MilliSleep((unsigned int)(msPerTick - TickDuration));
|
||||||
}
|
}
|
||||||
|
|
||||||
LastTime = NowTime;
|
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
|
// 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
|
// 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;
|
m_WorldAgeSecs += (double)a_Dt / 1000.0;
|
||||||
|
@ -729,7 +729,7 @@ private:
|
|||||||
cWorld(const AString & a_WorldName);
|
cWorld(const AString & a_WorldName);
|
||||||
~cWorld();
|
~cWorld();
|
||||||
|
|
||||||
void Tick(float a_Dt);
|
void Tick(float a_Dt, int a_LastTickDurationMSec);
|
||||||
|
|
||||||
/// Handles the weather in each tick
|
/// Handles the weather in each tick
|
||||||
void TickWeather(float a_Dt);
|
void TickWeather(float a_Dt);
|
||||||
|
Loading…
Reference in New Issue
Block a user