/*
	What should this script do:
	
	1: if A and B are pressed simultaneously
	2: check if grounded
	3: check if speed is under 10 FU/tic
	4: Do until any of the three checks are no longer true
		A: Charge up a blue mini-turbo
		B: Keep p speed at 0 FU/tic
	5: once blue mini-turbo is charged, any of the checks failing releases it

	Lets figure out how this shit works
*/

-- Sound definitions

freeslot("sfx_ssmtup", "sfx_ssmtus")

sfxinfo[sfx_ssmtup] = {
        singular = false,
        priority = 64,
        flags = SF_NOINTERRUPT 
}
sfxinfo[sfx_ssmtus] = {
        singular = false,
        priority = 64,
        flags = SF_NOINTERRUPT 
}

-- Pre-requisite functions (shoutouts to Darkybenji for the pspeed and drift spark functions that so my life isn't a living hell)

local function pspeed(p)
	return FixedDiv(p.speed, mapobjectscale)/FRACUNIT
end

local function SpawnSparksB(p)
	if (leveltime % 2 == 1) then return end

	local travelangle = p.mo.angle-(ANGLE_45/5)*p.kartstuff[k_drift]

	for i = 0, 1 do
		local newX = p.mo.x + P_ReturnThrustX(p.mo, travelangle + ((i == 0) and -1 or 1)*ANGLE_135, FixedMul(32*FRACUNIT, p.mo.scale))
		local newY = p.mo.y + P_ReturnThrustY(p.mo, travelangle + ((i == 0) and -1 or 1)*ANGLE_135, FixedMul(32*FRACUNIT, p.mo.scale))
		local Spark = P_SpawnMobj(newX, newY, p.mo.z, MT_DRIFTSPARK)
		Spark.target = p.mo
		Spark.angle = travelangle-(ANGLE_45/5)*p.kartstuff[k_drift]
		Spark.destscale = p.mo.scale*5/4
		P_SetScale(Spark, Spark.destscale)
		Spark.momx = p.mo.momx/2
		Spark.momy = p.mo.momy/2
		Spark.momz = p.mo.momz/2 
		Spark.color = SKINCOLOR_SAPPHIRE
		K_MatchGenericExtraFlags(Spark, p.mo)
	end
end

local function SpawnSparksR(p)
	if (leveltime % 2 == 1) then return end

	local travelangle = p.mo.angle-(ANGLE_45/5)*p.kartstuff[k_drift]

	for i = 0, 1 do
		local newX = p.mo.x + P_ReturnThrustX(p.mo, travelangle + ((i == 0) and -1 or 1)*ANGLE_135, FixedMul(32*FRACUNIT, p.mo.scale))
		local newY = p.mo.y + P_ReturnThrustY(p.mo, travelangle + ((i == 0) and -1 or 1)*ANGLE_135, FixedMul(32*FRACUNIT, p.mo.scale))
		local Spark = P_SpawnMobj(newX, newY, p.mo.z, MT_DRIFTSPARK)
		Spark.target = p.mo
		Spark.angle = travelangle-(ANGLE_45/5)*p.kartstuff[k_drift]
		Spark.destscale = p.mo.scale*5/4
		P_SetScale(Spark, Spark.destscale)
		Spark.momx = p.mo.momx/2
		Spark.momy = p.mo.momy/2
		Spark.momz = p.mo.momz/2 
		Spark.color = SKINCOLOR_KETCHUP
		K_MatchGenericExtraFlags(Spark, p.mo)
	end
end
-- Checkers (not chess)

local function ButtonCheck(p)
	if p.cmd.buttons & BT_BRAKE and p.cmd.buttons & BT_ACCELERATE 
		return true 
	else 
		return false 
	end
end

local function SpeedCheckInit(p)
	if pspeed(p)<=10
		return true
	else
		return false
	end
end

local function SpeedCheckDuring(p)
	if pspeed(p)<=5
		return true
	else
		return false
	end
end

local function GroundCheck(p)
	if P_IsObjectOnGround(p.mo)
		return true
	else
		return false
	end
end

-- Full assembly????

addHook("VoteThinker", do
	for p in players.iterate do
		p.ssmtengaged = 0
		p.ssmtprimed = 0
		p.ssmtticscharged = 0
	end
end)

local ssmtblibtimer = 0

addHook ("ThinkFrame", do
	for p in players.iterate do
		if leveltime < 245 then return end
		if not (p.mo and p.mo.valid) then continue end
		p.ssmtoffroadimmunity = $ or 0
		p.ssmtblibtimer = $ or 0
		if p.ssmtengaged == 0  and ButtonCheck(p) == true and SpeedCheckInit(p) == true and GroundCheck(p) == true //initial E-Brake
			p.ssmtengaged = 1
			p.powers[pw_nocontrol] = 1
			if p.mo.momx > 0 
				p.mo.momx = max($-(FRACUNIT), 0)
			else 
				p.mo.momx = min($+(FRACUNIT), 0)
			end
			if p.mo.momy > 0
				p.mo.momy = max($-(FRACUNIT), 0)
			else
				p.mo.momy = min($+(FRACUNIT), 0)
			end
		else 
			if p.ssmtengaged == 1 and ButtonCheck(p) == true and SpeedCheckDuring(p) == true and GroundCheck(p) == true //Continued E-Brake
				p.ssmtengaged = 1
				p.powers[pw_nocontrol] = 1
				if p.mo.momx > 0 
					p.mo.momx = max($-(FRACUNIT), 0)
				else 
					p.mo.momx = min($+(FRACUNIT), 0)
				end
				if p.mo.momy > 0
					p.mo.momy = max($-(FRACUNIT), 0)
				else
					p.mo.momy = min($+(FRACUNIT), 0)
				end
			end
		end
		if p.ssmtengaged == 1
			if p.ssmtticscharged < 50
				p.ssmtticscharged = p.ssmtticscharged + 1
				K_SpawnWipeoutTrail(p.mo, true)
				K_DriftDustHandling(p.mo)
				if not S_SoundPlaying(p.mo, sfx_ssmtup)
					S_StartSound(p.mo, sfx_ssmtup)
				end
			else
				if p.ssmtticscharged < 105
					if p.ssmtticscharged == 50 or p.ssmtticscharged == 104 then S_StartSound(p.mo, sfx_s3ka2) end
					p.ssmtticscharged = p.ssmtticscharged + 1
					K_SpawnWipeoutTrail(p.mo, true)
					K_DriftDustHandling(p.mo)
					p.ssmtprimed = 1
					SpawnSparksB(p)
					if not S_SoundPlaying(p.mo, sfx_ssmtup)
						S_StartSound(p.mo, sfx_ssmtup)
					end
				else
					K_SpawnWipeoutTrail(p.mo, true)
					SpawnSparksR(p)
					K_DriftDustHandling(p.mo)
					p.ssmtprimed = 2
				end
			end
		end
		if p.ssmtprimed == 1
				if p.cmd.buttons & BT_ACCELERATE and not (p.cmd.buttons & BT_BRAKE)
					p.ssmtengaged = 0
					p.ssmtprimed = 0
					p.ssmtticscharged = 0
					if BLib
						BLib.SetBoost(p, "ssmt", FRACUNIT/2, 4*FRACUNIT, 1*FRACUNIT/10, true, true, false)
						p.ssmtblibtimer = 25
					else
						p.kartstuff[k_driftboost] = $+65
					end
					--print("Success")
					S_StopSoundByID(p.mo, sfx_ssmtup)
					S_StartSound(p.mo, sfx_ssmtus)
				end
		end
		if p.ssmtprimed == 2
				if p.cmd.buttons & BT_ACCELERATE and not (p.cmd.buttons & BT_BRAKE)
					p.ssmtengaged = 0
					p.ssmtprimed = 0
					p.ssmtticscharged = 0
					if BLib
						BLib.SetBoost(p, "ssmt", 3*FRACUNIT/4, 4*FRACUNIT, 1*FRACUNIT/10, true, true, false)
						p.ssmtoffroadimmunity = 45
						p.ssmtblibtimer = 45
					else
						K_DoSneaker(p)
					end
					--print("Success")
					S_StopSoundByID(p.mo, sfx_ssmtup)
					S_StartSound(p.mo, sfx_ssmtus)
				end
		
		end
		if ButtonCheck(p) == false or SpeedCheckDuring(p) == false or GroundCheck(p) == false
			if S_SoundPlaying(p.mo, sfx_ssmtup)
				S_StopSoundByID(p.mo, sfx_ssmtup)
			end
			p.ssmtengaged = 0
			p.ssmtprimed = 0
			p.ssmtticscharged = 0
			--print("I reset the variables for controlling the ssmt")
		end
		if p.ssmtoffroadimmunity > 1
			p.ssmtoffroadimmunity = $ - 1
			p.kartstuff[k_offroad] = 0
			if S_SoundPlaying(p.mo, sfx_cdfm70) --delete offroad sound
				S_StopSoundByID(p.mo, sfx_cdfm70)
			end
		elseif p.ssmtoffroadimmunity == 1
			p.ssmtoffroadimmunity = 0
		end
		if p.ssmtblibtimer > 1
			p.ssmtblibtimer = $ - 1
		elseif p.ssmtblibtimer == 1
			BLib.SetBoost(p, "ssmt", 0, 0, 0, false, false, false)
			p.ssmtblibtimer = 0
		end
		--print(p.ssmtblibtimer)
		--print(p.ssmtoffroadimmunity)
	end
	--print(p.ssmtprimed)
	--print(p.ssmtengaged)
	--print(p.ssmtticscharged)
end)