Skip to content

Commit

Permalink
feat: save zone message
Browse files Browse the repository at this point in the history
fix: duplicate messages
fix: duplicate bulletimpacts (wallbang)
  • Loading branch information
oscar-wos committed Jun 27, 2024
1 parent 387c7ca commit db06e84
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Menu
17 changes: 10 additions & 7 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
"both": "Both",
"missingPermission": "Missing permission: {red}{0}",
"invalidInput": "Invalid input: '{lightred}{0}{default}' ({orange}{1}{default})",
"delayRemaining": "{0} in {1} second(s)",
"saving": "Saving zone '{0}' {1}",
"zone.Bounce": "Bounce",
"zone.Hurt": "Hurt",
"zone.Kill": "Kill",
"zone.Teleport": "Teleport",
"menu.Add": "Add Zone",
"menu.View": "View Zones",
"menu.Debug": "Debug: ",
"menu.Type": "Type: ",
"menu.Teams": "Teams: ",
"menu.Name": "Name: ",
"menu.Delay": "Delay: ",
"menu.Damage": "Damage: ",
"menu.Seconds": " Second(s)",
"menu.Debug": "Debug:",
"menu.Type": "Type:",
"menu.Teams": "Teams:",
"menu.Name": "Name:",
"menu.Delay": "Delay:",
"menu.Damage": "Damage:",
"menu.Seconds": "second(s)",
"menu.PerSecond": "per second",
"menu.Save": "Save",
"menu.Shoot": "Shoot point: {0}"
}
99 changes: 80 additions & 19 deletions src/AntiRush.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using AntiRush.Enums;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using AntiRush.Enums;

namespace AntiRush;

Expand Down Expand Up @@ -29,17 +29,6 @@ public override void Load(bool isReload)
Server.ExecuteCommand("mp_restartgame 1");
}

private void BouncePlayer(CCSPlayerController controller)
{
var pos = _playerData[controller].LastPosition;
var vel = _playerData[controller].LastVelocity;
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, controller.PlayerPawn!.Value.EyeAngles, vel);
}

private void SaveZone(CCSPlayerController controller)
{
var menu = _playerData[controller].AddZone;
Expand All @@ -52,17 +41,89 @@ private void SaveZone(CCSPlayerController controller)
_ => []
};

var zoneType = (ZoneType)menu.Items[0].Option;
var minPoint = new Vector(Math.Min(menu.Points[0].X, menu.Points[1].X), Math.Min(menu.Points[0].Y, menu.Points[1].Y), Math.Min(menu.Points[0].Z, menu.Points[1].Z));
var maxPoint = new Vector(Math.Max(menu.Points[0].X, menu.Points[1].X), Math.Max(menu.Points[0].Y, menu.Points[1].Y), Math.Max(menu.Points[0].Z, menu.Points[1].Z));
var delay = zoneType != ZoneType.Bounce && float.TryParse(menu.Items[3].DataString, out var valueDelay) ? valueDelay : 0;
var damage = zoneType == ZoneType.Hurt && int.TryParse(menu.Items[4].DataString, out var valueDamage) ? valueDamage : 10;
var name = menu.Items[2].DataString;

if (!float.TryParse(menu.Items[3].DataString, out var delay))
delay = 0.0f;
if (name.Length == 0)
name = "noname";

if (!int.TryParse(menu.Items[4].DataString, out var damage))
damage = 10;

var zone = new Zone((ZoneType)menu.Items[0].Option, teams, minPoint, maxPoint, menu.Items[2].DataString, delay, damage);
var zone = new Zone(zoneType, teams, minPoint, maxPoint, name, delay, damage);
_zones.Add(zone);

var printMessage = $"{Prefix}{Localizer["saving", name, FormatZoneString(zoneType)]} | {Localizer["menu.Teams"]} [";

if (teams.Contains(CsTeam.Terrorist))
printMessage += $" {ChatColors.LightYellow}{Localizer["t"]}{ChatColors.White}";

if (teams.Contains(CsTeam.CounterTerrorist))
printMessage += $" {ChatColors.Blue}{Localizer["ct"]}{ChatColors.White}";

printMessage += " ]";

if (zoneType != ZoneType.Bounce)
printMessage += $" | {Localizer["menu.Delay"]} {ChatColors.Green}{delay}{ChatColors.White}";

