//I/O stuff
local configplayer

local function SaveCVar(cvarname, path)
	local file = io.openlocal(path, "w+")
	local cvar = CV_FindVar(cvarname)
	file:write(cvar.value)
	file:close()
end

local function LoadCVar(cvarname, path)
	local file = io.openlocal(path)
	local cvar = CV_FindVar(cvarname)
	if file
		local num = file:read("*n")
		CV_Set(cvar, num)
		file:close()
	end
end

local function SavePVar(path, toinsert)
	local file = io.openlocal(path, "w+")
	if type(toinsert) == "string"
		file:write(toinsert)
	else
		file:write(tostring(toinsert))
	end
	file:close()
end

local function LoadPVar(player, path, command, defaultvar)
	local file = io.openlocal(path)
	if file
		local str = file:read("*a")
		COM_BufInsertText(player, command+" "+str)
		file:close()
	else
		COM_BufInsertText(player, command+" "+defaultvar)
	end
end

local function L_DecimalFixed(str)
	if str == nil return nil end
	local dec_offset = string.find(str,'%.')
	if dec_offset == nil
		return (tonumber(str) or 0)*FRACUNIT
	end
	local whole = tonumber(string.sub(str,0,dec_offset-1)) or 0
	local decimal = tonumber(string.sub(str,dec_offset+1)) or 0
	whole = $ * FRACUNIT
	local dec_len = string.len(decimal)
	decimal = $ * FRACUNIT / (10^dec_len)
	return whole + decimal
end

local function L_FixedDecimal(str,maxdecimal)
	if str == nil or tostring(str) == nil
		return '<invalid FixedDecimal>'
	end
	local number = tonumber(str)
	maxdecimal = ($ != nil) and $ or 3
	if tonumber(str) == 0 return '0' end
	local polarity = abs(number)/number
	local str_polarity = (polarity < 0) and '-' or ''
	local str_whole = tostring(abs(number/FRACUNIT))
	if maxdecimal == 0
		return str_polarity..str_whole
	end
	local decimal = number%FRACUNIT
	decimal = FRACUNIT + $
	decimal = FixedMul($,FRACUNIT*10^maxdecimal)
	decimal = $>>FRACBITS
	local str_decimal = string.sub(decimal,2)
	return str_polarity..str_whole..'.'..str_decimal
end

//Aura sound schemes
freeslot(
	"sfx_arabst",
	"sfx_spaura",
	"sfx_detrns",
	"sfx_ss3aur",
	"sfx_ssgaur",
	"sfx_ssgdra",
	"sfx_ssbaur",
	"sfx_ssbkau",
	"sfx_ssraur",
	"sfx_uitrns",
	"sfx_uiaura"
)
sfxinfo[sfx_arabst].caption = "Aura burst"
sfxinfo[sfx_spaura].caption = "Super form aura"
sfxinfo[sfx_detrns].caption = "Detransformation"
sfxinfo[sfx_ss3aur].caption = "Super Saiyan 3 aura"
sfxinfo[sfx_ssgaur].caption = "Super Saiyan God aura"
sfxinfo[sfx_ssgdra].caption = "Super Saiyan God draining"
sfxinfo[sfx_ssbaur].caption = "Super Saiyan Blue aura"
sfxinfo[sfx_ssbkau].caption = "Super Saiyan Blue Kaioken aura"
sfxinfo[sfx_ssraur].caption = "Super Saiyan Rose aura"
sfxinfo[sfx_uitrns].caption = "Ultra Instinct transformation"
sfxinfo[sfx_uiaura].caption = "Ultra Instinct aura"

if not SFA_SoundSchemes
	rawset(_G, "SFA_SoundSchemes", {})
end

SFA_SoundSchemes["none"] = {
	name = "None",
	transform = sfx_none,
	passive = sfx_none,
	detransform = sfx_none,
	passivetics = 1
}
SFA_SoundSchemes["ssj"] = {
	name = "Super Saiyan",
	transform = sfx_arabst,
	passive = sfx_spaura,
	detransform = sfx_detrns,
	passivetics = 27
}
SFA_SoundSchemes["ssj3"] = {
	name = "Super Saiyan 3",
	transform = sfx_arabst,
	passive = sfx_ss3aur,
	detransform = sfx_detrns,
	passivetics = 27
}
SFA_SoundSchemes["ssg"] = {
	name = "Super Saiyan God",
	transform = sfx_arabst,
	passive = sfx_ssgaur,
	detransform = sfx_ssgdra,
	passivetics = 324
}
SFA_SoundSchemes["ssb"] = {
	name = "Super Saiyan Blue",
	transform = sfx_arabst,
	passive = sfx_ssbaur,
	detransform = sfx_detrns,
	passivetics = 301
}
SFA_SoundSchemes["ssbk"] = {
	name = "Super Saiyan Blue Kaioken",
	transform = sfx_arabst,
	passive = sfx_ssbkau,
	detransform = sfx_detrns,
	passivetics = 119
}
SFA_SoundSchemes["ssr"] = {
	name = "Super Saiyan Rose",
	transform = sfx_arabst,
	passive = sfx_ssraur,
	detransform = sfx_detrns,
	passivetics = 78
}
SFA_SoundSchemes["ui"] = {
	name = "Ultra Instinct",
	transform = sfx_uitrns,
	passive = sfx_uiaura,
	detransform = sfx_detrns,
	passivetics = 306
}


//Aura styles
freeslot(
	"SPR_SPAU",
	"SPR_SSJA",
	"SPR_SSJF",
	"SPR_SSGA",
	"SPR_SSBA",
	"SPR_SSBK",
	"SPR_SSBB",
	"SPR_SSBE",
	"SPR_SRAA",
	"SPR_SSRA",
	"SPR_SGSA",
	"SPR_UIAU",
	"SPR_FARA"
)

if not SFA_AuraStyles
	rawset(_G, "SFA_AuraStyles", {})
end

SFA_AuraStyles["ssj"] = {
	name = "Super Saiyan",
	sprite = SPR_SSJA,
	frames = 3,
	tics = 3,
	scale = FixedDiv(5*FRACUNIT, 4*FRACUNIT),
	color = "player",
	sound = "ssj"
}
SFA_AuraStyles["ssjf"] = {
	name = "Super Saiyan (with light color filling)",
	sprite = SPR_SSJF,
	frames = 3,
	tics = 3,
	scale = FixedDiv(5*FRACUNIT, 4*FRACUNIT),
	color = "player",
	sound = "ssj"
}
SFA_AuraStyles["ssjalt"] = {
	name = "Super Saiyan (Roll the Dice)",
	sprite = SPR_SPAU,
	frames = 4,
	tics = 2,
	scale = FRACUNIT,
	color = "player",
	sound = "ssj"
}
SFA_AuraStyles["ssg"] = {
	name = "Super Saiyan God",
	sprite = SPR_SSGA,
	frames = 3,
	tics = 3,
	scale = FRACUNIT,
	sound = "ssg",
}
SFA_AuraStyles["ssb"] = {
	name = "Super Saiyan Blue",
	sprite = SPR_SSBA,
	frames = 3,
	tics = 3,
	scale = FRACUNIT+FRACUNIT/8,
	sound = "ssb"
}
SFA_AuraStyles["ssbk"] = {
	name = "Super Saiyan Blue + Kaioken",
	sprite = SPR_SSBK,
	frames = 3,
	tics = 3,
	scale = FRACUNIT+FRACUNIT/8,
	sound = "ssbk"
}
SFA_AuraStyles["ssbbc"] = {
	name = "Super Saiyan Blue (Berserk Controlled)",
	sprite = SPR_SSBB,
	frames = 3,
	tics = 3,
	scale = FRACUNIT+FRACUNIT/8,
	sound = "ssb"
}
SFA_AuraStyles["ssbe"] = {
	name = "Super Saiyan Blue Evolution",
	sprite = SPR_SSBE,
	frames = 3,
	tics = 3,
	scale = FRACUNIT+FRACUNIT/8,
	sound = "ssb"
}
SFA_AuraStyles["ssrage"] = {
	name = "Super Saiyan Rage",
	sprite = SPR_SRAA,
	frames = 3,
	tics = 3,
	scale = FRACUNIT+FRACUNIT/8,
	sound = "ssb"
}
SFA_AuraStyles["ssr"] = {
	name = "Super Saiyan Rose",
	sprite = SPR_SSRA,
	frames = 3,
	tics = 3,
	scale = FRACUNIT+FRACUNIT/8,
	sound = "ssr"
}
SFA_AuraStyles["ssgss"] = {
	name = "Super Saiyan God Super Saiyan (colorizable)",
	sprite = SPR_SGSA,
	frames = 3,
	tics = 3,
	scale = FRACUNIT+FRACUNIT/4
}
SFA_AuraStyles["ui"] = {
	name = "Ultra Instinct (Roll the Dice)",
	sprite = SPR_UIAU,
	frames = 6,
	tics = 2,
	scale = FRACUNIT,
	color = SKINCOLOR_SKY,
	sound = "ui"
}
SFA_AuraStyles["firesonic"] = {
	name = "Fire Sonic (Roll the Dice)",
	sprite = SPR_FARA,
	frames = 3,
	tics = 2,
	scale = FixedDiv(27*FRACUNIT, 20*FRACUNIT),
	color = SKINCOLOR_KETCHUP,
	sound = "ssbk"
}

//Aura state definition
freeslot("S_SUPERAURA")
states[S_SUPERAURA] = {
	sprite = SPR_SPAU,
	frame = A|FF_FULLBRIGHT,
	var1 = 1,
	nextstate = S_SUPERAURA
}

//Aura settings
//sfaura_sounds: determines if aura burst, detransformation and passive sound should be played.
CV_RegisterVar({
	name = "sfaura_sounds",
	defaultvalue = "On",
	flags = CV_SHOWMODIF|CV_CALL,
	PossibleValue = CV_OnOff,
	func = function(var)
		for player in players.iterate
			if var.value == 0
			and (player.auramo and player.auramo.valid) then
				S_StopSoundByID(player.auramo, SFA_SoundSchemes[player.aurasoundsch].passive)
			end
		end
	end
})

//sfaura_allowsounds: Sets if the players can play aura sounds. Server-only.
CV_RegisterVar({
	name = "sfaura_allowsounds",
	defaultvalue = "On",
	flags = CV_SHOWMODIF|CV_NETVAR|CV_CALL,
	PossibleValue = CV_OnOff,
	func = function(var)
		for player in players.iterate
			if var.value == 0
			and (player.auramo and player.auramo.valid) then
				S_StopSoundByID(player.auramo, SFA_SoundSchemes[player.aurasoundsch].passive)
				player.auramo.passfxtimer = 0
			end
		end
	end
})

//sfaura_autoconfig: Sets if the aura's sound scheme and color should be changed everytime you change the aura style.
CV_RegisterVar({
	name = "sfaura_autoconfig",
	defaultvalue = "On",
	flags = CV_SHOWMODIF,
	PossibleValue = CV_OnOff
})

//sfaura_scale: sets the aura's size.
COM_AddCommand("sfaura_scale", function(player, arg)
	if (player.mo and player.mo.valid) then
		if arg then
			arg = tostring(arg)
			if L_DecimalFixed(arg) > 0 and L_DecimalFixed(arg) <= 5*FRACUNIT then
				player.aurascale = L_DecimalFixed(arg)
				SavePVar("client/sfaura_scale.dat", arg)
				CONS_Printf(player, "Aura size is now "..L_FixedDecimal(player.aurascale))
			else
				CONS_Printf(player, "The specified argument is invalid.")
				CONS_Printf(player, "Only decimal numbers greater than 0 to 5 are allowed.")
			end
		else
			CONS_Printf(player, "Sets the aura's size.")
			CONS_Printf(player, "Current: "..L_FixedDecimal(player.aurascale))
			CONS_Printf(player, "Only decimal numbers greater than 0 to 5 are allowed.")
		end
	else
		CONS_Printf(player, "You must be in a map to use this command.")
	end
end)

//sfaura_conditions: sets in which cases the aura can be triggered.
COM_AddCommand("sfaura_conditions", function(player, arg)
	if (player.mo and player.mo.valid) then
		if arg then
			arg = arg:lower()
			if arg == "0" or arg == "never" then
				player.auraconditions = 0
				SavePVar("client/sfaura_conditions.dat", player.auraconditions)
				CONS_Printf(player, "Aura will now be always disabled.")
			elseif arg == "1" or arg == "super" then
				player.auraconditions = 1
				SavePVar("client/sfaura_conditions.dat", player.auraconditions)
				CONS_Printf(player, "Aura will now only trigger when you go Super.")
			elseif arg == "2" or arg == "always" then
				player.auraconditions = 2
				SavePVar("client/sfaura_conditions.dat", player.auraconditions)
				CONS_Printf(player, "Aura will now be always enabled.")
			else
				CONS_Printf(player, "The specified argument is invalid.")
			end
		else
			CONS_Printf(player, "Sets the conditions that must be met in order to trigger the aura.")
			if player.auraconditions == 0 then
				CONS_Printf(player, "Current: Never")
			elseif player.auraconditions == 1 then
				CONS_Printf(player, "Current: Super Only")
			else
				CONS_Printf(player, "Current: Always")
			end
		end
	else
		CONS_Printf(player, "You must be in a map to use this command.")
	end
end)

//sfaura_color: Sets the aura's color.
COM_AddCommand("sfaura_color", function(player, arg)
	if (player.mo and player.mo.valid) then
		if arg then
			arg = arg:lower()
			if R_GetColorByName(arg) != SKINCOLOR_NONE then
				player.auracolor = R_GetColorByName(arg)
				SavePVar("client/sfaura_color.dat", player.auracolor)
				CONS_Printf(player, "Aura color has been set to "..R_GetNameByColor(R_GetColorByName(arg)))
			elseif arg == "player" then
				player.auracolor = "player"
				SavePVar("client/sfaura_color.dat", player.auracolor)
				CONS_Printf(player, "Aura color will now be the same as the player's color.")
			elseif arg == "opposite" then
				player.auracolor = "opposite"
				SavePVar("client/sfaura_color.dat", player.auracolor)
				CONS_Printf(player, "Aura color will now be the opposite of the player's color.")
			else
				CONS_Printf(player, "The specified argument is invalid.")
			end
		else
			CONS_Printf(player, "Sets the aura's color.")
			if type(player.auracolor) == "number" then
				CONS_Printf(player, "Current aura color is "..R_GetNameByColor(player.auracolor))
			else
				CONS_Printf(player, "Current aura color is: "..player.auracolor)
			end
		end
	else
		CONS_Printf(player, "You must be in a map to use this command.")
	end
end)

//sfaura_soundscheme: Determines what type of sounds should the aura play.
COM_AddCommand("sfaura_soundscheme", function(player, arg)
	if (player.mo and player.mo.valid) then
		if arg then
			arg = arg:lower()
			if SFA_SoundSchemes[arg] != nil
				if (player.auramo and player.auramo.valid) then
					S_StopSoundByID(player.auramo, SFA_SoundSchemes[player.aurasoundsch].passive)
					player.auramo.passfxtimer = 0
				end
				player.aurasoundsch = arg
				SavePVar("client/sfaura_soundscheme.dat", arg)
				CONS_Printf(player, "Aura sound scheme is now "..SFA_SoundSchemes[arg].name)
			elseif arg == "list"
				for k, v in pairs(SFA_SoundSchemes)
					CONS_Printf(player, k.." - "..SFA_SoundSchemes[k].name)
				end
			else
				CONS_Printf(player, "The specified argument is invalid.")
			end
		else
			CONS_Printf(player, "Sets the sound scheme to be used by the aura.")
			CONS_Printf(player, "Current: "..SFA_SoundSchemes[player.aurasoundsch].name)
			CONS_Printf(player, "Type \"sfaura_soundscheme list\" to see all the available sound schemes.")
			
		end
	else
		CONS_Printf(player, "You must be in a map to use this command.")
	end
end)

//sfaura_style: Sets the style of the aura.
COM_AddCommand("sfaura_style", function(player, arg)
	if (player.mo and player.mo.valid) then
		if arg then
			arg = arg:lower()
			if SFA_AuraStyles[arg] != nil
				if (player.auramo and player.auramo.valid) then
					player.auramo.frame = A|($ & ~FF_FRAMEMASK)
					player.auramo.nextframe = 0
					player.auramo.sprite = SFA_AuraStyles[arg].sprite
					if CV_FindVar("sfaura_autoconfig").value == 1 then
						if SFA_AuraStyles[arg].color != nil then
							COM_BufInsertText(player, "sfaura_color "..SFA_AuraStyles[arg].color)
						end
						if SFA_AuraStyles[arg].sound != nil then
							COM_BufInsertText(player, "sfaura_soundscheme "..SFA_AuraStyles[arg].sound)
						end
					end
				end
				player.aurastyle = arg
				SavePVar("client/sfaura_style.dat", arg)
				CONS_Printf(player, "Aura style is now "..SFA_AuraStyles[arg].name)
			elseif arg == "list"
				for k, v in pairs(SFA_AuraStyles)
					CONS_Printf(player, k.." - "..SFA_AuraStyles[k].name)
				end
			else
				CONS_Printf(player, "The specified argument is invalid.")
			end
		else
			CONS_Printf(player, "Sets the style of the aura.")
			CONS_Printf(player, "Current: "..SFA_AuraStyles[player.aurastyle].name)
			CONS_Printf(player, "Type \"sfaura_style list\" to see all the available aura styles.")
		end
	else
		CONS_Printf(player, "You must be in a map to use this command.")
	end
end)

