path "E:\\GStudio\\GStudio_pro_6.6\\template";	// Path to WDL templates subdirectory

////////////////////////////////////////////////////////////////////////////
// The engine starts in the resolution given by the follwing vars.
var video_mode =8;	 // screen size 640x480
var video_depth = 32; // 16 bit colour D3D mode
PLUGINDIR=".";
/////////////////////////////
// Variablen
//////////////////////////////
string level_str = <demo7.WMB>; // give file names in angular brackets

dllfunction r2C_createCubemap(var, bmap);
dllfunction r2C_eventSky();
dllfunction r2C_eventEntities();
dllfunction r2C_eventLost();
dllfunction r2C_eventReset();
var max_particles=10000;
var cubeRenderDelay=1;
var gemMdl=0;

view refl_view
{
	size_x=256;
	size_y=256;
	layer=-1;
	aspect = 1;
	arc=90;
	flags=NOFLAG1;
}
view refl2_view
{
	size_x=256;
	size_y=256;
	layer=-2;
	aspect = 1;
	arc=90;
	flags=NOFLAG1;
}

view refl3_view
{
	size_x=256;
	size_y=256;
	layer=-3;
	aspect = 1;
	arc=90;
	flags=NOFLAG1;
}

view refl4_view
{
	size_x=256;
	size_y=256;
	layer=-4;
	aspect = 1;
	arc=90;
	flags=NOFLAG1;
}

view refl5_view
{
	size_x=256;
	size_y=256;
	layer=-5;
	aspect = 1;
	arc=90;
	flags=NOFLAG1;
}
view refl6_view
{
	size_x=256;
	size_y=256;
	layer=-6;
	aspect = 1;
	arc=90;
	flags=NOFLAG1;
}



bmap* cube_map;
bmap* cube_map2;

material mat_crystal
{
	flags=tangent;
	effect="crystal.fx";
}
function reload_shader()
{
	effect_load(mat_crystal,"crystal.fx");
}
on_r=reload_shader;

////////////////////////////////////////////////////////////////
// The main() function is started at game start
var vSpeed[3];
var vAngularSpeed[3];
var vForce[3];
var vMove[3];
var iSpeed = 5;
function main()
{
	level_load(level_str);
	wait(2);
	camera.x=-100;
	camera.z=200;
	cube_map = bmap_createblack(256, 256, 32);
	wait(1);
	//	refl_view.bmap = cube_map;
	cube_map = r2C_createCubemap(256, cube_map);
	refl_view.alpha = cube_map;		//dirty hack
	refl2_view.alpha = cube_map;
	refl3_view.alpha = cube_map;
	refl4_view.alpha = cube_map;
	refl5_view.alpha = cube_map;
	refl6_view.alpha = cube_map;
	wait(1);
	mat_crystal.skin1 = cube_map;
//	wait(2);	
	render_sky=r2C_eventSky;
	render_entities=r2C_eventEntities;
	on_d3d_lost = r2C_eventLost;
	on_d3d_reset = r2C_eventReset;
	fps_max=100;
	while(1)
	{
		if(mouse_mode==0)
		{
			vForce.x = -5*mouse_force.x;	// pan angle
			vForce.y = 5*mouse_force.y;	// tilt angle
			vForce.z = 0;	// roll angle
			vec_accelerate(vMove,vAngularSpeed,vForce,0.8);
			vec_add(camera.pan,vMove);				
			vForce.x = iSpeed * (key_w - key_s);		// forward
			vForce.y = iSpeed * (key_a - key_d);		// sideward
			vForce.z = iSpeed * (key_home - key_end);	// upward
			vec_accelerate(vMove,vSpeed,vForce,0.5);
			vec_rotate(vMove,camera.pan);
			vec_add(camera.x,vMove);
		}
		wait(1);
	}
	
}




panel tes_pan
{
	//	flags=visible;
	size_x=768;
	size_y=128;
}

action crystal
{
	my.flag1=on;
	my.material=mat_crystal;
	//	bmap_to_cubemap(cube_map);
	//	cube_map2 = bmap_createblack(768*2, 128*2, 32);
	//	bmap_to_cubemap(cube_map2);
	//	mat_crystal.skin1=bmap_to_cubemap(cube_map2);
	//	tes_pan.bmap=cube_map2;
	refl_view.visible = on;
	refl_view.arc=90;
	vec_set(refl_view.pan, nullvector);
	//		var i;
	//		i=0;
	var t;
	wait(2);
	//		breakpoint;
	t=0;
	refl_view.pos_x=-256;
	refl_view.pan = -90;
	my.skill1=gemMdl;
	while(1)
	{
		if(my.skill1!=int(gemMdl))
		{
			my.skill1=int(gemMdl);
			if(my.skill1==0)
			{
				ent_morph(my, "crystal2.mdl");
			}	
			if(my.skill1==1)
			{
				ent_morph(my, "crystal1.mdl");
			}	
			if(my.skill1==2)
			{
				ent_morph(my, "head.mdl");
			}	
			if(my.skill1==3)
			{
				ent_morph(my, "ball1.mdl");
			}	
			if(my.skill1==4)
			{
				ent_morph(my, "ball2.mdl");
			}	
			if(my.skill1==5)
			{
				ent_morph(my, "alien.mdl");
			}	
		}	
		//		refl_view.pos_x=t;
		//	mat_crystal.skin1=bmap_to_cubemap(refl_view.bmap);
		refl_view.visible = on;
		vec_set(refl_view.x, my.x);
		if(cubeRenderDelay==0)
		{
			refl2_view.visible = on;
			refl3_view.visible = on;
			refl4_view.visible = on;
			refl5_view.visible = on;
			refl6_view.visible = on;
			vec_set(refl2_view.x, my.x);
			vec_set(refl3_view.x, my.x);
			vec_set(refl4_view.x, my.x);
			vec_set(refl5_view.x, my.x);
			vec_set(refl6_view.x, my.x);
			refl_view.pos_x=0;
			refl_view.tilt = 0;
			refl_view.pan = 0;
			
			refl2_view.pos_x=256;
			refl2_view.tilt = 0;
			refl2_view.pan = 90;
			
			refl3_view.pos_x=512;
			refl3_view.tilt = 0;
			refl3_view.pan = 180;
			
			refl4_view.pos_x=768;
			refl4_view.tilt = 0;
			refl4_view.pan = 270;
			
			refl5_view.pos_x=1024;
			refl5_view.tilt = -90;
			refl5_view.pan = 90;
			
			refl6_view.pos_x=1280;
			refl6_view.tilt = 90;
			refl6_view.pan = 90;
		}else
		{	
			if(t==0)
			{
				refl_view.pos_x=0;
				refl_view.tilt = 0;
				refl_view.pan = 0;
				
			}
			if(t==1)
			{
				refl_view.pos_x=256;
				refl_view.tilt = 0;
				refl_view.pan = 90;
				
			}
			if(t==2)
			{
				refl_view.pos_x=512;
				refl_view.tilt = 0;
				refl_view.pan = 180;
				
			}
			if(t==3)
			{
				refl_view.pos_x=768;
				refl_view.tilt = 0;
				refl_view.pan = 270;
				
			}
			if(t==4)
			{
				refl_view.pos_x=1024;
				refl_view.tilt = -90;
				refl_view.pan = 90;
				
			}
			if(t==5)
			{
				refl_view.pos_x=1280;
				refl_view.tilt = 90;
				refl_view.pan = 90;
				
			}
			t+=1;
			if(t>=6)
			{
				t=0;
			}
		}
		//		t=refl_view.pos_x;
		//		refl_view.pos_x=0;
		wait(1);
		
			refl2_view.visible = off;
			refl3_view.visible = off;
			refl4_view.visible = off;
			refl5_view.visible = off;
			refl6_view.visible = off;
			refl_view.visible = off;
			if(cubeRenderDelay>1)
			{
				wait(cubeRenderDelay-1);
			}
		//		wait(100);
		//		bmap_blit(cube_map2, cube_map, vector(t, 0, 0), vector(128*2, 128*2, 0));
		//		mat_crystal.skin1=bmap_to_cubemap(cube_map2);
		//i+=1;
	}
	refl_view.visible=off;
	mat_crystal.skin1=bmap_to_cubemap(cube_map2);
	
}

bmap smk_bmp=<smk.tga>;
bmap blub_bmp=<blub.tga>;
bmap flare_bmp<flare.bmp>;
function smoke_fnc()
{
	my.alpha-=1.5*time;
	my.vel_x*=0.99;
	my.vel_y*=0.99;
	my.vel_z*=0.99;
	my.size+=time;
	if(my.alpha<=0)
	{
		my.lifespan=0;
	}
}


function smoke()
{
	my.bmap=smk_bmp;
	my.size=10+random(10);
	my.alpha=25+random(25);
	my.x+=(1-random(2))*2;
	my.y+=(1-random(2))*2;
	my.z+=(1-random(2))*2;
	my.red=60;
	my.green=60;
	my.blue=60;
	my.gravity=0.02;
	my.move=on;
	my.transparent=on;
	my.lifespan=100000;
	my.function=smoke_fnc;
	
}


