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