#using scripts\codescripts\struct;

#using scripts\shared\util_shared;
#using scripts\shared\compass;
#using scripts\shared\exploder_shared;
#using scripts\shared\scene_shared;
#using scripts\shared\flag_shared;
#using scripts\shared\array_shared;
#using scripts\shared\clientfield_shared;

    	   	                                                                                                                                         	                                	                                                                                                                                                                                                                                                                                                                                                                    	        	     	              	    	   	               	                                              	                                                           	                               	                     	                                                                                                           	                                                                 	                                                              	                                                                                                                                             	                            	                                     	                                       	                                                                                                              	   	                  	       	                                                    	                   	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            	                                                                                                 	                                    	                                                                                        	           	                        	                                            	                                             	                                                   	                                                             	                                                         	                                                                    	                                                                                                                                                                                                                                	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  	    	         	                                                              	                                                          	                                   	                                   	                                                    	                                       	                                           	     	                                                                                                                                                                                                                                                      	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	                                                               	        	                                                                                                                              	                                                                                                                     	                               	                                                      	                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	   	                                                                                                                                                                                                                                  	                                                                                                                                                                                                                                                                                                                                                                                                                                                   

#using scripts\mp\_load;
#using scripts\mp\_util;

#using scripts\mp\mp_city_fx;
#using scripts\mp\mp_city_sound;
                   

#using scripts\mp\_vehicle;

#using scripts\mp\vehicles\_quadtank;
#using scripts\mp\vehicles\_siegebot;
#using scripts\mp\vehicles\_siegebot_theia;

	//Wait time between start and the init going
	//Wait time between init playing and main playing
	




	





	









// values grabbed from mp_city on 2016-08-29










	

	
#precache( "fx", "vehicle/fx_siegebot_spawn_teleport" );
#precache( "fx", "vehicle/fx_siegebot_spawn_teleport_light_green" );
#precache( "fx", "vehicle/fx_siegebot_spawn_teleport_light_yellow" );
#precache( "fx", "vehicle/fx_siegebot_spawn_teleport_light_red" );


#precache( "model", "collision_clip_128x128x128" );
#precache( "model", "collision_clip_wall_128x128x10" );
#precache( "model", "collision_clip_64x64x256" );
#precache( "model", "collision_physics_32x32x32" );
#precache( "model", "collision_physics_64x64x64" );
#precache( "model", "collision_physics_128x128x128" );
#precache( "model", "collision_clip_wall_64x64x10" );
#precache( "model", "p7_out_modular_ac_unit_01" );
#precache( "model", "p7_ntx_dom_cover_crates" );

function main()
{
	clientfield::register( "scriptmover", "ring_state", 15000, 2, "int" );
	
	// fix for siegebot pushing players through geo
	push_out_threshold = GetDvarFloat( "tu16_physicsPushOutThreshold", -1.0 );
	if ( push_out_threshold != -1.0 )
		SetDvar( "tu16_physicsPushOutThreshold", 10.0 );

	precache();
	
	level.vehicle_about_to_spawn = &vehicle_about_to_spawn;
	level.vehicle_spawner_init = &vehicle_spawner_init;
	level.vehicle_destroyed = &vehicle_destroyed;
	level.vehicle_spawners_init_finished = &vehicle_spawners_init_finished;
	level.allow_vehicle_challenge_check = true;
	
	setup_vehicle_spawner_mesh_info();
	
	level flag::init( "city_vortex_sequence_playing" );
	
	mp_city_fx::main();
	mp_city_sound::main();

	load::main();

	compass::setupMiniMap( "compass_map_mp_city" );	

	SetDvar( "compassmaxrange", "2100" );	// Set up the default range of the compass

        // Spawn ac unit models and collision on science bldg 01 to block walker
        // block1 = spawn("script_model",( -2870 , -2600 , -1792 ));
  	// block1.angles = ( 270 , 270 , -90 );
        // block1 SetModel("p7_out_modular_ac_unit_01");

        // block2 = spawn("script_model",( -2924 , -2495 , -1792 ));
  	// block2.angles = ( 270 , 90 , 0 );
        // block2 SetModel("p7_out_modular_ac_unit_01");

        // block3 = spawn("script_model",( -2957 , -2600 , -1792 ));
  	// block3.angles = ( 270 , 270 , -90 );
        // block3 SetModel("p7_out_modular_ac_unit_01");

	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2892 , -2560 , -1746 ), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_128x128x128", "collider", ( -2892 , -2560 , -1728 ), ( 0, 0, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2892 , -2560 , -1618 ), ( 0, 0, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2892 , -2560 , -1490 ), ( 0, 0, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2892 , -2560 , -1362 ), ( 0, 0, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2892 , -2560 , -1234 ), ( 0, 0, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2892 , -2560 , -1106 ), ( 0, 0, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2892 , -2560 , -978 ), ( 0, 0, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2892 , -2560 , -850 ), ( 0, 0, 0 ));

        // Spawn ac unit models and collision on science bldg 04 to block walker
        // block1 = spawn("script_model",( -2510 , -1935 , -1765 ));
  	// block1.angles = ( 270 , 270 , 60 );
        // block1 SetModel("p7_out_modular_ac_unit_01");

	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2581 , -1931 , -1719 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_physics_128x128x128", "collider", ( -2581 , -1931 , -1701 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2581 , -1931 , -1591 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2581 , -1931 , -1463 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2581 , -1931 , -1335 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2581 , -1931 , -1207 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2581 , -1931 , -1079 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2581 , -1931 , -951 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2581 , -1931 , -823 ), ( 0, 240, 0 ));

        // block1 = spawn("script_model",( -2209 , -1477 , -1733 ));
  	// block1.angles = ( 270 , 90 , -30 );
        // block1 SetModel("p7_out_modular_ac_unit_01");

        // block2 = spawn("script_model",( -2092 , -1484 , -1733 ));
  	// block2.angles = ( 270 , 270 , 60 );
        // block2 SetModel("p7_out_modular_ac_unit_01");

        // block3 = spawn("script_model",( -2167 , -1404 , -1733 ));
  	// block3.angles = ( 270 , 90 , -30 );
        // block3 SetModel("p7_out_modular_ac_unit_01");

	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2164 , -1479 , -1687 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_physics_128x128x128", "collider", ( -2164 , -1479 , -1669 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2164 , -1479 , -1559 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2164 , -1479 , -1431 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2164 , -1479 , -1303 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2164 , -1479 , -1175 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2164 , -1479 , -1047 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2164 , -1479 , -919 ), ( 0, 240, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2164 , -1479 , -791 ), ( 0, 240, 0 ));

        // block1 = spawn("script_model",( -2702 , -966 , -1765 ));
  	// block1.angles = ( 270 , 90 , 60 );
        // block1 SetModel("p7_out_modular_ac_unit_01");

        // block2 = spawn("script_model",( -2695 , -849 , -1765 ));
  	// block2.angles = ( 270 , 90 , -30 );
        // block2 SetModel("p7_out_modular_ac_unit_01");

        // block3 = spawn("script_model",( -2775 , -924 , -1765 ));
  	// block3.angles = ( 270 , 90 , 60 );
        // block3 SetModel("p7_out_modular_ac_unit_01");

	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2692 , -907 , -1719 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_physics_128x128x128", "collider", ( -2692 , -907 , -1701 ), ( 0, 330, 0 ));

	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2692 , -907 , -1591 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2692 , -907 , -1463 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2692 , -907 , -1335 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2692 , -907 , -1207 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2692 , -907 , -1079 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2692 , -907 , -951 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2692 , -907 , -823 ), ( 0, 330, 0 ));

        // SpawnCollision("collision_clip_128x128x128", "collider", ( -2796 , -847 , -1719 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2796 , -847 , -1591 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2796 , -847 , -1463 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2796 , -847 , -1335 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2796 , -847 , -1207 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2796 , -847 , -1079 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2796 , -847 , -951 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2796 , -847 , -823 ), ( 0, 330, 0 ));

        // block1 = spawn("script_model",( -2910 , -846 , -1765 ));
  	// block1.angles = ( 270 , 90 , 60 );
        // block1 SetModel("p7_out_modular_ac_unit_01");

        // block2 = spawn("script_model",( -2903 , -729 , -1765 ));
  	// block2.angles = ( 270 , 90 , -30 );
        // block2 SetModel("p7_out_modular_ac_unit_01");

        // block3 = spawn("script_model",( -2983 , -804 , -1765 ));
  	// block3.angles = ( 270 , 90 , 60 );
        // block3 SetModel("p7_out_modular_ac_unit_01");

	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2900 , -787 , -1719 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_physics_128x128x128", "collider", ( -2900 , -787 , -1701 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2900 , -787 , -1591 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2900 , -787 , -1463 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2900 , -787 , -1335 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2900 , -787 , -1207 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2900 , -787 , -1079 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2900 , -787 , -951 ), ( 0, 330, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -2900 , -787 , -823 ), ( 0, 330, 0 ));

        // block1 = spawn("script_model",( -3240 , -1011 , -1750 ));
  	// block1.angles = ( 0 , 330 , 180 );
        // block1 SetModel("p7_ntx_dom_cover_crates");

        // block2 = spawn("script_model",( -3286 , -1091 , -1750 ));
  	// block2.angles = ( 0 , 330 , 180 );
        // block2 SetModel("p7_ntx_dom_cover_crates");

	// SpawnCollision("collision_clip_128x128x128", "collider", ( -3209 , -1070 , -1740 ), ( 0, 60, 0 ));
	// SpawnCollision("collision_physics_128x128x128", "collider", ( -3209 , -1070 , -1720 ), ( 0, 60, 0 ));

	// SpawnCollision("collision_clip_128x128x128", "collider", ( -3209 , -1070 , -1612 ), ( 0, 60, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -3209 , -1070 , -1484 ), ( 0, 60, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -3209 , -1070 , -1356 ), ( 0, 60, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -3209 , -1070 , -1228 ), ( 0, 60, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -3209 , -1070 , -1100 ), ( 0, 60, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -3209 , -1070 , -972 ), ( 0, 60, 0 ));
	// SpawnCollision("collision_clip_128x128x128", "collider", ( -3209 , -1070 , -844 ), ( 0, 60, 0 ));

	// Missing Phys Clip on crates on Blue building 04
	// SpawnCollision("collision_physics_32x32x32", "collider", ( -1936 , 1286 , -1508), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_32x32x32", "collider", ( -1968 , 1286 , -1508), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_32x32x32", "collider", ( -1970 , 1058 , -1508), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_32x32x32", "collider", ( -1938 , 1058 , -1508), ( 0, 0, 0 ));

	// Missing Phys Clip on rotating canisters in Monorail Station3
	// SpawnCollision("collision_physics_64x64x64", "collider", ( 230 , -670 , -1546), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_64x64x64", "collider", ( 230 , -720 , -1546), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_64x64x64", "collider", ( 230 , -670 , -1504), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_64x64x64", "collider", ( 230 , -720 , -1504), ( 0, 0, 0 ));
	
	// SpawnCollision("collision_physics_64x64x64", "collider", ( 230 , -464 , -1546), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_64x64x64", "collider", ( 230 , -424 , -1546), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_64x64x64", "collider", ( 230 , -464 , -1504), ( 0, 0, 0 ));
	// SpawnCollision("collision_physics_64x64x64", "collider", ( 230 , -424 , -1504), ( 0, 0, 0 ));
	
	// Spawning clip near science building crates DT 181677
	// SpawnCollision("collision_clip_wall_128x128x10", "collider", ( -2716.5 , -1138.5 , -1828.5), ( 0, 329, 0 ));
	// SpawnCollision("collision_clip_wall_128x128x10", "collider", ( -2716.5 , -1138.5 , -1703), ( 0, 329, 0 ));
	
	// Spawning clip beside monorail3 window to prevent clipping through geo (DT 181678)
	// SpawnCollision("collision_clip_wall_64x64x10", "collider", ( 722 , -386.5 , -1591.5 ), ( 0, 270, 0 ));
	// SpawnCollision("collision_clip_wall_64x64x10", "collider", ( 722 , -386.5 , -1532 ), ( 0, 270, 0 ));
	// SpawnCollision("collision_clip_wall_64x64x10", "collider", ( 722 , -386.5 , -1491 ), ( 0, 270, 0 ));

	// DT #181782 Spawning clip to prevent mantle bug into model geo
	// SpawnCollision("collision_clip_wall_128x128x10", "collider", ( -1980 , 334 , -1704 ), ( 0, 90, 0 ));
	// SpawnCollision("collision_clip_wall_128x128x10", "collider", ( -1986 , 334 , -1704 ), ( 0, 90, 0 ));

	// DT #181756 Spawning clip on monorail3 outer wall to prevent mantle bug into wall geo
	// SpawnCollision("collision_clip_64x64x256", "collider", ( 192 , -744 , -1352 ), ( 0, 0, 0 ));

	// Spawning clip on monorail3 inner wall to prevent exploitable spot
	// SpawnCollision("collision_clip_wall_128x128x10", "collider", ( 100 , -733 , -1288 ), ( 0, 270, 0 ));


	
	// Stockpile hub points
	level.cleanDepositPoints = Array ( ( -137.668 , 68.4889 , -1567.88 ),
                                  ( 1252.24 , -1316.22 , -1688.46 ),
                                  ( 1150.05 , 2627.64 , -1311.88 ),
                                  ( -1944.21 , -742.548 , -1767.88 ) );
	
	vehicle::InitVehicleMap();

	level city_vortex_init();
	level thread city_vortex_think();
}

function precache()
{
	// DO ALL PRECACHING HERE
}

function setup_vehicle_spawner_mesh_info()
{
	level.teleport_pad_meshes = [];
	
	for ( i = 0; i < 4; i++ )
		level.teleport_pad_meshes[i] = SpawnStruct();
	
	level.teleport_pad_meshes[0].origin = ( -2468.5 , -2125.5 , -1901 );
	level.teleport_pad_meshes[0].angles = ( 359.4 , 309.5 , 3.5 );

	level.teleport_pad_meshes[1].origin = ( -582 , -2689 , -1580 );
	level.teleport_pad_meshes[1].angles = ( 0 , 0 , 0 );

	level.teleport_pad_meshes[2].origin = ( 3043.14 , 713.521 , -1393.5 );
	level.teleport_pad_meshes[2].angles = ( 0 , 270 , 0 );

	level.teleport_pad_meshes[3].origin = ( 1129 , 3182 , -1310 );
	level.teleport_pad_meshes[3].angles = ( 0 , 0 , 0 );	
}

function find_teleport_mesh_index( origin )
{
	for ( i = 0; i < 4; i++ )
	{
		if ( DistanceSquared( origin, level.teleport_pad_meshes[ i ].origin ) < ( (24) * (24) ) )
			return i;
	}
	
	return 0; // failsafe but bad
}

function city_vortex_init()
{
	scene::add_scene_func( "p7_fxanim_mp_city_vista_energy_ball_bundle", &city_vortex_advancer, "done" );
	
	level thread city_vortex_init_rings();
	
	/#
	mapname = GetDvarString( "mapname" );
	AddDebugCommand( "devgui_cmd \"|" + mapname + "|/Debug Vortex Sequence/Start Sequence\" \"set level_notify city_vortex_sequence_start \"\n" );
	AddDebugCommand( "devgui_cmd \"|" + mapname + "|/Debug Vortex Sequence/Reset Sequence\" \"set level_notify city_vortex_sequence_reset \"\n" );
	#/
}

function set_all_ring_state( state )
{
	foreach( ring in level.ringArray )
	{
		ring clientfield::set( "ring_state", state );
	}
}

function set_specific_ring_state( ringIndex, state )
{
	ringStringIndex = "" + ringIndex;
	rings = level.rings[ ringStringIndex ];
	foreach( ring in rings )
	{
		ring clientfield::set( "ring_state", state );
	}
}

function city_vortex_init_rings()
{
	level.ringArray = GetEntArray( "city_ring", "targetname" );
	level.rings = [];
	level.rings[ "1" ] = [];
	level.rings[ "2" ] = [];
	level.rings[ "3" ] = [];
	level.rings[ "4" ] = [];
	for( i = 0; i < level.ringArray.size; i++ )
	{
		index = level.ringArray[i].script_noteworthy;
		level.rings[ index ][ level.rings[ index ].size ] = level.ringArray[i];
	}
	set_all_ring_state( 3 );
}

function city_vortex_think()
{
	level thread city_vortex_launcher();
	level city_rings_watchers();
	level thread city_vortex_start();
	level thread city_vortex_reset();
}

function city_vortex_launcher()
{
	played = 0;
	while( played < 2 )
	{
		waittime = RandomIntRange( 120, 360 );
		wait( waittime );
		played++;
		level notify( "city_vortex_sequence_start" );
	}
}

function city_vortex_start()
{
	level scene::init( "p7_fxanim_mp_city_vista_energy_ball_bundle" );
	
	level endon ( "city_vortex_sequence_reset" );
	
	for ( ;; )
	{		
		level waittill( "city_vortex_sequence_start" );
		level flag::set( "city_vortex_sequence_playing" );
		
		level thread scene::play( "p7_fxanim_mp_city_vista_energy_ball_bundle" );
		
		level array::thread_all( level.a_e_pa_speakers_all, &mp_city_sound::play_alarm_sfx );
	}
}

function city_rings_watchers()
{
	level thread watch_ramp();
	level thread watch_on();
	level thread watch_flicker();
	level thread watch_off();
}

function watch_ramp()
{
	for( ;; )
	{
		level waittill ( "ring_ramp" );
		
		set_specific_ring_state( 1, 0 );
		wait( 1 );
		set_specific_ring_state( 2, 0 );
		wait( 1 );
		set_specific_ring_state( 3, 0 );
		wait( 1 );
		set_specific_ring_state( 4, 0 );
	}
}

function watch_on()
{
	for( ;; )
	{
		level waittill ( "ring_on" );
		set_all_ring_state( 1 );
	}
}

function watch_flicker()
{
	for( ;; )
	{
		level waittill ( "ring_flicker" );
		set_all_ring_state( 2 );
	}
}

function watch_off()
{
	for( ;; )
	{
		level waittill ( "ring_off" );
		set_all_ring_state( 3 );
	}
}

function city_vortex_advancer( ents )
{
	level thread scene::init( "p7_fxanim_mp_city_vista_energy_ball_bundle" );
}

function city_vortex_reset()
{
	for ( ;; )
	{	
		level waittill( "city_vortex_sequence_reset" );
		level flag::clear( "city_vortex_sequence_playing" );

		level thread city_vortex_start();
		set_all_ring_state( 3 );
	}
}

function vehicle_spawner_init( veh_spawner_id, veh_name, origin, angles )
{
	if(!isdefined(level.map_vehicle_spawners))level.map_vehicle_spawners=[];
	
	mesh_index = find_teleport_mesh_index( origin );
	mesh_origin = level.teleport_pad_meshes[ mesh_index ].origin;
	mesh_angles = level.teleport_pad_meshes[ mesh_index ].angles;
	
	forward = AnglesToForward( mesh_angles );
	up = AnglesToUp( mesh_angles );
	
	fx_spawner_info = SpawnStruct();
	// fx_spawner_info.fx_green = SpawnFx( SPAWNER_GREEN_FX, mesh_origin, forward, up );
	fx_spawner_info.fx_yellow = SpawnFx( "vehicle/fx_siegebot_spawn_teleport_light_yellow", mesh_origin, forward, up );
	fx_spawner_info.fx_red = SpawnFx( "vehicle/fx_siegebot_spawn_teleport_light_red", mesh_origin, forward, up );

	level.map_vehicle_spawners[ veh_spawner_id ] = fx_spawner_info;
	
	level thread trigger_spawner_fx( veh_spawner_id, 1.0 );
}

function vehicle_spawners_init_finished()
{
	// free up some script vars no longer needed

	wait 5.0;
	
	level.teleport_pad_meshes = [];
	level.vehicle_spawner_init = [];
	
	wait 1.0;
}

function trigger_spawner_fx( veh_spawner_id, delay )
{
	wait delay;
	
	// triggerfx( level.map_vehicle_spawners[ veh_spawner_id ].fx_green );
	triggerfx( level.map_vehicle_spawners[ veh_spawner_id ].fx_yellow );
	triggerfx( level.map_vehicle_spawners[ veh_spawner_id ].fx_red );
	
	wait 0.1;

	level.map_vehicle_spawners[ veh_spawner_id ].fx_yellow Hide();
	level.map_vehicle_spawners[ veh_spawner_id ].fx_red Hide();	
}

function vehicle_about_to_spawn( veh_spawner_id, veh_name, origin, angles, time_before_spawn )
{
	PlayFx( "vehicle/fx_siegebot_spawn_teleport", origin, AnglesToForward( angles ), AnglesToUp( angles ) );
	
	level.map_vehicle_spawners[ veh_spawner_id ].fx_red Hide();
	
	level thread vehicle_spawn_sequence( veh_spawner_id, veh_name, origin, angles, time_before_spawn );
}

function vehicle_spawn_sequence( veh_spawner_id, veh_name, origin, angles, time_before_spawn )
{
		remaining_time = float( time_before_spawn );
	
	yellow_fx = level.map_vehicle_spawners[ veh_spawner_id ].fx_yellow;
	while ( remaining_time >= 0.2 )
	{
		PlaySoundAtPosition( "evt_siegepad_yellow", level.map_vehicle_spawners[ veh_spawner_id ].fx_yellow.origin + ( 0, 0, 10 ) );

		yellow_fx Show();
		flash_wait = Max( remaining_time / 20.0, Min( 0.1, remaining_time ) );
		wait flash_wait;
		
		yellow_fx Hide();
		wait 0.1;
		
		remaining_time -= ( flash_wait + 0.1 );
	}
	
	if ( remaining_time > 0.0 )
		wait remaining_time;
	
	level.map_vehicle_spawners[ veh_spawner_id ].fx_yellow Hide();
	// level.map_vehicle_spawners[ veh_spawner_id ].fx_green Show();
	
	PlaySoundAtPosition( "evt_siegepad_green", origin + ( 0, 0, 10 ) );
}

function vehicle_destroyed( veh_spawner_id )
{
	level.map_vehicle_spawners[ veh_spawner_id ].fx_red Show();
	// level.map_vehicle_spawners[ veh_spawner_id ].fx_green Hide();

	PlaySoundAtPosition( "evt_siegepad_red", level.map_vehicle_spawners[ veh_spawner_id ].fx_red.origin + ( 0, 0, 10 ) );
}
