Advertisement
Guest User

Untitled

a guest
Oct 24th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.19 KB | None | 0 0
  1.  
  2. local display_solution = false
  3. local auto_solve = true
  4.  
  5.  
  6. --beware
  7. --lots of spaghetti code follows below this line
  8.  
  9. local pic_data = {{}}
  10. local pic_size_x
  11. local pic_size_y
  12. local base_x = {[5] = 139, [10] = 121, [15] = 104, [20] = 87, [25] = 69}
  13. local base_y = {[5] = 123, [10] = 105, [15] = 88, [20] = 71, [25] = 71}
  14. local drop_nth_frame_per_level_id = {[105]=50, [108] = 150, [118] = 20, [119] = 220, [126] = 10, [130] = 200, [225] = 60, [255] = 33, [264]= 140, [279] = 255, [284] = 200}
  15. local box_size = 7
  16. local frame_delay = 1
  17. local x_pos = 0
  18. local y_pos = 0
  19. local status
  20. local pic_done
  21. local need_read = true
  22. local read_now = false
  23. local marked = false
  24. local prev_right = false
  25. local prev_left = false
  26. local prev_up = false
  27. local prev_down = false
  28. local prev_A = false
  29. local prev_Y = false
  30. local prev_A2 = false
  31. local prev_Y2 = false
  32. local course_corrected = false
  33. local sol_frame = 0
  34. local solve_done = false
  35. local moved = false
  36. local to_move_down = 2
  37. local level_id = -1
  38.  
  39.  
  40. gui.transparency(2)
  41.  
  42. function draw_gui()
  43.     if need_read == false and display_solution then
  44.         for i=0,pic_size_x do
  45.             for j=0,pic_size_y do
  46.                 if pic_data[i][j] % 2 == 1 then
  47.                     local x = base_x[pic_size_x]+box_size*i
  48.                     local y = base_y[pic_size_x]+box_size*j
  49.                     gui.drawbox(x,y,x+box_size,y+box_size,"#cccc00","#ffff00")
  50.                 end
  51.             end
  52.         end
  53.     end
  54. end
  55. gui.register(draw_gui)
  56.  
  57.  
  58. while true do
  59.     pic_done = memory.readbyte(0x7e0037) --done if 3, fade if 0
  60.     status = memory.readbyte(0x7e119e)
  61.     if status == 4 and need_read and pic_done ~= 0 then
  62.         read_now = true
  63.     else
  64.         read_now = false
  65.     end
  66.     if status ~= 4 or pic_done == 0 then
  67.         need_read = true
  68.     end
  69.     if need_read == true and read_now == true then
  70.         pic_size_x = memory.readbyte(0x7e1110)
  71.         pic_size_y = memory.readbyte(0x7e1112)
  72.         level_id = memory.readword(0x7e0e46)
  73.                            
  74.         for i=0,pic_size_x do
  75.             for j=0,pic_size_y do
  76.                 if pic_data[i] == nil then
  77.                     pic_data[i] = {}
  78.                 end
  79.                 pic_data[i][j] = memory.readbyte(0x7e1234+i+j*0x20)
  80.  
  81.             end
  82.         end
  83.         need_read = false
  84.         wait_frames = frame_delay
  85.         sol_frame = 0
  86.         x_pos = memory.readbyte(0x7e11d2)
  87.         y_pos = memory.readbyte(0x7e11d6)
  88.         --x_pos = 0
  89.         --y_pos = 0
  90.         prev_left = false
  91.         prev_right = false
  92.         prev_A = false
  93.         prev_Y = false
  94.         solve_done = false
  95.         course_corrected = false
  96.         to_move_down = 1
  97.     end
  98.     if need_read == false and solve_done == false and auto_solve == true then
  99.         if wait_frames == frame_delay and (x_pos ~= 0 or y_pos ~=0) then
  100.             local but = {}
  101.             if x_pos ~= 0 then
  102.                 but["left"] = true
  103.                 x_pos = x_pos - 1
  104.             end
  105.             if y_pos ~= 0 then
  106.                 but["up"] = true
  107.                 y_pos = y_pos - 1
  108.             end
  109.             joypad.set(1,but)
  110.             wait_frames = frame_delay + 2
  111.         end
  112.         if wait_frames == 1 then
  113.             local buttons3 = {}
  114.             buttons3["L"] = true
  115.             joypad.set(2,buttons3)
  116.         end
  117.         if wait_frames <= 0 then
  118.             local buttons = {}
  119.             local buttons2 = {}
  120.             if sol_frame == 0 then
  121.                 if pic_data[x_pos][y_pos] % 4 == 1 then
  122.                     buttons["A"] = true
  123.                     prev_A = true
  124.                 end
  125.                 if pic_data[pic_size_x-1-x_pos][pic_size_y-1-y_pos] % 4 == 1 then
  126.                     buttons2["A"] = true
  127.                     prev_A2 = true
  128.                 end
  129.             elseif drop_nth_frame_per_level_id[level_id] ~= sol_frame then
  130.                 moved = true
  131.                 local dir = (math.floor(y_pos/2)%2-0.5)*(-2)
  132.                 if prev_left == false and prev_right == false then
  133.                     if x_pos == pic_size_x - 1 or x_pos == 0 then
  134.                         if y_pos < pic_size_y/2 then
  135.                             if prev_down == true then
  136.                                 if to_move_down == -1 then
  137.                                     if (y_pos == math.floor(pic_size_y/2) - 2 and pic_size_y == 10) or y_pos == math.floor(pic_size_y/2) - 1 then
  138.                                         to_move_down = 1
  139.                                     else
  140.                                         to_move_down = 2
  141.                                     end
  142.                                 end
  143.                                 if to_move_down == 0 then
  144.                                     prev_down = false
  145.                                     if dir > 0 then
  146.                                         buttons["right"] = true
  147.                                         prev_right = true
  148.                                         prev_left = false
  149.                                     else
  150.                                         buttons["left"] = true
  151.                                         prev_left = true
  152.                                         prev_right = false
  153.                                     end
  154.                                     x_pos = x_pos + dir
  155.                                     to_move_down = -1
  156.                                 else
  157.                                     moved = false
  158.                                     prev_down = false
  159.                                 end
  160.                             else
  161.                                 buttons["down"] = true
  162.                                 prev_down = true
  163.                                 course_corrected = false
  164.                                 y_pos = y_pos + 1
  165.                                 to_move_down = to_move_down - 1
  166.                             end
  167.                         else
  168.                             solve_done = true
  169.                         end
  170.                     else
  171.                         if dir > 0 then
  172.                             buttons["right"] = true
  173.                             prev_right = true
  174.                             prev_left = false
  175.                         else
  176.                             buttons["left"] = true
  177.                             prev_left = true
  178.                             prev_right = false
  179.                         end
  180.                         if course_corrected == false and pic_size_x %2 == 0 and not (pic_size_y / 2 == y_pos + 1) then
  181.                             if prev_up == true then
  182.                                 buttons["down"] = true
  183.                                 prev_down = true
  184.                                 prev_up = false
  185.                                 y_pos = y_pos + 1
  186.                             else
  187.                                 buttons["up"] = true
  188.                                 prev_up = true
  189.                                 prev_down = false
  190.                                 y_pos = y_pos - 1
  191.                             end
  192.                             course_corrected = true
  193.                         else
  194.                             prev_up = false
  195.                             prev_down = false
  196.                         end
  197.                         x_pos = x_pos + dir
  198.                     end
  199.                 else
  200.                     prev_up = false
  201.                     prev_left = false
  202.                     prev_right = false
  203.                     prev_down = false
  204.                     if not (pic_size_y == 10 and y_pos + 1 == pic_size_y / 2) and not (pic_size_y == 5 and y_pos == math.floor(pic_size_y/2)) then
  205.                         if y_pos % 2 == 0  then
  206.                             buttons["down"] = true
  207.                             prev_down = true
  208.                             y_pos = y_pos + 1
  209.                         else
  210.                             buttons["up"] = true
  211.                             prev_up = true
  212.                             y_pos = y_pos - 1
  213.                         end
  214.                     else
  215.                         prev_up = true
  216.                         moved = false
  217.                     end
  218.                 end
  219.                
  220.                 if buttons["left"] == true then
  221.                     buttons2["right"] = true
  222.                 end
  223.                 if buttons["right"] == true then
  224.                     buttons2["left"] = true
  225.                 end
  226.                 if buttons["up"] == true then
  227.                     buttons2["down"] = true
  228.                 end
  229.                 if buttons["down"] == true then
  230.                     buttons2["up"] = true
  231.                 end
  232.                 --print(x_pos,y_pos, " ",pic_size_x-1-x_pos,pic_size_y-1-y_pos)
  233.                
  234.                 if moved == true and pic_data[x_pos][y_pos] % 4 == 1 then
  235.                     if prev_A then
  236.                         buttons["Y"] = true
  237.                         prev_Y = true
  238.                         prev_A = false
  239.                     else
  240.                         buttons["A"] = true
  241.                         prev_A = true
  242.                         prev_Y = false
  243.                     end
  244.                 else
  245.                     prev_A = false
  246.                     prev_Y = false         
  247.                 end
  248.                 local spaghetti_code_y_pos = y_pos
  249.                 local spaghetti_code_moved = moved
  250.                 if (pic_size_y == 15 and y_pos + 2 > math.floor(pic_size_y/2)) then
  251.                     if (buttons["up"] == true or buttons["down"] == true) and to_move_down ~= 0 then
  252.                         buttons2["down"] = nil
  253.                         buttons2["up"] = nil   
  254.                         spaghetti_code_moved = false
  255.                     end
  256.                     spaghetti_code_y_pos = 6
  257.                 end
  258.                 if spaghetti_code_moved == true and pic_data[pic_size_x-1-x_pos][pic_size_y-1-spaghetti_code_y_pos] % 4 == 1 then
  259.                     if prev_A2 then
  260.                         buttons2["Y"] = true
  261.                         prev_Y2 = true
  262.                         prev_A2 = false
  263.                     else
  264.                         buttons2["A"] = true
  265.                         prev_Y2 = false
  266.                         prev_A2 = true
  267.                     end
  268.                 else
  269.                     prev_A2 = false
  270.                     prev_Y2 = false
  271.                 end
  272.             end
  273.             sol_frame = sol_frame + 1
  274.             joypad.set(1,buttons)
  275.             joypad.set(2,buttons2)
  276.         else
  277.             wait_frames = wait_frames - 1
  278.         end
  279.     end
  280.     snes9x.frameadvance()
  281. end
  282.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement