User File #63822150986891162

Upload All User Files

#63822150986891162 -

stadium_events_new_parallel.lua
Game: Stadium Events ( NES, see all files )
339 downloads
Uploaded 6/5/2020 5:33 AM by link_7777 (see all 28)
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
local state
local change
local last_two
local instance

register_path = "C:\\Users\\pgrimsrud\\NES\\notes\\stadium_events_register.txt"

file = io.open(register_path,"r")
num = file:read()
instance = tonumber(num)
file:close()

if instance == 8 then
    instance = 1
else
    instance = instance + 1
end

file = io.open(register_path,"w")
file:write(instance)
file:close()

path_base = "C:\\Users\\pgrimsrud\\NES\\State\\stadium_events"
path = path_base .. instance ..".State"

outfile_base = "C:\\Users\\pgrimsrud\\NES\\notes\\stadium_events"
outfile = outfile_base .. instance .. ".txt"

print("instance " .. instance .. "\n")
file = io.open(outfile,"w")
file:write("instance " .. instance .. "\n")
file:close()

savestate.load(path_base .. "0.State")

for i=0,instance-2,1 do
    move(0,1)
end

savestate.save(path)


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)
    savestate.load(path)
    state = memory.read_u8(0x58)
    change = 0
    last_two = 0
end

function save(num)
    savestate.save(path)
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)
    --print("move " .. input)
    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

    new_state = memory.read_u8(0x58)
    if new_state == 1 or
       new_state == 2 or
       new_state == 3 or
       new_state == 5 or
       new_state == 6 or
       new_state == 7 then
        if new_state ~= state then
            change = change + 1
            --print("change " .. change)
            state = new_state
        end
    end
end

function move_real(index)
    --move(0,1)
    --move(0,1)
    if bit.band(index, 0x1) == 0x1 then
        move(2,1)
    else
        move(0,1)
    end
    --move(0,1)
    --move(0,1)
    if bit.band(index, 0x2) == 0x2 then
        move(2,1)
    else
        move(0,1)
    end
    --move(0,1)
    --move(0,1)
    if bit.band(index, 0x4) == 0x4 then
        move(2,1)
    else
        move(0,1)
    end
    --move(0,1)
    --move(0,1)
    --if bit.band(index, 0x8) == 0x8 then
    --    move(2,1)
    --else
    --    move(0,1)
    --end
end

function move_test(index)
    if bit.band(index, 0x1) == 0x1 then
        move(2,0)
    else
        move(0,0)
    end

    if bit.band(index, 0x2) == 0x2 then
        move(2,0)
    else
        move(0,0)
    end

    if bit.band(index, 0x4) == 0x4 then
        move(2,0)
    else
        move(0,0)
    end

    if bit.band(index, 0x8) == 0x8 then
        move(2,0)
    else
        move(0,0)
    end

    if bit.band(index, 0x10) == 0x10 then
        move(2,0)
        last_two = 1
    else
        move(0,0)
    end

    if bit.band(index, 0x20) == 0x20 then
        move(2,0)
        last_two = 2
    else
        move(0,0)
    end

    if bit.band(index, 0x40) == 0x40 then
        move(2,0)
    else
        move(0,0)
    end

    if bit.band(index, 0x80) == 0x80 then
        move(2,0)
    else
        move(0,0)
    end

    if bit.band(index, 0x100) == 0x100 then
        move(2,0)
    else
        move(0,0)
    end

    if bit.band(index, 0x200) == 0x200 then
        move(2,0)
    else
        move(0,0)
    end

    if bit.band(index, 0x400) == 0x400 then
        move(2,0)
    else
        move(0,0)
    end

    if bit.band(index, 0x800) == 0x800 then
        move(2,0)
    else
        move(0,0)
    end
end

while 1 do
    best_i = 0
    best = 0
    previous = top_previous
    for i=0,4095,1 do
        load(0)
        move_test(i)

        distance = 0
        for m=0,5,1 do
            if last_two == 0 then
                move(2,0)
                move(0,0)
                move(0,0)
            end
            if last_two == 1 then
                move(0,0)
                move(2,0)
                move(0,0)
            end
            if last_two == 2 then
                move(0,0)
                move(0,0)
                move(2,0)
            end
        end

        if change > best then
            best_i = i
            best = change
        end
    end

    load(0)
    move_real(best_i)
    save(0)
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()