summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pokemontools/vba/battle.py362
-rw-r--r--pokemontools/vba/vba.py69
-rw-r--r--tests/test_vba_battle.py28
3 files changed, 434 insertions, 25 deletions
diff --git a/pokemontools/vba/battle.py b/pokemontools/vba/battle.py
index 87cd7b1..39d7047 100644
--- a/pokemontools/vba/battle.py
+++ b/pokemontools/vba/battle.py
@@ -38,7 +38,7 @@ class Battle(EmulatorController):
"""
Detects if the battle is waiting for player input.
"""
- return self.is_player_turn() or self.is_mandatory_switch()
+ return self.is_player_turn() or self.is_mandatory_switch() or self.is_switch_prompt() or self.is_levelup_screen() or self.is_make_room_for_move_prompt()
def is_fight_pack_run_menu(self):
"""
@@ -49,12 +49,160 @@ class Battle(EmulatorController):
screentext = self.emulator.get_text()
return all([sign in screentext for sign in signs])
+ def select_battle_menu_action(self, action, execute=True):
+ """
+ Moves the cursor to the requested action and selects it.
+
+ :param action: fight, pkmn, pack, run
+ """
+ if not self.is_fight_pack_run_menu():
+ raise Exception(
+ "This isn't the fight-pack-run menu."
+ )
+
+ action = action.lower()
+
+ action_map = {
+ "fight": (1, 1),
+ "pkmn": (1, 2),
+ "pack": (2, 1),
+ "run": (2, 2),
+ }
+
+ if action not in action_map.keys():
+ raise Exception(
+ "Unexpected requested action {0}".format(action)
+ )
+
+ current_row = self.emulator.vba.read_memory_at(0xcfa9)
+ current_column = self.emulator.vba.read_memory_at(0xcfaa)
+
+ direction = None
+ if current_row != action_map[action][0]:
+ if current_row > action_map[action][0]:
+ direction = "u"
+ elif current_row < action_map[action][0]:
+ direction = "d"
+
+ self.emulator.vba.press(direction, hold=5, after=10)
+
+ direction = None
+ if current_column != action_map[action][1]:
+ if current_column > action_map[action][1]:
+ direction = "l"
+ elif current_column < action_map[action][1]:
+ direction = "r"
+
+ self.emulator.vba.press(direction, hold=5, after=10)
+
+ # now select the action
+ if execute:
+ self.emulator.vba.press("a", hold=5, after=100)
+
+ def select_attack(self, move_number=1, hold=5, after=10):
+ """
+ Moves the cursor to the correct attack in the menu and presses the
+ button.
+
+ :param move_number: the attack number on the FIGHT menu. Note that this
+ starts from 1.
+ :param hold: how many frames to hold each button press
+ :param after: how many frames to wait after each button press
+ """
+ # TODO: detect fight menu and make sure it's detected here.
+
+ pp_address = 0xc634 + (move_number - 1)
+ pp = self.emulator.vba.read_memory_at(pp_address)
+
+ # detect zero pp because i don't want to write a way to inform the
+ # caller that there was no more pp. Just check the pp yourself.
+ if pp == 0:
+ raise BattleException(
+ "Move {num} has no more PP.".format(
+ num=move_number,
+ )
+ )
+
+ valid_selection_states = (1, 2, 3, 4)
+
+ selection = self.emulator.vba.read_memory_at(0xcfa9)
+
+ while selection != move_number:
+ if selection not in valid_selection_states:
+ raise BattleException(
+ "The current selected attack is out of bounds: {num}".format(
+ num=selection,
+ )
+ )
+
+ direction = None
+
+ if selection > move_number:
+ direction = "d"
+ elif selection < move_number:
+ direction = "u"
+ else:
+ # probably never happens
+ raise BattleException(
+ "Not sure what to do here."
+ )
+
+ # press the arrow button
+ self.emulator.vba.press(direction, hold=hold, after=after)
+
+ # let's see what the current selection is
+ selection = self.emulator.vba.read_memory_at(0xcfa9)
+
+ # press to choose the attack
+ self.emulator.vba.press("a", hold=hold, after=after)
+
+ def fight(self, move_number):
+ """
+ Select FIGHT from the flight-pack-run menu and select the move
+ identified by move_number.
+ """
+ # make sure the menu is detected
+ if not self.is_fight_pack_run_menu():
+ raise BattleException(
+ "Wrong menu. Can't press FIGHT here."
+ )
+
+ # select FIGHT
+ self.select_battle_menu_action("fight")
+
+ # select the requested attack
+ self.select_attack(move_number)
+
def is_player_turn(self):
"""
Detects if the battle is waiting for the player to choose an attack.
"""
return self.is_fight_pack_run_menu()
+ def is_trainer_switch_prompt(self):
+ """
+ Detects if the battle is waiting for the player to choose whether or
+ not to switch pokemon. This is the prompt that asks yes/no for whether
+ to switch pokemon, like if the trainer is switching pokemon at the end
+ of a turn set.
+ """
+ return self.emulator.is_trainer_switch_prompt()
+
+ def is_wild_switch_prompt(self):
+ """
+ Detects if the battle is waiting for the player to choose whether or
+ not to continue to fight the wild pokemon.
+ """
+ return self.emulator.is_wild_switch_prompt()
+
+ def is_switch_prompt(self):
+ """
+ Detects both trainer and wild switch prompts (for prompting whether to
+ switch pokemon). This is a yes/no box and not the actual pokemon
+ selection menu.
+ """
+ return self.is_trainer_switch_prompt() or self.is_wild_switch_prompt()
+
def is_mandatory_switch(self):
"""
Detects if the battle is waiting for the player to choose a next
@@ -72,6 +220,79 @@ class Battle(EmulatorController):
else:
return False
+ def is_levelup_screen(self):
+ """
+ Detects the levelup stats screen.
+ """
+ # This is implemented as reading some text on the screen instead of
+ # using get_text() because checking every loop is really slow.
+
+ address = 0xc50f
+ values = [146, 143, 130, 139]
+
+ for (index, value) in enumerate(values):
+ if self.emulator.vba.read_memory_at(address + index) != value:
+ return False
+ else:
+ return True
+
+ def is_evolution_screen(self):
+ """
+ What? MEW is evolving!
+ """
+ address = 0xc5e4
+
+ values = [164, 181, 174, 171, 181, 168, 173, 166, 231]
+
+ for (index, value) in enumerate(values):
+ if self.emulator.vba.read_memory_at(address + index) != value:
+ return False
+ else:
+ # also check "What?"
+ what_address = 0xc5b9
+ what_values = [150, 167, 160, 179, 230]
+ for (index, value) in enumerate(what_values):
+ if self.emulator.vba.read_memory_at(what_address + index) != value:
+ return False
+ else:
+ return True
+
+ def is_evolved_screen(self):
+ """
+ Checks if the screen is the "evolved into METAPOD!" screen. Note that
+ this only works inside of a battle. This is because there may be other
+ text boxes that have the same text when outside of battle. But within a
+ battle, this is probably the only time the text "evolved into ... !" is
+ seen.
+ """
+ if not self.is_in_battle():
+ return False
+
+ address = 0x4bb1
+ values = [164, 181, 174, 171, 181, 164, 163, 127, 168, 173, 179, 174, 79]
+
+ for (index, value) in enumerate(values):
+ if self.emulator.vba.read_memory_at(address + index) != value:
+ return False
+ else:
+ return True
+
+ def is_make_room_for_move_prompt(self):
+ """
+ Detects the prompt that asks whether to make room for a move.
+ """
+ if not self.is_in_battle():
+ return False
+
+ address = 0xc5b9
+ values = [172, 174, 181, 164, 127, 179, 174, 127, 172, 160, 170, 164, 127, 177, 174, 174, 172]
+
+ for (index, value) in enumerate(values):
+ if self.emulator.vba.read_memory_at(address + index) != value:
+ return False
+ else:
+ return True
+
def skip_start_text(self, max_loops=20):
"""
Skip any initial conversation until the player can select an action.
@@ -112,8 +333,23 @@ class Battle(EmulatorController):
"""
Waits until the battle needs player input.
"""
- while not self.is_input_required():
- self.emulator.text_wait()
+ # callback causes text_wait to exit when the callback returns True
+ def is_in_battle_checker():
+ result = (self.emulator.vba.read_memory_at(0xd22d) == 0) and (self.emulator.vba.read_memory_at(0xc734) != 0)
+
+ # but also, jump out if it's the stats screen
+ result = result or self.is_levelup_screen()
+
+ # jump out if it's the "make room for a new move" screen
+ result = result or self.is_make_room_for_move_prompt()
+
+ # stay in text_wait if it's the evolution screen
+ result = result and not self.is_evolution_screen()
+
+ return result
+
+ while not self.is_input_required() and self.is_in_battle():
+ self.emulator.text_wait(callback=is_in_battle_checker)
# let the text draw so that the state is more obvious
self.emulator.vba.step(count=10)
@@ -128,20 +364,40 @@ class Battle(EmulatorController):
# xyz wants to battle, a wild foobar appeared
self.skip_start_text()
+ # skip a few hundred frames
+ self.emulator.vba.step(count=100)
+
+ wild = (self.emulator.vba.read_memory_at(0xd22d) == 1)
+
while self.is_in_battle():
self.skip_until_input_required()
+ if not self.is_in_battle():
+ continue
+
if self.is_player_turn():
# battle hook provides input to handle this situation
self.handle_turn()
+ elif self.is_trainer_switch_prompt():
+ self.handle_trainer_switch_prompt()
+ elif self.is_wild_switch_prompt():
+ self.handle_wild_switch_prompt()
elif self.is_mandatory_switch():
# battle hook provides input to handle this situation too
self.handle_mandatory_switch()
+ elif self.is_levelup_screen():
+ self.emulator.vba.press("a", hold=5, after=30)
+ elif self.is_evolved_screen():
+ self.emulator.vba.step(count=30)
+ elif self.is_make_room_for_move_prompt():
+ self.handle_make_room_for_move()
else:
raise BattleException("unknown state, aborting")
# "how did i lose? wah"
- self.skip_end_text()
+ # TODO: this doesn't happen for wild battles
+ if not wild:
+ self.skip_end_text()
# TODO: return should indicate win/loss (blackout)
@@ -151,6 +407,20 @@ class Battle(EmulatorController):
"""
raise NotImplementedError
+ def handle_trainer_switch_prompt(self):
+ """
+ The trainer is switching pokemon. The game asks yes/no for whether or
+ not the player would like to switch.
+ """
+ raise NotImplementedError
+
+ def handle_wild_switch_prompt(self):
+ """
+ The wild pokemon defeated the party pokemon. This is the yes/no box for
+ whether to switch pokemon or not.
+ """
+ raise NotImplementedError
+
def handle_turn(self):
"""
Take actions inside of a battle based on the game state.
@@ -159,35 +429,93 @@ class Battle(EmulatorController):
class BattleStrategy(Battle):
"""
- Throw a pokeball until everyone dies.
+ This class shows the relevant methods to make a battle handler.
"""
def handle_mandatory_switch(self):
"""
Something fainted, pick the next mon.
"""
- for pokemon in self.emulator.party:
- if pokemon.hp > 0:
- break
- else:
- # the game failed to do a blackout.. not sure what to do now.
- raise BattleException("No partymons left. wtf?")
+ raise NotImplementedError
+
+ def handle_trainer_switch_prompt(self):
+ """
+ The trainer is switching pokemon. The game asks yes/no for whether or
+ not the player would like to switch.
+ """
+ raise NotImplementedError
- return pokemon.id
+ def handle_wild_switch_prompt(self):
+ """
+ The wild pokemon defeated the party pokemon. This is the yes/no box for
+ whether to switch pokemon or not.
+ """
+ raise NotImplementedError
def handle_turn(self):
"""
Take actions inside of a battle based on the game state.
"""
- self.throw_pokeball()
+ raise NotImplementedError
+
+ def handle_make_room_for_move(self):
+ """
+ Choose yes/no then handle learning the move.
+ """
+ raise NotImplementedError
-class SimpleBattleStrategy(BattleStrategy):
+class SpamBattleStrategy(BattleStrategy):
"""
- Attack the enemy with the first move.
+ A really simple battle strategy that always picks the first move of the
+ first pokemon to attack the enemy.
"""
def handle_turn(self):
"""
- Always attack the enemy with the first move.
+ Always picks the first move of the current pokemon.
+ """
+ self.fight(1)
+
+ def handle_trainer_switch_prompt(self):
+ """
+ The trainer is switching pokemon. The game asks yes/no for whether or
+ not the player would like to switch.
+ """
+ # decline
+ self.emulator.vba.press(["b"], hold=5, after=10)
+
+ def handle_wild_switch_prompt(self):
"""
- self.attack(self.battle.party[0].moves[0].name)
+ The wild pokemon defeated the party pokemon. This is the yes/no box for
+ whether to switch pokemon or not.
+ """
+ # why not just make a battle strategy that doesn't lose?
+ # TODO: Note that the longer "after" value is required here.
+ self.emulator.vba.press("a", hold=5, after=30)
+
+ self.handle_mandatory_switch()
+
+ def handle_mandatory_switch(self):
+ """
+ Something fainted, pick the next mon.
+ """
+
+ # TODO: make a better selector for which pokemon.
+
+ # now scroll down
+ self.emulator.vba.press("d", hold=5, after=10)
+
+ # select this mon
+ self.emulator.vba.press("a", hold=5, after=30)
+
+ def handle_make_room_for_move(self):
+ """
+ Choose yes/no then handle learning the move.
+ """
+ # make room? no
+ self.emulator.vba.press("b", hold=5, after=100)
+
+ # stop learning? yes
+ self.emulator.vba.press("a", hold=5, after=20)
+
+ self.emulator.text_wait()
diff --git a/pokemontools/vba/vba.py b/pokemontools/vba/vba.py
index 02fd99d..204e102 100644
--- a/pokemontools/vba/vba.py
+++ b/pokemontools/vba/vba.py
@@ -462,16 +462,16 @@ class crystal(object):
# date/time box (day choice)
# 0x47ab is the one from the intro, 0x49ab is the one from mom.
elif 0x47ab in stack or 0x49ab in stack: # was any([x in stack for x in range(0x46EE, 0x47AB)])
- print "probably at a date/time box ? exiting."
- break
+ if not self.is_in_battle():
+ print "probably at a date/time box ? exiting."
+ break
# "How many minutes?" selection box
elif 0x4826 in stack:
print "probably at a \"How many minutes?\" box ? exiting."
break
- else:
- self.vba.step(count=step_size)
+ self.vba.step(count=step_size)
# if there is a callback, then call the callback and exit when the
# callback returns True. This is especially useful during the
@@ -523,6 +523,13 @@ class crystal(object):
self.vba.write_memory_at(0xd216, 0)
self.vba.write_memory_at(0xd217, 1)
+ def set_battle_mon_hp(self, hp):
+ """
+ Set the BattleMonHP variable to the given hp.
+ """
+ self.vba.write_memory_at(0xc63c, hp / 0x100)
+ self.vba.write_memory_at(0xc63c + 1, hp % 0x100)
+
def nstep(self, steplimit=500):
"""
Steps the CPU forward and calls some functions in between each step.
@@ -592,6 +599,50 @@ class crystal(object):
def is_in_link_battle(self):
return self.vba.read_memory_at(0xc2dc) != 0
+ def is_trainer_switch_prompt(self):
+ """
+ Checks if the game is currently displaying the yes/no prompt for
+ whether or not to switch pokemon. This happens when the trainer is
+ switching pokemon out.
+ """
+ # TODO: this method should return False if the game options have been
+ # set to not use the battle switching style.
+
+ # get on-screen text
+ text = self.get_text()
+
+ requirements = [
+ "YES",
+ "NO",
+ "Will ",
+ "change POKMON?",
+ ]
+
+ return all([requirement in text for requirement in requirements])
+
+ def is_wild_switch_prompt(self):
+ """
+ Detects if the battle is waiting for the player to choose whether or
+ not to continue to fight the wild pokemon.
+ """
+ # get on-screen text
+ screen_text = self.get_text()
+
+ requirements = [
+ "YES",
+ "NO",
+ "Use next POKMON?",
+ ]
+
+ return all([requirement in screen_text for requirement in requirements])
+
+ def is_switch_prompt(self):
+ """
+ Detects both the trainer-style switch prompt and the wild-style switch
+ prompt. This is the yes/no prompt for whether to switch pokemon.
+ """
+ return self.is_trainer_switch_prompt() or self.is_wild_switch_prompt()
+
def unlock_flypoints(self):
"""
Unlocks different destinations for flying.
@@ -604,6 +655,12 @@ class crystal(object):
self.vba.write_memory_at(0xDCA7, 0xFF)
self.vba.write_memory_at(0xDCA8, 0xFF)
+ def set_battle_type(self, battle_type):
+ """
+ Changes the battle type value.
+ """
+ self.vba.write_memory_at(0xd230, battle_type)
+
def get_gender(self):
"""
Returns 'male' or 'female'.
@@ -661,14 +718,14 @@ class crystal(object):
self.vba.write_memory_at(0xd8dc, 5)
self.vba.write_memory_at(0xd8dd, 99)
- def get_text(self, chars=chars):
+ def get_text(self, chars=chars, offset=0, bounds=1000):
"""
Returns alphanumeric text on the screen.
Other characters will not be shown.
"""
output = ""
- tiles = self.vba.memory[0xc4a0:0xc4a0 + 1000]
+ tiles = self.vba.memory[0xc4a0 + offset:0xc4a0 + offset + bounds]
for each in tiles:
if each in chars.keys():
thing = chars[each]
diff --git a/tests/test_vba_battle.py b/tests/test_vba_battle.py
index 61c0297..c6debc3 100644
--- a/tests/test_vba_battle.py
+++ b/tests/test_vba_battle.py
@@ -67,8 +67,7 @@ class BattleTests(unittest.TestCase):
self.vba.press(["a"], after=20)
# set partymon1 hp to very low
- self.vba.write_memory_at(0xc63c, 0)
- self.vba.write_memory_at(0xc63d, 1)
+ self.cry.set_battle_mon_hp(1)
# let the enemy attack and kill the pokemon
self.battle.skip_until_input_required()
@@ -89,5 +88,30 @@ class BattleTests(unittest.TestCase):
self.assertTrue(self.battle.is_player_turn())
+ def test_is_battle_switch_prompt(self):
+ self.battle.skip_start_text()
+ self.battle.skip_until_input_required()
+
+ # press "FIGHT"
+ self.vba.press(["a"], after=20)
+
+ # press the first move ("SCRATCH")
+ self.vba.press(["a"], after=20)
+
+ # set enemy hp to very low
+ self.cry.lower_enemy_hp()
+
+ # attack the enemy and kill it
+ self.battle.skip_until_input_required()
+
+ # yes/no menu is present, should be detected
+ self.assertTrue(self.battle.is_trainer_switch_prompt())
+
+ # and input should be required
+ self.assertTrue(self.battle.is_input_required())
+
+ # but it's not mandatory
+ self.assertFalse(self.battle.is_mandatory_switch())
+
if __name__ == "__main__":
unittest.main()