using System.Numerics; using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core;…
- User
using System.Numerics; using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Entities.Constants; using CounterStrikeSharp.API.Modules.Memory; using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions; using CounterStrikeSharp.API.Modules.Timers; using CounterStrikeSharp.API.Modules.Utils; using CustomRounds.Attributes; using Microsoft.Extensions.Logging; using Timer = CounterStrikeSharp.API.Modules.Timers.Timer; using Vector = CounterStrikeSharp.API.Modules.Utils.Vector;
namespace CustomRounds.Rounds;
[CustomRound(Name = "Test")] public class ChickenRound : ICustomRound { private readonly Plugin _plugin; private readonly ILogger _logger; private readonly List _chickens = [];
public ChickenRound(Plugin plugin, ILogger<ChickenRound> logger) { _plugin = plugin; _logger = logger; plugin.AddCommand("css_testc", "", (player, info) => CreateChicken()); } // 421, 2688, 256 | 11, -7, 0 // 402, 2850, 256 | 13, 75, 0 // 250, 2852, 256 | 17, 130, 0 // 270, 2691, 256 | 20, -134, 0 private readonly Vector[] _positions = [ new Vector(750, 2684, 180), new Vector(456, 3269, 210), new Vector(272, 3175, 220) ]; private readonly Vector[] _spawnPoints = [ new Vector(421, 2688, 256), new Vector(402, 2850, 256), new Vector(250, 2852, 256), new Vector(270, 2691, 256), ]; private const int MaxChickens = 100; private int _currentChicken; private float _spawnRate; private float _spawnInterval = 5; private CDynamicProp? _targetEntity; private Timer? _spawnTimer; private readonly List<Timer?> _gayTimer = []; public void Start() { //VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnTakeDamage, HookMode.Pre); var players = Utilities.GetPlayers(); foreach (var (player, spawnPoint) in players.Zip(_spawnPoints, (p, s) => (p, s))) { var playerPawn = player.PlayerPawn.Value; if (playerPawn is null) continue; playerPawn.Teleport(spawnPoint); } _plugin.RegisterEvent<EventRoundFreezeEnd>((@event, info) => { _targetEntity = Utilities.CreateEntityByName<CDynamicProp>("prop_dynamic"); if (_targetEntity is not null) { _targetEntity.DispatchSpawn(); _targetEntity.RenderMode = RenderMode_t.kRenderNone; _targetEntity.AbsOrigin.X = 345; _targetEntity.AbsOrigin.Y = 2770; _targetEntity.AbsOrigin.Z = 248; } Server.PrintToChatAll($"CHICKEN ROUND START!!!"); _spawnTimer = _plugin.AddTimer(_spawnInterval, Callback); return HookResult.Continue; }); } private HookResult OnTakeDamage(DynamicHook hook) { var entityInstance = hook.GetParam<CEntityInstance>(0); if (!entityInstance.DesignerName.Contains("chicken")) { Server.PrintToConsole($"entity instance designer name: {entityInstance.DesignerName}"); return HookResult.Continue; } Server.PrintToConsole($"2 entity instance designer name: {entityInstance.DesignerName}"); var info = hook.GetParam<CTakeDamageInfo>(1); if (info.Attacker.Value?.DesignerName.Contains("chicken") == false) { Server.PrintToConsole($"attacker designer name: {info.Attacker.Value?.DesignerName}"); return HookResult.Continue; } Server.PrintToConsole($"2 attacker designer name: {info.Attacker.Value?.DesignerName}"); info.Damage = 0; return HookResult.Changed; } private void Callback() { VirtualFunctions.ClientPrintAll(HudDestination.Center, $"Kуриц осталось: {MaxChickens - _currentChicken}", 0, 0, 0, 0); _spawnInterval *= 0.9f; if (_spawnInterval < 1) _spawnInterval = 1; _spawnRate += 0.1f; if (_currentChicken >= MaxChickens) { End(); } for (int i = 0; i < _spawnRate; i++) { CreateChicken(); } _spawnTimer = _plugin.AddTimer(_spawnInterval, Callback); } private void CreateChicken() { var chicken = Utilities.CreateEntityByName<CChicken>("chicken"); if (chicken is null) return; var raw = _targetEntity?.EntityHandle.Raw; if (raw is null) return; chicken.Leader.Raw = raw.Value; chicken.DispatchSpawn(); chicken.Leader.Raw = raw.Value; var position = _positions[Random.Shared.Next(_positions.Length)]; chicken.Teleport(position); var explosion = Utilities.CreateEntityByName<CEnvExplosion>("env_explosion"); if (explosion != null) { explosion.RadiusOverride = 250; explosion.Magnitude = 1000; explosion.PlayerDamage = 1000; explosion.DamageForce = 1000; explosion.DispatchSpawn(); Server.NextFrame(() => chicken.AcceptInput("SetParent", chicken, explosion, "!activator")); } var timer = _plugin.AddTimer(0.5f, () => { if (!chicken.IsValid || !_targetEntity!.IsValid) return; var min = Utilities.GetPlayers() .Select(u => Vector3.Distance(chicken.AbsOrigin!.ToVector3(), u.PlayerPawn.Value.AbsOrigin!.ToVector3())).Min(); _logger.LogInformation($"Distance: {min}"); if (min < 140) { _logger.LogError("min"); if (explosion is null || !explosion.IsValid) return; _logger.LogError("Explode"); explosion.AcceptInput("Explode"); } }, TimerFlags.REPEAT | TimerFlags.STOP_ON_MAPCHANGE); _gayTimer.Add(timer); _currentChicken++; _chickens.Add(chicken); } public void End() { _spawnTimer?.Kill(); foreach (var timer in _gayTimer) { timer?.Kill(); } if (_targetEntity?.IsValid == true) { _targetEntity.Remove(); } //VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(OnTakeDamage, HookMode.Pre); foreach (var chicken in _chickens.Where(cChicken => cChicken.IsValid)) { chicken.Remove(); } } - Assistant
To kill the timer when the explosion event occurs, you can modify the lambda function inside
AddTimerto include a method call totimer.Kill()when the explosion happens. Here's how you can achieve that:var timer = _plugin.AddTimer(0.5f, () => { if (!chicken.IsValid || !_targetEntity!.IsValid) return; var min = Utilities.GetPlayers() .Select(u => Vector3.Distance(chicken.AbsOrigin!.ToVector3(), u.PlayerPawn.Value.AbsOrigin!.ToVector3())).Min(); _logger.LogInformation($"Distance: {min}"); if (min < 140) { _logger.LogError("min"); if (explosion is null || !explosion.IsValid) return; _logger.LogError("Explode"); explosion.AcceptInput("Explode"); // Kill the timer now that the explosion has triggered timer?.Kill(); } }, TimerFlags.REPEAT | TimerFlags.STOP_ON_MAPCHANGE);