jRandomSkills/jRandomSkills - SRC Files/src/player/skills/Poison.cs
ByDexter 21e702654d Fix leaked skill state, second chance revives, and unrestored server cvars
General:
- Jester's no-damage state no longer applies to players who do not hold the
  skill. The shared health helper checked Jester's internal table without
  verifying the current assignment, so a single stale entry silently cancelled
  damage across every skill that deals damage through it.
- Jester's per-round reset no longer aborts when the holder is dead, which
  previously left the table populated for the following rounds.
- The per-round reset now covers every skill used on the current map instead of
  only the ones held in the round that just ended, so a skill nobody drew still
  clears state left over from an earlier round.
- Skills that track another player as a target are now notified when that player
  disconnects. Controller indexes are reused, so a leftover entry applied to
  whoever connected into that index next. Affects Poison, Deaf, Darkness,
  Glitch, Magnifier, Legless, No Rifles, Jammer and Jester.
- Aimbot no longer retains raw native pointers after a hit is handled. They were
  never cleared and were written back to later, which could corrupt memory the
  engine had already freed.
- An exception inside a skill's OnTick no longer cancels the tick of every skill
  after it in the same frame.
- Cooldowns configured low enough to truncate to zero no longer throw (Poison,
  Regeneration, Hot Bomb, Healing Smoke, Toxic Smoke).
- Round changes are cheaper: Long Knife no longer detaches a detour it never
  attached, and Wild Throw skips its player sweep when it has nothing to unwind.

Skill improvements:
- Second Chance: now survives several lethal hits landing in the same tick
  (shotgun, two attackers) and restores health inside the damage hook instead of
  a frame later. It also runs after skills that modify damage, so One-Shot and
  Soldier are seen at their final value instead of the raw one.
- Spectator: returning to your own view no longer leaves the camera handle set,
  which made the tick handler reset the weapon cooldown every frame and play as
  rapid fire. The manual toggle is unchanged.
- Radar Hack: players hidden behind a replacement model (Chicken) are shown on
  the radar again. Only Ghost, Ninja and C4 Camouflage hide the pawn itself, so
  render alpha alone no longer decides.
- Focus: weapon_accuracy_nospread is reference counted, so one holder losing the
  skill no longer removes the effect from the others still holding it.
- Friendly Fire: mp_autokick is restored at the end of the round instead of
  staying disabled for the rest of the map, and is no longer executed on every
  friendly hit.
- Reactive Armor / Iana: no longer restore health for a player who no longer
  holds the skill.
- Free Planter / Short Fuse: each restores mp_c4timer only when it actually
  changed it, so overlapping rounds cannot write back the wrong value.
- Illusionist: the replica movement timer now stops on map change.
- Ghost / Ninja / C4 Camouflage: the bomb is located once per transmit instead of
  once per receiver and hidden player, and the pass is skipped when nobody is
  hidden.
2026-07-21 19:44:13 +03:00

173 lines
No EOL
7.9 KiB
C#

