User File #62514018295035506

Upload All User Files

#62514018295035506 - DankBot Pokémon White Jade v1.0

DankBot_PWJ_v1.0.lua
351 downloads
Uploaded 4/7/2020 7:40 AM by ThunderAxe31 (see all 109)
Reuploading for fixing a minor issue, the previous file had
max_states_global = 10
instead than
max_states_global = 20
so now the script should work as intended.
--DankBot Pokemon White Jade v1.0 by ThunderAxe31
--Play until the beginning of the fight with the sailor, then launch this script and see it at work.

local addr_rng1          = 0x005F
local addr_rng2          = 0x005A
local addr_writing       = 0x003B --if == 0 then it's not writing
local addr_menuing       = 0x0074 --if == 1 then it's menuing
local addr_enemy_ko      = 0x057D --106 and 22 for ko, 110 for level up and 134 for new enemy
local addr_enemy_hp      = 0x0622
local addr_infight       = 0x0067 --if == 1     then it's during fight
--local addr_itemcursor    = 0x01E5 --if == 10626 then you can move cursor in item list
local addr_itemcursor    = 0x01E4 --if == 124   then you can move cursor in item list
local addr_partycursor   = 0x01CB --if == 205 or 210  then you can move cursor // warning: frame perfect only with first entry. use 0x01DE if needed
local addr_partycursor2  = 0x01E3 --if == 205 or 210  then you can move cursor 
local addr_fightdialog   = 0x01EC --if == 236   then you can close the dialogue
local addr_overwdialog   = 0x003F --if == 49360 then you can close the dialogue
local addr_canmove       = 0x01FE --if == 1     then you can move
local addr_stepsleft     = 0x061F --amount of steps left before ecountering a random wild pokemon
--local addr_enemy_hp_max  = 0x0624
local addr_player_hp     = 0x04C2
local addr_player_poison = 0x050A --if == 1 then player is poisoned
local addr_wild_level    = 0x05B4
local addr_wild_species  = 0x05AE
local addr_wild_move1    = 0x0634
local addr_wild_move2    = 0x0635
local addr_wild_move3    = 0x0636
local addr_wild_move4    = 0x0637
local addr_current_poke  = 0x05BD --index of the currently active party member in fight
local addr_party_start   = 0x04B0 --index of the entry. don't ask.
local addr_moves_start   = 0x04DA --begin of party pokemon moves
local addr_pps_start     = 0x04F2 --begin of party pokemon moves PPs left
local addr_trainerfight  = 0x061B --if == 0 then it's trainer fight

local fight      =  1
local move       =  2
local fight_item =  3
local market_buy =  4
local catch      =  5
local fight_exit =  6
local fight_run  =  7
local party_swap =  8
local hunt       =  9
local pc         = 10
local item       = 11

local up    = 0
local right = 1
local down  = 2
local left  = 3
local talk  = 4

local medicham = 22
local machamp  = 48
local garchomp = 89

local fire_punch  =   3 -- 75,100, fire
local tri_attack  = 130 -- 80,100, normal
local flying_kick = 156 --100, 90, fight
local karate_chop = 159 -- 50,100, fight --Medicham should use Flying Kick instead of this
local rolling_kick= 160 -- 60, 85, fight --Machamp should use Vital Throw instead of this
local vital_throw = 161 -- 70,100, fight
local rock_slide  = 191 -- 75, 90, rock
local cross_chop  = 164 --100, 80, fight
local mega_kick   = 153 --120, 75, normal
local hyper_beam  = 127 --150, 90, normal
local take_down   = 150 -- 90,100, normal
local crush_claw  = 145 -- 75, 95, normal --Garchomp should use Take Down instead of this
local brick_break = 162 -- 75,100, fight --Garchomp should use Cross Chop instead of this
local ice_ball    =  52 -- 30, 90, ice

local current_action = 1

--These are the two values that regulate how much obsessive the bot will be. Be careful: botting time grows exponentially.
local max_states_global = 20
local max_wait_global   = 20

--remember that in order to accumulate more useful savestates, always put the less inclusive alternate actions first
local action = { --function type, function arguments
{func = fight, {2, 14, 18} },
{func = fight, {1,  0, 18} },

{func = fight, {1,  0, 18} },

{func = fight, {1,  6, 18}, {1, 10, 18}, {2, 14, 18} },
{func = fight, {1,  0, 18} },

{func = move, {custom_wait = 0, left, 4, up, 3, right, 16, down, 6, up, 7, right, 2, up, 3, left, 29, down, 7, down, 7, left, 7, up, 1, left, 3, up, 9, right, 1, talk, 1, down, 4, right, 6, up, 8, left, 8, up, 3, talk, 1, right, 4, up, 12, up, 2, right, 2, up, 23, up, 7, up, 5} },
{func = move,
	{custom_wait = 0, right, 11, up, 13, left, 18, down, 7, left, 7, up, 1, up, 8, right, 3, up, 3, left, 2, up, 12, talk, 1},
	{custom_wait = 0, up, 1, right, 11, up, 12, left, 18, down, 7, left, 7, up, 1, up, 8, right, 3, up, 3, left, 2, up, 12, talk, 1} },

{func = fight, {2, 32, 18} },
{func = fight, {2, 32, 18, branch = 1}, {1, 20, 18, branch = 2}, {1, 22, 18, branch = 2} },
{func = fight, {1, 16, 18, branch = 1}, {1, 18, 18, branch = 1}, {1, 10, 18, branch = 2}, {1, 12, 18, branch = 2} },
{func = fight, {1,  0, 18} },

{func = fight, {2, 26, 18} },
{func = fight, {2, 26, 18, branch = 1}, {1, 16, 18, branch = 2}, {1, 18, 18, branch = 2} }, 
{func = fight, {1, 12, 18, branch = 1}, {1, 14, 18, branch = 1}, {1,  8, 18, branch = 2}, {1, 10, 18, branch = 2} },
{func = fight, {1,  0, 18} },

{func = fight, {2, 46, 10}, {2, 46, 9} },
{func = fight, {2, 46,  2}, {2, 46, 1} },
{func = fight_item, {1,  1, 14}, {1,  1, 13} },--use item 1 on party member 1, have at least 14 or 13 HP left after
{func = fight,
{2, 46,  6, branch = 1}, {2, 46,  5, branch = 1},
{1, 30,  6, branch = 2}, {1, 32,  6, branch = 2}, {1, 30,  5, branch = 2}, {1, 32,  5, branch = 2}  },
{func = fight_item,
{1,  1, 18, branch = 1}, {1,  1, 17, branch = 1},
{1,  1, 18, branch = 2}, {1,  1, 17, branch = 2} },--double branch for avoiding cross elimination of states
{func = fight,
{1, 22, 10, branch = 1}, {1, 22,  9, branch = 1}, {1, 24, 10, branch = 1}, {1, 24,  9, branch = 1},
{1, 14, 10, branch = 2}, {1, 16, 10, branch = 2}, {1, 14,  9, branch = 2}, {1, 16,  9, branch = 2}  },--*
{func = fight, {1,  0, 1} },

{func = fight, {2, 36, 1, branch = 1}, {1, 24, 1, branch = 2} },
{func = fight,
{1, 16, 1, branch = 1}, {1, 18, 1, branch = 1}, {1, 20, 1, branch = 1},
{1, 12, 1, branch = 2} },
{func = fight, {1,  0, 1} },

{func = move, {custom_wait = 0, down, 12, right, 2, down, 3, left, 2, down, 10, right, 7, up, 4} },

{func = move,
	{branch = 1, custom_wait = 0, up, 0, right, 4, up, 22},
	{branch = 2, custom_wait = 0, up, 1, right, 4, up, 21},
	{branch = 3, custom_wait = 0, up, 2, right, 4, up, 20},
	{branch = 4, custom_wait = 0, up, 3, right, 4, up, 19},
	{branch = 5, custom_wait = 0, up, 4, right, 4, up, 18},
	{branch = 6, custom_wait = 0, up, 0, right, 5, up, 22},
	{branch = 7, custom_wait = 0, up, 1, right, 5, up, 21},
	{branch = 8, custom_wait = 0, up, 2, right, 5, up, 20},
	{branch = 9, custom_wait = 0, up, 3, right, 5, up, 19},
	{branch =10, custom_wait = 0, up, 4, right, 5, up, 18} },
{func = move,
	{branch = 1, up, 1},
	{branch = 2, up, 1},
	{branch = 3, up, 1},
	{branch = 4, up, 1},
	{branch = 5, up, 1},
	{branch = 6, up, 1},
	{branch = 7, up, 1},
	{branch = 8, up, 1},
	{branch = 9, up, 1},
	{branch =10, up, 1} },
{func = move,
	{branch = 1, custom_wait = 1, up, 5, right, 10, up, 6, left, 10, up, 11},
	{branch = 2, custom_wait = 1, up, 5, right, 10, up, 6, left, 10, up, 11},
	{branch = 3, custom_wait = 1, up, 5, right, 10, up, 6, left, 10, up, 11},
	{branch = 4, custom_wait = 1, up, 5, right, 10, up, 6, left, 10, up, 11},
	{branch = 5, custom_wait = 1, up, 5, right, 10, up, 6, left, 10, up, 11},
	{branch = 6, custom_wait = 1, up, 5, right,  9, up, 6, left, 10, up, 11},
	{branch = 7, custom_wait = 1, up, 5, right,  9, up, 6, left, 10, up, 11},
	{branch = 8, custom_wait = 1, up, 5, right,  9, up, 6, left, 10, up, 11},
	{branch = 9, custom_wait = 1, up, 5, right,  9, up, 6, left, 10, up, 11},
	{branch =10, custom_wait = 1, up, 5, right,  9, up, 6, left, 10, up, 11} },

{func = move, {custom_states = 100, down, 2} },
{func = move, {custom_states = 100, custom_wait = 1, left, 5, up, 4, left, 3, talk, 1, down, 4, right, 7, up, 9} },
{func = move, {custom_states = 100, up, 1} },
{func = move, {custom_states = 100, custom_wait = 1, up, 1} },
{func = move, {custom_states = 100, custom_wait = 0, up, 9, right, 12, up, 1, right, 4, up, 4, talk, 1, left, 3, down, 5, right, 10, up, 26, left, 4, up, 2} },

{func = move, {custom_states = 100, up, 3, right, 3, left, 2} },
{func = move,
{custom_states = 100, left, 2, up, 3, left, 3},
{custom_states = 100, up, 3, left, 5} },
{func = move,
{custom_states = 100, left, 2, down, 3, left, 2, down, 3, left, 3, up, 1, talk, 1}, 
{custom_states = 100, left, 2, down, 2, left, 2, down, 4, left, 3, up, 1, talk, 1}, 
{custom_states = 100, down, 3, left, 4, down, 3, left, 3, up, 1, talk, 1} },

{func = fight, {custom_states = 100, 2, 47, 1} },
{func = fight, {custom_states = 100, 2, 47, 1} }, 
{func = fight, {custom_states = 50, 2, 47, 1, branch = 1}, {custom_states = 50, 1, 31, 1, branch = 2} }, 
{func = fight, {custom_states = 50, 1, 23, 1, branch = 1}, {custom_states = 50, 1, 15, 1, branch = 2} },
{func = fight, {custom_states = 100, 1,  0, 1} },

{func = fight, {custom_states = 100, 2, 39, 1} },
{func = fight, {custom_states = 100, 2, 39, 1} }, 
{func = fight, {custom_states = 100, 1, 19, 1} },
{func = fight, {custom_states = 100, 1,  0, 1} },

{func = fight, {custom_states = 100, custom_wait = 200, 1, 26, 1} },
{func = fight, {custom_states = 100, custom_wait = 200, 1, 14, 1} },
{func = fight, {custom_states = 100, custom_wait = 200, 1,  0, 1} }, 

{func = fight, {custom_states = 100, custom_wait = 200, 2, 38, 1} },
{func = fight,
	{custom_states = 100, custom_wait = 200, 2, 38, 1, branch = 1},
	{custom_states = 100, custom_wait = 200, 1, 24, 1, branch = 2} }, 
{func = fight,
	{custom_states = 100, custom_wait = 200, 1, 12, 1, branch = 1},
	{custom_states = 100, custom_wait = 200, 1, 26, 1, branch = 1},
	{custom_states = 100, custom_wait = 200, 1, 14, 1, branch = 2} }, 
{func = fight, {custom_states = 100, custom_wait = 200, 1,  0, 1} }

{func = move, {right, 3} },
{func = move, {up, 3} },
{func = move, {right, 4, up, 3, right, 2} },
{func = move, {right, 3, down, 3, right, 2, left, 2, down, 6, left, 32, down, 18, left, 16} },

{func = move, {custom_states =  50, left, 1} },
{func = move, {custom_states =  50, custom_wait = 1, left, 10, up, 2, talk, 1, up, 5, left, 55, up, 4, right, 2, up, 3, left, 1} },

{func = market_buy, {custom_states = 100, custom_wait = 0, {0, 0, 3}, { 1,-1, 2} } },--page+, row+, amount

{func = move, {custom_states =  50, custom_wait = 0, down, 21, left, 8, up, 5, left, 1} },
{func = move, {custom_states =  50, left, 1} },
{func = move, {custom_states = 100, custom_wait = 1, left, 2} },
{func = move, {custom_states = 100, custom_wait = 0, left, 23, up, 5, talk, 1, down, 5} },
{func = move, {custom_states = 100, down, 1} },
{func = move, {custom_states = 200, custom_wait = 1, right, 2} },
{func = move, {custom_states = 200, custom_wait = 0, right, 25, down, 5, left, 1} },
{func = move, {custom_states = 200, left, 1} },
{func = move, {custom_states = 400, custom_wait = 1, left, 2} },
{func = move, {custom_states = 400, custom_wait = 0, left, 10} },
{func = move, {custom_states = 400, left, 1} },
{func = move, {custom_states = 800, custom_wait = 1, left, 2} },
{func = move, {custom_states = 800, custom_wait = 0, left, 9, down, 6} },
{func = move, {custom_states = 800, down, 1} },

{func = move,
{branch = 1, custom_states = 1500, custom_wait = 1, down, 2, encounter_steps = 10},
{branch = 2, custom_states = 1500, custom_wait = 1, down, 2, encounter_steps = 11},
{branch = 3, custom_states = 1500, custom_wait = 1, down, 2, encounter_steps = 12},
{branch = 4, custom_states = 1500, custom_wait = 1, down, 2, encounter_steps = 13} },
{func = move,
{branch = 1, custom_states = 1500, custom_wait = 0, down, 1, right, 8, left, 4},
{branch = 2, custom_states = 1500, custom_wait = 0, down, 1, right, 8, left, 5},
{branch = 3, custom_states = 1500, custom_wait = 0, down, 1, right, 9, left, 5},
{branch = 4, custom_states = 1500, custom_wait = 0, down, 1, right, 9, left, 6} },

{func = catch,
{branch = 1, custom_wait = 255, species = medicham, level=25, direction = left,
move1={fire_punch}, move2={tri_attack,flying_kick}, move3={tri_attack,flying_kick}, move4={} },
{branch = 2, custom_wait = 255, species = medicham, level=25, direction = left,
move1={fire_punch}, move2={tri_attack,flying_kick}, move3={tri_attack,flying_kick}, move4={} },
{branch = 3, custom_wait = 255, species = medicham, level=25, direction = left,
move1={fire_punch}, move2={tri_attack,flying_kick}, move3={tri_attack,flying_kick}, move4={} },
{branch = 4, custom_wait = 255, species = medicham, level=25, direction = left,
move1={fire_punch}, move2={tri_attack,flying_kick}, move3={tri_attack,flying_kick}, move4={} } },

{func = fight_exit,
{branch = 1, custom_states = 10},
{branch = 2, custom_states = 10},
{branch = 3, custom_states = 10},
{branch = 4, custom_states = 10} },
{func = move,
{branch = 1, custom_states = 10, custom_wait = 1, left, 3},
{branch = 2, custom_states = 10, custom_wait = 1, left, 2},
{branch = 3, custom_states = 10, custom_wait = 1, left, 3},
{branch = 4, custom_states = 10, custom_wait = 1, left, 2} },

--{func = move, {custom_states =   1, custom_wait = 0, up, 4} },--temporary. add more states to take in account the fight_exit states

--{func = fight_exit, {} },
--{func = move, {custom_wait = 1, left, 3} },

{func = move, {custom_states =  50, custom_wait = 0, up, 4} },

{func = move, {custom_states =  50, up, 1} },
{func = move, {custom_states = 100, custom_wait = 1, up, 2} },
{func = move, {custom_states = 100, custom_wait = 0, up, 2, right, 22} },
{func = move, {custom_states = 200, right, 1} },
{func = move, {custom_states = 400, custom_wait = 1, right, 2} },
{func = move, {custom_states = 400, custom_wait = 0, right, 23, down, 6, right, 10, down, 6, left, 1, talk, 1, left, 23, up, 5, right, 5, up, 2, right, 2, up, 12} },

{func = move, {custom_states =  10, up, 1} },
{func = fight, {1, 55, 0, switch = true} },--intentional own ko
{func = fight, {move = fire_punch, 0,  0, 1} },--0 denotes a move code rather than an entry code. it's just a placeholder though

{func = move, {up, 3} },
{func = fight, {move = fire_punch, 0,  0, 1} },
{func = fight, {move = fire_punch, 0,  0, 1} },
{func = fight, {move = fire_punch, 0,  0, 1} },

{func = move, {up, 4} },
{func = fight, {move = fire_punch, 0,  0, 1} },
{func = fight, {move = fire_punch, 0,  0, 1} },
{func = fight, {move = fire_punch, 0,  0, 1} },

{func = move, {custom_wait = 0, up, 4, talk, 1} },
{func = fight, {move = fire_punch, 0,  0, 1} },
{func = fight,
{branch = 1, move = fire_punch, 0,  30, 1}, {branch = 2, move = fire_punch, 0,  60, 1},
{branch = 3, move = tri_attack, 0,  44, 1}, {branch = 4, move = tri_attack, 0,  58, 1} },
{func = fight,
{branch = 1, move = fire_punch, 0,  0, 1},
{branch = 2, move = tri_attack, 0,  0, 1} },
{func = fight, {move = fire_punch, 0,  0, 1} },
{func = fight, {move = fire_punch, 0,  0, 1} },

{func = move, {custom_wait = 0, down, 27, right, 18, talk, 1, right, 2, up, 6, left, 10, up, 6, left, 23} },
{func = move, {left, 1} },
{func = move, {custom_wait = 1, left, 2} },
{func = move, {custom_wait = 0, left, 10} },
{func = move, {left, 1} },
{func = move, {custom_wait = 1, left, 2} },
{func = move, {custom_wait = 0, left, 9, down, 5} },
{func = move, {down, 1} },
{func = move, {custom_wait = 1, down, 2} },
{func = move, {custom_wait = 0, down, 13, right, 2, down, 25, left, 4, down, 31, right, 24} },
{func = move, {right, 1} },
{func = move, {custom_wait = 1, right, 1} },
{func = move, {right, 1} },
{func = move, {custom_wait = 1, right, 2} },
{func = move, {custom_wait = 0, right, 31} },

{func = move,
{custom_wait = 0, right, 1, down, 16, right, 5, up, 3, right, 4, up, 5},
{custom_wait = 0,           down, 16, right, 6, up, 3, right, 4, up, 5},
{custom_wait = 0, right, 1, down, 16, right, 5, up, 4, right, 4, up, 4},
{custom_wait = 0,           down, 16, right, 6, up, 4, right, 4, up, 4},
{custom_wait = 0, right, 1, down, 16, right, 5, up, 5, right, 4, up, 3},
{custom_wait = 0,           down, 16, right, 6, up, 5, right, 4, up, 3},
{custom_wait = 0, right, 1, down, 16, right, 5, up, 7, right, 4, up, 1},
{custom_wait = 0,           down, 16, right, 6, up, 7, right, 4, up, 1} },

{func = move, {right, 8, up, 13, left, 5, down, 3, left, 3, down, 1} },
{func = move, {down, 3, left, 6, down, 2, left, 1, talk, 1} },

{func = fight, {move = flying_kick, 0,  0, 1} },
{func = fight, {move = flying_kick, 0,  0, 1} },
{func = fight, {move = flying_kick, 0,  0, 1} },
{func = fight, {move = flying_kick, 0,  0, 1} },

{func = move, {up, 2, right, 6, up, 3} },
{func = move,
{up, 1, right, 3, up, 3, right, 5, down, 8},
{       right, 3, up, 4, right, 5, down, 8} },
{func = move, {custom_states =  50, custom_wait = 0, down, 6, left, 7, down, 3, right, 30, up, 11, right, 6} },
{func = move, {custom_states =  50, right, 1} },
{func = move, {custom_states =  50, custom_wait = 1, right,  1} },
{func = move, {custom_states =  50, custom_wait = 0, right, 10} },
{func = move, {custom_states =  50, right, 1} },
{func = move, {custom_states = 100, custom_wait = 1, right,  3, min_encounter_steps = 11} },
{func = move, {custom_states = 100, custom_wait = 0, down, 4, right, 23, down, 3, right, 8, up, 2, right, 6} },
{func = move, {custom_states = 100, right, 1} },
{func = move, {custom_states = 200, custom_wait = 1, right, 5} },
{func = move, {custom_states = 200, right, 4, up, 4} },
{func = move, {custom_states = 200, custom_wait = 0, up, 40, left, 6, up, 3, right, 22, up, 3, right, 2, up, 3, right, 7, down, 3, right, 4, up, 1, talk, 1, up, 2, right, 10} },
{func = move, {custom_states = 400, right, 1} },
{func = move, {custom_states = 800, custom_wait = 1, right, 2} },
{func = move, {custom_states = 800, custom_wait = 0, right, 7, up, 10} },
{func = move,
{custom_states = 800, custom_wait = 0,        left, 5, up, 10},
{custom_states = 800, custom_wait = 0, up, 1, left, 5, up,  9} },
{func = move, {custom_states =1600, up, 1} },

--Fight against 3 Whishcash
{func = fight, {custom_states = 50, move = flying_kick, 0, 88, 1}, {custom_states = 50, move = tri_attack,  0, 94, 1} },
{func = fight, {custom_states = 50, move = flying_kick, min_pps = 6, 0, 60, 1}, {custom_states = 50, move = flying_kick, min_pps = 5, 0, 54, 1}, {custom_states = 50, move = tri_attack,  0, 66, 1} },
{func = fight, {custom_states = 50, move = flying_kick, min_pps = 6, 0, 32, 1}, {custom_states = 50, move = flying_kick, min_pps = 5, 0, 26, 1}, {custom_states = 50, move = tri_attack,  0, 36, 1} },
{func = fight, {custom_states = 50, move = flying_kick, min_pps = 6, 0,  0, 1}, {custom_states = 50, move = flying_kick, min_pps = 5, 0,  0, 1}, {custom_states = 50, move = tri_attack,  0,  0, 1} },

{func = fight, {custom_states = 50, move = flying_kick, 0, 88, 1}, {custom_states = 50, move = tri_attack,  0, 94, 1} },
{func = fight, {custom_states = 50, move = flying_kick, min_pps = 5, 0, 60, 1}, {custom_states = 50, move = flying_kick, min_pps = 4, 0, 54, 1}, {custom_states = 50, move = tri_attack,  0, 66, 1} },
{func = fight, {custom_states = 50, move = flying_kick, min_pps = 5, 0, 32, 1}, {custom_states = 50, move = flying_kick, min_pps = 4, 0, 26, 1}, {custom_states = 50, move = tri_attack,  0, 36, 1} },
{func = fight, {custom_states = 50, move = flying_kick, min_pps = 4, 0,  0, 1}, {custom_states = 50, move = tri_attack,  0,  0, 1} },

{func = fight,
--{custom_states = 50, custom_wait = 200, move = flying_kick, 0, 78, 1, max_player_hp = 70}, {custom_states = 50, custom_wait = 200, move = flying_kick, 0, 80, 1, max_player_hp = 70},
{custom_states = 50, custom_wait = 200, move = flying_kick, 0, 78, 1}, {custom_states = 50, custom_wait = 200, move = flying_kick, 0, 80, 1} },
{func = fight,
--{custom_states = 50, custom_wait = 200, move = flying_kick, 0, 38, 1, max_player_hp = 70}, {custom_states = 50, custom_wait = 200, move = flying_kick, 0, 40, 1, max_player_hp = 70},
{custom_states = 50, custom_wait = 200, move = flying_kick, 0, 38, 1}, {custom_states = 50, custom_wait = 200, move = flying_kick, 0, 40, 1} },
{func = fight,
{custom_wait = 200, move = flying_kick, 0,  0, 1} },

{func = fight_exit, {} },
{func = move, {custom_wait = 1, left, 2} },
{func = move, {custom_wait = 0, left, 3, up, 7, right, 6, up, 3, right, 3, up, 2, talk, 1, down, 2, left, 2, down, 3} },
{func = move, {down, 1} },
{func = move, {custom_wait = 1, left, 2, min_encounter_steps = 13} },
--{func = move, {custom_wait = 0, left, 1, up, 4, right, 2, up, 10, left, 3, up, 34, right, 2, up, 7, left, 2, up, 9, right, 4, up, 13, left, 5, up, 2} },
{func = move, {custom_wait = 0, left, 1, up, 4, right, 2, up, 10, left, 3, up, 3} },
{func = move, {up, 4} },
{func = move, {up, 11} },
{func = move, {custom_wait = 0, up, 16, right, 2, up, 7, left, 2, up, 9, right, 4, up, 13, left, 5, up, 2} },

--Rock Tunnel
{func = move, {up, 1} },
{func = move,
{branch = 1, custom_wait = 1, up, 2, encounter_steps = 10},
{branch = 2, custom_wait = 1, up, 2, encounter_steps = 11},
{branch = 3, custom_wait = 1, up, 2, encounter_steps = 12},
{branch = 4, custom_wait = 1, up, 2, encounter_steps = 13},
{branch = 5, custom_wait = 0, up, 2, encounter_steps = 14} },
{func = move,
{branch = 1, up, 5, right, 6},
{branch = 2, up, 5, right, 7},
{branch = 3, up, 5, right, 8},
{branch = 4, up, 5, right, 9},
{branch = 5, up, 5, right,10} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} },
{branch = 4, {} },
{branch = 5, {} } },
{func = move,
{branch = 1, custom_wait = 1, right, 2, min_encounter_steps = 14},
{branch = 2, custom_wait = 1, right, 2, min_encounter_steps = 13},
{branch = 3, custom_wait = 1, right, 2, min_encounter_steps = 12},
{branch = 4, custom_wait = 1, right, 2, min_encounter_steps = 11},
{branch = 5, custom_wait = 1, right, 2, min_encounter_steps = 10} },
{func = move,
{branch = 1, custom_wait = 0, right,  9, up, 5},
{branch = 2, custom_wait = 0, right, 10, up, 5},
{branch = 3, custom_wait = 0, right, 11, up, 5},
{branch = 4, custom_wait = 0, right, 12, up, 5},
{branch = 5, custom_wait = 0, right, 13, up, 5} },

{func = move, {up, 1} },
{func = move,
{branch = 1, custom_wait = 1, down, 2, encounter_steps =  9},
{branch = 2, custom_wait = 1, down, 2, encounter_steps = 10},
{branch = 3, custom_wait = 1, down, 2, encounter_steps = 11},
{branch = 4, custom_wait = 1, down, 2, encounter_steps = 12},
{branch = 5, custom_wait = 1, down, 2, encounter_steps = 13},
{branch = 6, custom_wait = 1, down, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, down, 6, right, 3},
{branch = 2, custom_wait = 0, down, 6, right, 3},
{branch = 3, custom_wait = 0, down, 6, right, 3},
{branch = 4, custom_wait = 0, down, 6, right, 3},
{branch = 5, custom_wait = 0, down, 6, right, 3},
{branch = 6, custom_wait = 0, down, 6, right, 3} },
{func = move,
{branch = 1, right, 1},
{branch = 2, right, 2},
{branch = 3, right, 3},
{branch = 4, right, 4},
{branch = 5, right, 5},
{branch = 6, right, 6} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} },
{branch = 4, {} },
{branch = 5, {} },
{branch = 6, {} } },
{func = move,
{branch = 1, custom_wait = 0, right,10},
{branch = 2, custom_wait = 1, right, 9},
{branch = 3, custom_wait = 1, right, 8},
{branch = 4, custom_wait = 1, right, 7},
{branch = 5, custom_wait = 1, right, 6},
{branch = 6, custom_wait = 1, right, 5} },

{func = move, {right, 1} },
{func = move,
{branch = 1, custom_wait = 1, down, 2, encounter_steps =  9},
{branch = 2, custom_wait = 1, down, 2, encounter_steps = 10},
{branch = 3, custom_wait = 1, down, 2, encounter_steps = 11},
{branch = 4, custom_wait = 1, down, 2, encounter_steps = 12},
{branch = 5, custom_wait = 1, down, 2, encounter_steps = 13},
{branch = 6, custom_wait = 1, down, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, left, 9},
{branch = 2, custom_wait = 0, left, 9},
{branch = 3, custom_wait = 0, left, 9},
{branch = 4, custom_wait = 0, left, 9},
{branch = 5, custom_wait = 0, left, 9},
{branch = 6, custom_wait = 0, left, 9} },
{func = move,
{branch = 1, up, 2},
{branch = 2, up, 3},
{branch = 3, up, 4},
{branch = 4, up, 5},
{branch = 5, up, 6},
{branch = 6, up, 7} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} },
{branch = 4, {} },
{branch = 5, {} },
{branch = 6, {} } },
{func = move,
{branch = 1, custom_wait = 0, up, 7, left, 2},
{branch = 2, custom_wait = 1, up, 6, left, 2},
{branch = 3, custom_wait = 1, up, 5, left, 2},
{branch = 4, custom_wait = 1, up, 4, left, 2},
{branch = 5, custom_wait = 1, up, 3, left, 2},
{branch = 6, custom_wait = 1, up, 2, left, 2} },

{func = move, {left, 1} },
{func = move,
{branch = 1, custom_wait = 1, right, 2, encounter_steps = 12},
{branch = 2, custom_wait = 1, right, 2, encounter_steps = 13},
{branch = 3, custom_wait = 1, right, 2, encounter_steps = 14} },
{func = move,
{branch = 1, right, 2, down, 11},
{branch = 2, right, 2, down, 12},
{branch = 3, right, 2, down, 13} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} } },
{func = move,
{branch = 1, custom_wait = 0, down, 2, encounter_steps = 14},
{branch = 2, custom_wait = 0, down, 2, encounter_steps = 14},
{branch = 3, custom_wait = 0, down, 1, left, 1, encounter_steps = 15} },
{func = move,
{branch = 1, down, 1, left, 2, down, 2, left, 10, down, 3},
{branch = 2,          left, 2, down, 2, left, 10, down, 4},
{branch = 3,          left, 1, down, 2, left, 10, down, 5} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} } },
{func = move,
{branch = 1, custom_wait = 0, down, 2, min_encounter_steps = 14},
{branch = 2, custom_wait = 1, down, 2, min_encounter_steps = 13},
{branch = 3, custom_wait = 1, down, 2, min_encounter_steps = 12} },
{func = move,
{branch = 1, custom_wait = 0, down, 4, right, 10, up, 1},
{branch = 2, custom_wait = 0, down, 3, right, 10, up, 1},
{branch = 3, custom_wait = 0, down, 2, right, 10, up, 1} },

{func = move, {up, 1} },
{func = move,
{branch = 1, custom_wait = 1, down, 4, encounter_steps =  7},
{branch = 2, custom_wait = 1, down, 4, encounter_steps =  8},
{branch = 3, custom_wait = 1, down, 4, encounter_steps =  9},
{branch = 4, custom_wait = 1, down, 4, encounter_steps = 10},
{branch = 5, custom_wait = 1, down, 4, encounter_steps = 11},
{branch = 6, custom_wait = 1, down, 4, encounter_steps = 12} },
{func = move,
{branch = 1, left, 8},
{branch = 2, left, 9},
{branch = 3, left, 9, up, 2},
{branch = 4, left, 9, up, 3},
{branch = 5, left, 9, up, 4},
{branch = 6, left, 9, up, 4, left, 2} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} },
{branch = 4, {} },
{branch = 5, {} },
{branch = 6, {} } },
{func = move,
{branch = 1, custom_wait = 1, left, 1, up, 4, left, 5, down, 1},
{branch = 2, custom_wait = 1,          up, 4, left, 5, down, 1},
{branch = 3, custom_wait = 1,          up, 2, left, 5, down, 1},
{branch = 4, custom_wait = 1,          up, 1, left, 5, down, 1},
{branch = 5, custom_wait = 1,                 left, 5, down, 1},
{branch = 6, custom_wait = 1,                 left, 3, down, 1} },

--Power Plant 1
{func = move, {down, 1} },
{func = move,
{branch = 1, custom_wait = 1, down, 1, right, 1, encounter_steps = 10},
{branch = 2, custom_wait = 1, down, 1, right, 1, encounter_steps = 11},
{branch = 3, custom_wait = 1, down, 1, right, 1, encounter_steps = 12},
{branch = 4, custom_wait = 1, down, 1, right, 1, encounter_steps = 13},
{branch = 5, custom_wait = 1, down, 1, right, 1, encounter_steps = 14},
{branch = 6, custom_wait = 1, down, 1, right, 1, encounter_steps = 15} },
{func = move,
{branch = 1, custom_wait = 0, right, 7, up, 7, left, 10, up, 5, right,  9},
{branch = 2, custom_wait = 0, right, 7, up, 7, left, 10, up, 5, right, 10},
{branch = 3, custom_wait = 0, right, 7, up, 7, left, 10, up, 5, right, 11},
{branch = 4, custom_wait = 0, right, 7, up, 7, left, 10, up, 5, right, 12},
{branch = 5, custom_wait = 0, right, 7, up, 7, left, 10, up, 5, right, 13},
{branch = 6, custom_wait = 0, right, 7, up, 7, left, 10, up, 5, right, 14} },
{func = move,
{branch = 1, right, 1},
{branch = 2, right, 1},
{branch = 3, right, 1},
{branch = 4, right, 1},
{branch = 5, right, 1},
{branch = 6, right, 1} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} },
{branch = 4, {} },
{branch = 5, {} },
{branch = 6, {} } },
{func = move,
{branch = 1, custom_wait = 0, right, 2, min_encounter_steps = 14},
{branch = 2, custom_wait = 1, right, 2, min_encounter_steps = 13},
{branch = 3, custom_wait = 1, right, 2, min_encounter_steps = 12},
{branch = 4, custom_wait = 1, right, 2, min_encounter_steps = 11},
{branch = 5, custom_wait = 1, right, 1, down, 1, min_encounter_steps = 11},
{branch = 6, custom_wait = 1, down,  2, min_encounter_steps = 10} },
{func = move,
{branch = 1, custom_wait = 0, right, 3, down, 11},
{branch = 2, custom_wait = 0, right, 2, down, 11},
{branch = 3, custom_wait = 0, right, 1, down, 11},
{branch = 4, custom_wait = 0,           down, 11},
{branch = 5, custom_wait = 0,           down, 10},
{branch = 6, custom_wait = 0,           down,  9} },

{func = move, {custom_states =  40, down, 1} },
{func = move, {custom_states =  80, custom_wait = 1, down, 2, min_encounter_steps = 12} },
{func = move, {custom_states =  80, custom_wait = 0, down, 5, left, 8, up, 3, left, 4, up, 2, right, 6, up, 8, right, 12, down, 5, right, 3, talk, 1, left, 3, up, 5, left, 12, down, 10, left, 5} },
{func = move, {custom_states = 160, down, 1} },
{func = move,
{custom_wait = 1, down, 1, right, 4, down, 2, min_encounter_steps = 15},
{custom_wait = 1, right, 4, down, 3, min_encounter_steps = 15} },
{func = move, {custom_wait = 0, right, 8, up, 8} },

--Retrieve Machine Part
{func = move, {up, 1} },
{func = move,
{branch = 1, custom_wait = 1, up, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, up, 2, encounter_steps = 14},
{branch = 3, custom_wait = 1, up, 2, encounter_steps = 13},
{branch = 4, custom_wait = 1, up, 2, encounter_steps = 13},
{branch = 5, custom_wait = 1, up, 2, encounter_steps = 12},
{branch = 6, custom_wait = 1, up, 2, encounter_steps = 12},
{branch = 7, custom_wait = 1, up, 2, encounter_steps = 11},
{branch = 8, custom_wait = 1, up, 2, encounter_steps = 11},
{branch = 9, custom_wait = 1, up, 2, encounter_steps = 10} },
{func = move,
{branch = 1, custom_wait = 0, up, 8, left, 7},
{branch = 2, custom_wait = 0, up, 8, left, 7},
{branch = 3, custom_wait = 0, up, 8, left, 6},
{branch = 4, custom_wait = 0, up, 8, left, 6},
{branch = 5, custom_wait = 0, up, 8, left, 5},
{branch = 6, custom_wait = 0, up, 8, left, 5},
{branch = 7, custom_wait = 0, up, 8, left, 4},
{branch = 8, custom_wait = 0, up, 8, left, 4},
{branch = 9, custom_wait = 0, up, 8, left, 3} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} },
{branch = 4, {} },
{branch = 5, {} },
{branch = 6, {} },
{branch = 7, {} },
{branch = 8, {} },
{branch = 9, {} } },
{func = move,
{branch = 1, custom_wait = 1, left, 2,     encounter_steps = 10},
{branch = 2, custom_wait = 1, left, 2, min_encounter_steps = 11},
{branch = 3, custom_wait = 1, left, 2,     encounter_steps = 11},
{branch = 4, custom_wait = 1, left, 2, min_encounter_steps = 12},
{branch = 5, custom_wait = 1, left, 2,     encounter_steps = 12},
{branch = 6, custom_wait = 1, left, 2, min_encounter_steps = 13},
{branch = 7, custom_wait = 1, left, 2,     encounter_steps = 13},
{branch = 8, custom_wait = 1, left, 2, min_encounter_steps = 14},
{branch = 9, custom_wait = 0, left, 2,     encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, left, 6, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 2, custom_wait = 0, left, 6, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 3, custom_wait = 0, left, 7, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 4, custom_wait = 0, left, 7, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 5, custom_wait = 0, left, 8, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 6, custom_wait = 0, left, 8, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 7, custom_wait = 0, left, 9, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 8, custom_wait = 0, left, 9, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 9, custom_wait = 0, left,10, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3} },
{func = move,
{branch = 1, custom_wait = 20, left, 5, down, 6, left, 3},
{branch = 2, custom_wait =  0, left, 6, down, 6, left, 2},
{branch = 3, custom_wait = 20, left, 5, down, 6, left, 3},
{branch = 4, custom_wait =  0, left, 6, down, 6, left, 2},
{branch = 5, custom_wait = 20, left, 5, down, 6, left, 3},
{branch = 6, custom_wait =  0, left, 6, down, 6, left, 2},
{branch = 7, custom_wait = 20, left, 5, down, 6, left, 3},
{branch = 8, custom_wait =  0, left, 6, down, 6, left, 2},
{branch = 9, custom_wait = 20, left, 5, down, 6, left, 3} },

{func = move, {custom_states = 200, custom_wait = 0, talk, 1, left, 1, up, 5, left, 12, up, 3, left, 4, down, 3, left, 5, up, 1} },
{func = move, {custom_states = 200, custom_wait = 0, up, 8, left, 6, talk, 1, right, 5, down, 9, right, 6, down, 7, right, 3, up, 7, right, 9} },

{func = move, {custom_states = 400, right, 1} },
{func = move, {custom_states = 800, custom_wait = 1, right, 1, down, 1} },
{func = move, {custom_states = 800, custom_wait = 0, down, 4, right, 1, talk, 1, right, 3, down, 7, right, 5} },

--Power Plant 2
{func = fight, {1, 78, 0} },--intentional own ko
{func = fight_exit, {} },
{func = move,
{branch = 1, custom_wait = 1, right, 2, encounter_steps = 10},
{branch = 2, custom_wait = 1, right, 2, encounter_steps = 11},
{branch = 3, custom_wait = 1, right, 2, encounter_steps = 12},
{branch = 4, custom_wait = 1, right, 2, encounter_steps = 13},
{branch = 5, custom_wait = 1, right, 2, encounter_steps = 14},
{branch = 6, custom_wait = 1, right, 2, encounter_steps = 15} },
{func = move,
{branch = 1, custom_wait = 0, right, 2, up, 6, left, 10, up, 5, right,  9},
{branch = 2, custom_wait = 0, right, 2, up, 6, left, 10, up, 5, right, 10},
{branch = 3, custom_wait = 0, right, 2, up, 6, left, 10, up, 5, right, 11},
{branch = 4, custom_wait = 0, right, 2, up, 6, left, 10, up, 5, right, 12},
{branch = 5, custom_wait = 0, right, 2, up, 6, left, 10, up, 5, right, 13},
{branch = 6, custom_wait = 0, right, 2, up, 6, left, 10, up, 5, right, 14} },
{func = move,
{branch = 1, right, 1},
{branch = 2, right, 1},
{branch = 3, right, 1},
{branch = 4, right, 1},
{branch = 5, right, 1},
{branch = 6, right, 1} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} },
{branch = 4, {} },
{branch = 5, {} },
{branch = 6, {} } },
{func = move,
{branch = 1, custom_wait = 0, right, 2, min_encounter_steps = 14},
{branch = 2, custom_wait = 1, right, 2, min_encounter_steps = 13},
{branch = 3, custom_wait = 1, right, 2, min_encounter_steps = 12},
{branch = 4, custom_wait = 1, right, 2, min_encounter_steps = 11},
{branch = 5, custom_wait = 1, right, 1, down, 1, min_encounter_steps = 11},
{branch = 6, custom_wait = 1, down,  2, min_encounter_steps = 10} },
{func = move,
{branch = 1, custom_wait = 0, right, 3, down, 11},
{branch = 2, custom_wait = 0, right, 2, down, 11},
{branch = 3, custom_wait = 0, right, 1, down, 11},
{branch = 4, custom_wait = 0,           down, 11},
{branch = 5, custom_wait = 0,           down, 10},
{branch = 6, custom_wait = 0,           down,  9} },

{func = move, {custom_states =  40, down, 1} },
{func = move, {custom_states =  80, custom_wait = 1, down, 2, min_encounter_steps = 12} },
{func = move, {custom_states =  80, custom_wait = 0, down, 5, left, 8, up, 3, left, 4, up, 2, right, 6, up, 8, right, 12, down, 5, right, 3, talk, 1, left, 3, up, 5, left, 12, down, 10, left, 5} },
{func = move, {custom_states = 160, down, 1} },
{func = move,
{custom_wait = 1, down, 1, right, 4, down, 2, min_encounter_steps = 15},
{custom_wait = 1, right, 4, down, 3, min_encounter_steps = 15} },
{func = move, {custom_wait = 0, right, 8, up, 8} },

{func = move, {up, 1} },
{func = move,
{branch = 1, custom_wait = 1, up, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, up, 2, encounter_steps = 14},
{branch = 3, custom_wait = 1, up, 2, encounter_steps = 13},
{branch = 4, custom_wait = 1, up, 2, encounter_steps = 13},
{branch = 5, custom_wait = 1, up, 2, encounter_steps = 12},
{branch = 6, custom_wait = 1, up, 2, encounter_steps = 12},
{branch = 7, custom_wait = 1, up, 2, encounter_steps = 11},
{branch = 8, custom_wait = 1, up, 2, encounter_steps = 11},
{branch = 9, custom_wait = 1, up, 2, encounter_steps = 10} },
{func = move,
{branch = 1, custom_wait = 0, up, 8, left, 7},
{branch = 2, custom_wait = 0, up, 8, left, 7},
{branch = 3, custom_wait = 0, up, 8, left, 6},
{branch = 4, custom_wait = 0, up, 8, left, 6},
{branch = 5, custom_wait = 0, up, 8, left, 5},
{branch = 6, custom_wait = 0, up, 8, left, 5},
{branch = 7, custom_wait = 0, up, 8, left, 4},
{branch = 8, custom_wait = 0, up, 8, left, 4},
{branch = 9, custom_wait = 0, up, 8, left, 3} },
{func = fight_run,
{branch = 1, {} },
{branch = 2, {} },
{branch = 3, {} },
{branch = 4, {} },
{branch = 5, {} },
{branch = 6, {} },
{branch = 7, {} },
{branch = 8, {} },
{branch = 9, {} } },
{func = move,
{branch = 1, custom_wait = 1, left, 2,     encounter_steps = 10},
{branch = 2, custom_wait = 1, left, 2, min_encounter_steps = 11},
{branch = 3, custom_wait = 1, left, 2,     encounter_steps = 11},
{branch = 4, custom_wait = 1, left, 2, min_encounter_steps = 12},
{branch = 5, custom_wait = 1, left, 2,     encounter_steps = 12},
{branch = 6, custom_wait = 1, left, 2, min_encounter_steps = 13},
{branch = 7, custom_wait = 1, left, 2,     encounter_steps = 13},
{branch = 8, custom_wait = 1, left, 2, min_encounter_steps = 14},
{branch = 9, custom_wait = 0, left, 2,     encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, left, 6, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 2, custom_wait = 0, left, 6, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 3, custom_wait = 0, left, 7, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 4, custom_wait = 0, left, 7, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 5, custom_wait = 0, left, 8, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 6, custom_wait = 0, left, 8, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 7, custom_wait = 0, left, 9, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 8, custom_wait = 0, left, 9, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3},
{branch = 9, custom_wait = 0, left,10, down, 5, left, 5, down, 6, left, 23, up, 4, left, 5, up, 5, left, 2, up, 5, left, 3} },
{func = move,
{branch = 1, custom_wait = 20, left, 5, down, 6, left, 3},
{branch = 2, custom_wait =  0, left, 6, down, 6, left, 2},
{branch = 3, custom_wait = 20, left, 5, down, 6, left, 3},
{branch = 4, custom_wait =  0, left, 6, down, 6, left, 2},
{branch = 5, custom_wait = 20, left, 5, down, 6, left, 3},
{branch = 6, custom_wait =  0, left, 6, down, 6, left, 2},
{branch = 7, custom_wait = 20, left, 5, down, 6, left, 3},
{branch = 8, custom_wait =  0, left, 6, down, 6, left, 2},
{branch = 9, custom_wait = 20, left, 5, down, 6, left, 3} },

