Skip to content

Commit 6fe228a

Browse files
committed
Implement per-bot rendering
1 parent 002a636 commit 6fe228a

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

RLBotCS/Server/FlatBuffersSession.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ public record DistributeBallPrediction(BallPredictionT BallPrediction) : Session
2525

2626
public record DistributeGameState(GamePacketT GameState) : SessionMessage;
2727

28-
public record RendersAllowed(DebugRendering Allowed) : SessionMessage;
28+
public record RendersAllowed(bool Allowed) : SessionMessage;
2929

3030
public record StateSettingAllowed(bool Allowed) : SessionMessage;
3131

3232
public record MatchComm(MatchCommT Message) : SessionMessage;
3333

3434
public record StopMatch(bool Force) : SessionMessage;
35+
36+
public record UpdateRendering(RenderingStatus Status) : SessionMessage;
3537
}
3638

3739
class FlatBuffersSession
@@ -63,7 +65,6 @@ class FlatBuffersSession
6365

6466
private bool _stateSettingIsEnabled;
6567
private bool _renderingIsEnabled;
66-
private DebugRendering _globalRenderingIsEnabled;
6768

6869
private string _agentId = string.Empty;
6970
private uint _team = Team.Other;
@@ -96,7 +97,6 @@ bool stateSettingIsEnabled
9697
DebugRendering.OnByDefault => true,
9798
_ => false,
9899
};
99-
_globalRenderingIsEnabled = renderingIsEnabled;
100100
_stateSettingIsEnabled = stateSettingIsEnabled;
101101

102102
NetworkStream stream = _client.GetStream();
@@ -381,17 +381,7 @@ private async Task HandleInternalMessages()
381381

382382
break;
383383
case SessionMessage.RendersAllowed m:
384-
_globalRenderingIsEnabled = m.Allowed;
385-
switch (_globalRenderingIsEnabled)
386-
{
387-
case DebugRendering.OnByDefault:
388-
_renderingIsEnabled = true;
389-
break;
390-
case DebugRendering.OffByDefault:
391-
case DebugRendering.AlwaysOff:
392-
_renderingIsEnabled = false;
393-
break;
394-
}
384+
_renderingIsEnabled = m.Allowed;
395385
break;
396386
case SessionMessage.StateSettingAllowed m:
397387
_stateSettingIsEnabled = m.Allowed;
@@ -418,6 +408,19 @@ private async Task HandleInternalMessages()
418408
when m.Force || (_connectionEstablished && _closeBetweenMatches):
419409
_sessionForceClosed = m.Force;
420410
return;
411+
case SessionMessage.UpdateRendering m
412+
when (m.Status.IsBot && (_team == Team.Blue || _team == Team.Orange))
413+
|| (!m.Status.IsBot && _team == Team.Scripts):
414+
415+
foreach (var player in _playerIdPairs)
416+
{
417+
if (player.Index == m.Status.Index)
418+
{
419+
_renderingIsEnabled = m.Status.Status;
420+
break;
421+
}
422+
}
423+
break;
421424
}
422425
}
423426

RLBotCS/Server/ServerMessage/StartMatch.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
22
using RLBot.Flat;
33
using RLBotCS.ManagerTools;
44
using RLBotCS.Server.BridgeMessage;
@@ -27,12 +27,16 @@ public ServerAction Execute(ServerContext context)
2727

2828
BallPredictor.UpdateMode(MatchConfig);
2929

30+
bool defaultRendering = context.RenderingIsEnabled switch
31+
{
32+
DebugRendering.OnByDefault => true,
33+
_ => false,
34+
};
35+
3036
// update all sessions with the new rendering and state setting settings
3137
foreach (var (writer, _) in context.Sessions.Values)
3238
{
33-
SessionMessage render = new SessionMessage.RendersAllowed(
34-
context.RenderingIsEnabled
35-
);
39+
SessionMessage render = new SessionMessage.RendersAllowed(defaultRendering);
3640
writer.TryWrite(render);
3741

3842
SessionMessage stateSetting = new SessionMessage.StateSettingAllowed(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using RLBot.Flat;
2+
3+
namespace RLBotCS.Server.ServerMessage;
4+
5+
record UpdateRendering(RenderingStatus Status) : IServerMessage
6+
{
7+
public ServerAction Execute(ServerContext context)
8+
{
9+
// Ignore all messages if rendering is force-disabled
10+
if (context.RenderingIsEnabled == DebugRendering.AlwaysOff)
11+
return ServerAction.Continue;
12+
13+
SessionMessage.UpdateRendering message = new(Status);
14+
15+
// Distribute to all sessions;
16+
// they will figure out on their own if rendering should be enable/disabled
17+
foreach (var (id, (writer, _)) in context.Sessions)
18+
{
19+
writer.TryWrite(message);
20+
}
21+
22+
return ServerAction.Continue;
23+
}
24+
}

0 commit comments

Comments
 (0)