freeslot("SPR_AIDU", "S_KICKDUST")

states[S_KICKDUST] = {
	sprite = SPR_AIDU,
	frame = FF_ANIMATE | FF_PAPERSPRITE,
	var1 = 5,
	var2 = 1,
	tics = 6
}

local commandargs = {
	on = 1,
	off = 0
}

addHook("PlayerSpawn", function(p)
	p.juniodust = 0
	p.groundjuniodust = 0
	p.juniowaterspeed = 0
end)

COM_AddCommand("juniokickdust", function(p, arg)
    io.openlocal("client/juniokickdust.txt", "a"):close()
    local file = io.openlocal("client/juniokickdust.txt", "w")
	if not arg == commandargs[arg]
		print("Enables or Disable the Kick Dust (On or Off)")
		return
	end
	if string.lower(commandargs[arg])
		p.juniodust = commandargs[arg]
        file:write(commandargs[arg])
		print("Junio Kick Dust Settings are now saved.")
    else
        print("Enables or Disable the Kick Dust (On or Off)")
    end
    file:close()
end)

local function juniodustset(p)
local file = io.openlocal("client/juniokickdust.txt", "r")
	if file then
		local text = file:read("*a")
		if text == ""
			p.juniodust = 1
		else
			p.juniodust = tonumber(text)
		end
		file:close()
	else
		p.juniodust = 1
	end
end

addHook("PlayerThink", juniodustset)

COM_AddCommand("juniogroundkickdust", function(p, arg)
    io.openlocal("client/juniokickdustground.txt", "a"):close()
    local file = io.openlocal("client/juniokickdustground.txt", "w")
	if not arg == commandargs[arg]
		print("Enables or Disable the Kick Dust when running fast on the ground (On or Off)")
		return
	end
    if string.lower(commandargs[arg]) 
        p.groundjuniodust = commandargs[arg]
        file:write(commandargs[arg])
		print("Junio Ground Kick Dust Settings are now saved.")
    else
        print("Enables or Disable the Kick Dust when running fast on the ground (On or Off)")
    end
    file:close()
end)

local function juniodustgroundset(p)
local file = io.openlocal("client/juniokickdustground.txt", "r")
	if file then
		local text = file:read("*a")
		if text == ""
			p.groundjuniodust = 15
		else
			p.groundjuniodust = tonumber(text)
		end
		file:close()
	else
		p.groundjuniodust = 15
	end
end

addHook("PlayerThink", juniodustgroundset)

COM_AddCommand("juniokickdustwaterspeed", function(p, arg)
    io.openlocal("client/juniokickdustwaterspeed.txt", "a"):close()
    local file = io.openlocal("client/juniokickdustwaterspeed.txt", "w")
	if not commandargs[arg] == arg
		print("Change the speed of how fast you need to be to see the dust when on the water.")
		return
	end
    if arg
        p.juniowaterspeed = arg
        file:write(arg)
		print("Junio Kick Dust Water Speed Settings are now saved.")
    else
        print("Change the speed of how fast you need to be to see the dust when on the water.")
    end
    file:close()
end)

local function juniodustwaterspeedset(p)
local file = io.openlocal("client/juniokickdustwaterspeed.txt", "r")
	if file then
		local text = file:read("*a")
		if text == ""
			p.juniowaterspeed = 5
		else
			p.juniowaterspeed = tonumber(text)
		end
		file:close()
	else
		p.juniowaterspeed = 5
	end
end

addHook("PlayerThink", juniodustwaterspeedset)

COM_AddCommand("juniokickdustgroundspeed", function(p, arg)
    io.openlocal("client/juniokickdustgroundspeed.txt", "a"):close()
    local file = io.openlocal("client/juniokickdustgroundspeed.txt", "w")
	if not commandargs[arg] == arg
		print("Change the speed of how fast you need to be to see the dust when on the ground.")
		return
	end
    if (arg)
        p.juniogroundspeed = arg
        file:write(arg)
		print("Junio Kick Dust Ground Speed Settings are now saved.")
    else
        print("Change the speed of how fast you need to be to see the dust when on the ground.")
    end
    file:close()
end)

local function juniokickdustgroundspeedset(p)
local file = io.openlocal("client/juniokickdustgroundspeed.txt", "r")
	if file then
		local text = file:read("*a")
		if text == ""
			p.juniogroundspeed = 15
		else
			p.juniogroundspeed = tonumber(text)
		end
		file:close()
	else
		p.juniogroundspeed = 15
	end
end

addHook("PlayerThink", juniokickdustgroundspeedset)					
		
addHook("PlayerThink", function(player)
	if player.mo and player.mo.valid then
		if player.mo.skin ~= "ultsonic" return end
		if player.speed >= player.runspeed + player.juniowaterspeed*FRACUNIT 
		and player.juniodust == 1
		and (leveltime % 3 == 0)
		and P_IsObjectOnGround(player.mo)
		and player.mo.eflags & MFE_TOUCHWATER
		and not (P_PlayerTouchingSectorSpecial(player, 1, 3))
			for i = -1, 1, 2
				local ang = i * ANG10
				local factor = FixedMul(4*player.mo.scale, cos(ang))
				local x = P_ReturnThrustX(player.mo, player.drawangle + ANGLE_180 + ang, 24*player.mo.scale)
				local y = P_ReturnThrustY(player.mo, player.drawangle + ANGLE_180 + ang, 24*player.mo.scale)
				local dust = P_SpawnMobjFromMobj(player.mo, x, y, 0, MT_THOK)
				dust.blendmode = AST_ADD
				dust.scale = 2*$/3
				dust.angle = R_PointToAngle2(dust.x, dust.y, player.mo.x, player.mo.y)
				dust.state = S_KICKDUST
				dust.momx = FixedMul(factor, cos(dust.angle - i*ANGLE_90))
				dust.momy = FixedMul(factor, sin(dust.angle - i*ANGLE_90))
			end
		end
	end
end)

addHook("PlayerThink", function(player)
	if player.mo and player.mo.valid then
		if player.mo.skin ~= "ultsonic" return end
		if player.speed >= player.juniogroundspeed*FRACUNIT 
		and player.juniodust == 1
		and player.groundjuniodust == 1
		and (leveltime % 3 == 0)
		and P_IsObjectOnGround(player.mo)
		and not (P_PlayerTouchingSectorSpecial(player, 1, 3))
		and not (player.mo.eflags & MFE_UNDERWATER)
			for i = -1, 1, 2
				local ang = i * ANG10
				local factor = FixedMul(4*player.mo.scale, cos(ang))
				local x = P_ReturnThrustX(player.mo, player.drawangle + ANGLE_180 + ang, 24*player.mo.scale)
				local y = P_ReturnThrustY(player.mo, player.drawangle + ANGLE_180 + ang, 24*player.mo.scale)
				local dust = P_SpawnMobjFromMobj(player.mo, x, y, 0, MT_THOK)
				dust.scale = 2*$/3
				dust.angle = R_PointToAngle2(dust.x, dust.y, player.mo.x, player.mo.y)
				dust.state = S_KICKDUST
				dust.momx = FixedMul(factor, cos(dust.angle - i*ANGLE_90))
				dust.momy = FixedMul(factor, sin(dust.angle - i*ANGLE_90))
			end
		end
	end
end)