plugins/include/timers.inc
author Kyle Sanderson <kyle.leet@gmail.com>
Thu Jul 18 19:40:42 2013 -0400 (2013-07-18)
changeset 3953 ea7a34452243
parent 2639 021fbd7a8cf6
child 3967 ceafb87d38a7
permissions -rw-r--r--
Make compile.sh set working dir to own dir (bug 5710, r=psychonic).
     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)		/**< Deprecated define, replaced by below */
    43 #define TIMER_DATA_HNDL_CLOSE		(1<<9)		/**< Timer will automatically call CloseHandle() on its data when finished */
    44 
    45 /**
    46  * Any of the following prototypes will work for a timed function.
    47  */
    48 funcenum Timer
    49 {
    50 	/**
    51  	 * Called when the timer interval has elapsed.
    52  	 * 
    53  	 * @param timer			Handle to the timer object.
    54  	 * @param hndl			Handle passed to CreateTimer() when timer was created.
    55  	 * @return				Plugin_Stop to stop a repeating timer, any other value for
    56  	 *						default behavior.
    57  	 */
    58 	Action:public(Handle:timer, Handle:hndl),
    59 	
    60 	/**
    61  	 * Called when the timer interval has elapsed.
    62  	 * 
    63  	 * @param timer			Handle to the timer object.
    64  	 * @param data			Data passed to CreateTimer() when timer was created.
    65  	 * @return				Plugin_Stop to stop a repeating timer, any other value for
    66  	 *						default behavior.
    67  	 */
    68 	Action:public(Handle:timer, any:data),
    69 	
    70 	/**
    71  	 * Called when the timer interval has elapsed.
    72  	 * 
    73  	 * @param timer			Handle to the timer object.
    74  	 * @return				Plugin_Stop to stop a repeating timer, any other value for
    75  	 *						default behavior.
    76  	 */
    77 	Action:public(Handle:timer),
    78 };
    79 
    80 /**
    81  * Creates a basic timer.  Calling CloseHandle() on a timer will end the timer.
    82  *
    83  * @param interval			Interval from the current game time to execute the given function.
    84  * @param func				Function to execute once the given interval has elapsed.
    85  * @param data				Handle or value to pass through to the timer callback function.
    86  * @param flags				Flags to set (such as repeatability or auto-Handle closing).
    87  * @return					Handle to the timer object.  You do not need to call CloseHandle().
    88  *							If the timer could not be created, INVALID_HANDLE will be returned.
    89  */
    90 native Handle:CreateTimer(Float:interval, Timer:func, any:data=INVALID_HANDLE, flags=0);
    91 
    92 /**
    93  * Kills a timer.  Use this instead of CloseHandle() if you need more options.
    94  *
    95  * @param autoClose			If autoClose is true, the data that was passed to CreateTimer() will
    96  *							be closed as a handle if TIMER_DATA_HNDL_CLOSE was not specified.
    97  * @noreturn
    98  */
    99 native KillTimer(Handle:timer, bool:autoClose=false);
   100 
   101 /**
   102  * Manually triggers a timer so its function will be called.
   103  *
   104  * @param timer				Timer Handle to trigger.
   105  * @param reset				If reset is true, the elapsed time counter is reset
   106  *							so the full interval must pass again.
   107  * @noreturn
   108  */
   109 native TriggerTimer(Handle:timer, bool:reset=false);
   110 
   111 /**
   112  * Returns the simulated game time.  
   113  *
   114  * This time is internally maintained by SourceMod and is based on the game 
   115  * tick count and tick rate.  Unlike GetGameTime(), it will increment past 
   116  * map changes and while no players are connected.  Unlike GetEngineTime(), 
   117  * it will not increment based on the system clock (i.e. it is still bound 
   118  * to the ticking process).
   119  *
   120  * @return					Time based on the game tick count.
   121  */
   122 native Float:GetTickedTime();
   123 
   124 /**
   125  * Returns an estimate of the time left before the map ends.  If the server 
   126  * has not processed any frames yet (i.e. no players have joined the map yet), 
   127  * then the time left returned will always be infinite.
   128  *
   129  * @param timeleft			Variable to store the time, in seconds.  If the 
   130  *							value is less than 0, the time limit is infinite.
   131  * @return					True if the operation is supported, false otherwise.
   132  */
   133 native bool:GetMapTimeLeft(&timeleft);
   134 
   135 /**
   136  * Retrieves the current map time limit.  If the server has not processed any 
   137  * frames yet (i.e. no players have joined the map yet), then the time limit 
   138  * returned will always be 0.
   139  *
   140  * @param time				Set to the number of total seconds in the map time 
   141  *							limit, or 0 if there is no time limit set.
   142  * @return					True on success, false if operation is not supported.
   143  */
   144 native bool:GetMapTimeLimit(&time);
   145 
   146 /**
   147  * Extends the map time limit in a way that will notify all plugins.
   148  *
   149  * @param time				Number of seconds to extend map time limit by.
   150  *							The number can be negative to decrease the time limit.
   151  *							If 0, the map will be set to have no time limit.
   152  * @return					True on success, false if operation is not supported.
   153  */
   154 native bool:ExtendMapTimeLimit(time);
   155 
   156 /**
   157  * Returns the number of seconds in between game server ticks.
   158  *
   159  * Note: A tick, in this context, is a frame.
   160  *
   161  * @return					Number of seconds in between ticks.
   162  */
   163 native Float:GetTickInterval();
   164 
   165 /**
   166  * Notification that the map's time left has changed via a change in the time 
   167  * limit or a change in the game rules (such as mp_restartgame).  This is useful 
   168  * for plugins trying to create timers based on the time left in the map.
   169  *
   170  * Calling ExtendMapTimeLimit() from here, without proper precaution, will 
   171  * cause infinite recursion.
   172  *
   173  * If the operation is not supported, this will never be called.
   174 
   175  * If the server has not yet processed any frames (i.e. no players have joined 
   176  * the map yet), then this will be called once the server begins ticking, even 
   177  * if there is no time limit set.
   178  */
   179 forward OnMapTimeLeftChanged();
   180 
   181 /**
   182  * Returns whether or not the server is processing frames or not.  
   183  *
   184  * The server does not process frames until at least one client joins the game.  
   185  * Once the first player has in, even if that player, leaves, the server's 
   186  * timers and entities will work.
   187  *
   188  * @return					True if the server is ticking, false otherwise.
   189  */
   190 native bool:IsServerProcessing();
   191 
   192 /**
   193  * Creates a timer associated with a new datapack, and returns the datapack.
   194  * @note The datapack is automatically freed when the timer ends.
   195  * @note The position of the datapack is not reset or changed for the timer function.
   196  *
   197  * @param interval			Interval from the current game time to execute the given function.
   198  * @param func				Function to execute once the given interval has elapsed.
   199  * @param datapack			The newly created datapack is passed though this by-reference 
   200  *							parameter to the timer callback function.
   201  * @param flags				Timer flags.
   202  * @return					Handle to the timer object.  You do not need to call CloseHandle().
   203  */
   204 stock Handle:CreateDataTimer(Float:interval, Timer:func, &Handle:datapack, flags=0)
   205 {
   206 	datapack = CreateDataPack();
   207 	flags |= TIMER_DATA_HNDL_CLOSE;
   208 	return CreateTimer(interval, func, datapack, flags);
   209 }
   210