Advertisement
Zeronoin

FCEUX LUA RNG Sample Testing Script

Jun 3rd, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. -- FCEUX LUA RNG Sample Testing Script
  2. -- by ZeroNoin
  3.  
  4.  
  5. -- This LUA script is heavily based on script by Omnigamer found here:
  6. -- http://pastebin.com/Qv6sw2j7
  7. -- This FCEUX script takes an RNG address and sets it to various values at the start of savestate loads.
  8. -- It counts the desired results found in other addresses from an event after the savestate load.
  9. -- Pause the emulator just before the RNG event, then run this script via File > Lua, then unpause the emulator.
  10. -- It can be difficult to find RNG in game assembly, and the RNG can shuffle in strange ways, so sample testing can help.
  11. -- The values given here were used in Blaster Master to test H drop rates.
  12.  
  13.  
  14. -- RNG RAM address, addresses showing result of RNG, number of result addresses, and value compared for RNG success.
  15. -- In the case of Blaster Master, an item drop (including an H drop) is labeled with a value of 0x35.
  16. -- There are 8 locations in the RAM where an item drop can be placed.
  17. RNG_ADDRESS = 0x0013
  18. RESULT_ADDRESSES = {0x0470,0x047E,0x048C,0x049A,0x04A8,0x04B6,0x04C4,0x04D2}
  19. RESULT_VALUE = 0x35
  20.  
  21. -- Count of RNG successes, iterated value put in the RNG RAM, and length of RNG value iterations.
  22. -- Since only one 8-bit address holds the RNG value in Blaster Master, there are 0x100 (256) possible values, 0x00 through 0xFF.
  23. COUNT = 0
  24. CURRENT = 0x00
  25. ITERATIONS = 0x100
  26.  
  27. -- Frames from the loaded savestate to give for the RNG event to occur.
  28. FRAMES = 15
  29.  
  30. -- A savestate number assigned for the test (this save is separate from normal savestate slots in the emulator).
  31. -- The savestate must be created and stored as an object in order for this script to reference it.
  32. -- The savestate object does not contain a savestate until one is saved in it.
  33. SAVE_NUMBER = 1
  34. SAVE_STATE = savestate.create(SAVE_NUMBER)
  35. savestate.save(SAVE_STATE)
  36.  
  37. -- Variables for placement of the displayed results.
  38. -- Pixels from the top left in X and Y distance, and pixels spacing the start of each line.
  39. DISPLAY_X = 16
  40. DISPLAY_Y = 32
  41. SPACING = 8
  42.  
  43.  
  44. -- Set the RNG at its specific address in the RAM to a given value.
  45. function set_rng(value)
  46.     memory.writebyte(RNG_ADDRESS,value)
  47. end
  48.  
  49.  
  50. -- Compare the value at a resulting address with the value considered RNG success.
  51. function compare_result(address,value)
  52.     if(memory.readbyte(address)==value) then
  53.         return true
  54.     else
  55.         return false
  56.     end
  57. end
  58.  
  59.  
  60. -- Check the results in the given addresses, and count the successes.
  61. -- Output (print) successful RNG values to the emulator LUA console.
  62. function check_result()
  63.     for i=1,table.getn(RESULT_ADDRESSES),1 do
  64.         if(compare_result(RESULT_ADDRESSES[i],RESULT_VALUE)) then
  65.             COUNT = COUNT+1
  66.             emu.print(string.upper(string.format("%02x",CURRENT)))
  67.             break
  68.         end
  69.     end
  70. end
  71.  
  72.  
  73. -- Display the current results of the test in text.
  74. -- The "O" displays wide in FCEUX, so I decided to use "0" instead.
  75. function display_results()
  76.     gui.text(DISPLAY_X,DISPLAY_Y,string.format("C0UNT   %3d",COUNT))
  77.     gui.text(DISPLAY_X,DISPLAY_Y+SPACING,string.format("CURRENT %3d",CURRENT))
  78.     gui.text(DISPLAY_X,DISPLAY_Y+SPACING+SPACING,string.format("PERCENT  %.2f%%",(COUNT/CURRENT)*100))
  79. end
  80.  
  81.  
  82. -- Advance the frames a given quantity for the event to occur.
  83. -- Also display the status of the results on each frame.
  84. function advance_frames(value)
  85.     for i=0,value,1 do
  86.         emu.frameadvance()
  87.         display_results()
  88.     end
  89. end
  90.  
  91.  
  92. -- Main script utilizing the above variables and functions.
  93. -- For all the RNG values, load the savestate, set the RNG, and advance the frames a given amount for the event.
  94. -- After the frames advance the event, check and count the result of the event in corresponding addresses in the RAM.
  95. -- When complete, output totals to the emulator LUA console and continually advance frames and display the results.
  96. while(CURRENT<ITERATIONS) do
  97.     savestate.load(SAVE_STATE)
  98.     set_rng(CURRENT)
  99.     advance_frames(FRAMES)
  100.     check_result()
  101.     CURRENT = CURRENT+0x01
  102. end
  103. emu.print(string.format("COUNT %3d",COUNT))
  104. emu.print(string.format("CURRENT %3d",CURRENT))
  105. emu.print(string.format("PERCENT %.2f%%",(COUNT/CURRENT)*100))
  106. while(true) do
  107.     advance_frames(1)
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement