Skip to content

Commit 1d7542c

Browse files
authored
Merge pull request #4 from VirxEC/better_state_setting
Better state setting API
2 parents 7722c50 + 4d3aacf commit 1d7542c

File tree

8 files changed

+149
-26
lines changed

8 files changed

+149
-26
lines changed

rlbot/managers/bot.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,36 @@ def send_match_comm(
209209
)
210210
)
211211

212-
def set_game_state(self, game_state: flat.DesiredGameState):
212+
def set_game_state(
213+
self,
214+
balls: dict[int, flat.DesiredBallState] = {},
215+
cars: dict[int, flat.DesiredCarState] = {},
216+
game_info: Optional[flat.DesiredGameInfoState] = None,
217+
commands: list[flat.ConsoleCommand] = [],
218+
):
213219
"""
214-
Sets the game to the given desired state.
220+
Sets the game to the desired state.
215221
"""
222+
223+
game_state = flat.DesiredGameState(
224+
game_info_state=game_info, console_commands=commands
225+
)
226+
227+
# convert the dictionaries to lists by
228+
# filling in the blanks with empty states
229+
230+
if balls:
231+
max_entry = max(balls.keys())
232+
game_state.ball_states = [
233+
balls.get(i, flat.DesiredBallState()) for i in range(max_entry + 1)
234+
]
235+
236+
if cars:
237+
max_entry = max(cars.keys())
238+
game_state.car_states = [
239+
cars.get(i, flat.DesiredCarState()) for i in range(max_entry + 1)
240+
]
241+
216242
self._game_interface.send_game_state(game_state)
217243

218244
def set_loadout(self, loadout: flat.PlayerLoadout, spawn_id: int):

rlbot/managers/hivemind.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,36 @@ def send_match_comm(
218218
)
219219
)
220220

221-
def set_game_state(self, game_state: flat.DesiredGameState):
221+
def set_game_state(
222+
self,
223+
balls: dict[int, flat.DesiredBallState] = {},
224+
cars: dict[int, flat.DesiredCarState] = {},
225+
game_info: Optional[flat.DesiredGameInfoState] = None,
226+
commands: list[flat.ConsoleCommand] = [],
227+
):
222228
"""
223-
Sets the game to the given desired state.
229+
Sets the game to the desired state.
224230
"""
231+
232+
game_state = flat.DesiredGameState(
233+
game_info_state=game_info, console_commands=commands
234+
)
235+
236+
# convert the dictionaries to lists by
237+
# filling in the blanks with empty states
238+
239+
if balls:
240+
max_entry = max(balls.keys())
241+
game_state.ball_states = [
242+
balls.get(i, flat.DesiredBallState()) for i in range(max_entry + 1)
243+
]
244+
245+
if cars:
246+
max_entry = max(cars.keys())
247+
game_state.car_states = [
248+
cars.get(i, flat.DesiredCarState()) for i in range(max_entry + 1)
249+
]
250+
225251
self._game_interface.send_game_state(game_state)
226252

227253
def set_loadout(self, loadout: flat.PlayerLoadout, spawn_id: int):

rlbot/managers/match.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,36 @@ def disconnect(self):
177177
def stop_match(self):
178178
self.rlbot_interface.stop_match()
179179

180-
def set_game_state(self, game_state: flat.DesiredGameState):
180+
def set_game_state(
181+
self,
182+
balls: dict[int, flat.DesiredBallState] = {},
183+
cars: dict[int, flat.DesiredCarState] = {},
184+
game_info: Optional[flat.DesiredGameInfoState] = None,
185+
commands: list[flat.ConsoleCommand] = [],
186+
):
187+
"""
188+
Sets the game to the desired state.
189+
"""
190+
191+
game_state = flat.DesiredGameState(
192+
game_info_state=game_info, console_commands=commands
193+
)
194+
195+
# convert the dictionaries to lists by
196+
# filling in the blanks with empty states
197+
198+
if balls:
199+
max_entry = max(balls.keys())
200+
game_state.ball_states = [
201+
balls.get(i, flat.DesiredBallState()) for i in range(max_entry + 1)
202+
]
203+
204+
if cars:
205+
max_entry = max(cars.keys())
206+
game_state.car_states = [
207+
cars.get(i, flat.DesiredCarState()) for i in range(max_entry + 1)
208+
]
209+
181210
self.rlbot_interface.send_game_state(game_state)
182211

183212
def shut_down(self, ensure_shutdown=True):

rlbot/managers/script.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,36 @@ def send_match_comm(
128128
)
129129
)
130130

131-
def set_game_state(self, game_state: flat.DesiredGameState):
131+
def set_game_state(
132+
self,
133+
balls: dict[int, flat.DesiredBallState] = {},
134+
cars: dict[int, flat.DesiredCarState] = {},
135+
game_info: Optional[flat.DesiredGameInfoState] = None,
136+
commands: list[flat.ConsoleCommand] = [],
137+
):
132138
"""
133-
Sets the game to the given desired state.
139+
Sets the game to the desired state.
134140
"""
141+
142+
game_state = flat.DesiredGameState(
143+
game_info_state=game_info, console_commands=commands
144+
)
145+
146+
# convert the dictionaries to lists by
147+
# filling in the blanks with empty states
148+
149+
if balls:
150+
max_entry = max(balls.keys())
151+
game_state.ball_states = [
152+
balls.get(i, flat.DesiredBallState()) for i in range(max_entry + 1)
153+
]
154+
155+
if cars:
156+
max_entry = max(cars.keys())
157+
game_state.car_states = [
158+
cars.get(i, flat.DesiredCarState()) for i in range(max_entry + 1)
159+
]
160+
135161
self._game_interface.send_game_state(game_state)
136162

137163
def set_loadout(self, loadout: flat.PlayerLoadout, spawn_id: int):

rlbot/version.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "5.0.0-beta.3"
1+
__version__ = "5.0.0-beta.4"
22

33

44
RESET_SEQ = "\033[0m"
@@ -15,6 +15,9 @@ def _get_color(color: int) -> str:
1515
)
1616

1717
RELEASE_NOTES = {
18+
"5.0.0-beta.4": """
19+
Better state setting API
20+
""",
1821
"5.0.0-beta.3": """
1922
Ensure bots don't fall behind the most recent GameTickPacket, without threading
2023
Add `team_color` static method to the rendering manager

tests/atba/atba.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,25 @@ def get_output(self, packet: flat.GameTickPacket) -> flat.ControllerState:
120120
return self.controller
121121

122122
def test_state_setting(self, packet: flat.GameTickPacket):
123-
game_state = flat.DesiredGameState(
124-
[
125-
flat.DesiredBallState(
123+
self.set_game_state(
124+
{
125+
0: flat.DesiredBallState(
126126
flat.DesiredPhysics(
127127
velocity=flat.Vector3Partial(
128128
z=packet.balls[0].physics.velocity.z + 10
129129
)
130130
)
131131
)
132-
],
133-
[
134-
flat.DesiredCarState(
132+
},
133+
{
134+
i: flat.DesiredCarState(
135135
flat.DesiredPhysics(
136-
velocity=flat.Vector3Partial(
137-
z=packet.players[i].physics.velocity.z + 1
138-
)
136+
velocity=flat.Vector3Partial(z=car.physics.velocity.z + 1)
139137
)
140138
)
141-
for i in range(len(packet.players))
142-
],
139+
for i, car in enumerate(packet.players)
140+
},
143141
)
144-
self.set_game_state(game_state)
145142

146143
def test_rendering(self, packet: flat.GameTickPacket):
147144
if not self.needs_render:

tests/run_forever.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,26 @@
1616

1717
current_map = -1
1818

19+
BLUE_NECTO = get_player_config(flat.RLBot(), 0, BOT_PATH)
20+
ORANGE_NECTO = get_player_config(flat.RLBot(), 1, BOT_PATH)
21+
1922
match_settings = flat.MatchSettings(
2023
launcher=flat.Launcher.Steam,
2124
auto_start_bots=True,
2225
game_mode=flat.GameMode.Soccer,
2326
enable_state_setting=True,
2427
existing_match_behavior=flat.ExistingMatchBehavior.Restart,
2528
skip_replays=True,
29+
mutator_settings=flat.MutatorSettings(
30+
match_length=flat.MatchLength.Unlimited,
31+
),
2632
player_configurations=[
27-
get_player_config(flat.RLBot(), 0, BOT_PATH),
28-
get_player_config(flat.RLBot(), 1, BOT_PATH),
33+
BLUE_NECTO,
34+
BLUE_NECTO,
35+
BLUE_NECTO,
36+
ORANGE_NECTO,
37+
ORANGE_NECTO,
38+
ORANGE_NECTO,
2939
],
3040
)
3141

@@ -49,9 +59,7 @@
4959
== flat.GameStateType.Countdown
5060
):
5161
match_manager.set_game_state(
52-
flat.DesiredGameState(
53-
game_info_state=flat.DesiredGameInfoState(game_speed=10)
54-
)
62+
game_info=flat.DesiredGameInfoState(game_speed=2)
5563
)
5664

5765
sleep(1)

tests/run_only.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
from pathlib import Path
22

33
from rlbot.managers import MatchManager
4+
from rlbot.version import print_current_release_notes
45

56
CURRENT_FILE = Path(__file__).parent
67

78
MATCH_CONFIG_PATH = CURRENT_FILE / "hvn.toml"
89

910
if __name__ == "__main__":
11+
print(print_current_release_notes())
12+
13+
# start the match
1014
match_manager = MatchManager()
1115
match_manager.start_match(MATCH_CONFIG_PATH, False)
12-
input()
16+
17+
# wait
18+
input("\nPress any enter to end the match: ")
19+
20+
# end the match and disconnect
1321
match_manager.stop_match()
1422
match_manager.disconnect()

0 commit comments

Comments
 (0)