-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.lua
356 lines (321 loc) · 12.6 KB
/
Bot.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
-----------------------------------------------------------------------------
-- An abstract class for Computer-Player algorithms to inherit from.
-- Implements functions to be used by the main program and also within
-- child classes.
--
-- 2017 Edward Hummerston, Michael Siers
-----------------------------------------------------------------------------
require("SSF2-AI.PadQueue")
Bot = {}
local Bot_mt = {__index = Bot}
-----------------------------------------------------------------------------
-- Constructor, initialises class variables.
--
-- @param playerSlot The controller port to represent that the algorithm
-- will occupy.
-- @param inputDelay The intended length of the controller list - the
-- number of frames between an input being commited by
-- an algorithm and that input occurring in-game.
-- @return An instance of the created object.
-----------------------------------------------------------------------------
function Bot.new(playerSlot, inputDelay)
local self = {}
setmetatable(self, Bot_mt)
assert(playerSlot==1 or playerSlot==2,
"Player slot " .. playerSlot .. " is not valid.")
self.playerSlot = playerSlot
self.pad = {}
self.name = ""
self.action = ""
self.inputDelay = inputDelay
self.padq = PadQueue.new()
for i=1,inputDelay do
self.padq:enqueue({})
end
return self
end
-----------------------------------------------------------------------------
-- Returns the player slot given at initialisation.
--
-- @return The player slot integer (1 or 2)
-----------------------------------------------------------------------------
function Bot:getPlayerSlot()
return self.playerSlot
end
-----------------------------------------------------------------------------
-- Resets the controller state, called at the start of each frame.
-----------------------------------------------------------------------------
function Bot:resetPad()
self.pad = {}
end
-----------------------------------------------------------------------------
-- Returns the first controller state in the input delay queue.
--
-- @return The controller table, formatted to be input to the
-- Bizhawk emulator.
-----------------------------------------------------------------------------
function Bot:getPad()
self.padq:enqueue(self.pad)
return self.padq:dequeue()
end
-----------------------------------------------------------------------------
-- Returns the bot name.
--
-- @return The code-given name of this player.
-----------------------------------------------------------------------------
function Bot:getName()
return self.name
end
-----------------------------------------------------------------------------
-- Returns the bot action. This is a string used for indicating what action
-- the algorithm is currently attempting. Useful for debugging.
--
-- @return A string indicating what action the algorithm is
-- attempting.
-----------------------------------------------------------------------------
function Bot:getAction()
return self.action
end
-----------------------------------------------------------------------------
-- Abstract function to be overridden by a child class.
-----------------------------------------------------------------------------
function Bot:advance()
return
end
-----------------------------------------------------------------------------
-- Sets a given button on the class' controller to a given state. Accepts
-- SNES button names (Y, X, L, B, A, L) or Street Fighter button names (LP,
-- MP, HP, LK, MK, HK). Also accepts "BACK", "BACKWARD" and "TOWARD".
--
-- @param button The joypad button to be manipulated.
-- @param state Boolean state for the button to be set to.
-----------------------------------------------------------------------------
function Bot:setButton(button,state)
if button == "Back" or button == "Backward" then
button = self:getDirectionButton(false)
elseif button == "Toward" then
button = self:getDirectionButton(true)
elseif button == "LP" then
button = "Y"
elseif button == "MP" then
button = "X"
elseif button == "HP" then
button = "L"
elseif button == "LK" then
button = "B"
elseif button == "MK" then
button = "A"
elseif button == "HK" then
button = "L"
end
self.pad[string.format("P%i %s", self.playerSlot, button)] = state
end
-----------------------------------------------------------------------------
-- Returns true if the player's facing direction is to the right. Used by
-- Bot:getDirectionButton().
--
-- @param button The joypad button to be manipulated.
-- @param state Boolean state for the button to be set to.
-----------------------------------------------------------------------------
function Bot:isFacingRight()
if self.playerSlot % 2 == 1 then --playerSlot is odd (1)
return memory.read_u8(0x5F3) == 0x40
else --playerSlot is even (2)
return memory.read_u8(0x833) == 0x40
end
end
-----------------------------------------------------------------------------
-- Returns a string of "Right" or "Left" based on the boolean that it is
-- passed.
--
-- @param forward A boolean indicating whether the requested
-- left/right direction is in front of the character.
-----------------------------------------------------------------------------
function Bot:getDirectionButton(forward)
intendedRight = forward
if not self:isFacingRight() then
intendedRight = not intendedRight
end
if intendedRight then
return "Right"
else
return "Left"
end
end
-----------------------------------------------------------------------------
-- Returns true if the player is crouching. Includes crouching attacks and
-- special moves (e.g. low tiger shot).
--
-- @return A boolean of whether the player is crouching.
-----------------------------------------------------------------------------
function Bot:isCrouching()
if self.playerSlot % 2 == 1 then
return memory.read_u8(0x544) == 0x1
else
return memory.read_u8(0x784) == 0x1
end
end
-----------------------------------------------------------------------------
-- Returns true if the opponent is crouching. Includes crouching attacks and
-- special moves (e.g. low tiger shot).
--
-- @return A boolean of whether the opponent is crouching.
-----------------------------------------------------------------------------
function Bot:isOpponentCrouching()
enemyPlayerSlot = self.playerSlot + 1
if enemyPlayerSlot % 2 == 1 then
return memory.read_u8(0x544) == 0x1
else
return memory.read_u8(0x784) == 0x1
end
end
-----------------------------------------------------------------------------
-- Returns true if the player is in the air. Includes not only jumps, but
-- special moves that make a character airborne
--
-- @return A boolean of whether the player is airborne.
-----------------------------------------------------------------------------
function Bot:isAir()
if self.playerSlot % 2 == 1 then
return memory.read_u8(0x548) == 0x1
else
return memory.read_u8(0x788) == 0x1
end
end
-----------------------------------------------------------------------------
-- Returns true if the opponent is in the air. Includes not only jumps, but
-- special moves that make a character airborne
--
-- @return A boolean of whether the opponent is airborne.
-----------------------------------------------------------------------------
function Bot:isOpponentAir()
enemyPlayerSlot = self.playerSlot + 1
if enemyPlayerSlot % 2 == 1 then
return memory.read_u8(0x548) == 0x1
else
return memory.read_u8(0x788) == 0x1
end
end
-----------------------------------------------------------------------------
-- Returns the distance from opponent stored for this player. Testing
-- indicates that this is the same value for both players, but is not
-- definitive.
--
-- @return An integer of distance between the players.
-----------------------------------------------------------------------------
function Bot:getDistance()
if self.playerSlot % 2 == 1 then
return memory.read_u8(0x5EB)
else
return memory.read_u8(0x82B)
end
end
-----------------------------------------------------------------------------
-- Returns true if this player is performing an attack. Includes the startup,
-- active and recovery frames of attacks. Does not include whether or not a
-- fireball is active, only whether they are in a fireball animation.
--
-- @return A boolean indicating whether this player is
-- attacking.
-----------------------------------------------------------------------------
function Bot:isAttacking ()
if self.playerSlot % 2 == 1 then
return memory.read_u8(0x5E9) == 0x1
else
return memory.read_u8(0x829) == 0x1
end
end
-----------------------------------------------------------------------------
-- Returns true if the opponent is performing an attack. Includes the
-- startup, active and recovery frames of attacks. Does not include whether
-- or not a fireball is active, only whether they are in a fireball
-- animation.
--
-- @return A boolean indicating whether the opponent is
-- attacking.
-----------------------------------------------------------------------------
function Bot:isOpponentAttacking()
enemyPlayerSlot = self.playerSlot + 1
if enemyPlayerSlot % 2 == 1 then
return memory.read_u8(0x5E9) == 0x1
else
return memory.read_u8(0x829) == 0x1
end
end
-----------------------------------------------------------------------------
-- Returns true if this player has a projectile active, meaning they cannot
-- make another.
--
-- @return A boolean indicating whether this player has a
-- fireball active.
-----------------------------------------------------------------------------
function Bot:hasFireball()
if self.playerSlot % 2 == 1 then
return memory.read_u8(0x5F7) ~= 0
else
return memory.read_u8(0x837) ~= 0
end
end
-----------------------------------------------------------------------------
-- Returns true if the opponent has a projectile active, meaning they cannot
-- make another.
--
-- @return A boolean indicating whether the opponent has a
-- fireball active.
-----------------------------------------------------------------------------
function Bot:hasOpponentFireball()
enemyPlayerSlot = self.playerSlot + 1
if enemyPlayerSlot % 2 == 1 then
return memory.read_u8(0x5F7) ~= 0
else
return memory.read_u8(0x837) ~= 0
end
end
-----------------------------------------------------------------------------
-- Returns the x and y coordinates of this player.
--
-- @return A table with x and y values for this player's
-- position. X value is this player's distance from the
-- wall behind them. Y value is the distance from the
-- ground.
-----------------------------------------------------------------------------
function Bot:getAbsolutePosition()
local xyReturn = {x = 0, y = 0}
if self.playerSlot % 2 == 1 then
xyReturn.x = memory.read_s16_le(0x5F7)
xyReturn.y = 192 - memory.read_u8(0x50A)
else
xyReturn.x = memory.read_s16_le(0x837)
xyReturn.y = 192 - memory.read_u8(0x74A)
end
if(self:isFacingRight()) then
xyReturn.x = xyReturn.x - 53
else
xyReturn.x = 459 - xyReturn.x
end
return xyReturn
end
-----------------------------------------------------------------------------
-- Returns the x and y coordinates of the opponent.
--
-- @return A table with x and y values for this player's
-- position. X value is the opponent's distance from the
-- wall behind THIS player. Y value is the distance from
-- the ground.
-----------------------------------------------------------------------------
function Bot:getOpponentAbsolutePosition()
local xyReturn = {x = 0, y = 0}
if (self.playerSlot + 1) % 2 == 1 then
xyReturn.x = memory.read_s16_le(0x507)
xyReturn.y = 192 - memory.read_u8(0x50A)
else
xyReturn.x = memory.read_s16_le(0x747)
xyReturn.y = 192 - memory.read_u8(0x74A)
end
if(self:isFacingRight()) then
xyReturn.x = xyReturn.x - 53
else
xyReturn.x = 459 - xyReturn.x
end
return xyReturn
end