addHook("PlayerThink", function(player)
	if player.combo == nil
		player.combo = 0
	end
	if player.comboamount == nil
		player.comboamount = 0
	end
	if player.combo > 0 and not (player.pflags & PF_FINISHED)
		player.combo = player.combo - 1
	end
	if player.ontop == nil
		player.ontop = true
	end
	if player.ignoreexit == nil
		player.ignoreexit = false
	end
	if player.combo == 1
		S_StartSound(player.mo, 163, player)
		player.ontop = false
	end
	if player.combo > 200
		player.combo = 200
	end
	if player.enemieskilled == nil
		player.enemieskilled = 0
	end
	if player.supertaunt == nil
		player.supertaunt = false
	end
	if player.enemieskilled == 10 and player.mo.skin == "prpeppino"
		player.supertaunt = true
		player.enemieskilled = 0
	end
	if player.haslostcombo == nil
		player.haslostcombo = false
	end
	if player.congrats == nil
		player.congrats = false
	end
	if player.exiting and player.lapimpossible == true
		player.ontop = true
	end
	if (player.exiting or (player.pflags & PF_FINISHED)) and (player.ignoreexit == false)
		player.combo = 0
		player.congrats = true
		player.ignoreexit = true
	end
	if player.combo == 0
		P_AddPlayerScore(player, player.comboamount*500)
		player.comboamount = 0
		player.enemieskilled = 0
	end
	if player.lapimpossible == nil
		player.lapimpossible = false
	end
	if player.lapimpossible == true
		player.lives = 1
	end
	if player.combo < 0
		player.combo = 0
	end
end)

COM_AddCommand("finalchallenge", function(player, arg)
	if arg == "on" then
		player.combo = 200
		player.score = 25001
		player.ontop = true
		player.lapimpossible = true
		print "successfully activated"
	elseif arg == "off" then
		player.combo = 0
		player.score = 0
		player.ontop = false
		player.lapimpossible = false
		print "successfully disactivated"
	else
		print "Not valid, choose on or off"
	end
end)

addHook("MobjDeath", function(target, inflictor, source, damagetype)
	if inflictor and inflictor.valid and inflictor.player and inflictor.player.valid and not (inflictor.player.pflags & PF_FINISHED)
		inflictor.player.combo = 200
		inflictor.player.comboamount = inflictor.player.comboamount + 1
		inflictor.player.enemieskilled = inflictor.player.enemieskilled + 1
	end
end)

addHook("MobjDamage", function(target, inflictor, source, damagetype)
	if inflictor and inflictor.valid and inflictor.player and inflictor.player.valid and not (inflictor.player.pflags & PF_FINISHED)
		inflictor.player.combo = inflictor.player.combo + 50
		if inflictor.player.lapimpossible == true
			inflictor.player.combo = inflictor.player.combo + 50
		end
	end
end)

addHook("TouchSpecial", function(special, toucher)
	if toucher and toucher.valid and toucher.player.combo > 0 and not (toucher.player.pflags & PF_FINISHED)
		toucher.player.combo = toucher.player.combo + 10
		toucher.player.score = toucher.player.score + 100
		if toucher.player.lapimpossible == true
			toucher.player.combo = toucher.player.combo + 40
		end
	end
end, MT_RING)

addHook("TouchSpecial", function(special, toucher)
	if toucher.player.starpostnum < special.health and toucher.player.combo > 0 and not (toucher.player.pflags & PF_FINISHED)
		toucher.player.combo = 200
		toucher.player.comboamount = toucher.player.comboamount + 1
		toucher.player.enemieskilled = toucher.player.enemieskilled + 1
	end
end, MT_STARPOST)

