Skip to content

Commit d3f3c4c

Browse files
Move MatchStarter to BridgeHandler (#100)
* Move MatchStarter to BridgeHandler * Use AgentMapping to ensure all agents are ready * Remove SetMutators message * Remove bridge writer from MatchStart and introduce PlayerSpawner * Move AddLoadout out of MatchStarter * Avoid distributing old FieldInfos * Format * Bump version * Remove unused match id * Properly remove unused match id * Ensure human is last index in valid match configs * Ensure loadouts are not null * Optimize imports and format * Make sure we do not send spawn commands while phase != paused * Fix Psyonix bots sometimes missing paints * Add some more debug logging * Update Bridge.dll * Fix command id of SpawnHuman * Refactor PlayerMapping * Use versioned Bridge
1 parent 888cf16 commit d3f3c4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+902
-909
lines changed

RLBotCS/Conversion/ColorSwatches.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using rlbot.flat;
1+
using RLBotCS.Model;
22
using Color = System.Drawing.Color;
33

44
namespace RLBotCS.Conversion;

RLBotCS/Conversion/FlatToModel.cs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
using System.Drawing;
21
using Bridge.Models.Command;
32
using Bridge.Models.Control;
4-
using Bridge.Models.Phys;
5-
using Bridge.Models.Render;
63
using Bridge.State;
4+
using rlbot.flat;
5+
using Color = System.Drawing.Color;
6+
using LoadoutPaint = Bridge.Models.Command.LoadoutPaint;
7+
using Physics = Bridge.Models.Phys.Physics;
8+
using RelativeAnchor = Bridge.Models.Render.RelativeAnchor;
9+
using RenderAnchor = Bridge.Models.Render.RenderAnchor;
10+
using Rotator = Bridge.Models.Phys.Rotator;
11+
using Vector3 = Bridge.Models.Phys.Vector3;
712

813
namespace RLBotCS.Conversion;
914

1015
static class FlatToModel
1116
{
12-
internal static CarInput ToCarInput(rlbot.flat.ControllerStateT state)
17+
internal static CarInput ToCarInput(ControllerStateT state)
1318
{
1419
float dodgeForward = -state.Pitch;
1520

@@ -34,41 +39,37 @@ internal static CarInput ToCarInput(rlbot.flat.ControllerStateT state)
3439
};
3540
}
3641

37-
internal static Vector3 ToVectorFromT(rlbot.flat.Vector3T vec) => new(vec.X, vec.Y, vec.Z);
42+
internal static Vector3 ToVectorFromT(Vector3T vec) => new(vec.X, vec.Y, vec.Z);
3843

3944
internal static RelativeAnchor ExtractRelativeAnchor(
40-
rlbot.flat.RelativeAnchorUnion? relative,
45+
RelativeAnchorUnion? relative,
4146
GameState gameState
4247
) =>
4348
relative?.Value switch
4449
{
45-
rlbot.flat.CarAnchorT { Index: var index, Local: var local } => new RelativeAnchor(
50+
CarAnchorT { Index: var index, Local: var local } => new RelativeAnchor(
4651
ToVectorFromT(local),
4752
gameState.PlayerMapping.ActorIdFromPlayerIndex(index) ?? 0
4853
),
49-
rlbot.flat.BallAnchorT { Index: var index, Local: var local } =>
50-
new RelativeAnchor(
51-
ToVectorFromT(local),
52-
gameState.GetBallActorIdFromIndex(index) ?? 0
53-
),
54+
BallAnchorT { Index: var index, Local: var local } => new RelativeAnchor(
55+
ToVectorFromT(local),
56+
gameState.GetBallActorIdFromIndex(index) ?? 0
57+
),
5458
_ => new RelativeAnchor(new Vector3(0, 0, 0), 0),
5559
};
5660

57-
internal static RenderAnchor ToRenderAnchor(
58-
rlbot.flat.RenderAnchorT offset,
59-
GameState gameState
60-
) =>
61+
internal static RenderAnchor ToRenderAnchor(RenderAnchorT offset, GameState gameState) =>
6162
new RenderAnchor(
6263
ToVectorFromT(offset.World),
6364
ExtractRelativeAnchor(offset.Relative, gameState)
6465
);
6566

66-
internal static Color ToColor(rlbot.flat.ColorT c) => Color.FromArgb(c.A, c.R, c.G, c.B);
67+
internal static Color ToColor(ColorT c) => Color.FromArgb(c.A, c.R, c.G, c.B);
6768

6869
internal static Rotator ToRotator(rlbot.flat.Rotator r) =>
6970
new Rotator(r.Pitch, r.Yaw, r.Roll);
7071

71-
internal static Loadout ToLoadout(rlbot.flat.PlayerLoadoutT l, uint team)
72+
internal static Loadout ToLoadout(PlayerLoadoutT l, uint team)
7273
{
7374
Color primaryColor = ColorSwatches.GetPrimary(l.TeamColorId, team);
7475
Color secondaryColor = ColorSwatches.GetSecondary(l.CustomColorId);
@@ -106,10 +107,7 @@ internal static Loadout ToLoadout(rlbot.flat.PlayerLoadoutT l, uint team)
106107
};
107108
}
108109

109-
internal static Vector3 DesiredToVector(
110-
rlbot.flat.Vector3PartialT? partVec,
111-
Vector3 defaultVec
112-
) =>
110+
internal static Vector3 DesiredToVector(Vector3PartialT? partVec, Vector3 defaultVec) =>
113111
partVec switch
114112
{
115113
not null => new Vector3(
@@ -120,10 +118,7 @@ Vector3 defaultVec
120118
_ => defaultVec,
121119
};
122120

123-
internal static Rotator DesiredToRotator(
124-
rlbot.flat.RotatorPartialT? partRot,
125-
Rotator defaultRot
126-
) =>
121+
internal static Rotator DesiredToRotator(RotatorPartialT? partRot, Rotator defaultRot) =>
127122
partRot switch
128123
{
129124
not null => new Rotator(
@@ -134,7 +129,7 @@ Rotator defaultRot
134129
_ => defaultRot,
135130
};
136131

137-
internal static Physics DesiredToPhysics(rlbot.flat.DesiredPhysicsT p, Physics defaultP) =>
132+
internal static Physics DesiredToPhysics(DesiredPhysicsT p, Physics defaultP) =>
138133
new(
139134
DesiredToVector(p.Location, defaultP.Location),
140135
DesiredToVector(p.Velocity, defaultP.Velocity),

RLBotCS/Conversion/GameStateToFlat.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22
using Bridge.Packet;
33
using Bridge.State;
44
using rlbot.flat;
5+
using RLBotCS.Model;
56
using CollisionShapeUnion = rlbot.flat.CollisionShapeUnion;
67
using MatchPhase = Bridge.Models.Message.MatchPhase;
8+
using Rotator = Bridge.Models.Phys.Rotator;
9+
using Vector2 = Bridge.Models.Phys.Vector2;
10+
using Vector3 = Bridge.Models.Phys.Vector3;
711

812
namespace RLBotCS.Conversion;
913

1014
static class GameStateToFlat
1115
{
12-
private static Vector2T ToVector2T(this Bridge.Models.Phys.Vector2 vec) =>
13-
new() { X = vec.X, Y = vec.Y };
16+
private static Vector2T ToVector2T(this Vector2 vec) => new() { X = vec.X, Y = vec.Y };
1417

15-
private static Vector3T ToVector3T(this Bridge.Models.Phys.Vector3 vec) =>
18+
private static Vector3T ToVector3T(this Vector3 vec) =>
1619
new()
1720
{
1821
X = vec.X,
1922
Y = vec.Y,
2023
Z = vec.Z,
2124
};
2225

23-
private static RotatorT ToRotatorT(this Bridge.Models.Phys.Rotator vec) =>
26+
private static RotatorT ToRotatorT(this Rotator vec) =>
2427
new()
2528
{
2629
Pitch = vec.Pitch,

RLBotCS/Main.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
if (args.Length > 0 && args[0] == "--version")
1010
{
11-
Console.WriteLine("RLBotServer v5.beta.6.1");
11+
Console.WriteLine("RLBotServer v5.beta.6.2");
1212
Environment.Exit(0);
1313
}
1414

@@ -70,13 +70,7 @@
7070

7171
Thread rlbotServer = new(() =>
7272
{
73-
MatchStarter matchStarter = new(bridgeWriter, gamePort, rlbotSocketsPort);
74-
FlatBuffersServer flatBuffersServer = new(
75-
rlbotSocketsPort,
76-
serverChannel,
77-
matchStarter,
78-
bridgeWriter
79-
);
73+
FlatBuffersServer flatBuffersServer = new(rlbotSocketsPort, serverChannel, bridgeWriter);
8074

8175
try
8276
{
@@ -92,7 +86,13 @@
9286
Thread bridgeHandler = new(() =>
9387
{
9488
TcpMessenger tcpMessenger = new(gamePort);
95-
BridgeHandler bridgeHandler = new(serverWriter, bridgeChannel.Reader, tcpMessenger);
89+
MatchStarter matchStarter = new(gamePort, rlbotSocketsPort);
90+
BridgeHandler bridgeHandler = new(
91+
serverWriter,
92+
bridgeChannel.Reader,
93+
tcpMessenger,
94+
matchStarter
95+
);
9696

9797
try
9898
{
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using rlbot.flat;
2+
using RLBotCS.Model;
3+
4+
namespace RLBotCS.ManagerTools;
5+
6+
public struct PlayerIdPair
7+
{
8+
public uint Index;
9+
public int SpawnId;
10+
}
11+
12+
public class AgentMapping
13+
{
14+
private readonly List<AgentMetadata> _agents = new();
15+
16+
public void SetAgents(MatchConfigurationT matchConfig)
17+
{
18+
_agents.Clear();
19+
20+
// Bots
21+
uint humans = 0;
22+
for (int i = 0; i < matchConfig.PlayerConfigurations.Count; i++)
23+
{
24+
var playerConfig = matchConfig.PlayerConfigurations[i];
25+
26+
if (playerConfig.Variety.Type != PlayerClass.CustomBot)
27+
{
28+
if (playerConfig.Variety.Type == PlayerClass.Human)
29+
humans++;
30+
31+
continue;
32+
}
33+
34+
uint index = (uint)i - humans;
35+
_agents.Add(
36+
new AgentMetadata(
37+
index,
38+
playerConfig.Team,
39+
playerConfig.Name,
40+
playerConfig.AgentId,
41+
playerConfig.SpawnId
42+
)
43+
);
44+
}
45+
46+
// Scripts
47+
for (int i = 0; i < matchConfig.ScriptConfigurations.Count; i++)
48+
{
49+
var scriptConfig = matchConfig.ScriptConfigurations[i];
50+
_agents.Add(
51+
new AgentMetadata(
52+
(uint)i,
53+
Team.Scripts,
54+
scriptConfig.Name,
55+
scriptConfig.AgentId,
56+
scriptConfig.SpawnId
57+
)
58+
);
59+
}
60+
}
61+
62+
public (PlayerIdPair, uint)? ReserveAgent(int clientId, string agentId)
63+
{
64+
AgentMetadata? player = _agents.FirstOrDefault(playerMetadata =>
65+
!playerMetadata.HasClient && playerMetadata.AgentId == agentId
66+
);
67+
if (player != null)
68+
{
69+
player.SetClient(clientId);
70+
71+
return (
72+
new PlayerIdPair { Index = player.Index, SpawnId = player.SpawnId },
73+
player.Team
74+
);
75+
}
76+
77+
return null;
78+
}
79+
80+
public (List<PlayerIdPair>, uint)? ReserveAgents(int clientId, string agentId)
81+
{
82+
// find the first player in the group
83+
if (ReserveAgent(clientId, agentId) is var (playerIdPair, team))
84+
{
85+
List<PlayerIdPair> players = new() { playerIdPair };
86+
87+
// find other players in the same group & team
88+
89+
var otherPlayers = _agents.Where(playerMetadata =>
90+
!playerMetadata.HasClient
91+
&& playerMetadata.AgentId == agentId
92+
&& playerMetadata.Team == team
93+
);
94+
95+
foreach (var playerMetadata in otherPlayers)
96+
{
97+
playerMetadata.SetClient(clientId);
98+
players.Add(
99+
new PlayerIdPair
100+
{
101+
Index = playerMetadata.Index,
102+
SpawnId = playerMetadata.SpawnId,
103+
}
104+
);
105+
}
106+
107+
return (players, team);
108+
}
109+
110+
return null;
111+
}
112+
113+
public void UnreserveAgents(int clientId)
114+
{
115+
foreach (var agent in _agents)
116+
{
117+
if (agent.ClientId == clientId)
118+
{
119+
agent.SetClient(null);
120+
}
121+
}
122+
}
123+
124+
public void ReadyAgents(int clientId)
125+
{
126+
foreach (var agent in _agents)
127+
{
128+
if (agent.ClientId == clientId)
129+
{
130+
agent.Ready = true;
131+
}
132+
}
133+
}
134+
135+
public bool AllReady() => _agents.All(a => a.Ready);
136+
137+
/// <summary>Returns how many agents are ready and how many there are in total.</summary>
138+
public (int, int) GetReadyStatus()
139+
{
140+
var ready = _agents.Count(a => a.Ready);
141+
var expected = _agents.Count;
142+
return (ready, expected);
143+
}
144+
}

0 commit comments

Comments
 (0)