Skip to content

Commit

Permalink
Change post-race retry to avoid ghost
Browse files Browse the repository at this point in the history
When doing a retry, the subsequent race will be against the ghost character. Doing a course 'change' rather than a retry avoids this (see #25)
  • Loading branch information
bzier committed Dec 17, 2017
1 parent 3614f42 commit d3aa5da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
25 changes: 18 additions & 7 deletions gym_mupen64plus/envs/MarioKart64/mario_kart_env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import abc
import inspect
import itertools
import os
import yaml
from termcolor import cprint
Expand Down Expand Up @@ -51,7 +52,9 @@ def _reset(self):
if self.episode_over:
self._act(ControllerState.NO_OP, count=59)
self._navigate_post_race_menu()
self._act(ControllerState.NO_OP, count=61) # Wait for race to load
self._act(ControllerState.NO_OP, count=40) # Wait for the map select screen
self._navigate_map_select()
self._act(ControllerState.NO_OP, count=50) # Wait for race to load
self.episode_over = False
else:
self.controller_server.send_controls(ControllerState.NO_OP, start_button=1)
Expand Down Expand Up @@ -212,32 +215,40 @@ def _navigate_map_select(self):
while cur_col != self.MAP_SERIES:
self._press_button(ControllerState.JOYSTICK_RIGHT)
cur_col += 1

self._press_button(ControllerState.A_BUTTON)

# Select map choice
while cur_row != self.MAP_CHOICE:
self._press_button(ControllerState.JOYSTICK_DOWN)
cur_row += 1

self._press_button(ControllerState.A_BUTTON)

# Press OK
self._press_button(ControllerState.A_BUTTON)

def _navigate_post_race_menu(self):
# Times screen
self._press_button(ControllerState.A_BUTTON)
self._act(ControllerState.NO_OP, count=13)

# Post race menu (previous choice selected by default)
# - Retry
# - Course Change
# - Driver Change
# - Quit
# - Replay
# - Save Ghost
self._press_button(ControllerState.A_BUTTON)
self._act(ControllerState.NO_OP, count=13)
self._press_button(ControllerState.A_BUTTON)


# Because the previous choice is selected by default, we navigate to the top entry so our
# navigation is consistent. The menu doesn't cycle top to bottom or bottom to top, so we can
# just make sure we're at the top by hitting up a few times
for _ in itertools.repeat(None, 5):
self._press_button(ControllerState.JOYSTICK_UP)

# Now we are sure to have the top entry selected
# Go down to 'course change'
self._press_button(ControllerState.JOYSTICK_DOWN)
self._press_button(ControllerState.A_BUTTON)

def _set_character(self, character):
characters = {'mario' : (0, 0),
Expand Down
5 changes: 2 additions & 3 deletions gym_mupen64plus/envs/mupen64plus_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import abc
import array
import inspect
import itertools
import json
import os
import subprocess
Expand Down Expand Up @@ -79,7 +80,7 @@ def _step(self, action):
return obs, reward, self.episode_over, {}

def _act(self, action, count=1):
for i in range(count):
for _ in itertools.repeat(None, count):
self.controller_server.send_controls(action)

def _press_button(self, button):
Expand Down Expand Up @@ -255,8 +256,6 @@ def _start_emulator(self,
self.mss_grabber = mss.mss()
time.sleep(2) # Give mss a couple seconds to initialize; also may not be necessary

time.sleep(30) # TODO: Remove... added for testing (give time to connect with VNC client)

# Restore the DISPLAY env var
os.environ["DISPLAY"] = initial_disp
cprint('Changed back to DISPLAY %s' % os.environ["DISPLAY"], 'red')
Expand Down

0 comments on commit d3aa5da

Please sign in to comment.