Skip to content

Commit

Permalink
Avoid spawning multiple of the same Psyonix bot if possible (#44)
Browse files Browse the repository at this point in the history
* Avoid spawning multiple of the same Psyonix bot if possible

* Reset PsyonixLoadouts generation when parsing match settings
  • Loading branch information
NicEastvillage authored Aug 9, 2024
1 parent 9adfa34 commit b2071a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
using System.Collections.Frozen;
using rlbot.flat;

namespace RLBotCS.Conversion;

internal static class PsyonixPresets
internal static class PsyonixLoadouts
{
private static Random _random = new Random();
private static Random _random = new();

/// Unused loadout names, used to avoid spawning multiple of the same Psyonix bot
private static List<string> Unused = new();

public static (string, PlayerLoadoutT) GetRandom(int team)
public static void Reset()
{
var randomPreset = _random.Next(0, BotPresets.Count);
var preset = BotPresets.ElementAt(randomPreset);
var teamPreset = preset.Value[team];

return (preset.Key, teamPreset);
Unused.Clear();
}

public static (string, PlayerLoadoutT) GetNext(int team)
{
if (Unused.Count == 0) Unused = DefaultLoadouts.Keys.OrderBy(_ => _random.Next()).ToList();
var fullName = Unused.Last();
Unused.RemoveAt(Unused.Count - 1);
var loadout = DefaultLoadouts[fullName][team];
var name = fullName.Split('_')[1];
return (name, loadout);
}

private static readonly Dictionary<string, List<PlayerLoadoutT>> BotPresets =
new()
private static readonly FrozenDictionary<string, List<PlayerLoadoutT>> DefaultLoadouts =
new Dictionary<string, List<PlayerLoadoutT>>()
{
{
"Bombers_Casper",
Expand Down Expand Up @@ -2178,5 +2188,5 @@ public static (string, PlayerLoadoutT) GetRandom(int team)
},
}
},
};
}.ToFrozenDictionary();
}
15 changes: 4 additions & 11 deletions RLBotCS/ManagerTools/ConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,24 +303,16 @@ List<string> missingValues
)
{
var team = ParseUint(table, "team", 0, missingValues);
var (fullName, preset) = PsyonixPresets.GetRandom((int)team);

var namePrefix = classUnion.AsPsyonix().BotSkill switch
{
< 0 => "Beginner ",
< 0.5f => "Rookie ",
< 1 => "Pro ",
_ => ""
};
var (name, loadout) = PsyonixLoadouts.GetNext((int)team);

return new()
{
Variety = classUnion,
Team = team,
Name = namePrefix + fullName.Split('_')[1],
Name = name,
Location = "",
RunCommand = "",
Loadout = preset,
Loadout = loadout,
};
}

Expand Down Expand Up @@ -544,6 +536,7 @@ public static MatchSettingsT GetMatchSettings(string path)
TomlTableArray players = ParseTableArray(rlbotToml, "cars", missingValues[""]);
TomlTableArray scripts = ParseTableArray(rlbotToml, "scripts", missingValues[""]);

PsyonixLoadouts.Reset();
List<PlayerConfigurationT> playerConfigs = [];
// Gets the PlayerConfigT object for the number of players requested
int numBots = ParseInt(matchTable, "num_cars", 0, missingValues["match"]);
Expand Down

0 comments on commit b2071a6

Please sign in to comment.