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