Guest User

RNG Brute Force Testing Script - Damage

a guest
Feb 11th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.91 KB | None | 0 0
  1. -- RNG Brute Force Script
  2. -- Author: Omnigamer
  3. -- Instructions:
  4. --
  5. -- Fill in the information below according to what you want to test.
  6. -- At the very minimum, you need an address for the RNG and a condition that you want to check.
  7. -- In an emulator, start the script 2 frames before the RNG is checked and the condition you're watching changes.
  8. -- Adapt the condition and tracking according to what you are trying to find out.
  9. -- You will also potentially need to change the RNG function to suit the target game.
  10.  
  11. -- Addresses
  12. RNG_ADDR = 0x00052C;
  13. COND_ADDR = 0x000046;
  14.  
  15. -- Control values; number of frames to advance and how many times to test
  16. NUM_FRAMES = 1;
  17. NUM_ITER = 1000;
  18.  
  19. -- Sets the RNG to val; assumes a 16-bit RNG
  20. function set_RNG(val)
  21.     mainmemory.write_u16_le(RNG_ADDR,val);
  22. end
  23.  
  24. -- Function that checks whether the condition and reports whatever information
  25. -- you are trying to track from it.
  26. function check_cond()
  27.  
  28.     cond = mainmemory.read_u16_le(COND_ADDR);
  29.    
  30.     myVal = hex2dec(cond);
  31.    
  32.    
  33.     -- Logs the min, max, and average damage.
  34.     if(myVal> maxVal) then
  35.         maxVal = myVal;
  36.     elseif(myVal<minVal) then
  37.         minVal = myVal;
  38.     end
  39.    
  40.     mySum = mySum + myVal;
  41.    
  42. end
  43.  
  44. -- Function that converts a hex listing of a base 10 value (damage) back to base 10.
  45. function hex2dec(val)
  46.    
  47.     nibble1 = bit.band(val,0x000F);
  48.     nibble2 = bit.band(bit.rshift(val,4),0x000F);
  49.     nibble3 = bit.band(bit.rshift(val,8),0x000F);
  50.     nibble4 = bit.band(bit.rshift(val,12),0x000F);
  51.    
  52.     return nibble1+nibble2*10+nibble3*100+nibble4*1000;
  53.    
  54. end
  55.  
  56. -- Advances the set number of frames. May need to be done when the RNG value is used
  57. -- but the testable action is some number of frames away. Defaults to 1.
  58. function advance_frames(val)
  59.     for i=0,val,1 do
  60.         emu.frameadvance();
  61.     end
  62. end
  63.  
  64. -- Sets up the save states
  65. savestate.save("test_state");
  66. savestate.load("test_state");
  67.  
  68. emu.limitframerate(false);
  69.  
  70. -- Initialize some variables
  71. mycount = 0;
  72. maxVal = 0;
  73. minVal = 999;
  74. mySum = 0;
  75.    
  76. -- Main testing loop
  77. for i=0,NUM_ITER,1 do
  78.     -- Grab a random value to set the RNG to; adjust size as necessary
  79.     myrand = math.random(0,0xFFFF);
  80.     set_RNG(myrand);
  81.    
  82.     -- Report various things that you're looking for on-screen
  83.     gui.drawText(30,30,string.format("ITER:   %d",i));
  84.     gui.drawText(30,45,string.format("MAX:    %d",maxVal));
  85.     gui.drawText(30,60,string.format("MIN:    %d",minVal))
  86.     gui.drawText(30,75,string.format("AVG:    %.2f",(mySum/i)));
  87.    
  88.    
  89.     advance_frames(NUM_FRAMES);
  90.     check_cond();
  91.     savestate.load("test_state");
  92.     final = i;
  93. end
  94.    
  95. emu.limitframerate(true);
  96.    
  97. -- After the processing is done, just leave the information on-screen
  98. while true do
  99.         gui.drawText(30,30,string.format("ITER:   %d",final));
  100.         gui.drawText(30,45,string.format("MAX:    %d",maxVal));
  101.         gui.drawText(30,60,string.format("MIN:    %d",minVal))
  102.         gui.drawText(30,75,string.format("AVG:    %.2f",(mySum/final)));
  103.  
  104.     emu.frameadvance();
  105. end
Advertisement
Add Comment
Please, Sign In to add comment