{func = move, {custom_states = 200, custom_wait =  0, talk, 1, left, 1, up, 5, left, 8, down, 8} },

--Catch Machamp
{func = move, {custom_states = 400, down, 1} },
{func = move,
{branch = 1, custom_states = 800, custom_wait = 1, down, 2, encounter_steps = 10},
{branch = 2, custom_states = 800, custom_wait = 1, down, 2, encounter_steps = 11},
{branch = 3, custom_states = 800, custom_wait = 1, down, 2, encounter_steps = 12} },
{func = move,
{branch = 1, custom_states = 800, custom_wait = 0, down, 17, up, 4, down, 2, left, 2, up, 1},
{branch = 2, custom_states = 800, custom_wait = 0, down, 17, up, 4, down, 3, left, 2, up, 1},
{branch = 3, custom_states = 800, custom_wait = 0, down, 17, up, 4, down, 3, left, 2, up, 2} },

{func = catch,
{branch = 1, custom_wait = 50, species = machamp, level=43, direction = up,
move1={mega_kick,cross_chop,rock_slide}, move2={mega_kick,cross_chop,rock_slide}, move3={mega_kick,cross_chop,rock_slide}, move4={} },
{branch = 2, custom_wait = 50, species = machamp, level=43, direction = up,
move1={mega_kick,cross_chop,rock_slide}, move2={mega_kick,cross_chop,rock_slide}, move3={mega_kick,cross_chop,rock_slide}, move4={} },
{branch = 3, custom_wait = 50, species = machamp, level=43, direction = up,
move1={mega_kick,cross_chop,rock_slide}, move2={mega_kick,cross_chop,rock_slide}, move3={mega_kick,cross_chop,rock_slide}, move4={} } },

{func = fight_exit,
{branch = 1},
{branch = 2},
{branch = 3} },
{func = move,
{branch = 1, custom_wait = 1, up, 2},
{branch = 2, custom_wait = 1, up, 3},
{branch = 3, custom_wait = 1, up, 2} },

{func = party_swap, {custom_wait = 0, 0, 1} },--this opens the overworld menu and swaps the chosen party entry with the other chosen party entry.

--Misty
{func = move, {custom_wait = 0, up, 24, left, 8, up, 10, left, 12, up, 10} },
{func = move, {up, 1} },
{func = move, {custom_wait = 1, up, 3} },
{func = move, {custom_wait = 0, up, 25} },
{func = move, {up, 1} },
{func = move, {custom_wait = 1, up, 2} },
{func = move, {custom_wait = 0, up, 3, right, 4} },
{func = move, {right, 1} },

{func = fight, {move = rock_slide, 0, 0, 1} },

{func = move, {custom_wait = 0, right, 10} },
{func = move, {right, 1} },

{func = fight, {move = mega_kick, 0, 0, 1}, {move = cross_chop, 0, 0, 1} },
{func = fight, {move = mega_kick, 0, 0, 1}, {move = cross_chop, 0, 0, 1} },

{func = move, {custom_wait = 0, right, 3, down, 3, right, 5, up, 3, left, 2, up, 4, right, 5, down, 4} },
{func = move, {down, 1} },

{func = fight, {move = mega_kick, min_pps = 4, 0, 0, 1}, {move = cross_chop, 0, 0, 1} },
{func = fight, {move = mega_kick, min_pps = 4, 0, 0, 1}, {move = cross_chop, min_pps = 3, 0, 0, 1} },
{func = fight, {move = mega_kick, min_pps = 4, 0, 0, 1}, {move = cross_chop, min_pps = 3, 0, 0, 1} },

{func = move, {custom_wait = 0, right, 3, up, 2} },
{func = move, {up, 1} },

{func = fight, {move = rock_slide, 0, 0, 1} },

{func = move, {custom_wait = 0, right, 10, up, 2, right, 2} },
{func = move, {custom_wait = 0, right, 2, down, 6, left, 4, down, 2, left, 31, down, 43, left, 3, down, 9, right, 8, up, 12, right, 1, talk, 1} },

{func = fight, {move = rock_slide, 0, 0, 1} },
{func = fight, {move = rock_slide, 0, 0, 1} },
{func = fight, {move = mega_kick, 0, 0, 1} },

{func = move, {custom_wait = 0, down, 14, left, 13, down, 6, right, 4, down, 2} },

{func = move, {down, 1} },
{func = move, {custom_wait = 1, down, 2} },
{func = move, {custom_wait = 0, down, 22, right, 7, down, 19, left, 16, down, 26, right, 14, down, 8} },

{func = move, {down, 1} },
{func = move, {custom_wait = 1, down, 1} },
{func = move, {custom_wait = 0, down, 9} },

{func = move, {down, 1} },
{func = move, {custom_wait = 1, down, 2} },
{func = move, {custom_wait = 0, down, 35, right, 22, up, 7} },
{func = move, {custom_wait = 0, up, 10} },

{func = move, {up, 1} },
{func = move, {custom_wait = 0, up, 2, encounter_steps = 14} },
{func = move, {custom_wait = 0, up, 13, left, 1} },

{func = move, {left, 1} },
{func = fight_run, {custom_states = 50} },
{func = move, {custom_wait = 0, left, 2, encounter_steps = 14} },
{func = move, {custom_wait = 0, left, 3, up, 11} },

{func = move, {up, 1} },
{func = fight_run, {custom_states = 50} },
{func = move, {custom_wait = 0, up, 2, encounter_steps = 14} },
{func = move, {custom_wait = 0, up, 2, left, 4, up, 10, down, 9} },

{func = move, {down, 1} },
{func = move, {custom_wait = 1, left, 2} },
{func = move, {custom_wait = 0, left, 2, down, 2, left, 6, up, 1, talk, 1, up, 2, right, 4, up, 11, left, 13, up, 11, right, 10, up, 14} },
{func = move, {up, 1} },

{func = fight, {move = mega_kick, 0, 0, 1} },

{func = move, {custom_wait = 0, up, 10, talk, 1} },

{func = fight, {move = cross_chop, 0, 39, 1}, {move = cross_chop, 0, 78, 1} },
{func = fight, {move = cross_chop, 0,  0, 1} },
{func = fight, {move = rock_slide, 0,  0, 1} },
{func = fight, {move = rock_slide, 0,  0, 1} },
{func = fight, {move = mega_kick,  0,  0, 1} },

{func = move, {custom_wait = 0, down, 25, left, 10, down, 11, right, 13, down, 2} },

{func = move, {down, 1} },
{func = move, {custom_wait = 1, down, 2} },
{func = move, {custom_wait = 0, down, 6, left, 4, down, 1, talk, 1, down, 2, right, 6, down, 2, right, 9, down, 7, left, 4, down, 2, talk, 1, down, 8, right, 2, down, 14} },

{func = move, {down, 1} },
{func = move, {custom_wait = 1, down, 2} },
{func = move, {custom_wait = 0, down, 12, left, 9, down, 6} },
{func = move, {custom_wait = 0, down, 20, right, 9, down, 3, right, 3, down, 10} },

--Route 1 to Pallet Town
{func = move, {down, 1} },
{func = move, {custom_wait = 1, down, 2} },
{func = move, {custom_wait = 0, down, 9} },
{func = move, {down, 5} },
{func = move, {custom_wait = 0, down, 47, left, 4, down, 6} },

{func = move, {custom_states = 50, down, 1} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 1, down, 2, encounter_steps = 13},
{branch = 2, custom_states = 50, custom_wait = 0, down, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 0, down, 12},
{branch = 2, custom_states = 50, custom_wait = 0, down, 13} },
{func = move,
{branch = 1, custom_states = 50, down, 1},
{branch = 2, custom_states = 50, down, 1} },

{func = fight_run,
{branch = 1, custom_states = 50},
{branch = 2, custom_states = 50} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 0, down, 2, encounter_steps = 14},
{branch = 2, custom_states = 50, custom_wait = 1, down, 2, encounter_steps = 13} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 0, down, 13},
{branch = 2, custom_states = 50, custom_wait = 0, down, 12} },
{func = move,
{branch = 1, custom_states = 50, down, 1},
{branch = 2, custom_states = 50, down, 1} },

{func = fight_run, {custom_states = 50} },
{func = move, {custom_states = 50, custom_wait = 1, down, 4, min_encounter_steps = 8} },
{func = move, {custom_states = 50, custom_wait = 0, down, 7} },

{func = move, {custom_states = 50, down, 1} },
{func = move, {custom_states = 50, custom_wait = 1, down, 2, min_encounter_steps = 13} },
{func = move, {custom_states = 50, custom_wait = 0, down, 3, right, 15, down, 3, right, 6, down, 4, right, 8} },

{func = move, {custom_states = 50, right, 1} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 0, right, 2, encounter_steps = 14},
{branch = 2, custom_states = 50, custom_wait = 1, right, 2, encounter_steps = 13},
{branch = 3, custom_states = 50, custom_wait = 1, right, 2, encounter_steps = 12} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 0, right, 13},
{branch = 2, custom_states = 50, custom_wait = 0, right, 12},
{branch = 3, custom_states = 50, custom_wait = 0, right, 11} },

{func = move,
{branch = 1, custom_states = 50, right, 1},
{branch = 2, custom_states = 50, right, 1},
{branch = 3, custom_states = 50, right, 1} },
{func = fight_run,
{branch = 1, custom_states = 50},
{branch = 2, custom_states = 50},
{branch = 3, custom_states = 50} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 1, right, 2, min_encounter_steps = 12},
{branch = 2, custom_states = 50, custom_wait = 1, right, 2, min_encounter_steps = 13},
{branch = 3, custom_states = 50, custom_wait = 0, right, 2, min_encounter_steps = 14} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 0, right, 11},
{branch = 2, custom_states = 50, custom_wait = 0, right, 12},
{branch = 3, custom_states = 50, custom_wait = 0, right, 13} },


{func = move, {custom_states = 200, right, 1} },
{func = move, {custom_states = 200, custom_wait = 1, right, 2, min_encounter_steps = 13} },
{func = move, {custom_states = 200, custom_wait = 0, right, 10, up, 7} },

{func = move, {custom_states = 400, down, 2} },
{func = move,
{branch = 1, custom_states = 400, custom_wait = 1, right, 2, encounter_steps = 15},
{branch = 2, custom_states = 400, custom_wait = 1, right, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_states = 400, custom_wait = 0, right, 16},
{branch = 2, custom_states = 400, custom_wait = 0, right, 15} },

{func = move,
{branch = 1, custom_states = 400, right, 1, no_fight = true},
{branch = 2, custom_states = 400, right, 1, no_fight = true} },
{func = fight_run,
{branch = 1, custom_states = 400},
{branch = 2, custom_states = 400} },
{func = move,
{branch = 1, custom_states = 400, custom_wait = 1, right, 2, min_encounter_steps = 13, no_fight = true},
{branch = 2, custom_states = 400, custom_wait = 0, right, 2, min_encounter_steps = 14, no_fight = true} },
{func = move,
{branch = 1, custom_states = 400, custom_wait = 0, right, 12, no_fight = true},
{branch = 2, custom_states = 400, custom_wait = 0, right, 13, no_fight = true} },

{func = move, {custom_states = 400, custom_wait = 50, right, 1} },

{func = move,
{branch = 1, custom_states = 800, custom_wait = 1, down, 2, encounter_steps =  9},
{branch = 2, custom_states = 800, custom_wait = 1, down, 2, encounter_steps = 10},
{branch = 3, custom_states = 800, custom_wait = 1, down, 2, encounter_steps = 11} },
{func = move,
{branch = 1, custom_states = 800, custom_wait = 0, down, 4, up, 5},
{branch = 2, custom_states = 800, custom_wait = 0, down, 5, up, 5},
{branch = 3, custom_states = 800, custom_wait = 0, down, 5, up, 6} },

{func = catch,
{branch = 1, species = garchomp, level=58, direction = up,
move1={hyper_beam}, move2={cross_chop,take_down,ice_ball}, move3={cross_chop,take_down,ice_ball}, move4={cross_chop,take_down,ice_ball} },
{branch = 2, species = garchomp, level=58, direction = up,
move1={hyper_beam}, move2={cross_chop,take_down,ice_ball}, move3={cross_chop,take_down,ice_ball}, move4={cross_chop,take_down,ice_ball} },
{branch = 3, species = garchomp, level=58, direction = up,
move1={hyper_beam}, move2={cross_chop,take_down,ice_ball}, move3={cross_chop,take_down,ice_ball}, move4={cross_chop,take_down,ice_ball} } },

{func = fight_exit,
{branch = 1},
{branch = 2},
{branch = 3} },
{func = move,
{branch = 1, left, 2, no_fight = true},
{branch = 2, up, 1, left, 2},
{branch = 3, left, 2, no_fight = true} },

{func = move,
{branch = 1, custom_wait = 0, left, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, left, 2, encounter_steps = 13},
{branch = 3, custom_wait = 1, left, 2, encounter_steps = 12} },
{func = party_swap,
{branch = 1, custom_wait = 0, 0, -1},
{branch = 2, custom_wait = 0, 0, -1},
{branch = 3, custom_wait = 0, 0, -1} },
{func = move,
{branch = 1, custom_wait = 0, left, 13},
{branch = 2, custom_wait = 0, left, 12},
{branch = 3, custom_wait = 0, left, 11} },
{func = hunt,
{branch = 1, species = 71, direction = left, move = take_down, 0, no_fight = true},
{branch = 2, species = 71, direction = left, move = take_down, 0, no_fight = true},
{branch = 3, species = 71, direction = left, move = take_down, 0, no_fight = true} },--the 0 value is just a placeholder for [1]
{func = fight_exit,
{branch = 1},
{branch = 2},
{branch = 3} },

{func = move,
{branch = 1, custom_wait = 1, left, 2, min_encounter_steps = 12},
{branch = 2, custom_wait = 1, left, 2, min_encounter_steps = 13},
{branch = 3, custom_wait = 0, left, 2, min_encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, left, 1},
{branch = 2, custom_wait = 0, left, 2},
{branch = 3, custom_wait = 0, left, 3} },

{func = move, {custom_wait = 0, left, 14, up, 8, talk, 1} },

--{func = move, {talk, 1} },--temporary

{func = fight, {move = take_down,  0,  0, 1} },
{func = fight, {move = take_down,  0,  0, 1} },
{func = fight, {move = take_down,  0,  0, 1} },


{func = move, {custom_wait = 0, down, 7} },

{func = move, {down, 1} },
{func = move, {custom_wait = 1, left, 3, min_encounter_steps = 13} },
{func = move, {custom_wait = 0, down, 6, left, 10} },


{func = move, {left, 1} },
{func = move,
{branch = 1, custom_wait = 0, left, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, left, 2, encounter_steps = 13},
{branch = 3, custom_wait = 1, left, 2, encounter_steps = 12} },
{func = move,
{branch = 1, custom_wait = 0, left, 13},
{branch = 2, custom_wait = 0, left, 12},
{branch = 3, custom_wait = 0, left, 11} },

{func = move,
{branch = 1, left, 1},
{branch = 2, left, 1},
{branch = 3, left, 1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3} },
{func = move,
{branch = 1, custom_wait = 1, left, 2, min_encounter_steps = 12},
{branch = 2, custom_wait = 1, left, 2, min_encounter_steps = 13},
{branch = 3, custom_wait = 0, left, 2, min_encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, left, 11},
{branch = 2, custom_wait = 0, left, 12},
{branch = 3, custom_wait = 0, left, 13} },


{func = move, {left, 1} },
{func = move, {custom_wait = 1, left, 2, min_encounter_steps = 13} },
{func = move, {custom_wait = 0, left, 5, up, 4, left, 17, up, 3, left, 4, up, 6} },


{func = move, {custom_states = 50, up, 1} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 1, up, 2, encounter_steps = 13},
{branch = 2, custom_states = 50, custom_wait = 0, up, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_states = 50, custom_wait = 0, up, 12},
{branch = 2, custom_states = 50, custom_wait = 0, up, 13} },
{func = move,
{branch = 1, custom_states = 50, up, 1},
{branch = 2, custom_states = 50, up, 1} },

{func = fight_run,
{branch = 1},
{branch = 2} },
{func = move,
{branch = 1, custom_wait = 0, up, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, up, 2, encounter_steps = 13} },
{func = move,
{branch = 1, custom_wait = 0, up, 13},
{branch = 2, custom_wait = 0, up, 12} },
{func = move,
{branch = 1, up, 1},
{branch = 2, up, 1} },

{func = fight_run, {custom_states = 50} },
{func = move, {custom_states = 50, custom_wait = 1, up, 2, min_encounter_steps = 10} },
{func = move, {custom_states = 50, custom_wait = 0, up, 17, right, 4, up, 21} },

--Route 1 to Emerald City
{func = move, {custom_states = 50, up, 1} },
{func = move, {custom_states = 50, custom_wait = 1, up, 2, min_encounter_steps = 15} },
{func = move, {custom_states = 50, custom_wait = 0, up, 6, left, 4, up, 2} },
{func = move, {custom_states = 50, up, 4} },
{func = move, {custom_states = 50, custom_wait = 0, right, 4, up, 4, left, 4, up, 11, right, 9, up, 12, left, 5, up, 7, left, 13, up, 26, right, 10, up, 2, right, 9, down, 6, left, 4, up, 23, talk, 1} },

{func = fight, {custom_states = 50, move = cross_chop, 0,  0, 1} },
{func = fight, {move = ice_ball,   0,108, 1}, {move = ice_ball,   0,110, 1} },
{func = fight, {move = ice_ball,   0, 86, 1}, {move = ice_ball,   0, 88, 1} },
{func = fight, {move = ice_ball,   0, 64, 1}, {move = ice_ball,   0, 66, 1} },
{func = fight, {move = ice_ball,   0, 42, 1}, {move = ice_ball,   0, 44, 1} },
{func = fight, {move = ice_ball,   0, 20, 1}, {move = ice_ball,   0, 22, 1} },
{func = fight, {move = ice_ball,   0,  0, 1} },
{func = fight, {move = take_down,  0,  0, 1}, {move = hyper_beam, 0,  0, 1} },--very unlikely for take down
{func = fight, {move = cross_chop, 0,  0, 1} },--probably impossible for take down
{func = fight, {move = take_down,  0,  0, 1} },

{func = move, {custom_wait = 0, down, 27, left, 3, down, 4, right, 4, down, 7, left, 5, down, 10} },

--Route 1 to Pallet Town
{func = move, {custom_states = 50, down, 1} },
{func = move, {custom_states = 50, custom_wait = 1, down, 2} },
{func = move, {custom_states = 50, custom_wait = 0, down, 9} },
{func = move, {custom_states = 50, down, 5} },
{func = move, {custom_states = 50, custom_wait = 0, down, 46, right, 4, up, 8, talk, 1, down, 9, left, 4, up, 21} },

--Route 1 to Emerald City
{func = move, {custom_states = 50, up, 1} },
{func = move, {custom_wait = 1, up, 2, min_encounter_steps = 15} },
{func = move, {custom_wait = 0, up, 6, left, 4, up, 2} },
{func = move, {up, 4} },
{func = move, {custom_wait = 0, right, 4, up, 4, left, 4, up, 11, right, 9, up, 12, left, 5, up, 7, left, 13, up, 13, left, 12} },

{func = move, {left, 1} },
{func = move, {custom_wait = 1, left, 1} },
{func = move, {custom_wait = 0, left, 3, down, 3, left, 6, up, 7, left,7, down, 7, left, 13, up, 7, left, 13, up, 4} },

--Victory Road

{func = move, {up, 1} },
{func = move,
{branch = 1, custom_wait = 0, up, 2, encounter_steps = 14},
{branch = 2, custom_wait = 0, up, 2, encounter_steps = 14},
{branch = 3, custom_wait = 1, up, 2, encounter_steps = 13} },
{func = move,
{branch = 1, custom_wait = 0, up, 1, left, 4, down, 3, left, 3, up, 5, right, 2},
{branch = 2, custom_wait = 0, up, 1, left, 4, down, 3, left, 3, up, 5, right, 2},
{branch = 3, custom_wait = 0, up, 1, left, 4, down, 3, left, 3, up, 5, right, 1} },
{func = move,
{branch = 1, right, 1},
{branch = 2, right, 1},
{branch = 3, right, 1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3} },
{func = move,
{branch = 1, custom_wait = 0, right, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, right, 2, encounter_steps = 13},
{branch = 3, custom_wait = 0, right, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, right, 3, up, 11},
{branch = 2, custom_wait = 0, right, 3, up, 10},
{branch = 3, custom_wait = 0, right, 4, up, 10} },
{func = move,
{branch = 1, up, 1},
{branch = 2, up, 1},
{branch = 3, up, 1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3} },
{func = move,
{branch = 1, custom_wait = 1, left, 2,        encounter_steps = 14},
{branch = 2, custom_wait = 0, up, 1, left, 2, encounter_steps = 14},
{branch = 3, custom_wait = 0, up, 1, left, 2, encounter_steps = 14} },

{func = move, {custom_wait = 0, left, 6, down, 3, right, 6, down, 1} },
{func = move, {down, 1} },
{func = fight_run, {} },

{func = move,
{branch = 1, custom_wait = 0, down, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, down, 2, encounter_steps = 13} },
{func = move,
{branch = 1, custom_wait = 0, down, 1, left, 4, up, 2, left, 7, down, 3},
{branch = 2, custom_wait = 0, down, 1, left, 4, up, 2, left, 7, down, 2} },
{func = move,
{branch = 1, down, 1},
{branch = 2, down, 1} },
{func = fight_run,
{branch = 1},
{branch = 2} },
{func = move,
{branch = 1, custom_wait = 1, left, 2,          encounter_steps = 14},
{branch = 2, custom_wait = 0, down, 1, left, 2, encounter_steps = 14} },

{func = move, {custom_wait = 0, left, 1, up, 7, right, 3, up, 4, left, 2} },
{func = move, {left, 1} },
{func = fight_run, {} },
{func = move, {custom_wait = 1, left, 1, up, 1, min_encounter_steps = 12} },
{func = move, {custom_wait = 1, up, 2, right, 5, up, 4, left, 3} },

{func = move, {left, 1} },
{func = move,
{branch = 1, custom_wait = 1, right, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, down, 4, left, 3, down, 4, right, 6} },
{func = move,
{branch = 1, right, 1} },
{func = fight_run,
{branch = 1} },
{func = move,
{branch = 1, custom_wait = 1, right, 1, up, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, up, 3, right, 11, down, 1} },
{func = move,
{branch = 1, down, 1} },
{func = fight_run,
{branch = 1} },
{func = move,
{branch = 1, custom_wait = 1, down, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, right, 11, up, 4} },
{func = move,
{branch = 1, up, 1} },
{func = fight_run,
{branch = 1} },
{func = move,
{branch = 1, custom_wait = 1, up, 1, left, 2, encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0, left, 1, down, 4, left, 4, up, 5, left, 3} },
{func = move,
{branch = 1, left, 1} },
{func = fight_run,
{branch = 1} },
{func = move,
{branch = 1, custom_wait = 1, left, 2, min_encounter_steps = 12} },
{func = move,
{branch = 1, custom_wait = 0, left, 3, up, 5, right, 5} },

{func = move, {right, 1} },
{func = move,
{branch = 1, custom_wait = 1, right, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, right, 2, encounter_steps = 13},
{branch = 3, custom_wait = 1, right, 2, encounter_steps = 12},
{branch = 4, custom_wait = 1, right, 2, encounter_steps = 11} },
{func = move,
{branch = 1, custom_wait = 0, up, 2, right, 9, up, 5},
{branch = 2, custom_wait = 0, up, 2, right, 9, up, 4},
{branch = 3, custom_wait = 0, up, 2, right, 9, up, 3},
{branch = 4, custom_wait = 0, up, 2, right, 9, up, 2} },
{func = move,
{branch = 1, up, 1},
{branch = 2, up, 1},
{branch = 3, up, 1},
{branch = 4, up, 1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
{branch = 4} },
{func = move,
{branch = 1, custom_wait = 1, up, 2, min_encounter_steps = 11},
{branch = 2, custom_wait = 1, up, 2, min_encounter_steps = 12},
{branch = 3, custom_wait = 1, up, 2, min_encounter_steps = 13},
{branch = 4, custom_wait = 1, up, 2, min_encounter_steps = 14} },
{func = move,
{branch = 1, custom_wait = 0,        left, 1},
{branch = 2, custom_wait = 0, up, 1, left, 1},
{branch = 3, custom_wait = 0, up, 2, left, 1},
{branch = 4, custom_wait = 0, up, 3, left, 1} },

{func = move, {custom_wait = 0, left, 10, up, 31, right, 5, up, 2} },
{func = pc,   {custom_wait = 0, 1, 0, 0} },
{func = move, {custom_wait = 0, down, 4, right, 18, up, 12, left, 1} },
{func = move, {talk, 1} },

{func = fight, {move = cross_chop, 0, 0, 1} },
{func = fight, {move = take_down,  0, 0, 1} },
{func = fight, {move = hyper_beam, 0, 0, 1} },
{func = fight, {move = take_down,  0, 0, 1} },

{func = move, {custom_wait = 0, up, 12, left, 1} },
{func = move, {talk, 1} },

{func = fight, {move = hyper_beam, 0, 0, 1} },
{func = fight, {move = hyper_beam, 0, 0, 1} },
{func = fight, {move = take_down,  0, 0, 1} },
{func = fight, {move = hyper_beam, 0, 0, 1} },

{func = move, {custom_wait = 0, up, 12, left, 1} },
{func = item, {custom_wait = 0, {1, 0, 0, move = hyper_beam} } },
{func = move, {talk, 1} },

{func = fight, {move = hyper_beam, 0, 0, 1} },
{func = fight, {move = hyper_beam, 0, 0, 1} },
{func = fight, {move = hyper_beam, 0, 0, 1} },
{func = fight, {move = cross_chop, 0, 0, 1} },

{func = move, {custom_wait = 0, up, 12, left, 1} },
{func = move, {talk, 1} },

{func = fight, {move = take_down,  0, 0, 1}, {move = hyper_beam, 0, 0, 1} },
{func = fight, {move = hyper_beam, 0, 0, 1} },
{func = fight, {move = take_down,  0, 0, 1}, {move = hyper_beam, 0, 0, 1, min_pps = 1} },
{func = fight, {move = cross_chop, 0, 0, 1} },

{func = move, {custom_wait = 0, up, 28} },
{func = item, {custom_wait = 0, {1, 0, 0, move = hyper_beam} } },
{func = move, {talk, 1} },

{func = fight,
{custom_states = 50, move = hyper_beam, 0,  0, 1},
{custom_states = 50, move = take_down,  0,  0, 1} },
{func = fight,
{branch = 1, custom_states = 50, move = hyper_beam, 0, 80, 1},
{branch = 2, move = take_down,  0, 83, 1},
{branch = 2, move = take_down,  0, 87, 1} },
{func = fight,
{custom_states = 50, move = hyper_beam, 0,  0, 1, min_pps = 4},
{custom_states = 50, move = take_down,  0,  0, 1} },
{func = fight, {custom_states = 50, move = hyper_beam, 0,  0, 1} },
{func = fight, {custom_states = 50, move = hyper_beam, 0,  0, 1} },
{func = fight, {custom_states = 50, move = hyper_beam, 0,  0, 1} },

{func = move, {custom_states = 50, custom_wait = 0, talk, 1} },
{func = move, {custom_states = 50, custom_wait = 0, down, 24} },

{func = move, {custom_states = 200, down, 1} },
{func = move,
{branch = 1, custom_wait = 0, down, 1, right, 1, encounter_steps = 15},
{branch = 2, custom_wait = 1, down, 1, right, 1, encounter_steps = 14},
{branch = 3, custom_wait = 1, down, 1, right, 1, encounter_steps = 13},
{branch = 4, custom_wait = 1, down, 1, right, 1, encounter_steps = 12},
{branch = 5, custom_wait = 1, down, 1, right, 1, encounter_steps = 11},
{branch = 6, custom_wait = 1, down, 1, right, 1, encounter_steps = 10},
{branch = 7, custom_wait = 0, right, 2, encounter_steps = 14},
{branch = 8, custom_wait = 0, right, 2, encounter_steps = 13},
{branch = 9, custom_wait = 0, right, 2, encounter_steps = 12},
{branch =10, custom_wait = 0, right, 2, encounter_steps = 11},
{branch =11, custom_wait = 0, right, 2, encounter_steps = 10},
{branch =12, custom_wait = 0, right, 2, encounter_steps =  9} },
{func = move,
{branch = 1, custom_wait = 0, right, 11, left, 4},
{branch = 2, custom_wait = 0, right, 11, left, 3},
{branch = 3, custom_wait = 0, right, 11, left, 2},
{branch = 4, custom_wait = 0, right, 11, left, 1},
{branch = 5, custom_wait = 0, right, 10},
{branch = 6, custom_wait = 0, right,  9},
{branch = 7, custom_wait = 0, right, 10, left, 4},
{branch = 8, custom_wait = 0, right, 10, left, 3},
{branch = 9, custom_wait = 0, right, 10, left, 2},
{branch =10, custom_wait = 0, right, 10, left, 1},
{branch =11, custom_wait = 0, right,  9},
{branch =12, custom_wait = 0, right,  8} },
{func = move,
{branch = 1, left, 1},
{branch = 2, left, 1},
{branch = 3, left, 1},
{branch = 4, left, 1},
{branch = 5, right, 1},
{branch = 6, right, 1},
{branch = 7, left, 1},
{branch = 8, left, 1},
{branch = 9, left, 1},
{branch =10, left, 1},
{branch =11, right, 1},
{branch =12, right, 1} },
{func = fight, {move = take_down, 0, 999, 0}, {move = ice_ball, 0, 999, 0} },--intentional own ko

{func = move, {custom_wait = 0, left, 11, up, 10, left, 8} },
{func = move, {left, 1} },
{func = move, {custom_wait = 1, left, 1} },
{func = move, {custom_wait = 0, left, 2, down, 3, left, 7, up, 7, left, 11, down, 7, left, 9, up, 7, left, 26, down, 2} },

{func = move, {down, 1} },
{func = move,
{custom_wait = 1, down, 1, left, 3, down, 1},
{custom_wait = 0, left, 3, down, 2} },
{func = move,
{branch = 1, custom_wait = 0, down, 1, encounter_steps = 15},
{branch = 2, custom_wait = 0, down, 1, encounter_steps = 14},
{branch = 3, custom_wait = 0, down, 1, encounter_steps = 13},
{branch = 4, custom_wait = 0, down, 1, encounter_steps = 12},
{branch = 5, custom_wait = 0, down, 1, encounter_steps = 11} },
{func = move,
{branch = 1, custom_wait = 0, left, 29, down, 5, left, 4},
{branch = 2, custom_wait = 0, left, 27},
{branch = 3, custom_wait = 0, left, 26},
{branch = 4, custom_wait = 0, left, 25},
{branch = 5, custom_wait = 0, left, 24} },
{func = move,
{branch = 1, left, 1},
{branch = 2, left, 1},
{branch = 3, left, 1},
{branch = 4, left, 1},
{branch = 5, left, 1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
{branch = 4},
{branch = 5} },
{func = move,
{branch = 1, custom_wait = 1, left, 8},
{branch = 2, custom_wait = 1, left, 1, down, 5, left, 13},
{branch = 3, custom_wait = 1, left, 2, down, 5, left, 13},
{branch = 4, custom_wait = 1, left, 3, down, 5, left, 13},
{branch = 5, custom_wait = 0, left, 4, down, 5, left, 13} },

{func = move, {custom_states = 100, left, 1} },
{func = move, {custom_states = 100, custom_wait = 1, left, 2, min_encounter_steps = 15} },
{func = move, {custom_states = 100, custom_wait = 0, down, 3, left, 9, up, 4, left, 4, up, 7, left, 2, up, 2, left, 2, up, 6, left, 3, up, 2, left, 3, up, 6, left, 2, up, 4} },

--Mt. Silver room 1
{func = move, {custom_states = 100, up, 1} },
{func = move,
{branch = 1, custom_wait = 0, up, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, up, 2, encounter_steps = 13},
{branch = 3, custom_wait = 1, up, 2, encounter_steps = 12},
--{branch = 4, custom_wait = 1, up, 2, encounter_steps = 12},
{branch = 5, custom_wait = 1, up, 2, encounter_steps = 11},
{branch = 6, custom_wait = 1, up, 2, encounter_steps = 10},
{branch = 7, custom_wait = 1, up, 2, encounter_steps =  9},
{branch = 8, custom_wait = 1, up, 2, encounter_steps =  8} },
{func = move,
{branch = 1, custom_wait = 0, up, 5, right, 4, up, 4, left, 3},
{branch = 2, custom_wait = 0, up, 5, right, 4, up, 4, left, 2},
{branch = 3, custom_wait = 0, up, 5, right, 4, up, 4, left, 1},
--{branch = 4, custom_wait = 0, up, 5, right, 4, up, 4},
{branch = 5, custom_wait = 0, up, 5, right, 4, up, 3},
{branch = 6, custom_wait = 0, up, 5, right, 4, up, 2},
{branch = 7, custom_wait = 0, up, 5, right, 4, up, 1},
{branch = 8, custom_wait = 0, up, 5, right, 3} },
{func = move,
{branch = 1, left, 1},
{branch = 2, left, 1},
{branch = 3, left, 1},
--{branch = 4, up,   1},
{branch = 5, up,   1},
{branch = 6, up,   1},
{branch = 7, up,   1},
{branch = 8, right,1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
--{branch = 4},
{branch = 5},
{branch = 6},
{branch = 7},
{branch = 8} },
{func = move,
{branch = 1, custom_wait = 1,        left, 1},
{branch = 2, custom_wait = 1,        left, 2},
{branch = 3, custom_wait = 1,        left, 3},
--{branch = 4, custom_wait = 1, },
{branch = 5, custom_wait = 1,        left, 5},
{branch = 6, custom_wait = 1, up, 1, left, 5},
{branch = 7, custom_wait = 1, up, 2, left, 5},
{branch = 8, custom_wait = 1, up, 4, left, 5} },

{func = move, {custom_states = 100, custom_wait = 0, left, 1} },
{func = move,
{branch = 1, custom_wait = 0, left, 1, encounter_steps = 13},
{branch = 2, custom_wait = 0, left, 1, encounter_steps = 12},
{branch = 3, custom_wait = 0, left, 1, encounter_steps = 11},
{branch = 4, custom_wait = 0, left, 1, encounter_steps = 10},
{branch = 5, custom_wait = 0, left, 1, encounter_steps =  9},
{branch = 6, custom_wait = 0, left, 1, encounter_steps =  8},
{branch = 7, custom_wait = 0, left, 1, encounter_steps =  7},
{branch = 8, custom_wait = 0, left, 1, encounter_steps =  6},
{branch = 9, custom_wait = 0, left, 1, encounter_steps =  5} },
{func = move,
{branch = 1, custom_wait = 0, left, 2, up, 6, right, 5, down, 2},
{branch = 2, custom_wait = 0, left, 2, up, 6, right, 5, down, 1},
{branch = 3, custom_wait = 0, left, 2, up, 6, right, 4},
{branch = 4, custom_wait = 0, left, 2, up, 6, right, 3},
{branch = 5, custom_wait = 0, left, 2, up, 6, right, 2},
{branch = 6, custom_wait = 0, left, 2, up, 6, right, 1},
{branch = 7, custom_wait = 0, left, 2, up, 5},
{branch = 8, custom_wait = 0, left, 2, up, 4},
{branch = 9, custom_wait = 0, left, 2, up, 3} },
{func = move,
{branch = 1, down,  1},
{branch = 2, down,  1},
{branch = 3, right, 1},
{branch = 4, right, 1},
{branch = 5, right, 1},
{branch = 6, right, 1},
{branch = 7, up,    1},
{branch = 8, up,    1},
{branch = 9, up,    1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
{branch = 4},
{branch = 5},
{branch = 6},
{branch = 7},
{branch = 8},
{branch = 9} },
{func = move,
{branch = 1, custom_wait = 1,                           right, 1},
{branch = 2, custom_wait = 1,                  down, 1, right, 1},
{branch = 3, custom_wait = 1,                  down, 3, right, 1},
{branch = 4, custom_wait = 1,        right, 1, down, 3, right, 1},
{branch = 5, custom_wait = 1,        right, 2, down, 3, right, 1},
{branch = 6, custom_wait = 1,        right, 3, down, 3, right, 1},
{branch = 7, custom_wait = 1,        right, 5, down, 3, right, 1},
{branch = 8, custom_wait = 1, up, 1, right, 5, down, 3, right, 1},
{branch = 9, custom_wait = 1, up, 2, right, 5, down, 3, right, 1} },

{func = move, {custom_states = 100, custom_wait = 0, right, 1} },
{func = move,
{branch = 1, custom_wait = 0, right, 1, encounter_steps = 13},
{branch = 2, custom_wait = 0, right, 1, encounter_steps = 12},
{branch = 3, custom_wait = 0, right, 1, encounter_steps = 11},
{branch =41, custom_wait = 0, right, 1, encounter_steps = 10},
{branch =42, custom_wait = 0, right, 1, encounter_steps = 10},
{branch =44, custom_wait = 0, right, 1, encounter_steps = 10},
{branch =51, custom_wait = 0, right, 1, encounter_steps =  9},
{branch =52, custom_wait = 0, right, 1, encounter_steps =  9},
{branch =55, custom_wait = 0, right, 1, encounter_steps =  9},
{branch =61, custom_wait = 0, right, 1, encounter_steps =  8},
{branch =62, custom_wait = 0, right, 1, encounter_steps =  8},
{branch =66, custom_wait = 0, right, 1, encounter_steps =  8},
{branch =67, custom_wait = 0, right, 1, encounter_steps =  8},
{branch = 7, custom_wait = 0, right, 1, encounter_steps =  7},
{branch =77, custom_wait = 0, right, 1, encounter_steps =  7},
{branch = 8, custom_wait = 0, right, 1, encounter_steps =  6} },
{func = move,
{branch = 1, custom_wait = 0, right, 5, up, 3, left, 2, up, 5},
{branch = 2, custom_wait = 0, right, 5, up, 3, left, 2, up, 4},
{branch = 3, custom_wait = 0, right, 5, up, 3, left, 2, up, 3},
{branch =41, custom_wait = 0, right, 5, up, 5, left, 1},
{branch =42, custom_wait = 0, right, 5, up, 5, left, 1},
{branch =44, custom_wait = 0, right, 5, up, 4, left, 2, up, 1},
{branch =51, custom_wait = 0, right, 5, up, 4, left, 1},
{branch =52, custom_wait = 0, right, 5, up, 4, left, 1},
{branch =55, custom_wait = 0, right, 5, up, 3, left, 2, up, 1},
{branch =61, custom_wait = 0, right, 5, up, 3, left, 1},
{branch =62, custom_wait = 0, right, 5, up, 3, left, 1},
{branch =66, custom_wait = 0, right, 5, up, 3},
{branch =67, custom_wait = 0, right, 5, up, 3},
{branch = 7, custom_wait = 0, right, 5, up, 2},
{branch =77, custom_wait = 0, right, 5, up, 2},
{branch = 8, custom_wait = 0, right, 5, up, 1} },
{func = move,
{branch = 1, up,   1},
{branch = 2, up,   1},
{branch = 3, up,   1},
{branch =41, left, 1},
{branch =42, left, 1},
{branch =44, up,   1},
{branch =51, left, 1},
{branch =52, left, 1},
{branch =55, up,   1},
{branch =61, left, 1},
{branch =62, left, 1},
{branch =66, up,   1},
{branch =67, up,   1},
{branch = 7, up,   1},
{branch =77, up,   1},
{branch = 8, up,   1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
{branch =41},
{branch =42},
{branch =44},
{branch =51},
{branch =52},
{branch =55},
{branch =61},
{branch =62},
{branch =66},
{branch =67},
{branch = 7},
{branch =77},
{branch = 8} },
{func = move,
{branch = 1, custom_wait = 1, up, 1, left, 1},
{branch = 2, custom_wait = 1, up, 2, left, 1},
{branch = 3, custom_wait = 1, up, 3, left, 1},
{branch =41, custom_wait = 1, up, 5, left, 1},
{branch =42, custom_wait = 1, left, 1, up, 5},
{branch =44, custom_wait = 1, up, 4, left, 1},
{branch =51, custom_wait = 1, up, 6, left, 1},
{branch =52, custom_wait = 1, left, 1, up, 6},
{branch =55, custom_wait = 1, up, 5, left, 1},
{branch =61, custom_wait = 1, up, 7, left, 1},
{branch =62, custom_wait = 1, left, 1, up, 7},
{branch =66, custom_wait = 1, left, 2, up, 6, left, 1},
{branch =67, custom_wait = 1, up, 1, left, 2, up, 5, left, 1},
{branch = 7, custom_wait = 1, up, 1, left, 2, up, 6, left, 1},
{branch =77, custom_wait = 1,        left, 2, up, 7, left, 1},
{branch = 8, custom_wait = 1, up, 1, left, 2, up, 7, left, 1} },

{func = move, {custom_states = 100, custom_wait = 0, left, 1} },
{func = move,
{branch = 1, custom_wait = 0, left, 1, encounter_steps = 13},
{branch = 2, custom_wait = 0, left, 1, encounter_steps = 12},
{branch = 3, custom_wait = 0, left, 1, encounter_steps = 11},
{branch = 4, custom_wait = 0, left, 1, encounter_steps = 10},
{branch = 5, custom_wait = 0, left, 1, encounter_steps =  9},
{branch = 6, custom_wait = 0, left, 1, encounter_steps =  8},
{branch = 7, custom_wait = 0, left, 1, encounter_steps =  7},
{branch = 8, custom_wait = 0, left, 1, encounter_steps =  6} },
{func = move,
{branch = 1, custom_wait = 0, left, 4, up, 6, right, 4},
{branch = 2, custom_wait = 0, left, 4, up, 6, right, 3},
{branch = 3, custom_wait = 0, left, 4, up, 6, right, 2},
{branch = 4, custom_wait = 0, left, 4, up, 6, right, 1},
{branch = 5, custom_wait = 0, left, 4, up, 5},
{branch = 6, custom_wait = 0, left, 4, up, 4},
{branch = 7, custom_wait = 0, left, 4, up, 3},
{branch = 8, custom_wait = 0, left, 4, up, 2} },
{func = move,
{branch = 1, right, 1},
{branch = 2, right, 1},
{branch = 3, right, 1},
{branch = 4, right, 1},
{branch = 5, up,    1},
{branch = 6, up,    1},
{branch = 7, up,    1},
{branch = 8, up,    1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
{branch = 4},
{branch = 5},
{branch = 6},
{branch = 7},
{branch = 8} },
{func = move,
{branch = 1, custom_wait = 1,                  down, 1},
{branch = 2, custom_wait = 1,        right, 1, down, 1},
{branch = 3, custom_wait = 1,        right, 2, down, 1},
{branch = 4, custom_wait = 1,        right, 3, down, 1},
{branch = 5, custom_wait = 1,        right, 5, down, 1},
{branch = 6, custom_wait = 1, up, 1, right, 5, down, 1},
{branch = 7, custom_wait = 1, up, 2, right, 5, down, 1},
{branch = 8, custom_wait = 1, up, 3, right, 5, down, 1} },

{func = move, {down, 1, min_encounter_steps = 8} },
{func = move, {custom_wait = 0, down, 1, right, 4, up, 4} },

--Mt. Silver room 2
{func = move, {custom_states = 100, up, 1} },
{func = move,
{branch = 1, custom_wait = 0, up, 1, left, 1, encounter_steps = 15},
{branch = 2, custom_wait = 1, up, 1, left, 1, encounter_steps = 14},
{branch = 3, custom_wait = 1, up, 1, left, 1, encounter_steps = 13},
{branch = 4, custom_wait = 1, up, 1, left, 1, encounter_steps = 12},
{branch = 5, custom_wait = 1, up, 1, left, 1, encounter_steps = 11},
{branch = 6, custom_wait = 1, up, 1, left, 1, encounter_steps = 10},
{branch = 7, custom_wait = 1, up, 1, left, 1, encounter_steps =  9},
{branch = 8, custom_wait = 0, left, 2, encounter_steps = 14},
{branch = 9, custom_wait = 0, left, 2, encounter_steps = 13},
{branch =10, custom_wait = 0, left, 2, encounter_steps = 12},
{branch =11, custom_wait = 0, left, 2, encounter_steps = 11},
{branch =12, custom_wait = 0, left, 2, encounter_steps = 10},
{branch =13, custom_wait = 0, left, 2, encounter_steps =  9} },
{func = move,
{branch = 1, custom_wait = 0, left, 8, up, 7},
{branch = 2, custom_wait = 0, left, 8, up, 6},
{branch = 3, custom_wait = 0, left, 8, up, 5},
{branch = 4, custom_wait = 0, left, 8, up, 4},
{branch = 5, custom_wait = 0, left, 8, up, 3},
{branch = 6, custom_wait = 0, left, 8, up, 2},
{branch = 7, custom_wait = 0, left, 8, up, 1},
{branch = 8, custom_wait = 0, left, 7, up, 7},
{branch = 9, custom_wait = 0, left, 7, up, 6},
{branch =10, custom_wait = 0, left, 7, up, 5},
{branch =11, custom_wait = 0, left, 7, up, 4},
{branch =12, custom_wait = 0, left, 7, up, 3},
{branch =13, custom_wait = 0, left, 7, up, 2} },
{func = move,
{branch = 1, up, 1},
{branch = 2, up, 1},
{branch = 3, up, 1},
{branch = 4, up, 1},
{branch = 5, up, 1},
{branch = 6, up, 1},
{branch = 7, up, 1},
{branch = 8, up, 1},
{branch = 9, up, 1},
{branch =10, up, 1},
{branch =11, up, 1},
{branch =12, up, 1},
{branch =13, up, 1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
{branch = 4},
{branch = 5},
{branch = 6},
{branch = 7},
{branch = 8},
{branch = 9},
{branch =10},
{branch =11},
{branch =12},
{branch =13} },
{func = move,
{branch = 1, custom_wait = 1, up, 1},
{branch = 2, custom_wait = 1, up, 2},
{branch = 3, custom_wait = 1, up, 3},
{branch = 4, custom_wait = 1, up, 4},
{branch = 5, custom_wait = 1, up, 5},
{branch = 6, custom_wait = 1, up, 6},
{branch = 7, custom_wait = 1, up, 7},
{branch = 8, custom_wait = 1, up, 2},
{branch = 9, custom_wait = 1, up, 3},
{branch =10, custom_wait = 1, up, 4},
{branch =11, custom_wait = 1, up, 5},
{branch =12, custom_wait = 1, up, 6},
{branch =13, custom_wait = 1, up, 7} },

{func = move, {custom_states = 100, custom_wait = 0, up, 1} },
{func = move,
{branch = 1, custom_wait = 0, up, 1, encounter_steps = 13},
{branch = 2, custom_wait = 0, up, 1, encounter_steps = 12},
{branch = 3, custom_wait = 0, up, 1, encounter_steps = 11},
{branch = 4, custom_wait = 0, up, 1, encounter_steps = 10},
{branch = 5, custom_wait = 0, up, 1, encounter_steps =  9},
{branch = 6, custom_wait = 0, up, 1, encounter_steps =  8},
{branch = 7, custom_wait = 0, up, 1, encounter_steps =  7},
{branch = 8, custom_wait = 0, up, 1, encounter_steps =  6},
{branch = 9, custom_wait = 0, up, 1, encounter_steps =  5} },
{func = move,
{branch = 1, custom_wait = 0, up, 1, left, 12},
{branch = 2, custom_wait = 0, up, 1, left, 11},
{branch = 3, custom_wait = 0, up, 1, left, 10},
{branch = 4, custom_wait = 0, up, 1, left,  9},
{branch = 5, custom_wait = 0, up, 1, left,  8},
{branch = 6, custom_wait = 0, up, 1, left,  7},
{branch = 7, custom_wait = 0, up, 1, left,  6},
{branch = 8, custom_wait = 0, up, 1, left,  5},
{branch = 9, custom_wait = 0, up, 1, left,  4} },
{func = move,
{branch = 1, left, 1},
{branch = 2, left, 1},
{branch = 3, left, 1},
{branch = 4, left, 1},
{branch = 5, left, 1},
{branch = 6, left, 1},
{branch = 7, left, 1},
{branch = 8, left, 1},
{branch = 9, left, 1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
{branch = 4},
{branch = 5},
{branch = 6},
{branch = 7},
{branch = 8},
{branch = 9} },
{func = move,
{branch = 1, custom_wait = 1, left, 1},
{branch = 2, custom_wait = 1, left, 2},
{branch = 3, custom_wait = 1, left, 3},
{branch = 4, custom_wait = 1, left, 4},
{branch = 5, custom_wait = 1, left, 5},
{branch = 6, custom_wait = 1, left, 6},
{branch = 7, custom_wait = 1, left, 7},
{branch = 8, custom_wait = 1, left, 8},
{branch = 9, custom_wait = 1, left, 9} },

{func = move, {custom_states = 100, custom_wait = 0, up, 1} },
{func = move,
{branch = 1, custom_wait = 0, up, 1, encounter_steps = 14},
{branch = 2, custom_wait = 0, up, 1, encounter_steps = 13},
{branch = 3, custom_wait = 0, up, 1, encounter_steps = 12},
{branch = 4, custom_wait = 0, up, 1, encounter_steps = 11},
{branch = 5, custom_wait = 0, up, 1, encounter_steps = 10},
{branch = 6, custom_wait = 0, up, 1, encounter_steps =  9},
{branch = 7, custom_wait = 0, up, 1, encounter_steps =  8},
{branch = 8, custom_wait = 0, up, 1, encounter_steps =  7},
{branch = 9, custom_wait = 0, up, 1, encounter_steps =  6} },
{func = move,
{branch = 1, custom_wait = 0, up, 1, left, 2, up, 8, right, 5},
{branch = 2, custom_wait = 0, up, 1, left, 2, up, 8, right, 4},
{branch = 3, custom_wait = 0, up, 1, left, 2, up, 8, right, 3},
{branch = 4, custom_wait = 0, up, 1, left, 2, up, 8, right, 2},
{branch = 5, custom_wait = 0, up, 1, left, 2, up, 8, right, 1},
{branch = 6, custom_wait = 0, up, 1, left, 2, up, 7},
{branch = 7, custom_wait = 0, up, 1, left, 2, up, 6},
{branch = 8, custom_wait = 0, up, 1, left, 2, up, 5},
{branch = 9, custom_wait = 0, up, 1, left, 2, up, 4} },
{func = move,
{branch = 1, right, 1},
{branch = 2, right, 1},
{branch = 3, right, 1},
{branch = 4, right, 1},
{branch = 5, right, 1},
{branch = 6, up, 1},
{branch = 7, up, 1},
{branch = 8, up, 1},
{branch = 9, up, 1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
{branch = 4},
{branch = 5},
{branch = 6},
{branch = 7},
{branch = 8},
{branch = 9} },
{func = move,
{branch = 1, custom_wait = 1, right, 1},
{branch = 2, custom_wait = 1, right, 2},
{branch = 3, custom_wait = 1, right, 3},
{branch = 4, custom_wait = 1, right, 4},
{branch = 5, custom_wait = 1, right, 5},
{branch = 6, custom_wait = 1, right, 7},
{branch = 7, custom_wait = 1, up, 1, right, 7},
{branch = 8, custom_wait = 1, up, 2, right, 7},
{branch = 9, custom_wait = 1, up, 3, right, 7} },

{func = move, {custom_states = 100, custom_wait = 0, up, 1, min_encounter_steps = 5} },
{func = move, {custom_states = 100, custom_wait = 0, up, 4} },

--Mt. Silver room 3
{func = move, {custom_states = 100, up, 1} },
{func = move,
{branch = 1, custom_wait = 0, up, 2, encounter_steps = 14},
{branch = 2, custom_wait = 1, up, 2, encounter_steps = 13},
{branch = 3, custom_wait = 1, up, 2, encounter_steps = 12},
{branch = 4, custom_wait = 1, up, 2, encounter_steps = 11},
{branch = 5, custom_wait = 1, up, 2, encounter_steps = 10},
{branch = 6, custom_wait = 1, up, 2, encounter_steps =  9},
{branch = 7, custom_wait = 1, up, 2, encounter_steps =  8} },
{func = move,
{branch = 1, custom_wait = 0, up, 13},
{branch = 2, custom_wait = 0, up, 12},
{branch = 3, custom_wait = 0, up, 11},
{branch = 4, custom_wait = 0, up, 10},
{branch = 5, custom_wait = 0, up,  9},
{branch = 6, custom_wait = 0, up,  8},
{branch = 7, custom_wait = 0, up,  7} },
{func = move,
{branch = 1, up, 1},
{branch = 2, up, 1},
{branch = 3, up, 1},
{branch = 4, up, 1},
{branch = 5, up, 1},
{branch = 6, up, 1},
{branch = 7, up, 1} },
{func = fight_run,
{branch = 1},
{branch = 2},
{branch = 3},
{branch = 4},
{branch = 5},
{branch = 6},
{branch = 7} },
{func = move,
{branch = 1, custom_wait = 1, up, 2, min_encounter_steps =  5},
{branch = 2, custom_wait = 1, up, 2, min_encounter_steps =  6},
{branch = 3, custom_wait = 1, up, 2, min_encounter_steps =  7},
{branch = 4, custom_wait = 1, up, 2, min_encounter_steps =  8},
{branch = 5, custom_wait = 1, up, 2, min_encounter_steps =  9},
{branch = 6, custom_wait = 1, up, 2, min_encounter_steps = 10},
{branch = 7, custom_wait = 1, up, 2, min_encounter_steps = 11} },
{func = move,
{branch = 1, custom_wait = 0, up, 4, talk, 1},
{branch = 2, custom_wait = 0, up, 5, talk, 1},
{branch = 3, custom_wait = 0, up, 6, talk, 1},
{branch = 4, custom_wait = 0, up, 7, talk, 1},
{branch = 5, custom_wait = 0, up, 8, talk, 1},
{branch = 6, custom_wait = 0, up, 9, talk, 1},
{branch = 7, custom_wait = 0, up,10, talk, 1} },

--Red battle
{func = fight, {custom_states = 100, move = hyper_beam, 0,  0, 1} },

{func = fight, {custom_states = 100, move = cross_chop, 0,  0, 1} },

{func = fight,
{custom_states = 50, branch = 1, move = cross_chop, 0, 98, 1},
{custom_states = 50, branch = 2, move = hyper_beam, 0, 98, 1},
{custom_states = 50, branch = 3, move = ice_ball,   0, 98, 1} },
{func = fight,
{custom_states = 50, move = cross_chop, 0,  0, 1},
{custom_states = 50, move = hyper_beam, 0,  0, 1},
{custom_states = 50, move = ice_ball,   0,  0, 1} },

{func = fight,
{custom_states = 50, branch = 1, custom_states = 50, move = cross_chop, 0,109, 1},
{custom_states = 50, branch = 1, custom_states = 50, move = cross_chop, 0,150, 1},
{custom_states = 50, branch = 2, custom_states = 50, move = hyper_beam, 0,107, 1} },
{func = fight,
{custom_states = 50, custom_states = 50, move = cross_chop, 0,  0, 1, min_pps = 2},
{custom_states = 50, custom_states = 50, move = hyper_beam, 0,  0, 1} },

{func = fight, {custom_states = 50, move = cross_chop, 0,  0, 1} },

{func = fight, {custom_states = 1, move = cross_chop, 0,  0, 1}, {custom_states = 1, move = hyper_beam, 0,  0, 1} },

{func = move, {custom_wait = 0, up, 1} },--just a crappy placeholder. remember to trim the movie file manually.

}
action[0] = {}
action[0][1] = {}
--action[0][0][0] = {}

local state = {}
for i = 1, #action do
	state[i] = {}
	for l = 1, #action[i] do
		state[i][l] = {}
	end
end
state[0] = {}
state[0][1] = {}
state[0][1][1] = {}
--state[0][1][2] = {}
state[0][1][1]["slot"] = savestate.object()
savestate.save(state[0][1][1]["slot"])

local function action_fight()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: fight, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				while memory.readbyte(addr_infight) ~= 1 do--if we're not into the fight yet
					if memory.readword(addr_overwdialog) == 49360 then--for closing dialogue
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})
					end
					emu.frameadvance()--keep waiting until we get into the fight
				end
				
				while not((memory.readbyte(addr_writing) == 0) and (memory.readbyte(addr_menuing) == 1)) do
					if (memory.readbyte(addr_writing) == 0) and (
					(memory.readbyte(addr_enemy_ko) == 20) or 
					(memory.readbyte(addr_enemy_ko) == 22) or 
					(memory.readbyte(addr_enemy_ko) == 104) or
					(memory.readbyte(addr_enemy_ko) == 106) or 
					(memory.readbyte(addr_enemy_ko) == 110) or 
					(memory.readbyte(addr_enemy_ko) == 134) or 
					(memory.readbyte(addr_enemy_ko) == 136) or 
					(memory.readbyte(addr_enemy_ko) == 138) or
					(memory.readbyte(addr_fightdialog) == 236)) then--in case of enemy ko or level up
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})--let's input for two frames, just in case...
					end
					emu.frameadvance()--keep waiting until we get to the main choice menu
				end
				joypad.set(1, {A=true})--enter the move choice menu
				emu.frameadvance()
				joypad.set(1, {A=true})--let's input for two frames, just in case...
				for i=1, 20 do
					emu.frameadvance()--we wait 20 frames before messing up with the ram watching
				end
				while (memory.readbyte(addr_writing) ~= 0) do
					emu.frameadvance()--keep waiting until we get to the move choice menu
				end
				
				local active_index = memory.readbyte(addr_party_start +memory.readbyte(addr_current_poke))
				
				local min_pps = 1
				if action[current_action][alt]["min_pps"] ~= nil then
					min_pps = action[current_action][alt]["min_pps"]
				end
				
				if action[current_action][alt]["move"] ~= nil then--below replaces the index move value with a mockup. a bit hacky, but functional
					if action[current_action][alt]["move"] == memory.readbyte(addr_moves_start +active_index) then
						action[current_action][alt][1] = 1
					elseif action[current_action][alt]["move"] == memory.readbyte(addr_moves_start +active_index +6) then
						action[current_action][alt][1] = 2
					elseif action[current_action][alt]["move"] == memory.readbyte(addr_moves_start +active_index +12) then
						action[current_action][alt][1] = 3
					elseif action[current_action][alt]["move"] == memory.readbyte(addr_moves_start +active_index +18) then
						action[current_action][alt][1] = 4
					else
						emu.print("ERROR: Specified move " .. action[current_action][alt]["move"] .. " not found for party member #" .. active_index .. ". It may ruin this botting session, but the script won't crash for this.")
						action[current_action][alt][1] = 1
						min_pps = 99 --this will abort this whole attempt.
					end
				end
				
				--check if there are enough PPs, otherwise just skip looking from this savestate althogether
				if memory.readbyte(addr_pps_start +active_index +( (action[current_action][alt][1] -1) *6) ) >= min_pps then
					if action[current_action][alt][1] == 2 then--this code block moves the cursor to the chosen move
						joypad.set(1, {right=true})
						emu.frameadvance()
						joypad.set(1, {right=true})
						while (memory.readbyte(addr_writing) ~= 0) do
							emu.frameadvance()
						end
					elseif action[current_action][alt][1] == 3 then
						joypad.set(1, {down=true})
						emu.frameadvance()
						joypad.set(1, {down=true})
						while (memory.readbyte(addr_writing) ~= 0) do
							emu.frameadvance()
						end
					elseif action[current_action][alt][1] == 4 then
						joypad.set(1, {down=true})
						emu.frameadvance()
						joypad.set(1, {down=true})
						while (memory.readbyte(addr_writing) ~= 0) do
							emu.frameadvance()
						end
						joypad.set(1, {right=true})
						emu.frameadvance()
						joypad.set(1, {right=true})
						while (memory.readbyte(addr_writing) ~= 0) do
							emu.frameadvance()
						end
					elseif action[current_action][alt][1] ~= 1 then
						emu.print("WARNING: Bad action code given (" .. action[current_action][alt][1] ..") for action " .. current_action .. ", type: fight, strategy " .. alt .. ". It may ruin this botting session or crash script execution.")
					--	emu.pause()--temporary
					--	emu.frameadvance()--temporary
					end
					
					local max_wait = max_wait_global
					if action[current_action][alt]["custom_wait"] ~= nil then
						max_wait = action[current_action][alt]["custom_wait"]
					end
					
					--here starts the actual botting. we make a savestate for bruteforcing
					local temp_state = savestate.object()
					savestate.save(temp_state)
					
					for wait=0, max_wait do
						savestate.load(temp_state)
						
						for i=1, wait do--we wait for a given amount of frames
							emu.frameadvance()
						end
						joypad.set(1, {A=true})--pick the chosen move
						emu.frameadvance()
						joypad.set(1, {A=true})--just in case
						
						while not((memory.readbyte(addr_writing) == 0) and (memory.readbyte(addr_menuing) == 1)) do
							if (memory.readbyte(addr_player_hp +active_index) < action[current_action][alt][3]) or (memory.readbyte(addr_player_poison +active_index) == 1) or (memory.readbyte(addr_enemy_hp) == 0) or (memory.readbyte(addr_player_hp +active_index) == 0) then
								break
							end
							emu.frameadvance()--keep waiting until we get again to the main choice menu
						end
						
						local max_player_hp = 999
						if action[current_action][alt]["max_player_hp"] ~= nil then
							max_player_hp = action[current_action][alt]["max_player_hp"]
						end
						
						if ((memory.readbyte(addr_enemy_hp) <= action[current_action][alt][2]) and (memory.readbyte(addr_player_hp +active_index) >= action[current_action][alt][3]) and (memory.readbyte(addr_player_poison +active_index) == 0)) and
						((action[current_action][alt][3] ~= 0) or (memory.readbyte(addr_player_hp +active_index) == 0)) and
						(memory.readbyte(addr_player_hp +active_index) <= max_player_hp) then
							
							if (action[current_action][alt][3] == 0) and action[current_action][alt]["switch"] then--if got ko, send in another
								--todo: add check for full party wipeout
								while (memory.readbyte(addr_writing) ~= 0) or (
								(memory.readbyte(addr_enemy_ko) ~= 20) and 
								(memory.readbyte(addr_enemy_ko) ~= 22) and 
								(memory.readbyte(addr_enemy_ko) ~= 104) and
								(memory.readbyte(addr_enemy_ko) ~= 106) and 
								(memory.readbyte(addr_enemy_ko) ~= 110) and 
								(memory.readbyte(addr_enemy_ko) ~= 134) and 
								(memory.readbyte(addr_enemy_ko) ~= 136) and 
								(memory.readbyte(addr_enemy_ko) ~= 138) and
								(memory.readbyte(addr_fightdialog) ~= 236)) do
									emu.frameadvance()--keep waiting 
								end
								joypad.set(1, {B=true})
								emu.frameadvance()
								joypad.set(1, {B=true})
								emu.frameadvance()
								
								for i=1, 40 do
									emu.frameadvance()--we wait 40 frames before messing up with the ram watching
								end
								while (memory.readbyte(addr_writing) ~= 0) or (memory.readbyte(addr_partycursor2) ~= 135) do
									emu.frameadvance()
								end
								joypad.set(1, {down=true})
								emu.frameadvance()
								joypad.set(1, {down=true})
								emu.frameadvance()
								
								for i=1, 15 do
									emu.frameadvance()--we wait 15 frames before messing up with the ram watching
								end
								while (memory.readbyte(addr_writing) ~= 0) or (memory.readbyte(addr_partycursor2) ~= 135) do
									emu.frameadvance()
								end
								joypad.set(1, {A=true})
								emu.frameadvance()
								
								while memory.readbyte(addr_writing) ~= 0 do
									emu.frameadvance()
								end
								joypad.set(1, {down=true})
								emu.frameadvance()
								
								while memory.readbyte(addr_writing) ~= 0 do
									emu.frameadvance()
								end
								joypad.set(1, {A=true})
								emu.frameadvance()
								joypad.set(1, {A=true})
								emu.frameadvance()
							end
							
							local framecount = emu.framecount()
							local rng1 = memory.readbyte(addr_rng1)
							local rng2 = memory.readbyte(addr_rng2)
							local enemy_hp = memory.readbyte(addr_enemy_hp)
							local player_hp = memory.readbyte(addr_player_hp +active_index)
							
							--this code block prevents creating double saves
							local is_double = false
							for z = 1, #state[current_action] do
								if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
									for i = 1, #state[current_action][z] do
										if (state[current_action][z][i]["frame"] == framecount) and
										(state[current_action][z][i]["rng1"] == rng1) and
										(state[current_action][z][i]["rng2"] == rng2) and
										(state[current_action][z][i]["enemy_hp"] == enemy_hp) and
										(state[current_action][z][i]["player_hp"] == player_hp) then
											is_double = true
											z = #state[current_action] +1
											break
										end
									end
								end
							end
							if is_double == false then
								local max_states = max_states_global
								if action[current_action][alt]["custom_states"] ~= nil then
									max_states = action[current_action][alt]["custom_states"]
								end
								
								if state[current_action][alt][max_states] == nil then
									for i=1, max_states do
										if state[current_action][alt][i] == nil then
											state[current_action][alt][i] = {}
											state[current_action][alt][i]["slot"] = savestate.object()
											savestate.save(state[current_action][alt][i]["slot"])
											state[current_action][alt][i]["frame"] = framecount
											state[current_action][alt][i]["rng1"] = rng1
											state[current_action][alt][i]["rng2"] = rng2
											state[current_action][alt][i]["enemy_hp"] = enemy_hp
											state[current_action][alt][i]["player_hp"] = player_hp
											emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
											break
										end
									end
								else
									local maximum = state[current_action][alt][1]["frame"]
									local maximum_index = 1
									for i=2, max_states do
										if state[current_action][alt][i]["frame"] > maximum then
											maximum = state[current_action][alt][i]["frame"]
											maximum_index = i
										end
									end
									if (state[current_action][alt][maximum_index]["frame"] > framecount) then
										savestate.save(state[current_action][alt][maximum_index]["slot"])
										state[current_action][alt][maximum_index]["frame"] = framecount
										state[current_action][alt][maximum_index]["rng1"] = rng1
										state[current_action][alt][maximum_index]["rng2"] = rng2
										state[current_action][alt][maximum_index]["enemy_hp"] = enemy_hp
										state[current_action][alt][maximum_index]["player_hp"] = player_hp
										emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
									end
								end
							end
						end
					end
					temp_state = nil--dunno if it helps clearing memory
				else
					emu.print("This savestate didn't provide enough PPs for using move #" .. action[current_action][alt][1])
				end
			end
		end
		if state[current_action][alt][1] == nil then--check if this function call failed to progress the botting
			emu.print("ERROR: Failed to meet the fight requirements for this strategy. Try raising the max_wait_global and the max_states values, or using more reasonable requirements, or routing the botting session from an earlier point of the movie.")
		--	emu.pause()
		--	current_action = false--this should abort script execution. or not?
		end
	end
end

local function action_move()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: move, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				while memory.readbyte(addr_infight) ~= 0 do --in case we need to exit from a fight
					if (memory.readbyte(addr_writing) == 0) and (
					(memory.readbyte(addr_enemy_ko) == 20) or 
					(memory.readbyte(addr_enemy_ko) == 22) or 
					(memory.readbyte(addr_enemy_ko) == 104) or 
					(memory.readbyte(addr_enemy_ko) == 106) or
					(memory.readbyte(addr_enemy_ko) == 110) or 
					(memory.readbyte(addr_enemy_ko) == 134) or 
					(memory.readbyte(addr_enemy_ko) == 136) or 
					(memory.readbyte(addr_enemy_ko) == 138) or
					(memory.readbyte(addr_fightdialog) == 236)) then--in case of enemy ko or level up
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})
					end
					emu.frameadvance()--keep waiting until we get out of the fight
				end
				
				local trainer_found_2 = false
				
				while memory.readbyte(addr_canmove) ~= 1 do
					if (memory.readword(addr_overwdialog) == 49360) or (memory.readword(addr_overwdialog) == 16592) then--for closing dialogue
						if action[current_action][alt]["no_fight"] then
							trainer_found_2 = true
							break
						end
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})
					elseif memory.readword(addr_overwdialog) == 47160 then--for closing dialogue
						joypad.set(1, {A=true})
						emu.frameadvance()
						joypad.set(1, {A=true})
					end
					emu.frameadvance()
				end
				
				if trainer_found_2 then
					break
				end
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					local trainer_found = false
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					
					for i=1, #action[current_action][alt], 2 do
						for x=1, action[current_action][alt][i+1] do
							while true do
								if memory.readbyte(addr_canmove) == 1 then
									if action[current_action][alt][i] == up then
										joypad.set(1, {up=true})
										emu.frameadvance()
										joypad.set(1, {up=true})
									elseif action[current_action][alt][i] == right then
										joypad.set(1, {right=true})
										emu.frameadvance()
										joypad.set(1, {right=true})
									elseif action[current_action][alt][i] == down then
										joypad.set(1, {down=true})
										emu.frameadvance()
										joypad.set(1, {down=true})
									elseif action[current_action][alt][i] == left then
										joypad.set(1, {left=true})
										emu.frameadvance()
										joypad.set(1, {left=true})
									elseif action[current_action][alt][i] == talk then
										joypad.set(1, {A=true})
										emu.frameadvance()
										joypad.set(1, {A=true})
										emu.frameadvance()
										while (memory.readbyte(addr_canmove) ~= 1) and (memory.readbyte(addr_infight) ~= 1) do
											if memory.readword(addr_overwdialog) == 49360 then--for closing dialogue
												joypad.set(1, {B=true})
												emu.frameadvance()
												joypad.set(1, {B=true})
											end
											emu.frameadvance()
										end
										break
									else
										emu.print("WARNING: Bad action code given (" .. action[current_action][alt][i] ..") for action " .. current_action .. ", type: move" .. ", movement " .. i .. ", strategy " .. alt .. ". It may ruin this botting session or crash script execution.")
									end
									emu.frameadvance()
									break
								elseif memory.readword(addr_overwdialog) == 49360 then--we got found by a trainer, abort this cicle attempt
									x = 1000
									i = 1000
									trainer_found = true
									break
								end
								emu.frameadvance()
							end
						end
					end
					
					local max_encounter_steps = 15
					if action[current_action][alt]["max_encounter_steps"] ~= nil then
						max_encounter_steps = action[current_action][alt]["max_encounter_steps"]
					end
					
					local min_encounter_steps = 10
					if action[current_action][alt]["min_encounter_steps"] ~= nil then
						min_encounter_steps = action[current_action][alt]["min_encounter_steps"]
					end

					if (trainer_found == false) and
					((action[current_action][alt]["max_encounter_steps"] == nil) or (memory.readbyte(addr_stepsleft) <= max_encounter_steps)) and
					((action[current_action][alt]["min_encounter_steps"] == nil) or (memory.readbyte(addr_stepsleft) >= min_encounter_steps)) and
					((action[current_action][alt]["encounter_steps"] == nil) or (memory.readbyte(addr_stepsleft) == action[current_action][alt]["encounter_steps"])) then
						local framecount = emu.framecount()
						local rng1 = memory.readbyte(addr_rng1)
						local rng2 = memory.readbyte(addr_rng2)
						
						--this code block prevents creating double saves
						local is_double = false
						for z = 1, #state[current_action] do
							if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
								for i = 1, #state[current_action][z] do
									if (state[current_action][z][i]["frame"] == framecount) and
									(state[current_action][z][i]["rng1"] == rng1) and
									(state[current_action][z][i]["rng2"] == rng2) then
										is_double = true
										z = #state[current_action] +1
										break
									end
								end
							end
						end
						if is_double == false then
							local max_states = max_states_global
							if action[current_action][alt]["custom_states"] ~= nil then
								max_states = action[current_action][alt]["custom_states"]
							end
							
							if state[current_action][alt][max_states] == nil then
								for i=1, max_states do
									if state[current_action][alt][i] == nil then
										state[current_action][alt][i] = {}
										state[current_action][alt][i]["slot"] = savestate.object()
										savestate.save(state[current_action][alt][i]["slot"])
										state[current_action][alt][i]["frame"] = framecount
										state[current_action][alt][i]["rng1"] = rng1
										state[current_action][alt][i]["rng2"] = rng2
										emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
										break
									end
								end
							else
								local maximum = state[current_action][alt][1]["frame"]
								local maximum_index = 1
								for i=2, max_states do
									if state[current_action][alt][i]["frame"] > maximum then
										maximum = state[current_action][alt][i]["frame"]
										maximum_index = i
									end
								end
								if (state[current_action][alt][maximum_index]["frame"] > framecount) then
									savestate.save(state[current_action][alt][maximum_index]["slot"])
									state[current_action][alt][maximum_index]["frame"] = framecount
									state[current_action][alt][maximum_index]["rng1"] = rng1
									state[current_action][alt][maximum_index]["rng2"] = rng2
									emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
								end
							end
						end
					end
				end
				temp_state = nil--dunno if it helps clearing memory
			end
		end
		if state[current_action][alt][1] == nil then--check if this function call failed to progress the botting
			emu.print("ERROR: Failed to meet the fight requirements for this strategy. Try raising the max_wait_global and the max_states values, or using more reasonable requirements, or routing the botting session from an earlier point of the movie.")
		--	emu.pause()
		--	current_action = false--this should abort script execution. or not?
		end
	end
