Skip to content

Commit

Permalink
Update action handling
Browse files Browse the repository at this point in the history
  • Loading branch information
msosav committed Jan 10, 2025
1 parent 1b54a2f commit a4c23b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
44 changes: 34 additions & 10 deletions config/gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,31 @@ def __init__(self, config: dict, debug=False):
self._previous_fitness = 0
self.debug = debug

self.action_freq = config["action_freq"]

if not self.debug:
self.pyboy.set_emulation_speed(0)

self.valid_actions = [
"",
"a",
"b",
"left",
"right",
"up",
"down",
"start",
"select",
WindowEvent.PRESS_ARROW_DOWN,
WindowEvent.PRESS_ARROW_LEFT,
WindowEvent.PRESS_ARROW_RIGHT,
WindowEvent.PRESS_ARROW_UP,
WindowEvent.PRESS_BUTTON_A,
WindowEvent.PRESS_BUTTON_B,
WindowEvent.PRESS_BUTTON_START,
WindowEvent.PRESS_BUTTON_SELECT
]

self.release_actions = [
WindowEvent.RELEASE_ARROW_DOWN,
WindowEvent.RELEASE_ARROW_LEFT,
WindowEvent.RELEASE_ARROW_RIGHT,
WindowEvent.RELEASE_ARROW_UP,
WindowEvent.RELEASE_BUTTON_A,
WindowEvent.RELEASE_BUTTON_B,
WindowEvent.RELEASE_BUTTON_START,
WindowEvent.RELEASE_BUTTON_SELECT
]

self.observation_space = Dict({
Expand Down Expand Up @@ -73,7 +85,8 @@ def step(self, action):
else:
self.pyboy.button(self.valid_actions[action])

self.pyboy.tick()
def step(self, action):
self.run_action(action)

done = self.__game_over()

Expand All @@ -87,6 +100,17 @@ def step(self, action):

return observation, reward, done, truncated, info

def run_action(self, action):
self.pyboy.send_input(self.valid_actions[action])
press_step = 8

self.action_freq = 24

self.pyboy.tick(press_step)
self.pyboy.send_input(self.release_actions[action])
self.pyboy.tick(self.action_freq - press_step - 1)
self.pyboy.tick(1, True)

def __game_over(self):
if self.pyboy.memory[ADDR_CURRENT_HEALTH] == 0:
return True
Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"rom_path": "roms/ZeldaLinksAwakening.gb",
"checkpoint_dir": "checkpoints/",
"log_dir": "logs/",
"action_freq": 24,
"game_with_sound": True,
}

Expand Down

0 comments on commit a4c23b6

Please sign in to comment.