From c531ad85641cd1c5834b466b17213939ee913e41 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 13 Sep 2007 01:14:02 +0000 Subject: [PATCH] finalized (i hope) new map timer api --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401421 --- core/TimerSys.cpp | 19 +++++++++---- core/TimerSys.h | 3 +- core/smn_timers.cpp | 57 ++++++++++++++++++++++++++++++++++++++ plugins/include/timers.inc | 40 ++++++++++++++++++++++++++ public/ITimerSystem.h | 27 ++++++++++++------ 5 files changed, 131 insertions(+), 15 deletions(-) diff --git a/core/TimerSys.cpp b/core/TimerSys.cpp index 4a7e15fc..05c00274 100644 --- a/core/TimerSys.cpp +++ b/core/TimerSys.cpp @@ -120,7 +120,12 @@ public: return; } - g_Timers.MapTimeLimitExtended(atoi(old_value) * 60, pVar->GetInt() * 60); + if (atoi(old_value) == pVar->GetInt()) + { + return; + } + + g_Timers.MapTimeLeftChanged(); } private: @@ -436,10 +441,12 @@ IMapTimer *TimerSystem::GetMapTimer() return m_pMapTimer; } -void TimerSystem::MapTimeLimitExtended(int old_limit, int new_limit) +void TimerSystem::MapTimeLeftChanged() { - if (old_limit == new_limit) - { - return; - } + +} + +float TimerSystem::GetTickedTime() +{ + return g_fUniversalTime; } diff --git a/core/TimerSys.h b/core/TimerSys.h index 8fca724c..84a3ede8 100644 --- a/core/TimerSys.h +++ b/core/TimerSys.h @@ -74,8 +74,9 @@ public: //ITimerSystem ITimer *CreateTimer(ITimedEvent *pCallbacks, float fInterval, void *pData, int flags); void KillTimer(ITimer *pTimer); void FireTimerOnce(ITimer *pTimer, bool delayExec=false); - void MapTimeLimitExtended(int old_limit, int new_limit); + void MapTimeLeftChanged(); IMapTimer *SetMapTimer(IMapTimer *pTimer); + float GetTickedTime(); public: void RunFrame(); void MapChange(bool real_mapchange); diff --git a/core/smn_timers.cpp b/core/smn_timers.cpp index 27f2f1c9..98c367f6 100644 --- a/core/smn_timers.cpp +++ b/core/smn_timers.cpp @@ -258,11 +258,68 @@ static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params) return sp_ftoc(*g_pUniversalTime); } +static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params) +{ + IMapTimer *pMapTimer = g_Timers.GetMapTimer(); + + if (!pMapTimer) + { + return 0; + } + + cell_t *addr; + pContext->LocalToPhysAddr(params[1], &addr); + + int time_left; + if (!pMapTimer->GetMapTimeLeft(&time_left)) + { + time_left = -1; + } + + *addr = time_left; + + return true; +} + +static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params) +{ + IMapTimer *pMapTimer = g_Timers.GetMapTimer(); + + if (!pMapTimer) + { + return 0; + } + + cell_t *addr; + pContext->LocalToPhysAddr(params[1], &addr); + + *addr = pMapTimer->GetMapTimeLimit(); + + return 1; +} + +static cell_t smn_ExtendMapTimeLimit(IPluginContext *pContext, const cell_t *params) +{ + IMapTimer *pMapTimer = g_Timers.GetMapTimer(); + + if (!pMapTimer) + { + return 0; + } + + pMapTimer->ExtendMapTimeLimit(params[1]); + + return 1; +} + REGISTER_NATIVES(timernatives) { {"CreateTimer", smn_CreateTimer}, {"KillTimer", smn_KillTimer}, {"TriggerTimer", smn_TriggerTimer}, {"GetTickedTime", smn_GetTickedTime}, + {"GetMapTimeLeft", smn_GetMapTimeLeft}, + {"GetMapTimeLimit", smn_GetMapTimeLimit}, + {"ExtendMapTimeLimit", smn_ExtendMapTimeLimit}, {NULL, NULL} }; diff --git a/plugins/include/timers.inc b/plugins/include/timers.inc index a49dea1d..c1758a0c 100644 --- a/plugins/include/timers.inc +++ b/plugins/include/timers.inc @@ -120,6 +120,46 @@ native TriggerTimer(Handle:timer, bool:reset=false); */ native Float:GetTickedTime(); +/** + * Returns an estimate of the time left before the map ends. + * + * @param timeleft Variable to store the time, in seconds. If the + * value is less than 0, the time limit is infinite. + * @return True if the operation is supported, false otherwise. + */ +native bool:GetMapTimeLeft(&timeleft); + +/** + * Retrieves the current map time limit. + * + * @param time Set to the number of total seconds in the map time + * limit, or 0 if there is no time limit set. + * @return True on success, false if operation is not supported. + */ +native bool:GetMapTimeLimit(&time); + +/** + * Extends the map time limit in a way that will notify all plugins. + * + * @param time Number of seconds to extend map time limit by. + * The number can be negative to decrease the time limit. + * If 0, the map will be set to have no time limit. + * @return True on success, false if operation is not supported. + */ +native bool:ExtendMapTimeLimit(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. + * + * Calling ExtendMapTimeLimit() from here, without proper precaution, will + * cause infinite recursion. + * + * If the operation is not supported, this will never be called. + */ +forward OnMapTimeLeftChanged(); + /** * Creates a timer associated with a new data pack, and returns the datapack. * @note The datapack is automatically freed when the timer ends. diff --git a/public/ITimerSystem.h b/public/ITimerSystem.h index dca2898b..28eae204 100644 --- a/public/ITimerSystem.h +++ b/public/ITimerSystem.h @@ -174,16 +174,27 @@ namespace SourceMod virtual IMapTimer *SetMapTimer(IMapTimer *pTimer) =0; /** - * @brief Notifies the timer that the map timelimit has been extended. - * - * A time limit of 0 implies that there is no limit. - * - * @param old_limit Old limit, in seconds. - * @param new_limit New limit, in seconds. + * @brief 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). */ - virtual void MapTimeLimitExtended(int old_limit, int new_limit) =0; + virtual void MapTimeLeftChanged() =0; + + /** + * @brief Returns the current universal tick time. This + * replacement for gpGlobals->curtime and engine->Time() correctly + * keeps track of ticks. + * + * During simulation, it is incremented by the difference between + * gpGlobals->curtime and the last simulated tick. Otherwise, + * it is incremented by the interval per tick. + * + * It is not reset past map changes. + * + * @return Universal ticked time. + */ + virtual float GetTickedTime() =0; }; } #endif //_INCLUDE_SOURCEMOD_TIMER_SYSTEM_H_ -