User File #49023977547981422

Upload All User Files

#49023977547981422 - Super Monkey Ball initial lua script

smb.lua
747 downloads
Uploaded 8/8/2018 6:59 PM by xy2_ (see all 139)
mword = memory.readdwordsigned
mwordu = memory.readdwordunsigned

local ds_width = 256
local ds_height = 192

function toBits(num,bits)
	-- Simple binary packing function
	-- To unpack, tonumber(num, 2)
	-- Returns a table of bits, most significant first.
	bits = bits or math.max(1, select(2, math.frexp(num)))
	local t = {} -- will contain the bits        
	for b = bits, 1, -1 do
		t[b] = math.fmod(num, 2)
		num = math.floor((num - t[b]) / 2)
	end
	return table.concat(t)
end

-- https://en.wikipedia.org/wiki/Q_(number_format)#Characteristics
-- Here, the sign bit is included in m, because n+m = 32, which is equal to
-- the length of the DS adress bus
-- Unpacking function
local function Q(number, m, n)
	local max = m+n
	local packed = toBits(number, 32)
	
	local sign = tonumber(string.sub(packed, 1, 1))
	local unsignedpacked = string.sub(packed, 2, max)
	
	-- If the number is signed: NOT the number, add 1 (one's complement)
	if sign == 1 then
		packed = toBits(bit.bnot(tonumber(packed,2))+1,32)
	end
	
	local integer = tonumber(string.sub(packed, 2, m),2) -- As usual, Lua indexes start at 1..
	local fractional = (tonumber(string.sub(packed, m+1, max), 2))/(2^n)
	local total = integer + fractional
	
	if sign == 1 then
		total = total * -1
	end
	
	return total
end

local function main()
	local pointer1 = mwordu(0x021FBA14)
	local zpos = Q(mwordu(pointer1 + 0xC),21,11)
	local ypos = Q(mwordu(pointer1 + 0x10),21,11)
	local xpos = Q(mwordu(pointer1 + 0x14),21,11)
	
	local zspeed = Q(mwordu(pointer1 + 0x24),21,11)
	local yspeed = Q(mwordu(pointer1 + 0x28),21,11)
	local xspeed = Q(mwordu(pointer1 + 0x3B),21,11)
	
	local time = memory.readwordunsigned(0x021FBA20)
	local minutes = math.floor(time/60)
	local seconds = math.floor(((time%60)*100/60))
	local frame = math.floor(time%60)

	gui.text(0,0,xpos)
	gui.text(0,8,ypos)
	gui.text(0,16,zpos)
	gui.text(0,28,xspeed,"green")
	gui.text(0,36,yspeed,"green")
	gui.text(0,44,zspeed,"green")
	
	gui.text(ds_width - 82,0,time)
	gui.text(ds_width - 52,0,minutes..":"..seconds.."["..frame.."]","yellow")

end

gui.register(main)