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