Skip to content

Commit 74c7efe

Browse files
authored
Set gravity for ball prediction (#105)
* Set gravity for ball prediction * Run formatter * Update ball pred test * Actually set gravity * Update build.yml to run tests * Fix test * Directly set WorldGravityZ
1 parent 0784434 commit 74c7efe

18 files changed

+46
-22
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ jobs:
1919
with:
2020
submodules: 'recursive'
2121

22+
- name: Test
23+
run: dotnet test
24+
2225
- name: Build
2326
run: dotnet publish -r win-x64
2427

@@ -37,6 +40,9 @@ jobs:
3740
with:
3841
submodules: 'recursive'
3942

43+
- name: Test
44+
run: dotnet test
45+
4046
- name: Build
4147
run: dotnet publish -r linux-x64
4248

RLBotCS/Main.cs

Lines changed: 1 addition & 1 deletion
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.3");
11+
Console.WriteLine("RLBotServer v5.beta.6.4");
1212
Environment.Exit(0);
1313
}
1414

RLBotCS/ManagerTools/BallPredictor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public static partial class BallPredictor
5656
[LibraryImport("rl_ball_sym", EntryPoint = "set_heatseeker_target")]
5757
private static partial void SetHeatseekerTarget(byte blueGoal);
5858

59+
[LibraryImport("rl_ball_sym", EntryPoint = "set_gravity")]
60+
private static partial void SetGravity(float gravityZ);
61+
5962
[LibraryImport("rl_ball_sym", EntryPoint = "step")]
6063
private static unsafe partial BallSlice* Step(BallSlice ball, ushort ticks);
6164

@@ -122,7 +125,8 @@ public static PredictionMode UpdateMode(MatchConfigurationT matchConfig)
122125
public static BallPredictionT Generate(
123126
float currentTime,
124127
BallInfoT currentBall,
125-
(TouchT, uint)? lastTouch
128+
(TouchT, uint)? lastTouch,
129+
float gravityZ
126130
)
127131
{
128132
BallSlice ball = new()
@@ -165,6 +169,8 @@ public static BallPredictionT Generate(
165169
}
166170
}
167171

172+
SetGravity(gravityZ);
173+
168174
unsafe
169175
{
170176
var ballSlices = Step(ball, numSlices);

RLBotCS/ManagerTools/ConfigValidator.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static bool Validate(MatchConfigurationT config)
4343
config.LauncherArg = "";
4444
}
4545
}
46-
46+
4747
config.Mutators ??= new();
4848
config.PlayerConfigurations ??= new();
4949
config.ScriptConfigurations ??= new();
@@ -139,7 +139,7 @@ List<PlayerConfigurationT> players
139139
// Fallback if above fails or user didn't include paints
140140
player.Loadout ??= new();
141141
player.Loadout.LoadoutPaint ??= new();
142-
142+
143143
player.RunCommand = "";
144144
player.RootDir = "";
145145

@@ -161,7 +161,10 @@ List<PlayerConfigurationT> players
161161
break;
162162
}
163163

164-
player.SpawnId = player.Variety.Type == PlayerClass.Human ? 0 : $"{player.AgentId}/{player.Team}/{i}".GetHashCode();
164+
player.SpawnId =
165+
player.Variety.Type == PlayerClass.Human
166+
? 0
167+
: $"{player.AgentId}/{player.Team}/{i}".GetHashCode();
165168
}
166169

167170
if (humanCount > 1)

RLBotCS/ManagerTools/MatchStarter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private void LoadMatch(MatchConfigurationT matchConfig, PlayerSpawner spawner)
305305
{
306306
spawner.DespawnPlayers(toDespawnIds);
307307
}
308-
308+
309309
// We can flush C&S despawn commands immediately
310310
spawner.Flush();
311311
}
@@ -451,7 +451,7 @@ private bool SpawnCars(
451451
{
452452
spawner.Flush();
453453
}
454-
454+
455455
return true;
456456
}
457457

RLBotCS/ManagerTools/PlayerSpawner.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,17 @@ public void SpawnHuman(PlayerConfigurationT config, uint desiredIndex)
5454
.FirstOrDefault(kp => config.SpawnId == kp.SpawnId);
5555
if (alreadySpawnedPlayer != null)
5656
{
57-
_gameState.PlayerMapping.QueueIndexChange(alreadySpawnedPlayer.PlayerIndex, desiredIndex);
57+
_gameState.PlayerMapping.QueueIndexChange(
58+
alreadySpawnedPlayer.PlayerIndex,
59+
desiredIndex
60+
);
5861
return;
5962
}
6063

