From 16dafe3893f5259e658f7841bd51ec924dd9727d Mon Sep 17 00:00:00 2001 From: Rolv-Arild Date: Thu, 9 Jan 2025 11:59:25 +0100 Subject: [PATCH 1/2] Add some convenience methods to Action --- rlgym_tools/rocket_league/misc/action.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/rlgym_tools/rocket_league/misc/action.py b/rlgym_tools/rocket_league/misc/action.py index dc3a1bc..b85f360 100644 --- a/rlgym_tools/rocket_league/misc/action.py +++ b/rlgym_tools/rocket_league/misc/action.py @@ -2,11 +2,15 @@ import numpy as np +INVERT_ACTION = np.array([1, -1, 1, -1, -1, 1, 1, 1], dtype=np.float32) # Invert steer, yaw and roll + @dataclass(slots=True) class Action: """ Dataclass representing an action in the environment. + RLGym uses numpy arrays, but this class can be used in place of them for better readability, + type hints and pretty printing. """ throttle: float = 0.0 steer: float = 0.0 @@ -23,10 +27,21 @@ def from_numpy(cls, action: np.ndarray): def to_numpy(self): return np.array( - [self.throttle, self.steer, self.pitch, self.yaw, self.roll, self.jump, self.boost, self.handbrake], + (self.throttle, self.steer, self.pitch, self.yaw, self.roll, self.jump, self.boost, self.handbrake), dtype=np.float32 ) + def mirror(self): + return ~self + + def __invert__(self): + # ~ operator mirrors the action + return Action.from_numpy(self.to_numpy() * INVERT_ACTION) + + def __array__(self): + # Make this class compatible with numpy functions + return self.to_numpy() + def __repr__(self): def format_bool(b): return int(b) From 9bc24d812b3593f746c1cd6a0c20f65320c453d1 Mon Sep 17 00:00:00 2001 From: Rolv-Arild Date: Thu, 9 Jan 2025 11:59:35 +0100 Subject: [PATCH 2/2] Add comments to ReplayFrame describing attributes --- rlgym_tools/rocket_league/replays/replay_frame.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rlgym_tools/rocket_league/replays/replay_frame.py b/rlgym_tools/rocket_league/replays/replay_frame.py index 6565773..b85b98f 100644 --- a/rlgym_tools/rocket_league/replays/replay_frame.py +++ b/rlgym_tools/rocket_league/replays/replay_frame.py @@ -9,10 +9,10 @@ @dataclass(slots=True) class ReplayFrame: - state: GameState - actions: Dict[int, np.ndarray] - update_age: Dict[int, float] - scoreboard: ScoreboardInfo - episode_seconds_remaining: float - next_scoring_team: Optional[int] - winning_team: Optional[int] + state: GameState # Current state of the game + actions: Dict[int, np.ndarray] # Actions for each player + update_age: Dict[int, float] # Time since the replay updated values for each car (>0 means state is interpolated) + scoreboard: ScoreboardInfo # Current scoreboard + episode_seconds_remaining: float # Time remaining until someone scores or ball hits ground at 0s + next_scoring_team: Optional[int] # Team that scores the next goal, None if ball hits ground at 0s + winning_team: Optional[int] # Team that wins the game, None if game ended without a winner