Skip to content

Commit

Permalink
Improve bot support
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Jul 20, 2024
1 parent c1b5779 commit b2eefc8
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 49 deletions.
2 changes: 1 addition & 1 deletion mod/TTT.Detective/DetectiveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DetectiveManager(IPlayerService roleService)
public void Start(BasePlugin parent) {
parent.RegisterListener<Listeners.OnTick>(() => {
foreach (var player in Utilities.GetPlayers()
.Where(player => player.IsValid && player.IsReal())
.Where(player => player is { IsValid: true, IsBot: false })
.Where(player => (player.Buttons & PlayerButtons.Use) != 0))
OnPlayerUse(player);
});
Expand Down
3 changes: 1 addition & 2 deletions mod/TTT.Logs/LogBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class LogBehavior : ILogService, IPluginBehavior {

public bool PrintLogs(int round) {
if (_logs.ContainsKey(round)) return false;
foreach (var player in Utilities.GetPlayers().Where(plr => plr.IsReal()))
PrintToPlayer(player, round);
foreach (var player in Utilities.GetPlayers()) PrintToPlayer(player, round);

PrintToConsole(round);
return true;
Expand Down
8 changes: 3 additions & 5 deletions mod/TTT.Logs/LogsListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public void Start(BasePlugin plugin) { }
public HookResult OnPlayerDamage(EventPlayerHurt @event, GameEventInfo info) {
var attackedPlayer = @event.Userid;

if (attackedPlayer == null || !attackedPlayer.IsReal())
return HookResult.Continue;
if (attackedPlayer == null) return HookResult.Continue;

var attackedRole = playerService.GetPlayer(attackedPlayer).PlayerRole();

Expand All @@ -39,12 +38,11 @@ public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) {
var killedPlayer = @event.Userid;
var killer = @event.Attacker;

if (killedPlayer == null || !killedPlayer.IsReal())
return HookResult.Continue;
if (killedPlayer == null) return HookResult.Continue;

var killedRole = playerService.GetPlayer(killedPlayer).PlayerRole();

if (killer == null || !killer.IsReal()) {
if (killer == null) {
logService.AddLog(new DeathAction(
new Tuple<CCSPlayerController, Role>(killedPlayer, killedRole)));
return HookResult.Continue;
Expand Down
3 changes: 2 additions & 1 deletion mod/TTT.Manager/InfoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ private HookResult OnPlayerSpectateChange(EventSpecTargetUpdated @event,
var player = @event.Userid;
var target = new CCSPlayerController(@event.Target);

if (!player.IsReal() || !target.IsReal()) return HookResult.Continue;
if (player == null || !player.IsReal() || !target.IsReal())
return HookResult.Continue;

_spectatorLookAtRole.TryAdd(player,
new Tuple<string, Role>(target.PlayerName,
Expand Down
2 changes: 0 additions & 2 deletions mod/TTT.Player/AntiBlockManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ private HookResult Event_PlayerSpawn(EventPlayerSpawn @event,

var player = @event.Userid;

if (!player.IsReal()) return HookResult.Continue;

if (!player.PlayerPawn.IsValid) return HookResult.Continue;

var pawn = player.PlayerPawn;
Expand Down
28 changes: 7 additions & 21 deletions mod/TTT.Roles/RoleBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void Start(BasePlugin parent) {

public void AddRoles() {
var eligible = Utilities.GetPlayers()
.Where(player => player.IsReal())
.Where(player => player.Team is not (CsTeam.Spectator or CsTeam.None))
.ToList();

Expand Down Expand Up @@ -149,15 +148,10 @@ public HookResult
OnRoundStart(EventRoundFreezeEnd @event, GameEventInfo info) {
_roundService.SetRoundStatus(RoundStatus.Waiting);
foreach (var player in Utilities.GetPlayers()
.Where(player => player.IsReal() && player.Team != CsTeam.None
|| player.Team != CsTeam.Spectator)) {
.Where(player
=> player.Team != CsTeam.None && player.Team != CsTeam.Spectator)) {
player.RemoveWeapons();
if (!string.IsNullOrEmpty("weapon_glock"))
player.GiveNamedItem("weapon_glock");

if (!string.IsNullOrEmpty(string.Empty))
player.GiveNamedItem(string.Empty);

player.GiveNamedItem("weapon_glock");
player.GiveNamedItem("weapon_knife");
service.GetPlayer(player).ModifyKarma();
}
Expand All @@ -169,8 +163,8 @@ public HookResult
public HookResult OnPlayerConnect(EventPlayerConnectFull @event,
GameEventInfo info) {
if (Utilities.GetPlayers()
.Count(player => player.IsReal() && player.Team != CsTeam.None
|| player.Team == CsTeam.Spectator) < 3)
.Count(player => player.Team != CsTeam.None
&& player.Team != CsTeam.Spectator) < 3)
_roundService.ForceEnd();

return HookResult.Continue;
Expand Down Expand Up @@ -219,10 +213,7 @@ public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) {

[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) {
var players = Utilities.GetPlayers()
.Where(player => player.IsValid)
.Where(player => player.IsReal())
.ToList();
var players = Utilities.GetPlayers().ToList();

foreach (var player in players)
player.PrintToCenter(GetWinner().FormatStringFullAfter("s has won!"));
Expand Down Expand Up @@ -250,12 +241,10 @@ public bool IsInnocent(CCSPlayerController player) {
}

private Role GetWinner() {
return _traitorsLeft == 0 ? Role.Traitor : Role.Innocent;
return _traitorsLeft > 0 ? Role.Traitor : Role.Innocent;
}

public void SetColor(CCSPlayerController player) {
if (!player.IsReal()) return;

var pawn = player.PlayerPawn.Value;

if (pawn == null || !pawn.IsValid) return;
Expand All @@ -267,16 +256,13 @@ public void SetColor(CCSPlayerController player) {
}

public void RemoveColor(CCSPlayerController player) {
if (!player.IsReal()) return;

var pawn = player.PlayerPawn.Value;

if (pawn == null || !pawn.IsValid) return;

pawn.RenderMode = RenderMode_t.kRenderTransColor;
pawn.Render = Color.FromArgb(254, 255, 255, 255);


Utilities.SetStateChanged(pawn, "CBaseModelEntity", "m_clrRender");
}
}
6 changes: 1 addition & 5 deletions mod/TTT.Round/Round.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public Round(IRoleService roleService, RoundConfig? config, int roundId) {
public void Tick() {
_graceTime--;

var players = Utilities.GetPlayers()
.Where(player => player.IsValid)
.Where(player => player.IsReal())
.ToList();
var players = Utilities.GetPlayers().ToList();

var formattedColor =
$"<font color=\"#{Color.Green.R:X2}{Color.Green.G:X2}{Color.Green.B:X2}\">";
Expand All @@ -42,7 +39,6 @@ public void Tick() {

public void Start() {
foreach (var player in Utilities.GetPlayers()
.Where(player => player.IsReal())
.Where(player => !player.PawnIsAlive)
.Where(player
=> player.Team is CsTeam.Terrorist or CsTeam.CounterTerrorist))
Expand Down
6 changes: 1 addition & 5 deletions mod/TTT.Round/RoundBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ public void TickWaiting() {
}

public void ForceStart() {
foreach (var player in Utilities.GetPlayers()
.Where(player => player.IsReal())
.Where(player => player.IsReal())
.ToList())
foreach (var player in Utilities.GetPlayers().ToList())
player.VoiceFlags = VoiceFlags.Normal;
_round!.Start();
ServerExtensions.GetGameRules().RoundTime = 360;
Expand Down Expand Up @@ -123,7 +120,6 @@ private HookResult
OnTeamJoin(CCSPlayerController? executor, CommandInfo info) {
if (_roundStatus != RoundStatus.Started) return HookResult.Continue;
if (executor == null) return HookResult.Continue;
if (!executor.IsReal()) return HookResult.Continue;
if (_roleService.GetRole(executor) != Role.Unassigned)
return HookResult.Continue;
Server.NextFrame(() => executor.CommitSuicide(false, true));
Expand Down
8 changes: 4 additions & 4 deletions public/TTT.Formatting/Extensions/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static IView ToAllCenter(this IView view) {

public static IView ToPlayerConsole(this IView view,
CCSPlayerController player) {
if (!player.IsReal() || player.IsBot) return view;
if (!player.IsReal(false)) return view;

var writer = view.ToWriter();

Expand All @@ -56,7 +56,7 @@ public static IView ToPlayerConsole(this IView view,

public static IView
ToPlayerChat(this IView view, CCSPlayerController player) {
if (!player.IsReal() || player.IsBot) return view;
if (!player.IsReal(false)) return view;

var writer = view.ToWriter();

Expand All @@ -67,7 +67,7 @@ public static IView

public static IView ToPlayerCenter(this IView view,
CCSPlayerController player) {
if (!player.IsReal() || player.IsBot) return view;
if (!player.IsReal(false)) return view;

var writer = view.ToWriter();
var merged = string.Join('\n', writer.Plain);
Expand All @@ -79,7 +79,7 @@ public static IView ToPlayerCenter(this IView view,

public static IView ToPlayerCenterHtml(this IView view,
CCSPlayerController player) {
if (!player.IsReal() || player.IsBot) return view;
if (!player.IsReal(false)) return view;

var writer = view.ToWriter();
var merged = string.Join('\n', writer.Panorama);
Expand Down
5 changes: 3 additions & 2 deletions public/TTT.Public/Extensions/PlayerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ public static string GetActiveWeaponName(this CCSPlayerController player) {
}


public static bool IsReal(this CCSPlayerController player) {
public static bool IsReal(this CCSPlayerController player, bool bot = true) {
// Do nothing else before this:
// Verifies the handle points to an entity within the global entity list.
if (!player.IsValid) return false;

if (player.Connected != PlayerConnectedState.PlayerConnected) return false;

if (player.IsBot || player.IsHLTV) return false;
if (player.IsBot) return bot;
if (player.IsHLTV) return false;

return true;
}
Expand Down
1 change: 0 additions & 1 deletion public/TTT.Public/Extensions/ScoreboardExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace TTT.Round;
public static class ScoreboardExtension {
public static void ModifyScoreBoard(this CCSPlayerController? player) {
if (player == null) return;
if (!player.IsReal()) return;
var actionService = player.ActionTrackingServices;
if (actionService == null) return;

Expand Down

0 comments on commit b2eefc8

Please sign in to comment.