/////////////////////////////////////////////////
// File: pfx.wdl
// WDL code fire, smoke, rain and spell - effects
/////////////////////////////////////////////////

/***************
* Smoke effect *
***************/

//bmap smoke_parmap=<smoke.pcx>;
bmap smoke_parmap2=<rauch01.bmp>;

function smoke_effect();	// prototype
function smoke_property();	// prototype

function smoke_effect() {
//	my.beam = on;
	my.x += random(32)-16;
	my.y += random(32)-16;
	my.vel_x=(Wind_X) + (random(2) -1);
	my.vel_y=(Wind_Y) + (random(2) -1);
	my.vel_z=random(3)+8 + ((Temperaturhoehe-20) * 0.3);
	my.move=on;
	my.size=120;
	my.bmap=smoke_parmap2;
	my.flare=on;
	my.bright=on;
	my.alpha=15;
	my.lifespan = 100;
	my.function=smoke_property;
}

function smoke_property()
{ 
	MY.ALPHA -= 0.30 * TIME;
	MY.SIZE += 2 * TIME;
	MY.VEL_Z = MY.ALPHA + ((Temperaturhoehe-20) * 0.3);
	IF ((MY.ALPHA <= 0) || (MY.ALPHA > 15)) {
		MY.LIFESPAN = 0;
	}
}

ACTION EFFEKT_Rauch_gross {
	WHILE (1) {
		effect (smoke_effect, 1, MY.X,NULLVECTOR);
	WAITT (1);
	}
}

/***************
* Smoke2 effect *
***************/

//bmap smoke2_parmap=<smoke.pcx>;

function smoke2_effect();	// prototype
function smoke2_property();	// prototype

function smoke2_effect() {
	my.x += random(8)-4;
	my.y += random(8)-4;
	my.vel_x=(Wind_X) + (random(2) -1);
	my.vel_y=(Wind_Y) + (random(2) -1);
	my.vel_z=random(3)+4 + ((Temperaturhoehe-20) * 0.15);
	my.move=on;
	my.size=50;
	my.bmap=smoke_parmap2;
	my.flare=on;
	my.bright=on;
	my.alpha=10;
	my.lifespan = 25;
	my.function=smoke2_property;
}

function smoke2_property()
{ 
	my.alpha-=0.4*time;
	my.size += 0.2 * TIME;
	MY.VEL_Z = MY.ALPHA + ((Temperaturhoehe-20) * 0.15);
	if ((my.alpha<=0) || (my.alpha>10)) {
		my.lifespan=0;
	}
}

ACTION EFFEKT_Rauch_klein {
	WHILE (1) {
		effect (smoke2_effect, 3, MY.X, NULLVECTOR);
	WAITT (1);
	}
}



/**************
* Fire effect *
**************/

//bmap fire_parmap=<fire.pcx>;
bmap Flamme0 = <Feuer00.bmp>;
bmap Flamme1 = <Feuer01.bmp>;
bmap Flamme2 = <Feuer02.bmp>;

function fire_effect();
function fire_property();


function fire_effect() {
var zentrum;
	zentrum.X = my.x;
	zentrum.Y = my.y;
//	VEC_SET (MY._SPEED, MY.X);
	my.x+=random (20*(sin(total_ticks*200) * cos(total_ticks*50)));
	my.y+=random (20*(sin(total_ticks*200) * sin(total_ticks*50)));
	my.z+=random (6);

//	my.beam = on;
	my.size=50;
	my.move=on;
	IF (INT(Random(3)) == 1) {
		MY.BMAP = Flamme0;
	} ELSE {
		IF (INT(Random(2)) == 1) {
			MY.BMAP = Flamme1;
		} ELSE {
			MY.BMAP = Flamme2;
		}
	}
	my.flare=on;
	my.bright=on;
	my.alpha=25;
	my.lifespan = 14;
	my.vel_x=((Wind_X / 3) + (random(2) -1) / 4) + (zentrum.X - my.x)/15;
	my.vel_y=((Wind_Y / 3) + (random(2) -1) / 4) + (zentrum.Y - my.y)/15;
//	my.vel_x=((Wind_X / 3) + (random(2) -1) / 4) + (MY._SPEED_X - my.x)/15;
//	my.vel_y=((Wind_Y / 3) + (random(2) -1) / 4) + (MY._SPEED_Y - my.y)/15;
	my.vel_z=1+random(2) + ((Temperaturhoehe-20) * 0.1);
	my.function=fire_property;
}