end

local function action_fight_item()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: fight_item, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end

				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				while memory.readbyte(addr_infight) ~= 1 do--if we're not into the fight yet
					if memory.readword(addr_overwdialog) == 49360 then--for closing dialogue
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})
					end
					emu.frameadvance()--keep waiting until we get into the fight
				end
				
				while not((memory.readbyte(addr_writing) == 0) and (memory.readbyte(addr_menuing) == 1)) do
					if (memory.readbyte(addr_writing) == 0) and (
					(memory.readbyte(addr_enemy_ko) == 20) or 
					(memory.readbyte(addr_enemy_ko) == 22) or 
					(memory.readbyte(addr_enemy_ko) == 104) or
					(memory.readbyte(addr_enemy_ko) == 106) or 
					(memory.readbyte(addr_enemy_ko) == 110) or 
					(memory.readbyte(addr_enemy_ko) == 134) or 
					(memory.readbyte(addr_enemy_ko) == 136) or 
					(memory.readbyte(addr_enemy_ko) == 138) or
					(memory.readbyte(addr_fightdialog) == 236)) then--in case of enemy ko or level up
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})--let's input for two frames, just in case...
					end
					emu.frameadvance()--keep waiting until we get to the main choice menu
				end
				joypad.set(1, {right=true})--move the cursor in choice menu
				emu.frameadvance()
				joypad.set(1, {right=true})
				emu.frameadvance()
				joypad.set(1, {A=true})--enter the item choice menu
				emu.frameadvance()
				joypad.set(1, {A=true})
				
				while (memory.readbyte(addr_itemcursor) ~= 124) do
					emu.frameadvance()
				end
				for i = 2, action[current_action][alt][2] do
					joypad.set(1, {down=true})
					emu.frameadvance()
					joypad.set(1, {down=true})--let's input for two frames, just in case...
					while (memory.readbyte(addr_itemcursor) ~= 124) do
						emu.frameadvance()
					end
				end
				joypad.set(1, {A=true})--enter the move choice menu
				emu.frameadvance()
				joypad.set(1, {A=true})--let's input for two frames, just in case...
				for i=1, 40 do
					emu.frameadvance()--we wait 40 frames before messing up with the ram watching
				end
				while ((memory.readbyte(addr_partycursor) ~= 205) and (memory.readbyte(addr_partycursor) ~= 210)) do
					emu.frameadvance()
				end
				
				local active_index = memory.readbyte(addr_party_start +memory.readbyte(addr_current_poke))
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				--here starts the actual botting. we make a savestate for bruteforcing
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					joypad.set(1, {A=true})--pick the chosen party member
					emu.frameadvance()
					joypad.set(1, {A=true})
					
					while (not((memory.readbyte(addr_writing) == 0) and (memory.readbyte(addr_menuing) == 1))) or (memory.readbyte(addr_enemy_hp) <= 0) do
						if ((memory.readbyte(addr_player_hp +active_index) == 0) and (memory.readbyte(addr_player_poison +active_index) == 0)) then
							break
						end
						emu.frameadvance()--keep waiting until we get again to the main choice menu
					end
					
					if ((memory.readbyte(addr_player_hp +active_index) >= action[current_action][alt][3]) and (memory.readbyte(addr_player_poison +active_index) == 0)) then
						
						local framecount = emu.framecount()
						local rng1 = memory.readbyte(addr_rng1)
						local rng2 = memory.readbyte(addr_rng2)
						local enemy_hp = memory.readbyte(addr_enemy_hp)
						local player_hp = memory.readbyte(addr_player_hp +active_index)
						
						--this code block prevents creating double saves
						local is_double = false
						for z = 1, #state[current_action] do
							if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
								for i = 1, #state[current_action][z] do
									if (state[current_action][z][i]["frame"] == framecount) and
									(state[current_action][z][i]["rng1"] == rng1) and
									(state[current_action][z][i]["rng2"] == rng2) and
									(state[current_action][z][i]["enemy_hp"] == enemy_hp) and
									(state[current_action][z][i]["player_hp"] == player_hp) then
										is_double = true
										z = #state[current_action] +1
										break
									end
								end
							end
						end
						if is_double == false then
							local max_states = max_states_global
							if action[current_action][alt]["custom_states"] ~= nil then
								max_states = action[current_action][alt]["custom_states"]
							end
							
							if state[current_action][alt][max_states] == nil then
								for i=1, max_states do
									if state[current_action][alt][i] == nil then
										state[current_action][alt][i] = {}
										state[current_action][alt][i]["slot"] = savestate.object()
										savestate.save(state[current_action][alt][i]["slot"])
										state[current_action][alt][i]["frame"] = framecount
										state[current_action][alt][i]["rng1"] = rng1
										state[current_action][alt][i]["rng2"] = rng2
										state[current_action][alt][i]["enemy_hp"] = enemy_hp
										state[current_action][alt][i]["player_hp"] = player_hp
										emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
										break
									end
								end
							else
								local maximum = state[current_action][alt][1]["frame"]
								local maximum_index = 1
								for i=2, max_states do
									if state[current_action][alt][i]["frame"] > maximum then
										maximum = state[current_action][alt][i]["frame"]
										maximum_index = i
									end
								end
								if (state[current_action][alt][maximum_index]["frame"] > framecount) then
									savestate.save(state[current_action][alt][maximum_index]["slot"])
									state[current_action][alt][maximum_index]["frame"] = framecount
									state[current_action][alt][maximum_index]["rng1"] = rng1
									state[current_action][alt][maximum_index]["rng2"] = rng2
									state[current_action][alt][maximum_index]["enemy_hp"] = enemy_hp
									state[current_action][alt][maximum_index]["player_hp"] = player_hp
									emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
								end
							end
						end
					end
				end
				temp_state = nil--dunno if it helps clearing memory
			end
		end
	end
end

local function action_market_buy()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: market_buy, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				while memory.readbyte(addr_canmove) ~= 1 do
					emu.frameadvance()
				end
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					
					joypad.set(1, {A=true})
					emu.frameadvance()
					joypad.set(1, {A=true})
					emu.frameadvance()
					while memory.readword(addr_overwdialog) ~= 49360 do
						emu.frameadvance()
					end
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					while memory.readword(addr_overwdialog) ~= 47160 do
						emu.frameadvance()
					end
					joypad.set(1, {A=true})
					emu.frameadvance()
					joypad.set(1, {A=true})
					emu.frameadvance()
					while (memory.readbyte(addr_fightdialog) ~= 180) or (memory.readbyte(addr_writing) ~= 0) do
						emu.frameadvance()
					end
					
					for i=1, #action[current_action][alt] do
						for x=1, action[current_action][alt][i][1] do
							joypad.set(1, {right=true})
							emu.frameadvance()
							joypad.set(1, {right=true})
							while (memory.readbyte(addr_fightdialog) ~= 180) or (memory.readbyte(addr_writing) ~= 0) do
								emu.frameadvance()
							end
						end
						
						if action[current_action][alt][i][2] > 0 then
							for x=1, action[current_action][alt][i][2] do
								joypad.set(1, {down=true})
								emu.frameadvance()
								joypad.set(1, {down=true})
								while (memory.readbyte(addr_fightdialog) ~= 180) or (memory.readbyte(addr_writing) ~= 0) do
									emu.frameadvance()
								end
							end
						elseif action[current_action][alt][i][2] < 0 then
							for x=-1, action[current_action][alt][i][2], -1 do
								joypad.set(1, {up=true})
								emu.frameadvance()
								joypad.set(1, {up=true})
								while (memory.readbyte(addr_fightdialog) ~= 180) or (memory.readbyte(addr_writing) ~= 0) do
									emu.frameadvance()
								end
							end
						end
						
						joypad.set(1, {A=true})
						emu.frameadvance()
						joypad.set(1, {A=true})
						while memory.readbyte(addr_writing) ~= 0 do
							emu.frameadvance()
						end
						
						for x=2, action[current_action][alt][i][3] do
							joypad.set(1, {up=true})
							emu.frameadvance()
							while memory.readbyte(addr_writing) ~= 0 do
								emu.frameadvance()
							end
						end
						
						joypad.set(1, {A=true})
						emu.frameadvance()
						joypad.set(1, {A=true})
						emu.frameadvance()
						while (memory.readbyte(addr_fightdialog) ~= 180) or (memory.readbyte(addr_writing) ~= 0) do
							emu.frameadvance()
						end
					end
					
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					while memory.readword(addr_overwdialog) ~= 49360 do
						emu.frameadvance()
					end
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					
					local framecount = emu.framecount()
					local rng1 = memory.readbyte(addr_rng1)
					local rng2 = memory.readbyte(addr_rng2)
					
					--this code block prevents creating double saves
					local is_double = false
					for z = 1, #state[current_action] do
						if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
							for i = 1, #state[current_action][z] do
								if (state[current_action][z][i]["frame"] == framecount) and
								(state[current_action][z][i]["rng1"] == rng1) and
								(state[current_action][z][i]["rng2"] == rng2) then
									is_double = true
									z = #state[current_action] +1
									break
								end
							end
						end
					end
					if is_double == false then
						local max_states = max_states_global
						if action[current_action][alt]["custom_states"] ~= nil then
							max_states = action[current_action][alt]["custom_states"]
						end
						
						if state[current_action][alt][max_states] == nil then
							for i=1, max_states do
								if state[current_action][alt][i] == nil then
									state[current_action][alt][i] = {}
									state[current_action][alt][i]["slot"] = savestate.object()
									savestate.save(state[current_action][alt][i]["slot"])
									state[current_action][alt][i]["frame"] = framecount
									state[current_action][alt][i]["rng1"] = rng1
									state[current_action][alt][i]["rng2"] = rng2
									emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
									break
								end
							end
						else
							local maximum = state[current_action][alt][1]["frame"]
							local maximum_index = 1
							for i=2, max_states do
								if state[current_action][alt][i]["frame"] > maximum then
									maximum = state[current_action][alt][i]["frame"]
									maximum_index = i
								end
							end
							if (state[current_action][alt][maximum_index]["frame"] > framecount) then
								savestate.save(state[current_action][alt][maximum_index]["slot"])
								state[current_action][alt][maximum_index]["frame"] = framecount
								state[current_action][alt][maximum_index]["rng1"] = rng1
								state[current_action][alt][maximum_index]["rng2"] = rng2
								emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
							end
						end
					end
				end
				temp_state = nil--dunno if it helps clearing memory
			end
		end
	end
end

local function action_catch()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: catch, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				while memory.readbyte(addr_canmove) ~= 1 do
					emu.frameadvance()
				end
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				--here starts the actual botting. we make a savestate for bruteforcing
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					
					local trainer_found = false
					
					if action[current_action][alt]["direction"] == up then
						joypad.set(1, {up=true})
						emu.frameadvance()
						joypad.set(1, {up=true})
					elseif action[current_action][alt]["direction"] == right then
						joypad.set(1, {right=true})
						emu.frameadvance()
						joypad.set(1, {right=true})
					elseif action[current_action][alt]["direction"] == down then
						joypad.set(1, {down=true})
						emu.frameadvance()
						joypad.set(1, {down=true})
					elseif action[current_action][alt]["direction"] == left then
						joypad.set(1, {left=true})
						emu.frameadvance()
						joypad.set(1, {left=true})
					else
						emu.print("WARNING: Bad direction code given (" .. action[current_action][alt]["direction"] ..") for action " .. current_action .. ", type: catch" .. ", strategy " .. alt .. ". It may ruin this botting session or crash script execution.")
					end
					
					local check_freeze = 0
					while memory.readbyte(addr_canmove) ~= 24 do
						if memory.readword(addr_overwdialog) == 49360 then--we got found by a trainer, abort this cicle attempt
							trainer_found = true
							break
						end
						check_freeze = check_freeze +1
						if check_freeze > 200 then
							emu.print("ERROR: Wild Pokemon encounter didn't happen where expected. You either made a routing mistake with the encounter steps, or the game did unexpectedly freeze.")
							emu.pause()--temporary
							--break
						end
						emu.frameadvance()
					end
					emu.frameadvance()--we wait two frames, just in case
					emu.frameadvance()
					
					if (trainer_found == false) and (memory.readbyte(addr_wild_species) == action[current_action][alt]["species"]) and
					(memory.readbyte(addr_wild_level) == action[current_action][alt]["level"]) then--it's the pokemon we need
						for i=0, 20 do--let's wait that the wild pokemon moves get loaded in RAM...
							emu.frameadvance()
						end
						if ((memory.readbyte(addr_wild_move1) == action[current_action][alt]["move1"][1]) or
						(memory.readbyte(addr_wild_move1) == action[current_action][alt]["move1"][2]) or
						(memory.readbyte(addr_wild_move1) == action[current_action][alt]["move1"][3]) or
						(action[current_action][alt]["move1"][1] == nil)) and
						((memory.readbyte(addr_wild_move2) == action[current_action][alt]["move2"][1]) or
						(memory.readbyte(addr_wild_move2) == action[current_action][alt]["move2"][2]) or
						(memory.readbyte(addr_wild_move2) == action[current_action][alt]["move2"][3]) or
						(action[current_action][alt]["move2"][1] == nil)) and
						((memory.readbyte(addr_wild_move3) == action[current_action][alt]["move3"][1]) or
						(memory.readbyte(addr_wild_move3) == action[current_action][alt]["move3"][2]) or
						(memory.readbyte(addr_wild_move3) == action[current_action][alt]["move3"][3]) or
						(action[current_action][alt]["move3"][1] == nil)) and
						((memory.readbyte(addr_wild_move4) == action[current_action][alt]["move4"][1]) or
						(memory.readbyte(addr_wild_move4) == action[current_action][alt]["move4"][2]) or
						(memory.readbyte(addr_wild_move4) == action[current_action][alt]["move4"][3]) or
						(action[current_action][alt]["move4"][1] == nil)) then--these are the moves that we need
							while memory.readbyte(addr_infight) ~= 1 do
								emu.frameadvance()--keep waiting until we get into the fight
							end
							while not((memory.readbyte(addr_writing) == 0) and (memory.readbyte(addr_menuing) == 1)) do
								if (memory.readbyte(addr_writing) == 0) and (
								(memory.readbyte(addr_enemy_ko) == 20) or 
								(memory.readbyte(addr_enemy_ko) == 22) or 
								(memory.readbyte(addr_enemy_ko) == 104) or
								(memory.readbyte(addr_enemy_ko) == 106) or 
								(memory.readbyte(addr_enemy_ko) == 110) or 
								(memory.readbyte(addr_enemy_ko) == 134) or 
								(memory.readbyte(addr_enemy_ko) == 136) or 
								(memory.readbyte(addr_enemy_ko) == 138) or
								(memory.readbyte(addr_fightdialog) == 236)) then--in case of enemy ko or level up
									joypad.set(1, {B=true})
									emu.frameadvance()
									joypad.set(1, {B=true})--let's input for two frames, just in case...
								end
								emu.frameadvance()--keep waiting until we get to the main choice menu
							end
							joypad.set(1, {right=true})--move the cursor in choice menu
							emu.frameadvance()
							joypad.set(1, {right=true})
							emu.frameadvance()
							joypad.set(1, {A=true})--enter the item choice menu
							emu.frameadvance()
							joypad.set(1, {A=true})
							while (memory.readbyte(addr_itemcursor) ~= 124) do
								emu.frameadvance()--wait until we can pick the item from the list
							end
							local temp_state_2 = savestate.object()
							savestate.save(temp_state_2)
							for wait_2=0, 255 do
								savestate.load(temp_state_2)
								for y=1, wait_2 do--we wait for a given amount of frames
									emu.frameadvance()
								end
								joypad.set(1, {A=true})--select the first item available, hopefully a ball if you routed correctly
								emu.frameadvance()
								joypad.set(1, {A=true})
								emu.frameadvance()
								while (memory.readbyte(addr_fightdialog) ~= 30) and (memory.readbyte(addr_fightdialog) ~= 76) do
									emu.frameadvance()
								end
								if memory.readbyte(addr_fightdialog) == 76 then
									--[[while memory.readbyte(addr_fightdialog) ~= 236 do--we're already doing this at the beginning of other functions
										emu.frameadvance()
									end]]
									local framecount = emu.framecount()
									local rng1 = memory.readbyte(addr_rng1)
									local rng2 = memory.readbyte(addr_rng2)
									
									--this code block prevents creating double saves
									local is_double = false
									for z = 1, #state[current_action] do
										if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
											for i = 1, #state[current_action][z] do
												if (state[current_action][z][i]["frame"] == framecount) and
												(state[current_action][z][i]["rng1"] == rng1) and
												(state[current_action][z][i]["rng2"] == rng2) then
													is_double = true
													z = #state[current_action] +1
													break
												end
											end
										end
									end
									if is_double == false then
										local max_states = max_states_global
										if action[current_action][alt]["custom_states"] ~= nil then
											max_states = action[current_action][alt]["custom_states"]
										end
										
										if state[current_action][alt][max_states] == nil then
											for i=1, max_states do
												if state[current_action][alt][i] == nil then
													state[current_action][alt][i] = {}
													state[current_action][alt][i]["slot"] = savestate.object()
													savestate.save(state[current_action][alt][i]["slot"])
													state[current_action][alt][i]["frame"] = framecount
													state[current_action][alt][i]["rng1"] = rng1
													state[current_action][alt][i]["rng2"] = rng2
													emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
													break
												end
											end
											break
										else
											local maximum = state[current_action][alt][1]["frame"]
											local maximum_index = 1
											for i=2, max_states do
												if state[current_action][alt][i]["frame"] > maximum then
													maximum = state[current_action][alt][i]["frame"]
													maximum_index = i
												end
											end
											if (state[current_action][alt][maximum_index]["frame"] > framecount) then
												savestate.save(state[current_action][alt][maximum_index]["slot"])
												state[current_action][alt][maximum_index]["frame"] = framecount
												state[current_action][alt][maximum_index]["rng1"] = rng1
												state[current_action][alt][maximum_index]["rng2"] = rng2
												emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
												break
											end
										end
									end
								elseif (wait_2==255) and (state[current_action][alt][1] == nil) then--it's a bit unlikely that all 256 RNG values give a bad outcome, but not impossible I guess...
									emu.print("ERROR: Failed to catch this pokemon. If this is a very low-catch species, like a legendary, you probably need to manipulate luck more or to use a stronger ball.")
									break--we don't need to try to throw the ball again, for this RNG iteration.
								end
							end
							temp_state_2 = nil--dunno if it helps clearing the memory
						end
					end
				end
				temp_state = nil--dunno if it helps clearing memory
			end
		end
		if state[current_action][alt][1] == nil then--check if this function call failed to progress the botting
			emu.print("ERROR: Failed to find the desired pokemon. Try raising the max_wait_global and the max_states values, and make sure that the desired pokemon does actually exist.")
		--	emu.pause()
		--	current_action = false--this should abort script execution. or not?
		end
	end
end

local function action_fight_exit()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: fight_exit, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				if memory.readbyte(addr_trainerfight) == 0 then
					while (memory.readbyte(addr_enemy_ko) ~= 16) and (memory.readbyte(addr_enemy_ko) ~= 18) do
						if (memory.readbyte(addr_writing) == 0) and (
						(memory.readbyte(addr_enemy_ko) == 20) or 
						(memory.readbyte(addr_enemy_ko) == 22) or 
						(memory.readbyte(addr_enemy_ko) == 104) or
						(memory.readbyte(addr_enemy_ko) == 106) or 
						(memory.readbyte(addr_enemy_ko) == 110) or 
						(memory.readbyte(addr_enemy_ko) == 134) or 
						(memory.readbyte(addr_enemy_ko) == 136) or 
						(memory.readbyte(addr_enemy_ko) == 138) or
						(memory.readbyte(addr_fightdialog) == 236)) then--in case of enemy ko or level up
							joypad.set(1, {B=true})
							emu.frameadvance()
							joypad.set(1, {B=true})--let's input for two frames, just in case...
						end
						emu.frameadvance()
					end
				else
					if memory.readbyte(addr_enemy_hp) == 0 then
						while (memory.readbyte(addr_enemy_ko) ~= 104) and
						(memory.readbyte(addr_enemy_ko) ~= 106) and
						(memory.readbyte(addr_enemy_ko) ~= 110) do
							emu.frameadvance()
						end
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})--let's input for two frames, just in case...
						while (memory.readbyte(addr_enemy_ko) ~= 20) and (memory.readbyte(addr_enemy_ko) ~= 22) do
							emu.frameadvance()
						end
					else
						while (memory.readbyte(addr_writing) ~= 0) or (memory.readbyte(addr_fightdialog) ~= 236) do
							emu.frameadvance()--keep waiting until we get to the exit point
						end
					end
				end
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				--here starts the actual botting. we make a savestate for bruteforcing
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					joypad.set(1, {B=true})--exit from this fight.
					emu.frameadvance()
					joypad.set(1, {B=true})--just in case
					emu.frameadvance()
					
					--[[while memory.readbyte(addr_canmove) ~= 1 do--this isn't really necessary, isn't it?
						emu.frameadvance()--keep waiting until we get back to the overworld
					end]]
					
					local framecount = emu.framecount()
					local rng1 = memory.readbyte(addr_rng1)
					local rng2 = memory.readbyte(addr_rng2)
					
					--this code block prevents creating double saves
					local is_double = false
					for z = 1, #state[current_action] do
						if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
							for i = 1, #state[current_action][z] do
								if (state[current_action][z][i]["frame"] == framecount) and
								(state[current_action][z][i]["rng1"] == rng1) and
								(state[current_action][z][i]["rng2"] == rng2) then
									is_double = true
									z = #state[current_action] +1
									break
								end
							end
						end
					end
					if is_double == false then
						local max_states = max_states_global
						if action[current_action][alt]["custom_states"] ~= nil then
							max_states = action[current_action][alt]["custom_states"]
						end
						
						if state[current_action][alt][max_states] == nil then
							for i=1, max_states do
								if state[current_action][alt][i] == nil then
									state[current_action][alt][i] = {}
									state[current_action][alt][i]["slot"] = savestate.object()
									savestate.save(state[current_action][alt][i]["slot"])
									state[current_action][alt][i]["frame"] = framecount
									state[current_action][alt][i]["rng1"] = rng1
									state[current_action][alt][i]["rng2"] = rng2
									emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
									break
								end
							end
						else
							local maximum = state[current_action][alt][1]["frame"]
							local maximum_index = 1
							for i=2, max_states do
								if state[current_action][alt][i]["frame"] > maximum then
									maximum = state[current_action][alt][i]["frame"]
									maximum_index = i
								end
							end
							if (state[current_action][alt][maximum_index]["frame"] > framecount) then
								savestate.save(state[current_action][alt][maximum_index]["slot"])
								state[current_action][alt][maximum_index]["frame"] = framecount
								state[current_action][alt][maximum_index]["rng1"] = rng1
								state[current_action][alt][maximum_index]["rng2"] = rng2
								emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
							end
						end
					end
				end
			end
		end
	end
end

local function action_fight_run()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: fight_run, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				local trainer_found = false
				
				local check_freeze = 0
				while memory.readbyte(addr_canmove) ~= 24 do
					if memory.readword(addr_overwdialog) == 49360 then--we got found by a trainer, abort this cicle attempt
						trainer_found = true
						break
					end
					check_freeze = check_freeze +1
					if check_freeze > 200 then
						emu.print("ERROR: Wild Pokemon encounter didn't happen where expected. You either made a routing mistake with the encounter steps, or the game did unexpectedly freeze.")
						emu.pause()--temporary
						--break
					end
					emu.frameadvance()
				end
				emu.frameadvance()--we wait two frames, just in case
				emu.frameadvance()
				
				if trainer_found then
					break
				end
				
				while memory.readbyte(addr_infight) ~= 1 do
					emu.frameadvance()--keep waiting until we get into the fight
				end
				
				while not((memory.readbyte(addr_writing) == 0) and (memory.readbyte(addr_menuing) == 1)) do
					if (memory.readbyte(addr_writing) == 0) and (
					(memory.readbyte(addr_enemy_ko) == 20) or 
					(memory.readbyte(addr_enemy_ko) == 22) or 
					(memory.readbyte(addr_enemy_ko) == 104) or
					(memory.readbyte(addr_enemy_ko) == 106) or 
					(memory.readbyte(addr_enemy_ko) == 110) or 
					(memory.readbyte(addr_enemy_ko) == 134) or 
					(memory.readbyte(addr_enemy_ko) == 136) or 
					(memory.readbyte(addr_enemy_ko) == 138) or
					(memory.readbyte(addr_fightdialog) == 236)) then--in case of enemy ko or level up
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})--let's input for two frames, just in case...
					end
					emu.frameadvance()--keep waiting until we get to the main choice menu
				end
				joypad.set(1, {right=true})
				emu.frameadvance()
				joypad.set(1, {right=true})
				emu.frameadvance()
				joypad.set(1, {down=true})
				emu.frameadvance()
				joypad.set(1, {down=true})
				emu.frameadvance()
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				--here starts the actual botting. we make a savestate for bruteforcing
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					joypad.set(1, {A=true})--exit from this fight.
					emu.frameadvance()
					joypad.set(1, {A=true})--just in case
					emu.frameadvance()
					
					--[[while memory.readbyte(addr_canmove) ~= 1 do--this isn't really necessary, isn't it?
						emu.frameadvance()--keep waiting until we get back to the overworld
					end]]
					
					local framecount = emu.framecount()
					local rng1 = memory.readbyte(addr_rng1)
					local rng2 = memory.readbyte(addr_rng2)
					
					--this code block prevents creating double saves
					local is_double = false
					for z = 1, #state[current_action] do
						if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
							for i = 1, #state[current_action][z] do
								if (state[current_action][z][i]["frame"] == framecount) and
								(state[current_action][z][i]["rng1"] == rng1) and
								(state[current_action][z][i]["rng2"] == rng2) then
									is_double = true
									z = #state[current_action] +1
									break
								end
							end
						end
					end
					if is_double == false then
						local max_states = max_states_global
						if action[current_action][alt]["custom_states"] ~= nil then
							max_states = action[current_action][alt]["custom_states"]
						end
						
						if state[current_action][alt][max_states] == nil then
							for i=1, max_states do
								if state[current_action][alt][i] == nil then
									state[current_action][alt][i] = {}
									state[current_action][alt][i]["slot"] = savestate.object()
									savestate.save(state[current_action][alt][i]["slot"])
									state[current_action][alt][i]["frame"] = framecount
									state[current_action][alt][i]["rng1"] = rng1
									state[current_action][alt][i]["rng2"] = rng2
									emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
									break
								end
							end
						else
							local maximum = state[current_action][alt][1]["frame"]
							local maximum_index = 1
							for i=2, max_states do
								if state[current_action][alt][i]["frame"] > maximum then
									maximum = state[current_action][alt][i]["frame"]
									maximum_index = i
								end
							end
							if (state[current_action][alt][maximum_index]["frame"] > framecount) then
								savestate.save(state[current_action][alt][maximum_index]["slot"])
								state[current_action][alt][maximum_index]["frame"] = framecount
								state[current_action][alt][maximum_index]["rng1"] = rng1
								state[current_action][alt][maximum_index]["rng2"] = rng2
								emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
							end
						end
					end
				end
			end
		end
	end
end

local function action_party_swap()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: party_swap, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				while memory.readbyte(addr_infight) ~= 0 do --in case we need to exit from a fight
					if (memory.readbyte(addr_writing) == 0) and (
					(memory.readbyte(addr_enemy_ko) == 20) or 
					(memory.readbyte(addr_enemy_ko) == 22) or 
					(memory.readbyte(addr_enemy_ko) == 104) or 
					(memory.readbyte(addr_enemy_ko) == 106) or
					(memory.readbyte(addr_enemy_ko) == 110) or 
					(memory.readbyte(addr_enemy_ko) == 134) or 
					(memory.readbyte(addr_enemy_ko) == 136) or 
					(memory.readbyte(addr_enemy_ko) == 138) or
					(memory.readbyte(addr_fightdialog) == 236)) then--in case of enemy ko or level up
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})
					end
					emu.frameadvance()--keep waiting until we get out of the fight
				end
				
				while memory.readbyte(addr_canmove) ~= 1 do
					if (memory.readword(addr_overwdialog) == 49360) or (memory.readword(addr_overwdialog) == 16592) then--for closing dialogue
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})
					end
					emu.frameadvance()
				end
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				--here starts the actual botting. we make a savestate for bruteforcing
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					
					joypad.set(1, {start=true})
					emu.frameadvance()
					joypad.set(1, {start=true})
					
					while memory.readbyte(addr_writing) ~= 0 do
						emu.frameadvance()
					end
					
					joypad.set(1, {down=true})
					emu.frameadvance()
					
					while memory.readbyte(addr_writing) ~= 0 do
						emu.frameadvance()
					end
					
					joypad.set(1, {A=true})
					emu.frameadvance()
					joypad.set(1, {A=true})
					
					while memory.readbyte(addr_fightdialog) ~= 243 do
						emu.frameadvance()
					end
					
					if action[current_action][alt][1] > 0 then
						for x=1, action[current_action][alt][1] do
							joypad.set(1, {down=true})
							emu.frameadvance()
							joypad.set(1, {down=true})
							while memory.readbyte(addr_fightdialog) ~= 243 do
								emu.frameadvance()
							end
						end
					elseif action[current_action][alt][1] < 0 then
						for x=-1, action[current_action][alt][1], -1 do
							joypad.set(1, {up=true})
							emu.frameadvance()
							joypad.set(1, {up=true})
							while memory.readbyte(addr_fightdialog) ~= 243 do
								emu.frameadvance()
							end
						end
					end
					
					joypad.set(1, {A=true})
					emu.frameadvance()
					emu.frameadvance()
					
					joypad.set(1, {down=true})
					emu.frameadvance()
					emu.frameadvance()
					
					joypad.set(1, {A=true})
					emu.frameadvance()
					emu.frameadvance()
					
					while memory.readbyte(addr_partycursor2) ~= 194 and memory.readbyte(addr_partycursor2) ~= 205 do
						emu.frameadvance()
					end
					
					if action[current_action][alt][2] > 0 then
						for x=1, action[current_action][alt][2] do
							joypad.set(1, {down=true})
							emu.frameadvance()
							joypad.set(1, {down=true})
							while memory.readbyte(addr_partycursor2) ~= 194 and memory.readbyte(addr_partycursor2) ~= 205 do
								emu.frameadvance()
							end
						end
					elseif action[current_action][alt][2] < 0 then
						for x=-1, action[current_action][alt][2], -1 do
							joypad.set(1, {up=true})
							emu.frameadvance()
							joypad.set(1, {up=true})
							while memory.readbyte(addr_partycursor2) ~= 194 and memory.readbyte(addr_partycursor2) ~= 205 do
								emu.frameadvance()
							end
						end
					end
					
					joypad.set(1, {A=true})
					emu.frameadvance()
					joypad.set(1, {A=true})
					emu.frameadvance()
					
					while memory.readbyte(addr_fightdialog) ~= 243 do
						emu.frameadvance()
					end
					
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					
					for i=1, 40 do
						emu.frameadvance()
					end
					
					while memory.readbyte(addr_writing) ~= 0 do
						emu.frameadvance()
					end
					
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					
					local framecount = emu.framecount()
					local rng1 = memory.readbyte(addr_rng1)
					local rng2 = memory.readbyte(addr_rng2)
					
					--this code block prevents creating double saves
					local is_double = false
					for z = 1, #state[current_action] do
						if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
							for i = 1, #state[current_action][z] do
								if (state[current_action][z][i]["frame"] == framecount) and
								(state[current_action][z][i]["rng1"] == rng1) and
								(state[current_action][z][i]["rng2"] == rng2) then
									is_double = true
									z = #state[current_action] +1
									break
								end
							end
						end
					end
					if is_double == false then
						local max_states = max_states_global
						if action[current_action][alt]["custom_states"] ~= nil then
							max_states = action[current_action][alt]["custom_states"]
						end
						
						if state[current_action][alt][max_states] == nil then
							for i=1, max_states do
								if state[current_action][alt][i] == nil then
									state[current_action][alt][i] = {}
									state[current_action][alt][i]["slot"] = savestate.object()
									savestate.save(state[current_action][alt][i]["slot"])
									state[current_action][alt][i]["frame"] = framecount
									state[current_action][alt][i]["rng1"] = rng1
									state[current_action][alt][i]["rng2"] = rng2
									emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
									break
								end
							end
						else
							local maximum = state[current_action][alt][1]["frame"]
							local maximum_index = 1
							for i=2, max_states do
								if state[current_action][alt][i]["frame"] > maximum then
									maximum = state[current_action][alt][i]["frame"]
									maximum_index = i
								end
							end
							if (state[current_action][alt][maximum_index]["frame"] > framecount) then
								savestate.save(state[current_action][alt][maximum_index]["slot"])
								state[current_action][alt][maximum_index]["frame"] = framecount
								state[current_action][alt][maximum_index]["rng1"] = rng1
								state[current_action][alt][maximum_index]["rng2"] = rng2
								emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
							end
						end
					end
				end
			end
		end
	end
end

