local rng
local i
local j
local k
local l
local m
local n
local o
local p
local current_character
local target
local target2
local row = {}
local found
local depth
local previous
local top_previous
local best
local test
local top_action
local top_best
local top_depth
local best_i
local best_j
local current
local current_i
path = "C:\\Users\\pgrimsrud\\NES\\State\\stadium_events"
path2 = "C:\\Users\\pgrimsrud\\NES\\State\\stadium_events1_"
outfile = "C:\\Users\\pgrimsrud\\NES\\notes\\stadium_events1.txt"
new_previous = "C:\\Users\\pgrimsrud\\NES\\notes\\stadium_events_new_previous.txt"
function L()
joypad.setfrommnemonicstr(string.format("|..|........|.1..........|"))
emu.frameadvance()
end
function R()
joypad.setfrommnemonicstr(string.format("|..|........|2...........|"))
emu.frameadvance()
end
function B()
joypad.setfrommnemonicstr(string.format("|..|........|............|"))
emu.frameadvance()
end
memory.usememorydomain("System Bus")
top_depth = 4
best = 0
function load(num)
if num == 0 then
savestate.load(path .. num ..".State")
else
savestate.load(path2 .. num ..".State")
end
end
function save(num)
if num == 0 then
savestate.save(path .. num ..".State")
else
savestate.save(path2 .. num ..".State")
end
end
--function recursion(depth, prev)
-- if depth == 0 then
-- x = memory.read_u8(0x2CB)
-- p = previoius
-- for i=0,199,1 do
-- if p == 1 then
-- R()
-- p = 2
-- else
-- L()
-- p = 1
-- end
--
-- x = x + memory.read_u8(0x2CB)
-- end
--
-- if x > best then
-- best = x
-- end
-- else
-- if depth == top_depth then
-- top_best = 0
-- end
--
-- save(depth)
-- if prev == 1 then
-- R()
-- recursion(depth-1,2)
-- if depth == top_depth and best > top_best then
-- top_best = best
-- top_action = 2
-- end
-- elseif prev == 2 then
-- L()
-- recursion(depth-1,1)
-- if depth == top_depth and best > top_best then
-- top_best = best
-- top_action = 1
-- end
-- else
-- R()
-- recursion(depth-1,2)
-- if depth == top_depth and best > top_best then
-- top_best = best
-- top_action = 2
-- end
--
-- load(depth)
-- L()
-- recursion(depth-1,1)
-- if depth == top_depth and best > top_best then
-- top_best = best
-- top_action = 1
-- end
-- end
--
-- load(depth)
-- B()
-- recursion(depth-1,0)
-- if depth == top_depth and best > top_best then
-- top_best = best
-- top_action = 0
-- end
-- end
--end
top_previous = 2
function run()
if previous == 2 then
move(1,0)
previous = 1
else
move(2,0)
previous = 2
end
--current = current + memory.read_u8(0x2CB)
end
function move(input, save)
if input == 0 then
if save == 1 then
file = io.open(outfile,"a")
file:write("|..|........|............|\n")
file:close()
end
joypad.setfrommnemonicstr(string.format("|..|........|............|"))
emu.frameadvance()
elseif input == 1 then
if save == 1 then
file = io.open(outfile,"a")
file:write("|..|........|.1..........|\n")
file:close()
end
joypad.setfrommnemonicstr(string.format("|..|........|.1..........|"))
emu.frameadvance()
elseif input == 2 then
if save == 1 then
file = io.open(outfile,"a")
file:write("|..|........|2...........|\n")
file:close()
end
joypad.setfrommnemonicstr(string.format("|..|........|2...........|"))
emu.frameadvance()
elseif input == 3 then
if save == 1 then
file = io.open(outfile,"a")
file:write("|..|........|12...........|\n")
file:close()
end
joypad.setfrommnemonicstr(string.format("|..|........|12..........|"))
emu.frameadvance()
end
end
while 1 do
best_i = 0
best_j = 0
best_k = 0
best_l = 0
best = 0
previous = top_previous
for i=0,0,1 do
load(0)
move(i,0)
save(1)
for j=0,3,1 do
load(1)
move(j,0)
save(2)
for k=0,3,1 do
load(2)
move(k,0)
save(3)
for l=0,3,1 do
load(3)
move(l,0)
distance = 0
for m=0,251,1 do
run()
distance = distance + memory.read_u8(0x2CB)
end
if distance > best then
best_i = i
best_j = j
best_k = k
best_l = l
best = distance
end
end
end
end
end
--write results
print("frame " .. emu.framecount() .. " best " .. best)
file = io.open(outfile,"w")
file:write(best)
file:close()
--wait for them to clear
value = ""
while value ~= "clear" do
file = io.open(outfile,"r")
value = file:read()
file:close()
client.sleep(1000)
end
--read the new previous
file = io.open(new_previous,"r")
top_previous = file:read()
file:close()
--print("frame " .. emu.framecount())
--load(0)
--move(best_i,1)
--save(0)
--if best_i == 1 then
-- top_previous = 1
--elseif best_i == 2 then
-- top_previous = 2
--end
end
--while 1 do
---- recursion(top_depth, previous)
----
---- load(top_depth + 1)
----
---- file = io.open(outfile,"a")
---- output = "frame: " .. emu.framecount() .. " " .. top_action .. "\n"
---- file:write(output)
---- file:close()
----
---- if top_action == 2 then
---- R()
---- elseif top_action == 1 then
---- L()
---- else
---- B()
---- end
---- save(top_depth + 1)
----
---- previous = top_action
--
-- best = 0
-- best_i = 0
-- nest_j = 0
--
-- load(0)
-- previous = top_previous
--
-- for i=0,9,1 do
-- --print("i=" .. i)
-- load(0)
-- current = 0
-- previous = top_previous
--
-- for k=0,i-1,1 do
-- run()
-- end
--
-- B()
-- save(1)
-- current_i = current
-- previous_i = previous
--
-- for j=i+1,10,1 do
-- --print("j=" .. j)
-- load(1)
-- current = current_i
-- previous = previous_i
--
-- for k=i+1,j-1,1 do
-- run()
-- end
--
-- B()
--
-- for k=j+1,127,1 do
-- run()
-- end
--
-- if current >= best then
-- best = current
-- best_i = i
-- best_j = j
-- end
-- end
-- end
--
-- load(0)
-- previous = top_previous
--
-- for k=0,1,1 do
-- file = io.open(outfile,"a")
-- output = "frame: " .. emu.framecount() .. " "
--
-- if k == best_i or k == best_j then
-- B()
-- output = output .. "|..|........|............|\n"
-- else
-- run()
-- if previous == 1 then
-- output = output .. "|..|........|.1..........|\n"
-- else
-- output = output .. "|..|........|2...........|\n"
-- end
-- end
--
-- file:write(output)
-- file:close()
-- end
--
-- save(0)
-- top_previous = previous
--
--end
client.pause()