Skip to content

Commit

Permalink
Disable cut hook. Enable cut scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
thatguy11325 committed May 27, 2024
1 parent 78e1f2c commit eb53a5d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
6 changes: 3 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ debug:
env:
headless: False
stream_wrapper: False
init_state: cut
init_state: cut3
max_steps: 1_000_000
train:
device: cpu
compile: False
compile_mode: default
num_envs: 4
num_envs: 1
envs_per_worker: 1
envs_per_batch: 4
envs_per_batch: 1
batch_size: 16
batch_rows: 4
bptt_horizon: 2
Expand Down
74 changes: 72 additions & 2 deletions pokemonred_puffer/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ def register_hooks(self):
)
self.pyboy.hook_register(None, "HandleBlackOut", self.blackout_hook, None)
self.pyboy.hook_register(None, "SetLastBlackoutMap.done", self.blackout_update_hook, None)
self.pyboy.hook_register(None, "UsedCut.nothingToCut", self.cut_hook, context=True)
self.pyboy.hook_register(None, "UsedCut.canCut", self.cut_hook, context=False)
# self.pyboy.hook_register(None, "UsedCut.nothingToCut", self.cut_hook, context=True)
# self.pyboy.hook_register(None, "UsedCut.canCut", self.cut_hook, context=False)

def update_state(self, state: bytes):
self.reset(seed=random.randint(0, 10), options={"state": state})
Expand Down Expand Up @@ -584,6 +584,76 @@ def run_action_on_emulator(self, action):
self.pyboy.send_input(VALID_ACTIONS[action])
self.pyboy.send_input(VALID_RELEASE_ACTIONS[action], delay=8)
self.pyboy.tick(self.action_freq, render=True)
if self.check_if_party_has_cut():
self.cut_if_next()

def cut_if_next(self):
# https://github.com/pret/pokered/blob/d38cf5281a902b4bd167a46a7c9fd9db436484a7/constants/tileset_constants.asm#L11C8-L11C11
in_erika_gym = self.pyboy.memory[self.pyboy.symbol_lookup("wCurMapTileset")[1]] == 7
in_overworld = self.pyboy.memory[self.pyboy.symbol_lookup("wCurMapTileset")[1]] == 0
if in_erika_gym or in_overworld:
wTileMap = self.pyboy.symbol_lookup("wTileMap")[1]
tileMap = self.pyboy.memory[wTileMap : wTileMap + 20 * 18]
tileMap = np.array(tileMap, dtype=np.uint8)
tileMap = np.reshape(tileMap, (18, 20))
y, x = 8, 8
up, down, left, right = (
tileMap[y - 2 : y, x : x + 2], # up
tileMap[y + 2 : y + 4, x : x + 2], # down
tileMap[y : y + 2, x - 2 : x], # left
tileMap[y : y + 2, x + 2 : x + 4], # right
)

# Gym trees apparently get the same tile map as outside bushes
# GYM = 7
if (in_overworld and 0x3D in up) or (in_erika_gym and 0x50 in up):
self.pyboy.send_input(WindowEvent.PRESS_ARROW_UP)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_UP, delay=8)
self.pyboy.tick(self.action_freq, render=True)
elif (in_overworld and 0x3D in down) or (in_erika_gym and 0x50 in down):
self.pyboy.send_input(WindowEvent.PRESS_ARROW_DOWN)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_DOWN, delay=8)
self.pyboy.tick(self.action_freq, render=True)
elif (in_overworld and 0x3D in left) or (in_erika_gym and 0x50 in left):
self.pyboy.send_input(WindowEvent.PRESS_ARROW_LEFT)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_LEFT, delay=8)
self.pyboy.tick(self.action_freq, render=True)
elif (in_overworld and 0x3D in right) or (in_erika_gym and 0x50 in right):
self.pyboy.send_input(WindowEvent.PRESS_ARROW_RIGHT)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_RIGHT, delay=8)
self.pyboy.tick(self.action_freq, render=True)
else:
return

# open start menu
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_START, delay=8)
self.pyboy.tick(self.action_freq, render=True)
# scroll to pokemon
# 1 is the item index for pokemon
while self.pyboy.memory[self.pyboy.symbol_lookup("wCurrentMenuItem")[1]] != 1:
self.pyboy.send_input(WindowEvent.PRESS_ARROW_DOWN)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_DOWN, delay=8)
self.pyboy.tick(self.action_freq, render=True)
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_A)
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_A, delay=8)
self.pyboy.tick(self.action_freq, render=True)

# find pokemon with cut
while True:
self.pyboy.send_input(WindowEvent.PRESS_ARROW_DOWN)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_DOWN, delay=8)
self.pyboy.tick(self.action_freq, render=True)
party_mon = self.pyboy.memory[self.pyboy.symbol_lookup("wCurrentMenuItem")[1]]
_, addr = self.pyboy.symbol_lookup(f"wPartyMon{party_mon+1}Moves")
if 15 in self.pyboy.memory[addr : addr + 4]:
break

# press a bunch of times
for _ in range(5):
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_A)
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_A, delay=8)
self.pyboy.tick(4 * self.action_freq, render=True)

def hidden_object_hook(self, *args, **kwargs):
hidden_object_id = self.pyboy.memory[self.pyboy.symbol_lookup("wHiddenObjectIndex")[1]]
Expand Down

0 comments on commit eb53a5d

Please sign in to comment.