Advertisement
partyboy1a

terranigma-rng-bugfixes.lua

Oct 3rd, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. -- Terranigma RNG script, updated to work with BizHawk 1.11.7
  2. -- original author: unknown
  3. -- main change: mainmemory.readbyterange seems to work differently
  4. -- minor change: the two columns used by the original script don't overlap anymore
  5.  
  6. -- Window properties
  7. local title = "     RNG     "
  8. local nr_rows = 16
  9. local nr_columns = 2
  10. local column_width = 8
  11. local text_size = 10;
  12. local targetxpos = 0
  13. local targetypos = 36
  14. local xpos = targetxpos
  15. local ypos = targetypos
  16.  
  17. local getCollapsedWidth = function()
  18.   return (math.max(column_width,string.len(title))+1)*4
  19. end
  20.  
  21. local my_text = function(x,y,counter,rn)
  22.   gui.drawText(x,y,string.upper(string.format('%02x:%02x',counter,rn)), 0xFFFFFFFF, text_size)
  23.   -- console.writeline(string.upper(string.format('%02x: %02x',counter,rn)));
  24. end
  25.  
  26. local my_display = function()
  27.  
  28.   -- Update the position of the RNG window
  29.   if ypos<targetypos then
  30.     ypos = ypos + 1
  31.   elseif ypos>targetypos then
  32.     ypos = ypos - 1
  33.   end
  34.   if xpos<targetxpos then
  35.     xpos = xpos + 1
  36.   elseif xpos>targetxpos then
  37.     xpos = xpos - 1
  38.   end
  39.  
  40.   -- Read the current state of the RNG
  41.   local currentRNG = mainmemory.readbyterange(0x0408, 16)
  42.  
  43.   -- Render the title box
  44.   --gui.drawBox (xpos,ypos-9,xpos+4*(string.len(title)+1) ,ypos-1,0xFF000000)
  45.   gui.drawText(xpos+3,ypos-8,title, 0xFFFFFFFF, text_size)
  46.  
  47.   -- Render the RNG boxes
  48.   for c=0,math.floor(nr_columns)-1 do
  49.  
  50.     local xcolumn = xpos+c*(column_width+5)*4*(1-math.abs(xpos/getCollapsedWidth()))
  51.     --gui.drawBox (xcolumn,  ypos,  xcolumn+(column_width+1)*4 ,ypos+nr_rows*8+2,0x8000000000)
  52.  
  53.     for n=0,math.floor(nr_rows)-1 do
  54.        
  55.           -- Render the text
  56.       my_text(xcolumn+3,ypos+(n)*8+2,currentRNG[15],currentRNG[0])
  57.          
  58.           -- Update the set of random numbers to predict the next one
  59.       local carry = 0
  60.           local temp
  61.       for i=15,1,-1 do
  62.         temp  = currentRNG[i-1]+currentRNG[i]+carry
  63.         currentRNG[i-1] = bit.band(temp,0xff)
  64.         carry = (temp-bit.band(temp,0xff))/256
  65.       end
  66.  
  67.       i=15
  68.       repeat
  69.         currentRNG[i]=bit.band(currentRNG[i]+1,0xff)
  70.         i=i-1
  71.       until i<0 or currentRNG[i+1]~=0
  72.     --
  73.     end
  74.    
  75.   end
  76. end
  77.  
  78. -- FIXME, bizhawk does not seem to have something to register function like this
  79. -- Hook the display function to the GUI update
  80. -- local on_gui_update_old = gui.register()
  81. local function on_gui_update_new()
  82.   if on_gui_update_old then
  83.     on_gui_update_old()
  84.   end
  85.   my_display()
  86. end
  87. -- gui.register(on_gui_update_new)
  88.  
  89. -- Create a public RNG object to interact with
  90. rng      = {}
  91. rng.show = function() targetxpos = 0 end
  92. rng.hide = function() targetxpos = -(getCollapsedWidth()+1) end
  93. rng.set_title = function( new_title ) title = new_title end
  94. rng.set_text = function( new_text ) my_text = new_text end
  95. rng.set_nr_rows = function( new_nr_rows ) nr_rows = new_nr_rows end
  96. rng.set_nr_columns = function( new_nr_columns ) nr_columns = new_nr_columns end
  97. rng.set_column_width = function( new_column_width ) column_width = new_column_width end
  98.  
  99. while true do
  100.         my_display()
  101.   emu.frameadvance()
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement