-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Control Generator #110
Open
JayTheYggdrasil
wants to merge
6
commits into
SaltieRL:master
Choose a base branch
from
JayTheYggdrasil:control_generator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Control Generator #110
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
92a02f7
Control Generator
JayTheYggdrasil 5e9c9e6
Updated Controls Generator
JayTheYggdrasil c98c6e5
Added Types
JayTheYggdrasil 1098878
Fixin stuffs
JayTheYggdrasil 3201b0d
Added List types
JayTheYggdrasil cb26953
Updated Doc Comments
JayTheYggdrasil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import os | ||
import sys | ||
path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
sys.path.insert(0, path) | ||
|
||
import numpy as np | ||
from carball.controls.controls import ControlsCreator | ||
from carball.json_parser.game import Game | ||
from carball.json_parser.player import Player | ||
import carball | ||
from data_generator.base_generator import BaseDataGenerator | ||
from rlbot.utils.logging_utils import get_logger | ||
from typing import List | ||
|
||
|
||
class ReplayControlGen(BaseDataGenerator): | ||
""" | ||
Generates Controler data from a set of local replays. | ||
Use method get_data() for Iterable | ||
""" | ||
|
||
def __init__(self, filepaths: List[str]): | ||
""" | ||
Arguments: | ||
filepaths -- a list of paths to replay files, exclude the .replay file extensions | ||
""" | ||
self.files = filepaths | ||
self.file = 0 | ||
self.game = gameFromFile(self.files[self.file]) | ||
self.maxFrame = len(self.game.frames.time) | ||
self.currentFrame = 1 | ||
self.player = 0 | ||
|
||
def initialize(self, **kwargs): | ||
pass | ||
|
||
def has_next(self): | ||
#If we are at the max frame, the last player and the last file then we have no more data to collect. | ||
if self.currentFrame >= self.maxFrame and self.player >= len( self.game.players ) - 1 and self.file >= len( self.files ) - 1: | ||
JayTheYggdrasil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return False | ||
return True | ||
|
||
def _next(self) -> List[float]: | ||
""" | ||
:return: list, [ Throttle, Steer, pitch, yaw, roll, jump, boost, handbrake ] | ||
""" | ||
if self.currentFrame >= self.maxFrame: | ||
self.player += 1 | ||
self.currentFrame = 1 | ||
if self.player >= len(self.game.players) and self.file < len(self.files): | ||
self.file += 1 | ||
self.game = gameFromFile(self.files[self.file]) | ||
self.maxFrame = len(self.game.frames.time) | ||
self.player = 0 | ||
self.currentFrame = 1 | ||
player = self.game.players[self.player] | ||
c = getControls(player, self.currentFrame) | ||
self.currentFrame += 1 | ||
return c | ||
|
||
|
||
def gameFromFile(replaypath: str) -> Game: | ||
""" | ||
Instantiats a game object from a replay and creates controls data. | ||
Creates json file representaion of replay. | ||
Argument: | ||
replaypath -- filepath to a replay, exclude .replay extension. | ||
:return: Game | ||
""" | ||
_json = carball.decompile_replay(replaypath + '.replay', | ||
replaypath + '.json', | ||
overwrite=True) | ||
|
||
game = Game() | ||
game.initialize(loaded_json=_json) | ||
|
||
ControlsCreator().get_controls(game) | ||
return game | ||
|
||
|
||
def getControls(player: Player, frame: int) -> List[float]: | ||
""" | ||
Gets the controls of a player at a frame | ||
Arguments: | ||
player -- player object, game.players[p] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not the python style i am normally used to seeing. try this instead example: |
||
frame -- frame to get the controls from | ||
:return: list of format [ Throttle, Steer, pitch, yaw, roll, jump, boost, handbrake ] | ||
""" | ||
c = player.controls | ||
throttle = c.throttle.get(frame) | ||
steer = c.steer.get(frame) | ||
pitch = c.pitch.get(frame) | ||
yaw = c.yaw.get(frame) | ||
roll = c.roll.get(frame) | ||
jump = c.jump.get(frame) | ||
boost = c.boost.get(frame) | ||
handbrake = c.handbrake.get(frame) | ||
controls = [throttle, steer, pitch, yaw, roll, jump, boost, handbrake] | ||
for c in range(len(controls)): | ||
control = controls[c] | ||
if control == False or control == None or np.isnan(control): | ||
controls[c] = 0 | ||
elif control == True: | ||
controls[c] = 1 | ||
return controls |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this a python style comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what that means...