From 135c64b73e6f69559010dd82b314757cb4d17e8a Mon Sep 17 00:00:00 2001 From: MSWS Date: Mon, 23 Dec 2024 04:44:07 -0800 Subject: [PATCH] Add warden paint perk --- mod/Gangs.Boostrap/Gangs.Boostrap.csproj | 1 + mod/Gangs.Boostrap/GangsServiceExtension.cs | 2 + mod/Jailbreak.Rainbow/Rainbowizer.cs | 4 + mod/Jailbreak.Warden/Jailbreak.Warden.csproj | 1 + .../Paint/WardenPaintBehavior.cs | 84 ++++++++++++++++++- .../WardenColorBootstrap.cs | 11 ++- mod/WardenPaintColorPerk/WardenPaintColor.cs | 55 ++++++++++++ .../WardenPaintColorPerk.csproj | 19 +++++ .../Mod/Rainbow/IRainbowColorizer.cs | 5 +- 9 files changed, 175 insertions(+), 7 deletions(-) create mode 100644 mod/WardenPaintColorPerk/WardenPaintColor.cs create mode 100644 mod/WardenPaintColorPerk/WardenPaintColorPerk.csproj diff --git a/mod/Gangs.Boostrap/Gangs.Boostrap.csproj b/mod/Gangs.Boostrap/Gangs.Boostrap.csproj index 68da4bd..cc29a96 100644 --- a/mod/Gangs.Boostrap/Gangs.Boostrap.csproj +++ b/mod/Gangs.Boostrap/Gangs.Boostrap.csproj @@ -13,6 +13,7 @@ + diff --git a/mod/Gangs.Boostrap/GangsServiceExtension.cs b/mod/Gangs.Boostrap/GangsServiceExtension.cs index 90178b9..d9207e3 100644 --- a/mod/Gangs.Boostrap/GangsServiceExtension.cs +++ b/mod/Gangs.Boostrap/GangsServiceExtension.cs @@ -9,6 +9,7 @@ using Jailbreak.Public; using Jailbreak.Public.Behaviors; using Jailbreak.Public.Extensions; using Microsoft.Extensions.DependencyInjection; +using WardenPaintColorPerk; namespace Gangs.Boostrap; @@ -29,5 +30,6 @@ public class GangsInit : IPluginBehavior { _ = new LRColorBootstrap(services); _ = new WardenIconBootstrap(services); _ = new SpecialIconBootstrap(services); + _ = new WardenColorBootstrap(services); } } \ No newline at end of file diff --git a/mod/Jailbreak.Rainbow/Rainbowizer.cs b/mod/Jailbreak.Rainbow/Rainbowizer.cs index 3102059..e1d3b99 100644 --- a/mod/Jailbreak.Rainbow/Rainbowizer.cs +++ b/mod/Jailbreak.Rainbow/Rainbowizer.cs @@ -32,6 +32,10 @@ public class Rainbowizer : IRainbowColorizer { colorTimer = null; } + public Color GetRainbow() { + return calculateColor(DateTime.Now - DateTime.UnixEpoch); + } + private void tick() { foreach (var (slot, start) in rainbowTimers) { var player = Utilities.GetPlayerFromSlot(slot); diff --git a/mod/Jailbreak.Warden/Jailbreak.Warden.csproj b/mod/Jailbreak.Warden/Jailbreak.Warden.csproj index ff71104..9cd791f 100644 --- a/mod/Jailbreak.Warden/Jailbreak.Warden.csproj +++ b/mod/Jailbreak.Warden/Jailbreak.Warden.csproj @@ -13,6 +13,7 @@ + diff --git a/mod/Jailbreak.Warden/Paint/WardenPaintBehavior.cs b/mod/Jailbreak.Warden/Paint/WardenPaintBehavior.cs index a6cc280..292c66b 100644 --- a/mod/Jailbreak.Warden/Paint/WardenPaintBehavior.cs +++ b/mod/Jailbreak.Warden/Paint/WardenPaintBehavior.cs @@ -1,23 +1,43 @@ -using CounterStrikeSharp.API; +using System.Drawing; +using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Modules.Utils; +using GangsAPI.Data; +using GangsAPI.Services.Gang; +using GangsAPI.Services.Player; +using Jailbreak.Public; using Jailbreak.Public.Behaviors; using Jailbreak.Public.Extensions; using Jailbreak.Public.Mod.Draw; +using Jailbreak.Public.Mod.Rainbow; using Jailbreak.Public.Mod.Warden; +using Microsoft.Extensions.DependencyInjection; +using WardenPaintColorPerk; namespace Jailbreak.Warden.Paint; -public class WardenPaintBehavior(IWardenService wardenService) - : IPluginBehavior { +public class WardenPaintBehavior(IWardenService wardenService, + IServiceProvider provider) : IPluginBehavior { private Vector? lastPosition; private BasePlugin? parent; + private IRainbowColorizer? colorizer = + provider.GetService(); + + private WardenPaintColor?[] colors = new WardenPaintColor?[65]; + public void Start(BasePlugin basePlugin) { parent = basePlugin; basePlugin.RegisterListener(paint); } + [GameEventHandler] + public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) { + colors = new WardenPaintColor?[65]; + return HookResult.Continue; + } + private void paint() { if (!wardenService.HasWarden) return; @@ -42,7 +62,63 @@ public class WardenPaintBehavior(IWardenService wardenService) if (parent == null) throw new NullReferenceException("Parent plugin is null"); - new BeamLine(parent, start.Clone(), position.Clone()).Draw(30f); + var color = getColor(warden); + var line = new BeamLine(parent, start.Clone(), position.Clone()); + line.SetColor(color); + line.Draw(30); + } + + private Color getColor(CCSPlayerController player) { + if (player.Pawn.Value == null || player.PlayerPawn.Value == null) + return Color.White; + var pawn = player.Pawn.Value; + if (pawn == null || !pawn.IsValid) return Color.White; + + var color = colors[player.Index]; + if (color != null) { + if (color == WardenPaintColor.RAINBOW) + return colorizer?.GetRainbow() ?? Color.White; + if ((color & WardenPaintColor.RANDOM) != 0) + return color.Value.PickRandom() ?? Color.White; + return color.Value.GetColor() ?? Color.White; + } + + var wrapper = new PlayerWrapper(player); + Task.Run(async () => { + color = await fetchColor(wrapper); + colors[player.Index] = color; + }); + + return Color.White; + } + + private async Task fetchColor(PlayerWrapper player) { + var gangs = API.Gangs?.Services.GetService(); + var playerStats = API.Gangs?.Services.GetService(); + var players = API.Gangs?.Services.GetService(); + var gangStats = API.Gangs?.Services.GetService(); + + if (gangs == null || playerStats == null || players == null + || gangStats == null) + return WardenPaintColor.DEFAULT; + + var (success, playerColors) = + await playerStats.GetForPlayer(player.Steam, + WardenPaintColorPerk.WardenPaintColorPerk.STAT_ID); + + if (!success) return WardenPaintColor.DEFAULT; + + var gangPlayer = await players.GetPlayer(player.Steam); + + if (gangPlayer?.GangId == null) return WardenPaintColor.DEFAULT; + + var gang = await gangs.GetGang(gangPlayer.GangId.Value); + if (gang == null) return WardenPaintColor.DEFAULT; + + var (_, available) = await gangStats.GetForGang(gang, + WardenPaintColorPerk.WardenPaintColorPerk.STAT_ID); + + return playerColors & available; } private Vector? findFloorIntersection(CCSPlayerController player) { diff --git a/mod/WardenPaintColorPerk/WardenColorBootstrap.cs b/mod/WardenPaintColorPerk/WardenColorBootstrap.cs index 9cd4b3f..02fe47c 100644 --- a/mod/WardenPaintColorPerk/WardenColorBootstrap.cs +++ b/mod/WardenPaintColorPerk/WardenColorBootstrap.cs @@ -1,5 +1,12 @@ -namespace WardenPaintColorPerk; +using GangsAPI.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace WardenPaintColorPerk; public class WardenColorBootstrap { - + public WardenColorBootstrap(IServiceProvider collection) { + new WardenColorCommand(collection).Start(); + collection.GetRequiredService() + .Perks.Add(new WardenPaintColorPerk(collection)); + } } \ No newline at end of file diff --git a/mod/WardenPaintColorPerk/WardenPaintColor.cs b/mod/WardenPaintColorPerk/WardenPaintColor.cs new file mode 100644 index 0000000..b06f95a --- /dev/null +++ b/mod/WardenPaintColorPerk/WardenPaintColor.cs @@ -0,0 +1,55 @@ +using System.Drawing; +using Gangs.BaseImpl.Extensions; + +namespace WardenPaintColorPerk; + +[Flags] +public enum WardenPaintColor { + DEFAULT = 1 << 0, + RED = 1 << 1, + ORANGE = 1 << 2, + YELLOW = 1 << 3, + GREEN = 1 << 4, + CYAN = 1 << 5, + BLUE = 1 << 6, + PURPLE = 1 << 7, + RANDOM = 1 << 8, + RAINBOW = 1 << 9 +} + +public static class WardenColorExtensions { + private static readonly Random rng = new(); + + public static Color? GetColor(this WardenPaintColor paintColor) { + return paintColor switch { + WardenPaintColor.RED => Color.Red, + WardenPaintColor.ORANGE => Color.Orange, + WardenPaintColor.YELLOW => Color.Yellow, + WardenPaintColor.GREEN => Color.Green, + WardenPaintColor.CYAN => Color.Cyan, + WardenPaintColor.BLUE => Color.Blue, + WardenPaintColor.PURPLE => Color.Purple, + WardenPaintColor.DEFAULT => null, + WardenPaintColor.RANDOM => null, + _ => Color.White + }; + } + + public static int GetCost(this WardenPaintColor paintColor) { + if (paintColor == WardenPaintColor.RAINBOW) return 10 * 7500; + if (paintColor == WardenPaintColor.DEFAULT) return 0; + return (int)Math.Round(paintColor.GetColor().GetColorMultiplier() * 7500); + } + + public static Color? PickRandom(this WardenPaintColor paintColor) { + var n = rng.Next(Enum.GetValues().Length); + var available = Enum.GetValues() + .Where(c => paintColor.HasFlag(c) && c.GetColor() != null) + .ToList(); + + // Gang bought the random perk, but no colors, sillies! + if (available.Count == 0) return null; + + return available[n % available.Count].GetColor(); + } +} \ No newline at end of file diff --git a/mod/WardenPaintColorPerk/WardenPaintColorPerk.csproj b/mod/WardenPaintColorPerk/WardenPaintColorPerk.csproj new file mode 100644 index 0000000..3cd1f42 --- /dev/null +++ b/mod/WardenPaintColorPerk/WardenPaintColorPerk.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + ..\..\public\Jailbreak.Public\Mixin\GangsAPI.dll + + + + diff --git a/public/Jailbreak.Public/Mod/Rainbow/IRainbowColorizer.cs b/public/Jailbreak.Public/Mod/Rainbow/IRainbowColorizer.cs index 5e6022a..fbc793e 100644 --- a/public/Jailbreak.Public/Mod/Rainbow/IRainbowColorizer.cs +++ b/public/Jailbreak.Public/Mod/Rainbow/IRainbowColorizer.cs @@ -1,4 +1,5 @@ -using CounterStrikeSharp.API.Core; +using System.Drawing; +using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Utils; using Jailbreak.Public.Behaviors; @@ -16,4 +17,6 @@ public interface IRainbowColorizer : IPluginBehavior { } void StopRainbow(int slot); + + Color GetRainbow(); } \ No newline at end of file