//Void Prototyps-----------------------



//-------------------------------------





void camera_movement(ENTITY* Editor)
{
		camera.x = Editor.x;
		camera.y = Editor.y;
		camera.z = Editor.z;
		
		if(mouse_right)
		{
			camera.tilt += mouse_force.y * time_step * 7;
			camera.pan -= mouse_force.x * time_step * 7 ;	
		}
		
		Editor.pan = camera.pan;
		Editor.tilt = camera.tilt; 
		if(!mouse_panel)
		{		
			c_move(Editor,vector(mickey.z*15*time_step,0,0),nullvector,IGNORE_WORLD|IGNORE_MODELS|IGNORE_MAPS);
		}
}

void mark_terrain()
{
	int Counter;
	ENTITY* dummy;
	for(Counter = 1;Counter <= 66049; Counter++)
	{
		CONTACT* c = ent_getvertex(newterrain,NULL,Counter);
		
		c.v = NULL;
		vec_set(dt_marker[Counter].pos.x, c.x);
		vec_set(dt_marker[Counter].pos.z, c.z);
		vec_set(dt_marker[Counter].pos.y, c.y);
		dt_marker[Counter].vertex_id = Counter;
		dt_marker[Counter].dist = 100000;
		dt_marker[Counter].normalize = 0;
		dt_marker[Counter].isactive = false;
		dt_marker[Counter].active = false;
	}
}




void edit_vertex(int vertexid)
{
	dt_marker[vertexid].isactive = true;
	while(dt_marker[vertexid].active == true && EditorModus == EDITOR_MODE_TERRAIN)
	{
				CONTACT* t = ent_getvertex(newterrain,NULL,vertexid);
				
				

				switch(Editor_Mode) // Deform Mode
				{
					case EDITOR_MODE_TERRAIN_ERODE:
						draw_point3d(vector(t.v.x,t.v.z,t.v.y), vector(0,255,0), 100, 15);
						if(mouse_left  && !mouse_panel)
						{
							
						
							if(key_shiftl)
							{
								
								if(t.v.y > 0)
								{
									t.v.y -= sinv(dt_marker[vertexid].normalize * 90) * EditSpeed * time_step;
								}
								else
								{
									t.v.y= 0;
								}
							}
							else
							{
								t.v.y += sinv(dt_marker[vertexid].normalize * 90) * EditSpeed * time_step;
							}
							
							
							ent_setvertex(newterrain,t,vertexid);
						}
						
						
						
							break;
						case EDITOR_MODE_TERRAIN_SMOOTH:
							draw_point3d(vector(t.v.x,t.v.z,t.v.y), vector(0,255,0), 100, 15);
							if(mouse_left && !mouse_panel)
							{
														
									var testhigh = (t.v.y + (t.v.y / 8)) / 2; 
									if(testhigh > t.v.y)
									{
										t.v.y += sinv(dt_marker[vertexid].normalize * 90) * EditSpeed * time_step;
									}
									else
									{
										t.v.y -= sinv(dt_marker[vertexid].normalize * 90) * EditSpeed * time_step;
									}
									ent_setvertex(newterrain,t,vertexid);
								
							
							}
							
						
						
						
						
						break;
					case EDITOR_MODE_TERRAIN_SETHIGH:
						draw_point3d(vector(t.v.x,t.v.z,t.v.y), vector(0,255,0), 100, 15);
						if(mouse_left && !mouse_panel)
						{
							t.v.y = terrain_high;
							ent_setvertex(newterrain,t,vertexid);
						}
						
						break;
					default:
						break;
					}
					
					wait(1);
					}

					
		
		

	dt_marker[vertexid].isactive = false;
}



