Kirkq

DW1 Lua

Dec 6th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. -- LOTO ROUTER
  2. -- warning: overwrites savestate 4 and 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.  
  17.  
  18. local tempstate = {}
  19. value = 1
  20. --tempstate[value] = savestate.create()
  21. --savestate.save(tempstate[value])
  22.  
  23. savestate.save(4)  --startsave
  24. --startsave = savestate.object(4)  -- save slot 4
  25.  
  26. -- searchsave is a scratch save slot used to search for the next crit buffer.
  27. savestate.save(5) --searchsave
  28. --searchsave = savestate.object(5) -- save slot 5
  29.  
  30. -- the maximum crit for whatever level you are attempting this insanity at.
  31. -- this is set for level 6 with a broad sword. (no, really, level 6.)
  32. maxcrit = 36
  33. local newmaxcrit = 0
  34.  
  35. -- joypad state to set before waiting
  36. jp = {}
  37.  
  38. -- wait for n frames while pushing some buttons
  39. function waitframes(n)
  40.   for i=1,n do
  41.     joypad.set(jp,1)
  42.     emu.frameadvance()
  43.   end
  44. end
  45.  
  46. -- detects a critical hit.
  47. crit = false
  48. function detectcrit()
  49.   crit = true
  50. end
  51. event.onmemoryexecute(detectcrit, 0xe628)
  52.  
  53. -- ok, here we go.
  54. savestate.load(4)
  55. client.speedmode(3200)
  56. -- walk right onto battle tile
  57. --jp.right = true
  58. --waitframes(32)
  59. --jp.right = false
  60.  
  61. -- wait for battle menu to load and poll joypad
  62. jp.B = true
  63. while emu.islagged() do
  64.   waitframes(1)
  65. end
  66. console.log(string.format("in battle. [%d hp]", memory.readbyte(0xe2)))
  67.  
  68. -- set to true if the battle is won.
  69. won = false
  70. -- count of how many times you've buffered in the current turn.
  71. buffers = 0
  72. -- the overall buffer route.
  73. route = {}
  74.  
  75. while true do
  76.   -- figure out what to do this turn.
  77.   repeat
  78.     -- savestate and then attack.
  79.     savestate.save(5)
  80.     console.log(string.format("attack after %d", buffers))
  81.     jp.A = true
  82.     waitframes(16)
  83.  
  84.     -- wait and see how it went.
  85.     crit = false
  86.     crit = true  --this is a hack
  87.     waitframes(200)
  88.  
  89.     if memory.readbyte(0xc5) == 0 then
  90.       -- thou art dead.
  91.       console.log("... cod")
  92.     elseif memory.read_s8(0xe2) <= 0 then
  93.       -- we got him!
  94.       console.log("... rekt!")
  95.       table.insert(route, buffers)
  96.       won = true
  97.       do break end
  98.     elseif memory.readbyte(0xdf) >= 0x80 then
  99.       -- thou art asleep.
  100.       console.log("... zzz")
  101.     elseif crit == true then
  102.       -- we got a crit, awesome.
  103.       if memory.readbyte(0xe2) <= maxcrit then
  104.         -- the axe knight has less hp left than our max crit, so we can kill it
  105.         -- next turn. let's keep this one.
  106.         console.log(string.format("good crit@%d. [%d hp]", buffers, memory.readbyte(0xe2)))
  107.     maxcrit = newmaxcrit
  108.         table.insert(route, buffers)
  109.         buffers = 0
  110.         -- rewind to the point of the savestate and commit it.
  111.         savestate.load(5)
  112.         jp.A = true
  113.         waitframes(16)
  114.         -- loop until the menu starts polling again.
  115.         jp.A = false
  116.         jp.B = true
  117.         while emu.islagged() do
  118.           waitframes(1)
  119.         end
  120.         -- next turn.
  121.         do break end
  122.       else
  123.         -- assume this crit is not going to cut it.
  124.         console.log(string.format("weak crit@%d. [%d hp]", buffers, memory.readbyte(0xe2)))
  125.       end
  126.     else
  127.       -- either this was a miss or a weak hit.
  128.       console.log("... meh")
  129.     end
  130.  
  131.     -- not a good attack. buffer another "command" window.
  132.     savestate.load(5)
  133.     jp.A = false
  134.     jp.B = true
  135.     waitframes(16)
  136.     while emu.islagged() do
  137.       waitframes(1)
  138.     end
  139.     buffers = buffers + 1
  140.   until true
  141.   -- lua is totally awesome.
  142.   if won then break end
  143. end
  144.  
  145. -- print out the route
  146. console.log("courage and wit have served thee well")
  147. for i,v in ipairs(route) do
  148.   console.log(string.format("- turn %d: buffer %d", i, v))
  149. end
  150. client.speedmode(100)
Advertisement
Add Comment
Please, Sign In to add comment