//==========================================================================================
//
//	lp1_2.gm
//
//	Who		When			What
//------------------------------------------------------------------------------------------
//	Advieszeur	Unknown			Created
//	FRY		16 Februari 2008	Tweaking and stuckage fixing
//	MickyP		02 December 2010	Full 0.81 Update
//
//==========================================================================================
//

global Map =
{
	Debug = 0,

	Movers =
	{
	"MOVER_hc_car",
	},

	BombBuild = false,
	GateBuild = false,
	Start = true,
	CartAtGate = false,

 	Switches =
	{
		openbox =
		{
		Enabled = true,
		Priority = 0,
		WaypointName = "wirebox",
		LimitTeam = (1<<TEAM.ALLIES),
		},
	},

	Fuse_Panel_Open = function( trigger )
	{
		Map.Switches.openbox.Enabled = false;

		//Util.MapDebugPrint( "Fuse_Panel_Open" );
	},

	Fuse_Box_Built = function( trigger )
	{
		Util.EnableGoal( "ROUTE_Fuse_Door" );
		SetAvailableMapGoals(TEAM.ALLIES, false, "BUILD_Fuse_Box");
		Util.MapDebugPrint( "Fuse_Box_Built" );
	},

	Weapons_Hold_Destroyed = function( trigger )
	{
		SetAvailableMapGoals(TEAM.ALLIES, true, "CAPPOINT_bomb_parts");  	//bring bomb parts here
		SetAvailableMapGoals(TEAM.ALLIES, true, "FLAG_Bomb_Parts"); 		//Bomb Parts
		SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_d_bay1"); 		//Defend bomb delivery

		SetAvailableMapGoals(TEAM.ALLIES, false, {
			"MOUNTMG42_mg42_bank_1",
			"REPAIRMG42_mg42_bank_1",
		});

		Util.MapDebugPrint( "Weapons_Hold_Destroyed" );
	},

	Bomb_Parts_Taken = function( trigger )
	{
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_a_wh.*"); 		//Attack Weapon hold
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_d_wh.*"); 		//Defend Weapon hold

		SetAvailableMapGoals(TEAM.ALLIES, true, {
			"MOUNTMG42_mg42_bank",
			"REPAIRMG42_mg42_bank",
		});

		//Assembly area
		SetAvailableMapGoals(TEAM.ALLIES, true, "ATTACK_a_assembly.*"); 	//Attack Weapon hold
		SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_d_assembly.*"); 		//Defend Weapon hold
		SetAvailableMapGoals(TEAM.AXIS, true, "SNIPE_s_bay.*"); 		//Sniperspots bay

		Util.MapDebugPrint( "Bomb_Parts_Taken" );
	},

	Bomb_Parts_Returned = function( trigger )
	{
		SetAvailableMapGoals(TEAM.ALLIES, false, {
			"MOUNTMG42_mg42_bank",
			"REPAIRMG42_mg42_bank",
		});

		SetAvailableMapGoals(TEAM.ALLIES, true, "ATTACK_a_wh.*"); 				//Attack Weapon hold
		SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_d_wh.*"); 				//Defend Weapon hold

		//Assembly area
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_a_assembly.*"); 			//Attack Weapon hold
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_d_assembly.*"); 				//Defend Weapon hold

		Util.MapDebugPrint( "Bomb_Parts_Returned" );
	},

	Bomb_Parts_Secured = function( trigger )
	{
		SetAvailableMapGoals(TEAM.ALLIES, false, {
			"MOUNTMG42_mg42_bank",
			"REPAIRMG42_mg42_bank",
			"CAPPOINT_bomb_parts",
		});
		SetAvailableMapGoals(TEAM.ALLIES, false, "FLAG_Bomb_Parts"); 			//Bomb Parts
		SetAvailableMapGoals(TEAM.ALLIES, true, "BUILD_Bomb_Assembly_Area");  		//Open bomb crates
  		SetAvailableMapGoals(TEAM.ALLIES, true, "BUILD_Bomb_Assembly_Area_1");  	//Assemble bomb

		SetAvailableMapGoals(TEAM.AXIS, true, "BUILD_Depot_Main_Gates");

		//thread(Map.BombTraceLineThread);
		Map.BombTraceLineThreadId = thread(Map.BombTraceLineThread);

		Util.MapDebugPrint( "Bomb_Parts_Secured" );
	},


	BombTraceLineThread = function()
	{
	// two map coordinates:
	start = Vector3(3096.323, -2928.174, 456.125);
	end = Vector3(3087.999, -3062.127, 456.125);
	while (1)
		{
		tr = TraceLine(start, end, null, TRACE.SHOT, 0, false);
		// The tr.fraction value tells us of a collision or not.
		// If it's 1, there was no  collision. If less than 1 there was a collision.
		if(tr.fraction < 1)
			{
			//print("Bomb_built");
			DrawDebugLine(start, end, COLOR.RED, 2);
			Map.Bomb_Loaded();
			}
		else
			{
			//print("Not_built");
			DrawDebugLine(start, end, COLOR.GREEN, 2);
			}
		sleep(2);
		}
	},

	Bomb_Loaded = function( trigger )
	{
		//Switching Defend / Attack Goals

		Map.BombBuild = true;

		//Assembly area

		SetAvailableMapGoals(TEAM.ALLIES, false, {
			"BUILD_Bomb_Assembly_Area",
			"BUILD_Bomb_Assembly_Area_1",
		});

		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_a_assembly.*"); 	//Attack Weapon hold
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_d_assembly.*"); 		//Defend Weapon hold
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_d_bay1");
		SetAvailableMapGoals(TEAM.AXIS, false, "SNIPE_s_bay.*"); 		//Sniperspots bay

		//Gate
		SetAvailableMapGoals(TEAM.AXIS, true, "BUILD_Depot_Main_Gates"); 	//Gate barrier
		SetAvailableMapGoals(TEAM.ALLIES, true, "PLANT_Depot_Main_Gates"); 	//Gate barrier

		SetAvailableMapGoals( TEAM.ALLIES, true, "RIDE_Train_Allies" );

		//Track Attack, Defend
		SetAvailableMapGoals(TEAM.ALLIES, true, "ATTACK_a_hc_bay.*"); 		//attack bay track
		SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_d_hc_bay.*"); 		//defend bay track
		SetAvailableMapGoals(TEAM.ALLIES, true, "SNIPE_s_hc_bay.*"); 		//Sniperspots allies bay

		Util.MapDebugPrint( "Bomb_Loaded" );
		threadKill(Map.BombTraceLineThreadId);
	},

	GateTraceLineThread = function()
	{
	// two map coordinates:
	start = Vector3(-2030.127, -3165.496, 385.958);
	end = Vector3(-1945.875, -3171.957, 386.770);
	while (1)
		{
		tr = TraceLine(start, end, null, TRACE.SHOT, 0, false);
		// The tr.fraction value tells us of a collision or not.
		// If it's 1, there was no  collision. If less than 1 there was a collision.
		if(tr.fraction < 1)
			{
			Map.GateBuild = true;
			//print("Gate_built");
			DrawDebugLine(start, end, COLOR.RED, 2);
			Map.Cart_At_Gate();
			Map.Kill_Gate();
			}
		else
			{
			Map.GateBuild = false;
			SetAvailableMapGoals( TEAM.ALLIES, true, "RIDE_Train_Allies" );
			DrawDebugLine(start, end, COLOR.GREEN, 2);
			//print("Gate_Not_built");
			}
		sleep(2);
		}
	},

	Kill_Gate = function( trigger )
	{
		threadKill(Map.GateTraceLineThreadId);
	},

	Depot_Main_Gates_Destroyed = function( trigger )
	{
		if ( Map.CartAtGate == true )
		{
		Map.GateTraceLineThreadId = thread(Map.GateTraceLineThread);
		}

		if ( Map.BombBuild == true )
			{
			SetAvailableMapGoals( TEAM.ALLIES, true, "RIDE_Train_Allies" );
			}
		Util.MapDebugPrint( "Depot_Main_Gates_Destroyed" );
	},


	TrainInDepot =
 	{
 		TriggerOnEntity = GetGoal("MOVER_hc_car").GetEntity(),
 		OnEnter = function(ent)
 		{
			if ( Map.Start == true )
			{
			Map.Start = false;
			return;
			}

			SetAvailableMapGoals(TEAM.ALLIES,true , "ATTACK_a_hc_gate.*"); 		//attack track track
			SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_d_hc_gate.*"); 		//defend track track
			SetAvailableMapGoals( TEAM.AXIS, false, "RIDE_Train_Axis" );
			Util.MapDebugPrint( "Train_InDepot" );
		},
 		OnExit = function(ent)
 		{
			SetAvailableMapGoals( TEAM.AXIS, true, "RIDE_Train_Axis" );
			//Util.MapDebugPrint( "Train_Exiting_Depot" );
 		},
	},

	Trainout =
 	{
 		TriggerOnEntity = GetGoal("MOVER_hc_car").GetEntity(),
 		OnEnter = function(ent)
 		{
			SetAvailableMapGoals(TEAM.ALLIES,true , "ATTACK_a_hc_gate.*"); 		//attack track track
			SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_d_hc_gate.*"); 		//defend track track
			Util.MapDebugPrint( "Train_Out" );
		},
 		OnExit = function(ent)
 		{

			SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_a_hc_bay.*"); 			//attack bay track
			SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_d_hc_bay.*"); 			//defend bay track
			//Util.MapDebugPrint( "Train_Exiting" );
 		},
	},

	GateArea =
 	{
 		TriggerOnEntity = GetGoal("MOVER_hc_car").GetEntity(),
 		OnEnter = function(ent)
 		{
			SetAvailableMapGoals(TEAM.ALLIES, true, "ATTACK_a_hc_gate.*"); 			//Attack gate track
			SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_d_hc_gate.*"); 			//defend gate track
			SetAvailableMapGoals(TEAM.AXIS, true, {
				"MOUNTMG42_mg42_bank_2",
				"REPAIRMG42_mg42_bank_2",
			});
			Util.MapDebugPrint( "Train_Entering_GateArea" );
		},
 		OnExit = function(ent)
 		{

			SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_a_hc_gate.*"); 		//Attack gate track
			SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_d_hc_gate.*"); 			//defend gate track
			SetAvailableMapGoals(TEAM.AXIS, false, {
				"MOUNTMG42_mg42_bank_2",
				"REPAIRMG42_mg42_bank_2",
			});
			//Util.MapDebugPrint( "Train_Exiting" );
 		},
	},

	AtGate =
 	{
 		TriggerOnEntity = GetGoal("MOVER_hc_car").GetEntity(),
 		OnEnter = function(ent)
 		{
			Map.CartAtGate = true;
			Util.MapDebugPrint( "Train_At_Gate" );
			Map.GateTraceLineThreadId = thread(Map.GateTraceLineThread);
		},
 		OnExit = function(ent)
 		{
			Map.CartAtGate = false;
			SetAvailableMapGoals( TEAM.ALLIES, true, "RIDE_Train_Allies" ); // Keep Active
			Util.MapDebugPrint( "Train_Exit_Gate" );
			Map.Kill_Gate();
		},
	},

	Cart_At_Gate = function( trigger )
	{
		sleep(1);
		if ( Map.GateBuild == true )
				{
				SetAvailableMapGoals( TEAM.ALLIES, false, "RIDE_Train_Allies" );
				}

	},

	PastBarrier =
 	{
 		TriggerOnEntity = GetGoal("MOVER_hc_car").GetEntity(),
 		OnEnter = function(ent)
 		{
			SetAvailableMapGoals(TEAM.ALLIES, true, "ATTACK_a_hc_trainyard.*"); 	//Attack yard track
			SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_d_hc_trainyard.*"); 	//defend yard track
			SetAvailableMapGoals(TEAM.AXIS, true, "SNIPE_s_trainyard.*"); 		//Sniperspots axis trainyard
			SetAvailableMapGoals(TEAM.AXIS, false, "BUILD_Depot_Main_Gates");
			Util.MapDebugPrint( "Train_PastBarrier" );
		},
 		OnExit = function(ent)
 		{
			SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_a_hc_trainyard.*"); 	//Attack yard track
			SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_d_hc_trainyard.*"); 	//defend yard track
			SetAvailableMapGoals(TEAM.AXIS, false, "SNIPE_s_trainyard.*"); 		//Sniperspots axis trainyard
			SetAvailableMapGoals(TEAM.AXIS, true, "BUILD_Depot_Main_Gates");
			//Util.MapDebugPrint( "Exiting_PastBarrier" );
 		},
	},


};

global OnMapLoad = function()
{
	// Register callback functions
	//OnTrigger( "The Allies have opened the crates and are assembling the bomb!", Map.Bomb_Assembly_Area_Built );
	//OnTrigger( "The Bomb has been assembled", Map.Bomb_Assembly_Area_1_Built );

	OnTrigger( "fuse_panel_start", Map.Fuse_Panel_Open );
	OnTrigger( "slide_right_goto", Map.Fuse_Box_Built );
	OnTrigger( "the Weapons Hold Destroyed.", Map.Weapons_Hold_Destroyed );
	OnTrigger( "Allies have stolen the Bomb Parts!", Map.Bomb_Parts_Taken );
	OnTrigger( "Axis have returned the Bomb Parts!", Map.Bomb_Parts_Returned );
	OnTrigger( "Allies captured bomb_ctf_2", Map.Bomb_Parts_Secured );
	//OnTrigger( "The Main Gates have been constructed.", Map.Depot_Main_Gates_Built ); // no trigger
	OnTrigger( "The Main Gates have been destroyed.", Map.Depot_Main_Gates_Destroyed );



	Train1 = OnTriggerRegion(AABB(2103.863,-3053.803,401.839,3210.530,-2949.096,507.125),Map.TrainInDepot);
	Trainout = OnTriggerRegion(AABB(-96.869,-3048.427,277.976,2097.554,-2943.513,471.086),Map.Trainout);
	GateArea = OnTriggerRegion(AABB(-1951.570,-3057.658,297.125,-110.123,-2935.960,439.947),Map.GateArea);
	AtGate = OnTriggerRegion(AABB(-1948.422,-3041.872,356.523,-1811.918,-2950.096,439.749),Map.AtGate);
	PastBarrier = OnTriggerRegion(AABB(-3014.948,-3058.303,363.323,-1992.164,-2940.299,454.125),Map.PastBarrier);

	Util.DisableGoal( ".*", true ); // all but routes
	Util.DisableGoal( "ROUTE_Fuse_Door" );

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

	// Camp times
	SetMapGoalProperties( "MOUNTMG42_.*", {MinCampTime=15, MaxCampTime=90} );
	SetMapGoalProperties( "MOBILEMG42_.*", {MinCampTime=10, MaxCampTime=60} );

	Util.EnableGoal( "AMMOCAB.*" );
	Util.EnableGoal( "HEALTHCAB.*" );

	//ALLIES GOALS
  	SetAvailableMapGoals(TEAM.ALLIES, true, "PLANT_Weapons_Hold");  	//Weapon hold door
	SetAvailableMapGoals(TEAM.ALLIES, true, "ATTACK_a_wh.*"); 		//Attack Weapon hold
	SetAvailableMapGoals(TEAM.ALLIES, true, {
		"BUILD_Fuse_Box",
		"MOUNTMG42_mg42_bank_1",
		"REPAIRMG42_mg42_bank_1",
	});

	//AXIS GOALS
	SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_d_wh.*"); 		//Defend Weapon hold

	MapRoutes =
	{
            //Goal: Secure the bomb parts
        	CAPPOINT_bomb_parts =
        	{
                    ROUTE_r_bomb_parts =
                    {
                        ROUTE_r_bomb_a1 =
                        {
                            ROUTE_r_bomb_a12 = { ROUTE_r_bomb_a13  =  {}, },
                            ROUTE_r_bomb_a2 =
                            {
                                ROUTE_r_bomb_a22  =
                                {
                                    ROUTE_r_bomb_a3a  =  {},
                                    ROUTE_r_bomb_a4  =
                                    {
                                        ROUTE_r_bomb_a4a  =  {},
                                        ROUTE_r_bomb_a4b  =  {},
                                    },
                                },
                                ROUTE_r_bomb_a3  =
                                {
                                    ROUTE_r_bomb_a3a  =  {},
                                    ROUTE_r_bomb_a4  =
                                    {
                                        ROUTE_r_bomb_a4a  =  {},
                                        ROUTE_r_bomb_a4b  =  {},
                                    },
                                },
                            },
                        },
                        ROUTE_r_bomb_b1 =
                        {
                            ROUTE_r_bomb_b2 =
                            {
                                ROUTE_r_bomb_a22  =
                                {
                                    ROUTE_r_bomb_a3a  =  {},
                                    ROUTE_r_bomb_a4  =
                                    {
                                        ROUTE_r_bomb_a4a  =  {},
                                        ROUTE_r_bomb_a4b  =  {},
                                    },
                                },
                            },
                        },
                    },
        	},
		PLANT_Depot_Main_Gates =
		{
			ROUTE_Spawn_1A  =
			{
				ROUTE_Spawn_1B  =
				{
					ROUTE_Fuse_Door  =
					{
					},
					ROUTE_Stairway  =
					{
					},
				},

			},
			ROUTE_Spawn_1B  =
			{
				ROUTE_Fuse_Door  =
				{
				},
				ROUTE_Stairway  =
				{
				},
			},
		},
		RIDE_Train_Allies =
		{
			ROUTE_Spawn_3  =
			{
				ROUTE_Spawn3_FrontExit  =
				{
				},
				ROUTE_Spawn3_SideExit  =
				{
				},
			},
		},
		ATTACK_a_hc_bay1 =
		{
			ROUTE_Spawn_3  =
			{
				ROUTE_Spawn3_FrontExit  =
				{
					Weight = 2,
					ROUTE_Depot_Main  =
					{
					},
					ROUTE_Depot_Side  =
					{
					},
				},
				ROUTE_Spawn3_SideExit  =
				{
					Weight = 2,
					ROUTE_Depot_Main  =
					{
					},
					ROUTE_Depot_Side  =
					{
					},
				},
			},
		},
		DEFEND_d_hc_bay1 =
		{
			ROUTE_Spawn_2  =
			{
				ROUTE_r_bomb_a13  =
				{
				},
				ROUTE_FlagPole_Exit  =
				{
				},
			},
		},
		DEFEND_d_hc_trainyard1 =
		{
			ROUTE_Spawn_4  =
			{
				ROUTE_Gate_Exit  =
				{
				},
				ROUTE_Ice_Exit  =
				{
				},
			},
		},
		DEFEND_d_hc_gate5 =
		{
			ROUTE_Spawn_2  =
			{
				ROUTE_Gate_Exit  =
				{
				},
			},
		},
    	};
	MapRoutes.MOUNTMG42_mg42_bank_1 = MapRoutes.PLANT_Depot_Main_Gates;
	MapRoutes.FLAG_Bomb_Parts = MapRoutes.PLANT_Depot_Main_Gates;
	MapRoutes.ATTACK_a_wh1 = MapRoutes.PLANT_Depot_Main_Gates;
	MapRoutes.ATTACK_a_wh2 = MapRoutes.PLANT_Depot_Main_Gates;
	MapRoutes.ATTACK_a_wh3 = MapRoutes.PLANT_Depot_Main_Gates;
	MapRoutes.ATTACK_a_wh4 = MapRoutes.PLANT_Depot_Main_Gates;
	MapRoutes.ATTACK_a_wh5 = MapRoutes.PLANT_Depot_Main_Gates;
	MapRoutes.ATTACK_a_assembly1 = MapRoutes.RIDE_Train_Allies;
	MapRoutes.ATTACK_a_assembly2 = MapRoutes.RIDE_Train_Allies;
	MapRoutes.ATTACK_a_assembly3 = MapRoutes.RIDE_Train_Allies;
	MapRoutes.ATTACK_a_assembly4 = MapRoutes.RIDE_Train_Allies;
	MapRoutes.ATTACK_a_assembly5 = MapRoutes.RIDE_Train_Allies;
	MapRoutes.ATTACK_a_hc_bay2 = MapRoutes.ATTACK_a_hc_bay1;
	MapRoutes.ATTACK_a_hc_bay3 = MapRoutes.ATTACK_a_hc_bay1;
	MapRoutes.ATTACK_a_hc_bay4 = MapRoutes.ATTACK_a_hc_bay1;
	MapRoutes.ATTACK_a_hc_bay1 = MapRoutes.ATTACK_a_hc_bay1;
	MapRoutes.ATTACK_a_hc_gate2 = MapRoutes.ATTACK_a_hc_bay1;
	MapRoutes.ATTACK_a_hc_gate3 = MapRoutes.ATTACK_a_hc_bay1;
	MapRoutes.ATTACK_a_hc_gate4 = MapRoutes.ATTACK_a_hc_bay1;
	MapRoutes.ATTACK_a_hc_trainyard2 = MapRoutes.DEFEND_d_hc_trainyard1;
	MapRoutes.ATTACK_a_hc_trainyard3 = MapRoutes.DEFEND_d_hc_trainyard1;
	MapRoutes.ATTACK_a_hc_trainyard4 = MapRoutes.DEFEND_d_hc_trainyard1;
	MapRoutes.DEFEND_d_hc_bay2 = MapRoutes.DEFEND_d_hc_bay1;
	MapRoutes.DEFEND_d_hc_bay3 = MapRoutes.DEFEND_d_hc_bay1;
	MapRoutes.DEFEND_d_hc_gate1 = MapRoutes.DEFEND_d_hc_bay1;
	MapRoutes.DEFEND_d_hc_gate2 = MapRoutes.DEFEND_d_hc_bay1;
	MapRoutes.DEFEND_d_hc_gate3 = MapRoutes.DEFEND_d_hc_bay1;
	MapRoutes.DEFEND_d_hc_gate6 = MapRoutes.DEFEND_d_hc_gate5;

	Util.Routes(MapRoutes);
	Util.UpdateSwitchData();

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

global OnBotJoin = function( bot )
{
	bot.TargetBreakableDist = 90.0;
	bot.MaxViewDistance = 2400;
};