void terrain_test_edit()
{
	int i = 1;
	if (ent_buffers(newterrain,0,0,&vbuffer,NULL,NULL) == 0)
	{
		error("vertex fehler");	
	}	
	while(1)
	{
		
		if(dt_marker[i].dist < range && dt_marker[i].isactive == false )
		{
			dt_marker[i].active = true;
			edit_vertex(i);
		}
	
		
		if(i > 66049)
		{
			i = 1;
		}
		else
		{
			i++;
		}
		
		wait(1);
	}
	
	
	

}




	




void dtmarker()
{
	int i;
	for(i = 1; i<= 66049; i++)
			{
				dt_marker[i].dist = vec_dist(vector(dt_marker[i].pos.x,dt_marker[i].pos.y,0), vector(edit_ball.x,edit_ball.y,0));
				if(dt_marker[i].dist < range)
				{
					dt_marker[i].normalize = (range - dt_marker[i].dist) / range;
					dt_marker[i].active = true;
					if(dt_marker[i].isactive == false)
					{
						edit_vertex(i);
					}
					
				}
				else
				{
					dt_marker[i].active = false;
				}
			}
}


void draw_bbox(ENTITY* temp)
{
  me = temp;
  var vMin[3];
  var vMax[3];  
 
    vec_set(vMin,my.x);
    vec_add(vMin,my.min_x);    
    vec_set(vMax,my.x);
    vec_add(vMax,my.max_x);    
    draw_box3d(vMin,vMax,vector(0,255,255),100);   
   
}



void main_schleife()
{
	EditSpeed  = 2;
	mouse_range = 10000000;
	var current_vertex = 1;
	
	var TR_EDIT_RANGE = 1;
	var TR_Range_WERT = 8;
	VECTOR* DT_TEMP = vector(0,0,0);
	
	my_player = ent_create("earth.mdl",vector(-7500,5120,5710),NULL);
	
	edit_ball = ent_create("earth.mdl",vector(5120,5120,2000),NULL);
	set (my_player, INVISIBLE);
	set (edit_ball, INVISIBLE);
	mouse_mode = 4;
	
	mark_terrain();
	
	while(1)
	{
		
		scala_object = (range / 1000) *100;
		edit_ball.scale_x = scala_object;
		edit_ball.scale_y = scala_object;
		edit_ball.scale_z = scala_object;
		
		dtmarker();
		camera_movement(my_player);
		
		if(hit)
		{
			DT_TEMP.x = mouse_pos.x;
			DT_TEMP.y = mouse_pos.y;				
			DT_TEMP.z = 10000000;
				
			vec_for_screen (DT_TEMP,camera);
			c_trace (camera.x,DT_TEMP,IGNORE_ME | IGNORE_PASSABLE | SCAN_TEXTURE|IGNORE_MODELS);
			
			
			
			switch(EditorModus) // Edit Mode zb. Terrain Deform oder Object Edit
			{
			
				case EDITOR_MODE_TERRAIN: //Terrain Deform
						set (edit_ball, INVISIBLE);
						edit_ball.x = hit.x;
						edit_ball.y = hit.y;
						edit_ball.z = hit.z;
						
						
				break;
				case EDITOR_MODE_OBJECTS: //Object Edit
				
								if(mouse_ent && !mouse_panel)
								{
									
									if(mouse_ent.skill21)
									{
										draw_bbox(mouse_ent);
									}
									
									
									if(mouse_left && button_e_over == 0)
									{
										LBG_listbox_set_marked(listbox1,mouse_ent.skill21);
										OBJ_Selected();
										while(mouse_left){wait(1);}
										
									}
								}
//								
								
								
				break;
				case EDITOR_MODE_PED: // Ped Generator
						set (edit_ball, INVISIBLE);
						if(!aas_window)
						GUI_PED_MAP_EDIT();
						
				break;
				case EDITOR_MODE_MONSTER: // Monster Edit
						set (edit_ball, INVISIBLE);
				break;
				case EDITOR_MODE_TEXTUR: // Textur Modus
						set (edit_ball, INVISIBLE);
				
				break;
			
			}
			
			
			
			
		}
		
		
		
		
		
		wait(1);
	}	
}