jRandomSkills/jRandomSkills - SRC Files/src/player/skills/Noclip.cs
ByDexter 0e86f54e0a v1.2.3.b1
Revive skills (Second Chance / Phoenix / Re-Zombie) — unified "on death" logic:
- Intercept the lethal hit before it is applied. Previously they reacted after the death was already committed, so one-shots, fall damage and skill-based kills were missed (Phoenix and Re-Zombie were rewritten from a post-death handler to pre-damage interception).

Killer Flash:
- Kills through real damage instead of forcing a suicide, so revive skills can intercept.

Baseball:
- Fixed a rare server crash from decoy grenades: a freed decoy's entity index could be reused by another entity and written to (use-after-free). Decoy tracking is now validated per tick (DesignerName gate) and cleared every round.

Native crash hardening:
- Aimbot: guard the hit-group native pointer before writing (avoids writing to freed/null memory).
- Wallhack: detach the glow when the target dies or disconnects (stale FollowEntity on a freed pawn).
- Spectator / Cypher / FalconEye / ThirdEye: null-guard the camera SceneNode walk and re-validate the target before teleport/parent (cross-frame freed-entity deref).
- CServerSideClient.ForceFullUpdate now bails on a null client handle.

Performance:
- Added a per-tick player/bomb cache (PlayerManager.GetTickPlayers / GetTickBomb).
- Migrated 82 skills from per-skill Utilities.GetPlayers() to the one shared per-tick scan, and the HUD loop reuses it. Much lower per-tick cost at high player counts (verified 10v10).

