EliteAnax17

Untitled

May 9th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. -- kyogre script for sapphire v1.0 --
  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.  
  13. -- altered sapphire ROM ONLY!
  14. NEW_RTC_COUNTER_SAPPHIRE = 0x0203A3D8
  15.  
  16. WHICH_KYOGRE = 2 -- 1-4 for which kyogre
  17.  
  18. A_PRESS_FRAME = 1385 -- kyogre offset before delay, account for bizhawk delay weirdness
  19. KYOGRE_DELAY = 700 -- DON'T ALTER THIS!!!
  20.  
  21. globaladdrchange = 0 -- used for pcall protect
  22.  
  23. function FramesAdvance(nframes)
  24. for frame = 1, nframes do
  25. emu.frameadvance()
  26. end
  27. end
  28.  
  29. function trychangedomain()
  30. memory.usememorydomain(globaladdrchange)
  31. end
  32.  
  33. -- this can probably be cleaned up but whatever.
  34. function getMemOffset(domaintype)
  35. if domaintype == "IWRAM" then
  36. return IWRAM
  37. elseif domaintype == "EWRAM" then
  38. return EWRAM
  39. else
  40. print("Unsupported mem offset. Assuming main and returning 0.")
  41. return 0
  42. end
  43. end
  44.  
  45. function readSpecific(domaintype, readtype, endian, addr) -- 0 endian for little, 1 for big
  46. local returnVar = 0xFF
  47. globaladdrchange = domaintype -- set the var for the pcall since pcall does not support passing arguments
  48.  
  49. if endian == 0 then
  50. if readtype == 8 then
  51. if pcall(trychangedomain) then
  52. returnVar = memory.read_u8_le(addr - getMemOffset(domaintype))
  53. else
  54. print("Error changing memory domains.")
  55. return
  56. end
  57. elseif readtype == 16 then
  58. if pcall(trychangedomain) then
  59. returnVar = memory.read_u16_le(addr - getMemOffset(domaintype))
  60. else
  61. print("Error changing memory domains.")
  62. return
  63. end
  64. elseif readtype == 24 then
  65. if pcall(trychangedomain) then
  66. returnVar = memory.read_u24_le(addr - getMemOffset(domaintype))
  67. else
  68. print("Error changing memory domains.")
  69. return
  70. end
  71. elseif readtype == 32 then
  72. if pcall(trychangedomain) then
  73. returnVar = memory.read_u32_le(addr - getMemOffset(domaintype))
  74. else
  75. print("Error changing memory domains.")
  76. return
  77. end
  78. else
  79. print("Invalid readtype. Exiting script.")
  80. return
  81. end
  82. elseif endian == 1 then
  83. if readtype == 8 then
  84. if pcall(trychangedomain) then
  85. returnVar = memory.read_u8_be(addr - getMemOffset(domaintype))
  86. else
  87. print("Error changing memory domains.")
  88. return
  89. end
  90. elseif readtype == 16 then
  91. if pcall(trychangedomain) then
  92. returnVar = memory.read_u16_be(addr - getMemOffset(domaintype))
  93. else
  94. print("Error changing memory domains.")
  95. return
  96. end
  97. elseif readtype == 24 then
  98. if pcall(trychangedomain) then
  99. returnVar = memory.read_u24_be(addr - getMemOffset(domaintype))
  100. else
  101. print("Error changing memory domains.")
  102. return
  103. end
  104. elseif readtype == 32 then
  105. if pcall(trychangedomain) then
  106. returnVar = memory.read_u32_be(addr - getMemOffset(domaintype))
  107. else
  108. print("Error changing memory domains.")
  109. return
  110. end
  111. else
  112. print("Invalid readtype. Exiting script.")
  113. return
  114. end
  115. else
  116. print("Invalid endian type. Exiting script.")
  117. return
  118. end
  119.  
  120. return returnVar
  121. end
  122.  
  123. function checkEmu()
  124. check = console.getavailabletools --this returns an error if not on Bizhawk
  125. end
  126.  
  127. if pcall(checkEmu) then
  128. print("Loaded script on Bizhawk, setting memory domain to IWRAM")
  129. memory.usememorydomain("IWRAM");
  130. else
  131. print("VBA-RR or other emulator detected - will not switch memory domain.") -- dont do anything else, only bizhawk needs the memory domain set
  132. end
  133.  
  134. if(readSpecific("IWRAM", 16, 0, RTC_ERROR_STATUS) == 0x0000) then
  135. memory.write_u16_le(RTC_ERROR_STATUS - getMemOffset("IWRAM"), 0x0FF0) -- force RTC to error.
  136. print("RTC forced into error state.")
  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. -- press left.
  145. local input = {}
  146. input["Left"] = true
  147. joypad.set(input)
  148.  
  149. -- wait 120 frames.
  150. FramesAdvance(120)
  151.  
  152. -- main loop --
  153.  
  154. repeat
  155. if(readSpecific("IWRAM", 16, 0, RTC_ERROR_STATUS) == 0x0000) then
  156. memory.write_u16_le(RTC_ERROR_STATUS - getMemOffset("IWRAM"), 0x0FF0) -- force RTC to error.
  157. print("RTC forced into error state.")
  158. end
  159. local vblankcounter = readSpecific("IWRAM", 32, 0, V_BLANK_COUNTER_ADDR)
  160. local rngcounter = readSpecific("EWRAM", 32, 0, NEW_RTC_COUNTER_SAPPHIRE)
  161. drawguitext(vblankcounter, rngcounter)
  162.  
  163. if(vblankcounter == (A_PRESS_FRAME - KYOGRE_DELAY - 1 + (WHICH_KYOGRE - 1))) then -- it takes 1 frame to input
  164. local input = {}
  165. input["A"] = true
  166. joypad.set(input)
  167. print("This should produce the right Kyogre.")
  168. 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.
  169. print(string.format("Current RNG counter is %i.", rngcounter))
  170. return -- end script.
  171. end
  172.  
  173. emu.frameadvance()
  174. until false
  175.  
  176. gui.register(drawguitext)
Advertisement
Add Comment
Please, Sign In to add comment