Skip to content

Commit 4a7288c

Browse files
committed
Async packet handling for hiveminds
1 parent 6b9d35b commit 4a7288c

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

rlbot/managers/bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def _packet_preprocess(self, packet: flat.GameTickPacket) -> bool:
133133

134134
if self.index == -1:
135135
return False
136-
136+
137137
return True
138138

139139
def _packet_processor(self):

rlbot/managers/hivemind.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from threading import Event, Thread
23
from traceback import print_exc
34
from typing import Optional
45

@@ -29,6 +30,12 @@ class Hivemind:
2930
_has_match_settings = False
3031
_has_field_info = False
3132

33+
_latest_packet = flat.GameTickPacket()
34+
_lastest_prediction = flat.BallPrediction()
35+
_packet_event = Event()
36+
_packet_thread = None
37+
_run_packet_thread = True
38+
3239
def __init__(self):
3340
spawn_ids = os.environ.get("RLBOT_SPAWN_IDS")
3441

@@ -49,6 +56,9 @@ def __init__(self):
4956
)
5057
self._game_interface.packet_handlers.append(self._handle_packet)
5158

59+
self._packet_thread = Thread(target=self._packet_processor, daemon=True)
60+
self._packet_thread.start()
61+
5262
self.renderer = Renderer(self._game_interface)
5363

5464
def _initialize_agent(self):
@@ -72,11 +82,11 @@ def _handle_match_settings(self, match_settings: flat.MatchSettings):
7282

7383
# search match settings for our spawn ids
7484
for i, player in enumerate(self.match_settings.player_configurations):
85+
print(player.name)
7586
if player.spawn_id in self.spawn_ids:
7687
self.team = player.team
7788
self.names.append(player.name)
7889
self.loggers[i] = get_logger(player.name)
79-
break
8090

8191
if not self._initialized_bot and self._has_field_info:
8292
self._initialize_agent()
@@ -89,9 +99,13 @@ def _handle_field_info(self, field_info: flat.FieldInfo):
8999
self._initialize_agent()
90100

91101
def _handle_ball_prediction(self, ball_prediction: flat.BallPrediction):
92-
self.ball_prediction = ball_prediction
102+
self._lastest_prediction = ball_prediction
93103

94104
def _handle_packet(self, packet: flat.GameTickPacket):
105+
self._latest_packet = packet
106+
self._packet_event.set()
107+
108+
def _packet_preprocess(self, packet: flat.GameTickPacket) -> bool:
95109
if len(self.indicies) != len(self.spawn_ids) or any(
96110
packet.players[i].spawn_id not in self.spawn_ids for i in self.indicies
97111
):
@@ -102,20 +116,35 @@ def _handle_packet(self, packet: flat.GameTickPacket):
102116
]
103117

104118
if len(self.indicies) != len(self.spawn_ids):
105-
return
119+
return False
106120

107-
try:
108-
controller = self.get_outputs(packet)
109-
except Exception as e:
110-
self._logger.error(
111-
"Hivemind (with %s) returned an error to RLBot: %s", self.names, e
112-
)
113-
print_exc()
114-
return
121+
# print([player.name for player in packet.players], self.spawn_ids, self.indicies)
122+
return True
123+
124+
def _packet_processor(self):
125+
while self._run_packet_thread:
126+
self._packet_event.wait()
127+
128+
self.ball_prediction = self._lastest_prediction
129+
packet = self._latest_packet
130+
131+
self._packet_event.clear()
132+
133+
if not self._packet_preprocess(self._latest_packet):
134+
continue
135+
136+
try:
137+
controller = self.get_outputs(packet)
138+
except Exception as e:
139+
self._logger.error(
140+
"Hivemind (with %s) returned an error to RLBot: %s", self.names, e
141+
)
142+
print_exc()
143+
continue
115144

116-
for index, controller in controller.items():
117-
player_input = flat.PlayerInput(index, controller)
118-
self._game_interface.send_player_input(player_input)
145+
for index, controller in controller.items():
146+
player_input = flat.PlayerInput(index, controller)
147+
self._game_interface.send_player_input(player_input)
119148

120149
def run(
121150
self,

0 commit comments

Comments
 (0)