
rainHard(transition)
{
	level notify ("rain_change", "hard", transition);
	level thread rainEffectChange(10, transition);
	wait (transition*0.5);
	if(level.ex_tlfx) thread lightningThink();
	level.ex_amibient_weather["rain"] = "";
	wait (transition*0.5);
}

rainLight(transition)
{
	level notify ("rain_change", "light", transition);
	level thread rainEffectChange(5, transition);
	wait (transition*0.5);
	if(level.ex_tlfx) thread lightningThink();
	level.ex_amibient_weather["rain"] = "light";
	wait (transition*0.5);
}

rainNone(transition)
{
	level notify ("rain_change", "none", transition);
	level thread rainEffectChange(0, transition);
	wait (transition*0.5);
	level.ex_amibient_weather["rain"] = "norain";
	wait (transition*0.5);
}

rainInit(lvl)
{
	if (lvl == "none")
	{
		level.ex_rainlvl = 0; // starting rain level
		level.ex_effect["rain_drops"] = level.ex_effect["rain_" + level.ex_rainlvl];
		rainNone(0.1);
	}
	else
	if (lvl == "light")
	{
		level.ex_rainlvl = 5; // starting rain level
		level.ex_effect["rain_drops"] = level.ex_effect["rain_" + level.ex_rainlvl];
		rainLight(0.1);
	}
	else
	{
		level.ex_rainlvl = 10; // starting rain level
		level.ex_effect["rain_drops"] = level.ex_effect["rain_" + level.ex_rainlvl];
		rainHard(0.1);
	}
}

rainEffectChange(change, transition)
{
	level notify ("rain_level_change");
	level endon ("rain_level_change");

	if(level.ex_rainlvl > change)
	{
		dif = level.ex_rainlvl - change;
		transition /= dif;
		for (i=0;i<dif;i++)
		{
			wait (transition);
			level.ex_rainlvl--;
			level.ex_effect["rain_drops"] = level.ex_effect["rain_" + level.ex_rainlvl];
		}
	}

	if (level.ex_rainlvl < change)
	{
		dif = change - level.ex_rainlvl;
		transition /= dif;
		for (i=0;i<dif;i++)
		{
			wait (transition);
			level.ex_rainlvl++;
			level.ex_effect["rain_drops"] = level.ex_effect["rain_" + level.ex_rainlvl];
		}
	}
}

playerWeather()
{
	self endon("disconnect");
	
	for (;;)
	{
		playfx (level.ex_effect["rain_drops"], self.origin + (0,0,650), self.origin + (0,0,680) );
		if(level.ex_rainlvl >= 8) playfx ( level.ex_effect["rain_heavy_cloudtype"], self.origin + (0,0,650));
		wait (0.3);
	}
}

rainlevelRandomwait()
{
	if(level.ex_rainlvl == 0) return (randomfloat (30));
	else if(level.ex_rainlvl == 1) return (randomfloat (24));
	else if(level.ex_rainlvl == 2) return (randomfloat (20));
	else if(level.ex_rainlvl == 3) return (randomfloat (15));
	else if(level.ex_rainlvl == 4) return (randomfloat (12));
	else if(level.ex_rainlvl == 5) return (randomfloat (9));
	else if(level.ex_rainlvl == 6) return (randomfloat (8));
	else if(level.ex_rainlvl == 7) return (randomfloat (8));
	else if(level.ex_rainlvl == 8) return (randomfloat (7));
	else if(level.ex_rainlvl == 9) return (randomfloat (6));
	else if (level.ex_rainlvl == 10) return (randomfloat (5));
}

rainlevelwait()
{
	if(level.ex_rainlvl == 0) return (20);
	else if(level.ex_rainlvl == 1) return (18);
	else if(level.ex_rainlvl == 2) return (16);
	else if(level.ex_rainlvl == 3) return (14);
	else if(level.ex_rainlvl == 4) return (12);
	else if(level.ex_rainlvl == 5) return (10);
	else if(level.ex_rainlvl == 6) return (9);
	else if(level.ex_rainlvl == 7) return (8);
	else if(level.ex_rainlvl == 8) return (7);
	else if(level.ex_rainlvl == 9) return (6);
	else if (level.ex_rainlvl == 10) return (5);
}

lightningThink()
{
	level endon ("rain_change");

	// in case we get broken out, we may have lightning sooner than planned if we're getting rainier
	nextStrike = gettime() + ((rainlevelwait() + rainlevelRandomwait())*1000);

	if(nextStrike < level.ex_nextLightning) level.ex_nextLightning = nextStrike;

	for (;;)
	{
		timer = (level.ex_nextLightning - gettime()) * 0.001;
		if(timer > 0) wait (timer);
			
		lightningFlash();
		level.ex_nextLightning = gettime() + ((rainlevelwait() + rainlevelRandomwait())*1000);
	}
}

