Skip to content

Commit

Permalink
feat: prevent player afk to avoid bomb
Browse files Browse the repository at this point in the history
  • Loading branch information
rick-yao committed Jan 16, 2025
1 parent 4deed2b commit 7e2cfae
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 42 deletions.
26 changes: 26 additions & 0 deletions l4d2_tank_draw/scripting/l4d2_tank_draw.sp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public void OnPluginStart()
HookEvent("map_transition", Event_RoundEnd, EventHookMode_Pre);
HookEvent("finale_win", Event_RoundEnd, EventHookMode_Pre);

HookEvent("player_afk", Event_Afk, EventHookMode_Pre);

HookConVarChange(ClearBuffIfMissionLost, ConVarChanged);
HookConVarChange(ChanceAverageHealth, ConVarChanged);
HookConVarChange(ChanceClearAllSurvivorHealth, ConVarChanged);
Expand Down Expand Up @@ -564,4 +566,28 @@ void SetConVar()
DebugPrint("调试菜单打开 / debug menu on");
RegAdminCmd("sm_tankdraw", MenuFunc_MainMenu, ADMFLAG_CHEATS);
}
}

// prevent player afk to avoid bomb
Action Event_Afk(Event event, const char[] name, bool dontBroadcast)
{
if (g_iTankDrawEnable == 0) { return Plugin_Continue; }

DebugPrint("Event_Afk triggered afk.");
int client = GetClientOfUserId(event.GetInt("player"));

if (g_hTimeBombTimer[client] != null)
{
BombPlayer(client, g_fTimerBombRadius, g_iTimerBombRangeDamage);
delete g_hTimeBombTimer[client];
g_iTimeBombTicks[client] = 0;
}

if (g_hFreezeBombTimer[client] != null)
{
delete g_hFreezeBombTimer[client];
FreezePlayer(client, g_iFreezeBombDuration);
}

return Plugin_Continue;
}
2 changes: 1 addition & 1 deletion l4d2_tank_draw/scripting/lib/freeze_timer_bomb.sp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Action Timer_FreezeBomb(Handle timer, DataPack pack)
return Plugin_Stop;
}

void FreezePlayer(int client, int duration)
stock void FreezePlayer(int client, int duration)
{
if (!IsValidAliveClient(client))
return;
Expand Down
87 changes: 46 additions & 41 deletions l4d2_tank_draw/scripting/lib/timer_bomb.sp
Original file line number Diff line number Diff line change
Expand Up @@ -105,47 +105,7 @@ public Action Timer_HandleBomb(Handle timer, DataPack data)

if (g_iTimeBombTicks[target] <= 0)
{
// Create explosion effect
if (g_ExplosionSprite > -1)
{
TE_SetupExplosion(vecOrigin, g_ExplosionSprite, 20.0, 1, 0,
RoundToNearest(radius), 5000);
TE_SendToAll();
}

float deathPos[3];
GetClientAbsOrigin(target, deathPos);

// Kill the bomb holder
ForcePlayerSuicide(target);

// Damage nearby players
for (int i = 1; i <= MaxClients; i++)
{
if (!IsValidAliveClient(i) || i == target)
continue;

float survivorPos[3];
GetClientAbsOrigin(i, survivorPos);

float distance = GetVectorDistance(deathPos, survivorPos);
if (distance <= radius)
{
// Calculate damage based on distance
int finalDamage = RoundToFloor(damage * ((radius - distance) / radius));

// Create smaller explosion effect on damaged players
if (g_ExplosionSprite > -1)
{
TE_SetupExplosion(survivorPos, g_ExplosionSprite, 0.2, 1, 0, 1, 1);
TE_SendToAll();
}

// Apply damage
SlapPlayer(i, 0);
SDKHooks_TakeDamage(i, i, target, float(finalDamage), DMG_GENERIC);
}
}
BombPlayer(target, radius, damage);

KillTimeBomb(target);
return Plugin_Stop;
Expand All @@ -169,4 +129,49 @@ stock void KillAllTimeBombs()
{
KillTimeBomb(i);
}
}

stock void BombPlayer(int target, float radius, int damage)
{
float deathPos[3];
GetClientAbsOrigin(target, deathPos);

// Create explosion effect
if (g_ExplosionSprite > -1)
{
TE_SetupExplosion(deathPos, g_ExplosionSprite, 20.0, 1, 0,
RoundToNearest(radius), 5000);
TE_SendToAll();
}

// Kill the bomb holder
ForcePlayerSuicide(target);

// Damage nearby players
for (int i = 1; i <= MaxClients; i++)
{
if (!IsValidAliveClient(i) || i == target)
continue;

float survivorPos[3];
GetClientAbsOrigin(i, survivorPos);

float distance = GetVectorDistance(deathPos, survivorPos);
if (distance <= radius)
{
// Calculate damage based on distance
int finalDamage = RoundToFloor(damage * ((radius - distance) / radius));

// Create smaller explosion effect on damaged players
if (g_ExplosionSprite > -1)
{
TE_SetupExplosion(survivorPos, g_ExplosionSprite, 0.2, 1, 0, 1, 1);
TE_SendToAll();
}

// Apply damage
SlapPlayer(i, 0);
SDKHooks_TakeDamage(i, i, target, float(finalDamage), DMG_GENERIC);
}
}
}

0 comments on commit 7e2cfae

Please sign in to comment.