-
Notifications
You must be signed in to change notification settings - Fork 0
/
yoshi-nes-ai.lua
344 lines (344 loc) · 13.8 KB
/
yoshi-nes-ai.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
do
local _ENV=_ENV
package.preload["ai"]=function(...)local e=_G.arg;require"util"require"read_memory"require"control"function simplifyMoves_Table(n)print("simplifying "..table.toString(n))Board:update()local a=Board:copy()if n then
local e=1
while e<#n+1 do
local o=n[e]if e<#n and o==n[e+1]then
table.remove(n,e)table.remove(n,e)e=1
a=Board:copy()elseif e<#n-2 and o==n[e+2]and n[e+1]==n[e+3]and areConsecutiveNums(o,n[e+1])then
table.remove(n,e)table.remove(n,e+2)e=1
a=Board:copy()elseif tablesEqualOrder(a.raw[o+1],a.raw[o+2])then
print("didn't swap empty columns")print("(col "..(o+1)..": ) "..table.toString(a.raw[o+1]))print("(col "..(o+2)..": ) "..table.toString(a.raw[o+2]))table.remove(n,e)e=1
a=Board:copy()else
a.raw=swapTableSubTabs(a.raw,o+1,o+2)e=e+1
end
end
print("simplified to "..table.toString(n))return n
else
return{}end
end
function generateMoves_Table(a,e)print("generating moves")local n
if not e then
n={1,2,3,4}else
n=e
end
local t={}local o=1
while not(tablesEqualOrder(n,a,-1)or o>#a)do
local e
if a[o]==-1 then
e=o
while hasValue(a,n[e])do
e=e+1
assert(e<=4,"somehow couldnt find an unused column to fill the wildcard")end
else
e=firstIndexOf(n,a[o])end
table.insert(n,o,n[e])table.remove(n,e+1)for e=e-1,o,-1 do
t[#t+1]=e-1
end
print("added moves to move column "..e.." to position "..o)print("currentSimBoard = "..table.toString(n))o=o+1
end
print("done")return{["moves"]=simplifyMoves_Table(t),["endBoard"]=n}end
function quickSwapPossible(o,a,t,n)print("ooh ahh im going to try to quick swap OwO")print("i think im quick swapping bc i think "..o.." is uncovered")for e=1,4,1 do
if e~=o and
t:numBlocksInCol(e)<=a and
n[#n][e]==-1 then
return e
end
end
return-1
end
function getTargetStates(o,l)if not o then
o=Board:copy()end
if not l then
l=MemMap.fallingBlocks()end
local r=false
local i={false,false,false,false}local e={{-1,-1,-1,-1}}repeat
r=false
local n={["height"]=-1,["colNum"]=-1,["fallingColNum"]=-1,["chunkLength"]=-1,["tempTargColNum"]=-1}for d,l in ipairs(l)do
local a={["height"]=-1,["colNum"]=-1,["chunkLength"]=-1,["tempTargColNum"]=-1}if l==Blocks.TOP_EGG then
print("trying to match top egg")print("about to look for col w bottom egg")for t,r in ipairs(o.raw)do
print("col num "..t..": "..table.toString(r))if hasValue(r,Blocks.BOTTOM_EGG)then
print("found col w bottom egg")local n=o:numBlocksInCol(t)local r=n-(8-firstIndexOf(r,Blocks.BOTTOM_EGG))if a.height<n or(a.height==n and a.chunkLength<r)then
if i[t]then
local e=quickSwapPossible(t,n,o,e)if e~=-1 then
a={["height"]=n,["colNum"]=t,["chunkLength"]=r,["tempTargColNum"]=e}end
else
a={["height"]=n,["colNum"]=t,["chunkLength"]=r,["tempTargColNum"]=-1}end
end
end
end
elseif l~=Blocks.NONE then
for n,t in ipairs(o:matchesOnTops(l))do
if t then
local t=o:numBlocksInCol(n)if a.height<t or(a.height==t and a.tempTargColNum~=-1)then
if i[n]then
local e=quickSwapPossible(n,t,o,e)if e~=-1 then
a={["height"]=t,["colNum"]=n,["chunkLength"]=1,["tempTargColNum"]=e}end
else
a={["height"]=t,["colNum"]=n,["chunkLength"]=1,["tempTargColNum"]=-1}end
end
end
end
end
if n.height<a.height then
n={["height"]=a.height,["colNum"]=a.colNum,["fallingColNum"]=d,["chunkLength"]=a.chunkLength,["tempTargColNum"]=a.tempTargColNum}end
end
print("best match in sweep col: "..n.fallingColNum)if n.fallingColNum~=-1 then
r=true
i[n.colNum]=true
if n.tempTargColNum~=-1 then
e[#e][n.fallingColNum]=n.tempTargColNum
e[#e+1]=4-instancesOf(l,Blocks.NONE)e[#e+1]={-1,-1,-1,-1}end
e[#e][n.fallingColNum]=n.colNum
for e=1+(8-o:numBlocksInCol(n.colNum)),1+(8-o:numBlocksInCol(n.colNum))+n.chunkLength-1,1 do
print("clearing block at col "..n.colNum.." block "..e)o.raw[n.colNum][e]=Blocks.NONE
end
l[n.fallingColNum]=Blocks.NONE
end
until(onlyHasValue(l,Blocks.NONE)or not r)print(table.toString(e))print("handling garbage")local s={false,false,false,false}local r=tableToInvertedDict(o:getTopBlocks())r[7]=-1
for d,a in ipairs(l)do
print("looking at falling block in col "..d)local n={["colNum"]=-1,["height"]=9,["type"]=7,["tempTargColNum"]=-1}if a~=Blocks.NONE and a~=Blocks.TOP_EGG then
local m={false,false,false,false,false,false}for a=1,4,1 do
if not s[a]then
print("col "..a.." is not taken")local l=o:numBlocksInCol(a)local t=o:getTopBlocks()[a]if l<n.height or(n.height==l and(t==Blocks.BOTTOM_EGG or(n.type~=Blocks.BOTTOM_EGG and
r[n.type]<r[t])))then
if i[a]then
local e=quickSwapPossible(a,l,o,e)if e~=-1 then
print("col "..a.." new best col. It is "..l.." blocks tall and the prev is "..n.height.." blocks tall.")print(" It has "..r[t].." "..t.."s on tops and prev has "..r[n.type].." "..n.type.."s on tops.")n={["colNum"]=a,["height"]=l,["type"]=t,["tempTargColNum"]=e}end
else
print("col "..a.." new best col. It is "..l.." blocks tall and the prev is "..n.height.." blocks tall")print(" It has "..r[t].." "..t.."s on tops and prev has "..r[n.type].." "..n.type.."s on tops.")n={["colNum"]=a,["height"]=l,["type"]=t,["tempTargColNum"]=-1}end
end
if t~=Blocks.NONE then
m[t]=true
end
else
print("col "..a.." is taken")end
end
assert(n.colNum~=-1,"somehow could not find a slot for garbage")if n.tempTargColNum~=-1 then
e[#e][d]=n.tempTargColNum
e[#e+1]=4-instancesOf(l,Blocks.NONE)e[#e+1]={-1,-1,-1,-1}end
e[#e][d]=n.colNum
s[n.colNum]=true
r[n.type]=r[n.type]-1
print("put garbage falling in column "..d.." in column "..n.colNum)end
end
return e
end
function handleBlockSet()local e=MemMap.fallingBlocks()local n=getTargetStates()print(table.toString(n))local o={1,2,3,4}for a,e in ipairs(n)do
if type(e)=="table"then
print("swapping blocks in state")print("goalBoard: "..table.toString(e))local e=generateMoves_Table(e,o)o=e["endBoard"]swapAtAllPosInTable(e["moves"],n,a)while MemMap.numFallingBlocks()==0 do
emu.frameadvance()end
else
print("sending the blocks down until there are "..e.." left")dropUntilNumBlocksFalling(e)end
end
dropUntilNumBlocksFalling(0)print("---------------------------------------")end
end
end
do
local _ENV=_ENV
package.preload["control"]=function(...)local e=_G.arg;function putMarioAtPos(e)print("mario needs to move from "..MemMap.marioPos().." to "..e)if MemMap.marioPos()>e then
while MemMap.marioPos()~=e do
print("moving left")joypad.set(BOT_PLAYER_NUM,{left=true})emu.frameadvance()joypad.set(BOT_PLAYER_NUM,{left=false})emu.frameadvance()end
elseif MemMap.marioPos()<e then
while MemMap.marioPos()~=e do
joypad.set(BOT_PLAYER_NUM,{right=true})emu.frameadvance()joypad.set(BOT_PLAYER_NUM,{right=false})emu.frameadvance()end
end
print("moved successfully")end
function swapAtPos(e)putMarioAtPos(e)local e=MemMap.marioFrame()local n=e==0 and 4 or 0
while MemMap.marioFrame()==e do
joypad.set(BOT_PLAYER_NUM,{A=true})emu.frameadvance()joypad.set(BOT_PLAYER_NUM,{A=false})emu.frameadvance()end
while MemMap.marioFrame()~=n do
joypad.set(BOT_PLAYER_NUM,{A=false})emu.frameadvance()end
end
function swapAtAllPosInTable(e)if#e>0 then
print("swapping at "..table.toString(e))for n,e in ipairs(e)do
swapAtPos(e)end
end
return states
end
function moveColumn(n,e)if n>0 then
print("need to move column "..n.." to position "..e)local e=e-n
local o=e/math.abs(e)print("iterating from 0 to "..math.abs(e))if e~=0 then
for e=0,math.abs(e)-1 do
swapAtPos(n+(e*o)+((o-3)/2))end
end
end
end
function randomPause(e)e=e or 5
for e=0,math.random(e)do
emu.frameadvance()end
end
function applySettings(o)local e=tonumber(o[1])if e==1 and tonumber(o[5])~=nil then
local e={"mushroom","flower","star","off"}o[5]=e[tonumber(o[5])]end
local r=o[6]=="on"math.randomseed(os.time())while MemMap.startMenuSelectorStatus()~=1 do
emu.frameadvance()end
while MemMap.startMenuSelectorStatus()~=e do
joypad.set(1,{down=true})emu.frameadvance()emu.frameadvance()end
joypad.set(1,{start=true})emu.frameadvance()while MemMap.settingsMenuSelectorStatus()do
emu.frameadvance()end
if r then
randomPause()end
local n=e==1 and{MemMap.menuSettGame,MemMap.menuSettLevel,MemMap.menuSettSpeed,MemMap.menuSettBGM}or{MemMap.menuSettLevel,MemMap.menuSettSpeed}for a,l in ipairs(n)do
for n=1,e do
local t=MemMap.menuSettName(e==1 and 0 or n)while tostring(l(e==1 and 0 or n))~=o[(e==1 and a+1 or(2*n+a-1))]do
joypad.set(n,{right=true})emu.frameadvance()emu.frameadvance()end
if r then
randomPause()end
if t~=(e==1 and"bgm"or"speed")then
while MemMap.menuSettName(e==1 and 0 or n)==t do
joypad.set(n,{down=true})emu.frameadvance()emu.frameadvance()end
end
print("[set "..(e==1 and" "or("player "..n.."'s "))..t.." to "..o[(e==1 and a+1 or(2*n+a-1))].."]")end
end
joypad.set(1,{start=true})emu.frameadvance()end
function dropUntilNumBlocksFalling(e)while e<MemMap.numFallingBlocks()do
joypad.set(BOT_PLAYER_NUM,{down=true})emu.frameadvance()end
end
end
end
do
local _ENV=_ENV
package.preload["read_memory"]=function(...)local e=_G.arg;require"util"Blocks={NONE=0,GOOMBA=1,PIRANHA=2,BOO=3,BLOOPER=4,TOP_EGG=5,BOTTOM_EGG=6}function readBytes(o,n)local e={}for n=0,n-1 do
e[n+1]=memory.readbyte(o+n)end
return e
end
function readColumn(n)return map({1,2,3,4,5,6,7,8},function(e)return memory.readbyte(1168+(e-1)+((n-1)*9))end)end
MemMap={addresses={MENU_PLAYERS=227,SETT_MENU_CURSOR_STAT=609,OP_MENU_SETT_ROW=758,OP_MENU_GAME=660,OP_MENU_LEVEL=661,OP_MENU_SPEED=662,OP_MENU_BGM=663,TP_MENU_P1_SETT_ROW=1349,TP_MENU_P2_SETT_ROW=1350,TP_MENU_P1_LEVEL=660,TP_MENU_P1_SPEED=661,TP_MENU_P2_LEVEL=662,TP_MENU_P2_SPEED=663,MARIO_POS=1088,LUIGI_POS=1089,MARIO_FRAME=1090,LUIGI_FRAME=1091,P1_FALL_TYPES_START=1114,P2_FALL_TYPES_START=1118,P1_FALL_STATUS_START=1122,P2_FALL_STATUS_START=1126,P1_BOARD_START=1168,P2_BOARD_START=1204}}function MemMap.marioPos(e)e=e or BOT_PLAYER_NUM
return memory.readbyte(MemMap.addresses[(e==2 and"LUIGI"or"MARIO").."_POS"])end
function MemMap.marioFrame(e)e=e or BOT_PLAYER_NUM
return memory.readbyte(MemMap.addresses[(e==2 and"LUIGI"or"MARIO").."_FRAME"])end
function MemMap.fallingBlocks(e)e=e or BOT_PLAYER_NUM
return readBytes(MemMap.addresses[(e==2 and"P2"or"P1").."_FALL_TYPES_START"],4)end
function MemMap.numFallingBlocks()pNum=pNum or BOT_PLAYER_NUM
return instancesOf(readBytes(MemMap.addresses[(pNum==2 and"P2"or"P1").."_FALL_STATUS_START"],4),2)end
function MemMap.column(n,e)e=e or BOT_PLAYER_NUM
return readBytes(MemMap.addresses[(e==2 and"P2"or"P1").."_BOARD_START"]+((n-1)*9),8)end
function MemMap.startMenuSelectorStatus()local e={[199]=1,[215]=2}return e[memory.readbyte(MemMap.addresses.MENU_PLAYERS)]end
function MemMap.settingsMenuSelectorStatus(e)e=e or 0
return memory.readbyte(MemMap.addresses.SETT_MENU_CURSOR_STAT)==(e>0 and 176 or 204)end
function MemMap.menuSettName(e)e=e or 0
local n=e==0 and{"game","level","speed","bgm"}or{"level","speed"}return n[memory.readbyte(MemMap.addresses[e==1 and"TP_MENU_P1_SETT_ROW"or e==2 and"TP_MENU_P2_SETT_ROW"or"OP_MENU_SETT_ROW"])+1]end
function MemMap.menuSettGame()local e={[104]="A",[160]="B"}return e[memory.readbyte(MemMap.addresses.OP_MENU_GAME)]end
function MemMap.menuSettLevel(e)e=e or 0
local n={[96]=1,[120]=2,[144]=3,[168]=4,[192]=5}return n[memory.readbyte(MemMap.addresses[e==1 and"TP_MENU_P1_LEVEL"or e==2 and"TP_MENU_P2_LEVEL"or"OP_MENU_LEVEL"])]end
function MemMap.menuSettSpeed(e)e=e or 0
local n={[100]="low",[160]="high"}return n[memory.readbyte(MemMap.addresses[e==1 and"TP_MENU_P1_SPEED"or e==2 and"TP_MENU_P2_SPEED"or"OP_MENU_SPEED"])]end
function MemMap.menuSettBGM()local e={[72]="mushroom",[104]="flower",[136]="star",[168]="off"}return e[memory.readbyte(MemMap.addresses.OP_MENU_BGM)]end
Board={raw={{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}}function Board:getRow(e)return{}end
function Board:update(e)e=e or BOT_PLAYER_NUM
self.raw=map({1,2,3,4},function(n)return MemMap.column(n,e)end)end
function Board:getTopBlocks()return map(self.raw,function(e)for n,e in ipairs(e)do
if e>0 then
return e
end
end
return 0
end)end
function Board:matchesOnTops(e)return map(self:getTopBlocks(),function(n)return n==e
end)end
function Board:numBlocksInCol(e)for e,n in ipairs(self.raw[e])do
if n>0 then
return 9-e
end
end
return 0
end
function Board:copy()return copy(self)end
end
end
do
local _ENV=_ENV
package.preload["util"]=function(...)local e=_G.arg;function concatTables(e,n)for o,n in ipairs(n)do
e[#e+1]=n
end
return e
end
function table.toString(n)if n then
local e=""for o,n in ipairs(n)do
e=e..(type(n)=="table"and table.toString(n)or n)..", "end
return string.sub(e,1,#e-2)else
return""end
end
function hasValue(e,n)for o,e in pairs(e)do
if e==n then
return true
end
end
return false
end
function onlyHasValue(e,n)for o,e in pairs(e)do
if not(e==n)then
return false
end
end
return true
end
function instancesOf(e,n)inst=0
for o,e in pairs(e)do
if e==n then
inst=inst+1
end
end
return inst
end
function firstIndexOf(e,o)for n,e in ipairs(e)do
if e==o then
return n
end
end
return 0
end
function tablesEqualOrder(o,a,n)for e=1,#o do
if not(o[e]==a[e]or(n~=nil and(o[e]==n or a[e]==n)))then
return false
end
end
return true
end
function areConsecutiveNums(e,n)return(e+1==n)or(n+1==e)end
function swapTableSubTabs(e,n,o)local a=e[n]e[n]=e[o]e[o]=a
return e
end
function map(n,o)local e={}for n,a in ipairs(n)do
e[n]=o(a)end
return e
end
function copy(e)local n={}for o,e in pairs(e)do
n[o]=type(e)=="table"and copy(e)or e
end
return n
end
function replace(e,o,n)for a,t in ipairs(e)do
if t==o then
e[a]=n
end
end
return e
end
function tableToInvertedDict(n)local e={}for o,n in ipairs(n)do
e[n]=e[n]==nil and 1 or e[n]+1
end
return e
end
end
end
require"ai"require"control"BOT_PLAYER_NUM=1
do
local n={"1","A","1","low","1","on"}if arg then
local e=1
for o in arg:gmatch("([^"..", ".."]+)")do
if e==1 and o=="2"then
n={"2","1","low","5","high","on"}BOT_PLAYER_NUM=2
else
n[e]=o
end
e=e+1
end
end
emu.poweron()applySettings(n)while onlyHasValue(MemMap.fallingBlocks(),0)do
print("waiting")joypad.set(1,{down=true})emu.frameadvance()end
while hasValue(readBytes(1130,4),40)do
print("waiting again")emu.frameadvance()end
print("starting falling blocks: "..table.toString(MemMap.fallingBlocks()))print("random byte thing: "..memory.readbyte(1130))Board:update()Board:copy()while true do
Board:update()handleBlockSet()emu.frameadvance()end
end