Advertisement
Dessyreqt

Mega Man 3 Practice script

Jun 21st, 2014
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. --How many frames ahead of the "action" to start savestates
  2. preFrames = 45
  3.  
  4. --Game-specific RAM addresses
  5. RAM = {
  6.     bytes = {
  7.         inGame = 0x00B2, --128 if in game
  8.         curPart = 0x002B,  --RoomNumber
  9.         lives = 0x00AE
  10.     }
  11. }
  12.  
  13. --Game-specific information that needs to be monitored
  14. gameInfo = {
  15.     lives = 0,
  16.     curPart = 0
  17. }
  18.  
  19. --Locations to output text
  20. output = {
  21.     time = {
  22.         x = 10,
  23.         y = 210
  24.     }
  25. }
  26.  
  27. --Tells the script when to do nothing regarding savestates
  28. function DoNothing()
  29.     if (memory.readbyte(RAM.bytes.inGame) ~= 128) then
  30.         return true
  31.     end
  32.     return false
  33. end
  34.  
  35. --Sets game information at the beginning of an attempt
  36. function SetGameInfo()
  37.     gameInfo.curPart = memory.readbyte(RAM.bytes.curPart);
  38.     gameInfo.Lives = memory.readbyte(RAM.bytes.lives);
  39. end
  40.  
  41. --Outputs relevant game information
  42. function OutputGameInfo()
  43. end
  44.  
  45. --Tells the script that the attempt should continue
  46. function ContinueAttempt()
  47.     if gameInfo.curPart == memory.readbyte(RAM.bytes.curPart) and (memory.readbyte(RAM.bytes.inGame) == 128) then
  48.         return true
  49.     end
  50.     return false
  51. end
  52.  
  53. --Use this function to update info that may be checked later (for example, if you gain a life)
  54. function UpdateGameInfo()
  55.     if (memory.readbyte(RAM.bytes.lives) > gameInfo.Lives) then
  56.         gameInfo.Lives = memory.readbyte(RAM.bytes.lives);                      
  57.     end
  58. end
  59.  
  60. --Tells the script that the last segment was okay (i.e. you didn't die)
  61. function SegmentOk()
  62.     if gameInfo.Lives <= memory.readbyte(RAM.bytes.lives) and gameInfo.curPart <= memory.readbyte(RAM.bytes.curPart) then
  63.         return true
  64.     end
  65.     return false
  66. end
  67.  
  68. --Handles all the hotkey stuff
  69. function HandleKeys()
  70.     keys = input.get()
  71.  
  72.     if press('Q') then
  73.         segment.done = true
  74.     end
  75.    
  76.     if press('R') then
  77.         segment.failed = true
  78.     end
  79.  
  80.     if press('C') then
  81.         segment.failed = true
  82.         segment.bestTime = 999999
  83.     end
  84.  
  85.     last_keys = keys                                              
  86. end
  87.  
  88. --Nothing below this point should have to change between games (though it might anyway)
  89.  
  90. segment = {
  91.     start = savestate.create(),
  92.     best = savestate.create(),
  93.     bestTime = 999999,
  94.     lastTime = 0,
  95.     curTime = 0,
  96.     done = false,
  97.     actionStart = 0,
  98.     failed = false
  99. }
  100.  
  101. segment.curTime = 0
  102.  
  103. function press(button)
  104.     if keys[button] and not last_keys[button] then
  105.         return true
  106.     end
  107.     return false
  108. end
  109.  
  110. function StartSegment()
  111.     savestate.save(segment.start)
  112.     segment.done = false
  113.     segment.bestTime = 999999
  114.     segment.lastTime = 0
  115.     segment.curTime = movie.framecount()
  116. end
  117.  
  118. function OutputTime()
  119.     gui.text(output.time.x,output.time.y,string.format("Last: %d  Best: %d", segment.lastTime, segment.bestTime))          
  120. end
  121.    
  122. while true do
  123.     StartSegment()
  124.    
  125.     local closerStart = false
  126.    
  127.     while not segment.done do
  128.         segment.failed = false
  129.         savestate.load(segment.start)        
  130.        
  131.         while not segment.done and not segment.failed and (DoNothing()) do
  132.             HandleKeys()
  133.             emu.frameadvance()
  134.            
  135.             --get a save state that's still in the transition, but close to the action for player control
  136.             if movie.framecount() == segment.actionStart - preFrames then
  137.                 savestate.save(segment.start)
  138.                 closerStart = true
  139.             end
  140.         end
  141.  
  142.         SetGameInfo()
  143.        
  144.         if not closerStart then
  145.             segment.actionStart = movie.framecount()
  146.         end
  147.  
  148.  
  149.         while not segment.done and not segment.failed and ContinueAttempt() do
  150.             emu.frameadvance()
  151.             UpdateGameInfo()
  152.             OutputGameInfo()
  153.             OutputTime()
  154.             HandleKeys()
  155.         end
  156.        
  157.         segment.lastTime = (movie.framecount() - segment.curTime)
  158.        
  159.         if (segment.bestTime > segment.lastTime) and (segment.lastTime > 10) and not segment.failed and not segment.done and SegmentOk() then
  160.             segment.bestTime = segment.lastTime
  161.             savestate.save(segment.best)
  162.         end
  163.     end
  164.    
  165.     savestate.load(segment.best)  
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement