Skip to content

Commit

Permalink
Add skippable safari zone
Browse files Browse the repository at this point in the history
  • Loading branch information
thatguy11325 committed Aug 11, 2024
1 parent c4c6567 commit 65bd7a6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ env:
auto_solve_strength_puzzles: True
auto_remove_all_nonuseful_items: True
auto_pokeflute: True
skip_safari_zone: True
infinite_money: True
use_global_map: False
save_state: True
Expand Down
10 changes: 10 additions & 0 deletions pokemonred_puffer/data/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,7 @@ class EventFlags(Union):

def __init__(self, emu: PyBoy):
super().__init__()
self.emu = emu
self.asbytes = (c_uint8 * 320)(
*emu.memory[EVENT_FLAGS_START : EVENT_FLAGS_START + EVENTS_FLAGS_LENGTH]
)
Expand All @@ -2588,6 +2589,15 @@ def get_event(self, event_name: str) -> int:
"""
return getattr(self.b, event_name)

def set_event(self, event_name: str, value: bool):
# This is O(N) but it's so rare that I'm not too worried about it
idx = [x[0] for x in self.b._fields_].index(event_name)
addr = EVENT_FLAGS_START + idx // 8
bit = idx % 8

self.emu.memory[addr] = self.emu.memory[addr] & int(value) << bit
setattr(self.b, event_name, int(value))


EVENTS = {
event for event, _, _ in EventFlagsBits._fields_ if not re.search("EVENT_[0-9,A-F]{3}$", event)
Expand Down
14 changes: 12 additions & 2 deletions pokemonred_puffer/data/missable_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,17 @@ class MissableFlags(Union):

def __init__(self, emu: PyBoy):
super().__init__()
self.emu = emu
self.asbytes = (c_uint8 * 32)(*emu.memory[0xD5A6 : 0xD5A6 + 32])

def get_missable(self, missable: str) -> bool:
return bool(getattr(self.b, missable))
def get_missable(self, missable: str) -> int:
return getattr(self.b, missable)

def set_missable(self, missable: str, value: bool):
# This is O(N) but it's so rare that I'm not too worried about it
idx = [x[0] for x in self.b._fields_].index(missable)
addr = 0xD5A6 + idx // 8
bit = idx % 8

self.emu.memory[addr] = self.emu.memory[addr] & int(value) << bit
setattr(self.b, missable, int(value))
25 changes: 25 additions & 0 deletions pokemonred_puffer/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,9 @@ def run_action_on_emulator(self, action):
if self.events.get_event("EVENT_GOT_POKE_FLUTE") and self.auto_pokeflute:
self.use_pokeflute()

if self.get_game_coords() == (18, 4, 7) and self.skip_safari_zone:
self.skip_safari_zone()

# One last tick just in case
self.pyboy.tick(1, render=True)

Expand Down Expand Up @@ -1122,6 +1125,28 @@ def solve_switch_strength_puzzle(self):
self.setup_enable_wild_ecounters()
break

def skip_safari_zone(self):
# First move down
self.pyboy.button("down", 8)
self.pyboy.tick(self.action_freq, render=self.animate_scripts)
_, wBagItems = self.pyboy.symbol_lookup("wBagItems")
_, wNumBagItems = self.pyboy.symbol_lookup("wNumBagItems")
numBagItems = self.read_m(wNumBagItems)
bag = np.array(self.pyboy.memory[wBagItems : wBagItems + 40], dtype=np.uint8)
if self.events.get_event("EVENT_GOT_HM03"):
self.events.set_event("EVENT_GOT_HM03", True)
bag[numBagItems * 2] = Items.HM_03.value
bag[numBagItems * 2 + 1] = 1
numBagItems += 1
if self.missables.get_missable("HS_SAFARI_ZONE_WEST_ITEM_4"):
self.missables.set_missable("HS_SAFARI_ZONE_WEST_ITEM_4", True)
bag[numBagItems * 2] = Items.GOLD_TEETH.value
bag[numBagItems * 2 + 1] = 1
numBagItems += 1
bag[numBagItems * 2 :] = 0xFF
self.pyboy.memory[wBagItems : wBagItems + 40] = bag
self.pyboy.memory[wNumBagItems] = numBagItems

def sign_hook(self, *args, **kwargs):
sign_id = self.pyboy.memory[self.pyboy.symbol_lookup("hSpriteIndexOrTextID")[1]]
map_id = self.pyboy.memory[self.pyboy.symbol_lookup("wCurMap")[1]]
Expand Down

0 comments on commit 65bd7a6

Please sign in to comment.