Advertisement
Guest User

terranigma_rng.lua

a guest
May 31st, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. -- Window properties
  2. local title = "     RNG     "
  3. local nr_rows = 16
  4. local nr_columns = 2
  5. local column_width = 6
  6.  
  7. local targetxpos = 0
  8. local targetypos = 36
  9. local xpos = targetxpos
  10. local ypos = targetypos
  11.  
  12. local getCollapsedWidth = function()
  13.   return (math.max(column_width,string.len(title))+1)*4
  14. end
  15.  
  16. local my_text = function(x,y,counter,rn)
  17.   gui.text(x,y,string.upper(string.format('%02x: %02x',counter,rn)))
  18. end
  19.  
  20. local my_display = function()
  21.  
  22.   -- Update the position of the RNG window
  23.   if ypos<targetypos then
  24.     ypos = ypos + 1
  25.   elseif ypos>targetypos then
  26.     ypos = ypos - 1
  27.   end
  28.   if xpos<targetxpos then
  29.     xpos = xpos + 1
  30.   elseif xpos>targetxpos then
  31.     xpos = xpos - 1
  32.   end
  33.  
  34.   -- Read the current state of the RNG
  35.   local currentRNG = memory.readbyterange(0x7e0408,16)
  36.  
  37.   -- Render the title box
  38.   gui.box (xpos,ypos-9,xpos+4*(string.len(title)+1) ,ypos-1,0x000000FF)
  39.   gui.text(xpos+3,ypos-8,title)
  40.  
  41.   -- Render the RNG boxes
  42.   for c=0,math.floor(nr_columns)-1 do
  43.  
  44.     local xcolumn = xpos+c*(column_width+1)*4*(1-math.abs(xpos/getCollapsedWidth()))
  45.     gui.box (xcolumn,  ypos,  xcolumn+(column_width+1)*4 ,ypos+nr_rows*8+2,0x00000080)
  46.  
  47.     for n=0,math.floor(nr_rows)-1 do
  48.    
  49.       -- Render the text
  50.       my_text(xcolumn+3,ypos+(n)*8+2,currentRNG[16],currentRNG[1])
  51.      
  52.       -- Update the set of random numbers to predict the next one
  53.       local carry = 0
  54.       local temp
  55.       for i=16,2,-1 do
  56.         temp  = currentRNG[i-1]+currentRNG[i]+carry
  57.         currentRNG[i-1] = AND(temp,0xff)
  58.         carry = (temp-AND(temp,0xff))/256
  59.       end
  60.  
  61.       i=16
  62.       repeat
  63.         currentRNG[i]=AND(currentRNG[i]+1,0xff)
  64.         i=i-1
  65.       until i<1 or currentRNG[i+1]~=0
  66.     --
  67.     end
  68.    
  69.   end
  70. end
  71.  
  72. -- Hook the display function to the GUI update
  73. local on_gui_update_old = gui.register()
  74. local function on_gui_update_new()
  75.   if on_gui_update_old then
  76.     on_gui_update_old()
  77.   end
  78.   my_display()
  79. end
  80. gui.register(on_gui_update_new)
  81.  
  82. -- Create a public RNG object to interact with
  83. rng      = {}
  84. rng.show = function() targetxpos = 0 end
  85. rng.hide = function() targetxpos = -(getCollapsedWidth()+1) end
  86. rng.set_title = function( new_title ) title = new_title end
  87. rng.set_text = function( new_text ) my_text = new_text end
  88. rng.set_nr_rows = function( new_nr_rows ) nr_rows = new_nr_rows end
  89. rng.set_nr_columns = function( new_nr_columns ) nr_columns = new_nr_columns end
  90. rng.set_column_width = function( new_column_width ) column_width = new_column_width end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement