Skip to content

Commit

Permalink
feat: random multiple bot model
Browse files Browse the repository at this point in the history
  • Loading branch information
samyycX committed Jan 1, 2025
1 parent ece647f commit 6230823
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
49 changes: 47 additions & 2 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using CounterStrikeSharp.API.Core;
namespace PlayerModelChanger;

public class BotIndexConvertor : JsonConverter<List<string>>
{
public override bool CanConvert(Type typeToConvert)
{
return typeToConvert == typeof(List<string>) || typeToConvert == typeof(string);
}

public override List<string> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
return new List<string> { reader.GetString()! };
}
else if (reader.TokenType == JsonTokenType.StartArray)
{
var list = new List<string>();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndArray)
break;
if (reader.TokenType == JsonTokenType.String)
list.Add(reader.GetString()!);
}
return list;
}

throw new JsonException("Expected string or array of strings");
}

public override void Write(Utf8JsonWriter writer, List<string> value, JsonSerializerOptions options)
{
if (value == null) return;

writer.WriteStartArray();
foreach (var item in value)
{
writer.WriteStringValue(item);
}
writer.WriteEndArray();
}
}

public class BotsConfig
{
public string CT { get; set; } = "";
public string T { get; set; } = "";
[JsonConverter(typeof(BotIndexConvertor))]
public List<string> CT { get; set; } = [];
[JsonConverter(typeof(BotIndexConvertor))]
public List<string> T { get; set; } = [];

}

Expand Down
21 changes: 13 additions & 8 deletions PlayerModelChanger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace PlayerModelChanger;
public partial class PlayerModelChanger : BasePlugin, IPluginConfig<ModelConfig>
{
public override string ModuleName => "Player Model Changer";
public override string ModuleVersion => "1.8.4";
public override string ModuleVersion => "1.8.5";

public override string ModuleAuthor => "samyyc";
public required ModelConfig Config { get; set; }
Expand Down Expand Up @@ -207,15 +207,23 @@ public HookResult OnPlayerSpawnEvent(EventPlayerSpawn @event, GameEventInfo info

if (player.IsBot)
{
string modelindex = team == CsTeam.Terrorist ? Config.ModelForBots.T : Config.ModelForBots.CT;
if (modelindex == "")
List<string> modelindexs = team == CsTeam.Terrorist ? Config.ModelForBots.T : Config.ModelForBots.CT;
if (modelindexs.Count() == 0)
{
return HookResult.Continue;
}
var modelindex = modelindexs[Random.Shared.Next(modelindexs.Count)];
var botmodel = Service.GetModel(modelindex);
if (modelindex == "@random")
{
botmodel = Service.GetRandomModel(player, team == CsTeam.Terrorist ? Side.T : Side.CT);
}
if (botmodel != null)
{
SetModelNextServerFrame(player, botmodel, botmodel.Disableleg);
AddTimer(0.03f, () =>
{
SetModelNextServerFrame(player, botmodel, botmodel.Disableleg);
});
}
else
{
Expand All @@ -225,6 +233,7 @@ public HookResult OnPlayerSpawnEvent(EventPlayerSpawn @event, GameEventInfo info
player.Pawn.Value.Render = Color.FromArgb(255, originalRender.R, originalRender.G, originalRender.B);
});
}
return HookResult.Continue;
}

if (player.AuthorizedSteamID == null)
Expand Down Expand Up @@ -302,10 +311,6 @@ public Task SetModelNextServerFrame(CCSPlayerController player, Model? model, bo
return Server.NextFrameAsync(() =>
{
var pawn = player.Pawn.Value!;
if (player.IsBot || pawn.CBodyComponent == null || pawn.CBodyComponent.SceneNode == null)
{
return;
}
if (model == null)
{
var defaultModel = Service.GetMapDefaultModel(player);
Expand Down
19 changes: 12 additions & 7 deletions Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ public bool SetPlayerModelWithCheck(CCSPlayerController player, string modelInde
return true;
}

public Model? GetRandomModel(CCSPlayerController player, Side side)
{
var models = GetAllAppliableModels(player, side);
if (models.Count() == 0)
{
return null;
}
var index = Random.Shared.Next(models.Count());
return models[index];
}

public Model? GetPlayerModel(CCSPlayerController player, Side side)
{
if (side == Side.All)
Expand Down Expand Up @@ -314,13 +325,7 @@ public bool SetPlayerModelWithCheck(CCSPlayerController player, string modelInde
}
if (modelIndex == "@random")
{
var models = GetAllAppliableModels(player, side);
if (models.Count() == 0)
{
return null;
}
var index = Random.Shared.Next(models.Count());
return models[index];
return GetRandomModel(player, side);
}
return GetModel(modelIndex);
}
Expand Down
Binary file modified build/Release/PlayerModelChanger/PlayerModelChanger.dll
Binary file not shown.
Binary file modified build/Release/PlayerModelChanger/PlayerModelChanger.pdb
Binary file not shown.

0 comments on commit 6230823

Please sign in to comment.