def merge(*args, quiet=True): import retro known_hashes = {} imported_games = 0 for game in retro.list_games(): shafile = os.path.join(retro.get_game_path(game), 'rom.sha') with open(shafile) as f: shas = f.read().strip().split('\n') for ext, platform in retro.EMU_EXTENSIONS.items(): if game.endswith('-' + platform): break for sha in shas: known_hashes[sha] = (game, ext) for rom in args: try: data, hash = groom_rom(rom) except IOError: continue if hash in known_hashes: game, ext = known_hashes[hash] if not quiet: print('Importing', game) with open(os.path.join(retro.get_game_path(game), 'rom%s' % ext), 'wb') as f: f.write(data) imported_games += 1 if not quiet: print('Imported %i games' % imported_games)
def verify_roms(): warnings = [] errors = [] for game in retro.list_games(): w, e = verify_rom(game) warnings.extend(w) errors.extend(e) return warnings, errors
def list_states(args): if args.game: games = args.game else: games = retro.list_games() games.sort() for game in games: states = retro.list_states(game) print(game + ':') states.sort() for state in states: print(' ' + state)
def verify_json(): warnings = [] errors = [] for game in retro.list_games(): gamedir = retro.get_game_path(game) w, e = verify_data(game) warnings.extend(w) errors.extend(e) w, e = verify_scenario(game) warnings.extend(w) errors.extend(e) return warnings, errors
def scan_missing(): missing = [] for game in retro.list_games(): gamedir = retro.get_game_path(game) if not os.path.isfile(os.path.join(gamedir, 'data.json')): missing.append((game, 'data.json')) if not os.path.isfile(os.path.join(gamedir, 'scenario.json')): missing.append((game, 'scenario.json')) if not os.path.isfile(os.path.join(gamedir, 'metadata.json')): missing.append((game, 'metadata.json')) if not retro.list_states(game): missing.append((game, '*.state')) if not os.path.isfile(os.path.join(gamedir, 'rom.sha')): missing.append((game, 'rom.sha')) return missing
def verify_hash_collisions(): errors = [] seen_hashes = {} for game in retro.list_games(): gamedir = retro.get_game_path(game) try: with open(os.path.join(gamedir, 'rom.sha')) as f: expected_shas = f.read().strip().split('\n') except IOError: continue for expected_sha in expected_shas: seen = seen_hashes.get(expected_sha, []) seen.append(game) seen_hashes[expected_sha] = seen for sha, games in seen_hashes.items(): if len(games) < 2: continue for game in games: errors.append((game, 'sha duplicate')) return [], errors
def main(): parser = argparse.ArgumentParser() parser.add_argument('--game', help='retro game to use') parser.add_argument('--state', help='retro state to start from') parser.add_argument('--scenario', help='scenario to use', default='scenario') args = parser.parse_args() if args.game is None: print('Please specify a game with --game <game>') print('Available games:') for game in sorted(retro.list_games()): print(game) sys.exit(1) if args.state is None: print('Please specify a state with --state <state>') print('Available states:') for state in sorted(retro.list_states(args.game)): print(state) sys.exit(1) env = retro.make(game=args.game, state=args.state, use_restricted_actions=retro.ACTIONS_ALL, scenario=args.scenario) obs = env.reset() screen_height, screen_width = obs.shape[:2] random.seed(0) key_handler = pyglet.window.key.KeyStateHandler() win_width = 2000 win_height = win_width * screen_height // screen_width win = pyglet.window.Window(width=win_width, height=win_height, vsync=False) if hasattr(win.context, '_nscontext'): pixel_scale = win.context._nscontext.view().backingScaleFactor() win.width = win.width // pixel_scale win.height = win.height // pixel_scale joysticks = pyglet.input.get_joysticks() if len(joysticks) > 0: joystick = joysticks[0] joystick.open() else: joystick = None win.push_handlers(key_handler) key_previous_states = {} button_previous_states = {} steps = 0 recorded_actions = [] recorded_states = [] pyglet.app.platform_event_loop.start() fps_display = pyglet.clock.ClockDisplay() clock.set_fps_limit(60) glEnable(GL_TEXTURE_2D) texture_id = GLuint(0) glGenTextures(1, ctypes.byref(texture_id)) glBindTexture(GL_TEXTURE_2D, texture_id) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, None) while not win.has_exit: win.dispatch_events() win.clear() keys_clicked = set() keys_pressed = set() for key_code, pressed in key_handler.items(): if pressed: keys_pressed.add(key_code) if not key_previous_states.get(key_code, False) and pressed: keys_clicked.add(key_code) key_previous_states[key_code] = pressed buttons_clicked = set() buttons_pressed = set() if joystick is not None: for button_code, pressed in enumerate(joystick.buttons): if pressed: buttons_pressed.add(button_code) if not button_previous_states.get(button_code, False) and pressed: buttons_clicked.add(button_code) button_previous_states[button_code] = pressed if keycodes.R in keys_clicked or buttoncodes.LEFT_BUMPER in buttons_clicked: if len(recorded_states) > 1: recorded_states.pop() steps, save_state = recorded_states.pop() recorded_states = recorded_states[:steps] recorded_actions = recorded_actions[:steps] env.em.set_state(save_state) if keycodes.ESCAPE in keys_pressed or buttoncodes.XBOX in buttons_clicked: # record all the actions so far to a bk2 and exit i = 0 while True: movie_filename = 'human/%s/%s/%s-%s-%04d.bk2' % ( args.game, args.scenario, args.game, args.state, i) if not os.path.exists(movie_filename): break i += 1 os.makedirs(os.path.dirname(movie_filename), exist_ok=True) env.record_movie(movie_filename) env.reset() for step, act in enumerate(recorded_actions): if step % 1000 == 0: print('saving %d/%d' % (step, len(recorded_actions))) env.step(act) env.stop_record() print('complete') sys.exit(1) inputs = { 'A': keycodes.Z in keys_pressed or buttoncodes.A in buttons_pressed, 'B': keycodes.X in keys_pressed or buttoncodes.B in buttons_pressed, 'C': keycodes.C in keys_pressed, 'X': keycodes.A in keys_pressed or buttoncodes.X in buttons_pressed, 'Y': keycodes.S in keys_pressed or buttoncodes.Y in buttons_pressed, 'Z': keycodes.D in keys_pressed, 'UP': keycodes.UP in keys_pressed or buttoncodes.D_UP in buttons_pressed, 'DOWN': keycodes.DOWN in keys_pressed or buttoncodes.D_DOWN in buttons_pressed, 'LEFT': keycodes.LEFT in keys_pressed or buttoncodes.D_LEFT in buttons_pressed, 'RIGHT': keycodes.RIGHT in keys_pressed or buttoncodes.D_RIGHT in buttons_pressed, 'MODE': keycodes.TAB in keys_pressed or buttoncodes.SELECT in buttons_pressed, 'START': keycodes.ENTER in keys_pressed or buttoncodes.START in buttons_pressed, } action = [inputs[b] for b in env.BUTTONS] if steps % SAVE_PERIOD == 0: recorded_states.append((steps, env.em.get_state())) obs, rew, done, info = env.step(action) recorded_actions.append(action) steps += 1 glBindTexture(GL_TEXTURE_2D, texture_id) video_buffer = ctypes.cast(obs.tobytes(), ctypes.POINTER(ctypes.c_short)) glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, obs.shape[1], obs.shape[0], GL_RGB, GL_UNSIGNED_BYTE, video_buffer) x = 0 y = 0 h = win.height w = win.width pyglet.graphics.draw( 4, pyglet.gl.GL_QUADS, ('v2f', [x, y, x + w, y, x + w, y + h, x, y + h]), ('t2f', [0, 1, 1, 1, 1, 0, 0, 0]), ) fps_display.draw() win.flip() # process joystick events timeout = clock.get_sleep_time(False) pyglet.app.platform_event_loop.step(timeout) clock.tick() pyglet.app.platform_event_loop.stop()
def list_games(args): games = retro.list_games() if args.system: games = [game for game in games if game.endswith('-' + args.system)] games.sort() print(*games, sep='\n')
""" Environments and wrappers for Sonic training. """ import gym import numpy as np import retro from atari_wrappers import WarpFrame, FrameStack count = 0 envs = [] for game in retro.list_games(): if "Sonic" in game: for state in retro.list_states(game): envs.append((game, state)) count += 1 def make_envs(stack=True, scale_rew=True, backtracking=True, num=count): return [ make_env(stack=stack, scale_rew=scale_rew, backtracking=backtracking, i=i) for i in range(num) ] def make_env(stack=True, scale_rew=True, backtracking=True, i=0): """ Create an environment with some standard wrappers.
import pytest import retro import os import warnings as w import subprocess warnings = [] errors = [] @pytest.fixture(params=[g.replace('-', '_') for g in retro.list_games()]) def game(request): return request.param.replace('_', '-') def error(test, info): global errors errors.append((test, info)) def warn(test, info): global warnings w.warn('%s: %s' % (test, info)) warnings.append((test, info)) def handle(warnings, errors): for warning in warnings: warn(*warning) for err in errors: error(*err)
import retro print(retro.list_games())
def main(): #ensure directory hierarchy is made dirs = ['logs', 'logs/model', 'logs/records', 'logs/tmp'] for path in dirs: if not os.path.exists(path): os.makedirs(path) #clean directorys but keep hierarchy if not path == dirs[0] and not params.recover: for old_file in os.listdir(path): os.remove(os.path.join(path, old_file)) #init the environment depending on the name if params.env_name in retro.list_games(): #init env without recorder env = retro.make(game=params.env_name, state=params.env_state, use_restricted_actions=retro.ACTIONS_DISCRETE) else: env = gym.make(params.env_name) env.seed(params.seed) print('[+] environment %s initialized' % params.env_name) #since we are preprocessing state state = env.reset() n_actions = env.action_space.n #discrete env state_shape = worker(None).process_state(state).shape print('[*] state shape: %s --> %s\n[*] actions: %s' % (state.shape, state_shape, n_actions)) agent = worker(model(state_shape, n_actions, recover=params.recover)) print('[+] worker %s' % agent.model.status) agent.train(env, episodes=params.train_episodes, max_steps=params.train_max_steps, batch_size=params.train_batch, print_interval=params.train_print_interval) #close train env, create new instance to record test games if params.env_name in retro.list_games(): env.close() env = retro.make(game='SonicTheHedgehog-Genesis', state='GreenHillZone.Act1', record=os.path.join(params.out_dir, 'records'), use_restricted_actions=retro.ACTIONS_DISCRETE) else: env.close() #func that indicates which episodes to record and write vc = lambda n: n in [ int(x) for x in np.linspace(params.test_episodes - 1, 0, params. test_records) ] #wrapper that records episodes #fails on reset if game not done env = gym.wrappers.Monitor(env, directory=os.path.join( params.out_dir, 'records'), force=True, video_callable=vc) #test and close the env agent.test(env, episodes=params.test_episodes, max_steps=params.test_max_steps, out_dir=params.out_dir, print_interval=params.test_print_interval) env.close()