Skip to content

Commit

Permalink
refactor: new logic
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-wos committed Nov 9, 2024
1 parent 4dc9f02 commit 9a0d747
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 114 deletions.
27 changes: 27 additions & 0 deletions src/ControllerExtends.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using CounterStrikeSharp.API.Core;

namespace Zones;

public static class ControllerExtends
{
public static bool IsValid(this CCSPlayerController? controller, bool checkBot = false)
{
if (checkBot)
return controller is { IsValid: true, IsBot: false };

return controller is { IsValid: true };
}

public static void Bounce(this CCSPlayerController? controller)
{
if (controller == null || !controller.IsValid() || controller.PlayerPawn.Value == null)
return;

var vel = controller.PlayerPawn.Value.AbsVelocity;
var speed = Math.Sqrt(vel.X * vel.X + vel.Y * vel.Y);

vel *= (-350 / (float)speed);
vel.Z = vel.Z <= 0 ? 150 : Math.Min(vel.Z, 150);
controller.PlayerPawn.Value.Teleport(controller.PlayerPawn.Value.AbsOrigin, controller.PlayerPawn.Value.EyeAngles, vel);
}
}
2 changes: 1 addition & 1 deletion src/ZoneType.cs → src/Enums/ZoneType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Zones;
namespace Zones.Enums;

public enum ZoneType
{
Expand Down
6 changes: 1 addition & 5 deletions src/Events.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CounterStrikeSharp.API;
using RetakesPluginShared.Events;
using RetakesPluginShared.Events;

namespace Zones;

Expand All @@ -9,8 +8,5 @@ private void OnRetakesEvent(object? sender, IRetakesPluginEvent @event)
{
if (@event is not AnnounceBombsiteEvent announceBombsiteEvent)
return;

foreach (var player in Utilities.GetPlayers().Where(IsValidPlayer))
LoadPlayerZones(player, announceBombsiteEvent.Bombsite);
}
}
8 changes: 1 addition & 7 deletions src/Globals.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
using CounterStrikeSharp.API.Core.Capabilities;
using CounterStrikeSharp.API.Modules.Utils;
using RetakesPluginShared;

namespace Zones;

public partial class Zones
{
public override string ModuleName => "Retakes-Zones";
public override string ModuleVersion => "1.0.2";
public override string ModuleVersion => "1.1.0";
public override string ModuleAuthor => "https://github.com/oscar-wos/Retakes-Zones";
private static PluginCapability<IRetakesPluginEventSender> RetakesPluginEventSenderCapability { get; } = new("retakes_plugin:event_sender");

private readonly List<Zone> _zones = [];
private readonly Dictionary<int, List<Zone>> _playerZones = [];
private readonly Dictionary<int, List<Zone>> _playerGreenZones = [];
private readonly Dictionary<int, Vector> _playerLastPosition = [];
private readonly Dictionary<int, Vector> _playerLastVelocity = [];
}
22 changes: 11 additions & 11 deletions src/Json.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
using System.Text.Json;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Logging;
using RetakesPluginShared.Enums;
using Zones.Enums;

namespace Zones;

public partial class Zones
{
public void LoadJson(string mapName)
private void LoadJson(string mapName)
{
_zones.Clear();
var path = $"../../csgo/addons/counterstrikesharp/configs/plugins/Zones/{mapName}.json";

if (!File.Exists(path))
{
Logger.LogError($"File {path} does not exist.");
return;
}

var json = File.ReadAllText(path);
var obj = JsonSerializer.Deserialize<JsonBombsite>(json);

foreach (var zone in obj!.a)
if (obj == null)
return;

foreach (var zone in obj.a)
AddZone(Bombsite.A, zone);

foreach (var zone in obj!.b)
foreach (var zone in obj.b)
AddZone(Bombsite.B, zone);

return;
Expand All @@ -35,17 +35,17 @@ void AddZone(Bombsite bombsite, JsonZone zone)
bombsite,
(ZoneType)zone.type,
zone.teams.Select(t => (CsTeam)Enum.ToObject(typeof(CsTeam), t)).ToArray(),
new Vector(Math.Min(zone.x[0], zone.y[0]), Math.Min(zone.x[1], zone.y[1]), Math.Min(zone.x[2], zone.y[2])),
new Vector(Math.Max(zone.x[0], zone.y[0]), Math.Max(zone.x[1], zone.y[1]), Math.Max(zone.x[2], zone.y[2]))
[Math.Min(zone.x[0], zone.y[0]), Math.Min(zone.x[1], zone.y[1]), Math.Min(zone.x[2], zone.y[2])],
[Math.Max(zone.x[0], zone.y[0]), Math.Max(zone.x[1], zone.y[1]), Math.Max(zone.x[2], zone.y[2])]
));
}
}
}

public class JsonBombsite
{
public required JsonZone[] a { get; set; }
public required JsonZone[] b { get; set; }
public required JsonZone[] a { get; init; }
public required JsonZone[] b { get; init; }
}

public class JsonZone
Expand Down
14 changes: 13 additions & 1 deletion src/Listeners.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API;

namespace Zones;