if (zoneType == ZoneType.Hurt)
printMessage += $" | {Localizer["menu.Damage"]} {ChatColors.Green}{damage}{ChatColors.White}";

controller.PrintToChat(printMessage);
}

private bool DoAction(CCSPlayerController controller, Zone zone)
{
if (zone.Type != ZoneType.Bounce && Server.CurrentTime - _playerData[controller].LastMessage > 1)
controller!.PrintToChat($"{Prefix}{FormatZoneString(zone.Type)}");

_playerData[controller].LastMessage = Server.CurrentTime;

switch (zone.Type)
{
case ZoneType.Bounce:
var speed = Math.Sqrt(_playerData[controller].LastVelocity.X * _playerData[controller].LastVelocity.X + _playerData[controller].LastVelocity.Y * _playerData[controller].LastVelocity.Y);

_playerData[controller].LastVelocity *= (-350 / (float)speed);
_playerData[controller].LastVelocity.Z = _playerData[controller].LastVelocity.Z <= 0f ? 150f : Math.Min(_playerData[controller].LastVelocity.Z, 150f);
controller.PlayerPawn.Value!.Teleport(_playerData[controller].LastPosition, controller.PlayerPawn!.Value.EyeAngles, _playerData[controller].LastVelocity);
return true;

case ZoneType.Hurt:
if (Server.CurrentTime % 1 != 0)
return false;

controller.PlayerPawn.Value!.Health -= zone.Damage;
Utilities.SetStateChanged(controller.PlayerPawn.Value, "CBaseEntity", "m_iHealth");

if (controller.PlayerPawn.Value.Health <= 0)
controller.PlayerPawn.Value.CommitSuicide(true, true);

return false;

case ZoneType.Kill:
controller.PlayerPawn.Value!.CommitSuicide(true, true);
return false;

case ZoneType.Teleport:
controller.PlayerPawn.Value!.Teleport(_playerData[controller].SpawnPos, controller.PlayerPawn!.Value.EyeAngles, Vector.Zero);
return false;
}

return false;
}

private string FormatZoneString(ZoneType type)
{
return type switch
{
ZoneType.Bounce => $"{ChatColors.Yellow}{Localizer["zone.Bounce"]}{ChatColors.White}",
ZoneType.Hurt => $"{ChatColors.Orange}{Localizer["zone.Hurt"]}{ChatColors.White}",
ZoneType.Kill => $"{ChatColors.Red}{Localizer["zone.Kill"]}{ChatColors.White}",
ZoneType.Teleport => $"{ChatColors.Magenta}{Localizer["zone.Teleport"]}{ChatColors.White}",
_ => ""
};
}

private static bool IsValidPlayer(CCSPlayerController? player)
Expand Down
4 changes: 2 additions & 2 deletions src/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using AntiRush.Enums;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using AntiRush.Enums;

namespace AntiRush;

Expand Down
8 changes: 7 additions & 1 deletion src/Events.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;

namespace AntiRush;
Expand Down Expand Up @@ -30,6 +31,11 @@ private HookResult OnBulletImpact(EventBulletImpact @event, GameEventInfo info)
if (!value.AddZone.Points[0].IsZero() && !value.AddZone.Points[1].IsZero())
return HookResult.Continue;

if (Server.CurrentTime - value.AddZone.LastShot < 0.1)
return HookResult.Continue;

value.AddZone.LastShot = Server.CurrentTime;

if (value.AddZone.Points[0].IsZero())
value.AddZone.Points[0] = new Vector(@event.X, @event.Y, @event.Z);
else if (value.AddZone.Points[1].IsZero())
Expand Down
5 changes: 1 addition & 4 deletions src/Json.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Logging;
using System.Text.Json.Nodes;
using System.Text.Json;
using Microsoft.Extensions.Logging;

namespace AntiRush;

Expand Down
70 changes: 14 additions & 56 deletions src/Listeners.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Diagnostics;
using AntiRush.Enums;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Utils;

