User File #63751684207465286

Upload All User Files

#63751684207465286 -

stadium_events_new.lua
Game: Stadium Events ( NES, see all files )
347 downloads
Uploaded 6/2/2020 1:23 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

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
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

function move_real(index)
    if bit.band(index, 0x1) == 0x1 then
        move(2,1)
    else
        move(0,1)
    end
    if bit.band(index, 0x2) == 0x2 then
        move(2,1)
    else
        move(0,1)
    end
    if bit.band(index, 0x4) == 0x4 then
        move(2,1)
    else
        move(0,1)
    end
    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
end

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

        distance = 0
        for m=0,17,1 do
            move(0,0)
            distance = distance + memory.read_u8(0x2CB)
            move(0,0)
            distance = distance + memory.read_u8(0x2CB)
            move(2,0)
            distance = distance + memory.read_u8(0x2CB)
        end

        if distance > best then
            best_i = i
            best = distance
        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()