function fire_property()
{
	my.alpha-=1.8*time;
	IF (my.alpha > 10) { my.size -= 0.2*time; } ELSE { my.size -= 0.4*time; }
	IF (my.alpha > 20) { my.vel_z=1+random(2) + ((Temperaturhoehe-20) * 0.1); } ELSE { IF (my.alpha > 5) { my.vel_z=(1+random(2))*2 + ((Temperaturhoehe-20) * 0.1); } ELSE { my.vel_z=(1+random(2))*5 + ((Temperaturhoehe-20) * 0.1); } }
	if ((my.alpha<=0) || (my.alpha>25)) {
		my.visible = off; my.lifespan=0;
	}
}

//var flacker = 0;

ACTION EFFEKT_Feuer_gross {
	MY.LIGHTRANGE = 150;
	MY.LIGHTRED = 30;
	MY.LIGHTGREEN = 30;
	MY.LIGHTBLUE = 0;
	WHILE (1) {
		IF (INT(total_ticks) / 3 == INT(INT(total_ticks) / 3)) {
			Effect (fire_effect, 2, MY.X, NULLVECTOR);
			Effect (smoke_effect, 1, MY.X, NULLVECTOR);
		}
//			MY.LIGHTRANGE = MY.LIGHTRED*4 + RANDOM(80);
			MY.LIGHTRANGE = MY.LIGHTRED * 5 + ABS(80*(sin(total_frames * 25) * cos(total_frames*10)));
	WAIT (1);
	}
}


/**************
* Fire2 effect *
**************/

//bmap fire2_parmap=<fire.pcx>;

function fire2_effect();
function fire2_property();


function fire2_effect() {
var zentrum;
	zentrum.X = my.x;
	zentrum.Y = my.y;
	my.x+=random (10*(sin(total_ticks*200) * cos(total_ticks*50)));
	my.y+=random (10*(sin(total_ticks*200) * sin(total_ticks*50)));
	my.z+=random (2)-1;

	my.size=35;
	my.vel_x=((Wind_X / 3) + (random(2) -1) / 4) + (zentrum.X - my.x)/15;
	my.vel_y=((Wind_Y / 3) + (random(2) -1) / 4) + (zentrum.Y - my.y)/15;
	my.vel_z=(1+random(2) + ((Temperaturhoehe-20) * 0.1))/2;
	my.move=on;
	IF (INT(Random(3)) == 1) {
		MY.BMAP = Flamme0;
	} ELSE {
		IF (INT(Random(2)) == 1) {
			MY.BMAP = Flamme1;
		} ELSE {
			MY.BMAP = Flamme2;
		}
	}
	my.flare=on;
	my.bright=on;
	my.alpha=10;
my.lifespan = 12;
	my.function=fire_property;
}

function fire2_property() {
	my.alpha-=0.9*time;
	IF (my.alpha > 10) { my.size -= 0.2; } ELSE { my.size -= 0.4; }
	IF (my.alpha > 20) { my.vel_z=((1+random(2))/2 + ((Temperaturhoehe-20) * 0.1))/2; } ELSE { IF (my.alpha > 5) { my.vel_z=((1+random(2)) + ((Temperaturhoehe-20) * 0.1))/2; } ELSE { my.vel_z=((1+random(2))*2.5 + ((Temperaturhoehe-20) * 0.1))/2; } }
	if ((my.alpha<=0) || (my.alpha>10)) {
		my.lifespan=0;
	}
}

var flacker2 = 0;

ACTION EFFEKT_Feuer_klein {
	MY.LIGHTRANGE = 100;
	MY.LIGHTRED = 30;
	MY.LIGHTGREEN = 30;
	MY.LIGHTBLUE = 0;
	WHILE (1) {
		IF (INT(total_ticks) / 2 == INT(INT(total_ticks) / 2)) {
			Effect (fire2_effect, 4, MY.X, NULLVECTOR);
			Effect (smoke2_effect, 2, MY.X, NULLVECTOR);
		}
//			MY.LIGHTRANGE = MY.LIGHTRED*3 + Random(50);
			MY.LIGHTRANGE = MY.LIGHTRED * 4 + ABS(50*(sin(total_frames * 25) * cos(total_frames*10)));
	WAIT (1);
	}
}


/***************
* Shine effect *
***************/

ACTION EFFEKT_Glanz {
WHILE(1) {
	vec_set(temp.x, camera.x);
	vec_sub(temp.x, my.x);
	vec_to_angle(temp2.pan, temp.x);
		my.u = 10 - temp2.pan * 2;
		my.v = 10 - temp2.tilt * 2;
	wait(1);
	}
}



//BMAP Blaues_Flare = <Blueflare.pcx>;

/****************
* Glitzereffect *
****************/
/*
function Glitzer_effect()
{
//	MY.X += Random(5);
//	MY.Y += Random(5);
//	MY.SIZE = 10;
//	MY.VEL_X = (Wind_X / 6) + (random(2) -1) / 4;
//	MY.VEL_Y = (Wind_Y / 6) + (random(2) -1) / 4;
//	MY.VEL_Z = -1 - Random(2);
//	MY.MOVE = ON;
	MY.BMAP = Blaues_Flare;
//	MY.FLARE = ON;
//	MY.BRIGHT = ON;
//	MY.ALPHA = 50;
	MY.FUNCTION = fire_property;
}
*/





//////////////////////////////////////////////////////////////////////////
//Glass Effect

ACTION Glass
{
MY._Kraft = SIN(((SUN_ANGLE - MY.PAN) + (CAMERA.PAN - SUN_ANGLE))/2)*100;
IF (MY._Kraft < 0) { MY._Kraft *= -1; }
MY.TRANSPARENT = ON;
MY.ALPHA = MY._Kraft;
MY.ALBEDO = 100;
MY.METAL = ON;
	WHILE 1 {
		MY._Kraft = (SIN((CAMERA.PAN - SUN_ANGLE)+MY.PAN)*80);
		IF (MY._Kraft < 0) { MY._Kraft *= -1; }		MY._Kraft += 10;
			my.alpha = MY._Kraft;
	WAIT (1);
	}
}

/*The idea is to randomize the transparency (85 to 95) just a bit to give the illusion of the windows reflection. I added the key checks so it only does it when the player is moving, if you use something other than the WASD key setup you will want to change the key press checks. You may also want to change the alpha value to get the best results. 

----------------------------------------------



the cursor keys are:

key_cuu - Up arrow
key_cud - Down arrow
key_cul - Left arrow
key_cur - Right arrow */

var Flagge_oben;
var Flagge_unten;
var TempFlagge;
var FlaggenZ;
var FlaggenTimer;
var FlatterTimer;
var FlaggenRandom;
var Flaggenwinkel;

