Advertisement
cleartonic

ffvi-encounter-script

Sep 8th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. # written in Lua script
  2. # bizhawk version 2.2 (x64)
  3. # resources from external sources: http://lua-users.org/wiki/TableUtils
  4. # author: cleartonic
  5.  
  6.  
  7. memory.usememorydomain("WRAM")
  8.  
  9. filename = "dumps/ff6-steps-log.txt"
  10.  
  11. file = io.open(filename,"w+")
  12. io.output(file)
  13. io.write("#INTENTIONAL BLANK#\n")
  14. io.close()
  15.  
  16.  
  17. -- Table to string code --
  18.  
  19. function table.val_to_str ( v )
  20. if "string" == type( v ) then
  21. v = string.gsub( v, "\n", "\\n" )
  22. if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
  23. return "'" .. v .. "'"
  24. end
  25. return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
  26. else
  27. return "table" == type( v ) and table.tostring( v ) or
  28. tostring( v )
  29. end
  30. end
  31.  
  32. function table.key_to_str ( k )
  33. if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
  34. return k
  35. else
  36. return "[" .. table.val_to_str( k ) .. "]"
  37. end
  38. end
  39.  
  40. function table.tostring( tbl )
  41. local result, done = {}, {}
  42. for k, v in ipairs( tbl ) do
  43. table.insert( result, table.val_to_str( v ) )
  44. done[ k ] = true
  45. end
  46. for k, v in pairs( tbl ) do
  47. if not done[ k ] then
  48. table.insert( result,
  49. table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
  50. end
  51. end
  52. return "{" .. table.concat( result, "," ) .. "}"
  53. end
  54.  
  55. stepcounter = memory.read_u16_le(0x001866) -- 1 byte
  56. threat = memory.read_u16_le(0x001F6E) --
  57. seed = memory.readbyte(0x001FA3) -- seed changed upon reset
  58. Xcoord = memory.readbyte(0x0000C6) -- 0x0000C6 #if its 160, press left. If its 144, press right.
  59. battlemode = memory.readbyte(0x000087) --0x000087 #if its 224, world map. if its 0, battle. Used for reset & load as well
  60. currentstepstring = {} -- will temporarily house steps for any given threat, then will be written upon finished loop
  61.  
  62. laststeps = 0 -- set every time a new stepcounter is readbyte
  63. newsteps = 0 -- temporary "last round's steps to a battle"
  64. iteration = 1 -- current number of loops, used for logging
  65. maxiterations = 128 -- total number of loops
  66. battlecounter = 99 -- number of battles looped for a threat (will be eventually 99)
  67. stepscalled = 0 -- temporary viewer of how many times insert.table gets called
  68.  
  69.  
  70. savestate.loadslot(1) -- later needs to be REMOVED
  71.  
  72. function wait(number)
  73. while number > 0 do
  74. displayfunc()
  75. emu.frameadvance()
  76. number = number - 1
  77. updatevalues()
  78. end
  79. end
  80.  
  81. function displayfunc()
  82. gui.text(200,0,"Ins: "..stepscalled)
  83. end
  84.  
  85.  
  86. function updatevalues()
  87. stepcounter = memory.read_u16_le(0x001866) -- 1 byte
  88. threat = memory.read_u16_le(0x001F6E) -- logged upon first
  89. Xcoord = memory.readbyte(0x0000C6) -- 0x0000C6 #if its 160, press left. If its 144, press right.
  90. battlemode = memory.readbyte(0x000087) --0x000087 #if its 224, world map. if its 0, battle. Used for reset & load as well
  91. end
  92.  
  93. function updatecounters()
  94. seed = memory.readbyte(0x001FA3)
  95. battlecounter = 99 -- number of battles looped for a threat (will be eventually 99)
  96. laststeps = 0
  97. newsteps = 0
  98. currentstepstring = {}
  99. end
  100.  
  101. function reset()
  102. joypad.set({Reset=true})
  103. console.log(joypad.get())
  104. gui.text(1,1,"Power")
  105. temp = 470
  106. while temp > 0 do
  107. onoff()
  108. temp = temp - 1
  109. end
  110. end
  111.  
  112. function onoff()
  113. joypad.set({A=true},1)
  114. wait(1)
  115. joypad.set({A=false},1)
  116. wait(1)
  117. end
  118.  
  119.  
  120.  
  121. while iteration <= maxiterations do
  122.  
  123. while battlecounter >= 0 do
  124. updatevalues()
  125. gui.text(1,1,"First Step")
  126.  
  127. --take first step
  128. if Xcoord == 160 then
  129. joypad.set({Left=true},1)
  130. wait(24)
  131. elseif Xcoord == 144 then
  132. joypad.set({Right=true},1)
  133. wait(24)
  134. else
  135. gui.text(1,1,"Failure")
  136. wait(1)
  137. end
  138.  
  139. -- loop world map movements
  140. updatevalues()
  141.  
  142. while threat > 0 do
  143. wait(1)
  144. if Xcoord == 160 then
  145. joypad.set({Left=true},1)
  146. wait(24)
  147. elseif Xcoord == 144 then
  148. joypad.set({Right=true},1)
  149. wait(24)
  150. else
  151. gui.text(1,1,"Failure")
  152. wait(1)
  153. end
  154.  
  155. end
  156.  
  157.  
  158. -- transition to battle
  159.  
  160. wait(30)
  161.  
  162. -- get through a battle
  163. while battlemode == 0 do
  164. gui.text(1,20,"Laststeps: "..laststeps)
  165. joypad.set({A=true},1)
  166. wait(1)
  167. updatevalues()
  168. end
  169.  
  170.  
  171. -- log steps
  172. newsteps = stepcounter - laststeps
  173. table.insert(currentstepstring, newsteps)
  174. stepscalled = stepscalled + 1
  175. laststeps = stepcounter
  176.  
  177.  
  178. battlecounter = battlecounter - 1
  179. wait(1)
  180. end
  181.  
  182.  
  183. -- write to file, move to next iteration
  184. file = io.open(filename,"a+")
  185. io.output(file)
  186. io.write('Iteration ',iteration," | Seed ",seed,': ',table.tostring(currentstepstring))
  187. io.write("\n")
  188. io.close()
  189.  
  190. iteration = iteration + 1
  191.  
  192. reset()
  193. updatevalues()
  194. updatecounters()
  195. end
  196.  
  197. while true do
  198. gui.text(1,1,"Finished.")
  199. wait(1)
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement