plugins/include/timers.inc
author David Anderson <dvander@alliedmods.net>
Wed Sep 12 22:46:00 2007 +0000 (2007-09-12)
changeset 1412 a2ca3d22dd71
parent 1409 84e9d3494603
child 1415 edf4efe5707c
permissions -rw-r--r--
- moved gameframe hook to timersys
- rewrote simulation algorithm
- various internal improvements to timer
- removed the unused parameter to GetTickedTime()
faluco@612
     1
/**
faluco@612
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
faluco@612
     4
 * SourceMod (C)2004-2007 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
/**
faluco@612
   124
 * Creates a timer associated with a new data pack, and returns the datapack.
faluco@612
   125
 * @note The datapack is automatically freed when the timer ends.
faluco@612
   126
 * @note The position of the datapack is not reset or changed for the timer function.
faluco@612
   127
 *
faluco@612
   128
 * @param interval			Interval from the current game time to execute the given function.
faluco@612
   129
 * @param func				Function to execute once the given interval has elapsed.
faluco@612
   130
 * @param data				The newly created datapack is passed though this by-reference parameter.
faluco@612
   131
 * @param flags				Timer flags.
faluco@612
   132
 * @return					Handle to the timer object.  You do not need to call CloseHandle().
faluco@612
   133
 */
dvander@716
   134
stock Handle:CreateDataTimer(Float:interval, Timer:func, &Handle:data, flags=0)
faluco@612
   135
{
faluco@612
   136
	data = CreateDataPack();
faluco@612
   137
	flags |= TIMER_HNDL_CLOSE;
faluco@612
   138
	return CreateTimer(interval, func, data, flags);
faluco@612
   139
}