Skip to content

Commit

Permalink
Continued improvements, we now check and automatically instadefuse on…
Browse files Browse the repository at this point in the history
… player death.
  • Loading branch information
B3none committed Jan 28, 2024
1 parent 4cfb518 commit 4f9c49a
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions InstadefusePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public override void Load(bool hotReload)
Console.WriteLine($"{LogPrefix}Plugin loaded!");
}

[GameEventHandler]
public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
{
AttemptInstadefuse();
return HookResult.Continue;
}

[GameEventHandler]
public HookResult OnGrenadeThrown(EventGrenadeThrown @event, GameEventInfo info)
{
Expand All @@ -43,7 +50,7 @@ public HookResult OnGrenadeThrown(EventGrenadeThrown @event, GameEventInfo info)
{
_heThreat++;
}
else if (weapon == "incgrenade" || @event.Weapon == "molotov")
else if (weapon == "incgrenade" || weapon == "molotov")
{
_molotovThreat++;
}
Expand Down Expand Up @@ -101,7 +108,7 @@ public HookResult OnInfernoExtinguish(EventInfernoExtinguish @event, GameEventIn

_infernoThreat.Remove(@event.Entityid);

PrintThreatLevel();
AttemptInstadefuse();

return HookResult.Continue;
}
Expand All @@ -113,7 +120,7 @@ public HookResult OnInfernoExpire(EventInfernoExpire @event, GameEventInfo info)

_infernoThreat.Remove(@event.Entityid);

PrintThreatLevel();
AttemptInstadefuse();

return HookResult.Continue;
}
Expand All @@ -128,7 +135,7 @@ public HookResult OnHeGrenadeDetonate(EventHegrenadeDetonate @event, GameEventIn
_heThreat--;
}

PrintThreatLevel();
AttemptInstadefuse();

return HookResult.Continue;
}
Expand All @@ -142,8 +149,8 @@ public HookResult OnMolotovDetonate(EventMolotovDetonate @event, GameEventInfo i
{
_molotovThreat--;
}

PrintThreatLevel();
AttemptInstadefuse();

return HookResult.Continue;
}
Expand Down Expand Up @@ -179,19 +186,12 @@ public HookResult OnBombBeginDefuse(EventBombBegindefuse @event, GameEventInfo i
{
Console.WriteLine($"{LogPrefix}OnBombBeginDefuse");

var player = @event.Userid;

if (player == null || !player.IsValid)
{
return HookResult.Continue;
}

AttemptInstadefuse(player);
AttemptInstadefuse();

return HookResult.Continue;
}

private void AttemptInstadefuse(CCSPlayerController player)
private void AttemptInstadefuse()
{
Console.WriteLine($"{LogPrefix}Attempting instadefuse...");

Expand Down Expand Up @@ -229,12 +229,19 @@ private void AttemptInstadefuse(CCSPlayerController player)
return;
}

var defuser = GetDefuser();

if (defuser == null)
{
return;
}

var bombTimeUntilDetonation = plantedBomb.TimerLength - (Server.CurrentTime - _bombPlantedTime);

var defuseLength = plantedBomb.DefuseLength;
if (defuseLength != 5 && defuseLength != 10)
{
defuseLength = player.PawnHasDefuser ? 5.0f : 10.0f;
defuseLength = defuser.PawnHasDefuser ? 5.0f : 10.0f;
}
Console.WriteLine($"{LogPrefix}DefuseLength: {defuseLength}");

Expand All @@ -243,7 +250,7 @@ private void AttemptInstadefuse(CCSPlayerController player)

if (!bombCanBeDefusedInTime)
{
var outputText = $"{player.PlayerName} was {ChatColors.DarkRed}{Math.Abs(timeLeftAfterDefuse):n3} seconds{ChatColors.White} away from defusing.";
var outputText = $"{defuser.PlayerName} was {ChatColors.DarkRed}{Math.Abs(timeLeftAfterDefuse):n3} seconds{ChatColors.White} away from defusing.";
Console.WriteLine($"{LogPrefix}{outputText}");
Server.PrintToChatAll($"{MessagePrefix}{outputText}");

Expand All @@ -259,12 +266,33 @@ private void AttemptInstadefuse(CCSPlayerController player)
{
plantedBomb.DefuseCountDown = 0;

var outputText = $"{player.PlayerName} defused with {ChatColors.Green}{bombTimeUntilDetonation:n3} seconds{ChatColors.White} left on the bomb.";
var outputText = $"{defuser.PlayerName} defused with {ChatColors.Green}{bombTimeUntilDetonation:n3} seconds{ChatColors.White} left on the bomb.";
Console.WriteLine($"{LogPrefix}{outputText}");
Server.PrintToChatAll($"{MessagePrefix}{outputText}");
});
}

private static CCSPlayerController? GetDefuser()
{
var players = Utilities.GetPlayers();

foreach (var player in players)
{
if (!player.IsValid) continue;
if (!player.PawnIsAlive) continue;
if (player.Team != CsTeam.CounterTerrorist) continue;

var playerPawn = player.PlayerPawn.Value;
if (playerPawn == null) continue;
if (!playerPawn.IsValid) continue;
if (!playerPawn.IsDefusing) continue;

return player;
}

return null;
}

private static bool TeamHasAlivePlayers(CsTeam team)
{
var players = Utilities.GetPlayers();
Expand Down

0 comments on commit 4f9c49a

Please sign in to comment.