public partial class Zones
{
private void OnTick()
{
}

private void OnMapStart(string mapName)
{
LoadJson(mapName);
}

private void OnClientPutInServer(int playerSlot)
{
var controller = Utilities.GetPlayerFromSlot(playerSlot);

if (controller == null || !controller.IsValid())
return;
}
}
9 changes: 5 additions & 4 deletions src/Zone.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using CounterStrikeSharp.API.Modules.Utils;
using RetakesPluginShared.Enums;
using Zones.Enums;

namespace Zones;

public class Zone(Bombsite bombsite, ZoneType type, CsTeam[] teams, Vector minPoint, Vector maxPoint)
public class Zone(Bombsite bombsite, ZoneType type, CsTeam[] teams, float[] minPoint, float[] maxPoint)
{
public Bombsite Bombsite { get; init; } = bombsite;
public ZoneType Type { get; init; } = type;
public CsTeam[] Teams { get; init; } = teams;
public Vector MinPoint { get; init; } = minPoint;
public Vector MaxPoint { get; init; } = maxPoint;
private float[] MinPoint { get; } = minPoint;
private float[] MaxPoint { get; } = maxPoint;

public bool IsInZone(Vector point)
{
return point.X >= MinPoint.X && point.X <= MaxPoint.X && point.Y >= MinPoint.Y && point.Y <= MaxPoint.Y && point.Z >= MinPoint.Z && point.Z <= MaxPoint.Z;
return point.X >= MinPoint[0] && point.X <= MaxPoint[0] && point.Y >= MinPoint[1] && point.Y <= MaxPoint[1] && point.Z >= MinPoint[2] && point.Z <= MaxPoint[2];
}
}
87 changes: 2 additions & 85 deletions src/Zones.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Utils;
using RetakesPluginShared.Enums;

namespace Zones;

public partial class Zones : BasePlugin
{
public override void Load(bool isReload)
{
RegisterListener<Listeners.OnTick>(OnTick);
RegisterListener<Listeners.OnMapStart>(OnMapStart);
AddTimer(0.1f, Timer_Repeat, TimerFlags.REPEAT);
RegisterListener<Listeners.OnClientPutInServer>(OnClientPutInServer);

if (!isReload)
return;
Expand All @@ -23,85 +21,4 @@ public override void OnAllPluginsLoaded(bool isReload)
{
RetakesPluginEventSenderCapability.Get()!.RetakesPluginEventHandlers += OnRetakesEvent;
}

private void LoadPlayerZones(CCSPlayerController player, Bombsite bombsite)
{
_playerZones.Remove(player.Slot);
_playerLastPosition.Remove(player.Slot);
_playerLastVelocity.Remove(player.Slot);
_playerGreenZones.Remove(player.Slot);

var playerZones = _zones.Where(z => z.Bombsite == bombsite && z.Teams.Contains((CsTeam)player!.PlayerPawn.Value!.TeamNum)).ToList();
_playerZones.Add(player.Slot, playerZones);
_playerLastPosition.Add(player.Slot, player!.PlayerPawn.Value!.AbsOrigin!);
_playerLastVelocity.Add(player.Slot, player!.PlayerPawn.Value!.AbsVelocity!);
_playerGreenZones.Add(player.Slot, []);
}

private void Timer_Repeat()
{
foreach (var player in Utilities.GetPlayers().Where(IsValidPlayer))
{
if (!_playerZones.TryGetValue(player.Slot, out var zones))
continue;

var pos = player!.PlayerPawn.Value!.AbsOrigin;
var vel = player.PlayerPawn.Value!.AbsVelocity;
var eyes = player.PlayerPawn.Value!.EyeAngles;
var teleport = false;

foreach (var zone in zones)
{
var isInZone = zone.IsInZone(pos!);

if (zone.Type == ZoneType.Red && isInZone)
{
teleport = true;
BouncePlayer(player, eyes);
continue;
}

if (zone.Type != ZoneType.Green)
continue;

switch (isInZone)
{
case true when !_playerGreenZones[player.Slot].Contains(zone):
_playerGreenZones[player.Slot].Add(zone);
break;
case false when _playerGreenZones[player.Slot].Contains(zone):
_playerGreenZones[player.Slot].Remove(zone);

if (_playerGreenZones[player.Slot].Count == 0)
{
teleport = true;
BouncePlayer(player, eyes);
}
break;
}
}

if (teleport)
continue;

_playerLastPosition[player.Slot] = pos!;
_playerLastVelocity[player.Slot] = vel!;
}
}

private void BouncePlayer(CCSPlayerController controller, QAngle eyes)
{
var pos = _playerLastPosition[controller.Slot];
var vel = _playerLastVelocity[controller.Slot];
var speed = Math.Sqrt(vel.X * vel.X + vel.Y * vel.Y);

vel *= (-350 / (float)speed);
vel.Z = vel.Z <= 0f ? 150f : Math.Max(vel.Z, 150f);
controller.PlayerPawn.Value!.Teleport(pos, eyes, vel);
}

private static bool IsValidPlayer(CCSPlayerController? player)
{
return player != null && player is { IsValid: true, Connected: PlayerConnectedState.PlayerConnected, PawnIsAlive: true, IsBot: false, IsHLTV: false };
}
}

0 comments on commit 9a0d747

Please sign in to comment.