Skip to content

Instantly share code, notes, and snippets.

@Skarsnik
Created August 20, 2016 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skarsnik/073cd95f4c979e6022b629cb798e1ba6 to your computer and use it in GitHub Desktop.
Save Skarsnik/073cd95f4c979e6022b629cb798e1ba6 to your computer and use it in GitHub Desktop.
-- Window properties
local title = " RNG "
local nr_rows = 16
local nr_columns = 2
local column_width = 16
local text_size = 10;
local targetxpos = 0
local targetypos = 36
local xpos = targetxpos
local ypos = targetypos
local is_snes9x = true
function DrawNiceText(text_x, text_y, str, color)
--local sh = client.screenheight
--local sw = client.screenwidth
if is_snes9x then
gui.text(text_x, text_y, str, color)
else
local calc_x = client.transformPointX(text_x)
local calc_y = client.transformPointY(text_y)
gui.text(calc_x, calc_y, str, 0xFF000000, color)
end
end
local getCollapsedWidth = function()
return (math.max(column_width,string.len(title))+1)*4
end
local my_text = function(x,y,counter,rn)
DrawNiceText(x,y,string.upper(string.format('%02x:%02x',counter,rn)), 0xFFFFFFFF)
-- console.writeline(string.upper(counter..rn));
end
local my_display = function()
-- Update the position of the RNG window
if ypos<targetypos then
ypos = ypos + 1
elseif ypos>targetypos then
ypos = ypos - 1
end
if xpos<targetxpos then
xpos = xpos + 1
elseif xpos>targetxpos then
xpos = xpos - 1
end
-- Read the current state of the RNG
local currentRNG = mainmemory.readbyterange(0x0408, 16)
--console.writeline(currentRNG)
-- Render the title box
--gui.drawBox (xpos,ypos-9,xpos+4*(string.len(title)+1) ,ypos-1,0xFF000000)
DrawNiceText(xpos+3,ypos-8,title, 0xFFFFFFFF)
-- Render the RNG boxes
for c=0,math.floor(nr_columns)-1 do
local xcolumn = xpos+c*(column_width+1)*4*(1-math.abs(xpos/getCollapsedWidth()))
gui.drawBox (xcolumn, ypos, xcolumn+(column_width+1)*4 ,ypos+nr_rows*8+2,0x8000000000)
for n=0,math.floor(nr_rows)-1 do
-- Render the text
my_text(xcolumn+3,ypos+(n)*16+2,currentRNG[15],currentRNG[0])
-- Update the set of random numbers to predict the next one
local carry = 0
local temp
for i=15,1,-1 do
temp = currentRNG[i-1]+currentRNG[i]+carry
currentRNG[0x0408 + i-1] = bit.band(temp,0xff)
carry = (temp-bit.band(temp,0xff))/256
end
i=15
repeat
currentRNG[i]=bit.band(currentRNG[i]+1,0xff)
i=i-1
until i<1 or currentRNG[i+1]~=0
--
end
end
end
-- FIXME, bizhawk does not seem to have something to register function like this
-- Hook the display function to the GUI update
-- local on_gui_update_old = gui.register()
local function on_gui_update_new()
if on_gui_update_old then
on_gui_update_old()
end
my_display()
end
-- gui.register(on_gui_update_new)
-- Create a public RNG object to interact with
rng = {}
rng.show = function() targetxpos = 0 end
rng.hide = function() targetxpos = -(getCollapsedWidth()+1) end
rng.set_title = function( new_title ) title = new_title end
rng.set_text = function( new_text ) my_text = new_text end
rng.set_nr_rows = function( new_nr_rows ) nr_rows = new_nr_rows end
rng.set_nr_columns = function( new_nr_columns ) nr_columns = new_nr_columns end
rng.set_column_width = function( new_column_width ) column_width = new_column_width end
while true do
my_display()
emu.frameadvance()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment