Advertisement
EliteAnax17

Untitled

May 10th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 KB | None | 0 0
  1. -- kyogre script for sapphire v1.2 --
  2. -- Written by ProjectRevoTPP --
  3. -- For use on Bizhawk. --
  4.  
  5. -- TO USE: Run as soon as the map loads and your player has control ASAP and let the script do the rest to manip the right Kyogre.--
  6.  
  7. IWRAM = 0x03000000
  8. EWRAM = 0x02000000
  9.  
  10. V_BLANK_COUNTER_ADDR = 0x03001790
  11. RTC_ERROR_STATUS = 0x03000458
  12. RNG_SEED_ADDR = 0x03004818
  13.  
  14. -- altered sapphire ROM ONLY!
  15. NEW_RTC_COUNTER_SAPPHIRE = 0x0203A3D8
  16.  
  17. WHICH_KYOGRE = 1 -- 1-4 for which kyogre
  18.  
  19. YOLOBALL_RNG_FRAME = 3613 -- which yoloball to go for
  20.  
  21. A_PRESS_FRAME = 1337 -- kyogre offset before delay, account for bizhawk delay weirdness
  22. KYOGRE_DELAY = 652 -- DON'T ALTER THIS!!!
  23. YOLO_DELAY = 68 -- number of frames delay between A press in menu and yolo calculation.
  24. YOLO_DELAY_RNG = 150
  25.  
  26. globaladdrchange = 0 -- used for pcall protect
  27.  
  28. function FramesAdvance(nframes)
  29. for frame = 1, nframes do
  30. emu.frameadvance()
  31. end
  32. end
  33.  
  34. function trychangedomain()
  35. memory.usememorydomain(globaladdrchange)
  36. end
  37.  
  38. -- this can probably be cleaned up but whatever.
  39. function getMemOffset(domaintype)
  40. if domaintype == "IWRAM" then
  41. return IWRAM
  42. elseif domaintype == "EWRAM" then
  43. return EWRAM
  44. else
  45. print("Unsupported mem offset. Assuming main and returning 0.")
  46. return 0
  47. end
  48. end
  49.  
  50. function readSpecific(domaintype, readtype, endian, addr) -- 0 endian for little, 1 for big
  51. local returnVar = 0xFF
  52. globaladdrchange = domaintype -- set the var for the pcall since pcall does not support passing arguments
  53.  
  54. if endian == 0 then
  55. if readtype == 8 then
  56. if pcall(trychangedomain) then
  57. returnVar = memory.read_u8_le(addr - getMemOffset(domaintype))
  58. else
  59. print("Error changing memory domains.")
  60. return
  61. end
  62. elseif readtype == 16 then
  63. if pcall(trychangedomain) then
  64. returnVar = memory.read_u16_le(addr - getMemOffset(domaintype))
  65. else
  66. print("Error changing memory domains.")
  67. return
  68. end
  69. elseif readtype == 24 then
  70. if pcall(trychangedomain) then
  71. returnVar = memory.read_u24_le(addr - getMemOffset(domaintype))
  72. else
  73. print("Error changing memory domains.")
  74. return
  75. end
  76. elseif readtype == 32 then
  77. if pcall(trychangedomain) then
  78. returnVar = memory.read_u32_le(addr - getMemOffset(domaintype))
  79. else
  80. print("Error changing memory domains.")
  81. return
  82. end
  83. else
  84. print("Invalid readtype. Exiting script.")
  85. return
  86. end
  87. elseif endian == 1 then
  88. if readtype == 8 then
  89. if pcall(trychangedomain) then
  90. returnVar = memory.read_u8_be(addr - getMemOffset(domaintype))
  91. else
  92. print("Error changing memory domains.")
  93. return
  94. end
  95. elseif readtype == 16 then
  96. if pcall(trychangedomain) then
  97. returnVar = memory.read_u16_be(addr - getMemOffset(domaintype))
  98. else
  99. print("Error changing memory domains.")
  100. return
  101. end
  102. elseif readtype == 24 then
  103. if pcall(trychangedomain) then
  104. returnVar = memory.read_u24_be(addr - getMemOffset(domaintype))
  105. else
  106. print("Error changing memory domains.")
  107. return
  108. end
  109. elseif readtype == 32 then
  110. if pcall(trychangedomain) then
  111. returnVar = memory.read_u32_be(addr - getMemOffset(domaintype))
  112. else
  113. print("Error changing memory domains.")
  114. return
  115. end
  116. else
  117. print("Invalid readtype. Exiting script.")
  118. return
  119. end
  120. else
  121. print("Invalid endian type. Exiting script.")
  122. return
  123. end
  124.  
  125. return returnVar
  126. end
  127.  
  128. function checkEmu()
  129. check = console.getavailabletools --this returns an error if not on Bizhawk
  130. end
  131.  
  132. if pcall(checkEmu) then
  133. print("Loaded script on Bizhawk, setting memory domain to IWRAM")
  134. memory.usememorydomain("IWRAM");
  135. else
  136. print("VBA-RR or other emulator detected - will not switch memory domain.") -- dont do anything else, only bizhawk needs the memory domain set
  137. end
  138.  
  139. function drawguitext(framecount, rngcounter)
  140. gui.text(1,10,string.format("Frame Counter: %i",framecount))
  141. gui.text(1,18,string.format("RNG Counter: %i",rngcounter))
  142. end
  143.  
  144. client.reboot_core()
  145.  
  146. FramesAdvance(5)
  147.  
  148. local rtcStatus = readSpecific("IWRAM", 16, 0, RTC_ERROR_STATUS)
  149.  
  150. if(rtcStatus == 0x0FF0) then
  151. print("WARNING: RTC already in error state.")
  152. else
  153. memory.write_u16_le(RTC_ERROR_STATUS - getMemOffset("IWRAM"), 0x0FF0) -- force RTC to error.
  154. print("RTC forced into error state.")
  155. end
  156.  
  157. -- seed has to be forced to be 0 after 5 frames due to Bizhawk/game for some reason not liking RTC error being forced before 5 frames. on real hardware, RTC hardware will return 0x0FF0 to the game, but here it has to be forced to match.
  158. memory.write_u32_le(RNG_SEED_ADDR - getMemOffset("IWRAM"), 0x00000000)
  159.  
  160. rngSeed = readSpecific("IWRAM", 32, 0, RNG_SEED_ADDR)
  161. print(string.format("Current RNG seed is: 0x%08x", rngSeed))
  162.  
  163. FramesAdvance(25)
  164.  
  165. -- THIS IS A MESS LMAO but it gets in-game to Kyogre. JUST CLOSE THE ROM AND REOPEN IT BEFORE RUNNING, Bizhawk could crash.
  166. -- wait for intro, and press start.
  167. FramesAdvance(210)
  168. input = {}
  169. input["Start"] = true
  170. joypad.set(input)
  171. print("I input start")
  172.  
  173. --wait for title screen and Mash A.
  174. FramesAdvance(45)
  175. input["A"] = true
  176. joypad.set(input)
  177. print("I input A")
  178. -- unpress.
  179. FramesAdvance(1)
  180. input["A"] = false
  181. joypad.set(input)
  182. print("I uninput A")
  183. FramesAdvance(10)
  184. input["Start"] = true
  185. joypad.set(input)
  186. print("I input start")
  187. FramesAdvance(110)
  188. input["A"] = true
  189. joypad.set(input)
  190. print("I input A")
  191. FramesAdvance(75)
  192. input["A"] = true
  193. joypad.set(input)
  194. print("I input A")
  195. FramesAdvance(5)
  196. input["A"] = true
  197. joypad.set(input)
  198. print("I input start")
  199. FramesAdvance(60)
  200.  
  201. -- press left.
  202. input = {}
  203. input["Left"] = true
  204. joypad.set(input)
  205.  
  206. -- wait 120 frames.
  207. FramesAdvance(120)
  208.  
  209. -- main loop --
  210.  
  211. local rngCounterTable = {}
  212. local i = 0
  213.  
  214. repeat
  215. if(readSpecific("IWRAM", 16, 0, RTC_ERROR_STATUS) == 0x0000) then
  216. memory.write_u16_le(RTC_ERROR_STATUS - getMemOffset("IWRAM"), 0x0FF0) -- force RTC to error.
  217. print("RTC forced into error state.")
  218. end
  219. local vblankcounter = readSpecific("IWRAM", 32, 0, V_BLANK_COUNTER_ADDR)
  220. local rngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  221. rngCounterTable[i] = rngcounter
  222. i = i + 1 -- increment i.
  223. drawguitext(vblankcounter, rngcounter)
  224.  
  225. function FramesAdvanceAndPrintRng(nframes, vblankcounter, rngcounter)
  226. print(string.format("Final frame counter is %i.", vblankcounter))
  227. print(string.format("Final RNG counter is %i.", rngcounter))
  228. for frame = 1, nframes do
  229. emu.frameadvance()
  230. end
  231. end
  232.  
  233. if(vblankcounter == (A_PRESS_FRAME - KYOGRE_DELAY - 1 + (WHICH_KYOGRE - 1))) then -- it takes 1 frame to input
  234. local input = {}
  235. input["A"] = true
  236. joypad.set(input)
  237. print("This should produce the right Kyogre.")
  238. print(string.format("I inputted on: %i, Kyogre RNG frame is expected to be %i",vblankcounter + 1, vblankcounter + KYOGRE_DELAY + 1)) -- it takes 1 frame to input, account for it.
  239. print(string.format("Final RNG counter is %i.", rngcounter))
  240. print(rngCounterTable)
  241.  
  242. FramesAdvance(KYOGRE_DELAY + 1)
  243. local vblankcounter = readSpecific("IWRAM", 32, 0, V_BLANK_COUNTER_ADDR)
  244. local rngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  245. print(string.format("Final frame counter is %i.", vblankcounter))
  246. print(string.format("Final RNG counter is %i.", rngcounter))
  247. FramesAdvance(1) -- rng advanced after this frame.
  248. local newvblankcounter = readSpecific("IWRAM", 32, 0, V_BLANK_COUNTER_ADDR)
  249. local newrngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  250. print(string.format("Updated final frame counter is %i.", newvblankcounter))
  251. print(string.format("Updated final RNG counter is %i.", newrngcounter))
  252.  
  253. break -- end loop.
  254. end
  255.  
  256. emu.frameadvance()
  257. until false
  258.  
  259. print("Waiting for yoloball A press window.")
  260. FramesAdvance(405)
  261. input = {}
  262. input["A"] = true
  263. joypad.set(input)
  264. print("I input A")
  265. FramesAdvance(290)
  266. input = {}
  267. input["Right"] = true
  268. joypad.set(input)
  269. print("I input Right")
  270. FramesAdvance(1)
  271. input = {}
  272. input["Right"] = false
  273. joypad.set(input)
  274. print("I uninput Right")
  275. FramesAdvance(1)
  276. input = {}
  277. input["A"] = true
  278. joypad.set(input)
  279. print("I input A")
  280. FramesAdvance(60)
  281. input = {}
  282. input["Right"] = true
  283. joypad.set(input)
  284. print("I input Right")
  285. FramesAdvance(30)
  286. input = {}
  287. input["A"] = true
  288. joypad.set(input)
  289. FramesAdvance(1)
  290.  
  291. local rngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  292. if rngcounter > YOLOBALL_RNG_FRAME - YOLO_DELAY then
  293. print(string.format("ERROR: Unreachable frame. Try a bigger yoloball frame than %i.", rngcounter))
  294. return
  295. end
  296.  
  297. local vblankcounter = readSpecific("IWRAM", 32, 0, V_BLANK_COUNTER_ADDR)
  298. local rngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  299. print(string.format("Starting frame when waiting: %i", vblankcounter))
  300. print(string.format("Starting RNG frame when waiting: %i", rngcounter))
  301.  
  302. -- 2nd loop.
  303. repeat
  304. local vblankcounter = readSpecific("IWRAM", 32, 0, V_BLANK_COUNTER_ADDR)
  305. local rngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  306. if rngcounter == YOLOBALL_RNG_FRAME - YOLO_DELAY_RNG then
  307. print(string.format("I input A on this frame: %i", vblankcounter))
  308. print(string.format("I input A on this RNG frame: %i", rngcounter))
  309. input = {}
  310. input["A"] = true
  311. joypad.set(input)
  312. FramesAdvance(1)
  313. print("Hope yoloball works!")
  314. FramesAdvance(YOLO_DELAY) -- normal frame advances, NOT RNG advances!
  315. local vblankcounter = readSpecific("IWRAM", 32, 0, V_BLANK_COUNTER_ADDR)
  316. local rngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  317. print(string.format("I input A on this frame: %i", vblankcounter))
  318. print(string.format("I input A on this RNG frame: %i", rngcounter))
  319. FramesAdvance(1)
  320. local vblankcounter = readSpecific("IWRAM", 32, 0, V_BLANK_COUNTER_ADDR)
  321. local rngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  322. print(string.format("I input A on this frame: %i", vblankcounter))
  323. print(string.format("I input A on this RNG frame: %i", rngcounter))
  324. return
  325. end
  326. emu.frameadvance()
  327. until false
  328.  
  329. gui.register(drawguitext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement