forked from alexkara15/PokeBot
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.lua
More file actions
199 lines (177 loc) · 4.75 KB
/
main.lua
File metadata and controls
199 lines (177 loc) · 4.75 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
-- Customization settings
GAME_NAME = "red" -- Only currently supported option
RESET_FOR_TIME = true -- Set to false if you just want to see the bot finish a run
local CUSTOM_SEED = nil -- Set to a known seed to replay it, or leave nil for random ones
local PAINT_ON = true -- Displays contextual information while the bot runs
-- Start code (hard hats on)
local START_WAIT = 99
local VERSION = "1.0"
local battle = require "action.battle"
local textbox = require "action.textbox"
local walk = require "action.walk"
local combat = require "ai.combat"
local control = require "ai.control"
local strategies = require "ai.strategies"
local bridge = require "util.bridge"
local input = require "util.input"
local memory = require "util.memory"
local menu = require "util.menu"
local paint = require "util.paint"
local utils = require "util.utils"
local settings = require "util.settings"
local pokemon = require "storage.pokemon"
local YELLOW = GAME_NAME == "yellow"
local hasAlreadyStartedPlaying = false
local inBattle, oldSecs
local running = true
local previousPartySize = 0
local lastHP
local criticaled = false
local function startNewAdventure()
local startMenu, withBattleStyle
if (YELLOW) then
startMenu = memory.raw(0x0F95) == 0
withBattleStyle = "battle_style"
else
startMenu = memory.value("player", "name") ~= 0
end
if (startMenu and menu.getCol() ~= 0) then
if (settings.set("text_speed", "battle_animation", withBattleStyle)) then
menu.select(0)
end
elseif (math.random(0, START_WAIT) == 0) then
input.press("Start")
end
end
local function choosePlayerNames()
local name
if (memory.value("player", "name2") == 80) then
name = "W"
else
name = "B"
end
textbox.name(name, true)
end
local function pollForResponse()
local response = bridge.process()
if (response) then
bridge.polling = false
textbox.setName(tonumber(response))
end
end
local function resetAll()
strategies.softReset()
combat.reset()
control.reset()
walk.reset()
paint.reset()
bridge.reset()
oldSecs = 0
running = false
previousPartySize = 0
-- client.speedmode = 200
if (CUSTOM_SEED) then
strategies.seed = CUSTOM_SEED
print("RUNNING WITH A FIXED SEED ("..strategies.seed.."), every run will play out identically!")
else
strategies.seed = os.time()
end
math.randomseed(strategies.seed)
end
-- Execute
print("Welcome to PokeBot "..GAME_NAME.." version "..VERSION)
local productionMode = not walk.init()
if (CUSTOM_SEED) then
client.reboot_core()
else
hasAlreadyStartedPlaying = utils.ingame()
end
strategies.init(hasAlreadyStartedPlaying)
if (RESET_FOR_TIME and hasAlreadyStartedPlaying) then
RESET_FOR_TIME = false
print("Disabling time-limit resets as the game is already running. Please reset the emulator and restart the script if you'd like to go for a fast time.")
end
if (productionMode) then
bridge.init()
else
input.setDebug(true)
end
local previousMap
while true do
local currentMap = memory.value("game", "map")
if (currentMap ~= previousMap) then
input.clear()
previousMap = currentMap
end
if (not input.update()) then
if (not utils.ingame()) then
if (currentMap == 0) then
if (running) then
if (not hasAlreadyStartedPlaying) then
client.reboot_core()
hasAlreadyStartedPlaying = true
else
resetAll()
end
else
startNewAdventure()
end
else
if (not running) then
bridge.liveSplit()
running = true
end
choosePlayerNames()
end
else
bridge.time()
local battleState = memory.value("game", "battle")
if (battleState > 0) then
if (battleState == 1) then
if (not inBattle) then
control.wildEncounter()
if (strategies.moonEncounters) then
strategies.moonEncounters = strategies.moonEncounters + 1
end
inBattle = true
end
end
local isCritical
local battleMenu = memory.value("battle", "menu")
if (battleMenu == 94) then
isCritical = false
elseif (memory.double("battle", "our_hp") == 0) then
if (memory.value("battle", "critical") == 1) then
isCritical = true
end
end
if (isCritical ~= nil and isCritical ~= criticaled) then
criticaled = isCritical
strategies.criticaled = criticaled
end
else
inBattle = false
end
local currentHP = pokemon.index(0, "hp")
if (currentHP == 0 and not strategies.canDie and pokemon.index(0) > 0) then
strategies.death(currentMap)
elseif (walk.strategy) then
if (strategies.execute(walk.strategy)) then
walk.traverse(currentMap)
end
elseif (battleState > 0) then
if (not control.shouldCatch(partySize)) then
battle.automate()
end
elseif (textbox.handle()) then
walk.traverse(currentMap)
end
end
end
if (PAINT_ON) then
paint.draw(currentMap)
end
input.advance()
emu.frameadvance()
end
bridge.close()