User File #45319532279038868

Upload All User Files

#45319532279038868 - GBA DK: King of Swing - speed hud v1.1

DKKoS speed hud v1.1.lua
926 downloads
Uploaded 2/22/2018 11:03 PM by ThunderAxe31 (see all 111)
Fixed a bug that sometimes made the speed value slightly incorrect.
Also, the script now distinguishes between maximum jumping speed and maximum falling speed.
--Position and average speed hud values v1.1 by ThunderAxe31 for GBA DK: King of Swing
--If the script encounters a speed amount that goes over the known limit, it will show it in blue. Otherwise:
local x_max    =  1.34 --target horizontal speed for walking
local x_middle =  1.13 --target horizontal speed for moving mid-air
local y_max    =  1.12 --target falling speed
local y_low    = -1.87 --initial vertical speed for jumping

local player_x_addr = 0x022012
local player_y_addr = 0x022016
local player_x_sub_addr = 0x022010
local player_y_sub_addr = 0x022014
local framecount = -1
local player_x = 0
local player_x_old = 0
local speed_x = 0
local player_y = 0
local player_y_old = 0
local speed_y = 0
local speed_x_list = {}
local speed_y_list = {}
local pos_x_all = ""
local pos_y_all = ""

function decide_color_horizontal(speed, middle, maxi)
	if speed == nil then 
		return
	end
	
	speed = math.abs(speed)
	maxi = math.abs(maxi)
	
	if speed > maxi then --in case we broke the limit, use blue color
		color = 0x000000FF
	elseif speed < middle then --if 0: red; if middle: white
		local value = math.floor(0xFF*(speed/middle))
		color = 0xFF0000 + value*0x101
	else --if middle: white; if maxi: green
		local value = math.floor(0xFF*(1-(speed-middle)/(maxi-middle)))
		color = 0x00FF00 + value*0x10001
	end
	return color+0xFF000000
end

function decide_color_vertical(speed, low, maxi)
	if speed == nil then 
		return
	end
	
	speed = tonumber(speed)
	
	if speed > y_max or speed < y_low then --in case we broke the limit, use blue color
		color = 0x000000FF
	elseif speed >= 0 then -- if 0: white; if low: green
		local value = math.floor(0xFF*(1-speed/maxi))
		color = 0x00FF00 + value*0x10001
	else -- if 0: white; if maxi: green
		local value = math.floor(0xFF*(1-(speed*-1)/(low*-1)))
		color = 0x00FF00 + value*0x10001
	end	
	return color+0xFF000000
end

if memory.usememorydomain("EWRAM") then
--	console.log("Starting script at frame: " .. emu.framecount() .. " with X: " .. player_x .. ", Y: " .. player_y)
	
	while true do
		if framecount ~= emu.framecount()-1 then
			pos_x_all = ""
			pos_y_all = ""
			speed_x_list = {}
			speed_y_list = {}
		end
		
		player_x_old = player_x
		player_x = string.sub(memory.read_u16_le(player_x_addr)+memory.read_u16_le(player_x_sub_addr)/65536, 0, 7)
		player_y_old = player_y		
		player_y = string.sub(memory.read_u16_le(player_y_addr)+memory.read_u16_le(player_y_sub_addr)/65536, 0, 7)
		
		pos_x_all = player_x .. "\n" .. pos_x_all
		pos_y_all = player_y .. "\n" .. pos_y_all
		
		speed_x = player_x -player_x_old
		speed_y = player_y -player_y_old
		
		if speed_x >= 0 then
			speed_x = " " .. string.sub(speed_x, 0, 4)
		else
			speed_x = string.sub(speed_x, 0, 5)
		end
		if speed_y >= 0 then
			speed_y = " " .. string.sub(speed_y, 0, 4)
		else
			speed_y = string.sub(speed_y, 0, 5)
		end
		
		for i=21, 1, -1 do
			speed_x_list[i]=speed_x_list[i-1]
			speed_y_list[i]=speed_y_list[i-1]
			
			gui.pixelText( 33, 7+i*7, speed_x_list[i], decide_color_horizontal(speed_x_list[i], x_middle, x_max))
			gui.pixelText(219, 7+i*7, speed_y_list[i], decide_color_vertical(speed_y_list[i], y_low, y_max))
		end
		
		local last_found = 0
		local occurences = 0
		while last_found ~= nil do
			occurences = occurences + 1
			if occurences > 22 then
				pos_x_all = string.sub(pos_x_all, 0, last_found)
			end
			last_found = string.find(pos_x_all, "\n", last_found+1)
		end
		
		last_found = 0
		occurences = 0
		while last_found ~= nil do
			occurences = occurences + 1
			if occurences > 22 then
				pos_y_all = string.sub(pos_y_all, 0, last_found)
			end
			last_found = string.find(pos_y_all, "\n", last_found+1)
		end
		
		if framecount == emu.framecount()-1 then
			speed_x_list[0] = speed_x
			speed_y_list[0] = speed_y
		end
		
		gui.pixelText(  0, 0, "X Pos   Speed")
		gui.pixelText(187, 0, "Y Pos   Speed")
		gui.pixelText(  0, 7, pos_x_all)
		gui.pixelText(186, 7, pos_y_all)
		gui.pixelText( 33, 7, speed_x_list[0], decide_color_horizontal(speed_x_list[0], x_middle, x_max))
		gui.pixelText(219, 7, speed_y_list[0], decide_color_vertical(speed_y_list[0], y_low, y_max))
		
		framecount = emu.framecount()
		
		emu.frameadvance()
	end
else
	console.log("Error: failed to set EWRAM memory domain")
end