plugins/include/timers.inc
author David Anderson <dvander@alliedmods.net>
Sat Jul 07 17:51:29 2007 +0000 (2007-07-07)
changeset 1048 9f5605daf52d
parent 822 73f60bf0285d
child 1108 fafe809b16ca
permissions -rw-r--r--
changed SOUND_FROM_PLAYER from -1 to -2 to maintain compatibility with SOUND_FROM_LOCAL_PLAYER
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15 
    16 #if defined _timers_included
    17  #endinput
    18 #endif
    19 #define _timers_included
    20 
    21 #include <datapack>
    22 
    23 #define TIMER_REPEAT		(1<<0)		/**< Timer will repeat until it returns Plugin_Stop */
    24 #define TIMER_HNDL_CLOSE	(1<<9)		/**< Timer will automatically call CloseHandle() on its value when finished */
    25 
    26 /**
    27  * Any of the following prototypes will work for a timed function.
    28  */
    29 funcenum Timer
    30 {
    31 	/**
    32  	 * Called when the timer interval has elapsed.
    33  	 * 
    34  	 * @param timer			Handle to the timer object.
    35  	 * @param hndl			Handle passed when the timer was created.
    36  	 * @return				Plugin_Stop to stop a repeating timer, any other value for
    37  	 *						default behavior.
    38  	 */
    39 	Action:public(Handle:timer, Handle:hndl),
    40 	
    41 	/**
    42  	 * Called when the timer interval has elapsed.
    43  	 * 
    44  	 * @param timer			Handle to the timer object.
    45  	 * @param value			Value passed when the timer was created.
    46  	 * @return				Plugin_Stop to stop a repeating timer, any other value for
    47  	 *						default behavior.
    48  	 */
    49 	Action:public(Handle:timer, any:value),
    50 	
    51 	/**
    52  	 * Called when the timer interval has elapsed.
    53  	 * 
    54  	 * @param timer			Handle to the timer object.
    55  	 * @return				Plugin_Stop to stop a repeating timer, any other value for
    56  	 *						default behavior.
    57  	 */
    58 	Action:public(Handle:timer),
    59 };
    60 
    61 /**
    62  * Creates a basic timer.  Calling CloseHandle() on a timer will end the timer.
    63  *
    64  * @param interval			Interval from the current game time to execute the given function.
    65  * @param func				Function to execute once the given interval has elapsed.
    66  * @param value				Handle or value to give to the timer function.
    67  * @param flags				Flags to set (such as repeatability or auto-Handle closing).
    68  * @return					Handle to the timer object.  You do not need to call CloseHandle().
    69  */
    70 native Handle:CreateTimer(Float:interval, Timer:func, any:value=INVALID_HANDLE, flags=0);
    71 
    72 /**
    73  * Kills a timer.  Use this instead of CloseHandle() if you need more options.
    74  *
    75  * @param autoClose			If autoClose is true, the timer's value will be 
    76  *							closed as a handle if TIMER_HNDL_CLOSE was not specified.
    77  * @noreturn
    78  */
    79 native KillTimer(Handle:timer, bool:autoClose=false);
    80 
    81 /**
    82  * Manually triggers a timer so its function will be called.
    83  *
    84  * @param timer				Timer Handle to trigger.
    85  * @param reset				If reset is true, the elapsed time counter is reset
    86  *							so the full interval must pass again.
    87  * @noreturn
    88  */
    89 native TriggerTimer(Handle:timer, bool:reset=false);
    90 
    91 /**
    92  * Returns the simulated game time.
    93  *
    94  * @param simulated			Retrieves whether or not the tick count
    95  *							is being manually simulated by SourceMod.
    96  *							This is the case if no players have joined
    97  *							the map yet.
    98  * @return					Time based on the game tick count.
    99  */
   100 native Float:GetTickedTime(&bool:simulated);
   101 
   102 /**
   103  * Creates a timer associated with a new data pack, and returns the datapack.
   104  * @note The datapack is automatically freed when the timer ends.
   105  * @note The position of the datapack is not reset or changed for the timer function.
   106  *
   107  * @param interval			Interval from the current game time to execute the given function.
   108  * @param func				Function to execute once the given interval has elapsed.
   109  * @param data				The newly created datapack is passed though this by-reference parameter.
   110  * @param flags				Timer flags.
   111  * @return					Handle to the timer object.  You do not need to call CloseHandle().
   112  */
   113 stock Handle:CreateDataTimer(Float:interval, Timer:func, &Handle:data, flags=0)
   114 {
   115 	data = CreateDataPack();
   116 	flags |= TIMER_HNDL_CLOSE;
   117 	return CreateTimer(interval, func, data, flags);
   118 }