Advertisement
Dotsarecool

Untitled

Jul 16th, 2015
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. local frame = 0
  2. local run = true
  3. local load_state = true
  4. local death_timer = 0
  5. local controller_seed = 987654321
  6. local first = true
  7. local total_tiles = 0
  8. local max_x = 0
  9.  
  10. local c_a, c_b, c_x, c_y, c_u, c_d, c_l, c_r
  11. local p_a, p_b, p_x, p_y, p_u, p_d, p_l, p_r
  12.  
  13. function randomly_flip(is_now_pressed, chance_of_press, chance_of_release)
  14. local rand = math.random()
  15. if is_now_pressed then
  16. if rand >= chance_of_release then
  17. return true
  18. end
  19. else
  20. if rand < chance_of_press then
  21. return true
  22. end
  23. end
  24. return nil
  25. end
  26.  
  27. function do_controller_input()
  28. local input = joypad.get(1)
  29.  
  30. math.randomseed(controller_seed + 37 * frame)
  31.  
  32. c_u = randomly_flip(p_u, 0.01, 0.1)
  33. c_d = randomly_flip(p_d, 0.005, 0.1)
  34. c_l = randomly_flip(p_l, 0.002, 0.05)
  35. c_r = randomly_flip(p_r, 0.008, 0.001)
  36. c_a = randomly_flip(p_a, 0.005, 0.01)
  37. c_b = randomly_flip(p_b, 0.01, 0.01)
  38. c_x = randomly_flip(p_x, 0.005, 0.008)
  39. c_y = randomly_flip(p_y, 0.001, 0.01)
  40.  
  41. if c_u then c_d = nil end
  42. if c_l then c_r = nil end
  43.  
  44. input["Up"] = c_u
  45. input["Down"] = c_d
  46. input["Left"] = c_l
  47. input["Right"] = c_r
  48. input["A"] = c_a
  49. input["B"] = c_b
  50. input["X"] = c_x
  51. input["Y"] = c_y
  52.  
  53. p_u = c_u
  54. p_d = c_d
  55. p_l = c_l
  56. p_r = c_r
  57. p_a = c_a
  58. p_b = c_b
  59. p_x = c_x
  60. p_y = c_y
  61.  
  62. joypad.set(input, 1)
  63. end
  64.  
  65. function mario_is_finished()
  66. return false
  67. end
  68.  
  69. function mario_is_dead()
  70. if memory.readbyte(0x71) == 9 then
  71. death_timer = death_timer + 1
  72. end
  73. return death_timer > 60
  74. end
  75.  
  76. function mario_is_stuck()
  77. local mario_x = memory.read_s16_le(0x94)
  78. if mario_x > max_x then max_x = mario_x end
  79. return mario_x < (frame / 2) - 200
  80. end
  81.  
  82. function get_address(x, y)
  83. if y > 27 or x > 512 then return -1 end
  84. local address = 0
  85. address = address + 0x10 * (y - 1)
  86. local screen = math.floor((x - 1) / 0x10)
  87. address = address + ((x - 1) % 0x10)
  88. address = address + 0x1B0 * screen
  89. address = address + 0xC800
  90. return address
  91. end
  92.  
  93. function get_tile_num(tile_type)
  94. if tile_type == "solid" then return 0x130
  95. elseif tile_type == "coin" then return 0x2B
  96. elseif tile_type == "climb" then return 0x6
  97. elseif tile_type == "bounce" then return 0x116
  98. elseif tile_type == "cloud" then return 0x106
  99. elseif tile_type == "powerup" then return 0x120
  100. elseif tile_type == "throw" then return 0x12E
  101. elseif tile_type == "muncher" then return 0x12F
  102. elseif tile_type == "moon" then return 0x6E
  103. elseif tile_type == "water" then return 0x2
  104. elseif tile_type == "turn" then return 0x11E
  105. elseif tile_type == "kaizo" then return 0x21
  106. elseif tile_type == "air" then return 0x25
  107. else return -1 end
  108. end
  109.  
  110. function valid_tile(address, x, y, tile_number)
  111. if x == 2 and (y == 24 or y == 25) then
  112. return false
  113. else
  114. return tile_number > 0 and address > 0
  115. end
  116. -- return address > 0 and memory.readbyte(address) == 0x25 and memory.readbyte(0x10000 + address) == 0
  117. end
  118.  
  119. function set_tile(entry)
  120. -- <username, tile type, x-pos, y-pos>
  121. if string.len(entry) < 4 then
  122. return
  123. end
  124.  
  125. local username = string.sub(entry, 1, string.find(entry, ",") - 1)
  126. entry = string.sub(entry, string.find(entry, ",") + 1)
  127. local tile_type = string.sub(entry, 1, string.find(entry, ",") - 1)
  128. entry = string.sub(entry, string.find(entry, ",") + 1)
  129. local x_pos = tonumber(string.sub(entry, 1, string.find(entry, ",") - 1))
  130. y_pos = tonumber(string.sub(entry, string.find(entry, ",") + 1))
  131.  
  132. local address = get_address(x_pos, y_pos)
  133. local tile_number = get_tile_num(tile_type)
  134.  
  135. if valid_tile(address, x_pos, y_pos, tile_number) then
  136. memory.writebyte(address, tile_number % 0x100)
  137. memory.writebyte(0x10000 + address, math.floor(tile_number / 0x100))
  138. total_tiles = total_tiles + 1
  139. end
  140. end
  141.  
  142. function draw_graphics()
  143. local x_offset = memory.read_s16_le(0x1A)
  144. local y_offset = memory.read_s16_le(0x1C)
  145.  
  146. local starting_x_tile = math.floor(x_offset / 0x10)
  147. local pixel_x_offset = x_offset % 0x10
  148.  
  149. for i=0,0x10 do
  150. local x = (0x10 * i) - pixel_x_offset
  151. local number = starting_x_tile + i + 1
  152. gui.drawString(x + 4, 191, math.floor(number / 100) % 10, 0xC0200000)
  153. gui.drawString(x + 4, 201, math.floor(number / 10) % 10, 0xC0200000)
  154. gui.drawString(x + 4, 211, number % 10, 0xC0200000)
  155. gui.drawString(x + 3, 190, math.floor(number / 100) % 10, 0xFFFFFFFF)
  156. gui.drawString(x + 3, 200, math.floor(number / 10) % 10, 0xFFFFFFFF)
  157. gui.drawString(x + 3, 210, number % 10, 0xFFFFFFFF)
  158. gui.drawLine(x, 0, x, 250, 0x50FFFFFF)
  159. end
  160.  
  161. local starting_y_tile = math.floor(y_offset / 0x10)
  162. local pixel_y_offset = y_offset % 0x10
  163.  
  164. for i=0,0xB do
  165. local y = (0x10 * i) - pixel_y_offset - 1
  166. gui.drawString(4, y + 1, starting_y_tile + i + 1, 0xC0200000)
  167. gui.drawString(3, y, starting_y_tile + i + 1, 0xFFFFFFFF)
  168. gui.drawLine(0, y, 280, y, 0x50FFFFFF)
  169. end
  170.  
  171. gui.drawString(151, 31, total_tiles, 0xC0200000)
  172. gui.drawString(150, 30, total_tiles, 0xFFFFFFFF)
  173.  
  174. local boundary = (frame / 2) - 200 - x_offset
  175. gui.drawLine(boundary, 0, boundary, 280, 0x50FF0000)
  176. local maximum = max_x - x_offset + 12
  177. gui.drawLine(maximum, 0, maximum, 280, 0x500000FF)
  178.  
  179. --gui.drawString(50,50,"x_offset: " .. x_offset)
  180. --gui.drawString(50,80,"y_offset: " .. y_offset)
  181. end
  182.  
  183. -- Start Main --
  184.  
  185. console.writeline("Starting...")
  186. savestate.load("MarioCreatorTest.State")
  187.  
  188. while run do
  189. frame = frame + 1
  190. do_controller_input()
  191.  
  192. if first or mario_is_dead() or mario_is_stuck() then
  193. p_u = nil
  194. p_d = nil
  195. p_l = nil
  196. p_r = nil
  197. p_a = nil
  198. p_b = nil
  199. p_x = nil
  200. p_y = nil
  201.  
  202. death_timer = 0
  203. frame = 0
  204. total_tiles = 0
  205. savestate.load("MarioCreatorTest.State")
  206.  
  207. local file = io.open("MarioCreatorTestBlocks.txt","r")
  208. local input = {}
  209. for line in file:lines() do
  210. table.insert(input, line)
  211. end
  212. file:close()
  213.  
  214. local indices = io.open("MarioCreatorTestIndices.txt","r")
  215. local index = {}
  216. for line in indices:lines() do
  217. table.insert(index, tonumber(line))
  218. end
  219. indices:close()
  220.  
  221. total_entries = table.getn(index)
  222. for i=1,total_entries do
  223. set_tile(input[index[i]])
  224. end
  225. end
  226.  
  227. draw_graphics()
  228. first = false;
  229.  
  230. run = not mario_is_finished()
  231.  
  232. emu.frameadvance()
  233. end
  234.  
  235. console.writeline("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement