Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add bots support #34

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-- 2024.12.23 - 1.5.5

- feat: Added 3 new natives - IsAFK, FindOpponents & TerminateRoundIfPossible
- update: When a player executes !afk, execute TerminateRoundIfPossible.
- update: Added `allowed-weapon-prefs` to toggle showing weaponTypes in `!guns` menu.

-- 2024.12.20 - 1.5.4

- fix: PerformAFKAction didnt change the AFK state of player
195 changes: 195 additions & 0 deletions src-botsplugin/K4-Arenas-Bots.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@

using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Capabilities;
using CounterStrikeSharp.API.Modules.Cvars;
using K4ArenaSharedApi;
using Microsoft.Extensions.Logging;

namespace K4ArenaBots;

[MinimumApiVersion(284)]
public class Plugin : BasePlugin
{
public override string ModuleName => "K4-Arenas Addon - Bots Support";
public override string ModuleDescription => "Adds a bot in empty arena if there is no opponent.";
public override string ModuleAuthor => "Cruze";
public override string ModuleVersion => "1.0.0";

public static PluginCapability<IK4ArenaSharedApi> Capability_SharedAPI { get; } = new("k4-arenas:sharedapi");
public static IK4ArenaSharedApi? SharedAPI_Arena { get; private set; } = null;

private CCSGameRules? gameRules = null;
private string botQuotaMode = "normal";

public override void OnAllPluginsLoaded(bool hotReload)
{
SharedAPI_Arena = Capability_SharedAPI.Get();
}

public override void Load(bool hotReload)
{
if(hotReload)
{
gameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules;

SharedAPI_Arena = Capability_SharedAPI.Get();
}

RegisterListener<Listeners.OnMapStart>((mapName) =>
{
DeleteOldGameConfig();
AddTimer(0.1f, () =>
{
gameRules = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").First().GameRules;
});
});

RegisterListener<Listeners.OnMapEnd>(() =>
{
gameRules = null;
});

RegisterEventHandler((EventPlayerSpawn @event, GameEventInfo info) =>
{
var player = @event.Userid;

if(player == null || !player.IsValid || player.IsBot || player.IsHLTV || SharedAPI_Arena?.IsAFK(player) == true)
return HookResult.Continue;

SpawnBotInEmptyArena(player);

return HookResult.Continue;
});

RegisterEventHandler((EventPlayerDisconnect @event, GameEventInfo info) =>
{
var player = @event.Userid;

if(player == null || !player.IsValid || player.IsHLTV || !player.IsBot)
return HookResult.Continue;

info.DontBroadcast = true;
return HookResult.Continue;
}, HookMode.Pre);

RegisterEventHandler((EventRoundPrestart @event, GameEventInfo info) =>
{
if(gameRules == null || gameRules.WarmupPeriod)
{
Server.ExecuteCommand($"bot_prefix \"WARMUP\"");
return HookResult.Continue;
}

(var players, var bots) = GetPlayers();

if(!bots.Any() || bots.First() == null)
{
Server.ExecuteCommand($"bot_prefix \"\"");
return HookResult.Continue;
}

var arenaS = SharedAPI_Arena?.GetArenaName(bots.First()) + " |" ?? "";

Server.ExecuteCommand($"bot_prefix {arenaS}");
return HookResult.Continue;
}, HookMode.Post);

RegisterEventHandler((EventRoundEnd @event, GameEventInfo info) =>
{
SpawnBotInEmptyArena(null, true);
return HookResult.Continue;
});
}

private void SpawnBotInEmptyArena(CCSPlayerController? player, bool roundEnd = false)
{
(var players, var bots) = GetPlayers();

// Logger.LogInformation($"Players: {players.Count()} | Bots: {bots.Count()}");

botQuotaMode = ConVar.Find("bot_quota_mode")?.StringValue ?? "none";

if(players.Count() % 2 == 0 || botQuotaMode != "normal")
{
Logger.LogInformation($"Even players / botQuotaMode not normal, no need to spawn bot.");
if(bots.Count() > 0)
{
Server.ExecuteCommand("bot_quota 0");
Server.ExecuteCommand($"bot_prefix \"\"");
}
return;
}

if(player != null && SharedAPI_Arena?.FindOpponents(player).Count() > 0)
return;

if(!bots.Any() || bots.Count() > 1)
{
if(botQuotaMode == "fill")
Server.ExecuteCommand("bot_quota 2");
else
Server.ExecuteCommand("bot_quota 1");

Logger.LogInformation($"Spawning bot in empty arena.");
}

if(roundEnd)
return;

AddTimer(0.1f, () =>
{
players = new();
bots = new();

(players, bots) = GetPlayers();

if(!bots.Any())
return;

SharedAPI_Arena?.TerminateRoundIfPossible();
});
}

private void DeleteOldGameConfig()
{
// If bot_quota 0 exists in gameconfig.cfg, unlimited round restart will be there so we need to delete it & create a fresh config without it.
// Creating of new file is handled by main plugin with the update.

string filePath = Path.Combine(Server.GameDirectory, "csgo/addons/counterstrikesharp/plugins", "K4-Arenas", "gameconfig.cfg");

if(File.Exists(filePath))
{
if(File.ReadAllText(filePath).Contains("bot_quota "))
{
Logger.LogWarning($"Old gameconfig file found, deleting it.");
File.Delete(filePath);
}
}
}

private (List<CCSPlayerController>, List<CCSPlayerController>) GetPlayers()
{
var players = new List<CCSPlayerController>();
var bots = new List<CCSPlayerController>();

for (int i = 0; i < Server.MaxPlayers; i++)
{
var controller = Utilities.GetPlayerFromSlot(i);

if (controller == null || !controller.IsValid || controller.IsHLTV)
continue;

if(controller.IsBot)
{
bots.Add(controller);
continue;
}

if(controller.Connected == PlayerConnectedState.PlayerConnected && SharedAPI_Arena?.IsAFK(controller) == false)
players.Add(controller);
}
return (players, bots);
}
}
29 changes: 29 additions & 0 deletions src-botsplugin/K4-Arenas-Bots.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<GenerateDependencyFile>false</GenerateDependencyFile>
<PublishDir>./bin/K4-Arenas-Bots/plugins/K4-Arenas-Bots/</PublishDir>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="*">
<PrivateAssets>none</PrivateAssets>
<ExcludeAssets>runtime</ExcludeAssets>
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Dapper" Version="*" />
<PackageReference Include="MySqlConnector" Version="*" />
<Reference Include="K4ArenaSharedApi">
<HintPath>../src-shared/K4-ArenaSharedApi.dll</HintPath>
<Private>false</Private>
</Reference>
</ItemGroup>
<Target Name="CopyCustomFilesToPublishDirectory" AfterTargets="Publish">
<Copy SourceFiles="$(ProjectDir)$(ReferencePath)../src-shared/K4-ArenaSharedApi.dll" DestinationFolder="$(PublishDir)../../shared/K4-ArenaSharedApi/" />
</Target>
</Project>
9 changes: 7 additions & 2 deletions src-plugin/Plugin/Models/ArenaModel.cs
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ public Arena(Plugin plugin, Tuple<List<SpawnPoint>, List<SpawnPoint>> spawns)
}

public bool IsActive
=> Team1?.Any(p => p.IsValid) == true && Team2?.Any(p => p.IsValid) == true;
=> Team1?.Any(p => p.IsValid && Plugin.Arenas?.FindPlayer(p.Controller)?.AFK == false) == true && Team2?.Any(p => p.IsValid && Plugin.Arenas?.FindPlayer(p.Controller)?.AFK == false) == true;

public bool HasFinished
=> !IsActive || Team1?.Any(p => p.IsValid && p.IsAlive) == false || Team2?.Any(p => p.IsValid && p.IsAlive) == false;
@@ -178,7 +178,12 @@ public void SetPlayerDetails(List<ArenaPlayer>? team, List<SpawnPoint> spawns, C

if (ArenaID != -1)
{
player.Controller.PrintToChat($" {Localizer["k4.general.prefix"]} {Localizer["k4.chat.arena_roundstart", Plugin.GetRequiredArenaName(ArenaID), Plugin.GetOpponentNames(opponents) ?? "Unknown", Localizer[RoundType.Name ?? "Missing"]]}");
// Bots plugin sets bot_prefix at EventRoundPreStart hence some delay to print opponent names. (Frame not enough sometimes)
Plugin.AddTimer(0.001f, () =>
{
if(player.Controller.IsValid)
player.Controller.PrintToChat($" {Localizer["k4.general.prefix"]} {Localizer["k4.chat.arena_roundstart", Plugin.GetRequiredArenaName(ArenaID), Plugin.GetOpponentNames(opponents) ?? "Unknown", Localizer[RoundType.Name ?? "Missing"]]}");
});
}

if (Plugin.gameRules?.WarmupPeriod == true)
23 changes: 20 additions & 3 deletions src-plugin/Plugin/Models/ArenaPlayerModel.cs
Original file line number Diff line number Diff line change
@@ -228,7 +228,7 @@ private void ShowChatWeaponPreferenceMenu()
ChatMenu weaponPreferenceMenu = new ChatMenu(Localizer["k4.menu.weaponpref.title"]);
foreach (WeaponType weaponType in Enum.GetValues(typeof(WeaponType)))
{
if (weaponType == WeaponType.Unknown)
if (weaponType == WeaponType.Unknown || !IsAllowedWeaponType(weaponType))
continue;
weaponPreferenceMenu.AddMenuOption(Localizer[$"k4.rounds.{weaponType.ToString().ToLower()}"],
(player, option) =>
@@ -243,24 +243,41 @@ private void ShowChatWeaponPreferenceMenu()
private void ShowCenterWeaponPreferenceMenu()
{
var items = new List<MenuItem>();
var values = new Dictionary<int, WeaponType>();
int count = 0;
foreach (WeaponType weaponType in Enum.GetValues(typeof(WeaponType)))
{
if (weaponType == WeaponType.Unknown)
if (weaponType == WeaponType.Unknown || !IsAllowedWeaponType(weaponType))
continue;
items.Add(new MenuItem(MenuItemType.Button, [new MenuValue($"{Localizer[$"k4.rounds.{weaponType.ToString().ToLower()}"]}")]));
values.Add(count++, weaponType);
}

Plugin.Menu?.ShowScrollableMenu(Controller, Localizer["k4.menu.weaponpref.title"], items, (buttons, menu, selected) =>
{
if (selected == null) return;
if (buttons == MenuButtons.Select)
{
WeaponType selectedWeaponType = (WeaponType)(menu.Option);
WeaponType selectedWeaponType = values[menu.Option];
ShowWeaponSubPreferenceMenu(selectedWeaponType);
}
}, false, Config.CommandSettings.FreezeInMenu, disableDeveloper: Config.CommandSettings.ShowMenuCredits);
}

private bool IsAllowedWeaponType(WeaponType weaponType)
{
return weaponType switch
{
WeaponType.Rifle => Config.AllowedWeaponPreferences.Rifle,
WeaponType.Sniper => Config.AllowedWeaponPreferences.Sniper,
WeaponType.SMG => Config.AllowedWeaponPreferences.SMG,
WeaponType.LMG => Config.AllowedWeaponPreferences.LMG,
WeaponType.Shotgun => Config.AllowedWeaponPreferences.Shotgun,
WeaponType.Pistol => Config.AllowedWeaponPreferences.Pistol,
_ => false
};
}

public void ShowWeaponSubPreferenceMenu(WeaponType weaponType)
{
if (Plugin.Config.CommandSettings.CenterMenuMode)
20 changes: 20 additions & 0 deletions src-plugin/Plugin/Models/ArenasModel.cs
Original file line number Diff line number Diff line change
@@ -40,6 +40,26 @@ public Arenas(Plugin plugin)
return allPlayers.FirstOrDefault(p => p.Controller == player);
}

public List<CCSPlayerController> FindOpponents(CCSPlayerController? player)
{
var arenaPlayer = FindPlayer(player);

if (arenaPlayer is null)
return new List<CCSPlayerController>();

var arenaID = Plugin.GetPlayerArenaID(arenaPlayer);

if(arenaID < 0)
return new List<CCSPlayerController>();

var arena = ArenaList.FirstOrDefault(a => a.ArenaID == arenaID);
if (arena == null)
return new List<CCSPlayerController>();

var opponents = arena.Team1?.Any(p => p.Controller == player) == true ? arena.Team2 : arena.Team1;
return opponents?.Select(p => p.Controller).ToList() ?? new List<CCSPlayerController>();
}

public ArenaPlayer? FindPlayer(ulong steamId)
{
IEnumerable<ArenaPlayer> allPlayers = Plugin.WaitingArenaPlayers
13 changes: 10 additions & 3 deletions src-plugin/Plugin/Models/GameConfigModel.cs
Original file line number Diff line number Diff line change
@@ -24,8 +24,15 @@ public GameConfig(Plugin plugin)

public void Apply()
{
if (ConfigSettings is null)
return;
string filePath = Path.Combine(Plugin.ModuleDirectory, "gameconfig.cfg");

if (!File.Exists(filePath) || ConfigSettings is null)
{
Load();

if(ConfigSettings is null)
return;
}

foreach (var (key, value) in ConfigSettings)
{
@@ -63,7 +70,7 @@ private void Create(string path)
var defaultConfigLines = new List<string>
{
"// Changing these might break the gamemode",
"bot_quota 0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somehow let the bot_quota be in config if bots plugin exists

"bot_quota_mode \"normal\"",
"mp_autoteambalance 0",
"mp_ct_default_primary \"\"",
"mp_ct_default_secondary \"\"",
25 changes: 25 additions & 0 deletions src-plugin/Plugin/PluginAPI.cs
Original file line number Diff line number Diff line change
@@ -55,6 +55,31 @@ public string GetArenaName(CCSPlayerController player)
return string.Empty;
}

public bool IsAFK(CCSPlayerController player)
{
var arenaPlayer = plugin.Arenas?.FindPlayer(player);
if (arenaPlayer is not null)
{
return arenaPlayer.AFK;
}
return false;
}

public List<CCSPlayerController> FindOpponents(CCSPlayerController player)
{
var arenaOpponents = plugin.Arenas?.FindOpponents(player);
if (arenaOpponents is not null)
{
return arenaOpponents;
}
return new List<CCSPlayerController>();
}

public void TerminateRoundIfPossible()
{
plugin.TerminateRoundIfPossible();
}

public void PerformAFKAction(CCSPlayerController player, bool afk)
{
ArenaPlayer? arenaPlayer = plugin.Arenas?.FindPlayer(player!);
Loading