Advertisement
Guest User

loto router

a guest
Dec 5th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. -- LOTO ROUTER
  2. -- warning: overwrites savestate 5 ("searchsave" below).
  3. --
  4. -- this program finds crit routes for axe knight battles in Dragon Warrior.
  5. --
  6. -- loads a prepared save state ("startsave") and then buffers "Command?"
  7. -- prompts using B some number of times until attacking gives useful crits. it
  8. -- assumes the battle is set up so that you get to attack first. the tactic is
  9. -- to crit high, tank a hit, and then crit again, because buffering "sleep"
  10. -- is too hard and would require L7 which is harder to manipulate.
  11.  
  12. -- startsave is a savestate on the swamp tile left of the axe knight.
  13. -- for the battle to sync consistently, the savestate should either have the
  14. -- hero moving right onto the axe knight tile, or be in the 16-frame
  15. -- "hesitation" window after releasing right.
  16. startsave = savestate.object(4)  -- save slot 4
  17.  
  18. -- searchsave is a scratch save slot used to search for the next crit buffer.
  19. searchsave = savestate.object(5) -- save slot 5
  20.  
  21. -- the maximum crit for whatever level you are attempting this insanity at.
  22. -- this is set for level 6 with a broad sword. (no, really, level 6.)
  23. maxcrit = 36
  24.  
  25. -- joypad state to set before waiting
  26. jp = {}
  27.  
  28. -- wait for n frames while pushing some buttons
  29. function waitframes(n)
  30.   for i=1,n do
  31.     joypad.set(1, jp)
  32.     emu.frameadvance()
  33.   end
  34. end
  35.  
  36. -- detects a critical hit.
  37. crit = false
  38. function detectcrit()
  39.   crit = true
  40. end
  41. memory.registerexecute(0xe628, detectcrit)
  42.  
  43. -- ok, here we go.
  44. savestate.load(startsave)
  45. emu.speedmode("turbo")
  46. -- walk right onto battle tile
  47. jp.right = true
  48. waitframes(32)
  49. jp.right = false
  50.  
  51. -- wait for battle menu to load and poll joypad
  52. jp.B = true
  53. while emu.lagged() do
  54.   waitframes(1)
  55. end
  56. emu.print(string.format("in battle. [%d hp]", memory.readbyte(0xe2)))
  57.  
  58. -- set to true if the battle is won.
  59. won = false
  60. -- count of how many times you've buffered in the current turn.
  61. buffers = 0
  62. -- the overall buffer route.
  63. route = {}
  64.  
  65. while true do
  66.   -- figure out what to do this turn.
  67.   repeat
  68.     -- savestate and then attack.
  69.     savestate.save(searchsave)
  70.     emu.print(string.format("attack after %d", buffers))
  71.     jp.A = true
  72.     waitframes(16)
  73.  
  74.     -- wait and see how it went.
  75.     crit = false
  76.     waitframes(200)
  77.     if memory.readbyte(0xc5) == 0 then
  78.       -- thou art dead.
  79.       emu.print("... cod")
  80.     elseif memory.readbytesigned(0xe2) <= 0 then
  81.       -- we got him!
  82.       emu.print("... rekt!")
  83.       table.insert(route, buffers)
  84.       won = true
  85.       do break end
  86.     elseif memory.readbyte(0xdf) >= 0x80 then
  87.       -- thou art asleep.
  88.       emu.print("... zzz")
  89.     elseif crit == true then
  90.       -- we got a crit, awesome.
  91.       if memory.readbyte(0xe2) <= maxcrit then
  92.         -- the axe knight has less hp left than our max crit, so we can kill it
  93.         -- next turn. let's keep this one.
  94.         emu.print(string.format("good crit@%d. [%d hp]", buffers, memory.readbyte(0xe2)))
  95.         table.insert(route, buffers)
  96.         buffers = 0
  97.         -- rewind to the point of the savestate and commit it.
  98.         savestate.load(searchsave)
  99.         jp.A = true
  100.         waitframes(16)
  101.         -- loop until the menu starts polling again.
  102.         jp.A = false
  103.         jp.B = true
  104.         while emu.lagged() do
  105.           waitframes(1)
  106.         end
  107.         -- next turn.
  108.         do break end
  109.       else
  110.         -- assume this crit is not going to cut it.
  111.         emu.print(string.format("weak crit@%d. [%d hp]", buffers, memory.readbyte(0xe2)))
  112.       end
  113.     else
  114.       -- either this was a miss or a weak hit.
  115.       emu.print("... meh")
  116.     end
  117.  
  118.     -- not a good attack. buffer another "command" window.
  119.     savestate.load(searchsave)
  120.     jp.A = false
  121.     jp.B = true
  122.     waitframes(16)
  123.     while emu.lagged() do
  124.       waitframes(1)
  125.     end
  126.     buffers = buffers + 1
  127.   until true
  128.   -- lua is totally awesome.
  129.   if won then break end
  130. end
  131.  
  132. -- print out the route
  133. emu.print("courage and wit have served thee well")
  134. for i,v in ipairs(route) do
  135.   emu.print(string.format("- turn %d: buffer %d", i, v))
  136. end
  137. emu.speedmode("normal")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement