//==========================================================================================
//
//	the_shrine_beta3.gm
//
//	Who           	When		What
//------------------------------------------------------------------------------------------
//	^1[13th]^4Thunder	24 April 2014	Initial Script
//
//==========================================================================================
//

global Map =
{
	Debug = 1, // please set to zero before distributing your script
	ShowMovers = false,

	Neutral_Command_Post_Dyno = 0,
	Pyramid_gate_Dyno = 0,
	Tank_Barrier_1_Dyno = 0,
	Tank_Barrier_2_Dyno = 0,
	Tank_Barrier_2 = true,
	Tank_Barrier_1 = true,
	Old_City_Wall = true,
	Allied_CP = false,
	Axis_CP = false,
	Pyramid_gate = false,
	truck = true,
	spawnflagpyramid_Axis = false,
	spawnflagpyramid_Allies = true,
	Shrine_Taken = false,
	Door_Opened = true,

	Allied_CP_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Allied_CP = true;
		
		Util.MapDebugPrint( "Allied_CP_Built" );
	},
	
	Axis_CP_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Axis_CP = true;
		
		Util.MapDebugPrint( "Axis_CP_Built" );
	},
	
	Neutral_Command_Post_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		if ( Map.Allied_CP )
		{
			Util.MapDebugPrint( "Allied_CP_Planted" );
		}
		else
		{
			Util.MapDebugPrint( "Axis_CP_Planted" );
		}
	},

	Neutral_Command_Post_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		if ( Map.Allied_CP )
		{
			Util.MapDebugPrint( "Allied_CP_Defused" );
		}
		else
		{
			Util.MapDebugPrint( "Axis_CP_Defused" );
		}
	},

	Allied_CP_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Allied_CP = false;
		
		Util.MapDebugPrint( "Allied_CP_Destroyed" );
	},
	
	Axis_CP_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Axis_CP = false;
		
		Util.MapDebugPrint( "Axis_CP_Destroyed" );
	},

	Pyramid_gate_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Pyramid_gate = true;
		
		SetAvailableMapGoals( TEAM.ALLIES, false, "BUILD_Pyramid_gate" );
		
		SetAvailableMapGoals( TEAM.AXIS, false, "FLAG_shrine" );
		
		SetAvailableMapGoals( 0, false, "SWITCH_Door" );
		
		if ( !Map.Old_City_Wall )
		{
			SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_Pyramid_gate" );
		}
		
		Util.MapDebugPrint( "Pyramid_gate_Built" );
	},

	Pyramid_gate_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Pyramid_gate_Dyno += 1;
		Util.MapDebugPrint( "Pyramid_gate_Planted" );
	},

	Pyramid_gate_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Pyramid_gate_Dyno -= 1;
		Util.MapDebugPrint( "Pyramid_gate_Defused" );
	},

	Pyramid_gate_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Pyramid_gate_Dyno = 0;
		Map.Pyramid_gate = false;
		
		SetAvailableMapGoals( TEAM.AXIS, false, "PLANT_Pyramid_gate" );
		
		if ( !Map.Old_City_Wall )
		{
			if ( !Map.Shrine_Taken )
			{
				SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Pyramid_gate" );
				
				if ( Map.Door_Opened )
				{
					SetAvailableMapGoals( TEAM.AXIS, true, "FLAG_shrine" );
					
					SetAvailableMapGoals( TEAM.ALLIES, true, "SWITCH_Door" );
				}
				else
				{
					SetAvailableMapGoals( TEAM.AXIS, true, "SWITCH_Door" );
				}
			}
		}
		
		Util.MapDebugPrint( "Pyramid_gate_Destroyed" );
	},

	Tank_Barrier_1_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }
		
		Util.MapDebugPrint( "Tank_Barrier_1_Built" );
	},

	Tank_Barrier_1_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Tank_Barrier_1_Dyno += 1;
		Util.MapDebugPrint( "Tank_Barrier_1_Planted" );
	},

	Tank_Barrier_1_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Tank_Barrier_1_Dyno -= 1;
		Util.MapDebugPrint( "Tank_Barrier_1_Defused" );
	},

	Tank_Barrier_1_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Tank_Barrier_1_Dyno = 0;

		Util.MapDebugPrint( "Tank_Barrier_1_Destroyed" );
	},

	Tank_Barrier_2_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "Tank_Barrier_2_Built" );
	},

	Tank_Barrier_2_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Tank_Barrier_2_Dyno += 1;
		Util.MapDebugPrint( "Tank_Barrier_2_Planted" );
	},

	Tank_Barrier_2_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Tank_Barrier_2_Dyno -= 1;
		Util.MapDebugPrint( "Tank_Barrier_2_Defused" );
	},

	Tank_Barrier_2_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Tank_Barrier_2_Dyno = 0;
		
		Util.MapDebugPrint( "Tank_Barrier_2_Destroyed" );
	},
	
	truck_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.truck = true;
		
		Util.MapDebugPrint( "truck_Built" );
	},
	
	truck_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.truck = false;
		
		Util.MapDebugPrint( "truck_Destroyed" );
	},

	Old_City_Wall_Planted = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "Old_City_Wall_Planted" );
	},
	
	Old_City_Wall_Defused = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "Old_City_Wall_Defused" );
	},
	
	Old_City_Wall_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Old_City_Wall = false;
		
		SetAvailableMapGoals( 0, true, "CHECKPOINT_spawnflagpyramid" );
		
		SetAvailableMapGoals( TEAM.AXIS, true, "HEALTHCAB_second_healthcabinet" );
		SetAvailableMapGoals( TEAM.AXIS, true, "AMMOCAB_second_ammocabinet" );
		SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_Neutral_Command_Post" );
		SetAvailableMapGoals( TEAM.AXIS, true, "BUILD_Neutral_Command_Post" );
		SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_Tank_Barrier_1" );
		SetAvailableMapGoals( TEAM.AXIS, true, "CAPPOINT_Truck" );
		
		if ( Map.Pyramid_gate )
		{
			SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_Pyramid_gate" );
		}
		else
		{
			if ( Map.Door_Opened )
			{
				SetAvailableMapGoals( TEAM.AXIS, true, "FLAG_shrine" );
				
				SetAvailableMapGoals( TEAM.ALLIES, true, "SWITCH_Door" );
			}
			else
			{
				SetAvailableMapGoals( TEAM.AXIS, true, "SWITCH_Door" );
			}
		}
		
		
		
		Util.MapDebugPrint( "Old_City_Wall_Destroyed" );
	},

	shrine_Taken = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Shrine_Taken = true;
		
		SetAvailableMapGoals( 0, false, "SWITCH_Door" );
		
		Util.MapDebugPrint( "shrine_Taken" );
	},

	shrine_Returned = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.Shrine_Taken = false;
		
		if ( !Map.Pyramid_gate )
		{
			if ( Map.Door_Opened )
			{
				SetAvailableMapGoals( TEAM.ALLIES, true, "SWITCH_Door" );
			}
			else
			{
				SetAvailableMapGoals( TEAM.AXIS, true, "SWITCH_Door" );
			}
			
		}
		
		Util.MapDebugPrint( "shrine_Returned" );
	},

	shrine_Secured = function( trigger )
	{
		if ( TestMap )
			{ return; }
		
		SetAvailableMapGoals( TEAM.ALLIES, false, "BUILD_Pyramid_gate" );
		
		SetAvailableMapGoals( TEAM.AXIS, false, "FLAG_shrine" );
		SetAvailableMapGoals( TEAM.AXIS, false, "PLANT_Pyramid_gate" );
		
		SetAvailableMapGoals( 0, false, "SWITCH_Door" );
		
		SetAvailableMapGoals( TEAM.AXIS, true, "ESCORT_truck" );
		SetAvailableMapGoals( TEAM.AXIS, true, "BUILD_truck" );
		
		SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Tank_Barrier_2" );
		
		SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_Tank_Barrier_2" );
		
		Util.MapDebugPrint( "shrine_Secured" );
	},

	spawnflagpyramid_Axis_Captured = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.spawnflagpyramid_Axis = true;
		Map.spawnflagpyramid_Allies = false;
		
		Util.MapDebugPrint( "spawnflagpyramid_Axis_Captured" );
	},

	spawnflagpyramid_Allies_Captured = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.spawnflagpyramid_Axis = false;
		Map.spawnflagpyramid_Allies = true;
		
		Util.MapDebugPrint( "spawnflagpyramid_Allies_Captured" );
	},
	
	Truck_Pass_First_Barrier = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "Truck_Pass_First_Barrier" );
	},
	
	Truck_Pass_Second_Barrier = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "Truck_Pass_Second_Barrier" );
	},
	
	Marketplace_Reached = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "Marketplace_Reached" );
	},
	
	Axis_Win = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "Axis_Win" );
	},
	
	testTraceLineThread1 = function()
	{
		start = Vector3(1363.319, -137.875, 325.125);
		end = Vector3(1363.319, -238.125, 325.125);
		print("^5Second Barrier thread started");
		while (1)
		{
			tr = TraceLine(start, end, null, TRACE.SOLID, 0, false);
			if(tr.fraction < 1)
			{	
				if ( !Map.Tank_Barrier_2 )
				{
					Map.Tank_Barrier_2_Built();
				}
				
				Map.Tank_Barrier_2 = true;
			}
			else
			{
				if ( Map.Tank_Barrier_2 )
				{
					Map.Tank_Barrier_2_Destroyed();
				}
				
				Map.Tank_Barrier_2 = false;
			}
			yield();
		}
	},
	
	testTraceLineThread = function()
	{
		start = Vector3(-771.551, 1475.084, 325.125);
		end = Vector3(-857.904, 1529.283, 325.125);
		print("^5First Barrier thread started");
		while (1)
		{
			tr = TraceLine(start, end, null, TRACE.SOLID, 0, false);
			if(tr.fraction < 1)
			{	
				if ( !Map.Tank_Barrier_1 )
				{
					Map.Tank_Barrier_1_Built();
				}
				
				Map.Tank_Barrier_1 = true;
			}
			else
			{
				if ( Map.Tank_Barrier_1 )
				{
					Map.Tank_Barrier_1_Destroyed();
				}
				
				Map.Tank_Barrier_1 = false;
			}
			yield();
		}
	},
	
	door = function()
	{
		start = Vector3(-1756.467, 2637.875, 336.125);
		end = Vector3(-1748.042, 2682.125, 336.125);
		print("^5Door thread started");
		while (1)
		{
			tr = TraceLine(start, end, null, TRACE.SOLID, 0, false);
			if(tr.fraction < 1)
			{	
				if ( Map.Door_Opened )
				{
					print("Door_Closed");
					
					if ( !Map.Old_City_Wall and !Map.Shrine_Taken and !Map.Pyramid_gate )
					{
						SetAvailableMapGoals( TEAM.ALLIES, false, "SWITCH_Door" );
						
						SetAvailableMapGoals( TEAM.AXIS, true, "SWITCH_Door" );
						SetAvailableMapGoals( TEAM.AXIS, false, "FLAG_shrine" );
					}
					
					Map.Door_Opened = false;
				}
			}
			else
			{
				if ( !Map.Door_Opened )
				{
					print("Door_Opened");
					
					if ( !Map.Old_City_Wall and !Map.Shrine_Taken and !Map.Pyramid_gate )
					{
						SetAvailableMapGoals( TEAM.ALLIES, true, "SWITCH_Door" );
						
						SetAvailableMapGoals( TEAM.AXIS, false, "SWITCH_Door" );
						SetAvailableMapGoals( TEAM.AXIS, true, "FLAG_shrine" );
					}
					
					Map.Door_Opened = true;
				}
			}
			yield();
		}
	},

};

