//Add gametypes
G_AddGametype({
	name = "Race Comp",
	identifier = "racecomp",
	typeoflevel = TOL_RACE|TOL_COMPETITION,
	rules = GTR_RACE|GTR_RESPAWNDELAY|GTR_PITYSHIELD|GTR_EMERALDTOKENS|GTR_NOSPECTATORSPAWN|
	GTR_SPAWNENEMIES|GTR_SPAWNINVUL|GTR_HURTMESSAGES|GTR_ALLOWEXIT,
	rankingtype = GT_COMPETITION,
	intermissiontype = 7,
	defaultpointlimit = 0,
	defaulttimelimit = 0,
	headerleftcolor = 71,
	headerrightcolor = 71,
	description = 'Collect the most stuff the fastest and survive to the end!'
})
// int_competition is suppost to be 7 buts its not so we set it to 7 lol

addHook("PlayerThink", function(player) // award extra rings after reaching a new starpost
	if player.startpostrings == nil then
		player.startpostrings = 0
	end

	if player.realtime <= 0 and gametype == GT_RACECOMP then // extand invun frames
		player.powers[pw_flashing] = 2*TICRATE
	end
	
	if player.exiting and gametype == GT_RACECOMP then // make sure players that finsh in circuit are invulabule
		player.powers[pw_flashing] = 2*TICRATE
		player.powers[pw_invulnerability] = 0
	end
	
	if player.startpostrings != player.starpostnum and gametype == GT_RACECOMP then
		if player.starpostnum > 0 and player.startpostrings < player.starpostnum then
			local mo = player.mo
			local icon = P_SpawnMobj(mo.x, mo.y, mo.z,  MT_RING_ICON)
			icon.target = mo
		end
		player.startpostrings = player.starpostnum
	end
end)

//Player death steal half the score from the target in comp
addHook("MobjDeath",function(target,inflictor,source,damagetype)
local B = CBW_Battle

	if gametype != GT_RACECOMP then return end
	
	local killer
	local player = target.player
	local halfscore = player.score/2
	
	//Standard kill
	if inflictor and inflictor.player
		killer = inflictor.player
	elseif source and source.player
		killer = source.player
	end
	
	//Player was pushed into a death hazard battle only
	if player and (damagetype == DMG_DEATHPIT or damagetype == DMG_CRUSHED) and B 
		and player.pushed_creditplr and player.pushed_creditplr.valid and not(B.MyTeam(player,player.pushed_creditplr))
		then
		killer = player.pushed_creditplr
	end

	if not (target.player and target.player.revenge) and killer then // also add total ring
		P_AddPlayerScore(killer, halfscore)
		if B then
		killer.totalring = $+20
		end
		player.score = halfscore
	else // dvide score amoung other players
		local aliveplayers = 0
		local deadplayer = player
		player.score = halfscore
		for player in players.iterate do // dont get score if your dead!
			if player and player.valid and player.playerstate != 1 and player != deadplayer then
				aliveplayers = $+1
			end
		end
		if aliveplayers >= 1 then
			halfscore = $/aliveplayers
			for player in players.iterate do //do it twice this time we add the score
				if player and player.valid and player.playerstate != 1 and player != deadplayer then
					P_AddPlayerScore(player, halfscore)
				end
			end
		end
	end
	
end, MT_PLAYER)

addHook("MapLoad", function(mapnum) // auto set console commands
	if	gametype != GT_RACECOMP then return end
	local B = CBW_Battle
	CV_Set(CV_FindVar("respawnitem"), 0)
		if B then
			local CV = B.Console
			CV_Set(CV_FindVar("battle_collision"), 1)
		end
end)