freeslot("sfx_boost")

-- LibreBoost, by ffoxD. Reusable content, use as long as you credit.
-- Credit to MARIOMARIO13531, and to everyone in the Discord server that helped me.
local function LibreBoost(player)
    -- Prevents the code from running in NiGHTs special stages.
	if (maptol & TOL_NIGHTS) then
		return
	end
	if player.mo.skin == "frontiersclassic" and player.mo.valid
		-- Lets the player boost.
		if not (player.pflags & PF_STASIS)
		and not (player.pflags & PF_FULLSTASIS)
		and not (player.pflags & PF_STARTDASH)
		and not (player.pflags & PF_FINISHED)
		and	player.playerstate ~= PST_DEAD
		and player.powers[pw_carry] == CR_NONE
		and not P_PlayerInPain(player)then
			if not (player.cmd.buttons & player.boostbutton)
				player.canboost = true
			end
		else
			player.canboost = false
		end
		if player.canboost == nil then
			player.canboost = true
		end
		-- Tells the game when the player shall boost, and whenever not.
		if (player.cmd.buttons & player.boostbutton)
			and player.canboost
			player.boosting = true
		else
			player.boosting = false
		end
		-- Determines how fast shall the player boost.
		player.boostspeed = 4 * player.normalspeed / 3
		-- Stores the player's speed angle.
		if player.speed > player.normalspeed then
			player.momangle2 = R_PointToAngle2(0,0,player.mo.momx,player.mo.momy)
		else
			player.momangle2 = player.mo.angle
		end
		-- What happens if the player boosts.
		if player.boosting then
				if not player.cmd.forwardmove then
				player.cmd.forwardmove = 50 --Intentionally not in PreThinkFrame
			end
			player.drawangle = player.momangle2
			player.charflags = $|SF_NOSKID|SF_RUNONWATER|SF_CANBUSTWALLS
			if player.mo.state == S_PLAY_STND
				or player.mo.state == S_PLAY_WALK
				or player.mo.state == S_PLAY_WAIT
				or player.mo.state == S_PLAY_EDGE then
				player.mo.state = S_PLAY_RUN
			end
			if player.speed < (player.boostspeed) then
				P_Thrust(player.mo, player.momangle2, player.boostspeed - player.speed)
			end
		else -- What happens if the player isn't boosting. Mostly resetting values.
			if not (skins[player.mo.skin].flags & SF_NOSKID) then
				player.charflags = $ & ~SF_NOSKID
			end
			if not (skins[player.mo.skin].flags & SF_RUNONWATER) then
				player.charflags = $ & ~SF_RUNONWATER
			end
			if not (skins[player.mo.skin].flags & SF_CANBUSTWALLS ) then
				player.charflags = $ & ~SF_CANBUSTWALLS
			end
			player.hasnomomentum = true -- Prevents XMomentum from preserving your speed.
		end
	end
end

addHook("PlayerThink", LibreBoost)

addHook("MobjMoveBlocked", function(mo) -- You shall not boost if stopped by walls.
    local player = mo.player
    player.canboost = false
	player.boosting = false
end, MT_PLAYER)

local function boosteffect(p)
	if p.mo.skin == "frontiersclassic"
	and p.boosting == true
	and p.canspawn == true
		S_StartSound(p.mo, sfx_boost)
		local playerfrbo = P_SpawnMobjFromMobj(p.mo, 0, 0, 0, MT_FRONTIERSBOOST)
		playerfrbo.color = p.mo.color
		playerfrbo.angle = p.cmd.angleturn << 16 + R_PointToAngle2(0, 0, p.cmd.forwardmove*FRACUNIT, -p.cmd.sidemove*FRACUNIT) + ANGLE_90
		playerfrbo.scale = p.mo.scale
		playerfrbo.destscale = p.mo.scale
		playerfrbo.tics = 3
		p.canspawn = false
	end
end

addHook("PlayerThink", boosteffect)

addHook("PlayerThink", function(p)
	if p.mo.skin == "frontiersclassic"
	and p.boosting == false
		p.canspawn = true
	end
end)

addHook("PlayerThink", function(p)
	if p.mo.skin == "frontiersclassic"
	and p.rings > 59
	and p.boosting == true
		p.powers[pw_sneakers] = 15
		local g = P_SpawnGhostMobj(p.mo)
		g.fuse = 4
	else
		p.powers[pw_sneakers] = 0
	end
end)

addHook("PlayerThink", function(p)
	-- Check if the player object exists, and is alive.
	if not (p.mo and p.mo.valid) then return end
	if not p.mo.health then return end

	-- Check for your skin.
	if p.mo.skin ~= "frontiersclassic" then return end
	
	-- Setup a simple button press check.
	
	if not (p.cmd.buttons & BT_CUSTOM1) then
		p.custom1ready = true
		p.custom1tap = false
	elseif p.custom1ready then
		p.custom1ready = false
		p.custom1tap = true
	else
		p.custom1tap = false
	end
end)

addHook("PlayerThink", function(p)
	if p.mo.skin == "frontiersclassic"
	and not P_IsObjectOnGround(p.mo)
	and p.boosting == true
	and p.custom1tap == true
	and not p.powers[pw_super]
	and p.alreadyairboosted ~= true
		P_SetObjectMomZ(p.mo, 5*FRACUNIT)
		p.mo.state = S_PLAY_ROLL
		p.mo.tics = 3
		p.mo.state = S_PLAY_GLIDE
		p.alreadyairboosted = true
	end
end)

addHook("PlayerThink", function(p)
	if p.mo.skin == "frontiersclassic"
	and P_IsObjectOnGround(p.mo)
		p.alreadyairboosted = false
	end
end)