global OnMapLoad = function()
{
	if ( TestMapOn )
		{ Util.AutoTestMap(); }

	// Register callback functions
	OnTrigger( "Allied Command Post constructed. Charge speed increased!", Map.Allied_CP_Built );
	OnTrigger( "Axis team has destroyed the Allied Command Post!", Map.Allied_CP_Destroyed );
	OnTrigger( "Axis Command Post constructed. Charge speed increased!", Map.Axis_CP_Built );
	OnTrigger( "Allied team has destroyed the Axis Command Post!", Map.Axis_CP_Destroyed );
	
	OnTrigger( "Allied Team have built the pyramid gate!", Map.Pyramid_gate_Built );
	OnTrigger( "The truck has been repaired!", Map.truck_Built );
	
	OnTrigger( "Planted at the Neutral Command Post.", Map.Neutral_Command_Post_Planted );
	OnTrigger( "Planted at Pyramid gate.", Map.Pyramid_gate_Planted );
	OnTrigger( "Planted at Tank Barrier #1.", Map.Tank_Barrier_1_Planted );
	OnTrigger( "Planted at Tank Barrier #2.", Map.Tank_Barrier_2_Planted );
	
	OnTrigger( "Defused at the Neutral Command Post.", Map.Neutral_Command_Post_Defused );
	OnTrigger( "Defused at Pyramid gate.", Map.Pyramid_gate_Defused );
	OnTrigger( "Defused at Tank Barrier #1.", Map.Tank_Barrier_1_Defused );
	OnTrigger( "Defused at Tank Barrier #2.", Map.Tank_Barrier_2_Defused );
	
	OnTrigger( "Axis have destroyed the pyramid gate!", Map.Pyramid_gate_Destroyed );
	OnTrigger( "Planted at old city wall.", Map.Old_City_Wall_Planted );
	OnTrigger( "Defused at old city wall.", Map.Old_City_Wall_Defused );
	OnTrigger( "^1Axis have destroyed the old city wall!", Map.Old_City_Wall_Destroyed );
	
	OnTrigger( "Axis have stolen The Shrine!", Map.shrine_Taken );
	OnTrigger( "Allies have returned The Shrine!", Map.shrine_Returned );
	OnTrigger( "Axis have loaded the Shrine on to truck", Map.shrine_Secured );
	
	OnTrigger( "^1the pyramid spawn is captured by axsis.", Map.spawnflagpyramid_Axis_Captured );
	OnTrigger( "^4the pyramid spawn is captured by Allies..", Map.spawnflagpyramid_Allies_Captured );
	
	OnTrigger( "^1 The truck have passed the first barrier", Map.Truck_Pass_First_Barrier );
	OnTrigger( "^1 the truck have passed the second barrier", Map.Truck_Pass_Second_Barrier );
	OnTrigger( "^1AXIS ^4have reach the marketplace!", Map.Marketplace_Reached );
	OnTrigger( "^!Axis ^4have successfully brought ^8The shrine ^4to the boat and a safe retour", Map.Axis_Win );
	
	Util.DisableGoal( ".*", true ); // all but routes
	
	thread(Map.testTraceLineThread1);
	thread(Map.testTraceLineThread);
	thread(Map.door);
	
	SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_old_city_wall" );
	SetAvailableMapGoals( TEAM.AXIS, true, "HEALTHCAB_first_healthcabinet" );
	SetAvailableMapGoals( TEAM.AXIS, true, "AMMOCAB_first_ammocabinet" );
	
	SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Neutral_Command_Post" );
	SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Pyramid_gate" );
	SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Tank_Barrier_1" );
	SetAvailableMapGoals( TEAM.ALLIES, true, "HEALTHCAB_second_healthcabinet" );
	SetAvailableMapGoals( TEAM.ALLIES, true, "AMMOCAB_second_ammocabinet" );

	// Max users per goal
	//Util.SetMaxUsers( 1, "DEFEND_.*" );
	//Util.SetMaxUsers( 1, "GRENADE_.*" );
	//Util.SetMaxUsers( 1, "MOUNTMG42_.*" );

	Util.AddUseWp( "PLANT_old_city_wall", "wall" );


	Util.MapDebugPrint( "Omni-bot map script for " + GetMapName() + " executed." );
};

global OnBotJoin = function( bot )
{
	// Uncomment for shootable breakables
	//~bot.TargetBreakableDist = 90.0;
	// Only set MaxViewDistance on maps with limited sight (e.g. fog)
	//~bot.MaxViewDistance = 2400;
};

global InitializeRoutes = function()
{
	MapRoutes =
	{
		FLAG_shrine =
		{
		},
		BUILD_Pyramid_gate =
		{
		},
		PLANT_Pyramid_gate =
		{
		},
		BUILD_truck =
		{
		},
		BUILD_Tank_Barrier_1 =
		{
		},
		PLANT_Tank_Barrier_1 =
		{
		},
		BUILD_Tank_Barrier_2 =
		{
		},
		PLANT_Tank_Barrier_2 =
		{
		},
		CHECKPOINT_spawnflagpyramid =
		{
		},
		BUILD_Neutral_Command_Post =
		{
		},
		PLANT_Neutral_Command_Post =
		{
		},
	};
	Util.Routes(MapRoutes);
};
