faluco@612: /**
faluco@612: * vim: set ts=4 :
ds@1330: * =============================================================================
faluco@612: * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
ds@1330: * =============================================================================
ds@1330: *
ds@1330: * This file is part of the SourceMod/SourcePawn SDK.
faluco@612: *
ds@1330: * This program is free software; you can redistribute it and/or modify it under
ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330: * Free Software Foundation.
ds@1330: *
ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1330: * details.
ds@1330: *
ds@1330: * You should have received a copy of the GNU General Public License along with
ds@1330: * this program. If not, see .
ds@1330: *
ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1330: * this exception to all derivative works. AlliedModders LLC defines further
ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330: * or .
faluco@612: *
faluco@612: * Version: $Id$
faluco@612: */
faluco@612:
faluco@612: #if defined _timers_included
faluco@612: #endinput
faluco@612: #endif
faluco@612: #define _timers_included
faluco@612:
faluco@612: #include
faluco@612:
dvander@1108: #define TIMER_REPEAT (1<<0) /**< Timer will repeat until it returns Plugin_Stop */
dvander@1108: #define TIMER_FLAG_NO_MAPCHANGE (1<<1) /**< Timer will not carry over mapchanges */
dvander@1108: #define TIMER_HNDL_CLOSE (1<<9) /**< Timer will automatically call CloseHandle() on its value when finished */
faluco@612:
faluco@612: /**
faluco@612: * Any of the following prototypes will work for a timed function.
faluco@612: */
faluco@612: funcenum Timer
faluco@612: {
faluco@612: /**
faluco@612: * Called when the timer interval has elapsed.
faluco@612: *
faluco@612: * @param timer Handle to the timer object.
faluco@612: * @param hndl Handle passed when the timer was created.
faluco@612: * @return Plugin_Stop to stop a repeating timer, any other value for
faluco@612: * default behavior.
faluco@612: */
faluco@612: Action:public(Handle:timer, Handle:hndl),
faluco@612:
faluco@612: /**
faluco@612: * Called when the timer interval has elapsed.
faluco@612: *
faluco@612: * @param timer Handle to the timer object.
faluco@612: * @param value Value passed when the timer was created.
faluco@612: * @return Plugin_Stop to stop a repeating timer, any other value for
faluco@612: * default behavior.
faluco@612: */
dvander@716: Action:public(Handle:timer, any:value),
faluco@612:
faluco@612: /**
faluco@612: * Called when the timer interval has elapsed.
faluco@612: *
faluco@612: * @param timer Handle to the timer object.
faluco@612: * @return Plugin_Stop to stop a repeating timer, any other value for
faluco@612: * default behavior.
faluco@612: */
faluco@612: Action:public(Handle:timer),
faluco@612: };
faluco@612:
faluco@612: /**
faluco@612: * Creates a basic timer. Calling CloseHandle() on a timer will end the timer.
faluco@612: *
faluco@612: * @param interval Interval from the current game time to execute the given function.
faluco@612: * @param func Function to execute once the given interval has elapsed.
faluco@612: * @param value Handle or value to give to the timer function.
faluco@612: * @param flags Flags to set (such as repeatability or auto-Handle closing).
faluco@612: * @return Handle to the timer object. You do not need to call CloseHandle().
dvander@1409: * If the timer could not be created, INVALID_HANDLE will be returned.
faluco@612: */
dvander@716: native Handle:CreateTimer(Float:interval, Timer:func, any:value=INVALID_HANDLE, flags=0);
faluco@612:
faluco@612: /**
faluco@612: * Kills a timer. Use this instead of CloseHandle() if you need more options.
faluco@612: *
faluco@612: * @param autoClose If autoClose is true, the timer's value will be
faluco@612: * closed as a handle if TIMER_HNDL_CLOSE was not specified.
faluco@612: * @noreturn
faluco@612: */
faluco@612: native KillTimer(Handle:timer, bool:autoClose=false);
faluco@612:
faluco@612: /**
faluco@612: * Manually triggers a timer so its function will be called.
faluco@612: *
faluco@612: * @param timer Timer Handle to trigger.
faluco@612: * @param reset If reset is true, the elapsed time counter is reset
faluco@612: * so the full interval must pass again.
faluco@612: * @noreturn
faluco@612: */
faluco@612: native TriggerTimer(Handle:timer, bool:reset=false);
faluco@612:
faluco@612: /**
dvander@1412: * Returns the simulated game time.
dvander@822: *
dvander@1412: * This time is internally maintained by SourceMod and is based on the game
dvander@1412: * tick count and tick rate. Unlike GetGameTime(), it will increment past
dvander@1412: * map changes and while no players are connected. Unlike GetEngineTime(),
dvander@1412: * it will not increment based on the system clock (i.e. it is still bound
dvander@1412: * to the ticking process).
dvander@1412: *
dvander@822: * @return Time based on the game tick count.
dvander@822: */
dvander@1412: native Float:GetTickedTime();
dvander@822:
dvander@822: /**
dvander@1462: * Returns an estimate of the time left before the map ends. If the server
dvander@1462: * has not processed any frames yet (i.e. no players have joined the map yet),
dvander@1462: * then the time left returned will always be infinite.
dvander@1415: *
dvander@1415: * @param timeleft Variable to store the time, in seconds. If the
dvander@1415: * value is less than 0, the time limit is infinite.
dvander@1415: * @return True if the operation is supported, false otherwise.
dvander@1415: */
dvander@1415: native bool:GetMapTimeLeft(&timeleft);
dvander@1415:
dvander@1415: /**
dvander@1462: * Retrieves the current map time limit. If the server has not processed any
dvander@1462: * frames yet (i.e. no players have joined the map yet), then the time limit
dvander@1462: * returned will always be 0.
dvander@1415: *
dvander@1415: * @param time Set to the number of total seconds in the map time
dvander@1415: * limit, or 0 if there is no time limit set.
dvander@1415: * @return True on success, false if operation is not supported.
dvander@1415: */
dvander@1415: native bool:GetMapTimeLimit(&time);
dvander@1415:
dvander@1415: /**
dvander@1415: * Extends the map time limit in a way that will notify all plugins.
dvander@1415: *
dvander@1415: * @param time Number of seconds to extend map time limit by.
dvander@1415: * The number can be negative to decrease the time limit.
dvander@1415: * If 0, the map will be set to have no time limit.
dvander@1415: * @return True on success, false if operation is not supported.
dvander@1415: */
dvander@1415: native bool:ExtendMapTimeLimit(time);
dvander@1415:
dvander@1415: /**
dvander@1462: * Notification that the map's time left has changed via a change in the time
dvander@1415: * limit or a change in the game rules (such as mp_restartgame). This is useful
dvander@1415: * for plugins trying to create timers based on the time left in the map.
dvander@1415: *
dvander@1415: * Calling ExtendMapTimeLimit() from here, without proper precaution, will
dvander@1415: * cause infinite recursion.
dvander@1415: *
dvander@1415: * If the operation is not supported, this will never be called.
dvander@1462:
dvander@1462: * If the server has not yet processed any frames (i.e. no players have joined
dvander@1462: * the map yet), then this will be called once the server begins ticking, even
dvander@1462: * if there is no time limit set.
dvander@1415: */
dvander@1415: forward OnMapTimeLeftChanged();
dvander@1415:
dvander@1415: /**
dvander@1459: * Returns whether or not the server is processing frames or not.
dvander@1459: *
dvander@1459: * The server does not process frames until at least one client joins the game.
dvander@1459: * Once the first player has in, even if that player, leaves, the server's
dvander@1459: * timers and entities will work.
dvander@1459: *
dvander@1459: * @return True if the server is ticking, false otherwise.
dvander@1459: */
dvander@1459: native bool:IsServerProcessing();
dvander@1459:
dvander@1459: /**
faluco@612: * Creates a timer associated with a new data pack, and returns the datapack.
faluco@612: * @note The datapack is automatically freed when the timer ends.
faluco@612: * @note The position of the datapack is not reset or changed for the timer function.
faluco@612: *
faluco@612: * @param interval Interval from the current game time to execute the given function.
faluco@612: * @param func Function to execute once the given interval has elapsed.
faluco@612: * @param data The newly created datapack is passed though this by-reference parameter.
faluco@612: * @param flags Timer flags.
faluco@612: * @return Handle to the timer object. You do not need to call CloseHandle().
faluco@612: */
dvander@716: stock Handle:CreateDataTimer(Float:interval, Timer:func, &Handle:data, flags=0)
faluco@612: {
faluco@612: data = CreateDataPack();
faluco@612: flags |= TIMER_HNDL_CLOSE;
faluco@612: return CreateTimer(interval, func, data, flags);
faluco@612: }
dvander@1462: