Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local frame = 0
- local run = true
- local load_state = true
- local death_timer = 0
- local controller_seed = 987654321
- local first = true
- local total_tiles = 0
- local max_x = 0
- local c_a, c_b, c_x, c_y, c_u, c_d, c_l, c_r
- local p_a, p_b, p_x, p_y, p_u, p_d, p_l, p_r
- function randomly_flip(is_now_pressed, chance_of_press, chance_of_release)
- local rand = math.random()
- if is_now_pressed then
- if rand >= chance_of_release then
- return true
- end
- else
- if rand < chance_of_press then
- return true
- end
- end
- return nil
- end
- function do_controller_input()
- local input = joypad.get(1)
- math.randomseed(controller_seed + 37 * frame)
- c_u = randomly_flip(p_u, 0.01, 0.1)
- c_d = randomly_flip(p_d, 0.005, 0.1)
- c_l = randomly_flip(p_l, 0.002, 0.05)
- c_r = randomly_flip(p_r, 0.008, 0.001)
- c_a = randomly_flip(p_a, 0.005, 0.01)
- c_b = randomly_flip(p_b, 0.01, 0.01)
- c_x = randomly_flip(p_x, 0.005, 0.008)
- c_y = randomly_flip(p_y, 0.001, 0.01)
- if c_u then c_d = nil end
- if c_l then c_r = nil end
- input["Up"] = c_u
- input["Down"] = c_d
- input["Left"] = c_l
- input["Right"] = c_r
- input["A"] = c_a
- input["B"] = c_b
- input["X"] = c_x
- input["Y"] = c_y
- p_u = c_u
- p_d = c_d
- p_l = c_l
- p_r = c_r
- p_a = c_a
- p_b = c_b
- p_x = c_x
- p_y = c_y
- joypad.set(input, 1)
- end
- function mario_is_finished()
- return false
- end
- function mario_is_dead()
- if memory.readbyte(0x71) == 9 then
- death_timer = death_timer + 1
- end
- return death_timer > 60
- end
- function mario_is_stuck()
- local mario_x = memory.read_s16_le(0x94)
- if mario_x > max_x then max_x = mario_x end
- return mario_x < (frame / 2) - 200
- end
- function get_address(x, y)
- if y > 27 or x > 512 then return -1 end
- local address = 0
- address = address + 0x10 * (y - 1)
- local screen = math.floor((x - 1) / 0x10)
- address = address + ((x - 1) % 0x10)
- address = address + 0x1B0 * screen
- address = address + 0xC800
- return address
- end
- function get_tile_num(tile_type)
- if tile_type == "solid" then return 0x130
- elseif tile_type == "coin" then return 0x2B
- elseif tile_type == "climb" then return 0x6
- elseif tile_type == "bounce" then return 0x116
- elseif tile_type == "cloud" then return 0x106
- elseif tile_type == "powerup" then return 0x120
- elseif tile_type == "throw" then return 0x12E
- elseif tile_type == "muncher" then return 0x12F
- elseif tile_type == "moon" then return 0x6E
- elseif tile_type == "water" then return 0x2
- elseif tile_type == "turn" then return 0x11E
- elseif tile_type == "kaizo" then return 0x21
- elseif tile_type == "air" then return 0x25
- else return -1 end
- end
- function valid_tile(address, x, y, tile_number)
- if x == 2 and (y == 24 or y == 25) then
- return false
- else
- return tile_number > 0 and address > 0
- end
- -- return address > 0 and memory.readbyte(address) == 0x25 and memory.readbyte(0x10000 + address) == 0
- end
- function set_tile(entry)
- -- <username, tile type, x-pos, y-pos>
- if string.len(entry) < 4 then
- return
- end
- local username = string.sub(entry, 1, string.find(entry, ",") - 1)
- entry = string.sub(entry, string.find(entry, ",") + 1)
- local tile_type = string.sub(entry, 1, string.find(entry, ",") - 1)
- entry = string.sub(entry, string.find(entry, ",") + 1)
- local x_pos = tonumber(string.sub(entry, 1, string.find(entry, ",") - 1))
- y_pos = tonumber(string.sub(entry, string.find(entry, ",") + 1))
- local address = get_address(x_pos, y_pos)
- local tile_number = get_tile_num(tile_type)
- if valid_tile(address, x_pos, y_pos, tile_number) then
- memory.writebyte(address, tile_number % 0x100)
- memory.writebyte(0x10000 + address, math.floor(tile_number / 0x100))
- total_tiles = total_tiles + 1
- end
- end
- function draw_graphics()
- local x_offset = memory.read_s16_le(0x1A)
- local y_offset = memory.read_s16_le(0x1C)
- local starting_x_tile = math.floor(x_offset / 0x10)
- local pixel_x_offset = x_offset % 0x10
- for i=0,0x10 do
- local x = (0x10 * i) - pixel_x_offset
- local number = starting_x_tile + i + 1
- gui.drawString(x + 4, 191, math.floor(number / 100) % 10, 0xC0200000)
- gui.drawString(x + 4, 201, math.floor(number / 10) % 10, 0xC0200000)
- gui.drawString(x + 4, 211, number % 10, 0xC0200000)
- gui.drawString(x + 3, 190, math.floor(number / 100) % 10, 0xFFFFFFFF)
- gui.drawString(x + 3, 200, math.floor(number / 10) % 10, 0xFFFFFFFF)
- gui.drawString(x + 3, 210, number % 10, 0xFFFFFFFF)
- gui.drawLine(x, 0, x, 250, 0x50FFFFFF)
- end
- local starting_y_tile = math.floor(y_offset / 0x10)
- local pixel_y_offset = y_offset % 0x10
- for i=0,0xB do
- local y = (0x10 * i) - pixel_y_offset - 1
- gui.drawString(4, y + 1, starting_y_tile + i + 1, 0xC0200000)
- gui.drawString(3, y, starting_y_tile + i + 1, 0xFFFFFFFF)
- gui.drawLine(0, y, 280, y, 0x50FFFFFF)
- end
- gui.drawString(151, 31, total_tiles, 0xC0200000)
- gui.drawString(150, 30, total_tiles, 0xFFFFFFFF)
- local boundary = (frame / 2) - 200 - x_offset
- gui.drawLine(boundary, 0, boundary, 280, 0x50FF0000)
- local maximum = max_x - x_offset + 12
- gui.drawLine(maximum, 0, maximum, 280, 0x500000FF)
- --gui.drawString(50,50,"x_offset: " .. x_offset)
- --gui.drawString(50,80,"y_offset: " .. y_offset)
- end
- -- Start Main --
- console.writeline("Starting...")
- savestate.load("MarioCreatorTest.State")
- while run do
- frame = frame + 1
- do_controller_input()
- if first or mario_is_dead() or mario_is_stuck() then
- p_u = nil
- p_d = nil
- p_l = nil
- p_r = nil
- p_a = nil
- p_b = nil
- p_x = nil
- p_y = nil
- death_timer = 0
- frame = 0
- total_tiles = 0
- savestate.load("MarioCreatorTest.State")
- local file = io.open("MarioCreatorTestBlocks.txt","r")
- local input = {}
- for line in file:lines() do
- table.insert(input, line)
- end
- file:close()
- local indices = io.open("MarioCreatorTestIndices.txt","r")
- local index = {}
- for line in indices:lines() do
- table.insert(index, tonumber(line))
- end
- indices:close()
- total_entries = table.getn(index)
- for i=1,total_entries do
- set_tile(input[index[i]])
- end
- end
- draw_graphics()
- first = false;
- run = not mario_is_finished()
- emu.frameadvance()
- end
- console.writeline("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement