Advertisement
EliteAnax17

Untitled

Mar 25th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. -- emerald mudkip script v0.5 --
  2. -- written by ProjectRevoTPP --
  3.  
  4. -- BIZHAWK ONLY FOR NOW --
  5.  
  6. IWRAM = 0x03000000
  7. EWRAM = 0x02000000
  8.  
  9. RNG_ADDR = 0x3005D80
  10. RNG_ADDR2 = 0x3005D84
  11. RTC_ERROR_STATUS = 0x3000DB8 -- this needs to be
  12. RNG_COUNTER = 0x020249C0
  13.  
  14. CORRECT_MUDKIP = 0x0E1AB5A1
  15. CORRECT_MUDKIP_RNG_COUNTER = 1023 -- the function that actually creates the mudkip needs 1026, but the counter advances a few times before the correct function is called when this is 1026.
  16.  
  17. -- config --
  18.  
  19. force_mudkip = 1 -- 1 for true, 0 for false
  20. display_gui = 0 -- 1 for true, 0 for false
  21.  
  22. ------------------------
  23.  
  24. local last_known_rng = 0
  25. local last_known_rng2 = 0
  26. framecount = 0
  27. rng_count = 0
  28.  
  29. globaladdrchange = 0 -- used for pcall protect
  30.  
  31. function trychangedomain()
  32. memory.usememorydomain(globaladdrchange)
  33. end
  34.  
  35. -- this can probably be cleaned up but whatever.
  36. function getMemOffset(domaintype)
  37. if domaintype == "IWRAM" then
  38. return IWRAM
  39. elseif domaintype == "EWRAM" then
  40. return EWRAM
  41. else
  42. print("Unsupported mem offset. Assuming main and returning 0.")
  43. return 0
  44. end
  45. end
  46.  
  47. function readSpecific(domaintype, readtype, endian, addr) -- 0 endian for little, 1 for big
  48. local returnVar = 0xFF
  49. globaladdrchange = domaintype -- set the var for the pcall since pcall does not support passing arguments
  50.  
  51. if endian == 0 then
  52. if readtype == 8 then
  53. if pcall(trychangedomain) then
  54. returnVar = memory.read_u8_le(addr - getMemOffset(domaintype))
  55. else
  56. print("Error changing memory domains.")
  57. return
  58. end
  59. elseif readtype == 16 then
  60. if pcall(trychangedomain) then
  61. returnVar = memory.read_u16_le(addr - getMemOffset(domaintype))
  62. else
  63. print("Error changing memory domains.")
  64. return
  65. end
  66. elseif readtype == 24 then
  67. if pcall(trychangedomain) then
  68. returnVar = memory.read_u24_le(addr - getMemOffset(domaintype))
  69. else
  70. print("Error changing memory domains.")
  71. return
  72. end
  73. elseif readtype == 32 then
  74. if pcall(trychangedomain) then
  75. returnVar = memory.read_u32_le(addr - getMemOffset(domaintype))
  76. else
  77. print("Error changing memory domains.")
  78. return
  79. end
  80. else
  81. print("Invalid readtype. Exiting script.")
  82. return
  83. end
  84. elseif endian == 1 then
  85. if readtype == 8 then
  86. if pcall(trychangedomain) then
  87. returnVar = memory.read_u8_be(addr - getMemOffset(domaintype))
  88. else
  89. print("Error changing memory domains.")
  90. return
  91. end
  92. elseif readtype == 16 then
  93. if pcall(trychangedomain) then
  94. returnVar = memory.read_u16_be(addr - getMemOffset(domaintype))
  95. else
  96. print("Error changing memory domains.")
  97. return
  98. end
  99. elseif readtype == 24 then
  100. if pcall(trychangedomain) then
  101. returnVar = memory.read_u24_be(addr - getMemOffset(domaintype))
  102. else
  103. print("Error changing memory domains.")
  104. return
  105. end
  106. elseif readtype == 32 then
  107. if pcall(trychangedomain) then
  108. returnVar = memory.read_u32_be(addr - getMemOffset(domaintype))
  109. else
  110. print("Error changing memory domains.")
  111. return
  112. end
  113. else
  114. print("Invalid readtype. Exiting script.")
  115. return
  116. end
  117. else
  118. print("Invalid endian type. Exiting script.")
  119. return
  120. end
  121.  
  122. return returnVar
  123. end
  124.  
  125. function drawguitext(last_known_rng, rng_count, framecount)
  126. gui.text(1,10,string.format("Current RNG1: %08x",last_known_rng))
  127. gui.text(1,24,string.format("RNG Counter: %i",rng_count))
  128. gui.text(1,38,string.format("Frame Counter: %i",framecount))
  129. end
  130.  
  131. function checkEmu()
  132. check = console.getavailabletools --this returns an error if not on Bizhawk
  133. end
  134.  
  135. if pcall(checkEmu) then
  136. print("Loaded script on Bizhawk, setting memory domain to IWRAM")
  137. memory.usememorydomain("IWRAM");
  138. else
  139. print("VBA-RR or other emulator detected - will not switch memory domain.") -- dont do anything else, only bizhawk needs the memory domain set
  140. end
  141.  
  142. if(readSpecific("IWRAM", 16, 0, RTC_ERROR_STATUS) == 0x0000) then
  143. memory.write_u16_le(RTC_ERROR_STATUS - getMemOffset("IWRAM"), 0x0FF0) -- force RTC to error.
  144. print("RTC forced into error state.")
  145. end
  146.  
  147. local input = {}
  148. input["A"] = true
  149.  
  150. -- force correct mudkip and advance --.
  151. if force_mudkip == 1 then
  152. memory.write_u32_le(RNG_ADDR - getMemOffset("IWRAM"), CORRECT_MUDKIP) -- force RNG1
  153. --memory.write_u32_le() -- force RNG2 (unsure of correct behavior yet)
  154. memory.usememorydomain("EWRAM")
  155. memory.write_u32_le(RNG_COUNTER - getMemOffset("EWRAM"), CORRECT_MUDKIP_RNG_COUNTER) -- force RNG Counter
  156. memory.usememorydomain("IWRAM")
  157. -- press A. --
  158. joypad.set(input)
  159. -- advance frame and unpause.
  160. emu.frameadvance()
  161. print("Speedrun mudkip forced, proceeding")
  162. end
  163.  
  164. repeat
  165. if(readSpecific("IWRAM", 16, 0, RTC_ERROR_STATUS) == 0x0000) then
  166. memory.write_u16_le(RTC_ERROR_STATUS - getMemOffset("IWRAM"), 0x0FF0) -- force RTC to error.
  167. print("RTC forced into error state.")
  168. end
  169. last_known_rng = readSpecific("IWRAM", 32, 0, RNG_ADDR)
  170. last_known_rng2 = readSpecific("IWRAM", 32, 0, RNG_ADDR2)
  171. rng_count = readSpecific("EWRAM", 32, 0, RNG_COUNTER)
  172. framecount = emu.framecount()
  173. if display_gui == 1 then
  174. drawguitext(last_known_rng, rng_count, framecount)
  175. end
  176. emu.frameadvance()
  177. until false
  178.  
  179. gui.register(drawguitext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement