//HUD with player color:

rawset(_G, "rings", {}) //Will help with counting how many rings are left in a Coop Special Stage.

addHook("NetVars", function(network)
	rings = network(rings)
end)

// Get the X, Y coordinates and flags of HUD_SCORE
local xscore = hudinfo[HUD_SCORE].x
local yscore = hudinfo[HUD_SCORE].y
local fscore = hudinfo[HUD_SCORE].f

// Get the X, Y coordinates and flags of HUD_TIME
local xtime = hudinfo[HUD_TIME].x
local ytime = hudinfo[HUD_TIME].y
local ftime = hudinfo[HUD_TIME].f

// Get the X, Y coordinates and flags of HUD_RINGS
local xrings = hudinfo[HUD_RINGS].x
local yrings = hudinfo[HUD_RINGS].y
local frings = hudinfo[HUD_RINGS].f

local islevelcoopspecialstage = false
local nightsmode = false

local lastcolor = SKINCOLOR_BLUE
local lastskin = "sonic"

local timebonus = false
local ringbonus = false
local guardbonus = false
local perfectbonus = false

local xtimebonus = 152
local ytimebonus = 100
//---
local xguardbonus = 152
local yguardbonus = 100
//---
local xringsbonus = 152
local yringsbonus = 117

--local xperfectbonus = 152
--local yperfectbonus = 151

local xtotal = 152
local ytotal = 151



local xlinkbonus = 141
local ylinkbonus = 108

local xscorebonus = 141
local yscorebonus = 125
//---
local xdownscorebonus = 141
local ydownscorebonus = 142
//---
local xcontinuesbonus = 141
local ycontinuesbonus = 151


//Testing stuff:

--local pressright = false
--local pressleft = false
--local pressup = false
--local pressdown = false

--addHook("PreThinkFrame", do
--	for player in players.iterate
--		if (player.cmd.buttons & BT_WEAPONNEXT)
--			pressup = true
--			pressdown = false
--		elseif (player.cmd.buttons & BT_WEAPONPREV)
--			pressup = false
--			pressdown = true
--		else
--			pressup = false
--			pressdown = false
--		end
--		if (player.cmd.buttons & BT_CUSTOM1)
--			pressright = true
--			pressleft = false
--		elseif (player.cmd.buttons & BT_CUSTOM2)
--			pressright = false
--			pressleft = true
--		else
--			pressright = false
--			pressleft = false
--		end
--	end
--end)

addHook("MapLoad", do
	if (G_IsSpecialStage(gamemap))		  //In a Coop Special Stage!
		for ring in mobjs.iterate()
			if (ring.type == MT_RING)
				table.insert(rings, ring)
			end
		end
	end
end)

addHook("MobjRemoved", function(ring)
	if (G_IsSpecialStage(gamemap))		  //In a Coop Special Stage!
		table.remove(rings)
	end
end, MT_RING)

local function HUDColor(v, player)

	if not (player.mo) return end
	
	local skin = skins["sonic"]
	
	if (player.mo.skin ~= nil)
		skin = player.mo.skin //Get the skin of the player.
	end
	
	lastskin = skins[player.mo.skin].name //Save the skin of the player in a variable.
	lastcolor = player.skincolor //Save the color of the player in a variable.
	
	if (player.rings > 0) //Player gets Ring Bonus ONLY if he has any rings left.
		ringbonus = true
	else
		ringbonus = false
	end
	
	if (player.realtime >= 600*TICRATE) //Player gets Time Bonus ONLY if he finished the level under 600 seconds.
		timebonus = false //					(yes I actually looked at the source code to find out)
	else
		timebonus = true
	end
	
	if (modeattacking == true) //If in Record Attack
		if (mapheaderinfo[gamemap].bonustype == 1) //Boss Type
			guardbonus = true
		else
			guardbonus = false
		end
	else
		guardbonus = false //Guardbonus is not visible in any other mode.
	end
	
	//If NiGHTS SS!
	if (player.powers[pw_carry] & CR_NIGHTSMODE) //In NiGHTS!
		nightsmode = true
	else
		nightsmode = false
	end
		
	
	//If in a Coop Special Stage!
	if (G_IsSpecialStage(gamemap))
	and (nightsmode == false) //Not in NiGHTS!
		islevelcoopspecialstage = true
		if (#rings == 0) //No more rings are left???
			perfectbonus = true //YOOOOOO
		else
			perfectbonus = false //Well at least you tried... Unless...
		end
	else
		islevelcoopspecialstage = false
	end //Perfect Bonus?
	
	--if (pressup == true)
	--	ycontinuesbonus = $1 - 1
	--	print("ycontinuesbonus = "+ycontinuesbonus)
	--end
	--if (pressdown == true)
	--	ycontinuesbonus = $1 + 1
	--	print("ycontinuesbonus = "+ycontinuesbonus)
	--end
	
	//The transition when loading a level:
	local trans = 0
	
	if (leveltime >= 53)
		trans = 0
	else
		if (leveltime < 35)
			trans = 1
		elseif (leveltime < 37)
			trans = V_90TRANS
		elseif (leveltime < 39)
			trans = V_80TRANS
		elseif (leveltime < 41)
			trans = V_70TRANS
		elseif (leveltime < 43)
			trans = V_60TRANS
		elseif (leveltime < 45)
			trans = V_50TRANS
		elseif (leveltime < 47)
			trans = V_40TRANS
		elseif (leveltime < 49)
			trans = V_30TRANS
		elseif (leveltime < 51)
			trans = V_20TRANS
		elseif (leveltime < 53)
			trans = V_10TRANS
		end
	end
	//------------------------------
	
	//Drawing:
	
	if not (G_IsSpecialStage(gamemap)) //Not in COOP Special Stage!
	
		//Draw the custom score:
		if (hud.enabled("score") == true)
			if (trans == 0)
				v.draw(xscore, yscore, v.cachePatch("H_SCORE"), fscore, v.getColormap(skin, player.skincolor))
			elseif (trans != 1)
				v.draw(xscore, yscore, v.cachePatch("H_SCORE"), fscore|trans, v.getColormap(skin, player.skincolor))
			end
		end
		
		//Draw the custom time:
		if (hud.enabled("time") == true)
			if (trans == 0)
				v.draw(xtime, ytime, v.cachePatch("H_TIME"), ftime, v.getColormap(skin, player.skincolor))
			elseif (trans != 1)
				v.draw(xtime, ytime, v.cachePatch("H_TIME"), ftime|trans, v.getColormap(skin, player.skincolor))
			end
		end
		
		//Draw the custom rings:
		if (hud.enabled("rings") == true)
			if (trans == 0)
				if (player.rings == 0)
					if (leveltime % 10 <= 5)
						v.draw(xrings, yrings, v.cachePatch("H_RRINGS"), frings, v.getColormap(skin, player.skincolor))
					else
						v.draw(xrings, yrings, v.cachePatch("H_RINGS"), frings, v.getColormap(skin, player.skincolor))
					end
				else
					v.draw(xrings, yrings, v.cachePatch("H_RINGS"), frings, v.getColormap(skin, player.skincolor))
				end
			elseif (trans != 1)
				if (player.rings == 0)
					if (leveltime % 10 <= 5)
						v.draw(xrings, yrings, v.cachePatch("H_RRINGS"), frings|trans, v.getColormap(skin, player.skincolor))
					else
						v.draw(xrings, yrings, v.cachePatch("H_RINGS"), frings|trans, v.getColormap(skin, player.skincolor))
					end
				else
					v.draw(xrings, yrings, v.cachePatch("H_RINGS"), frings|trans, v.getColormap(skin, player.skincolor))
				end
			end
		end
	end
end

local function HUDColorIntermission(v)
	
	//Use the saved skin and color in the intermission screen:
	local skin = lastskin
	local color = lastcolor
	
	if (hud.enabled("score") == true)
		v.draw(xscore, yscore, v.cachePatch("H_SCORE"), fscore, v.getColormap(skin, color))
	end
	
	if (hud.enabled("time") == true)
		v.draw(xtime, ytime, v.cachePatch("H_TIME"), ftime, v.getColormap(skin, color))
	end
	
	if (guardbonus == false) //guardbonus replaces timebonus if Record Attack -> Boss.
		v.draw(xtimebonus, ytimebonus, v.cachePatch("H_TIMEB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
	else
		v.draw(xguardbonus, yguardbonus, v.cachePatch("H_GUARDB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
	end
	
	--if (ringbonus == true)
	v.draw(xringsbonus, yringsbonus, v.cachePatch("H_RINGSB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
	--end
	
	--if (guardbonus == true)
	--	v.draw(xguardbonus, yguardbonus, v.cachePatch("H_GUARDB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
	--end
	
	v.draw(xtotal, ytotal, v.cachePatch("H_TOTAL"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
end

local function HUDColorIntermissionSPNights(v)
	
	//Use the saved skin and color in the intermission screen:
	local skin = lastskin
	local color = lastcolor
	
	v.draw(xlinkbonus, ylinkbonus, v.cachePatch("H_NIGHTB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
	
	v.draw(xscorebonus, yscorebonus, v.cachePatch("H_SCOREB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))

	v.draw(xcontinuesbonus, ycontinuesbonus, v.cachePatch("H_CONTB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
end

local function HUDColorIntermissionCoopNights(v)
	
	//Use the saved skin and color in the intermission screen:
	local skin = lastskin
	local color = lastcolor
	
	v.draw(xlinkbonus, ylinkbonus, v.cachePatch("H_RINGSB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
	
	if (perfectbonus == true) //Perfect Bonus!
		v.draw(xscorebonus, yscorebonus, v.cachePatch("H_PERB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
		v.draw(xdownscorebonus, ydownscorebonus, v.cachePatch("H_SCOREB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
	else
		v.draw(xscorebonus, yscorebonus, v.cachePatch("H_SCOREB"), V_SNAPTOLEFT|V_SNAPTOTOP, v.getColormap(skin, color))
	end
end

hud.add(function(v, player)
	HUDColor(v, player)
end, "game")

hud.add(function(v)
	if (nightsmode == true)                               //In SP NiGHTS!
		HUDColorIntermissionSPNights(v)
	elseif (islevelcoopspecialstage == true)		  	  //In a Coop Special Stage!
		HUDColorIntermissionCoopNights(v)
	else                                             	  //Not in a Special Stage!
		HUDColorIntermission(v)
	end
end, "intermission")
