local loadedorsmth = false
local ysnap = 158
addHook("PlayerThink",  function(p)
    if p.mo and p.mo.valid and not p.bot then
        local ticket = FixedHypot(FixedHypot(p.rmomx, p.rmomy), p.mo.momz)
        p.miles = (ticket / FRACUNIT) * (1 + (2 / 5))
        p.kilos = (ticket / FRACUNIT) * (2 + (1 / 5))
    end
end)

local function TooFast(player, arg)
    if arg then
        if arg == "1" or arg == "on" or arg == "fracs" or arg == "fracunits" then
			if io and player == consoleplayer then
				local file = io.openlocal("client/speedometer.dat", "w+")
				file:write(arg)
				file:close()
			end
            player.howfast = 1
            CONS_Printf(player, "Speed will be displayed in Fracunits per Tic")
        elseif arg == "miles" or arg == "2" or arg == "mph" then
			if io and player == consoleplayer then
				local file = io.openlocal("client/speedometer.dat", "w+")
				file:write(arg)
				file:close()
			end
            player.howfast =  2
            CONS_Printf(player, "Speed will be displayed in miles per hour")
        elseif arg == "3" or arg == "kilometers" or arg == "kmh" then
			if io and player == consoleplayer then
				local file = io.openlocal("client/speedometer.dat", "w+")
				file:write(arg)
				file:close()
			end
            player.howfast = 3
            CONS_Printf(player, "Speed will be displayed in kilometers per hour.")
        elseif arg == "0" or arg == "off" or arg == "disable" then
			if io and player == consoleplayer then
				local file = io.openlocal("client/speedometer.dat", "w+")
				file:write(arg)
				file:close()
			end
            player.howfast = 4
            CONS_Printf(player, "Speedometer is diabled.")
        end
    else
        CONS_Printf(player, "Changes the style of the speed display")
    end
end

COM_AddCommand("speedometer", function(player, arg)
	TooFast(player, arg)
end)

local function SPEEDHUD(v, p)
    if (p.mrce and p.mrce.hud == 1)  or modeattacking then
        ysnap = 158
    else
        ysnap = 170
    end
	if gamemap >95 and gamemap < 101 then return end
    if p.howfast == 1 then
        v.drawString(4, ysnap, ("\x89Player Speed: \x88" .. tostring(FixedHypot(FixedHypot(p.rmomx, p.rmomy), p.realmo.momz) / FRACUNIT) .. " FU / T"), V_ALLOWLOWERCASE|V_PERPLAYER|V_SNAPTOLEFT|V_HUDTRANSHALF, "thin")
    elseif p.howfast == 2 then
        v.drawString(4, ysnap, ("\x89Player Speed: \x88" .. tostring(p.miles) .. " mph"), V_ALLOWLOWERCASE|V_PERPLAYER|V_SNAPTOLEFT|V_HUDTRANSHALF, "thin")
    elseif p.howfast == 3 then
        v.drawString(4, ysnap, ("\x89Player Speed: \x88" .. tostring(p.kilos) .. " kmh"), V_ALLOWLOWERCASE|V_PERPLAYER|V_SNAPTOLEFT|V_HUDTRANSHALF, "thin")
    end
end

hud.add(SPEEDHUD)

addHook("PlayerSpawn", function(p)
	p.howfast = $ or 1
	if io and p == consoleplayer and not loadedorsmth then
		local file = io.openlocal("client/speedometer.dat")
		if file then
			local string = file:read("*a")
			if string == "1" or string == "fracs" or string == "on" or string == "fracunits" or string == nil then
				COM_BufInsertText(p, "speedometer 1")
			elseif string == "2" or string == "miles" or string == "mph" then
				COM_BufInsertText(p, "speedometer 2")
			elseif string == "3" or string == "kilometers" or string == "kmh" then
				COM_BufInsertText(p, "speedometer 3")
			elseif string == "0" or string == "disable" or string == "off" then
				COM_BufInsertText(p, "speedometer 0")
			end
			file:close()
		else
			COM_BufInsertText(p, "speedometer 1")
		end
        loadedorsmth = true
	end
end)