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