-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathingTests.lua
162 lines (139 loc) · 3.01 KB
/
PathingTests.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
PATH = {{33,8},
{21,8},
{21,3},
{11,57},
{11,53},
{16,53},
{16,37}
}
X_ADDR = 0x14E6
Y_ADDR = 0x14E7
WALK_FRAMES = 16
TURN_FRAMES = 4
WAIT_FRAMES = 16
PRESS_FRAMES = 2
-- Number of times in a row a move can be retried
-- before moving on to a different move, to avoid deadlocking
MAX_MOVE_RETRIES = 7
LEVEL_RETRY_CUTOFF = 15
POKECENTER_PATH = {{"Left", 12},
{"Up", 9},
{"Right", 1}
}
POKE_LEVEL_ADDR = 0x1CFE
STATE_ADDR = 0x143A
IN_ATTACK_MENU_ADDR = 0x00CE
IN_ATTACK_MENU_VAL = 0x44
SELECTED_MOVE_ADDR = 0x0FA9
NEED_TO_WALK_STATE = 14
BONKING_1_STATE = 86
BONKING_2_STATE = 79
ENCOUNTER_STATE = 251
OUT_OF_POKE_STATE = 209
WHITED_OUT_STATE = 210
WHITING_OUT_1_STATE = 213
WHITING_OUT_2_STATE = 215
JUST_WON = 252
DONE_WINNING = 253
PP_ADDR = {0x1CF6, 0x1CF7, 0x1CF8, 0x1CF9}
ENEMY_HP_ADDR = 0x1217
last_move_used = 1
move_retries = 0
fighting = 0
fights = 0
deaths = 0
death_counter_locked = true
last_enemy_hp = -1
whiffs = 0
path_index = 1
function doFrame()
emu.frameadvance()
end
function waitFrames(count)
for i=1,count,1 do
doFrame()
end
end
function press(button)
for i=1,PRESS_FRAMES,1 do
local dpad = {}
dpad[button] = true
joypad.set(dpad)
doFrame()
end
end
function handleEncounter()
state = memory.readbyte(IN_ATTACK_MENU_ADDR, "WRAM")
if state == IN_ATTACK_MENU_VAL then
local current_move = memory.readbyte(SELECTED_MOVE_ADDR, "WRAM")
if (last_move_used == current_move and move_retries >= MAX_MOVE_RETRIES) then
-- We need to adjust the move we're using because it's probably not a damage move
console.log("Switiching from ineffective move")
press("Down")
waitFrames(2*WAIT_FRAMES)
end
current_move = memory.readbyte(SELECTED_MOVE_ADDR, "WRAM")
if last_move_used ~= current_move then
move_retries = 0
end
local current_pp = memory.readbyte(PP_ADDR[current_move], "WRAM")
if current_pp == 0 then
console.log("Move #" .. current_move .. " is out of PP!")
press("Down")
waitFrames(2*WAIT_FRAMES)
else
last_move_used = current_move
move_retries = move_retries + 1
console.log("Last Move: " .. last_move_used)
console.log("Move retries: " .. move_retries)
press("A")
waitFrames(WAIT_FRAMES)
end
else
press("A")
waitFrames(WAIT_FRAMES)
end
end
function walkToTile(dest_x,dest_y)
local x = memory.readbyte(X_ADDR, "WRAM")
local y = memory.readbyte(Y_ADDR, "WRAM")
local dir = nil
if x < dest_x then
dir = "Right"
elseif x > dest_x then
dir = "Left"
elseif y < dest_y then
dir = "Down"
elseif y > dest_y then
dir = "Up"
end
if dir == nil then
return true
end
for i=1,WALK_FRAMES,1 do
local dpad = {}
dpad[dir] = true
joypad.set(dpad)
doFrame()
end
return false
end
function walkPath()
if path_index == #PATH + 1 then
doFrame()
return
end
x = PATH[path_index][1]
y = PATH[path_index][2]
if walkToTile(x,y) then
path_index = path_index + 1
end
end
while true do
local state = memory.readbyte(STATE_ADDR, "WRAM")
if state == ENCOUNTER_STATE then
handleEncounter()
else
walkPath()
end
end