@@ -75,20 +75,8 @@ private async Task HandleServer()
7575 if ( timeAdvanced )
7676 _context . PerfMonitor . AddRLBotSample ( deltaTime ) ;
7777
78- GamePacketT ? packet =
79- timeAdvanced
80- || (
81- _context . ticksSkipped > MAX_TICK_SKIP
82- && (
83- _context . GameState . MatchPhase == MatchPhase . Replay
84- || _context . GameState . MatchPhase == MatchPhase . Paused
85- || _context . GameState . MatchPhase == MatchPhase . Ended
86- || _context . GameState . MatchPhase == MatchPhase . Inactive
87- )
88- )
89- ? _context . GameState . ToFlatBuffers ( )
90- : null ;
91- _context . Writer . TryWrite ( new DistributeGamePacket ( packet ) ) ;
78+ ConsiderDistributingPacket ( _context , timeAdvanced ) ;
79+
9280 _context . MatchStarter . SetCurrentMatchPhase (
9381 _context . GameState . MatchPhase ,
9482 _context . GetPlayerSpawner ( )
@@ -189,4 +177,44 @@ public void Cleanup()
189177 }
190178 }
191179 }
180+
181+ private static void ConsiderDistributingPacket ( BridgeContext context , bool timeAdvanced )
182+ {
183+ var config = context . MatchConfig ;
184+ if ( config == null )
185+ return ;
186+
187+ // While game is paused (or similar), we distribute less often
188+ bool due =
189+ context
190+ is {
191+ ticksSkipped : > MAX_TICK_SKIP ,
192+ GameState . MatchPhase : MatchPhase . Replay
193+ or MatchPhase . Paused
194+ or MatchPhase . Ended
195+ or MatchPhase . Inactive
196+ } ;
197+ if ( ! timeAdvanced && ! due )
198+ return ;
199+
200+ // We only distribute the packet if it has all players from the match config,
201+ // - unless the match is already ongoing, then only the bot players are required (humans may leave).
202+ bool inactive =
203+ context . GameState . MatchPhase is MatchPhase . Inactive or MatchPhase . Ended ;
204+ int botCount = config . PlayerConfigurations . Count ( p =>
205+ p . Variety . Type != PlayerClass . Human
206+ ) ;
207+ int requiredPlayers = inactive ? config . PlayerConfigurations . Count : botCount ;
208+ // Assumption: Bots are always the lower indexes
209+ for ( uint i = 0 ; i < requiredPlayers ; i ++ )
210+ {
211+ if ( ! context . GameState . GameCars . ContainsKey ( i ) )
212+ {
213+ return ;
214+ }
215+ }
216+
217+ var packet = context . GameState . ToFlatBuffers ( ) ;
218+ context . Writer . TryWrite ( new DistributeGamePacket ( packet ) ) ;
219+ }
192220}
0 commit comments