#include extreme\_ex_drm;

init()
{
	efogstr = extreme\_ex_utils::strip(level.ex_efogstr);
	if(efogstr == "") return;

	efog = extreme\_ex_utils::explode(efogstr," ");
	if(efog.size != 6) return;

	level.ex_efog = int(efog[0]);
	level.ex_efogdensity = float(efog[1]);
	level.ex_efogdensity2 = float(efog[2]);
	level.ex_efogred = float(efog[3]);
	level.ex_efoggreen = float(efog[4]);
	level.ex_efogblue = float(efog[5]);

	level thread startfogThreads();
}

float(temp)
{
	setcvar("ex_float", temp);
	temp = drm_getCvarFloat("ex_float");
	return temp;
}

startfogThreads()
{
	level endon("ex_gameover");
	wait .05;
	level thread overridefog();
}

overridefog()
{
	level endon("ex_gameover");

	if(randomInt(100) < level.ex_efog)
	{
		if(level.ex_efogdensity2) thread fadeExpFog();
		else setExpFog(level.ex_efogdensity, level.ex_efogred, level.ex_efoggreen, level.ex_efogblue, 0);
	}
}

fadeExpFog()
{
	level endon("awe_killthreads");

	if(level.awe_roundbased)
	{
		time = level.roundlength * 30;
		if(!time) time = 5 * 30;
	}
	else
	{
		time = level.timelimit * 30;
		if(!time) time = 20 * 30;
	}

	if(randomInt(2))
	{
		startfog = level.ex_efogdensity;
		endfog = level.ex_efogdensity2;
	}
	else
	{
		startfog = level.ex_efogdensity2;
		endfog = level.ex_efogdensity;
	}

	density = startfog;
	delta = (endfog - startfog)/time;

	for(i=0;i<time;i++)
	{
		setExpFog(density, level.ex_efogred, level.ex_efoggreen, level.ex_efogblue, 0);
		density = density + delta;
		wait 1;
	}

	density = endfog;
	delta = 0 - delta;

	for(i=0;i<time;i++)
	{
		setExpFog(density, level.ex_efogred, level.ex_efoggreen, level.ex_efogblue, 0);
		density = density + delta;
		wait 1;
	}

	density = startfog;
	delta = 0 - delta;

	for(i=0;i<time;i++)
	{
		setExpFog(density, level.ex_efogred, level.ex_efoggreen, level.ex_efogblue, 0);
		density = density + delta;
		wait 1;
	}

	density = endfog;
	delta = 0 - delta;

	for(i=0;i<time;i++)
	{
		setExpFog(density, level.ex_efogred, level.ex_efoggreen, level.ex_efogblue, 0);
		density = density + delta;
		wait 1;
	}
}