action smoker
{
		my.invisible=on;
	while(1)
	{
		temp.x=(1-random(2))*1;
		temp.y=(1-random(2))*1;
		temp.z=5+(random(2));
		effect(smoke, 5, my.x, temp.x);
		wait(1);
	}
}
function blub_fnc()
{
	my.alpha-=6*time;
	my.skill_b+=0.3*time*my.skill_b;
	my.vel_x=-sin(my.skill_a)*my.skill_b*5;
	my.vel_y=-cos(my.skill_a)*my.skill_b*5;
	my.x+=sin(my.skill_a)*my.skill_b*time*2.0;
	my.y+=cos(my.skill_a)*my.skill_b*time*2.0;
	//	my.vel_z-=15.5*time;
	//	my.size+=time;
	if(my.alpha<=0)
	{
		my.lifespan=0;
	}
}

function blubb()
{
	my.bmap=blub_bmp;
	my.size=5+random(5);
	my.alpha=50+random(50);
	my.red=my.vel_x;
	my.green=my.vel_y;
	my.blue=my.vel_z;
	my.x+=(1-random(2))*2;
	my.y+=(1-random(2))*2;
	my.z+=(1-random(2))*2;	
	my.skill_a=random(360);
	my.skill_b=1;
	my.vel_x=sin(my.skill_a)*1;
	my.vel_y=cos(my.skill_a)*1;
	my.vel_z=15;
	my.gravity=0;
	my.bright=on;
	my.move=off;
	my.streak=on;
	my.lifespan=100000;
	my.function=blub_fnc;
	
}


action blubber
{
		my.invisible=on;
	//	return;
	while(1)
	{
		temp.x=random(255);
		temp.y=random(255);
		temp.z=random(255);
		effect(blubb, 250, my.x, temp.x);
		wait(-2);
	}
}

function flare_fadeout()
{
	my.alpha-=2*time;
	my.size-=0.4*time;
	if(my.alpha<=0 || my.size<=0)
	{
		my.lifespan = 0;
	}
}


function flare_par()
{
	my.bmap=flare_bmp;
	my.size=10+random(20);
	my.alpha=50+random(50);
	my.red=my.vel_x;
	my.green=my.vel_y;
	my.blue=my.vel_z;
	my.bright=on;
	my.transparent=on;
	my.x+=(1-random(2))*10;
	my.y+=(1-random(2))*10;
	my.z+=(1-random(2))*10;	
	my.lifespan=10000;
	my.function=flare_fadeout;
	
}

action flare_act()
{
		my.invisible=on;
	my.skill1=random(360);
	my.skill2=50+random(200);
	my.skill3=10-random(20);
	while(1)
	{
		my.x=cos(my.skill1)*my.skill2;
		my.y=sin(my.skill1)*my.skill2;
		my.z=200+sin(my.skill1*0.4)*100;
		temp.x=random(255);
		temp.y=random(255);
		temp.z=random(255);
		my.skill1+=time*my.skill3;
		effect(flare_par, 5, my.x, temp);
		wait(1);
	}
}

function smoke2()
{
	my.bmap=smk_bmp;
	my.size=5+random(5);
	my.alpha=25+random(25);
	my.x+=(1-random(2))*2;
	my.y+=(1-random(2))*2;
	my.z+=(1-random(2))*2;
	my.red=60;
	my.green=60;
	my.blue=60;
	my.gravity=0.02;
	my.move=on;
	my.transparent=on;
	my.lifespan=100000;
	my.function=smoke_fnc;
}

action smoker2_act()
{
		my.invisible=on;
	my.skill1=random(360);
	my.skill2=50+random(200);
	my.skill3=2-random(4);
	while(1)
	{
		my.x=cos(my.skill1)*my.skill2;
		my.y=sin(my.skill1)*my.skill2;
		my.z=200+sin(my.skill1*0.4)*100;
		temp.x=(1-random(2))*1;
		temp.y=(1-random(2))*1;
		temp.z=(random(2));
		my.skill1+=time*my.skill3;
		effect(smoke2, 15, my.x, temp);
		wait(1);
	}
}



bmap slider_b_bmap=<slider_back.tga>;
bmap slider_bmap=<slider.tga>;
bmap button_r_bmap=<button_rechts.tga>;
bmap button_l_bmap=<button_links.tga>;
font verdana="verdana",0,20;

var current_mode=0;
function top_button();
panel top_pan
{
	size_x=400;
	size_y=64;
	pos_x=500;
	flags=visible;
	button=0,0,button_l_bmap,button_l_bmap,button_l_bmap,top_button,null,null;
	button=300,0,button_r_bmap,button_r_bmap,button_r_bmap,top_button,null,null;
	layer=10;
}
text top_text
{
	pos_x=620;
	pos_y=18;
	flags=visible,shadow;
	strings=1;
	string="crytal settings";
	font=verdana;
	layer=10;
}
text bottom_text
{
	pos_x=10;
	pos_y=730;
	flags=visible,shadow;
	string="click right mouse button to move the camera      ";
	font=verdana;
	layer=10;
}
//predefined effect parameters
var crystal_refractiv=1.5;
var crystal_facet=6;
var crystal_facetAll=0;
var crystal_colorred=255;
var crystal_colorgreen=255;
var crystal_colorblue=255;

panel crystal_pan
{
	pos_y=60;
	pos_x=500;
	size_x=400;
	size_y=420;
	layer=10;
	flags=visible;
	window=210,7,140,10,slider_b_bmap,0,0;
	window=210,37,140,10,slider_b_bmap,0,0;
	window=210,67,140,10,slider_b_bmap,0,0;
	window=210,97,140,10,slider_b_bmap,0,0;
	window=210,127,140,10,slider_b_bmap,0,0;
	window=210,157,140,10,slider_b_bmap,0,0;
	hslider = 200,0,160,slider_bmap,1,3,crystal_refractiv;
	hslider = 200,30,160,slider_bmap,1,20,crystal_facet;
	hslider = 200,60,160,slider_bmap,0,255,crystal_colorred;
	hslider = 200,90,160,slider_bmap,0,255,crystal_colorgreen;
	hslider = 200,120,160,slider_bmap,0,255,crystal_colorblue;
	hslider = 200,150,160,slider_bmap,0,1,crystal_facetAll;
	digits=0,0,"refractiv index",verdana,0,0;
	digits=0,30,"facets",verdana,0,0;
	digits=0,60,"red",verdana,0,0;
	digits=0,90,"green",verdana,0,0;
	digits=0,120,"blue",verdana,0,0;
	digits=0,150,"facet plain",verdana,0,0;
	digits=370,0,1.2,verdana,1,crystal_refractiv;
	digits=370,30,1.1,verdana,1,crystal_facet;
	digits=370,60,3.0,verdana,1,crystal_colorred;
	digits=370,90,3.0,verdana,1,crystal_colorgreen;	
	digits=370,120,3.0,verdana,1,crystal_colorblue;	
	digits=370,150,1.0,verdana,1,crystal_facetAll;	
}


panel cube_pan
{
	pos_y=60;
	pos_x=500;
	size_x=400;
	size_y=420;
	layer=10;
//	flags=visible;
	window=210,7,140,10,slider_b_bmap,0,0;
	window=210,37,140,10,slider_b_bmap,0,0;
	hslider = 200,0,160,slider_bmap,0,5,cubeRenderDelay;
	hslider = 200,30,160,slider_bmap,0,5,gemMdl;
	digits=0,0,"cube map delay",verdana,0,0;
	digits=0,30,"crystal model",verdana,0,0;
	digits=370,0,2.0,verdana,1,cubeRenderDelay;
	digits=370,30,2.0,verdana,1,gemMdl;
}

function set_panels()
{
	crystal_pan.visible=off;
	cube_pan.visible=off;
	if(current_mode==0)
	{
		crystal_pan.visible=1;
		crystal_pan.pos_x=screen_size.x-520;
		str_cpy(top_text.string[0],"crystal settings");
	}
	if(current_mode==1)
	{
		cube_pan.visible=1;
		cube_pan.pos_x=screen_size.x-520;
		str_cpy(top_text.string[0],"general settings");
	}
}

function top_button(button_number,panel)
{
	//	beep;
	current_mode+=-(button_number==1)+(button_number==2);
	current_mode=clamp(current_mode,0,2);
	set_panels();	
}


starter mouse_movement()
{
	
	mouse_mode=1-mouse_mode;
	crystal_pan.pos_x=screen_size.x-520;	
	top_pan.pos_x=screen_size.x-top_pan.size_x;
	top_text.pos_x=screen_size.x-top_pan.size_x+80;
	if(mouse_mode==0)
	{
		str_cpy(bottom_text.string[0], "click right mouse button to get a mouse cursor");
	}else
	{		
		str_cpy(bottom_text.string[0], "click right mouse button to move the camera");
	}
	while(mouse_mode==1)
	{
		mouse_pointer=2;
		mouse_pos.x = pointer.x;//mouse_cursor.x;
		mouse_pos.y = pointer.y;//mouse_cursor.y;
		if(crystal_facetAll>0.5)
		{
			crystal_facetAll=1;
		}else
		{
			crystal_facetAll=0;
		}
		//update the effect parameters
		mat_crystal.skill1=float(crystal_refractiv);			//vecSkill1
		mat_crystal.skill2=float(crystal_facet);
		mat_crystal.skill3=float(crystal_facetAll);
		mat_crystal.ambient_red=crystal_colorred;
		mat_crystal.ambient_green=crystal_colorgreen;
		mat_crystal.ambient_blue=crystal_colorblue;		
		wait(1);
	}
	mouse_pointer=0;
}

on_mouse_right = mouse_movement;