//sfaura_opacity: sets the aura's opacity.
COM_AddCommand("sfaura_opacity", function(player, arg)
	if (player.mo and player.mo.valid) then
		if arg then
			arg = tonumber(arg)
			if arg >= 0 and arg <= 10 then
				player.auraopacity = arg
				if player.auramo and player.auramo.valid then
					if player.auraopacity == 0 then
						player.auramo.flags2 = $|MF2_DONTDRAW
					elseif player.auraopacity > 0 and player.auraopacity < 10
						player.auramo.flags2 = $ & ~MF2_DONTDRAW
						player.auramo.frame = ($ & ~FF_TRANSMASK)|((10-player.auraopacity)<<FF_TRANSSHIFT)
					else
						player.auramo.frame = $ & ~FF_TRANSMASK
						player.auramo.flags2 = $ & ~MF2_DONTDRAW
					end
				end
				SavePVar("client/sfaura_opacity.dat", arg)
				if player.auraopacity > 0 then
					CONS_Printf(player, "Aura opacity is now "..player.auraopacity.."0%")
				else
					CONS_Printf(player, "Aura opacity is now "..player.auraopacity)
				end
			else
				CONS_Printf(player, "The specified argument is invalid.")
				CONS_Printf(player, "Only numbers from 0 to 10 are allowed.")
			end
		else
			CONS_Printf(player, "Sets the aura's size.")
			if player.auraopacity > 0 then
				CONS_Printf(player, "Current: "..player.auraopacity.."0%")
			else
				CONS_Printf(player, "Current: "..player.auraopacity)
			end
			CONS_Printf(player, "Only numbers from 0 to 10 are allowed.")
		end
	else
		CONS_Printf(player, "You must be in a map to use this command.")
	end
end)

//I/O stuff
LoadCVar("sfaura_sounds", "client/sfaura_sounds.dat")
LoadCVar("sfaura_autoconfig", "client/sfaura_autoconfig.dat")

addHook("GameQuit", function()
	SaveCVar("sfaura_sounds", "client/sfaura_sounds.dat")
	SaveCVar("sfaura_autoconfig", "client/sfaura_autoconfig.dat")
end)

addHook("ThinkFrame", do
	if (consoleplayer and consoleplayer.valid)
	and not (configplayer and configplayer.valid)
		configplayer = consoleplayer
		LoadPVar(consoleplayer, "client/sfaura_conditions.dat", "sfaura_conditions", 1)
		LoadPVar(consoleplayer, "client/sfaura_color.dat", "sfaura_color", "player")
		LoadPVar(consoleplayer, "client/sfaura_soundscheme.dat", "sfaura_soundscheme", "ssj")
		LoadPVar(consoleplayer, "client/sfaura_style.dat", "sfaura_style", "ssj")
		LoadPVar(consoleplayer, "client/sfaura_opacity.dat", "sfaura_opacity", 10)
		LoadPVar(consoleplayer, "client/sfaura_scale.dat", "sfaura_scale", 1)
	end
end)

