public/ITimerSystem.h
author David Anderson <dvander@alliedmods.net>
Wed Sep 26 14:36:08 2007 +0000 (2007-09-26)
changeset 1487 810b3fc562cb
parent 1462 c5feb60740cf
child 1947 e73a1e51833b
permissions -rw-r--r--
updated sdk for more interfaces
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_TIMER_SYSTEM_H_
    33 #define _INCLUDE_SOURCEMOD_TIMER_SYSTEM_H_
    34 
    35 /**
    36  * @file ITimerSystem.h
    37  * @brief Contains functions for creating and managing timers.
    38  */
    39 
    40 
    41 #include <IShareSys.h>
    42 #include <IForwardSys.h>
    43 
    44 #define SMINTERFACE_TIMERSYS_NAME		"ITimerSys"
    45 #define SMINTERFACE_TIMERSYS_VERSION	3
    46 
    47 namespace SourceMod
    48 {
    49 	class ITimer;
    50 
    51 	/**
    52 	 * @brief Interface for map timers.
    53 	 */
    54 	class IMapTimer
    55 	{
    56 	public:
    57 		/**
    58 		 * Returns the current map time limit in seconds.
    59 		 *
    60 		 * @return				Time limit, in seconds, or 
    61 		 *						0 if there is no limit.
    62 		 */
    63 		virtual int GetMapTimeLimit() =0;
    64 		
    65 		/**
    66 		 * Extends the map limit (either positively or negatively) in seconds.
    67 		 *
    68 		 * @param extra_time	Time to extend map by.  If 0, the map will 
    69 		 *						be set to have no time limit.
    70 		 */
    71 		virtual void ExtendMapTimeLimit(int extra_time) =0;
    72 
    73 		/**
    74 		 * Tells the map timer whether it is being used or not.
    75 		 *
    76 		 * Map timers are automatically enabled when they are set, and 
    77 		 * automatically disabled if being un-set.
    78 		 *
    79 		 * @param enabled		True if enabling, false if disabling.
    80 		 */
    81 		virtual void SetMapTimerStatus(bool enabled) =0;
    82 	};
    83 
    84 	/**
    85 	 * @brief Event callbacks for when a timer is executed.
    86 	 */
    87 	class ITimedEvent
    88 	{
    89 	public:
    90 		/**
    91 		 * @brief Called when a timer is executed.
    92 		 *
    93 		 * @param pTimer		Pointer to the timer instance.
    94 		 * @param pData			Private pointer passed from host.
    95 		 * @return				Pl_Stop to stop timer, Pl_Continue to continue.
    96 		 */
    97 		virtual ResultType OnTimer(ITimer *pTimer, void *pData) =0;
    98 
    99 		/**
   100 		 * @brief Called when the timer has been killed.
   101 		 *
   102 		 * @param pTimer		Pointer to the timer instance.
   103 		 * @param pData			Private data pointer passed from host.
   104 		 */
   105 		virtual void OnTimerEnd(ITimer *pTimer, void *pData) =0;
   106 	};
   107 
   108 	#define TIMER_FLAG_REPEAT			(1<<0)		/**< Timer will repeat until stopped */
   109 	#define TIMER_FLAG_NO_MAPCHANGE		(1<<1)		/**< Timer will not carry over mapchanges */
   110 
   111 	class ITimerSystem : public SMInterface
   112 	{
   113 	public:
   114 		const char *GetInterfaceName()
   115 		{
   116 			return SMINTERFACE_TIMERSYS_NAME;
   117 		}
   118 		unsigned int GetInterfaceVersion()
   119 		{
   120 			return SMINTERFACE_TIMERSYS_VERSION;
   121 		}
   122 	public:
   123 		/**
   124 		 * @brief Creates a timed event.
   125 		 *
   126 		 * @param pCallbacks		Pointer to ITimedEvent callbacks.
   127 		 * @param fInterval			Interval, in seconds, of the timed event to occur.
   128 		 *							The smallest allowed interval is 0.1 seconds.
   129 		 * @param pData				Private data to pass on to the timer.
   130 		 * @param flags				Extra flags to pass on to the timer.
   131 		 * @return					An ITimer pointer on success, NULL on 
   132 		 *							failure.
   133 		 */
   134 		virtual ITimer *CreateTimer(ITimedEvent *pCallbacks, 
   135 										 float fInterval, 
   136 										 void *pData,
   137 										 int flags) =0;
   138 
   139 		/**
   140 		 * @brief Kills a timer.
   141 		 *
   142 		 * @param pTimer			Pointer to the ITimer structure.
   143 		 * @return
   144 		 */
   145 		virtual void KillTimer(ITimer *pTimer) =0;
   146 
   147 		/**
   148 		 * @brief Arbitrarily fires a timer.  If the timer is not a repeating 
   149 		 * timer, this will also kill the timer.
   150 		 *
   151 		 * @param pTimer			Pointer to the ITimer structure.
   152 		 * @param delayExec			If true, and the timer is repeating, the
   153 		 *							next execution will be delayed by its 
   154 		 *							interval.
   155 		 * @return
   156 		 */
   157 		virtual void FireTimerOnce(ITimer *pTimer, bool delayExec=false) =0;
   158 
   159 		/**
   160 		 * @brief Sets the interface for dealing with map time limits.
   161 		 *
   162 		 * @param pMapTimer			Map timer interface pointer.
   163 		 * @return					Old pointer.
   164 		 */
   165 		virtual IMapTimer *SetMapTimer(IMapTimer *pTimer) =0;
   166 
   167 		/**
   168 		 * @brief Notification that the map's time left has changed 
   169 		 * via a change in the time limit or a change in the game rules (
   170 		 * such as mp_restartgame).
   171 		 */
   172 		virtual void MapTimeLeftChanged() =0;
   173 
   174 		/**
   175 		 * @brief Returns the current universal tick time.  This 
   176 		 * replacement for gpGlobals->curtime and engine->Time() correctly 
   177 		 * keeps track of ticks.
   178 		 *
   179 		 * During simulation, it is incremented by the difference between 
   180 		 * gpGlobals->curtime and the last simulated tick.  Otherwise, 
   181 		 * it is incremented by the interval per tick.
   182 		 *
   183 		 * It is not reset past map changes.
   184 		 *
   185 		 * @return					Universal ticked time.
   186 		 */
   187 		virtual float GetTickedTime() =0;
   188 
   189 		/**
   190 		 * @brief Notification that the "starting point" in the game has has 
   191 		 * changed.  This does not invoke MapTimeLeftChanged() automatically.
   192 		 *
   193 		 * @param offset			Optional offset to add to the new time.
   194 		 */
   195 		virtual void NotifyOfGameStart(float offset = 0.0f) =0;
   196 
   197 		/**
   198 		 * @brief Returns the time left in the map.
   199 		 *
   200 		 * @param pTime				Pointer to store time left, in seconds.
   201 		 * 							If there is no time limit, the number will 
   202 		 * 							be below 0.
   203 		 * @return					True on success, false if no support.
   204 		 */
   205 		virtual bool GetMapTimeLeft(float *pTime) =0;
   206 	};
   207 }
   208 
   209 #endif //_INCLUDE_SOURCEMOD_TIMER_SYSTEM_H_