User File #45319368046140001

Upload All User Files

#45319368046140001 - GBA DK: King of Swing - average speed hud v1.2

DKKoS average speed hud v1.2.lua
859 downloads
Uploaded 2/22/2018 10:52 PM by ThunderAxe31 (see all 111)
Script that calculates the average speed amount, starting from the moment it's launched. Every time you want to do a new measurement, you must restart script execution.
--Position and average speed hud values v1.2 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_average = 0
local player_y = 0
local player_y_old = 0
local speed_y_average = 0
local speed_x_average_list = {}
local speed_y_average_list = {}
local pos_x_all = ""
local pos_y_all = ""
local frames_amount = 0

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_average_list = {}
			speed_y_average_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
		
		if framecount == emu.framecount()-1 then
			frames_amount = frames_amount + 1
			
			speed_x_average = ( ( speed_x_average * ( frames_amount - 1 ) ) +player_x -player_x_old ) / frames_amount
			speed_y_average = ( ( speed_y_average * ( frames_amount - 1 ) ) +player_y -player_y_old ) / frames_amount
			
			if string.find(speed_x_average, "E") ~= nil then
				speed_x_average = 0
			end
			if string.find(speed_y_average, "E") ~= nil then
				speed_y_average = 0
			end
		end
		
		if speed_x_average >= 0 then
			speed_x_average = string.sub(speed_x_average, 0, 4)
		else
			speed_x_average = string.sub(speed_x_average, 0, 5)
		end
		if speed_y_average >= 0 then
			speed_y_average = string.sub(speed_y_average, 0, 4)
		else
			speed_y_average = string.sub(speed_y_average, 0, 5)
		end
		
		for i=21, 1, -1 do
			speed_x_average_list[i]=speed_x_average_list[i-1]
			speed_y_average_list[i]=speed_y_average_list[i-1]
			
			gui.pixelText( 33, 7+i*7, speed_x_average_list[i], decide_color_horizontal(speed_x_average_list[i], x_middle, x_max))
			gui.pixelText(219, 7+i*7, speed_y_average_list[i], decide_color_vertical(speed_y_average_list[i], y_low, y_max))
		end
		
		last_found = 0
		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_average_list[0] = speed_x_average
			speed_y_average_list[0] = speed_y_average
		end
		
		gui.pixelText(82, 0, "Frames counted: " .. frames_amount)
		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_average_list[0], decide_color_horizontal(speed_x_average_list[0], x_middle, x_max))
		gui.pixelText(219, 7, speed_y_average_list[0], decide_color_vertical(speed_y_average_list[0], y_low, y_max))
		
		framecount = emu.framecount()
		
		emu.frameadvance()
	end
else
	console.log("Error: failed to set EWRAM memory domain")
end