Honestly, I'm not sure if these forums has to be about specifically TASing the game or if I can simply plunk down a lua bot, but in any case, someone (
t3h Icy?) on IRC wanted to create a LuaBot for this game. I got interested in creating my own bot due to that, and here's the result:
.fm2 of me getting wiped by my own creation.
As for the bot itself... Nothing extravagant. The idea is that we find which cookie appears the most, find a line which has the most of them, then move cookies into place. Said idea was mentioned in IRC...
No attempt was made at checking to see if lining up those special cookies would end up smacking oneself with a -7 or panic. Also, No attempt was made at making more than one line at once. Could also be cleaned up a bit...
local grid= {{},{},{},{},{}}
local C_Count= {}
local C_Row= {}
local C_Column= {}
local Ri,Ci,tx,ty,cx,cy,Type,ToX,ToY
--*****
function count()
--*****
-- Everything's fine if it returns false.
-- (re-)initialize arrays
for i= 1, 5 do
C_Count[i]= 0
C_Row[i]= 0
C_Column[i]= 0
end
C_Count[6]= 0
for x=1,5 do
for y=1,5 do
grid[x][y] = memory.readbyte(0x026A + 0x02*x + 0x14*y)/2 -7
if grid[x][y] > 6 or grid[x][y] < 1 or grid[x][y] ~= math.floor(grid[x][y]) then
return true -- Error: Unusual value
end
C_Count[grid[x][y]]= C_Count[grid[x][y]]+1
end
end
return false
end
--*****
function CursorGoTo(TargetX,TargetY)
--*****
-- Eats 0 to 4 frames to reach destination
local zx = memory.readbyte(0x03E5); zx = (zx+ 2)/ 2
local zy = memory.readbyte(0x03E4); zy = (zy+ 2)/ 2
local up,dn,lf,rt= {},{},{},{}
up["up"]= true; dn["down"]= true; lf["left"]= true; rt["right"]= true
zx= (zx-TargetX+2)%5-2 -- range of output is -2 ~ +2
zy= (zy-TargetY+2)%5-2
if zy == -2 or zy == 2 then
if zy == 2 then
joypad.set(2,up)
zy= zy-1
else
joypad.set(2,dn)
zy= zy+1
end
FCEU.frameadvance()
if zx == 0 then
FCEU.frameadvance() -- Idle, holding the button won't help.
end
end
if zx ~= 0 then
if zx > 0 then
joypad.set(2,lf)
zx= zx-1
else
joypad.set(2,rt)
zx= zx+1
end
FCEU.frameadvance()
if zy == 0 and zx ~= 0 then
FCEU.frameadvance() -- Idle, holding the button won't help.
end
end
if zy ~= 0 then
if zy > 0 then
joypad.set(2,up)
else
joypad.set(2,dn)
end
FCEU.frameadvance()
end
if zx ~= 0 then
if zx > 0 then
joypad.set(2,lf)
else
joypad.set(2,rt)
end
FCEU.frameadvance()
end
end
--*****
function MoveCookie(dir)
--*****
local pad= {}
pad["A"]= true
pad[dir]= true
joypad.set(2,pad)
for i= 1, 8 do
FCEU.frameadvance()
end
end
gui.register(DispCookieVal)
FCEU.pause()
--*****
while true do
local Dot= 0
while count() do -- grid[][] and CookieCount[] are refreshed
Dot= (Dot+1)%30
gui.drawbox(10+Dot,10,11+Dot,11,"green") -- Gogo dancing dot!
FCEU.frameadvance() -- If something's wrong, do not act.
end
gui.drawpixel(0,10,"clear") -- For some reason, the gui "sticks"...
-- Find the greatest cookie count
Type= 1
for i= 2, 6 do
if C_Count[i] > C_Count[Type] then
Type= i
end
end
-- Count up the right cookies in rows/columns
for x=1,5 do
for y=1,5 do
if grid[x][y] == Type then
C_Row[y]= C_Row[y]+1
C_Column[x]= C_Column[x]+1
end
end
end
-- Compare rows and columns
Ri, Ci= 1, 1
for i=2,5 do
if C_Row[i] > C_Row[Ri] then
Ri= i
end
if C_Column[i] > C_Column[Ci] then
Ci= i
end
end
local Var= "Row"
if C_Column[Ci] > C_Row[Ri] then
Var= "Column"
end
-- Find a spot where we need to put a cookie!
FCEU.frameadvance()
tx,ty= 0,0
for i= 1, 5 do
local x,y= Ci, Ri
if Var == "Row" then
x= i
else
y= i
end
if grid[x][y] ~= Type then
tx, ty= x, y
break
end
end
if tx ~= 0 then -- Did not find a target location? Stop now.
-- Find a cookie to move to the spot!
cx,cy= 0,0
local SmarTarg= { 0, 1,-1, 2,-2} -- What direction should we scan?
for x=1, 5 do
local found= false
for y=1, 5 do
local Xx= ((tx+SmarTarg[x]-1)%5)+1
local Yy= ((ty+SmarTarg[y]-1)%5)+1
if (Var=="Row" and y==1) or (Var=="Column" and x==1) then
-- Uh, sir... We're looking at our own target line...
elseif grid[Xx][Yy] == Type then
found= true
cx,cy= Xx,Yy
break
end
end
if found then break end
end
-- Move cursor to target location
ToX,ToY= 0,0
if Var == "Row" then
ToX,ToY= tx,cy
else
ToX,ToY= cx,ty
end
CursorGoTo(ToX,ToY)
if Var == "Row" then
while (tx ~= cx) do
if (tx-cx+2)%5-2 < 0 then
MoveCookie("left")
cx= (cx-2)%5 +1
else
MoveCookie("right")
cx= (cx )%5 +1
end
end
end
while (ty ~= cy) do
if (ty-cy+2)%5-2 < 0 then
MoveCookie("up")
cy= (cy-2)%5 +1
else
MoveCookie("down")
cy= (cy )%5 +1
end
end
while (tx ~= cx) do -- If it's column, we didn't move x yet
if (tx-cx+2)%5-2 < 0 then -- If it's row, we're already there
MoveCookie("left")
cx= (cx-2)%5 +1
else
MoveCookie("right")
cx= (cx )%5 +1
end
end
end
end