plugins/include/timers.inc
author Matt Woodrow <pred@alliedmods.net>
Wed Feb 18 15:03:17 2009 +1300 (2009-02-18)
changeset 2577 2c03ca54c114
parent 2289 44045ead61cd
child 2639 021fbd7a8cf6
permissions -rw-r--r--
Fixed MsgSentNotify callbacks not firing on intercept hook (bug3631, r=dvander)
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #if defined _timers_included
    34  #endinput
    35 #endif
    36 #define _timers_included
    37 
    38 #include <datapack>
    39 
    40 #define TIMER_REPEAT			(1<<0)		/**< Timer will repeat until it returns Plugin_Stop */
    41 #define TIMER_FLAG_NO_MAPCHANGE	(1<<1)		/**< Timer will not carry over mapchanges */
    42 #define TIMER_HNDL_CLOSE		(1<<9)		/**< Timer will automatically call CloseHandle() on its value when finished */
    43 
    44 /**
    45  * Any of the following prototypes will work for a timed function.
    46  */
    47 funcenum Timer
    48 {
    49 	/**
    50  	 * Called when the timer interval has elapsed.
    51  	 * 
    52  	 * @param timer			Handle to the timer object.
    53  	 * @param hndl			Handle passed when the timer was created.
    54  	 * @return				Plugin_Stop to stop a repeating timer, any other value for
    55  	 *						default behavior.
    56  	 */
    57 	Action:public(Handle:timer, Handle:hndl),
    58 	
    59 	/**
    60  	 * Called when the timer interval has elapsed.
    61  	 * 
    62  	 * @param timer			Handle to the timer object.
    63  	 * @param value			Value passed when the timer was created.
    64  	 * @return				Plugin_Stop to stop a repeating timer, any other value for
    65  	 *						default behavior.
    66  	 */
    67 	Action:public(Handle:timer, any:value),
    68 	
    69 	/**
    70  	 * Called when the timer interval has elapsed.
    71  	 * 
    72  	 * @param timer			Handle to the timer object.
    73  	 * @return				Plugin_Stop to stop a repeating timer, any other value for
    74  	 *						default behavior.
    75  	 */
    76 	Action:public(Handle:timer),
    77 };
    78 
    79 /**
    80  * Creates a basic timer.  Calling CloseHandle() on a timer will end the timer.
    81  *
    82  * @param interval			Interval from the current game time to execute the given function.
    83  * @param func				Function to execute once the given interval has elapsed.
    84  * @param value				Handle or value to give to the timer function.
    85  * @param flags				Flags to set (such as repeatability or auto-Handle closing).
    86  * @return					Handle to the timer object.  You do not need to call CloseHandle().
    87  *							If the timer could not be created, INVALID_HANDLE will be returned.
    88  */
    89 native Handle:CreateTimer(Float:interval, Timer:func, any:value=INVALID_HANDLE, flags=0);
    90 
    91 /**
    92  * Kills a timer.  Use this instead of CloseHandle() if you need more options.
    93  *
    94  * @param autoClose			If autoClose is true, the timer's value will be 
    95  *							closed as a handle if TIMER_HNDL_CLOSE was not specified.
    96  * @noreturn
    97  */
    98 native KillTimer(Handle:timer, bool:autoClose=false);
    99 
   100 /**
   101  * Manually triggers a timer so its function will be called.
   102  *
   103  * @param timer				Timer Handle to trigger.
   104  * @param reset				If reset is true, the elapsed time counter is reset
   105  *							so the full interval must pass again.
   106  * @noreturn
   107  */
   108 native TriggerTimer(Handle:timer, bool:reset=false);
   109 
   110 /**
   111  * Returns the simulated game time.  
   112  *
   113  * This time is internally maintained by SourceMod and is based on the game 
   114  * tick count and tick rate.  Unlike GetGameTime(), it will increment past 
   115  * map changes and while no players are connected.  Unlike GetEngineTime(), 
   116  * it will not increment based on the system clock (i.e. it is still bound 
   117  * to the ticking process).
   118  *
   119  * @return					Time based on the game tick count.
   120  */
   121 native Float:GetTickedTime();
   122 
   123 /**
   124  * Returns an estimate of the time left before the map ends.  If the server 
   125  * has not processed any frames yet (i.e. no players have joined the map yet), 
   126  * then the time left returned will always be infinite.
   127  *
   128  * @param timeleft			Variable to store the time, in seconds.  If the 
   129  *							value is less than 0, the time limit is infinite.
   130  * @return					True if the operation is supported, false otherwise.
   131  */
   132 native bool:GetMapTimeLeft(&timeleft);
   133 
   134 /**
   135  * Retrieves the current map time limit.  If the server has not processed any 
   136  * frames yet (i.e. no players have joined the map yet), then the time limit 
   137  * returned will always be 0.
   138  *
   139  * @param time				Set to the number of total seconds in the map time 
   140  *							limit, or 0 if there is no time limit set.
   141  * @return					True on success, false if operation is not supported.
   142  */
   143 native bool:GetMapTimeLimit(&time);
   144 
   145 /**
   146  * Extends the map time limit in a way that will notify all plugins.
   147  *
   148  * @param time				Number of seconds to extend map time limit by.
   149  *							The number can be negative to decrease the time limit.
   150  *							If 0, the map will be set to have no time limit.
   151  * @return					True on success, false if operation is not supported.
   152  */
   153 native bool:ExtendMapTimeLimit(time);
   154 
   155 /**
   156  * Returns the number of seconds in between game server ticks.
   157  *
   158  * Note: A tick, in this context, is a frame.
   159  *
   160  * @return					Number of seconds in between ticks.
   161  */
   162 native Float:GetTickInterval();
   163 
   164 /**
   165  * Notification that the map's time left has changed via a change in the time 
   166  * limit or a change in the game rules (such as mp_restartgame).  This is useful 
   167  * for plugins trying to create timers based on the time left in the map.
   168  *
   169  * Calling ExtendMapTimeLimit() from here, without proper precaution, will 
   170  * cause infinite recursion.
   171  *
   172  * If the operation is not supported, this will never be called.
   173 
   174  * If the server has not yet processed any frames (i.e. no players have joined 
   175  * the map yet), then this will be called once the server begins ticking, even 
   176  * if there is no time limit set.
   177  */
   178 forward OnMapTimeLeftChanged();
   179 
   180 /**
   181  * Returns whether or not the server is processing frames or not.  
   182  *
   183  * The server does not process frames until at least one client joins the game.  
   184  * Once the first player has in, even if that player, leaves, the server's 
   185  * timers and entities will work.
   186  *
   187  * @return					True if the server is ticking, false otherwise.
   188  */
   189 native bool:IsServerProcessing();
   190 
   191 /**
   192  * Creates a timer associated with a new data pack, and returns the datapack.
   193  * @note The datapack is automatically freed when the timer ends.
   194  * @note The position of the datapack is not reset or changed for the timer function.
   195  *
   196  * @param interval			Interval from the current game time to execute the given function.
   197  * @param func				Function to execute once the given interval has elapsed.
   198  * @param data				The newly created datapack is passed though this by-reference parameter.
   199  * @param flags				Timer flags.
   200  * @return					Handle to the timer object.  You do not need to call CloseHandle().
   201  */
   202 stock Handle:CreateDataTimer(Float:interval, Timer:func, &Handle:data, flags=0)
   203 {
   204 	data = CreateDataPack();
   205 	flags |= TIMER_HNDL_CLOSE;
   206 	return CreateTimer(interval, func, data, flags);
   207 }
   208