6164
_gameState.PlayerMapping.AddPendingSpawn(
6265
new SpawnTracker
6366
{
64-
CommandId = 0, // Human spawning must use command id 0 for reasons in bridge
67+
CommandId = 0, // Human spawning must use command id 0 for reasons in bridge
6568
SpawnId = config.SpawnId,
6669
DesiredPlayerIndex = desiredIndex,
6770
IsBot = false,

RLBotCS/Server/BridgeContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ MatchStarter matchStarter
4444
public PerfMonitor PerfMonitor { get; } = new();
4545

4646
public PlayerSpawner GetPlayerSpawner() => new(ref GameState, SpawnCommandQueue);
47+
4748
public void UpdateTimeMutators()
4849
{
4950
var mutators = MatchConfig!.Mutators;

RLBotCS/Server/BridgeHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ private async Task HandleServer()
137137
_context.PerfMonitor.ClearAll();
138138
}
139139

140-
if (_context.MatchStarter.HasSpawnedMap && _context.GameState.MatchPhase == MatchPhase.Paused && _context.SpawnCommandQueue.Count > 0)
140+
if (
141+
_context.MatchStarter.HasSpawnedMap
142+
&& _context.GameState.MatchPhase == MatchPhase.Paused
143+
&& _context.SpawnCommandQueue.Count > 0
144+
)
141145
{
142146
_context.Logger.LogDebug("Sending queued spawning commands");
143147
_context.SpawnCommandQueue.Flush();

RLBotCS/Server/BridgeMessage/SetGameState.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ public void HandleMessage(BridgeContext context)
1313
if (GameState.MatchInfo is { } matchInfo)
1414
{
1515
if (matchInfo.WorldGravityZ is { } gravity)
16+
{
1617
context.MatchCommandQueue.AddConsoleCommand(
1718
FlatToCommand.MakeGravityCommand(gravity.Val)
1819
);
20+
context.GameState.WorldGravityZ = gravity.Val;
21+
}
1922

2023
if (matchInfo.GameSpeed is { } speed)
2124
context.MatchCommandQueue.AddConsoleCommand(
@@ -58,10 +61,7 @@ public void HandleMessage(BridgeContext context)
5861

5962
if (car.BoostAmount is { } boostAmount)
6063
{
61-
context.MatchCommandQueue.AddSetBoostCommand(
62-
(ushort)id,
63-
(int)boostAmount.Val
64-
);
64+
context.MatchCommandQueue.AddSetBoostCommand((ushort)id, (int)boostAmount.Val);
6565
}
6666
}
6767

RLBotCS/Server/FlatBuffersServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private void AddSession(TcpClient client)
3232
Thread sessionThread = new(() =>
3333
{
3434
_context.Logger.LogDebug("Client {} connected", clientId);
35-
35+
3636
FlatBuffersSession session = new(
3737
client,
3838
clientId,

RLBotCS/Server/FlatBuffersSession.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class FlatBuffersSession
7474

7575
private readonly FlatBufferBuilder _messageBuilder = new(1 << 10);
7676

77-
public string ClientName =>
77+
public string ClientName =>
7878
_agentId != ""
7979
? $"client {_clientId} (index {string.Join("+", _playerIdPairs.Select(p => p.Index))}, team {_team}, aid {_agentId})"
8080
: $"client {_clientId} (w/o aid)";
@@ -394,7 +394,7 @@ private async Task HandleInternalMessages()
394394
_messageBuilder
395395
)
396396
);
397-
397+
398398
Logger.LogDebug("Reserved agents for {}", ClientName);
399399

400400
break;

RLBotCS/Server/ServerMessage/DistributeGamePacket.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ private static void DistributeBallPrediction(ServerContext context, GamePacketT
2525
BallPredictionT prediction = BallPredictor.Generate(
2626
packet.MatchInfo.SecondsElapsed,
2727
firstBall,
28-
lastTouch
28+
lastTouch,
29+
packet.MatchInfo.WorldGravityZ
2930
);
3031

3132
foreach (var (writer, _) in context.Sessions.Values)
602 KB
Binary file not shown.
241 KB
Binary file not shown.
85 KB
Binary file not shown.
269 KB
Binary file not shown.

RLBotCSTests/BallPrediction.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void TestBallPred()
3131
packet.Balls[12345] = new();
3232
var gTP = packet.ToFlatBuffers();
3333

34-
BallPredictor.Generate(1, gTP.Balls[0], null);
34+
BallPredictor.Generate(1, gTP.Balls[0], null, -650f);
3535

3636
packet.Balls[12345].Physics = new Physics(
3737
new Vector3(0, 0, 1.1f * 91.25f),
@@ -41,7 +41,7 @@ public void TestBallPred()
4141
);
4242
var gTP2 = packet.ToFlatBuffers();
4343

44-
var ballPred = BallPredictor.Generate(1, gTP2.Balls[0], null);
44+
var ballPred = BallPredictor.Generate(1, gTP2.Balls[0], null, -650f);
4545

4646
int numSlices = 6 * 120;
4747
Assert.AreEqual(numSlices, ballPred.Slices.Count);
@@ -65,7 +65,7 @@ public void TestBallPred()
6565
var gTP3 = packet.ToFlatBuffers();
6666

6767
stopWatch.Start();
68-
BallPredictor.Generate(1, gTP3.Balls[0], null);
68+
BallPredictor.Generate(1, gTP3.Balls[0], null, -650f);
6969
stopWatch.Stop();
7070
}
7171

RLBotCSTests/PlayerMappingTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void TestSpawnProcess()
5151
Assert.AreEqual(0u, _playerMapping.PlayerIndexFromActorId(111));
5252
Assert.IsNotNull(index);
5353
Assert.AreEqual(0u, index);
54-
Assert.IsNull(metadata2.SpawnId);
54+
Assert.AreEqual(0, metadata2.SpawnId);
5555
Assert.AreEqual(desiredIndex, _playerMapping.PlayerIndexFromActorId(actorId));
5656
Assert.IsTrue(!metadata2.IsBot);
5757
Assert.IsTrue(!metadata2.IsCustomBot);

0 commit comments

Comments
 (0)