namespace AntiRush;
Expand All @@ -12,91 +9,52 @@ private void OnTick()
{
foreach (var controller in Utilities.GetPlayers().Where(player => player is { IsValid: true, PawnIsAlive: true }))
{
var pos = controller!.PlayerPawn.Value!.AbsOrigin!;
var bounce = false;

foreach (var zone in _zones)
{
var isInZone = zone.IsInZone(pos);
var isInZone = zone.IsInZone(controller!.PlayerPawn.Value!.AbsOrigin!);

if (!isInZone)
{
zone.Entry.Remove(controller);
continue;
}

if (!zone.Entry.ContainsKey(controller))
zone.Entry[controller] = Server.CurrentTime;

if (!zone.Teams.Contains(controller.Team))
continue;

if (zone.Delay != 0 && !zone.Entry.ContainsKey(controller))
zone.Entry[controller] = Server.CurrentTime;

if (zone.Delay != 0)
{
var diff = (zone.Entry[controller] + zone.Delay) - Server.CurrentTime;

if (diff > 0)
{
if (diff % 1 == 0)
controller!.PrintToChat($"{Prefix} {FormatZoneString(zone.Type)} in {diff} seconds");
var diffString = diff % 1;

if (diffString.ToString("0.00") is ("0.00" or "0.01") && diff >= 1.0)
controller!.PrintToChat($"{Prefix}{Localizer["delayRemaining", FormatZoneString(zone.Type), diff.ToString("0")]}");
}
else
DoAction(controller, zone);
bounce = DoAction(controller, zone);

continue;
}

DoAction(controller, zone);
bounce = DoAction(controller, zone);
}

if (bounce)
continue;

_playerData[controller].LastPosition = controller!.PlayerPawn.Value!.AbsOrigin!;
_playerData[controller].LastVelocity = controller.PlayerPawn.Value.AbsVelocity;
}
}

private void DoAction(CCSPlayerController controller, Zone zone)
{
switch (zone.Type)
{
case ZoneType.Bounce:
BouncePlayer(controller);
break;

case ZoneType.Hurt:
if (Server.CurrentTime % 1 == 0)
{
controller.PlayerPawn.Value!.Health -= zone.Damage;
Utilities.SetStateChanged(controller.PlayerPawn.Value, "CBaseEntity", "m_iHealth");

if (controller.PlayerPawn.Value.Health <= 0)
controller.PlayerPawn.Value.CommitSuicide(true, true);
}

break;

case ZoneType.Kill:
controller.PlayerPawn.Value!.CommitSuicide(true, true);
break;

case ZoneType.Teleport:
controller.PlayerPawn.Value!.Teleport(_playerData[controller].SpawnPos, controller.PlayerPawn!.Value.EyeAngles, Vector.Zero);
break;
_playerData[controller].LastPosition = new Vector(controller!.PlayerPawn.Value!.AbsOrigin!.X, controller!.PlayerPawn.Value!.AbsOrigin!.Y, controller!.PlayerPawn.Value!.AbsOrigin!.Z);
_playerData[controller].LastVelocity = new Vector(controller.PlayerPawn.Value.AbsVelocity.X, controller.PlayerPawn.Value.AbsVelocity.Y, controller.PlayerPawn.Value.AbsVelocity.Z);
}
}

private string FormatZoneString(ZoneType type)
{
return type switch
{
ZoneType.Hurt => $"{ChatColors.Orange}{Localizer["zone.Hurt"]}{ChatColors.White}",
ZoneType.Kill => $"{ChatColors.Red}{Localizer["zone.Kill"]}{ChatColors.White}",
ZoneType.Teleport => $"{ChatColors.Magenta}{Localizer["zone.Teleport"]}{ChatColors.White}",
};
}