using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using System.Collections.Concurrent;
using src.utils;
namespace src.player.skills
{
public class Poison : ISkill
{
private const Skills skillName = Skills.Poison;
private static readonly ConcurrentDictionary<uint, byte> poisonedPlayers = [];
private static readonly ConcurrentDictionary<uint, uint> playersToTarget = [];
private static readonly object setLock = new();
public static void LoadSkill()
{
SkillUtils.RegisterSkill(skillName, SkillsInfo.GetValue<string>(skillName, "color"), false);
}
public static void PlayerDisconnect(uint playerIndex)
{
lock (setLock)
{
poisonedPlayers.TryRemove(playerIndex, out _);
playersToTarget.TryRemove(playerIndex, out _);
foreach (var kvp in playersToTarget)
if (kvp.Value == playerIndex)
playersToTarget.TryRemove(kvp.Key, out _);
}
}
public static void NewRound()
{
lock (setLock)
{
poisonedPlayers.Clear();
playersToTarget.Clear();
}
}
public static void OnTick()
{
int cooldown = Math.Max(1, (int)(64 * SkillsInfo.GetValue<float>(skillName, "Cooldown")));
if (Server.TickCount % cooldown == 0)
{
int cooldown2 = cooldown * 2;
foreach (var playerIndex in poisonedPlayers.Keys)
{
var player = Utilities.GetPlayerFromIndex((int)playerIndex);
if (player == null || !player.IsValid || player.PlayerPawn == null) continue;
var pawn = player.PlayerPawn.Value;
if (pawn == null || !pawn.IsValid) continue;
if (Jester.IsActiveJester(playerIndex)) continue;
if (pawn.Health <= SkillsInfo.GetValue<int>(skillName, "MinHealth")) continue;
SkillUtils.TakeHealth(pawn, SkillsInfo.GetValue<int>(skillName, "Damage"));
if (Server.TickCount % cooldown2 == 0)
PlayerManager.GetPlayerFromEvent(player)?.ExecuteClientCommand($"play player/player_damagebody_0{jRandomSkills.Instance.Random.Next(4, 8)}");
}
}
if (Server.TickCount % 32 != 0) return;
foreach (var player in Utilities.GetPlayers())
{
if (!SkillUtils.HasMenu(player)) continue;
var playerInfo = PlayerManager.GetPlayerByIndex(player!.Index);
if (playerInfo == null || playerInfo.Skill != skillName) continue;
var enemies = Utilities.GetPlayers().Where(p =>p != null &&p.IsValid).Select(p => PlayerManager.GetPlayerEvent(p)).Where(p =>p != null &&p.IsValid &&p.Team != player.Team &&p.PlayerPawn?.Value != null &&p.PlayerPawn.Value.IsValid &&p.PlayerPawn.Value.Health > 0 &&!p.IsHLTV &&p.Team != CsTeam.Spectator&& p.Team != CsTeam.None).ToArray();
ConcurrentBag<(string, string)> menuItems = new(enemies.Select(e => (e.PlayerName, e.Index.ToString())));
SkillUtils.UpdateMenu(player, menuItems);
}
}
public static void TypeSkill(CCSPlayerController player, string[] commands)
{
if (player == null || !player.IsValid || player.LifeState != (byte)LifeState_t.LIFE_ALIVE) return;
var playerInfo = PlayerManager.GetPlayerByIndex(player!.Index);
if (playerInfo?.Skill != skillName) return;
var playerEvent = PlayerManager.GetPlayerFromEvent(player);
if (playerEvent == null || !playerEvent.IsValid) return;
if (playerInfo.SkillUsed)
{
playerEvent.PrintToChat($" {ChatColors.Red}{playerEvent.GetTranslation("areareaper_used_info")}");
return;
}
string enemyId = commands[0];
if (!uint.TryParse(enemyId, out uint enemyIndex)) {
playerEvent.PrintToChat($" {ChatColors.Red}" + playerEvent.GetTranslation("selectplayerskill_incorrect_enemy_index"));
return;
}
var enemy = Utilities.GetPlayerFromIndex((int)enemyIndex);
if (enemy == null)
{
playerEvent.PrintToChat($" {ChatColors.Red}" + playerEvent.GetTranslation("selectplayerskill_incorrect_enemy_index"));
return;
}
poisonedPlayers.TryAdd(enemy.Index, 0);
playersToTarget[player.Index] = enemy.Index;
playerInfo.SkillUsed = true;
var enemyEvent = PlayerManager.GetPlayerFromEvent(enemy);
if (enemyEvent == null || !enemyEvent.IsValid) return;
playerEvent.PrintToChat($" {ChatColors.Green}" + playerEvent.GetTranslation("poison_player_info", enemy.PlayerName));
enemyEvent.PrintToChat($" {ChatColors.Red}" + enemyEvent.GetTranslation("poison_enemy_info"));
}
public static void EnableSkill(CCSPlayerController player)
{
var playerInfo = PlayerManager.GetPlayerByIndex(player!.Index);
if (playerInfo == null) return;
playerInfo.SkillUsed = false;
var playerEvent = PlayerManager.GetPlayerFromEvent(player);
if (playerEvent == null || !playerEvent.IsValid) return;
var enemies = Utilities.GetPlayers().Where(p =>p != null &&p.IsValid).Select(p => PlayerManager.GetPlayerEvent(p)).Where(p =>p != null &&p.IsValid &&p.Team != player.Team &&p.PlayerPawn?.Value != null &&p.PlayerPawn.Value.IsValid &&p.PlayerPawn.Value.Health > 0 &&!p.IsHLTV &&p.Team != CsTeam.Spectator&& p.Team != CsTeam.None).ToArray();
if (enemies.Length > 0)
{
ConcurrentBag<(string, string)> menuItems = new(enemies.Select(e => (e.PlayerName, e.Index.ToString())));
SkillUtils.CreateMenu(player, menuItems);
}
else
playerEvent.PrintToChat($" {ChatColors.Red}{player.GetTranslation("selectplayerskill_incorrect_enemy_index")}");
}
public static void DisableSkill(CCSPlayerController player)
{
if (playersToTarget.TryRemove(player.Index, out uint targetIndex))
{
poisonedPlayers.TryRemove(targetIndex, out _);
var target = PlayerManager.GetPlayerFromEvent(Utilities.GetPlayerFromIndex((int)targetIndex));
if (target != null && target.IsValid && target.PawnIsAlive && !SkillUtils.IsFreezeTime())
target.PrintToChat($" {ChatColors.Green}" + target.GetTranslation("poison_disable_info"));
}
SkillUtils.CloseMenu(player);
}
public static void PlayerDeath(EventPlayerDeath @event)
{
var player = @event.Userid;
if (player == null || !player.IsValid) return;
poisonedPlayers.TryRemove(player.Index, out _);
SkillUtils.CloseMenu(player);
}
public class SkillConfig(Skills skill = skillName, bool active = true, string color = "#902eff", CsTeam onlyTeam = CsTeam.None, bool disableOnFreezeTime = true, bool needsTeammates = false, string requiredPermission = "", float? hudDuration = null, float? descriptionHudDuration = null, int maxPerServer = 2, Rarity rarity = Rarity.Common, float cooldown = .85f, int damage = 1, int minHealth = 30) : SkillsInfo.DefaultSkillInfo(skill, active, color, onlyTeam, disableOnFreezeTime, needsTeammates, requiredPermission, hudDuration, descriptionHudDuration, maxPerServer, rarity)
{
public int Damage { get; set; } = damage;
public float Cooldown { get; set; } = cooldown;
public int MinHealth { get; set; } = minHealth;
}
}
}