Skip to content

Commit

Permalink
Add more HNS customization support
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Aug 27, 2024
1 parent e58d12d commit 828ee97
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 86 deletions.
2 changes: 1 addition & 1 deletion mod/Jailbreak.LastGuard/LastGuard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class LastGuard(ILGLocale notifications, ILastRequestManager lrManager)

public readonly FakeConVar<string> CvLGWeapon = new("css_jb_lg_t_weapon",
"Weapon to give remaining prisoners once LG activates", "",
ConVarFlags.FCVAR_NONE, new WeaponValidator());
ConVarFlags.FCVAR_NONE, new ItemValidator());

public readonly FakeConVar<int> CvMaxCtHealth = new("css_jb_lg_max_hp",
"Max HP that the LG can have otherwise", 125, ConVarFlags.FCVAR_NONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Jailbreak.SpecialDay.SpecialDays;

public abstract class AbstractZoneRestrictedDay : AbstractSpecialDay {
protected readonly CsTeam RestrictedTeam;
protected CsTeam RestrictedTeam;

protected readonly IList<MovementRestrictor> Restrictors =
new List<MovementRestrictor>();
Expand Down
80 changes: 54 additions & 26 deletions mod/Jailbreak.SpecialDay/SpecialDays/HideAndSeekDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,70 @@ public class HideAndSeekDay(BasePlugin plugin, IServiceProvider provider)
public readonly FakeConVar<string> CvGuardWeapons = new(
"jb_sd_hns_weapons_ct",
"List of weapons/items CTs may use, empty for no restrictions",
string.Join(",", Tag.PISTOLS.Union(Tag.UTILITY)));
string.Join(",", Tag.PISTOLS.Union(Tag.UTILITY)), ConVarFlags.FCVAR_NONE,
new ItemValidator(allowMultiple: true));

public readonly FakeConVar<string> CvPrisonerWeapons = new(
"jb_sd_hns_weapons_t",
"List of weapons/items Ts may use, empty for no restrictions", "");
"List of weapons/items Ts may use, empty for no restrictions", "",
ConVarFlags.FCVAR_NONE, new ItemValidator(allowMultiple: true));

public readonly FakeConVar<string> CvSeekerTeam = new("jb_sd_hns_seekers",
"Team to assign as seekers and restrict to armory", "t",
ConVarFlags.FCVAR_NONE, new TeamValidator(false));

public readonly FakeConVar<int> CvSeekTime = new("jb_sd_hns_seektime",
"Duration in seconds to give the hiders time to hide", 45,
ConVarFlags.FCVAR_NONE, new RangeValidator<int>(5, 120));

private CsTeam? seekerTeam => TeamUtil.FromString(CvSeekerTeam.Value);

private CsTeam? hiderTeam
=> (seekerTeam ?? CsTeam.Terrorist) == CsTeam.Terrorist ?
CsTeam.CounterTerrorist :
CsTeam.Terrorist;

public override void Setup() {
Timers[10] += () => {
foreach (var ct in PlayerUtil.FromTeam(CsTeam.CounterTerrorist))
ct.SetSpeed(1.5f);

msg.DamageWarning(15).ToTeamChat(CsTeam.CounterTerrorist);

Locale.BeginsIn(35).ToAllChat();
};
Timers[25] += () => {
foreach (var ct in PlayerUtil.FromTeam(CsTeam.CounterTerrorist)) {
ct.SetSpeed(1.25f);
EnableDamage(ct);
}
};
Timers[30] += () => {
foreach (var ct in PlayerUtil.FromTeam(CsTeam.CounterTerrorist))
ct.SetSpeed(1.1f);
Locale.BeginsIn(15).ToAllChat();
};
Timers[45] += Execute;
if (seekerTeam == null || hiderTeam == null) return;
RestrictedTeam = seekerTeam.Value;

if (CvSeekTime.Value >= 10)
Timers[10] += () => {
foreach (var ct in PlayerUtil.FromTeam(seekerTeam.Value))
ct.SetSpeed(1.5f);

msg.DamageWarning(15).ToTeamChat(seekerTeam.Value);
};
if (CvSeekTime.Value >= 25)
Timers[25] += () => {
foreach (var player in PlayerUtil.FromTeam(seekerTeam.Value)) {
player.SetSpeed(1.25f);
EnableDamage(player);
}
};

if (CvSeekTime.Value >= 30)
Timers[30] += () => {
foreach (var ct in PlayerUtil.FromTeam(seekerTeam.Value))
ct.SetSpeed(1.1f);
};

for (var offset = 15; offset < CvSeekTime.Value; offset += 15) {
var beginsIn = CvSeekTime.Value - offset;
Timers[CvSeekTime.Value - offset] +=
() => Locale.BeginsIn(beginsIn).ToAllChat();
}

Timers[CvSeekTime.Value] += Execute;

base.Setup();

foreach (var ct in PlayerUtil.FromTeam(CsTeam.CounterTerrorist))
ct.SetSpeed(2f);
foreach (var player in PlayerUtil.FromTeam(seekerTeam.Value))
player.SetSpeed(2f);
}

