Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // VC:MP HealingSystem
- // ------------------------------------------------------------------------------------------------
- // Global table used to scope base configurations
- _CFG <- {}
- // Global table used to scope script commands
- _CMD <- {}
- // ------------------------------------------------------------------------------------------------
- // Global enumeration to hold the command syntax
- enum CmdSyn
- {
- HealMe = "healme"
- }
- // ------------------------------------------------------------------------------------------------
- // Global enumeration to hold the message style and colours
- enum MsgTag {
- Dbg = "[#11455D][DEBUG][#F4F3EE]",
- Msg = "[#64A5BB][MESSAGE][#F4F3EE]",
- Scs = "[#698433][SUCCESS][#F4F3EE]",
- Inf = "[#FFD35C][INFO][#F4F3EE]",
- Wrn = "[#FF8202][WARNING][#F4F3EE]",
- Err = "[#EA3E70][ERROR][#F4F3EE]",
- Ftl = "[#C82C3A][FATAL][#F4F3EE]"
- }
- // ------------------------------------------------------------------------------------------------
- // Cost of normal heal command
- _CFG.Heal_Cost <- 1000;
- // The actual amount of health to set
- _CFG.Heal_Amount <- 100;
- // Number of milliseconds to delay the healing process
- _CFG.Heal_Delay <- 2000;
- // Number of milliseconds to wait before waking up the coroutines
- _CFG.Heal_Speed <- 50;
- // ------------------------------------------------------------------------------------------------
- // Scoped functions used to send formatted messages to players
- _Msg <- {
- // --------------------------------------------------------------------------------------------
- function Player(...)
- {
- // Grab the player instance from the first argument
- local i_player = vargv[0];
- // Replace the player instance with the (this) environment
- vargv[0] = this;
- // Forward all the arguments to the format function
- local str = ::format.acall(vargv);
- // Send the generated message to the specified player
- MessagePlayer(str, i_player);
- }
- }
- // ------------------------------------------------------------------------------------------------
- _ClientPool <- []
- // ------------------------------------------------------------------------------------------------
- _HealmePool <- []
- // ------------------------------------------------------------------------------------------------
- // Class responsible for management and identification of player instances
- class _Client
- {
- // --------------------------------------------------------------------------------------------
- function constructor(i_player)
- {
- // Verify the player instance
- if (typeof i_player != "instance") return null;
- // Store the player instance
- else Instance = i_player
- }
- // --------------------------------------------------------------------------------------------
- function Connect()
- {
- // Ignored....
- }
- // --------------------------------------------------------------------------------------------
- function Disconnect(reason)
- {
- // See if there's any active healing processes made by this instance remove it
- if (_HealmePool[Instance.ID] != null) _HealmePool[Instance.ID] = null;
- // Clear any stored instances
- Instance = null;
- }
- // --------------------------------------------------------------------------------------------
- Instance = null
- }
- // ------------------------------------------------------------------------------------------------
- // The actual coroutine that increases the player health progressively
- function _HealmeCoroutine(i_player, amount, limit, delay)
- {
- // Suspend the coroutine while the delay is in effect
- while (delay > 0) delay -= ::suspend();
- // Start adding health points until we reach zero
- while (amount--) {
- // Increase the player health until the limit is reached
- if (++i_player.Health >= limit) break;
- // Suspend the coroutine
- ::suspend();
- }
- }
- // ------------------------------------------------------------------------------------------------
- // Processes all the active healing coroutines
- function _HealmeUpdate()
- {
- // Loop through all the active coroutines
- foreach (idx, coro in _HealmePool) {
- // See if the player at this index activated the healing process
- if (coro != null) {
- // Wake up the coroutine and pass the elapsed time
- coro.wakeup(_CFG.Heal_Speed);
- // Remove the coroutine if it's finished
- if (coro.getstatus() != "suspended") _HealmePool[idx] = null;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------
- _CMD.HealMe <- function(i_player, args)
- {
- // Make sure there isn't another healing process active
- if (_HealmePool[i_player.ID] != null) return _Msg.Player(i_player, @"%s Please wait until the current healing process is finished.", MsgTag.Wrn);
- // Make sure the invoker can afford the cost
- else if (i_player.Money < _CFG.Heal_Cost) return _Msg.Player(i_player, @"%s You need $%d more to use the healing command.", MsgTag.Wrn, (_CFG.Heal_Cost - i_player.Money));
- // Make sure the invoker actually needs healing
- else if (i_player.Health >= _CFG.Heal_Amount) return _Msg.Player(i_player, @"%s You have more than enough health to use this command.", MsgTag.Wrn);
- // Create and store the coroutine to be continued on next update
- _HealmePool[i_player.ID] = ::newthread(_HealmeCoroutine);
- // Initiate the coroutine with the default arguments
- _HealmePool[i_player.ID].call(i_player, _CFG.Heal_Amount, _CFG.Heal_Amount, _CFG.Heal_Delay);
- // Get the payment for the healing process
- i_player.Money -= _CFG.Heal_Cost;
- // Notify the invoker about the result
- _Msg.Player(i_player, @"%s You activated the healing process in exchange for $%d", MsgTag.Scs, _CFG.Heal_Cost);
- }
- // ------------------------------------------------------------------------------------------------
- function onServerStart()
- {
- // Reserve enough space to hold the maximum number of Clients that the server can hold
- _ClientPool = array(GetMaxPlayers());
- _HealmePool = array(GetMaxPlayers());
- // Attempt to find any previously connected Clients in case of a script reload
- foreach (idx, inst in _ClientPool) {
- // Attempt to find the player by it's unique identifier
- local res = FindPlayer(idx);
- // See if a valid player instance was found at this ID
- if (res != null) {
- // Create a Client instance for this player instance
- inst = _Client(res);
- // Allow the Client instance to initialize
- inst.Connect();
- }
- }
- // Start the healing update timer
- NewTimer("_HealmeUpdate", _CFG.Heal_Speed, 0);
- }
- // ------------------------------------------------------------------------------------------------
- function onServerStop()
- {
- // Attempt to de-initialize all connected players from the server
- foreach (inst in _ClientPool) {
- // See if there was any Client instance in this slot
- if (inst != null) {
- // Allow the Client instance to de-initialize
- inst.Disconnect();
- // Clear the slot
- _ClientPool[i_player.ID] = null;
- }
- }
- // Clear all the healing coroutines
- foreach (idx, req in _HealmePool) _HealmePool[idx] = null;
- }
- // ------------------------------------------------------------------------------------------------
- function onPlayerJoin(i_player)
- {
- // Create a Client instance for this player instance
- _ClientPool[i_player.ID] = _Client(i_player);
- // Allow the newly created Client instance to initialize
- _ClientPool[i_player.ID].Connect();
- }
- // ------------------------------------------------------------------------------------------------
- function onPlayerPart(i_player, reason)
- {
- // Allow the Client instance to de-initialize
- _ClientPool[i_player.ID].Disconnect(reason);
- // Clear the slot for other Clients
- _ClientPool[i_player.ID] = null;
- }
- // ------------------------------------------------------------------------------------------------
- function onPlayerCommand(i_player, cmd, args)
- {
- switch (cmd)
- {
- // ----------------------------------------------------------------------------------------
- case "healme": _CMD.HealMe(i_player, args); break;
- // ----------------------------------------------------------------------------------------
- default: _Msg.Player(i_player, @"%s Unknown command: %s %s", MsgTag.Err, cmd, typeof args == "string" ? args : "");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement