//==========================================================================================
//
//	al_kad_b3.gm
//
//	Who			When		What
//------------------------------------------------------------------------------------------
//	d00d			05/17/08	Original script al_kad_b2.gm
//	jaskot			02/03/09	Converted old script to new format
//	d00d			11/13/09	Updated for Omni-bot 0.8
//	d00d			12/05/10	Updated for 0.81/b3 version of the map
//	d00d			02/10/13	Added some attack goals
//
//==========================================================================================
//

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",

	Debug = false,
	DefendingTeam = TEAM.AXIS,
	Talk = true,

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

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

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

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

	Front_door_Destroyed = function( trigger )
	{
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_frontdoor.*" );
		SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_relic.*" );
		Util.MapDebugPrint( "Front_door_Destroyed" );
	},

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

	Relic_Box_Planted = function( trigger )
	{
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_frontdoor.*" );
		SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_relic.*" );

		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 );
						break;
					}
					else if ( ran > 6 )
					{
						sleep( 0.5 );
						bot.SayVoice( VOICE.DISARM_DYNAMITE );
						break;
					}
				}
			}
		}

		Util.MapDebugPrint( "Relic_Box_Planted" );
	},

	Relic_Box_Destroyed = function( trigger )
	{
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_frontdoor.*" );
		SetAvailableMapGoals( TEAM.ALLIES, true, {
			"ATTACK_relic.*",
			Map.Flag_relic,
		});

		Util.MapDebugPrint( "Relic_Box_Destroyed" );
	},

	Rocket_Planted = function( trigger )
	{
		SetAvailableMapGoals( TEAM.ALLIES, false, {
			"ATTACK_relic.*",
			"ATTACK_frontdoor.*",
		});
		SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_rocket.*" );

		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 );
						break;
					}
					else if ( ran > 6 )
					{
						sleep( 0.7 );
						bot.SayVoice( VOICE.DISARM_DYNAMITE );
						break;
					}
				}
			}
		}

		Util.MapDebugPrint( "Rocket_Planted" );
	},

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

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

		Util.MapDebugPrint( "relic_Taken" );
	},

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

		iw1 = OnTriggerRegion( AABB(5081.272,-5190.540,29.721,5281.996,-5060.853,203.810), RegionTrigger.IntrusionWarning );
		iw2 = OnTriggerRegion( AABB(5120.875,-6170.852,26.119,5337.876,-6095.651,213.138), RegionTrigger.IntrusionWarning );
		iw3 = OnTriggerRegion( AABB(5136.401,-5292.614,39.392,5250.774,-5225.107,232.702), RegionTrigger.IntrusionWarning );

		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( trigger )
	{
		SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_relic.*" );
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_cappoint.*" );
		SetAvailableMapGoals( TEAM.AXIS, false, "ATTACK_cappoint.*" );
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_relic_axis.*" );

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

global OnMapLoad = function()
{
	/*** REGISTER CALLBACK FUNCTIONS ***/
	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( "GRENADE_back_door Exploded.", Map.Back_door_Destroyed );
	OnTrigger( "Axis team has destroyed the Allied Command Post!", Map.Command_Post_Destroyed );
	OnTrigger( "GRENADE_front_door Exploded.", 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 relic!", Map.relic_Returned );

	/*** GOAL POSITIONS/USE POINTS ***/
	Util.SetGoalOffset( 10, 0, 0, Map.Plant_Relic_Box );
	Util.SetGoalOffset( 0, 0, -50, Map.Build_Prison_gate );
	Util.SetGoalOffset( -10, 0, 0, Map.Plant_Front_door );
	Util.AddUsePoint( Map.Plant_Back_door, Vec3(4346,-2280,40) );
	Util.AddUsePoint( Map.Plant_Back_door, Vec3(4346,-2400,30) );

	/*** REMOVE BOGUS BUILD GOAL ***/
	Util.RemoveGoal( Map.Build_Relic_Box );

	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 );

	/*** INITIAL GOAL AVAILABILITY ***/
	SetAvailableMapGoals( TEAM.ALLIES, false, {
		Map.Flag_relic,
		Map.Plant_Prison_gate,
		Map.Plant_Rocket,
		"ATTACK_.*",
		"DEFEND_.*",
		"SNIPE_.*",
	});
	SetAvailableMapGoals( TEAM.ALLIES, true, {
		Map.Plant_Back_door,
		"ATTACK_frontdoor.*",
	});

	SetAvailableMapGoals( TEAM.AXIS, false, {
		Map.Build_Prison_gate,
		Map.Plant_Back_door,
		"ATTACK_.*",
		"DEFEND_.*",
		"SNIPE_.*",
	});
	SetAvailableMapGoals( TEAM.AXIS, true, {
		"SNIPE_axis_relic.*",
		"DEFEND_relic_axis.*",
	});

	/*** SET MAX USERS FOR GOALS ***/
	Util.SetMaxUsers( 1, "GRENADE_.*" );
	Util.SetMaxUsers( 1, "BUILD_.*" );
	Util.SetMaxUsers( 1, "PLANT_.*" );
	Util.SetMaxUsers( 1, "DEFEND_.*" );
	Util.SetMaxUsers( 1, "ATTACK.*" );
	Util.SetMaxUsers( 3, "FLAG_.*" );
	Util.SetMaxUsers( 3, "FLAGRETURN_.*" );

	/*** REGION TRIGGERS ***/
	iw4 = OnTriggerRegion( AABB(3976.287,-2802.724,204.068,4161.330,-2657.157,359.478), RegionTrigger.IntrusionWarning );
	iw5 = OnTriggerRegion( AABB(4496.825,-2966.977,217.758,4599.867,-2634.540,346.658), RegionTrigger.IntrusionWarning );

	/*** ROUTES ***/
	MapRoutes =
	{
		DEFEND_relic_axis =
		{
			ROUTE_axis_spawn =
			{
				ROUTE_axis_bottom = {},
				ROUTE_axis_center = {},
				ROUTE_axis_left = {},
				ROUTE_axis_right = {}
			}
		},
		DEFEND_axis_rocket2 =
		{
			ROUTE_axis_spawn =
			{
				ROUTE_axis_bottom =
				{
					ROUTE_rocket_left = {},
					ROUTE_rocket_right = {}
				},
				ROUTE_axis_right =
				{
					ROUTE_rocket_left = {},
					ROUTE_rocket_right = {}
				},
				ROUTE_axis_left =
				{
					ROUTE_rocket_left = {},
					ROUTE_rocket_right = {}
				},
				ROUTE_axis_center =
				{
					ROUTE_rocket_left = {},
					ROUTE_rocket_right = {}
				}
			}
		},
		PLANT_Front_door =
		{
			ROUTE_allied_spawn =
			{
				ROUTE_roof = {},
				ROUTE_as_right = {},
				ROUTE_as_left =
				{
					ROUTE_door_left = {},
					ROUTE_door_right = {}
				},
			}
		},
		PLANT_Rocket =
		{
			ROUTE_allied_spawn =
			{
				ROUTE_roof =
				{
					ROUTE_rocket_left = {},
					ROUTE_rocket_right = {}
				},
				ROUTE_as_right =
				{
					ROUTE_rocket_left = {},
					ROUTE_rocket_right = {}
				},
				ROUTE_as_left =
				{
					ROUTE_door_left =
					{
						ROUTE_rocket_left = {},
						ROUTE_rocket_right = {}
					},
					ROUTE_door_right =
					{
						ROUTE_rocket_left = {},
						ROUTE_rocket_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;
	MapRoutes.ATTACK_cappoint4 = MapRoutes.CAPPOINT_relic;
	MapRoutes.ATTACK_cappoint5 = MapRoutes.CAPPOINT_relic;
	MapRoutes.ATTACK_cappoint6 = MapRoutes.CAPPOINT_relic;
	MapRoutes.ATTACK_cappoint7 = MapRoutes.CAPPOINT_relic;
	MapRoutes.ATTACK_rocket = MapRoutes.PLANT_Rocket;
	MapRoutes.ATTACK_rocket_1 = MapRoutes.PLANT_Rocket;
	MapRoutes.ATTACK_rocket_2 = MapRoutes.PLANT_Rocket;
	MapRoutes.ATTACK_rocket_3 = MapRoutes.PLANT_Rocket;
	MapRoutes.ATTACK_frontdoor = MapRoutes.PLANT_Front_door;
	MapRoutes.ATTACK_frontdoor_1 = MapRoutes.PLANT_Front_door;
	MapRoutes.ATTACK_frontdoor_2 = MapRoutes.PLANT_Front_door;
	MapRoutes.ATTACK_frontdoor_3 = MapRoutes.PLANT_Front_door;
	MapRoutes.ATTACK_frontdoor_4 = MapRoutes.PLANT_Front_door;
	MapRoutes.ATTACK_frontdoor_5 = MapRoutes.PLANT_Front_door;
	MapRoutes.PLANT_Relic_Box = MapRoutes.FLAG_relic;
	MapRoutes.ATTACK_relic = MapRoutes.FLAG_relic;
	MapRoutes.ATTACK_relic_1 = MapRoutes.FLAG_relic;
	MapRoutes.ATTACK_relic_2 = MapRoutes.FLAG_relic;
	MapRoutes.ATTACK_relic_3 = MapRoutes.FLAG_relic;
	MapRoutes.ATTACK_relic_4 = MapRoutes.FLAG_relic;
	MapRoutes.ATTACK_relic_5 = MapRoutes.FLAG_relic;
	MapRoutes.DEFEND_relic_axis_1 = MapRoutes.DEFEND_relic_axis;
	MapRoutes.DEFEND_relic_axis_2 = MapRoutes.DEFEND_relic_axis;
	MapRoutes.DEFEND_relic_axis_3 = MapRoutes.DEFEND_relic_axis;
	MapRoutes.DEFEND_relic_axis_4 = MapRoutes.DEFEND_relic_axis;
	MapRoutes.DEFEND_relic_axis_5 = MapRoutes.DEFEND_relic_axis;
	MapRoutes.DEFEND_relic_axis_6 = MapRoutes.DEFEND_relic_axis;
	MapRoutes.DEFEND_relic_axis_7 = MapRoutes.DEFEND_relic_axis;
	MapRoutes.DEFEND_relic_axis_8 = MapRoutes.DEFEND_relic_axis;
	MapRoutes.DEFEND_axis_rocket3 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket4 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket5 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket6 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket7 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket8 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket9 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket10 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket11 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket12 = MapRoutes.DEFEND_axis_rocket2;
	MapRoutes.DEFEND_axis_rocket13 = MapRoutes.DEFEND_axis_rocket2;
	Util.Routes( MapRoutes );

	Util.MapDebugPrint( "OnMapLoad" );
};

global OnBotJoin = function( bot )
{
};
