//==========================================================================================
//
//	al_kad_b2.gm
//
//	Who			When		What
//------------------------------------------------------------------------------------------
//	d00d			05/17/08	Original script
//	jaskot			02/03/09	Converted old script to new format
//	d00d			11/13/09	Updated for 0.8
//
//==========================================================================================
//

global Map =
{
	Ammo_Cabinet_first_ammocabinet = "AMMOCAB_first_ammocabinet",
	Ammo_Cabinet_north_ammocabinet = "AMMOCAB_north_ammocabinet",
	Health_Cabinet_first_healthcabinet = "HEALTHCAB_first_healthcabinet",
	Health_Cabinet_north_healthcabinet = "HEALTHCAB_north_healthcabinet",
	Flag_relic = "FLAG_relic",
	Cappoint_relic = "CAPPOINT_relic",
	Build_Command_Post = "BUILD_Command_Post",
	Build_Prison_gate = "BUILD_Prision_gate",
	Build_Relic_Box = "BUILD_Relic_Box",
	Plant_Back_door = "PLANT_Back_door",
	Plant_Command_Post = "PLANT_Command_Post",
	Plant_Front_door = "PLANT_Front_door",
	Plant_Prison_gate = "PLANT_Prision_gate",
	Plant_Relic_Box = "PLANT_Relic_Box",
	Plant_Rocket = "PLANT_Rocket",

	Talk = true,

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

	Prison_gate_Built = function()
	{
		Util.MapDebugPrint( "Prison_gate_Built" );
	},

	Back_door_Destroyed = function()
	{
		Util.MapDebugPrint( "Back_door_Destroyed" );
	},

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

	Front_door_Destroyed = function()
	{
		Util.MapDebugPrint( "Front_door_Destroyed" );
	},

	Prison_gate_Destroyed = function()
	{
		Util.MapDebugPrint( "Prison_gate_Destroyed" );
	},

	Relic_Box_Planted = function()
	{
		if ( Map.Talk && MAP_TALK )
		{
			wppos = Util.GetWpNamePosition( "the_box" );

			foreach ( id and bot in BotTable )
			{
				if ( bot.GetTeam() == TEAM.AXIS &&
				     bot.GetClass() != CLASS.ENGINEER &&
				     bot.DistanceTo( wppos ) < 1000 )
				{
					ran = RandRange( 0, 10 );

					if ( ran < 4 &&
						!bot.GetNearestAlly( CAT.PLAYER,
							CLASS.ENGINEER ) )
					{
						sleep(ran);
						bot.SayVoice( VOICE.NEED_ENGINEER );
					}
					else if ( ran > 6 )
					{
						sleep( 0.5 );
						bot.SayVoice( VOICE.DISARM_DYNAMITE );
					}
				}
			}
		}

		Util.MapDebugPrint( "Relic_Box_Planted" );
	},

	Relic_Box_Destroyed = function()
	{
		SetAvailableMapGoals( TEAM.ALLIES, true, Map.Flag_relic );

		Util.MapDebugPrint( "Relic_Box_Destroyed" );
	},

	Rocket_Planted = function()
	{
		if ( Map.Talk && MAP_TALK )
		{
			wppos = Util.GetWpNamePosition( "axis_rocket9" );

			foreach ( id and bot in BotTable )
			{
				if ( bot.GetTeam() == TEAM.AXIS &&
				     bot.GetClass() != CLASS.ENGINEER &&
				     bot.DistanceTo( wppos ) < 1000 )
				{
					ran = RandRange(0,10);

					if ( ran < 4 &&
					     !bot.GetNearestAlly(CAT.PLAYER,
						CLASS.ENGINEER) )
					{
						sleep( ran );
						bot.SayVoice( VOICE.NEED_ENGINEER );
					}
					else if ( ran > 6 )
					{
						sleep( 0.7 );
						bot.SayVoice( VOICE.DISARM_DYNAMITE );
					}
				}
			}
		}

		Util.MapDebugPrint( "Rocket_Planted" );
	},

	Rocket_Destroyed = function()
	{
		Util.MapDebugPrint( "Rocket_Destroyed" );
	},

	relic_Taken = function()
	{
		SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_cappoint.*" );
		SetAvailableMapGoals( TEAM.AXIS, true, "ATTACK_cappoint.*" );

		Util.MapDebugPrint( "relic_Taken" );
	},

	relic_Captured = function()
	{
		SetGoalPriority( Map.Plant_Rocket, 0.9 );
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_cappoint.*" );
		SetAvailableMapGoals( TEAM.AXIS, false, "ATTACK_cappoint.*" );
		SetAvailableMapGoals( TEAM.AXIS, true, {
			"DEFEND_axis_rocket.*",
			"SNIPE_axis_rocket.*",
		});
		SetAvailableMapGoals( TEAM.ALLIES, true, {
			"ATTACK_allies_rocket.*",
			"SNIPE_allies_rocket.*",
			Map.Plant_Prison_gate,
			Map.Plant_Rocket,
		});

		Util.ChangeSpawn( TEAM.ALLIES, 3 );
		Util.MapDebugPrint( "relic_Captured" );

		if ( Map.Talk && MAP_TALK )
		{
			timeLeft = GetGameTimeLeft();
			if ( timeLeft < 120 ) {
				return;
			}

			rand = ETUtil.CountTeam( TEAM.AXIS ) * 2;
			rand2 = ETUtil.CountTeam( TEAM.ALLIES ) * 2;

			foreach ( gameId and bot in BotTable )
			{
				team = bot.GetTeam();

				if ( team == TEAM.AXIS )
				{
					if ( RandInt(0, rand) < 2 ) {
						sleep( RandRange( 0.7, 2.1 ) );
						bot.SayVoice( VOICE.DEFEND_OBJECTIVE );
					}
					else if ( RandInt(0, rand) < 2 ) {
						sleep( RandRange( 1, 2.0 ) );
						bot.ExecCommand( "vsay_team FTFallBack" );
					}
					else if ( RandInt( 0, rand ) < 2 ) {
						sleep( RandRange( 0.5, 1.5 ) );
						bot.SayVoice( VOICE.REINFORCE_DEF );
					}
				}
				else if ( team == TEAM.ALLIES )
				{
					if ( RandInt(0, rand2) < 2 ) {
						sleep( RandRange( 1, 2.8 ) );
						bot.SayVoice( VOICE.REINFORCE_OFF );
					}
					else if ( RandInt(0,rand2) < 2 ) {
						sleep( 1.5 );
						bot.ExecCommand( "vsay_team FTAttack" );
					}
					else if ( RandInt(1, rand2) < 2
								&& bot.GetClass() != CLASS.ENGINEER
								&& timeLeft > 180)
					{
						sleep(0.6);
						bot.SayVoice( VOICE.DESTROY_PRIMARY );
					}
				}
			}
		}
	},

	relic_Returned = function()
	{
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_cappoint.*" );
		SetAvailableMapGoals( TEAM.AXIS, false, "ATTACK_cappoint.*" );

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

global OnMapLoad = function()
{
	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 has built the Prison gate!", Map.Prison_gate_Built );
	OnTrigger( "Back door Destroyed.", Map.Back_door_Destroyed );
	OnTrigger( "Axis team has destroyed the Allied Command Post!", Map.Command_Post_Destroyed );
	OnTrigger( "Front door Destroyed.", Map.Front_door_Destroyed );
	OnTrigger( "Allies have destroyed the Prison gate!", Map.Prison_gate_Destroyed );
	OnTrigger( "Planted at the Relic Box.", Map.Relic_Box_Planted );
	OnTrigger( "Allies have destroyed the Box!!", Map.Relic_Box_Destroyed );
	OnTrigger( "Planted at The Rocket.", Map.Rocket_Planted );
	OnTrigger( "Allies have destroyed the rocket!", Map.Rocket_Destroyed );
	OnTrigger( "Allies have stolen the relic!", Map.relic_Taken );
	OnTrigger( "Allied team has secured the relic!", Map.relic_Captured );
	OnTrigger( "Flag returned the relic!", Map.relic_Returned );

	Util.SetGoalOffset( 10, 0, 0, Map.Plant_Relic_Box );
	Util.SetGoalOffset( 0, 0, -50, Map.Build_Prison_gate );
	Util.SetGoalOffset( -10, 0, 0, "PLANT_Front_door" );
	Util.AddUseWp( "PLANT_Back_door", "door_inside" );
	Util.AddUseWp( "PLANT_Back_door", "door_outside" );

	SetGoalPriority( Map.Plant_Relic_Box, 0.8 );
	SetGoalPriority( Map.Plant_Front_door, 0.82 );
	SetGoalPriority( Map.Plant_Back_door, 0.81 );
	SetGoalPriority( Map.Plant_Command_Post, 0.75 );
	SetGoalPriority( Map.Build_Command_Post, 0.75 );
	SetGoalPriority( "DEFUSE_Rocket.*", 1.2, TEAM.AXIS, CLASS.ENGINEER, true );
	SetGoalPriority( Map.Plant_Prison_gate, 0.65, TEAM.ALLIES, CLASS.ENGINEER );

	SetAvailableMapGoals( TEAM.ALLIES, true, Map.Plant_Back_door );
	SetAvailableMapGoals( TEAM.AXIS, false, {
		Map.Plant_Back_door,
		Map.Build_Relic_Box,
	});
	SetAvailableMapGoals( TEAM.ALLIES, false, {
		Map.Flag_relic,
		Map.Plant_Prison_gate,
		Map.Plant_Rocket,
	});

	SetAvailableMapGoals( TEAM.AXIS, false, {
		"ATTACK_.*",
		"DEFEND_.*",
		"SNIPE_.*",
	});

	SetAvailableMapGoals( TEAM.ALLIES, false, {
		"ATTACK_.*",
		"DEFEND_.*",
		"SNIPE_.*",
	});

	SetAvailableMapGoals( TEAM.ALLIES, true, "SNIPE_allies_relic.*" );
	SetAvailableMapGoals( TEAM.AXIS, true, "SNIPE_axis_relic.*" );

	// set max users for goals
	Util.SetMaxUsersInProgress( 1, "GRENADE_.*" );
	Util.SetMaxUsersInProgress( 1, "BUILD.*" );
	Util.SetMaxUsersInProgress( 1, "PLANT.*" );
	Util.SetMaxUsersInProgress( 1, "DEFEND.*" );
	Util.SetMaxUsersInProgress( 1, "ATTACK.*" );
	Util.SetMaxUsersInProgress( 3, "FLAG_.*" );

	// Routes
	MapRoutes =
	{
		PLANT_Relic_Box =
		{
			ROUTE_allied_spawn =
			{
				ROUTE_as_right = {},
				ROUTE_as_left =
				{
					ROUTE_door_left = {},
					ROUTE_door_right = {}
				},
			}
		},
		PLANT_Front_door =
		{
			ROUTE_allied_spawn =
			{
				ROUTE_as_right = {},
				ROUTE_as_left =
				{
					ROUTE_door_left = {},
					ROUTE_door_right = {}
				},
			}
		},
		FLAG_relic =
		{
			ROUTE_allied_spawn =
			{
				ROUTE_ladder = {},
				ROUTE_as_right = {},
				ROUTE_as_left =
				{
					ROUTE_door_left = {},
					ROUTE_door_right = {}
				},
			}
		},
		CAPPOINT_relic =
		{
			ROUTE_box =
			{
				ROUTE_stairs1 = {},
				ROUTE_stairs2 = {},
				ROUTE_jump = {},
				ROUTE_jump2 = {},
				ROUTE_balcony = {},
			},
		},
	};
	MapRoutes.ATTACK_cappoint1 = MapRoutes.CAPPOINT_relic;
	MapRoutes.ATTACK_cappoint2 = MapRoutes.CAPPOINT_relic;
	MapRoutes.ATTACK_cappoint3 = MapRoutes.CAPPOINT_relic;
	Util.Routes(MapRoutes);

	Util.MapDebugPrint( "OnMapLoad" );
};