ACTION ITEM_Flagge3x5
{
VEC_FOR_MESH (Flagge_oben, ME, 4);
VEC_FOR_MESH (Flagge_unten, ME, 3);
Flagge_unten.X = Flagge_oben.X;
Flagge_unten.Y = Flagge_oben.Y;
Flagge_unten.Z = Flagge_oben.Z - 32;
VEC_FOR_MESH (Flagge_oben, ME, 6);
VEC_FOR_MESH (Flagge_unten, ME, 5);
Flagge_unten.X = Flagge_oben.X;
Flagge_unten.Y = Flagge_oben.Y;
Flagge_unten.Z = Flagge_oben.Z - 32;
VEC_TO_MESH (Flagge_unten, ME, 5);
// 4 -> 3 -> 8 -> 11 -> 14
// 2 -> 1 -> 7 -> 10 -> 13
// 6 -> 5 -> 9 -> 12 -> 15

MY.METAL = ON;
MY.ALBEDO = 100;
//MY.PAN = Random(360);
	VEC_SET(TEMP, MY.POS);
	TEMP.Z -= 1000;
	TRACE_MODE = IGNORE_SPRITES + IGNORE_ME + IGNORE_PASSENTS + IGNORE_PASSABLE;
	RESULT = TRACE(MY.POS,temp); MY.Z = TARGET.Z - MY.MIN_Z - 32;
	WHILE 1 {
		TEMP.X = 0;
		TEMP.Y = 0;
		TEMP.Z = 32;
		TempFlagge.X = Wind_X*2 + FlaggenRandom.X;
		TempFlagge.Y = Wind_Y*2 + FlaggenRandom.Y;
		TempFlagge.Z = 0;
		Flaggenwinkel.PAN = -MY.PAN;
		VEC_ROTATE (TempFlagge, Flaggenwinkel.PAN);

		Restart:
		IF (FlaggenTimer == 11) {
			VEC_FOR_MESH (Flagge_oben, ME, 12);
			VEC_FOR_MESH (Flagge_unten, ME, 15);
			FlaggenTimer = 12;
		}
		IF (FlaggenTimer == 10) {
			VEC_FOR_MESH (Flagge_oben, ME, 10);
			VEC_FOR_MESH (Flagge_unten, ME, 13);
			FlaggenTimer = 11;
		}
		IF (FlaggenTimer == 9) {
			VEC_FOR_MESH (Flagge_oben, ME, 11);
			VEC_FOR_MESH (Flagge_unten, ME, 14);
			FlaggenTimer = 10;
		}
		IF (FlaggenTimer == 8) {
			VEC_FOR_MESH (Flagge_oben, ME, 9);
			VEC_FOR_MESH (Flagge_unten, ME, 12);
			FlaggenTimer = 9;
		}
		IF (FlaggenTimer == 7) {
			VEC_FOR_MESH (Flagge_oben, ME, 7);
			VEC_FOR_MESH (Flagge_unten, ME, 10);
			FlaggenTimer = 8;
		}
		IF (FlaggenTimer == 6) {
			VEC_FOR_MESH (Flagge_oben, ME, 8);
			VEC_FOR_MESH (Flagge_unten, ME, 11);
			FlaggenTimer = 7;
		}

		IF (FlaggenTimer == 5) {
			VEC_FOR_MESH (Flagge_oben, ME, 5);
			VEC_FOR_MESH (Flagge_unten, ME, 9);
			FlaggenTimer = 6;
		}
		IF (FlaggenTimer == 4) {
			VEC_FOR_MESH (Flagge_oben, ME, 1);
			VEC_FOR_MESH (Flagge_unten, ME, 7);
			FlaggenTimer = 5;
		}
		IF (FlaggenTimer == 3) {
			VEC_FOR_MESH (Flagge_oben, ME, 3);
			VEC_FOR_MESH (Flagge_unten, ME, 8);
			FlaggenTimer = 4;
		}
		IF (FlaggenTimer == 2) {
			VEC_FOR_MESH (Flagge_oben, ME, 6);
			VEC_FOR_MESH (Flagge_unten, ME, 5);
			FlaggenTimer = 3;
		}
		IF (FlaggenTimer == 1) {
			VEC_FOR_MESH (Flagge_oben, ME, 2);
			VEC_FOR_MESH (Flagge_unten, ME, 1);
			FlaggenTimer = 2;
		}
		IF (FlaggenTimer == 0) {
			VEC_FOR_MESH (Flagge_oben, ME, 4);
			VEC_FOR_MESH (Flagge_unten, ME, 3);
			FlaggenTimer = 1;
		}


		IF (Flagge_unten.X < Flagge_oben.X + Wind_X*2 - 2 + FlaggenRandom.X) { Flagge_unten.X += 0.5 * TIME; }
		IF (Flagge_unten.X > Flagge_oben.X + Wind_X*2 + 2 + FlaggenRandom.X) { Flagge_unten.X -= 0.5 * TIME; }
		IF (Flagge_unten.Y < Flagge_oben.Y + Wind_Y*2 - 2 + FlaggenRandom.Y) { Flagge_unten.Y += 0.5 * TIME; }
		IF (Flagge_unten.Y > Flagge_oben.Y + Wind_Y*2 + 2 + FlaggenRandom.Y) { Flagge_unten.Y -= 0.5 * TIME; }
		FlaggenZ.X = VEC_LENGTH (TempFlagge.X);
		FlaggenZ.Y = VEC_LENGTH (TEMP.X);
		IF (Flaggenz.X > FlaggenZ.Y) { FlaggenZ.X = FlaggenZ.Y; }
		FlaggenZ.Z = SQRT (FlaggenZ.Y * FlaggenZ.Y - FlaggenZ.X * FlaggenZ.X);
		IF (Flagge_unten.Z < Flagge_oben.Z - FlaggenZ.Z - 0.2) { Flagge_unten.Z += 0.3 * TIME; }
		IF (Flagge_unten.Z > Flagge_oben.Z - FlaggenZ.Z + 0.2) { Flagge_unten.Z -= 0.3 * TIME; }

		IF (Flagge_unten.X > Flagge_oben.X + 32) { Flagge_unten.X = Flagge_oben.X + 32; }
		IF (Flagge_unten.X < Flagge_oben.X - 32) { Flagge_unten.X = Flagge_oben.X - 32; }
		IF (Flagge_unten.Y > Flagge_oben.Y + 32) { Flagge_unten.Y = Flagge_oben.Y + 32; }
		IF (Flagge_unten.Y < Flagge_oben.Y - 32) { Flagge_unten.Y = Flagge_oben.Y - 32; }
		
		IF (FlaggenTimer == 1) { VEC_TO_MESH (Flagge_unten, ME, 3); }
		IF (FlaggenTimer == 2) { VEC_TO_MESH (Flagge_unten, ME, 1); }
		IF (FlaggenTimer == 3) { VEC_TO_MESH (Flagge_unten, ME, 5); }
		IF (FlaggenTimer == 4) { VEC_TO_MESH (Flagge_unten, ME, 8); }
		IF (FlaggenTimer == 5) { VEC_TO_MESH (Flagge_unten, ME, 7); }
		IF (FlaggenTimer == 6) { VEC_TO_MESH (Flagge_unten, ME, 9); }
		IF (FlaggenTimer == 7) { VEC_TO_MESH (Flagge_unten, ME, 11); }
		IF (FlaggenTimer == 8) { VEC_TO_MESH (Flagge_unten, ME, 10); }
		IF (FlaggenTimer == 9) { VEC_TO_MESH (Flagge_unten, ME, 12); }
		IF (FlaggenTimer == 10) { VEC_TO_MESH (Flagge_unten, ME, 14); }
		IF (FlaggenTimer == 11) { VEC_TO_MESH (Flagge_unten, ME, 13); }
		IF (FlaggenTimer == 12) { VEC_TO_MESH (Flagge_unten, ME, 15); }
		IF (FlaggenTimer < 12) { GOTO Restart; } ELSE { FlaggenTimer = 0; FlatterTimer + = 1 * TIME; }

		MY._Kraft += VEC_LENGTH (TempFlagge.X)/2 * TIME;
		IF (MY._Kraft > 360) { MY._Kraft -= 360; }
		IF (MY._Kraft < 0) { MY._Kraft += 360; }
		IF (FlatterTimer > 16) { FlaggenRandom.X = SIN(MY._Kraft) * 10; FlaggenRandom.Y = COS(MY._Kraft) * 10; FlatterTimer = 0; }
	WAIT (1);
	}
}

ACTION Waffe_rotate
{
MY.METAL = ON;
MY.SHADOW = ON;
MY.ALBEDO = 100;
	WHILE 1 {
		MY.PAN += 1;
	WAIT (1);
	}
}

ACTION Near_Entities
{
MY.NEAR = ON;
}


function wellen_erschaffen()
{
	MY.Passable = on;
	MY.SCALE_X = 1;
	MY.SCALE_Y = 1;
	my.ambient = 100;
	my.flare = on;
	MY.TRANSPARENT = ON;
	my.bright = on;
	my.alpha = 45;
	WHILE 1 {
		MY.SCALE_X += 0.125 * TIME;
		MY.SCALE_Y += 0.125 * TIME;
		MY.alpha -= 2.4 * time;
		IF (MY.alpha <= 0) {
			Remove(ME); RETURN;
		}
	WAIT (1);
	}
}

function Fontaene_flug()
{
	MY.VEL_Z -= 2 * TIME;
	RESULT = CONTENT(MY.X);
		IF (RESULT == CONTENT_PASSABLE) { CREATE (<Wellen1.mdl>, MY.X, Wellen_erschaffen); MY.LIFESPAN = 0; }
		IF (RESULT == CONTENT_SOLID) { MY.LIFESPAN = 0; }
}

function Wasser_Fontaene()
{
	MY.X += Random(10)-5;
	MY.Y += Random(10)-5;
	MY.Z += Random(5)-20;
	MY.VEL_X = Random(4)-2 + Wind_X/5;
	MY.VEL_Y = Random(4)-2 + Wind_Y/5;
	MY.VEL_Z = (Random(12)+4);  // 4 to 16
	MY.SIZE = 6;
	MY.FLARE = ON;
	MY.BRIGHT = ON;
	MY.BMAP = Spritzer;
	MY.MOVE = ON;
	MY.FUNCTION = Fontaene_flug;
}

ACTION Springbrunnen
{
MY.INVISIBLE = ON;
	WHILE (1) {
		Effect (Wasser_Fontaene, 2, MY.X, NULLSKILL);
	WAIT (1);
	}
}