addHook("PlayerThink", function(player)
	if not (player.mo and player.mo.valid) then return end
	
	//Conditions to trigger the aura
	player.cantriggeraura = player.playerstate == PST_LIVE and ((player.auraconditions == 1 and (player.powers[pw_super] or player.nightstime)) or player.auraconditions == 2)
	
	if (player.mo.state != S_PLAY_SUPER_TRANS1
	and player.mo.state != S_PLAY_SUPER_TRANS2
	and player.mo.state != S_PLAY_NIGHTS_TRANS1
	and player.mo.state != S_PLAY_NIGHTS_TRANS2)
	and player.cantriggeraura
	and not (player.auramo and player.auramo.valid) then
		//Spawn aura mobj if the player is currently in their Super Form
		player.auramo = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_OVERLAY)
		if CV_FindVar("sfaura_sounds").value == 1
		and CV_FindVar("sfaura_allowsounds").value == 1 then
			S_StartSound(player.auramo, SFA_SoundSchemes[player.aurasoundsch].transform)
		end
		player.auramo.target = player.mo
		player.auramo.state = S_SUPERAURA
		if player.auraopacity == 0 then
			player.auramo.flags2 = $|MF2_DONTDRAW
		elseif player.auraopacity > 0 and player.auraopacity < 10
			player.auramo.flags2 = $ & ~MF2_DONTDRAW
			player.auramo.frame = ($ & ~FF_TRANSMASK)|((10-player.auraopacity)<<FF_TRANSSHIFT)
		end
		player.auramo.sprite = SFA_AuraStyles[player.aurastyle].sprite
	end
	
	if (player.auramo and player.auramo.valid)
	and player.auraconditions == 2 then
		player.mo.renderflags = $|RF_FULLBRIGHT
	else
		player.mo.renderflags = $ & ~RF_FULLBRIGHT
	end
	
	if not player.cantriggeraura
	and (player.auramo and player.auramo.valid) then
		//Remove aura if player is no longer in their Super Form
		if CV_FindVar("sfaura_sounds").value == 1 
		and CV_FindVar("sfaura_allowsounds").value == 1 then
			S_StopSoundByID(player.auramo, SFA_SoundSchemes[player.aurasoundsch].passive)
			S_StartSound(player.mo, SFA_SoundSchemes[player.aurasoundsch].detransform)
		end
		P_RemoveMobj(player.auramo)
	end
end)

addHook("MobjThinker", function(aura)
	if aura.state != S_SUPERAURA then return end
	
	//Timer
	if aura.passfxtimer == nil then
		aura.passfxtimer = 0
	end
	aura.passfxtimer = $+1
	
	//Aura frame cycle behavior
	if aura.nextframe == nil then
		aura.nextframe = 0
	end
	aura.frame = aura.nextframe|($ & ~FF_FRAMEMASK)
	if (leveltime % SFA_AuraStyles[aura.target.player.aurastyle].tics == 1) then
		aura.nextframe = $+1
	end
	if aura.nextframe > SFA_AuraStyles[aura.target.player.aurastyle].frames-1 then
		aura.nextframe = 0
	end
	
	//Aura properties
	
	//Aura colors based on current supercolor:
		//Silver: Bone (2)
		//Red: Super Red 5 (122)
		//Orange: Apricot (43)
		//Gold: Yellow (51)
		//Peridot: Peridot (56)
		//Sky: Sky (77)
		//Purple: Super Purple 4 (146)
		//Rust: Copper (42)
		//Tan: Super Tan 4 (156)
	if aura.target.player.auracolor == "player" then
		if aura.target.color >= 113 and aura.target.color <= 117 then
			aura.color = SKINCOLOR_BONE
		elseif aura.target.color >= 118 and aura.target.color <= 122 then
			aura.color = SKINCOLOR_SUPERRED5
		elseif aura.target.color >= 123 and aura.target.color <= 127 then
			aura.color = SKINCOLOR_APRICOT
		elseif aura.target.color >= 128 and aura.target.color <= 132 then
			aura.color = SKINCOLOR_YELLOW
		elseif aura.target.color >= 133 and aura.target.color <= 137 then
			aura.color = SKINCOLOR_PERIDOT
		elseif aura.target.color >= 138 and aura.target.color <= 142 then
			aura.color = SKINCOLOR_SKY
		elseif aura.target.color >= 143 and aura.target.color <= 147 then
			aura.color = SKINCOLOR_SUPERPURPLE4
		elseif aura.target.color >= 148 and aura.target.color <= 152 then
			aura.color = SKINCOLOR_COPPER
		elseif aura.target.color >= 153 and aura.target.color <= 157 then
			aura.color = SKINCOLOR_SUPERTAN4
		else
			aura.color = aura.target.color
		end
	elseif aura.target.player.auracolor == "opposite" then
		aura.color = ColorOpposite(aura.target.color)
	elseif type(aura.target.player.auracolor) == "number" then
		aura.color = aura.target.player.auracolor
	end
	
	aura.spritexscale = FixedMul(aura.target.player.aurascale, SFA_AuraStyles[aura.target.player.aurastyle].scale)
	aura.spriteyscale = aura.spritexscale
	
	//Play the aura's passive sound
	if (aura.passfxtimer % SFA_SoundSchemes[aura.target.player.aurasoundsch].passivetics == 1)
	and CV_FindVar("sfaura_sounds").value == 1
	and CV_FindVar("sfaura_allowsounds").value == 1 then
		S_StartSound(aura, SFA_SoundSchemes[aura.target.player.aurasoundsch].passive)
	end
end, MT_OVERLAY)