User File #45672300981449793

Upload All User Files

#45672300981449793 - GBA Crash HA - average speed hud v1.1

Crash HA average speed hud v1.1.lua
944 downloads
Uploaded 3/10/2018 8:20 PM by ThunderAxe31 (see all 111)
updating with better code.
--Position and average speed hud values v1.1 by ThunderAxe31 for GBA Crash Bandicoot - The Huge Adventure
--If the script encounters a speed amount that goes over the known limit, it will show it in blue. Otherwise:
local x_max    =  1224 --initial slide speed
local x_middle =   512 --target walking speed
local y_max    =  1536 --highest falling speed
local y_low    = -1229 --initial jumping speed

local player_x_addr = 0x010A14
local player_y_addr = 0x010A18
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 = memory.read_u32_le(player_x_addr)
		player_y_old = player_y
		player_y = memory.read_u32_le(player_y_addr)
		
		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
		end
		
		if speed_x_average >= 0 then
			speed_x_average = string.sub(speed_x_average, 0, 6)
		else
			speed_x_average = string.sub(speed_x_average, 0, 8)
		end
		if speed_y_average >= 0 then
			speed_y_average = string.sub(speed_y_average, 0, 6)
		else
			speed_y_average = string.sub(speed_y_average, 0, 8)
		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( 29, 7+i*7, speed_x_average_list[i], decide_color_horizontal(speed_x_average_list[i], x_middle, x_max))
			gui.pixelText(211, 7+i*7, speed_y_average_list[i], decide_color_vertical(speed_y_average_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_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  AvSpeed")
		gui.pixelText(183, 0, "Y Pos  AvSpeed")
		gui.pixelText(  0, 7, pos_x_all)
		gui.pixelText(182, 7, pos_y_all)
		gui.pixelText( 29, 7, speed_x_average_list[0], decide_color_horizontal(speed_x_average_list[0], x_middle, x_max))
		gui.pixelText(211, 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