addHook("PlayerThink", function(player)
	if P_PlayerInPain(player) and player.haslostcombo == false and not (player.pflags & PF_FINISHED)
		player.combo = player.combo - 50
		player.haslostcombo = true
	end
	if not P_PlayerInPain(player) and P_IsObjectOnGround(player.mo)
		player.haslostcombo = false
	end
	if (player.lapimpossible == true) and (player.combo == 0) and not (player.exiting or (player.pflags & PF_FINISHED)) and (player.ontop == false) and (player.playerstate == PST_LIVE)
		player.lives = 0
		P_KillMobj(player.mo, nil, nil)
		player.lives = 0
	end
end)

addHook("PlayerSpawn", function(player)
	if player.congrats == true
		player.congrats = false
	end
	if player.ignoreexit == true
		player.ignoreexit = false
	end
	if player.mo and player.mo.valid and player.lapimpossible == false
		player.score = 0
		player.combo = 0
		player.comboamount = 0
		player.enemieskilled = 0
		player.supertaunt = false
		player.ontop = true
	end
	if player.mo and player.mo.valid and player.lapimpossible == true
		player.combo = 200
		player.ignoreexit = false
		player.congrats = false
	end
end)

addHook("ThinkFrame", function()
	for player in players.iterate do
		if player.congrats == true
			if player.score < 4000
				chatprint(player.name+" just got a D! Sucks to be them!")
				player.congrats = false
			end
			if player.score >= 5000 and player.score < 10000
				chatprint(player.name+" just got a C! That's alright...")
				player.congrats = false
			end
			if player.score >= 10000 and player.score < 20000
				chatprint(player.name+" just got a B! They're doing just fine!")
				player.congrats = false
			end
			if player.score >= 20000 and player.score < 25000
				chatprint(player.name+" just got a A! They're doing real good!")
				player.congrats = false
			end
			if player.score >= 25000 and player.ontop == false
				chatprint(player.name+" just got a S! I'm shocked!")
				player.congrats = false
			end
			if player.score >= 25001 and player.ontop == true and player.lapimpossible == false
				chatprint(player.name+" just got a P! A true perfectionist!")
				player.congrats = false
			end
			if player.score >= 25001 and player.ontop == true and player.lapimpossible == true
				chatprint("OMG! "+player.name+" IS ACTUALLY DOING IT!")
				player.congrats = false
			end
		end
	end
end)
			

-- The HUD code, developed by MARIOMARIO13531.
-- Some of the code is from boss hud mod made by Inuyasha.
--Lol I took this from Boost, well, the beginning part
local function comb(v, player)
	if player.mo and player.mo.valid
		if (maptol & TOL_NIGHTS) or player.combo == nil then -- Prevents the code from running in a Nights special stage
			return
		end 
			local p_comb = v.cachePatch("COMBR") --this junk is mine
			local p_ind = v.cachePatch("COMIND")
			local posx = (player.combo/4)

		if player.mo.valid and player.combo > 0
		v.draw(0, 0, p_comb, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		v.draw(posx, 0, p_ind, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		v.drawString(10, 37, (player.comboamount), V_PERPLAYER)
		end
	end
end
hud.add(comb, "game")

local function ranks(v, player)
	if player.mo and player.mo.valid
		if (maptol & TOL_NIGHTS) then -- Prevents the code from running in a Nights special stage
			return
		end 
			local p_rand = v.cachePatch("RANKD") --this junk is mine
			local p_ranc = v.cachePatch("RANKC")
			local p_ranb = v.cachePatch("RANKB")
			local p_rana = v.cachePatch("RANKA")
			local p_rans = v.cachePatch("RANPEE")
			local p_ranp = v.cachePatch("RANKP")
			
		if player.mo.valid and player.score < 4000
			v.draw(0, 100, p_rand, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 5000 and player.score < 10000
			v.draw(0, 100, p_ranc, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 10000 and player.score < 20000
			v.draw(0, 100, p_ranb, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 20000 and player.score < 25000
			v.draw(0, 100, p_rana, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 25000
			v.draw(0, 100, p_rans, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 25001 and player.ontop == true
			v.draw(0, 100, p_ranp, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
	end
end
hud.add(ranks, "game")