private void OnMapStart(string mapName)
{
LoadJson(mapName);
Expand Down
19 changes: 11 additions & 8 deletions src/Menus.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using AntiRush.Enums;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Utils;
using Menu;
using Menu.Enums;
using AntiRush.Enums;

namespace AntiRush;

Expand All @@ -19,6 +19,7 @@ private void BuildMenu(CCSPlayerController controller, MenuType type = MenuType.
for (var i = 0; i < 2; i++)
_playerData[controller].AddZone!.Points[i] = Vector.Zero;

_playerData[controller].AddZone!.LastShot = 0;
_playerData[controller].AddZone = null;
}

Expand Down Expand Up @@ -51,7 +52,7 @@ private void BuildMainMenu(CCSPlayerController controller, bool updateMenu = fal

mainMenu.AddItem(new MenuItem(MenuItemType.Button, customButtons));
mainMenu.AddItem(new MenuItem(MenuItemType.Spacer));
mainMenu.AddItem(new MenuItem(MenuItemType.Bool, new MenuValue(Localizer["menu.Debug"])) { Data = [_playerData[controller].Debug ? 1 : 0] });
mainMenu.AddItem(new MenuItem(MenuItemType.Bool, new MenuValue($"{Localizer["menu.Debug"]} ")) { Data = [_playerData[controller].Debug ? 1 : 0] });

if (_playerData[controller].Debug)
{
Expand Down Expand Up @@ -124,16 +125,16 @@ private void BuildAddZoneMenu(CCSPlayerController controller)
new(Localizer["both"]) { Prefix = "<font color=\"#C2AD7D\">", Suffix = "<font color=\"#FFFFFF\">" }
};

addZoneMenu.AddItem(new MenuItem(MenuItemType.Choice, new MenuValue(Localizer["menu.Type"]), zoneTypes));
addZoneMenu.AddItem(new MenuItem(MenuItemType.Choice, new MenuValue(Localizer["menu.Teams"]), teams, true));
addZoneMenu.AddItem(new MenuItem(MenuItemType.Input, new MenuValue(Localizer["menu.Name"])));
addZoneMenu.AddItem(new MenuItem(MenuItemType.Choice, new MenuValue($"{Localizer["menu.Type"]} "), zoneTypes));
addZoneMenu.AddItem(new MenuItem(MenuItemType.Choice, new MenuValue($"{Localizer["menu.Teams"]} "), teams, true));
addZoneMenu.AddItem(new MenuItem(MenuItemType.Input, new MenuValue($"{Localizer["menu.Name"]} ")));

addZoneMenu.AddItem(_playerData[controller].AddZone!.Items[0].Option != 0
? new MenuItem(MenuItemType.Input, new MenuValue(Localizer["menu.Delay"]), new MenuValue(Localizer["menu.Seconds"]))
? new MenuItem(MenuItemType.Input, new MenuValue($"{Localizer["menu.Delay"]} "), new MenuValue($" {Localizer["menu.Seconds"]}"))
: new MenuItem(MenuItemType.Spacer));

addZoneMenu.AddItem(_playerData[controller].AddZone!.Items[0].Option == 1
? new MenuItem(MenuItemType.Input, new MenuValue(Localizer["menu.Damage"]))
? new MenuItem(MenuItemType.Input, new MenuValue($"{Localizer["menu.Damage"]} "), new MenuValue($" {Localizer["menu.PerSecond"]}"))
: new MenuItem(MenuItemType.Spacer));

addZoneMenu.AddItem(new MenuItem(MenuItemType.Button, [new CustomButton(Localizer["menu.Save"], SaveZone) { Prefix = "<font color=\"#ADD8E6\">" }]));
Expand All @@ -146,6 +147,7 @@ private void BuildAddZoneMenu(CCSPlayerController controller)
{
addZoneMenu.Points = _playerData[controller].AddZone!.Points;
addZoneMenu.Option = _playerData[controller].AddZone!.Option;
addZoneMenu.LastShot = _playerData[controller].AddZone!.LastShot;

for (var i = 0; i < _playerData[controller].AddZone!.Items.Count; i++)
{
Expand Down Expand Up @@ -202,4 +204,5 @@ public class CustomButton(string value, Action<CCSPlayerController> callback) :
public class AddZoneMenu(MenuValue title) : MenuBase(title)
{
public Vector[] Points { get; set; } = [Vector.Zero, Vector.Zero];
public float LastShot { get; set; }
}
1 change: 1 addition & 0 deletions src/PlayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class PlayerData
public bool Debug { get; set; }
public bool[] DebugOptions { get; set; } = new bool[Enum.GetValues(typeof(ZoneType)).Length];
public AddZoneMenu? AddZone { get; set; } = null;
public float LastMessage { get; set; }
}
1 change: 0 additions & 1 deletion src/Zone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using CounterStrikeSharp.API.Modules.Utils;
using AntiRush.Enums;


namespace AntiRush;

public class Zone(ZoneType type, CsTeam[] teams, Vector minPoint, Vector maxPoint, string name, float delay, int damage)
Expand Down

0 comments on commit db06e84

Please sign in to comment.