Advertisement
EliteAnax17

Untitled

May 10th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. -- kyogre script for sapphire v1.1 --
  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. A_PRESS_FRAME = 1385 -- kyogre offset before delay, account for bizhawk delay weirdness
  20. KYOGRE_DELAY = 700 -- DON'T ALTER THIS!!!
  21.  
  22. globaladdrchange = 0 -- used for pcall protect
  23.  
  24. function FramesAdvance(nframes)
  25. for frame = 1, nframes do
  26. emu.frameadvance()
  27. end
  28. end
  29.  
  30. function trychangedomain()
  31. memory.usememorydomain(globaladdrchange)
  32. end
  33.  
  34. -- this can probably be cleaned up but whatever.
  35. function getMemOffset(domaintype)
  36. if domaintype == "IWRAM" then
  37. return IWRAM
  38. elseif domaintype == "EWRAM" then
  39. return EWRAM
  40. else
  41. print("Unsupported mem offset. Assuming main and returning 0.")
  42. return 0
  43. end
  44. end
  45.  
  46. function readSpecific(domaintype, readtype, endian, addr) -- 0 endian for little, 1 for big
  47. local returnVar = 0xFF
  48. globaladdrchange = domaintype -- set the var for the pcall since pcall does not support passing arguments
  49.  
  50. if endian == 0 then
  51. if readtype == 8 then
  52. if pcall(trychangedomain) then
  53. returnVar = memory.read_u8_le(addr - getMemOffset(domaintype))
  54. else
  55. print("Error changing memory domains.")
  56. return
  57. end
  58. elseif readtype == 16 then
  59. if pcall(trychangedomain) then
  60. returnVar = memory.read_u16_le(addr - getMemOffset(domaintype))
  61. else
  62. print("Error changing memory domains.")
  63. return
  64. end
  65. elseif readtype == 24 then
  66. if pcall(trychangedomain) then
  67. returnVar = memory.read_u24_le(addr - getMemOffset(domaintype))
  68. else
  69. print("Error changing memory domains.")
  70. return
  71. end
  72. elseif readtype == 32 then
  73. if pcall(trychangedomain) then
  74. returnVar = memory.read_u32_le(addr - getMemOffset(domaintype))
  75. else
  76. print("Error changing memory domains.")
  77. return
  78. end
  79. else
  80. print("Invalid readtype. Exiting script.")
  81. return
  82. end
  83. elseif endian == 1 then
  84. if readtype == 8 then
  85. if pcall(trychangedomain) then
  86. returnVar = memory.read_u8_be(addr - getMemOffset(domaintype))
  87. else
  88. print("Error changing memory domains.")
  89. return
  90. end
  91. elseif readtype == 16 then
  92. if pcall(trychangedomain) then
  93. returnVar = memory.read_u16_be(addr - getMemOffset(domaintype))
  94. else
  95. print("Error changing memory domains.")
  96. return
  97. end
  98. elseif readtype == 24 then
  99. if pcall(trychangedomain) then
  100. returnVar = memory.read_u24_be(addr - getMemOffset(domaintype))
  101. else
  102. print("Error changing memory domains.")
  103. return
  104. end
  105. elseif readtype == 32 then
  106. if pcall(trychangedomain) then
  107. returnVar = memory.read_u32_be(addr - getMemOffset(domaintype))
  108. else
  109. print("Error changing memory domains.")
  110. return
  111. end
  112. else
  113. print("Invalid readtype. Exiting script.")
  114. return
  115. end
  116. else
  117. print("Invalid endian type. Exiting script.")
  118. return
  119. end
  120.  
  121. return returnVar
  122. end
  123.  
  124. function checkEmu()
  125. check = console.getavailabletools --this returns an error if not on Bizhawk
  126. end
  127.  
  128. if pcall(checkEmu) then
  129. print("Loaded script on Bizhawk, setting memory domain to IWRAM")
  130. memory.usememorydomain("IWRAM");
  131. else
  132. print("VBA-RR or other emulator detected - will not switch memory domain.") -- dont do anything else, only bizhawk needs the memory domain set
  133. end
  134.  
  135. function drawguitext(framecount, rngcounter)
  136. gui.text(1,10,string.format("Frame Counter: %i",framecount))
  137. gui.text(1,18,string.format("RNG Counter: %i",rngcounter))
  138. end
  139.  
  140. client.reboot_core()
  141.  
  142. FramesAdvance(5)
  143.  
  144. local rtcStatus = readSpecific("IWRAM", 16, 0, RTC_ERROR_STATUS)
  145.  
  146. if(rtcStatus == 0x0FF0) then
  147. print("WARNING: RTC already in error state.")
  148. else
  149. memory.write_u16_le(RTC_ERROR_STATUS - getMemOffset("IWRAM"), 0x0FF0) -- force RTC to error.
  150. print("RTC forced into error state.")
  151. end
  152.  
  153. -- 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.
  154. memory.write_u32_le(RNG_SEED_ADDR - getMemOffset("IWRAM"), 0x00000000)
  155.  
  156. rngSeed = readSpecific("IWRAM", 32, 0, RNG_SEED_ADDR)
  157. print(string.format("Current RNG seed is: 0x%08x", rngSeed))
  158.  
  159. FramesAdvance(25)
  160.  
  161. -- THIS IS A MESS LMAO but it gets in-game to Kyogre. JUST CLOSE THE ROM AND REOPEN IT BEFORE RUNNING, Bizhawk could crash.
  162. -- wait for intro, and press start.
  163. FramesAdvance(210)
  164. input = {}
  165. input["Start"] = true
  166. joypad.set(input)
  167. print("I input start")
  168.  
  169. --wait for title screen and Mash A.
  170. FramesAdvance(45)
  171. input["A"] = true
  172. joypad.set(input)
  173. print("I input A")
  174. -- unpress.
  175. FramesAdvance(1)
  176. input["A"] = false
  177. joypad.set(input)
  178. print("I uninput A")
  179. FramesAdvance(10)
  180. input["Start"] = true
  181. joypad.set(input)
  182. print("I input start")
  183. FramesAdvance(110)
  184. input["A"] = true
  185. joypad.set(input)
  186. print("I input A")
  187. FramesAdvance(75)
  188. input["A"] = true
  189. joypad.set(input)
  190. print("I input A")
  191. FramesAdvance(5)
  192. input["A"] = true
  193. joypad.set(input)
  194. print("I input start")
  195. FramesAdvance(60)
  196.  
  197. -- press left.
  198. input = {}
  199. input["Left"] = true
  200. joypad.set(input)
  201.  
  202. -- wait 120 frames.
  203. FramesAdvance(120)
  204.  
  205. -- main loop --
  206.  
  207. local rngCounterTable = {}
  208. local i = 0
  209.  
  210. repeat
  211. if(readSpecific("IWRAM", 16, 0, RTC_ERROR_STATUS) == 0x0000) then
  212. memory.write_u16_le(RTC_ERROR_STATUS - getMemOffset("IWRAM"), 0x0FF0) -- force RTC to error.
  213. print("RTC forced into error state.")
  214. end
  215. local vblankcounter = readSpecific("IWRAM", 32, 0, V_BLANK_COUNTER_ADDR)
  216. local rngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  217. rngCounterTable[i] = rngcounter
  218. i = i + 1 -- increment i.
  219. drawguitext(vblankcounter, rngcounter)
  220.  
  221. if(vblankcounter == (A_PRESS_FRAME - KYOGRE_DELAY - 1 + (WHICH_KYOGRE - 1))) then -- it takes 1 frame to input
  222. local input = {}
  223. input["A"] = true
  224. joypad.set(input)
  225. print("This should produce the right Kyogre.")
  226. 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.
  227. print(string.format("Current RNG counter is %i.", rngcounter))
  228. print(rngCounterTable)
  229. return -- end script.
  230. end
  231.  
  232. emu.frameadvance()
  233. until false
  234.  
  235. gui.register(drawguitext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement