Advertisement
EliteAnax17

Untitled

Jul 4th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. -- Emerald Steps v1.0 --
  2. -- written by ProjectRevoTPP --
  3.  
  4. -- KNOWN BUG: do not start the script during or before the copyright logo. The step counter changes during this when loading the variable from save block, so it would falsely increment steps by 1. --
  5. -- recommended to start it after hitting new game or in the field doing tests. --
  6.  
  7. IWRAM = 0x03000000
  8. EWRAM = 0x02000000
  9.  
  10. EMERALD_SAVEBLOCK_1_PTR = 0x3005D8C
  11. EMERALD_STEPCOUNTER_OFFSET = 0x314C -- the counter is 8-bit, so we need to observe it changing and then keep a local step counter track.
  12. EMERALD_CAN_PLAYER_MOVE = 0x3000F2C -- if 0, player can move, if 1, do not allow counter to increase.
  13. EMERALD_RNG_COUNTER_ADDR = 0x020249C0
  14.  
  15. ------------------------------------
  16.  
  17. globaladdrchange = 0 -- used for pcall protect
  18. is_vba = 0
  19. true_step_counter = 0
  20.  
  21. function trychangedomain()
  22. memory.usememorydomain(globaladdrchange)
  23. end
  24.  
  25. -- this can probably be cleaned up but whatever.
  26. function getMemOffset(domaintype)
  27. if domaintype == "IWRAM" then
  28. return IWRAM
  29. elseif domaintype == "EWRAM" then
  30. return EWRAM
  31. elseif is_vba == 1 then -- the reason we return 0 is because addresses are direct in VBA.
  32. return 0
  33. else
  34. print("Unsupported mem offset. Assuming main and returning 0.")
  35. return 0
  36. end
  37. end
  38.  
  39. function readSpecific(domaintype, readtype, endian, addr) -- 0 endian for little, 1 for big
  40. local returnVar = 0xFF
  41. globaladdrchange = domaintype -- set the var for the pcall since pcall does not support passing arguments
  42.  
  43. if endian == 0 then
  44. if readtype == 8 then
  45. if pcall(trychangedomain) then
  46. returnVar = memory.read_u8_le(addr - getMemOffset(domaintype))
  47. elseif is_vba == 1 then
  48. returnVar = memory.readbyte(addr)
  49. else
  50. print("Error changing memory domains.")
  51. return
  52. end
  53. elseif readtype == 16 then
  54. if pcall(trychangedomain) then
  55. returnVar = memory.read_u16_le(addr - getMemOffset(domaintype))
  56. elseif is_vba == 1 then
  57. returnVar = memory.readword(addr)
  58. else
  59. print("Error changing memory domains.")
  60. return
  61. end
  62. elseif readtype == 24 then
  63. if pcall(trychangedomain) then
  64. returnVar = memory.read_u24_le(addr - getMemOffset(domaintype))
  65. elseif is_vba == 1 then
  66. returnVar = memory.read_u24_le(addr)
  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. elseif is_vba == 1 then
  75. returnVar = memory.readdword(addr)
  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. elseif is_vba == 1 then
  89. returnVar = memory.readbyte(addr)
  90. else
  91. print("Error changing memory domains.")
  92. return
  93. end
  94. elseif readtype == 16 then
  95. if pcall(trychangedomain) then
  96. returnVar = memory.read_u16_be(addr - getMemOffset(domaintype))
  97. elseif is_vba == 1 then
  98. returnVar = memory.readword(addr)
  99. else
  100. print("Error changing memory domains.")
  101. return
  102. end
  103. elseif readtype == 24 then
  104. if pcall(trychangedomain) then
  105. returnVar = memory.read_u24_be(addr - getMemOffset(domaintype))
  106. elseif is_vba == 1 then
  107. returnVar = memory.read_u24_be(addr)
  108. else
  109. print("Error changing memory domains.")
  110. return
  111. end
  112. elseif readtype == 32 then
  113. if pcall(trychangedomain) then
  114. returnVar = memory.read_u32_be(addr - getMemOffset(domaintype))
  115. elseif is_vba == 1 then
  116. returnVar = memory.readdword(addr)
  117. else
  118. print("Error changing memory domains.")
  119. return
  120. end
  121. else
  122. print("Invalid readtype. Exiting script.")
  123. return
  124. end
  125. else
  126. print("Invalid endian type. Exiting script.")
  127. return
  128. end
  129.  
  130. return returnVar
  131. end
  132.  
  133. --------------------------------
  134.  
  135. function drawguitext(steps, rng_counter)
  136. gui.text(1,10,string.format("Current 8-bit Steps: %i",steps))
  137. if is_vba == 0 then
  138. gui.text(1,26,string.format("Current RNG Counter: %i",rng_counter))
  139. else
  140. gui.text(1,18,string.format("Current RNG Counter: %i",rng_counter))
  141. end
  142. end
  143.  
  144. function checkEmu()
  145. check = console.getavailabletools --this returns an error if not on Bizhawk
  146. end
  147.  
  148. if pcall(checkEmu) then
  149. print("Loaded script on Bizhawk, setting memory domain to IWRAM")
  150. memory.usememorydomain("IWRAM");
  151. else
  152. print("VBA-RR or other emulator detected - will not switch memory domain.") -- dont do anything else, only bizhawk needs the memory domain set
  153. is_vba = 1
  154. end
  155.  
  156. local save_ptr = readSpecific("IWRAM", 32, 0, EMERALD_SAVEBLOCK_1_PTR) -- load save ptr once
  157. last_step_counter = readSpecific("EWRAM", 32, 0, save_ptr + EMERALD_STEPCOUNTER_OFFSET) -- set the last step counter.
  158.  
  159. repeat
  160. local save_ptr = readSpecific("IWRAM", 32, 0, EMERALD_SAVEBLOCK_1_PTR)
  161. local new_step_counter = readSpecific("EWRAM", 32, 0, save_ptr + EMERALD_STEPCOUNTER_OFFSET)
  162. local rng_counter = readSpecific("EWRAM", 32, 0, EMERALD_RNG_COUNTER_ADDR)
  163. drawguitext(new_step_counter, rng_counter)
  164. emu.frameadvance()
  165. until false
  166.  
  167. gui.register(drawguitext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement