/********************************************************
Script and waypoints made by  >WES< P!NNAN & >WES< GRANNY
********************************************************/

global Map =
{
	Quiet = true,
	Debug = false,
	Talk = true, // set this to false if you wont bots talking ingame
	Code = false,
	PulledOne = true,
	Pulled2 = true,
	PulledTwo = true,
	Pulled = true,
	SubOne = false,
	CraneSwitchStatus = "up",
	switch1Status = "down",
	switch2Status = "down",

	Ammo_Cabinet_supplycabinet = "AMMOCAB_supplycabinet",
	Health_Cabinet_loading_health_model = "HEALTHCAB_loading_health_model",
	Flag_access_codes = "FLAG_access_codes",
	Cappoint_Dropoff = "CAPPOINT_Dropoff",
	Build_Command_Post = "BUILD_Command_Post",
	Build_Crate_of_Explosives = "BUILD_Crate_of_Explosives",
	Plant_Command_Post = "PLANT_Command_Post",
	Plant_Side_Door = "PLANT_Side_Door",
	Plant_U_245 = "PLANT_U_245",
	Plant_U_952 = "PLANT_U_952",
	Mobile_MG42_m_al_sub = "MOBILEMG42_m_al_sub",
	Mobile_MG42_m_ax_start = "MOBILEMG42_m_ax_start",
	Mobile_MG42_m_ax_sub_1 = "MOBILEMG42_m_ax_sub_1",
	Mobile_MG42_m_ax_sub_2 = "MOBILEMG42_m_ax_sub_2",
	Mobile_MG42_mg_al_1 = "MOBILEMG42_mg_al_1",
	Mobile_MG42_mg_ax_start = "MOBILEMG42_mg_ax_start",
	Mobile_Mortar_mo_al_start = "MOBILEMORTAR_mo_al_start",
	Mobile_Mortar_mo_ax_start = "MOBILEMORTAR_mo_ax_start",
	Snipe_s_al_start_1 = "SNIPE_s_al_start_1",
	Snipe_s_al_start_2 = "SNIPE_s_al_start_2",
	Snipe_s_al_sub_1 = "SNIPE_s_al_sub_1",
	Snipe_s_al_sub_2 = "SNIPE_s_al_sub_2",
	Snipe_s_ax_start_1 = "SNIPE_s_ax_start_1",
	Snipe_s_ax_start_2 = "SNIPE_s_ax_start_2",
	Snipe_s_ax_sub_1 = "SNIPE_s_ax_sub_1",
	Snipe_s_ax_sub_2 = "SNIPE_s_ax_sub_2",

	Navigation =
	{
		pcladderpt =
		{
			navigate = function(_this)
			{
				_this.Bot.HoldButton(BTN.FORWARD, 3);
				sleep(3);
			},
		},
		roomladderpt =
		{
			navigate = function(_this)
			{
				_this.Bot.HoldButton(BTN.FORWARD, 3);
				sleep(3);
			},
		},
	},

	Switches =
	{
		CraneSwitch =
		{
			Enabled = true,
			//Debug = true,
			WaypointName = "Craneswitch",
			LimitBots = 1,
			LimitTeam = 0, // disabled initially.
			LimitClass = Util.AllClasses,
			AimPosition = Vector3(-2593.811,-454.712,129.545),
			Priority = 0.7,
		},

		switch1 =
		{
			Enabled = true,
			//Debug = true,
			WaypointName = "Switch_1",
			LimitBots = 1,
			LimitClass = Util.AllClasses,
			AimPosition = Vector3(61.875,623.541,-121.811),
			Priority = 0.0, // paththrough
			Wait = function()
			{
				sleep(1.5);
				return true;
			},
		},

		switch2 =
		{
			Enabled = true,
			//Debug = true,
			WaypointName = "Switch_2",
			LimitBots = 1,
			LimitClass = Util.AllClasses,
			AimPosition = Vector3(61.875,175.645,-121.811),
			Priority = 0.0, // paththrough
			Wait = function()
			{
				sleep(1.5);
				return true;
			},
		},

		switch3 =
		{
			Enabled = true,
			//Debug = true,
			WaypointName = "Switch_3",
			LimitBots = 1,
			LimitClass = Util.AllClasses,
			AimPosition = Vector3(82.125,623.541,-121.811),
			Priority = 0.0, // paththrough
			Wait = function()
			{
				sleep(1.5);
				return true;
			},
		},

		switch4 =
		{
			Enabled = true,
			//Debug = true,
			WaypointName = "Switch_4",
			LimitBots = 1,
			LimitClass = Util.AllClasses,
			AimPosition = Vector3(82.125,175.645,-121.811),
			Priority = 0.0, // paththrough
			Wait = function()
			{
				sleep(1.5);
				return true;
			},
		},
	},

	//CP stuff
	Command_Post_Built = function( trigger )
	{
		Groups.DisableAllies(Groups.group5);
		Groups.EnableAllies(Groups.group7);

		Util.MapDebugPrint( "Allied_Command_Post_Built" );
	},

	Command_Post_Built = function( trigger )
	{
		Util.MapDebugPrint( "Axis_Command_Post_Built" );
	},

	Command_Post_Destroyed = function( trigger )
	{
		Groups.DisableAllies(Groups.group7);
		Groups.EnableAllies(Groups.group5);

		Util.MapDebugPrint( "Allied_Command_Post_Destroyed" );
	},

	Command_Post_Destroyed = function( trigger )
	{
		Util.MapDebugPrint( "Axis_Command_Post_Destroyed" );
	},

	//sidedoor
	Side_Door_Destroyed = function( trigger )
	{
		Util.MapDebugPrint( "Side_Door_Destroyed" );
	},

	//obj
	access_codes_Taken = function( trigger )
	{
		Map.Code = true;

		if ( Map.Talk && MAP_TALK )
		{
			foreach ( id and bot in BotTable )
			{
				if ( bot.GetTeam() == TEAM.ALLIES )
				{
					r = RandRange(0,10);
					if( r < 2 )
					{
						bot.SayVoice(VOICE.LETS_GO);
					}
				}
			}
		}

		Util.MapDebugPrint( "access_codes_Taken" );
	},

	access_codes_Returned = function( trigger )
	{
		if ( Map.Talk && MAP_TALK )
		{
			foreach ( id and bot in BotTable )
			{
				if ( bot.GetTeam() == TEAM.AXIS )
				{
					r = RandRange(0,10);
					if( r < 2 )
					{
						bot.SayVoice(VOICE.G_CHEER);
					}
				}
			}
		}

		Util.MapDebugPrint( "access_codes_Returned" );
	},

	Dropoff_Captured = function( trigger )
	{
		Groups.DisableAllies(Groups.group1);
		Groups.DisableAxis(Groups.group2);
		Groups.EnableAllies(Groups.group3);
		Groups.EnableAxis(Groups.group4);

		if ( Map.Talk && MAP_TALK )
		{
			foreach ( id and bot in BotTable )
			{
				if ( bot.GetTeam() == TEAM.ALLIES )
				{
					r = RandRange(0,10);
					if( r < 2 )
					{
						bot.SayVoice(VOICE.LETS_GO);
					}
				}
			}
		}

		Map.Switches.CraneSwitch.LimitTeam = (1<<TEAM.ALLIES);

		Util.MapDebugPrint( "Dropoff_Captured" );
	},

	//Switches
	CraneMove = function( trigger )
	{
		vec = ToVector(trigger.Action);

		if ( vec.y < -18 )
		{
			//Util.MapDebugPrint( "craneswitch pulled down" );
			sleep(1.0);

			Map.CraneSwitchStatus = "down";
			Map.PulledCrane = true;
			Map.Switches.CraneSwitch.LimitTeam = (1<<TEAM.AXIS);
			Util.MapDebugPrint( "Switch ready for Axis", true);
		}
		else if ( vec.y > 18 )
		{
			//Util.MapDebugPrint( "craneswitch pulled up" );
			sleep(1.0);
			Map.CraneSwitchStatus = "up";

			if ( Map.PulledCrane )
			{
				Map.Switches.CraneSwitch.LimitTeam = (1<<TEAM.ALLIES);
				Util.MapDebugPrint( "Switch ready for Allies", true);
			}
		}
	},

	LeverOneMove = function( trigger )
	{
		Map.Switches.switch3.Enabled = false;
		Map.Switches.switch1.Enabled = false;

		vec = ToVector(trigger.Action);
		if ( vec.z < 0 )
		{
			Map.switch1Status = "down";
			Map.Switches.switch3.Enabled = true;
			Map.Switches.switch1.Enabled = true;
		}
		else if ( vec.z > 0 )
		{
			Map.switch1Status = "up";
		}

		Util.MapDebugPrint( "switch1 is now " + Map.switch1Status, true );
	},

	LeverTwoMove = function( trigger )
	{
		Map.Switches.switch4.Enabled = false;
		Map.Switches.switch2.Enabled = false;

		vec = ToVector(trigger.Action);
		if ( vec.z < 0 )
		{
			Map.switch2Status = "down";
			Map.Switches.switch4.Enabled = true;
			Map.Switches.switch2.Enabled = true;
		}
		else if ( vec.z > 0 )
		{
			Map.switch2Status = "up";
		}

		Util.MapDebugPrint( "switch2 is now" + Map.switch2Status, true );
	},

	sub_active = function( trigger )
	{
		Groups.DisableAllies(Groups.group3);
		Groups.DisableAxis(Groups.group4);
		Groups.EnableAllies(Groups.group5);
		Groups.EnableAxis(Groups.group6);

		if ( Map.Talk && MAP_TALK )
		{
			foreach ( id and bot in BotTable )
			{
				if ( bot.GetTeam() == TEAM.ALLIES )
				{
					r = RandRange(0,10);
					if( r < 2 )
					{
						bot.SayVoice(VOICE.REINFORCE_OFF);
					}
					else if( r > 8 )
					{
						sleep(1.2);
						bot.ExecCommand("vsay_team FTAttack");
					}
				}
				if ( bot.GetTeam() == TEAM.AXIS )
				{
					r = RandRange(0,10);
					if( r < 2 )
					{
						bot.SayVoice(VOICE.DEFEND_OBJECTIVE);
					}
					else if( r > 8 )
					{
						sleep(1.2);
						bot.ExecCommand("vsay_team FTFallBack");
					}
				}
			}
		}

		Util.MapDebugPrint( "crane at subs" );
	},

	//Submarines
	U_245_Destroyed = function( trigger )
	{
		Map.SubOne = true;

		if ( Map.SubTwo ) {
			ETUtil.WinningChat( TEAM.ALLIES );
			ETUtil.LosingChat( TEAM.AXIS );
		}

		Util.MapDebugPrint( "U_245_Destroyed" );
	},

	U_952_Destroyed = function( trigger )
	{
		Map.SubTwo = true;

		if ( Map.SubOne ) {
			ETUtil.WinningChat( TEAM.ALLIES );
			ETUtil.LosingChat( TEAM.AXIS );
		}

		Util.MapDebugPrint( "U_952_Destroyed" );
	},
};

global OnMapLoad = function()
{
	Util.SetGoalPosition(2687.878,1276.992,-161.875, "PLANT_U_952");

	OnTrigger( "Allied Command Post constructed. Charge speed increased!", Map.Command_Post_Built );
	OnTrigger( "Axis Command Post constructed. Charge speed increased!", Map.Command_Post_Built );
	OnTrigger( "Axis team has destroyed the Allied Command Post!", Map.Command_Post_Destroyed );
	OnTrigger( "Allied team has destroyed the Axis Command Post!", Map.Command_Post_Destroyed );
	OnTrigger( "The Allies destroyed the side door", Map.Side_Door_Destroyed );
	OnTrigger( "The Allies have sunk U-245", Map.U_245_Destroyed );
	OnTrigger( "The Allies have sunk U-952", Map.U_952_Destroyed );
	OnTrigger( "The Allies have stolen the Code Module", Map.access_codes_Taken );
	OnTrigger( "The Axis have retrieved the Code Module", Map.access_codes_Returned );
	OnTrigger( "Crane controls enabled", Map.Dropoff_Captured );
	OnTrigger( "The explosives have been delivered to the submarine pen", Map.sub_active );

	// cs: commented these out as imo they should be high priority. these were set lower than switches ...
	//Destroy Main goals
	// SetGoalPriority( "PLANT_U_952", 0.6 );
	// SetGoalPriority( "BUILD_Command_Post", 0.6 );

	//Switchtriggers
	OnTrigger( "lift_switch_goto", Map.CraneMove );
	OnTrigger( "garage_door1_lever1_goto", Map.LeverOneMove );
	OnTrigger( "garage_door2_lever1_goto", Map.LeverTwoMove );

	//set max users for goals
	Util.SetMaxUsersInProgress( 2, "BUILD.*" );
	Util.SetMaxUsersInProgress( 2, "PLANT.*" );
	Util.SetMaxUsersInProgress( 1, "DEFEND_d_start.*" );
	Util.SetMaxUsersInProgress( 1, "DEFEND_d_cp" );
	Util.SetMaxUsersInProgress( 1, "DEFEND_d_sub.*" );
	Util.SetMaxUsersInProgress( 1, "ATTACK_a_start.*" );
	Util.SetMaxUsersInProgress( 1, "ATTACK_a_sub.*" );
	Util.SetMaxUsersInProgress( 1, "ATTACK_a_cp" );

	SetMapGoalProperties( "MOBILEMG42_.*", {MinCampTime=120, MaxCampTime=120});
	SetMapGoalProperties( "SNIPE_.*", {MinCampTime=60, MaxCampTime=120});
	SetMapGoalProperties( "ATTACK_.*", {MinCampTime=30, MaxCampTime=60});
	SetMapGoalProperties( "DEFEND_.*", {MinCampTime=60, MaxCampTime=120});

	SetAvailableMapGoals( TEAM.AXIS, false, ".*" );
	SetAvailableMapGoals( TEAM.ALLIES, false, ".*" );

	//enable part 1 goals
	Groups.EnableAllies(Groups.group1);
	Groups.EnableAxis(Groups.group2);

	//Routes
	MapRoutes =
	{
		CAPPOINT_Dropoff =
		{
			ROUTE_r_obj =
			{
				ROUTE_r_stairexit =
				{
					ROUTE_r_bunker = {},
					Weight = 2,
					ROUTE_r_truck =
					{
						ROUTE_r_behindtrain2 =
						{
							Weight = 4,
							ROUTE_r_ramp = {},
							ROUTE_r_behindtrain3 ={ ROUTE_r_behindtrain3_1 = {}, },
						},
					},
				},
			},
		},
		PLANT_U_952 =
		{
			ROUTE_r_alliedspawn_1 =
			{
				ROUTE_r_shortway_1 =
				{
					ROUTE_r_shortway_2 = {},
				},
			},
			ROUTE_r_alliedspawn_2 =
			{
				ROUTE_r_leftdoor =
				{
					ROUTE_r_uboot_pen = {},
				},
			},

		},
	};

	//set up the Switches and Routes
	Util.UpdateSwitchData();
	Util.Routes(MapRoutes);
	Util.MapDebugPrint( "OnMapLoad" );
};

global OnBotJoin = function( bot )
{
	bot.TargetBreakableDist = 100.0;
	bot.MaxViewDistance = 2500;

	if( class == CLASS.COVERTOPS )
	{
		if ( bot.HasWeapon(WEAPON.K43) || bot.HasWeapon(WEAPON.GARAND))
		{
			bot.MaxViewDistance = 10000.0;
		}
	}
};

global Groups =
{
	//Allied part 1
	group1 =
	{
		Map.Flag_access_codes,
		Map.Cappoint_Dropoff,
		Map.Plant_Side_Door,
		Map.Mobile_MG42_mg_al_1,
		"MOBILEMORTAR_mo_al_start.*",
		//"ATTACK_a_start.*", // cs: doesn't exist
	},

	//Axis part 1
	group2 =
	{
		Map.Mobile_MG42_mg_ax_start,
		Map.Build_Command_Post,
		"MOBILEMORTAR_mo_ax_start.*",
		"DEFEND_d_start.*",
	},

	//Allied part 2
	group3=
	{
		Map.Mobile_MG42_mg_al_1,
		Map.Snipe_s_al_start_1,
		Map.Snipe_s_al_start_2,
		"MOBILEMORTAR_mo_al_start.*",
		"ATTACK_a_switch.*",
	},

	//Axis part2
	group4=
	{
		Map.Snipe_s_ax_start_1,
		Map.Snipe_s_ax_start_2,
		Map.Mobile_MG42_mg_al_1,
		"MOBILEMORTAR_mo_ax_start.*",
		"DEFEND_d_switch.*",
	},

	//Allied part 3
	group5=
	{
		Map.Plant_U_245,
		Map.Plant_U_952,
		Map.Plant_Command_Post,
		Map.Build_Command_Post,
		"ATTACK_a_sub.*",
	},

	//Axis part 3
	group6 =
	{
		Map.Plant_Command_Post,
		Map.Build_Command_Post,
		Map.Mobile_MG42_m_ax_sub_1,
		Map.Mobile_MG42_m_ax_sub_2,
		Map.Snipe_s_ax_sub_1,
		Map.Snipe_s_ax_sub_2,
		"DEFEND_d_sub.*",
	},

	//Allied part 4
	group7=
	{
		Map.Mobile_MG42_m_al_sub,
		Map.Snipe_s_al_sub_1,
		Map.Snipe_s_al_sub_2,
		Map.Plant_U_245,
		Map.Plant_U_952,
		"ATTACK_a_sub.*",
	},

	EnableAllies = function(group)
	{
		foreach (id and goal in group)
		{
			SetAvailableMapGoals( TEAM.ALLIES, true, goal );
			Util.MapDebugPrint("EnableAllies: enabling " + goal, true);
		}
	},

	EnableAxis = function(group)
	{
		foreach (id and goal in group)
		{
			SetAvailableMapGoals( TEAM.AXIS, true, goal );
			Util.MapDebugPrint("EnableAxis: enabling " + goal, true);
		}
	},

	DisableAllies = function(group)
	{
		foreach (id and goal in group)
		{
			SetAvailableMapGoals( TEAM.ALLIES, false, goal );
			Util.MapDebugPrint("DisableAllies: disabling " + goal, true);
		}
	},

	DisableAxis = function(group)
	{
		foreach (id and goal in group)
		{
			SetAvailableMapGoals( TEAM.AXIS, false, goal );
			Util.MapDebugPrint("DisableAxis: disabling " + goal, true);
		}
	},
};
