User File #45318288373104743

Upload All User Files

#45318288373104743 - GBA Crash HA - speed hud v2.2

Crash HA speed hud v2.2.lua
905 downloads
Uploaded 2/22/2018 9:42 PM by ThunderAxe31 (see all 109)
Now the script distinguishes between maximum jumping speed and maximum falling speed. Hurray!
--Position and speed hud values v2.2 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 = 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 = 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
		
		speed_x = player_x -player_x_old
		speed_y = player_y -player_y_old
		
		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
		
		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_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