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 SkillPlayerInfo = []; private static readonly object setLock = new(); public static void LoadSkill() { SkillUtils.RegisterSkill(skillName, SkillsInfo.GetValue(skillName, "color")); } public static void NewRound() { lock (setLock) SkillPlayerInfo.Clear(); } public static void OnTick() { foreach (var player in Utilities.GetPlayers()) { 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(skillName, "cooldown")) - DateTime.Now).TotalSeconds); cooldown = Math.Max(time, 0); float flyingTime = (int)(skillInfo.Cooldown.AddSeconds(SkillsInfo.GetValue(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", $"{Math.Round(flying / 100, 2)}")}" : $"{player.GetTranslation("hud_info", $"{cooldown}")}"; } 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(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(skillName, "cooldown") + SkillsInfo.GetValue(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 = "", 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, maxPerServer, rarity) { public float Cooldown { get; set; } = cooldown; public float CooldownWhenStuck { get; set; } = cooldownWhenStuck; public float Duration { get; set; } = duration; } } }