Skip to content

Commit

Permalink
fix: freeze timer bomb shared one timer
Browse files Browse the repository at this point in the history
  • Loading branch information
rick-yao committed Dec 12, 2024
1 parent 355fc4f commit a530dac
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions l4d2_tank_draw/scripting/l4d2_tank_draw.sp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public void OnPluginStart()

HookEvent("player_incapacitated", Event_PlayerIncapacitated);

HookEvent("player_death", Event_PlayerDeath);
HookEvent("mission_lost", Event_Lost);
HookEvent("player_death", Event_PlayerDeath, EventHookMode_Pre);
HookEvent("mission_lost", Event_Lost, EventHookMode_Pre);

HookEvent("molotov_thrown", Event_Molotov);

Expand Down
49 changes: 28 additions & 21 deletions l4d2_tank_draw/scripting/lib/freeze_timer_bomb.sp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#define DEFAULT_FREEZE_TIME 10
#define BEEP_SOUND "weapons/hegrenade/beep.wav"

int iColorBlue[4] = { 0, 128, 255, 192 };
int iColorBlue[4] = { 0, 128, 255, 192 };

Handle g_hFreezeTimer[MAXPLAYERS + 1] = { INVALID_HANDLE, ... };
Handle g_hFreezeBombTimer[MAXPLAYERS + 1] = { INVALID_HANDLE, ... };
Handle g_hUnfreezeTimer[MAXPLAYERS + 1] = { INVALID_HANDLE, ... };
int g_iFreezeBombTicks[MAXPLAYERS + 1];

/**
Expand All @@ -23,10 +24,10 @@ stock bool SetPlayerFreezeBomb(int target, int ticks = 8, float radius = DEFAULT
return false;

// If timer exists, kill it
if (g_hFreezeTimer[target] != INVALID_HANDLE)
if (g_hFreezeBombTimer[target] != INVALID_HANDLE)
{
KillTimer(g_hFreezeTimer[target]);
g_hFreezeTimer[target] = INVALID_HANDLE;
KillTimer(g_hFreezeBombTimer[target]);
g_hFreezeBombTimer[target] = INVALID_HANDLE;
g_iFreezeBombTicks[target] = 0;

// Reset player color
Expand All @@ -43,7 +44,7 @@ stock bool SetPlayerFreezeBomb(int target, int ticks = 8, float radius = DEFAULT
pack.WriteFloat(radius);
pack.WriteCell(freezeTime);

g_hFreezeTimer[target] = CreateTimer(1.0, Timer_FreezeBomb, pack, REPEAT_TIMER);
g_hFreezeBombTimer[target] = CreateTimer(1.0, Timer_FreezeBomb, pack, REPEAT_TIMER);

return true;
}
Expand All @@ -58,7 +59,8 @@ public Action Timer_FreezeBomb(Handle timer, DataPack pack)

if (!IsValidAliveClient(target))
{
g_hFreezeTimer[target] = INVALID_HANDLE;
KillTimer(g_hFreezeBombTimer[target]);
g_hFreezeBombTimer[target] = INVALID_HANDLE;
return Plugin_Stop;
}

Expand Down Expand Up @@ -114,7 +116,7 @@ public Action Timer_FreezeBomb(Handle timer, DataPack pack)
}
}

g_hFreezeTimer[target] = INVALID_HANDLE;
g_hFreezeBombTimer[target] = INVALID_HANDLE;
return Plugin_Stop;
}

Expand All @@ -123,17 +125,17 @@ void FreezePlayer(int client, int duration)
if (!IsValidAliveClient(client))
return;

// Clear existing timer if there is one
if (g_hFreezeTimer[client] != INVALID_HANDLE)
// Clear existing unfreeze timer if there is one
if (g_hUnfreezeTimer[client] != INVALID_HANDLE)
{
KillTimer(g_hFreezeTimer[client]);
g_hFreezeTimer[client] = INVALID_HANDLE;
KillTimer(g_hUnfreezeTimer[client]);
g_hUnfreezeTimer[client] = INVALID_HANDLE;
}

SetEntityMoveType(client, MOVETYPE_NONE);
SetEntityRenderColor(client, iColorBlue[0], iColorBlue[1], iColorBlue[2], iColorBlue[3]);

g_hFreezeTimer[client] = CreateTimer(float(duration), Timer_Unfreeze, client, NO_REPEAT_TIMER);
g_hUnfreezeTimer[client] = CreateTimer(float(duration), Timer_Unfreeze, client, NO_REPEAT_TIMER);
}

public Action Timer_Unfreeze(Handle timer, any client)
Expand All @@ -144,25 +146,30 @@ public Action Timer_Unfreeze(Handle timer, any client)
SetEntityRenderColor(client, 255, 255, 255, 255);
EmitSoundToClient(client, FREEZE_SOUND);
}
g_hFreezeTimer[client] = INVALID_HANDLE;
g_hUnfreezeTimer[client] = INVALID_HANDLE;
return Plugin_Stop;
}
stock void KillFreezeBomb(int client)

stock void KillFreezeBombTimer(int client)
{
if (g_hFreezeTimer[client] != INVALID_HANDLE)
if (g_hFreezeBombTimer[client] != INVALID_HANDLE)
{
KillTimer(g_hFreezeTimer[client]);
g_hFreezeTimer[client] = INVALID_HANDLE;
KillTimer(g_hFreezeBombTimer[client]);
g_hFreezeBombTimer[client] = INVALID_HANDLE;
g_iFreezeBombTicks[client] = 0;
SetEntityMoveType(client, MOVETYPE_WALK);
SetEntityRenderColor(client, 255, 255, 255, 255);
}

if (g_hUnfreezeTimer[client] != INVALID_HANDLE)
{
KillTimer(g_hUnfreezeTimer[client]);
g_hUnfreezeTimer[client] = INVALID_HANDLE;
}
}

stock void KillAllFreezeBombs()
{
for (int i = 1; i <= MaxClients; i++)
{
KillFreezeBomb(i);
KillFreezeBombTimer(i);
}
}
6 changes: 3 additions & 3 deletions l4d2_tank_draw/scripting/lib/lib.sp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ Handle g_WorldGravityTimer = INVALID_HANDLE;

int g_GlowDisabled = 0;

#define REPEAT_TIMER TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE | TIMER_DATA_HNDL_CLOSE
#define NO_REPEAT_TIMER TIMER_FLAG_NO_MAPCHANGE | TIMER_DATA_HNDL_CLOSE
#define REPEAT_TIMER TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE
#define NO_REPEAT_TIMER TIMER_FLAG_NO_MAPCHANGE

stock void ResetAllTimer()
{
Expand Down Expand Up @@ -157,7 +157,7 @@ stock void ResetClient(int client)

KillTimeBomb(client);

KillFreezeBomb(client);
KillFreezeBombTimer(client);

SetEntityGravity(client, 1.0);

Expand Down

0 comments on commit a530dac

Please sign in to comment.