@@ -102,6 +102,40 @@ def _handle_packet(self, packet: flat.GameTickPacket):
102102 self ._latest_packet = packet
103103 self ._packet_event .set ()
104104
105+ def _packet_preprocess (self , packet : flat .GameTickPacket ) -> bool :
106+ if (
107+ self .index == - 1
108+ or len (packet .players ) <= self .index
109+ or packet .players [self .index ].spawn_id != self .spawn_id
110+ ):
111+ # spawn id should only be 0 if RLBOT_SPAWN_IDS was not set
112+ if self .spawn_id == 0 :
113+ # in this case, if there's only one player, we can assume it's us
114+ player_index = - 1
115+ for i , player in enumerate (packet .players ):
116+ # skip human players/psyonix bots
117+ if not player .is_bot :
118+ continue
119+
120+ if player_index != - 1 :
121+ self .logger .error (
122+ "Multiple bots in the game, please set RLBOT_SPAWN_IDS"
123+ )
124+ return False
125+
126+ player_index = i
127+ self .index = player_index
128+
129+ for i , player in enumerate (packet .players ):
130+ if player .spawn_id == self .spawn_id :
131+ self .index = i
132+ break
133+
134+ if self .index == - 1 :
135+ return False
136+
137+ return True
138+
105139 def _packet_processor (self ):
106140 while self ._run_packet_thread :
107141 self ._packet_event .wait ()
@@ -117,43 +151,15 @@ def _packet_processor(self):
117151
118152 self ._packet_event .clear ()
119153
120- if (
121- self .index == - 1
122- or len (packet .players ) <= self .index
123- or packet .players [self .index ].spawn_id != self .spawn_id
124- ):
125- # spawn id should only be 0 if RLBOT_SPAWN_IDS was not set
126- if self .spawn_id == 0 :
127- # in this case, if there's only one player, we can assume it's us
128- player_index = - 1
129- for i , player in enumerate (packet .players ):
130- # skip human players/psyonix bots
131- if not player .is_bot :
132- continue
133-
134- if player_index != - 1 :
135- self .logger .error (
136- "Multiple bots in the game, please set RLBOT_SPAWN_IDS"
137- )
138- return
139-
140- player_index = i
141- self .index = player_index
142-
143- for i , player in enumerate (packet .players ):
144- if player .spawn_id == self .spawn_id :
145- self .index = i
146- break
147-
148- if self .index == - 1 :
149- return
154+ if not self ._packet_preprocess (packet ):
155+ continue
150156
151157 try :
152158 controller = self .get_output (packet )
153159 except Exception as e :
154160 self .logger .error ("Bot %s returned an error to RLBot: %s" , self .name , e )
155161 print_exc ()
156- return
162+ continue
157163
158164 player_input = flat .PlayerInput (self .index , controller )
159165 self ._game_interface .send_player_input (player_input )
0 commit comments