lightningFlash()
{
	flash[0] = "quick";
	flash[1] = "double";
	flash[2] = "triple";

	lightfx[0] = "lightning";

	// get a random position in the sky
	position = getTLPosition();

	thread thunder(position);			

	flashType = randomint(flash.size);
	lightFx = lightfx[randomInt(lightfx.size)];

	lit_num = 0;

	switch (flash[flashType])
	{
		case "quick":
		{
			lightning(lightFx, position);
			break;
		}
		case "double":
		{
			lightning(lightFx, position);
			wait (0.05);
			lightning(lightFx, position);
			break;
		}
		case "triple":
		{
			lightning(lightFx, position);
			wait (0.05);
			lightning(lightFx, position);
			wait (0.5);
			lightning(lightFx, position);
			break;
		}
	}
}

lightning(lightFx, position)
{
	playfx(level.ex_effect[lightFx], position);
}

thunder(position)
{
	if(level.ex_rainlvl == 0) wait (6 + randomfloat(2));
	else if(level.ex_rainlvl == 1) wait (5 + randomfloat(1.8));
	else if(level.ex_rainlvl == 2) wait (4.5 + randomfloat(1.6));
	else if(level.ex_rainlvl == 3) wait (4 + randomfloat(1.6));
	else if(level.ex_rainlvl == 4) wait (3.5 + randomfloat(1.5));
	else if(level.ex_rainlvl == 5) wait (3 + randomfloat(1.5));
	else if(level.ex_rainlvl == 6) wait (2.5 + randomfloat(1.2));
	else if(level.ex_rainlvl == 7) wait (2 + randomfloat(1));
	else if(level.ex_rainlvl == 8) wait (1.9 + randomfloat(0.5));
	else if(level.ex_rainlvl == 9) wait (1.5);
	else if(level.ex_rainlvl == 10) wait (1);
	
	thunder = spawn ("script_origin",(0,0,0));
	thunder.origin = position;

	thunder playsound ("elm_thunder", "sounddone");

	thunder waittill ("sounddone");
	thunder delete();	
}

player_launch()
{
	thread playerWeather(); // make the actual rain effect generate around the player
}

rainControl()
{
	level.ex_effect["rain_heavy_cloudtype"]	= loadfx ("fx/misc/rain_heavy_cloudtype.efx");
	level.ex_effect["rain_10"]		= loadfx ("fx/misc/rain_heavy.efx");
	level.ex_effect["rain_9"]		= loadfx ("fx/misc/rain_9.efx");
	level.ex_effect["rain_8"]		= loadfx ("fx/misc/rain_8.efx");
	level.ex_effect["rain_7"]		= loadfx ("fx/misc/rain_7.efx");
	level.ex_effect["rain_6"]		= loadfx ("fx/misc/rain_6.efx");
	level.ex_effect["rain_5"]		= loadfx ("fx/misc/rain_5.efx");
	level.ex_effect["rain_4"]		= loadfx ("fx/misc/rain_4.efx");
	level.ex_effect["rain_3"]		= loadfx ("fx/misc/rain_3.efx");
	level.ex_effect["rain_2"]		= loadfx ("fx/misc/rain_2.efx");
	level.ex_effect["rain_1"]		= loadfx ("fx/misc/rain_1.efx");
	level.ex_effect["rain_0"]		= loadfx ("fx/misc/rain_0.efx");

	// Thunder & Lightning
	if(level.ex_tlfx)
	{
		level.ex_effect["lightning"] = loadfx ("fx/misc/lightning.efx");
		level.ex_nextLightning = gettime() + 10000 + randomfloat(4000); // sets when the first lightning of the level will go off
	}

	// controls the temperment of the weather
	rainInit("none"); // "none" "light" or "hard"

	wait (15);
	rainLight(15);
	wait (30);
	rainHard(15);
	wait (20);
	rainLight(10);
	wait (15);
	rainHard(10);
	wait (60);

	for (;;)
	{	
		rainLight(15);
		wait (60);
		rainNone(10);
		wait (25 + randomint(15));
		rainLight(15);
		wait (25 + randomint(15));
		rainHard(15);
		wait (60);
		rainLight(10);
		wait (25 + randomint(15));
		rainHard(10);
		wait (25 + randomint(15));
	}
}

getTLPosition()
{
	//get a random position
	xpos = level.ex_playArea_Min[0] + randomInt(level.ex_playArea_Width);
	ypos = level.ex_playArea_Min[1] + randomInt(level.ex_playArea_Length);
	zpos =  level.ex_mapArea_Max[2] - randomInt(100);	

	position = ( xpos, ypos, zpos);
	
	return position;
}