From 8bbbb57d8c9b82db6c9abbfd8f520be312de23e1 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 23 Sep 2007 20:11:48 +0000 Subject: [PATCH] clarified and fixed the meaning of timelimit/timeleft when no frames have been processed --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401468 --- core/TimerSys.cpp | 21 ++++++++++++++++++--- core/TimerSys.h | 1 + core/smn_timers.cpp | 17 ++++++----------- plugins/include/timers.inc | 15 ++++++++++++--- public/ITimerSystem.h | 5 +++-- 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/core/TimerSys.cpp b/core/TimerSys.cpp index f5cc8bcd..54e0054c 100644 --- a/core/TimerSys.cpp +++ b/core/TimerSys.cpp @@ -152,6 +152,7 @@ TimerSystem::TimerSystem() { m_pMapTimer = NULL; m_bHasMapTickedYet = false; + m_bHasMapSimulatedYet = false; m_fLastTickedTime = 0.0f; m_LastExecTime = 0.0f; } @@ -198,6 +199,7 @@ void TimerSystem::OnSourceModLevelChange(const char *mapName) void TimerSystem::OnSourceModLevelEnd() { m_bHasMapTickedYet = false; + m_bHasMapSimulatedYet = false; } void TimerSystem::GameFrame(bool simulating) @@ -205,6 +207,11 @@ void TimerSystem::GameFrame(bool simulating) if (simulating && m_bHasMapTickedYet) { g_fUniversalTime += gpGlobals->curtime - m_fLastTickedTime; + if (!m_bHasMapSimulatedYet) + { + m_bHasMapSimulatedYet = true; + MapTimeLeftChanged(); + } } else { @@ -450,13 +457,21 @@ float TimerSystem::GetTickedTime() bool TimerSystem::GetMapTimeLeft(float *time_left) { - int time_limit; - if (!m_pMapTimer || (time_limit = m_pMapTimer->GetMapTimeLimit()) < 1) + if (!m_pMapTimer) { return false; } - *time_left = (g_fGameStartTime + time_limit * 60.0f) - gpGlobals->curtime; + int time_limit; + if (!m_bHasMapSimulatedYet || (time_limit = m_pMapTimer->GetMapTimeLimit()) < 1) + { + *time_left = -1.0f; + } + else + { + *time_left = (g_fGameStartTime + time_limit * 60.0f) - gpGlobals->curtime; + } return true; } + diff --git a/core/TimerSys.h b/core/TimerSys.h index bc80c17e..98f9a6c0 100644 --- a/core/TimerSys.h +++ b/core/TimerSys.h @@ -93,6 +93,7 @@ private: /* This is stuff for our manual ticking escapades. */ bool m_bHasMapTickedYet; /** Has the map ticked yet? */ + bool m_bHasMapSimulatedYet; /** Has the map simulated yet? */ float m_fLastTickedTime; /** Last time that the game currently gave us while ticking. */ diff --git a/core/smn_timers.cpp b/core/smn_timers.cpp index 0ea57eb5..bd7b54fa 100644 --- a/core/smn_timers.cpp +++ b/core/smn_timers.cpp @@ -260,23 +260,17 @@ static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params) static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params) { - cell_t *addr; - pContext->LocalToPhysAddr(params[1], &addr); - float time_left; - int int_time; if (!g_Timers.GetMapTimeLeft(&time_left)) { - int_time = -1; - } - else - { - int_time = (int)time_left; + return 0; } - *addr = int_time; + cell_t *addr; + pContext->LocalToPhysAddr(params[1], &addr); + *addr = (int)time_left; - return true; + return 1; } static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params) @@ -327,3 +321,4 @@ REGISTER_NATIVES(timernatives) {"IsServerProcessing", smn_IsServerProcessing}, {NULL, NULL} }; + diff --git a/plugins/include/timers.inc b/plugins/include/timers.inc index 81ca5fb9..f7b5d402 100644 --- a/plugins/include/timers.inc +++ b/plugins/include/timers.inc @@ -121,7 +121,9 @@ native TriggerTimer(Handle:timer, bool:reset=false); native Float:GetTickedTime(); /** - * Returns an estimate of the time left before the map ends. + * Returns an estimate of the time left before the map ends. If the server + * has not processed any frames yet (i.e. no players have joined the map yet), + * then the time left returned will always be infinite. * * @param timeleft Variable to store the time, in seconds. If the * value is less than 0, the time limit is infinite. @@ -130,7 +132,9 @@ native Float:GetTickedTime(); native bool:GetMapTimeLeft(&timeleft); /** - * Retrieves the current map time limit. + * Retrieves the current map time limit. If the server has not processed any + * frames yet (i.e. no players have joined the map yet), then the time limit + * returned will always be 0. * * @param time Set to the number of total seconds in the map time * limit, or 0 if there is no time limit set. @@ -149,7 +153,7 @@ native bool:GetMapTimeLimit(&time); native bool:ExtendMapTimeLimit(time); /** - * Notification that the map's time left has changed via a change in the time + * Notification that the map's time left has changed via a change in the time * limit or a change in the game rules (such as mp_restartgame). This is useful * for plugins trying to create timers based on the time left in the map. * @@ -157,6 +161,10 @@ native bool:ExtendMapTimeLimit(time); * cause infinite recursion. * * If the operation is not supported, this will never be called. + + * If the server has not yet processed any frames (i.e. no players have joined + * the map yet), then this will be called once the server begins ticking, even + * if there is no time limit set. */ forward OnMapTimeLeftChanged(); @@ -188,3 +196,4 @@ stock Handle:CreateDataTimer(Float:interval, Timer:func, &Handle:data, flags=0) flags |= TIMER_HNDL_CLOSE; return CreateTimer(interval, func, data, flags); } + diff --git a/public/ITimerSystem.h b/public/ITimerSystem.h index 00fa57bb..41119b7a 100644 --- a/public/ITimerSystem.h +++ b/public/ITimerSystem.h @@ -198,8 +198,9 @@ namespace SourceMod * @brief Returns the time left in the map. * * @param pTime Pointer to store time left, in seconds. - * @return True on success, false if there no time limit - * or if the time limit could not be determined. + * If there is no time limit, the number will + * be below 0. + * @return True on success, false if no support. */ virtual bool GetMapTimeLeft(float *pTime) =0; };