User File #63810678495897487

Upload All User Files

#63810678495897487 -

stadium_events_test12.lua
Game: Stadium Events ( NES, see all files )
290 downloads
Uploaded 6/4/2020 5:09 PM 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

path = "C:\\Users\\pgrimsrud\\NES\\State\\stadium_events"
path2 = "C:\\Users\\pgrimsrud\\NES\\State\\stadium_events4"
outfile = "C:\\Users\\pgrimsrud\\NES\\notes\\stadium_events.txt"
results_base = "C:\\Users\\pgrimsrud\\NES\\notes\\stadium_events"
results1 = results_base .. "1.txt"
results2 = results_base .. "2.txt"
results3 = results_base .. "3.txt"
result_base = "C:\\Users\\pgrimsrud\\NES\\notes\\stadium_events"
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
    state = memory.read_u8(0x58)
    change = 0
    last_two = 0
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)
    --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)
    else
        move(0,0)
    end

    if bit.band(index, 0x20) == 0x20 then
        move(2,0)
    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)
        last_two = 1
    else
        move(0,0)
    end

    if bit.band(index, 0x800) == 0x800 then
        move(2,0)
        last_two = 2
    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()