local function action_hunt()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: hunt, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				local trainer_found_2 = false
				
				while memory.readbyte(addr_canmove) ~= 1 do
					if (memory.readword(addr_overwdialog) == 49360) or (memory.readword(addr_overwdialog) == 16592) then--for closing dialogue
						if action[current_action][alt]["no_fight"] then
							trainer_found_2 = true
							break
						end
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})
					elseif memory.readword(addr_overwdialog) == 47160 then--for closing dialogue
						joypad.set(1, {A=true})
						emu.frameadvance()
						joypad.set(1, {A=true})
					end
					emu.frameadvance()
				end
				
				if trainer_found_2 then
					break
				end
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				--here starts the actual botting. we make a savestate for bruteforcing
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					
					local trainer_found = false
					
					if action[current_action][alt]["direction"] == up then
						joypad.set(1, {up=true})
						emu.frameadvance()
						joypad.set(1, {up=true})
					elseif action[current_action][alt]["direction"] == right then
						joypad.set(1, {right=true})
						emu.frameadvance()
						joypad.set(1, {right=true})
					elseif action[current_action][alt]["direction"] == down then
						joypad.set(1, {down=true})
						emu.frameadvance()
						joypad.set(1, {down=true})
					elseif action[current_action][alt]["direction"] == left then
						joypad.set(1, {left=true})
						emu.frameadvance()
						joypad.set(1, {left=true})
					else
						emu.print("WARNING: Bad direction code given (" .. action[current_action][alt]["direction"] ..") for action " .. current_action .. ", type: catch" .. ", strategy " .. alt .. ". It may ruin this botting session or crash script execution.")
					end
					
					local check_freeze = 0
					while memory.readbyte(addr_canmove) ~= 24 do
						if memory.readword(addr_overwdialog) == 49360 then--we got found by a trainer, abort this cicle attempt
							trainer_found = true
							break
						end
						check_freeze = check_freeze +1
						if check_freeze > 200 then
							emu.print("ERROR: Wild Pokemon encounter didn't happen where expected. You either made a routing mistake with the encounter steps, or the game did unexpectedly freeze.")
							emu.pause()--temporary
							--break
						end
						emu.frameadvance()
					end
					emu.frameadvance()--we wait two frames, just in case
					emu.frameadvance()
					
					if (trainer_found == false) and (memory.readbyte(addr_wild_species) == action[current_action][alt]["species"]) then
						while memory.readbyte(addr_infight) ~= 1 do
							emu.frameadvance()--keep waiting until we get into the fight
						end
						
						while not((memory.readbyte(addr_writing) == 0) and (memory.readbyte(addr_menuing) == 1)) do
							if (memory.readbyte(addr_writing) == 0) and (
							(memory.readbyte(addr_enemy_ko) == 20) or 
							(memory.readbyte(addr_enemy_ko) == 22) or 
							(memory.readbyte(addr_enemy_ko) == 104) or
							(memory.readbyte(addr_enemy_ko) == 106) or 
							(memory.readbyte(addr_enemy_ko) == 110) or 
							(memory.readbyte(addr_enemy_ko) == 134) or 
							(memory.readbyte(addr_enemy_ko) == 136) or 
							(memory.readbyte(addr_enemy_ko) == 138) or
							(memory.readbyte(addr_fightdialog) == 236)) then--in case of enemy ko or level up
								joypad.set(1, {B=true})
								emu.frameadvance()
								joypad.set(1, {B=true})--let's input for two frames, just in case...
							end
							emu.frameadvance()--keep waiting until we get to the main choice menu
						end
						joypad.set(1, {A=true})--enter the move choice menu
						emu.frameadvance()
						joypad.set(1, {A=true})--let's input for two frames, just in case...
						for i=1, 20 do
							emu.frameadvance()--we wait 20 frames before messing up with the ram watching
						end
						while (memory.readbyte(addr_writing) ~= 0) do
							emu.frameadvance()--keep waiting until we get to the move choice menu
						end
						
						local active_index = memory.readbyte(addr_party_start +memory.readbyte(addr_current_poke))
						
						local min_pps = 1
						if action[current_action][alt]["min_pps"] ~= nil then
							min_pps = action[current_action][alt]["min_pps"]
						end
						
						if action[current_action][alt]["move"] ~= nil then--below replaces the index move value with a mockup. a bit hacky, but functional
							if action[current_action][alt]["move"] == memory.readbyte(addr_moves_start +active_index) then
								action[current_action][alt][1] = 1
							elseif action[current_action][alt]["move"] == memory.readbyte(addr_moves_start +active_index +6) then
								action[current_action][alt][1] = 2
							elseif action[current_action][alt]["move"] == memory.readbyte(addr_moves_start +active_index +12) then
								action[current_action][alt][1] = 3
							elseif action[current_action][alt]["move"] == memory.readbyte(addr_moves_start +active_index +18) then
								action[current_action][alt][1] = 4
							else
								emu.print("ERROR: Specified move " .. action[current_action][alt]["move"] .. " not found for party member #" .. active_index .. ". It may ruin this botting session, but the script won't crash for this.")
								action[current_action][alt][1] = 1
								min_pps = 99 --this will abort this whole attempt.
							end
						end	
						
						if action[current_action][alt][1] == 2 then--this code block moves the cursor to the chosen move
							joypad.set(1, {right=true})
							emu.frameadvance()
							joypad.set(1, {right=true})
							while (memory.readbyte(addr_writing) ~= 0) do
								emu.frameadvance()
							end
						elseif action[current_action][alt][1] == 3 then
							joypad.set(1, {down=true})
							emu.frameadvance()
							joypad.set(1, {down=true})
							while (memory.readbyte(addr_writing) ~= 0) do
								emu.frameadvance()
							end
						elseif action[current_action][alt][1] == 4 then
							joypad.set(1, {down=true})
							emu.frameadvance()
							joypad.set(1, {down=true})
							while (memory.readbyte(addr_writing) ~= 0) do
								emu.frameadvance()
							end
							joypad.set(1, {right=true})
							emu.frameadvance()
							joypad.set(1, {right=true})
							while (memory.readbyte(addr_writing) ~= 0) do
								emu.frameadvance()
							end
						elseif action[current_action][alt][1] ~= 1 then
							emu.print("WARNING: Bad action code given (" .. action[current_action][alt][1] ..") for action " .. current_action .. ", type: fight, strategy " .. alt .. ". It may ruin this botting session or crash script execution.")
						--	emu.pause()--temporary
						--	emu.frameadvance()--temporary
						end
						
						local max_wait = max_wait_global
						if action[current_action][alt]["custom_wait"] ~= nil then
							max_wait = action[current_action][alt]["custom_wait"]
						end
						
						local temp_state_2 = savestate.object()
						savestate.save(temp_state_2)
						for wait_2=0, 255 do
							savestate.load(temp_state_2)
							for y=1, wait_2 do--we wait for a given amount of frames
								emu.frameadvance()
							end
							joypad.set(1, {A=true})--select the move
							emu.frameadvance()
							joypad.set(1, {A=true})
							
							while not((memory.readbyte(addr_writing) == 0) and (memory.readbyte(addr_menuing) == 1)) do
								if (memory.readbyte(addr_player_poison +active_index) == 1) or (memory.readbyte(addr_enemy_hp) == 0) or (memory.readbyte(addr_player_hp +active_index) == 0) then
									break
								end
								emu.frameadvance()--keep waiting until we get again to the main choice menu
							end
							
							local max_player_hp = 999
							if action[current_action][alt]["max_player_hp"] ~= nil then
								max_player_hp = action[current_action][alt]["max_player_hp"]
							end
							
							if memory.readbyte(addr_enemy_hp) == 0 then
								local framecount = emu.framecount()
								local rng1 = memory.readbyte(addr_rng1)
								local rng2 = memory.readbyte(addr_rng2)
								
								--this code block prevents creating double saves
								local is_double = false
								for z = 1, #state[current_action] do
									if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
										for i = 1, #state[current_action][z] do
											if (state[current_action][z][i]["frame"] == framecount) and
											(state[current_action][z][i]["rng1"] == rng1) and
											(state[current_action][z][i]["rng2"] == rng2) then
												is_double = true
												z = #state[current_action] +1
												break
											end
										end
									end
								end
								if is_double == false then
									local max_states = max_states_global
									if action[current_action][alt]["custom_states"] ~= nil then
										max_states = action[current_action][alt]["custom_states"]
									end
									
									if state[current_action][alt][max_states] == nil then
										for i=1, max_states do
											if state[current_action][alt][i] == nil then
												state[current_action][alt][i] = {}
												state[current_action][alt][i]["slot"] = savestate.object()
												savestate.save(state[current_action][alt][i]["slot"])
												state[current_action][alt][i]["frame"] = framecount
												state[current_action][alt][i]["rng1"] = rng1
												state[current_action][alt][i]["rng2"] = rng2
												emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
												break
											end
										end
										break
									else
										local maximum = state[current_action][alt][1]["frame"]
										local maximum_index = 1
										for i=2, max_states do
											if state[current_action][alt][i]["frame"] > maximum then
												maximum = state[current_action][alt][i]["frame"]
												maximum_index = i
											end
										end
										if (state[current_action][alt][maximum_index]["frame"] > framecount) then
											savestate.save(state[current_action][alt][maximum_index]["slot"])
											state[current_action][alt][maximum_index]["frame"] = framecount
											state[current_action][alt][maximum_index]["rng1"] = rng1
											state[current_action][alt][maximum_index]["rng2"] = rng2
											emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
										end
									end
								end
							elseif (wait_2==255) and (state[current_action][alt][1] == nil) then--it's a bit unlikely that all 256 RNG values give a bad outcome, but not impossible I guess...
								emu.print("ERROR: Failed to hunt this pokemon. Try to set more reasonable conditions.")
								break--we don't need to try to attack again, for this RNG iteration.
							end
						end
						temp_state_2 = nil--dunno if it helps clearing the memory
					end
				end
				temp_state = nil--dunno if it helps clearing memory
			end
		end
		if state[current_action][alt][1] == nil then--check if this function call failed to progress the botting
			emu.print("ERROR: Failed to find the desired pokemon. Try raising the max_wait_global and the max_states values, and make sure that the desired pokemon does actually exist.")
		--	emu.pause()
		--	current_action = false--this should abort script execution. or not?
		end
	end
end

local function action_pc()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: pc_deposit, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				while memory.readbyte(addr_canmove) ~= 1 do
					emu.frameadvance()
				end
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					
					joypad.set(1, {A=true})
					emu.frameadvance()
					joypad.set(1, {A=true})
					emu.frameadvance()
					
					while memory.readword(addr_overwdialog) ~= 49360 do
						emu.frameadvance()
					end
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					while memory.readword(addr_overwdialog) ~= 49360 do
						emu.frameadvance()
					end
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					
					while memory.readword(addr_overwdialog) ~= 47160 do
						emu.frameadvance()
					end
					joypad.set(1, {right=true})
					emu.frameadvance()
					joypad.set(1, {A=true})
					emu.frameadvance()
					joypad.set(1, {A=true})
					emu.frameadvance()
					
					while (memory.readbyte(addr_partycursor2) ~= 114) or (memory.readbyte(addr_writing) ~= 0) do
						emu.frameadvance()
					end
					
					for i=1, #action[current_action][alt] do
						if action[current_action][alt][i] > 0 then
							for x=1, action[current_action][alt][i] do
								joypad.set(1, {down=true})
								emu.frameadvance()
								joypad.set(1, {down=true})
								while (memory.readbyte(addr_partycursor2) ~= 35) or (memory.readbyte(addr_writing) ~= 0) do
									emu.frameadvance()
								end
							end
						elseif action[current_action][alt][i] < 0 then
							for x=-1, action[current_action][alt][i], -1 do
								joypad.set(1, {up=true})
								emu.frameadvance()
								joypad.set(1, {up=true})
								while (memory.readbyte(addr_partycursor2) ~= 35) or (memory.readbyte(addr_writing) ~= 0) do
									emu.frameadvance()
								end
							end
						end
						emu.print("asd")
						joypad.set(1, {A=true})
						emu.frameadvance()
						while memory.readbyte(addr_writing) ~= 0 do
							emu.frameadvance()
						end
						joypad.set(1, {down=true})
						emu.frameadvance()
						while memory.readbyte(addr_writing) ~= 0 do
							emu.frameadvance()
						end
						joypad.set(1, {A=true})
						emu.frameadvance()
						
						while memory.readbyte(addr_partycursor2) ~= 193 do
							emu.frameadvance()
						end
					end
					
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					while memory.readword(addr_overwdialog) ~= 49360 do
						emu.frameadvance()
					end
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					while memory.readword(addr_overwdialog) ~= 49360 do
						emu.frameadvance()
					end
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					
					local framecount = emu.framecount()
					local rng1 = memory.readbyte(addr_rng1)
					local rng2 = memory.readbyte(addr_rng2)
					
					--this code block prevents creating double saves
					local is_double = false
					for z = 1, #state[current_action] do
						if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
							for i = 1, #state[current_action][z] do
								if (state[current_action][z][i]["frame"] == framecount) and
								(state[current_action][z][i]["rng1"] == rng1) and
								(state[current_action][z][i]["rng2"] == rng2) then
									is_double = true
									z = #state[current_action] +1
									break
								end
							end
						end
					end
					if is_double == false then
						local max_states = max_states_global
						if action[current_action][alt]["custom_states"] ~= nil then
							max_states = action[current_action][alt]["custom_states"]
						end
						
						if state[current_action][alt][max_states] == nil then
							for i=1, max_states do
								if state[current_action][alt][i] == nil then
									state[current_action][alt][i] = {}
									state[current_action][alt][i]["slot"] = savestate.object()
									savestate.save(state[current_action][alt][i]["slot"])
									state[current_action][alt][i]["frame"] = framecount
									state[current_action][alt][i]["rng1"] = rng1
									state[current_action][alt][i]["rng2"] = rng2
									emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
									break
								end
							end
						else
							local maximum = state[current_action][alt][1]["frame"]
							local maximum_index = 1
							for i=2, max_states do
								if state[current_action][alt][i]["frame"] > maximum then
									maximum = state[current_action][alt][i]["frame"]
									maximum_index = i
								end
							end
							if (state[current_action][alt][maximum_index]["frame"] > framecount) then
								savestate.save(state[current_action][alt][maximum_index]["slot"])
								state[current_action][alt][maximum_index]["frame"] = framecount
								state[current_action][alt][maximum_index]["rng1"] = rng1
								state[current_action][alt][maximum_index]["rng2"] = rng2
								emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
							end
						end
					end
				end
				temp_state = nil--dunno if it helps clearing memory
			end
		end
	end
end

local function action_item()
	for alt = 1, #action[current_action] do
		emu.print("-Start of action " .. current_action .. ", type: item, strategy " .. alt)
		for alt_num = 1, #state[current_action-1] do
			for state_num = 1, #state[current_action-1][alt_num] do
				
				if (action[current_action-1][alt_num]["branch"] ~= nil) and (action[current_action][alt]["branch"] ~= nil) and
				(action[current_action-1][alt_num]["branch"] ~= action[current_action][alt]["branch"]) then
					break
				end
				
				emu.print("Looking from state " .. current_action-1 .. "-" .. alt_num .. "-" .. state_num)
				
				savestate.load(state[current_action-1][alt_num][state_num]["slot"])
				
				while memory.readbyte(addr_infight) ~= 0 do --in case we need to exit from a fight
					if (memory.readbyte(addr_writing) == 0) and (
					(memory.readbyte(addr_enemy_ko) == 20) or 
					(memory.readbyte(addr_enemy_ko) == 22) or 
					(memory.readbyte(addr_enemy_ko) == 104) or 
					(memory.readbyte(addr_enemy_ko) == 106) or
					(memory.readbyte(addr_enemy_ko) == 110) or 
					(memory.readbyte(addr_enemy_ko) == 134) or 
					(memory.readbyte(addr_enemy_ko) == 136) or 
					(memory.readbyte(addr_enemy_ko) == 138) or
					(memory.readbyte(addr_fightdialog) == 236)) then--in case of enemy ko or level up
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})
					end
					emu.frameadvance()--keep waiting until we get out of the fight
				end
				
				while memory.readbyte(addr_canmove) ~= 1 do
					if (memory.readword(addr_overwdialog) == 49360) or (memory.readword(addr_overwdialog) == 16592) then--for closing dialogue
						joypad.set(1, {B=true})
						emu.frameadvance()
						joypad.set(1, {B=true})
					end
					emu.frameadvance()
				end
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				local max_wait = max_wait_global
				if action[current_action][alt]["custom_wait"] ~= nil then
					max_wait = action[current_action][alt]["custom_wait"]
				end
				
				local temp_state = savestate.object()
				savestate.save(temp_state)
				
				for wait=0, max_wait do
					savestate.load(temp_state)
					
					for i=1, wait do--we wait for a given amount of frames
						emu.frameadvance()
					end
					
					joypad.set(1, {start=true})
					emu.frameadvance()
					joypad.set(1, {start=true})
					while memory.readbyte(addr_writing) ~= 0 do
						emu.frameadvance()
					end
					joypad.set(1, {down=true})
					emu.frameadvance()
					emu.frameadvance()
					joypad.set(1, {down=true})
					emu.frameadvance()
					while memory.readbyte(addr_writing) ~= 0 do
						emu.frameadvance()
					end
					joypad.set(1, {A=true})
					emu.frameadvance()
					joypad.set(1, {A=true})
					emu.frameadvance()
					
					while (memory.readbyte(addr_fightdialog) ~= 124) or (memory.readbyte(addr_writing) ~= 0) do
						emu.frameadvance()
					end
					
					for i=1, #action[current_action][alt] do
						if action[current_action][alt][i][1] > 0 then
							for x=1, action[current_action][alt][i][1] do
								joypad.set(1, {down=true})
								emu.frameadvance()
								joypad.set(1, {down=true})
								while (memory.readbyte(addr_fightdialog) ~= 124) or (memory.readbyte(addr_writing) ~= 0) do
									emu.frameadvance()
								end
							end
						elseif action[current_action][alt][i][1] < 0 then
							for x=-1, action[current_action][alt][i][1], -1 do
								joypad.set(1, {up=true})
								emu.frameadvance()
								joypad.set(1, {up=true})
								while (memory.readbyte(addr_fightdialog) ~= 124) or (memory.readbyte(addr_writing) ~= 0) do
									emu.frameadvance()
								end
							end
						end
						joypad.set(1, {A=true})
						emu.frameadvance()
						joypad.set(1, {A=true})
						
						for i=1, 50 do
							emu.frameadvance()
						end
						while (memory.readbyte(addr_partycursor2) ~= 35) or (memory.readbyte(addr_writing) ~= 0) do
							emu.frameadvance()
						end
						
						if action[current_action][alt][i][2] > 0 then
							for x=1, action[current_action][alt][i][2] do
								joypad.set(1, {down=true})
								emu.frameadvance()
								joypad.set(1, {down=true})
								while (memory.readbyte(addr_partycursor2) ~= 35) or (memory.readbyte(addr_writing) ~= 0) do
									emu.frameadvance()
								end
							end
						elseif action[current_action][alt][i][2] < 0 then
							for x=-1, action[current_action][alt][i][2], -1 do
								joypad.set(1, {up=true})
								emu.frameadvance()
								joypad.set(1, {up=true})
								while (memory.readbyte(addr_partycursor2) ~= 35) or (memory.readbyte(addr_writing) ~= 0) do
									emu.frameadvance()
								end
							end
						end
						joypad.set(1, {A=true})
						emu.frameadvance()
						joypad.set(1, {A=true})
						
						while (memory.readbyte(addr_itemcursor) ~= 99) or (memory.readbyte(addr_writing) ~= 0) do
							emu.frameadvance()
						end
						joypad.set(1, {A=true})
						emu.frameadvance()
						joypad.set(1, {A=true})
						emu.frameadvance()
						
						
						local active_index = memory.readbyte(addr_party_start +action[current_action][alt][i][3])
						local move_index = 0
						
						if action[current_action][alt][i]["move"] ~= nil then
							if action[current_action][alt][i]["move"] == memory.readbyte(addr_moves_start +active_index) then
								move_index = 1
							elseif action[current_action][alt][i]["move"] == memory.readbyte(addr_moves_start +active_index +6) then
								move_index = 2
							elseif action[current_action][alt][i]["move"] == memory.readbyte(addr_moves_start +active_index +12) then
								move_index = 3
							elseif action[current_action][alt][i]["move"] == memory.readbyte(addr_moves_start +active_index +18) then
								move_index = 4
							else
								emu.print("ERROR: Specified move " .. action[current_action][alt]["move"] .. " not found for party member #" .. active_index .. ". It may ruin this botting session, but the script won't crash for this.")
								action[current_action][alt][i][1] = 1--here goes nothing.
							end
						end
						
						while (memory.readword(addr_overwdialog) ~= 28440) do
							emu.frameadvance()
						end
						if move_index == 2 then--this code block moves the cursor to the chosen move
							joypad.set(1, {down=true})
							emu.frameadvance()
							joypad.set(1, {down=true})
							while (memory.readword(addr_overwdialog) ~= 34584) do
								emu.frameadvance()
							end
						elseif move_index == 3 then
							joypad.set(1, {down=true})
							emu.frameadvance()
							joypad.set(1, {down=true})
							while (memory.readword(addr_overwdialog) ~= 34584) do
								emu.frameadvance()
							end
							joypad.set(1, {down=true})
							emu.frameadvance()
							joypad.set(1, {down=true})
							while (memory.readword(addr_overwdialog) ~= 40728) do
								emu.frameadvance()
							end
						elseif move_index == 4 then
							joypad.set(1, {up=true})
							emu.frameadvance()
							joypad.set(1, {up=true})
							while (memory.readword(addr_overwdialog) ~= 46872) do
								emu.frameadvance()
							end
						end
						
						joypad.set(1, {A=true})
						emu.frameadvance()
						joypad.set(1, {A=true})
						emu.frameadvance()
						
						while (memory.readbyte(addr_fightdialog) ~= 124) or (memory.readbyte(addr_writing) ~= 0) do
							emu.frameadvance()
						end
					end
					
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					
					for i=1, 50 do
						emu.frameadvance()
					end
					while memory.readbyte(addr_writing) ~= 0 do
						emu.frameadvance()
					end
					
					joypad.set(1, {B=true})
					emu.frameadvance()
					joypad.set(1, {B=true})
					emu.frameadvance()
					
					local framecount = emu.framecount()
					local rng1 = memory.readbyte(addr_rng1)
					local rng2 = memory.readbyte(addr_rng2)
					
					--this code block prevents creating double saves
					local is_double = false
					for z = 1, #state[current_action] do
						if action[current_action][z]["branch"] == action[current_action][alt]["branch"] then
							for i = 1, #state[current_action][z] do
								if (state[current_action][z][i]["frame"] == framecount) and
								(state[current_action][z][i]["rng1"] == rng1) and
								(state[current_action][z][i]["rng2"] == rng2) then
									is_double = true
									z = #state[current_action] +1
									break
								end
							end
						end
					end
					if is_double == false then
						local max_states = max_states_global
						if action[current_action][alt]["custom_states"] ~= nil then
							max_states = action[current_action][alt]["custom_states"]
						end
						
						if state[current_action][alt][max_states] == nil then
							for i=1, max_states do
								if state[current_action][alt][i] == nil then
									state[current_action][alt][i] = {}
									state[current_action][alt][i]["slot"] = savestate.object()
									savestate.save(state[current_action][alt][i]["slot"])
									state[current_action][alt][i]["frame"] = framecount
									state[current_action][alt][i]["rng1"] = rng1
									state[current_action][alt][i]["rng2"] = rng2
									emu.print("Added savestate " .. current_action .. "-" .. alt .. "-" .. i .. ", frame " .. framecount)
									break
								end
							end
						else
							local maximum = state[current_action][alt][1]["frame"]
							local maximum_index = 1
							for i=2, max_states do
								if state[current_action][alt][i]["frame"] > maximum then
									maximum = state[current_action][alt][i]["frame"]
									maximum_index = i
								end
							end
							if (state[current_action][alt][maximum_index]["frame"] > framecount) then
								savestate.save(state[current_action][alt][maximum_index]["slot"])
								state[current_action][alt][maximum_index]["frame"] = framecount
								state[current_action][alt][maximum_index]["rng1"] = rng1
								state[current_action][alt][maximum_index]["rng2"] = rng2
								emu.print("Replaced savestate " .. current_action .. "-" .. alt .. "-" .. maximum_index .. ", frame " .. framecount)
							end
						end
					end
				end
				temp_state = nil--dunno if it helps clearing memory
			end
		end
	end
end

local function memory_clear()--this should clear the memory... maybe.
	for z=1, #state[current_action-1] do
		for i=1, #state[current_action-1][z] do
			state[current_action-1][z][i]["slot"] = nil
			state[current_action-1][z][i]["frame"] = nil
			state[current_action-1][z][i] = nil
		end
		state[current_action-1][z] = nil
	end
end

movie.rerecordcounting(false)
movie.setreadonly(false)

--this is the main code block that controls the flow
while current_action do	
	if action[current_action]["func"] == fight then--more actions coming soon
		action_fight()
	elseif action[current_action]["func"] == move then
		action_move()
	elseif action[current_action]["func"] == fight_item then
		action_fight_item()
	elseif action[current_action]["func"] == market_buy then
		action_market_buy()
	elseif action[current_action]["func"] == catch then
		action_catch()
	elseif action[current_action]["func"] == fight_exit then
		action_fight_exit()
	elseif action[current_action]["func"] == fight_run then
		action_fight_run()
	elseif action[current_action]["func"] == party_swap then
		action_party_swap()
	elseif action[current_action]["func"] == hunt then
		action_hunt()
	elseif action[current_action]["func"] == pc then
		action_pc()
	elseif action[current_action]["func"] == item then
		action_item()
	else
		emu.print("WARNING: Bad action code given for action " .. current_action .. ". It may ruin this botting session or crash script execution.")
	--	emu.pause()--temporary
	--	emu.frameadvance()--temporary
	end
	memory_clear()
	state[current_action-1] = nil
	current_action = current_action +1
	if action[current_action] == nil then--it's over! let's load the best state.
		current_action = current_action -1
		local minimum = state[current_action][1][1]["frame"]
		local minumum_index = 1
		local minimum_alt   = 1
		for z=1, #state[current_action] do
			for i=1, #state[current_action][z] do
				if state[current_action][z][i]["frame"] ~= nil then
					if state[current_action][z][i]["frame"] < minimum then
						minimum = state[current_action][z][i]["frame"]
						minumum_index = i
						minimum_alt = z
					end
				end
			end
		end
		emu.print("Loading savestate " .. current_action .. "-" .. minimum_alt .. "-" .. minumum_index)
		savestate.load(state[current_action][minimum_alt][minumum_index]["slot"])
		emu.print("Botting session ended successfully at frame " .. emu.framecount())
		movie.rerecordcounting(true)
		movie.setreadonly(true)
		emu.pause()
		current_action = false
	end
end