RayTrace:
- Prints a single notice when the module is not installed; dependent skills (Long Zeus, Long Knife, Iana, Cypher, Noclip, Shade + the skill-use aim check) no-op instead of failing silently (#43).
2026-07-26 17:49:36 +03:00

237 lines
No EOL
9.6 KiB
C#

using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using src.utils;
using System.Collections.Concurrent;
using static src.jRandomSkills;
using Timer = CounterStrikeSharp.API.Modules.Timers.Timer;
namespace src.player.skills
{
public class Noclip : ISkill
{
private const Skills skillName = Skills.Noclip;
private static readonly ConcurrentDictionary<uint, PlayerSkillInfo> SkillPlayerInfo = [];
private static readonly object setLock = new();
public static void LoadSkill()
{
SkillUtils.RegisterSkill(skillName, SkillsInfo.GetValue<string>(skillName, "color"));
}
public static void NewRound()
{
lock (setLock)
SkillPlayerInfo.Clear();
}
public static void OnTick()
{
foreach (var player in PlayerManager.GetTickPlayers())
{
var playerInfo = PlayerManager.GetPlayerByIndex(player!.Index);
if (playerInfo?.Skill == skillName)
if (SkillPlayerInfo.TryGetValue(player.Index, out var skillInfo))
UpdateHUD(player, skillInfo);
}
}
public static void EnableSkill(CCSPlayerController player)
{
SkillPlayerInfo.TryAdd(player.Index, new PlayerSkillInfo
{
SteamID = player.Index,
CanUse = true,
IsFlying = false,
Cooldown = DateTime.MinValue,
LastPosition = null,
});
}
public static void DisableSkill(CCSPlayerController player)
{
SkillPlayerInfo.TryRemove(player.Index, out _);
SkillUtils.ResetPrintHTML(player);
}
private static void UpdateHUD(CCSPlayerController player, PlayerSkillInfo skillInfo)
{
float cooldown = 0;
float flying = 0;
if (skillInfo != null)
{
float time = (int)Math.Ceiling((skillInfo.Cooldown.AddSeconds(SkillsInfo.GetValue<float>(skillName, "cooldown")) - DateTime.Now).TotalSeconds);
cooldown = Math.Max(time, 0);
float flyingTime = (int)(skillInfo.Cooldown.AddSeconds(SkillsInfo.GetValue<float>(skillName, "duration")) - DateTime.Now).TotalMilliseconds;
flying = Math.Max(flyingTime, 0);
if (cooldown == 0 && skillInfo?.CanUse == false)
skillInfo.CanUse = true;
}
var playerInfo = PlayerManager.GetPlayerByIndex(player!.Index);
if (playerInfo == null) return;
if (cooldown == 0)
{
playerInfo.PrintHTML = null;
return;
}
playerInfo.PrintHTML =
skillInfo?.IsFlying == true
? $"{player.GetTranslation("active_hud_info", $"<font color='#00FF00'>{Math.Round(flying / 100, 2)}</font>")}"
: $"{player.GetTranslation("hud_info", $"<font color='#FF0000'>{cooldown}</font>")}";
}
public static void UseSkill(CCSPlayerController player)
{
var playerPawn = player.PlayerPawn.Value;
if (playerPawn?.CBodyComponent == null) return;
uint playerIndex = player.Index;
if (SkillPlayerInfo.TryGetValue(playerIndex, out var skillInfo))
{
if (skillInfo.IsFlying)
{
StopFlying(player, skillInfo);
return;
}
if (skillInfo.CanUse)
{
var duration = SkillsInfo.GetValue<float>(skillName, "duration");
skillInfo.CanUse = false;
skillInfo.IsFlying = true;
skillInfo.Cooldown = DateTime.Now;
skillInfo.LastPosition = playerPawn.AbsOrigin == null ? null : new Vector(playerPawn.AbsOrigin.X, playerPawn.AbsOrigin.Y, playerPawn.AbsOrigin.Z);
SetNoclip(player, true);
skillInfo.Timer?.Kill();
skillInfo.Timer = Instance.AddTimer(duration, () =>
{
var player = Utilities.GetPlayerFromIndex((int)playerIndex);
if (player == null || !player.IsValid) return;
var playerInfo = PlayerManager.GetPlayerByIndex(playerIndex);
if (playerInfo == null) return;
StopFlying(player, skillInfo);
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
}
}
}
private static void SetNoclip(CCSPlayerController player, bool noclip = true)
{
if (player == null || !player.IsValid) return;
var playerPawn = player.PlayerPawn.Value;
if (playerPawn == null || !playerPawn.IsValid || player.LifeState != (byte)LifeState_t.LIFE_ALIVE) return;
playerPawn.MoveType = noclip ? MoveType_t.MOVETYPE_NOCLIP : MoveType_t.MOVETYPE_WALK;
Schema.SetSchemaValue(playerPawn.Handle, "CBaseEntity", "m_nActualMoveType", (int)playerPawn.MoveType);
Utilities.SetStateChanged(playerPawn, "CBaseEntity", "m_MoveType");
}
private static void StopFlying(CCSPlayerController player, PlayerSkillInfo skillInfo)
{
skillInfo.Timer?.Kill();
skillInfo.Timer = null;
if (!skillInfo.IsFlying) return;
skillInfo.IsFlying = false;
if (player == null || !player.IsValid || player.LifeState != (byte)LifeState_t.LIFE_ALIVE) return;
SetNoclip(player, false);
var playerPawn = player.PlayerPawn.Value;
if (playerPawn == null || !playerPawn.IsValid || skillInfo.IsFlying) return;
Vector? safePoint = GetCorrectPosition(player, skillInfo);
playerPawn.Teleport(safePoint ?? skillInfo.LastPosition, null, new Vector(0, 0, 0));
}
private static Vector? GetCorrectPosition(CCSPlayerController player, PlayerSkillInfo skillInfo)
{
var playerPawn = player.PlayerPawn.Value;
if (playerPawn == null || !playerPawn.IsValid || playerPawn.AbsOrigin == null) return null;
Vector currentPos = playerPawn.AbsOrigin;
float offset = 50;
Vector[] checkOffsets =
{
currentPos,
currentPos + new Vector(offset, 0, 10),
currentPos + new Vector(-offset, 0, 10),
currentPos + new Vector(0, offset, 10),
currentPos + new Vector(0, -offset, 10),
currentPos + new Vector(offset, 0, 60),
currentPos + new Vector(-offset, 0, 60),
currentPos + new Vector(0, offset, 60),
currentPos + new Vector(0, -offset, 60),
};
ulong mask = playerPawn.Collision.CollisionAttribute.InteractsWith;
ulong contents = playerPawn.Collision.CollisionGroup;
bool hasGround = false;
Vector stuckVector = new(currentPos.X, currentPos.Y, currentPos.Z);
foreach (Vector targetPos in checkOffsets)
{
Vector start = new(targetPos.X, targetPos.Y, targetPos.Z + 70);
Vector end = new(targetPos.X, targetPos.Y, targetPos.Z - 1000);
var groundResult = RayTrace.TraceShape(player, start, end, mask, contents);
if (!groundResult.HasValue || !groundResult.Value.DidHit) continue;
Vector newPos =
groundResult.Value.EndPos.Z > targetPos.Z
? new(groundResult.Value.EndPos.X, groundResult.Value.EndPos.Y, groundResult.Value.EndPos.Z)
: targetPos;
hasGround = true;
stuckVector = newPos;
var result = RayTrace.TraceHullShape(
targetPos,
targetPos,
player
);
if (result.HasValue && !result.Value.DidHit)
return newPos;
}
if (hasGround)
skillInfo.Cooldown = DateTime.Now.AddSeconds(-SkillsInfo.GetValue<float>(skillName, "cooldown") + SkillsInfo.GetValue<float>(skillName, "cooldownWhenStuck"));
return hasGround ? stuckVector : null;
}
public class PlayerSkillInfo
{
public ulong SteamID { get; set; }
public bool CanUse { get; set; }
public bool IsFlying { get; set; }
public DateTime Cooldown { get; set; }
public Vector? LastPosition { get; set; }
public Timer? Timer { get; set; }
}
public class SkillConfig(Skills skill = skillName, bool active = true, string color = "#44ebd4", CsTeam onlyTeam = CsTeam.None, bool disableOnFreezeTime = true, bool needsTeammates = false, string requiredPermission = "", float? hudDuration = null, float? descriptionHudDuration = null, int maxPerServer = -1, Rarity rarity = Rarity.Common, float cooldown = 30f, float duration = 2f, float cooldownWhenStuck = 5f) : SkillsInfo.DefaultSkillInfo(skill, active, color, onlyTeam, disableOnFreezeTime, needsTeammates, requiredPermission, hudDuration, descriptionHudDuration, maxPerServer, rarity)
{
public float Cooldown { get; set; } = cooldown;
public float CooldownWhenStuck { get; set; } = cooldownWhenStuck;
public float Duration { get; set; } = duration;
}
}
}