User File #57036697529347266

Upload All User Files

#57036697529347266 - Kirby Super Star - RNG view Lua (for Snes9x)

KirbySuperStar_RNGview.lua
601 downloads
Uploaded 8/4/2019 3:31 PM by WaddleDX (see all 4)
function bitSeparate(byte)
	local arr = {}
	for i = 1, 9 do
		arr[i] = bit.band(bit.arshift(byte, 8-i), 1)
	end
	return arr
end

function bittoint(arr)
	n = arr[1]*128 + arr[2]*64 + arr[3]*32 + arr[4]*16 + arr[5]*8 + arr[6]*4 + arr[7]*2 + arr[8]
	return n
end

function nextRnd(byte1, byte2)
	local c1 = {}
	local c2 = {}
	local n1 = {}
	local n2 = {}
	
	c1 = bitSeparate(byte1)
	c2 = bitSeparate(byte2)
	
	n1[1] = bit.bxor(1, c1[7], c1[8], c2[1], c2[3], c2[4])
	n1[2] = bit.bxor(c1[7], c2[1], c2[2], c2[4], c2[5])
	n1[3] = bit.bxor(c1[8], c2[2], c2[3], c2[5], c2[6])
	n1[4] = bit.bxor(n1[1], c2[6], c2[7])
	n1[5] = bit.bxor(n1[2], c2[7], c2[8])
	n1[6] = bit.bxor(n1[3], c2[8], c1[1])
	n1[7] = bit.bxor(n1[4], c1[1], c1[2])
	n1[8] = bit.bxor(n1[5], c1[2], c1[3])
	
	n2[1] = bit.bxor(c1[4])
	n2[2] = bit.bxor(c1[5])
	n2[3] = bit.bxor(c1[6])
	n2[4] = bit.bxor(c1[7])
	n2[5] = bit.bxor(c1[8])
	n2[6] = bit.bxor(1, c1[7], c1[8], c2[1])
	n2[7] = bit.bxor(c1[7], c2[1], c2[2])
	n2[8] = bit.bxor(c1[8], c2[2], c2[3])
	
	return n1, n2
end

function prevRnd(byte1, byte2)
	local c1 = {}
	local c2 = {}
	local p1 = {}
	local p2 = {}
	
	c1 = bitSeparate(byte1)
	c2 = bitSeparate(byte2)
	
	p1[1] = bit.bxor(1, c1[4], c1[5], c1[6])
	p1[2] = bit.bxor(1, c1[5], c1[6], c1[7])
	p1[3] = bit.bxor(1, c1[6], c1[7], c1[8])
	p1[4] = bit.bxor(c2[1])
	p1[5] = bit.bxor(c2[2])
	p1[6] = bit.bxor(c2[3])
	p1[7] = bit.bxor(c2[4])
	p1[8] = bit.bxor(c2[5])
	
	p2[1] = bit.bxor(1, c2[4], c2[5], c2[6])
	p2[2] = bit.bxor(1, c2[5], c2[6], c2[7])
	p2[3] = bit.bxor(1, c2[6], c2[7], c2[8])
	p2[4] = bit.bxor(1, c2[7], c2[8], c1[1])
	p2[5] = bit.bxor(1, c2[8], c1[1], c1[2])
	p2[6] = bit.bxor(1, c1[1], c1[2], c1[3])
	p2[7] = bit.bxor(1, c1[2], c1[3], c1[4])
	p2[8] = bit.bxor(1, c1[3], c1[4], c1[5])
	
	return p1, p2
end

function createRndTable(rnd1, rnd2)
	rnd_Table[rnd_curNum][1] = rnd1
	rnd_Table[rnd_curNum][2] = rnd2
	
	for i = 0, rnd_prevNum - 1 do
		local r = rnd_curNum - i
		local prev1 = {}
		local prev2 = {}
		local prev1, prev2 = prevRnd(rnd_Table[r][1], rnd_Table[r][2])
		for j = 1, 9 do
			rnd_Table[r-1][j+2] = prev1[j]
			rnd_Table[r-1][j+10] = prev2[j]
		end
		rnd_Table[r-1][1] = bittoint(prev1)
		rnd_Table[r-1][2] = bittoint(prev2)
	end
	
	for i = 0, rnd_nextNum - 1 do
		local r = rnd_curNum + i
		local next1 = {}
		local next2 = {}
		local next1, next2 = nextRnd(rnd_Table[r][1], rnd_Table[r][2])
		for j = 1, 9 do
			rnd_Table[r+1][j+2] = next1[j]
			rnd_Table[r+1][j+10] = next2[j]
		end
		rnd_Table[r+1][1] = bittoint(next1)
		rnd_Table[r+1][2] = bittoint(next2)
	end
	
	local top = 21 - (rnd_curNum + rnd_nextNum + 1)
	gui.text (txtposx+(range*22), txtposy+(range*top), "Rnd")
	gui.text (txtposx+(range*22), txtposy+(range*(top+rnd_curNum)), "Cur")
	for i = 1, rnd_curNum + rnd_nextNum do
		gui.text (txtposx+(range*24), txtposy+(range*(top+i)), string.format ("%02X  %02X", rnd_Table[i][1], rnd_Table[i][2]))
	end
end

-- Main

emu = snes9x
if not emu then
	error("This script runs under snes9x.")
end

range = 9
txtposx = 6
txtposy = 9

rnd_prevNum = 3
rnd_nextNum = 12
rnd_curNum = rnd_prevNum + 1
rnd_Table = {}

for i = 1, rnd_curNum + rnd_nextNum do
	rnd_Table[i] = {}
end

local rnd_Table

local randomcall = 0

memory.registerexec(0x008a9f, 2, function()
	randomcall = randomcall + 1

end)

gui.register(function()
	local random = memory.readbyte(0x003743)
	local random2 = memory.readbyte(0x003744)
	local x = memory.readwordsigned(0x700988) + memory.readword(0x700A7D) / 256
	local y = memory.readwordsigned(0x700A02) + memory.readword(0x700AF7) / 256
	local xs = memory.readwordsigned(0x7016BF)
	local ys = memory.readwordsigned(0x700BEA)
	gui.text (txtposx+(range*19), txtposy+(range*0), string.format ("Variable RN  : %01d", randomcall))
	gui.text (txtposx+(range*19), txtposy+(range*1), string.format ("Random number: %02X", random))
	gui.text (txtposx+(range*19), txtposy+(range*2), string.format ("Random number: %02X", random2))
	gui.text (txtposx+(range*3), txtposy+(range*2), "Kirby")
	gui.text (txtposx, txtposy+(range*3), string.format ("X-Pos  : %.03f", x))
	gui.text (txtposx, txtposy+(range*4), string.format ("Y-Pos  : %.03f", y))
	gui.text (txtposx, txtposy+(range*5), string.format ("X-Speed: % 01d", xs))
	gui.text (txtposx, txtposy+(range*6), string.format ("Y-Speed: % 01d", ys))
	
	createRndTable(random, random2)

	local helper = memory.readword(0x7014A1)
	if 1 <= helper then
	if helper <= 19 then
		local xh = memory.readwordsigned(0x70098A) + memory.readword(0x700A7F) / 256
		local yh = memory.readwordsigned(0x700A04) + memory.readword(0x700AF9) / 256
		local xsh = memory.readwordsigned(0x7016C1)
		local ysh = memory.readwordsigned(0x700BEC)
		local place = memory.readbyte(0x701451)
		local life = memory.readbytesigned(0x700EC8)
		gui.text (txtposx+(range*3), txtposy+(range*8), "Helper")
		gui.text (txtposx, txtposy+(range*9), string.format ("X-Pos  : %.03f", xh))
		gui.text (txtposx, txtposy+(range*10), string.format ("Y-Pos  : %.03f", yh))
		gui.text (txtposx, txtposy+(range*11), string.format ("X-Speed: % 01d", xsh))
		gui.text (txtposx, txtposy+(range*12), string.format ("Y-Speed: % 01d", ysh))
		gui.text (txtposx, txtposy+(range*13), string.format ("Place  : %01d", place))
		gui.text (txtposx, txtposy+(range*14), string.format ("life count: %01d", life))
	end
end

	local moviename = movie.name()
	if movie.mode()  then
		local rerecordcount = movie.rerecordcount()
	--	gui.text (txtposx, txtposy+(range*19), moviename)
		gui.text (txtposx, txtposy+(range*20), "Movie Recording / Replay")
		gui.text (txtposx, txtposy+(range*21), string.format("ReRecording Count:%01d", rerecordcount))
	else
		 gui.text (txtposx, txtposy+(range*20), "No Movie!")
	end
end)

while true do
	emu.frameadvance()
end