Updated function

This commit is contained in:
Wizzard 2023-08-08 00:06:20 -04:00
parent 692d83494b
commit edcf5777cd
1 changed files with 22 additions and 57 deletions

View File

@ -1,6 +1,10 @@
#include <sourcemod> #include <sourcemod>
#include <tf2> #include <tf2>
#define TOGGLE_TIME 612.0
#define MIN_FF_DURATION 30
#define MAX_FF_DURATION 270
public Plugin myinfo = public Plugin myinfo =
{ {
name = "Toggle Friendly Fire", name = "Toggle Friendly Fire",
@ -10,27 +14,26 @@ public Plugin myinfo =
url = "https://deadzone.lol" url = "https://deadzone.lol"
}; };
Handle timerToggleFFOn = null; Handle timerToggle = null;
Handle timerToggleFFOff = null;
bool g_bFriendlyFire = false; bool g_bFriendlyFire = false;
bool g_bPluginEnabled = true; bool g_bPluginEnabled = true;
public void OnPluginStart() public void OnPluginStart()
{ {
RegConsoleCmd("sm_friendlyfire", CmdToggleFriendlyFire, "Toggle the Friendly Fire script. Admins with 'z' flag only."); RegConsoleCmd("sm_friendlyfire", CmdToggleFriendlyFire, "Toggle the Friendly Fire script. Admins with 'z' flag only.");
StartFFOnTimer(); StartToggleTimer();
} }
public Action CmdToggleFriendlyFire(int client, int args) public Action CmdToggleFriendlyFire(int client, int args)
{ {
if (CheckCommandAccess(client, "sm_friendlyfire", ADMFLAG_ROOT)) if (CheckCommandAccess(client, "sm_friendlyfire", ADMFLAG_ROOT))
{ {
g_bPluginEnabled = !g_bPluginEnabled; g_bPluginEnabled = !g_bPluginEnabled
if (g_bPluginEnabled) if (g_bPluginEnabled)
{ {
PrintToChat(client, "Friendly Fire script has been enabled!"); PrintToChat(client, "Friendly Fire script has been enabled!");
StartFFOnTimer(); StartToggleTimer();
} }
else else
{ {
@ -47,16 +50,10 @@ public Action CmdToggleFriendlyFire(int client, int args)
PrintToChat(client, "Friendly Fire script has been disabled!"); PrintToChat(client, "Friendly Fire script has been disabled!");
if (timerToggleFFOn != null) if (timerToggle != null)
{ {
KillTimer(timerToggleFFOn); KillTimer(timerToggle);
timerToggleFFOn = null; timerToggle = null;
}
if (timerToggleFFOff != null)
{
KillTimer(timerToggleFFOff);
timerToggleFFOff = null;
} }
} }
} }
@ -67,71 +64,39 @@ public Action CmdToggleFriendlyFire(int client, int args)
return Plugin_Handled; return Plugin_Handled;
} }
void StartFFOnTimer() void StartToggleTimer()
{ {
if (!g_bPluginEnabled) if (!g_bPluginEnabled)
return; return;
if (timerToggleFFOn != null) if (timerToggle != null)
KillTimer(timerToggleFFOn); KillTimer(timerToggle);
float randomTimeToTurnOn = GetRandomFloat(60.0, 600.0); // Between 1 and 10 minutes timerToggle = CreateTimer(TOGGLE_TIME, ToggleFriendlyFire);
timerToggleFFOn = CreateTimer(randomTimeToTurnOn, TurnOnFriendlyFire);
} }
public Action TurnOnFriendlyFire(Handle timer, any data) public Action ToggleFriendlyFire(Handle timer, any data)
{ {
g_bFriendlyFire = true; g_bFriendlyFire = !g_bFriendlyFire;
Handle hCvar = FindConVar("mp_friendlyfire"); Handle hCvar = FindConVar("mp_friendlyfire");
if(hCvar == INVALID_HANDLE) if(hCvar == INVALID_HANDLE)
{ {
SetFailState("Failed to find 'mp_friendlyfire' cvar!"); SetFailState("Failed to find 'mp_friendlyfire' cvar!");
} }
SetConVarInt(hCvar, 1); SetConVarInt(hCvar, g_bFriendlyFire ? 1 : 0);
PrintToChatAll("[DEBUG] Friendly Fire is now ON"); PrintToChatAll("[DEBUG] Friendly Fire is now %s", g_bFriendlyFire ? "ON" : "OFF");
StartFFOffTimer(); StartToggleTimer();
return Plugin_Continue;
}
void StartFFOffTimer()
{
if (timerToggleFFOff != null)
KillTimer(timerToggleFFOff);
float randomTimeToTurnOff = GetRandomFloat(30.0, 150.0); // Between 30 seconds and 2.5 minutes
timerToggleFFOff = CreateTimer(randomTimeToTurnOff, TurnOffFriendlyFire);
}
public Action TurnOffFriendlyFire(Handle timer, any data)
{
g_bFriendlyFire = false;
Handle hCvar = FindConVar("mp_friendlyfire");
if(hCvar == INVALID_HANDLE)
{
SetFailState("Failed to find 'mp_friendlyfire' cvar!");
}
SetConVarInt(hCvar, 0);
PrintToChatAll("[DEBUG] Friendly Fire is now OFF");
StartFFOnTimer();
return Plugin_Continue; return Plugin_Continue;
} }
public void OnPluginEnd() public void OnPluginEnd()
{ {
if(timerToggleFFOn != null) if(timerToggle != null)
{ {
KillTimer(timerToggleFFOn); KillTimer(timerToggle);
}
if(timerToggleFFOff != null)
{
KillTimer(timerToggleFFOff);
} }
} }