public override void Execute() {
if (seekerTeam == null || hiderTeam == null) return;
base.Execute();
foreach (var player in PlayerUtil.GetAlive()) {
var hp = (player.Team == CsTeam.Terrorist ?
Expand All @@ -110,8 +139,7 @@ public override void Execute() {
if (armor != -1) player.SetArmor(armor);
}

foreach (var ct in PlayerUtil.FromTeam(CsTeam.CounterTerrorist))
ct.SetSpeed(1);
foreach (var ct in PlayerUtil.FromTeam(seekerTeam.Value)) ct.SetSpeed(1);
}

public class HNSSettings : SpecialDaySettings {
Expand Down
4 changes: 2 additions & 2 deletions mod/Jailbreak.SpecialDay/SpecialDays/SpeedrunDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public class SpeedrunDay(BasePlugin plugin, IServiceProvider provider)
"css_jb_speedrun_win_weapons",
"Weapon(s) to give to the winner to kill other competitors",
"weapon_knife,weapon_negev",
customValidators: new WeaponValidator(allowMultiple: true));
customValidators: new ItemValidator(allowMultiple: true));

public static readonly FakeConVar<string> CvLosersWeapon = new(
"css_jb_speedrun_loser_weapons",
"Weapon(s) to give to the losers to use against the winner", "",
customValidators: new WeaponValidator(allowMultiple: true));
customValidators: new ItemValidator(allowMultiple: true));

public static readonly FakeConVar<bool> CvWinnerDamageable = new(
"css_jb_speedrun_winner_damageable", "Whether the winner can be damaged");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public abstract class AbstractSpecialDay(BasePlugin plugin,
/// know why you are not calling it.
/// </summary>
public virtual void Setup() {
Plugin.RegisterFakeConVars(this);
Plugin.RegisterEventHandler<EventRoundEnd>(OnEnd);

foreach (var entry in Settings.ConVarValues) {
Expand Down
19 changes: 19 additions & 0 deletions public/Jailbreak.Public/Utils/TeamUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using CounterStrikeSharp.API.Modules.Utils;

namespace Jailbreak.Public.Utils;

public static class TeamUtil {
public static CsTeam? FromString(string team) {
return team.ToLower() switch {
"0" or "n" or "none" => CsTeam.None,
"1" or "s" or "spec" or "spectator" or "spectators" or "specs" => CsTeam
.Spectator,
"2" or "t" or "ts" or "terror" or "terrorist" or "terrorists"
or "prisoner" or "prisoners" => CsTeam.Terrorist,
"3" or "ct" or "cts" or "counter" or "counterterrorist"
or "counterterrorists" or "guard"
or "guards" => CsTeam.CounterTerrorist,
_ => null
};
}
}
68 changes: 68 additions & 0 deletions public/Jailbreak.Validator/ItemValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using CounterStrikeSharp.API.Modules.Cvars.Validators;

namespace Jailbreak.Validator;

public class ItemValidator(
ItemValidator.WeaponType type = ItemValidator.WeaponType.WEAPON
| ItemValidator.WeaponType.UTILITY, bool allowEmpty = true,
bool allowMultiple = false) : IValidator<string> {
[Flags]
public enum WeaponType {
GRENADE,
UTILITY,
WEAPON,
SNIPERS,
RIFLES,
PISTOLS,
SHOTGUNS,
SMGS,
HEAVY
}

public bool Validate(string value, out string? errorMessage) {
if (value.Contains(',') && !allowMultiple) {
errorMessage = "Value cannot contain multiple values";
return false;
}

if (string.IsNullOrWhiteSpace(value)) {
errorMessage = allowEmpty ? null : "weapon cannot be empty";
return allowEmpty;
}

foreach (var weapon in value.Split(',')) {
if (string.IsNullOrWhiteSpace(weapon)) {
if (!allowEmpty) {
errorMessage = allowEmpty ? null : "weapon cannot be empty";
return allowEmpty;
}

continue;
}

errorMessage = $"invalid {nameof(type).ToLower()}: {weapon}";
return Enum.GetValues<WeaponType>()
.Where(t => (t & type) == type)
.Any(t => validateType(t, weapon));
}

errorMessage = null;
return true;
}

private bool validateType(WeaponType current, string weapon) {
var result = current switch {
WeaponType.GRENADE => Tag.GRENADES.Contains(weapon),
WeaponType.UTILITY => Tag.UTILITY.Contains(weapon),
WeaponType.WEAPON => Tag.WEAPONS.Contains(weapon),
WeaponType.SNIPERS => Tag.SNIPERS.Contains(weapon),
WeaponType.RIFLES => Tag.RIFLES.Contains(weapon),
WeaponType.PISTOLS => Tag.PISTOLS.Contains(weapon),
WeaponType.SHOTGUNS => Tag.SHOTGUNS.Contains(weapon),
WeaponType.SMGS => Tag.SMGS.Contains(weapon),
WeaponType.HEAVY => Tag.HEAVY.Contains(weapon),
_ => throw new ArgumentOutOfRangeException(nameof(weapon))
};
return result;
}
}
25 changes: 25 additions & 0 deletions public/Jailbreak.Validator/TeamValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CounterStrikeSharp.API.Modules.Cvars.Validators;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Public.Utils;

namespace Jailbreak.Validator;

public class TeamValidator(bool allowSpecs = true) : IValidator<string> {
public bool Validate(string value, out string? errorMessage) {
errorMessage = null;

var team = TeamUtil.FromString(value);

if (team == null) {
errorMessage = $"Unknown team: \"{value}\"";
return false;
}

if (team is CsTeam.None or CsTeam.Spectator && !allowSpecs) {
errorMessage = "Team must be CT or T";
return false;
}

return true;
}
}
56 changes: 0 additions & 56 deletions public/Jailbreak.Validator/WeaponValidator.cs

This file was deleted.

0 comments on commit 828ee97

Please sign in to comment.