global Map =
{
	Quiet = true,

	Checkpoint_ForwardSpawn_flag = "CHECKPOINT_ForwardSpawn_flag",
	Plant_Axis_Submarine = "PLANT_Axis_Submarine",
	Plant_Filtration_Door = "PLANT_Filtration_Door",
	Plant_Storage_Door = "PLANT_Storage_Door",
	Build_Allied_Command_Post = "BUILD_Allied_Command_Post",
	Build_Axis_Command_Post = "BUILD_Axis_Command_Post",
	Plant_Allied_Command_Post = "PLANT_Allied_Command_Post",
	Plant_Axis_Command_Post = "PLANT_Axis_Command_Post",

	FocusPrimary = false,
	StorageStatus = false,
	FiltStatus = false,
	SubDyno = 0,

	Axis_Submarine_Destroyed = function( trigger )
	{
		Map.SubDyno = 0;
	},

	Submarine_Planted = function( trigger )
	{
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND.*" );
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_rSub[345678]" );
		SetAvailableMapGoals( TEAM.AXIS, false, Map.Checkpoint_ForwardSpawn_flag );

		Map.SubDyno += 1;
	},

	Submarine_Defused = function( trigger )
	{
		Map.SubDyno -= 1;
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND.*" );
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_rSub.*" );
	},

	Filtration_Door_Destroyed = function( trigger )
	{
		Map.FiltStatus = true;

		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_rFlag.*" );
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_rSub.*" );
		SetAvailableMapGoals( TEAM.AXIS, false, Map.Checkpoint_ForwardSpawn_flag );

		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_eng_storage" );

		if ( Map.FocusPrimary || Map.StorageStatus )
		{
			SetAvailableMapGoals( TEAM.ALLIES, true, Map.Plant_Axis_Submarine );
		}
	},

	Filtration_Door_Planted = function( trigger )
	{
		//don't bother defusing this
		Util.DisableGoal("DEFUSE_Filtration_.*");

		//open up the route nodes for the back way from flag
		Util.EnableGroup("backroutes");

		if ( Map.StorageStatus )
			{ SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_eng_.*" ); }
	},

	Storage_Door_Destroyed = function( trigger )
	{
		Map.StorageStatus = true;

		if ( Map.FocusPrimary || Map.FiltStatus )
		{
			SetAvailableMapGoals( TEAM.ALLIES, true, Map.Plant_Axis_Submarine );
		}

		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_eng_filtration" );
	},

	Storage_Door_Planted = function( trigger )
	{
		//don't bother defusing this
		Util.DisableGoal("DEFUSE_Storage_.*");

		if ( Map.FiltStatus )
			{ SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_eng_.*" ); }
	},

	flag_respawn_Axis_Captured = function( trigger )
	{

                 SetAvailableMapGoals( TEAM.ALLIES, false, "BUILD_Allied_Command_Post" );
	},

	flag_respawn_Allies_Captured = function( trigger )
	{
                SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Allied_Command_Post" );

                if ( !Map.FiltStatus )
                        { SetAvailableMapGoals( TEAM.AXIS, true, Map.Checkpoint_ForwardSpawn_flag ); }
	},

	two_minute = function( trigger )
	{

		//time is low, so start focusing on main obj
		Map.FocusPrimary = true;
	},

	thirty_second = function( trigger )
	{

		if ( Map.SybDyno > 1 ) { return; }

		foreach ( id and bot in BotTable )
		{
			if (RandInt(0,10) < 4)
			{
				bot.SayVoice(VOICE.G_GOODGAME);
			}
			sleep(1.0);
		}
	},
	Sub =
	{
		Name = "Sub",
		TriggerOnClass = CLASS.ENGINEER,
		OnEnter = function(ent)
		{


			if( GetEntTeam(ent) == TEAM.ALLIES )
			{
				SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_.*" );
				SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_rSub[345678]" );
				foreach ( id and bot in BotTable )
				{
					if ( bot.GetTeam() == TEAM.AXIS )
					{
						bot.SayTeam("Allies are planting!");
						break;
					}
				}
				sleep(15);
				SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_.*" );
				SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_rSub.*" );
			}
		},

	},
};

global OnMapLoad = function()
{
	Util.SetGoalPosition( 974, 2559, 386, "PLANT_Axis_Submarine" );

	tOne = OnTriggerRegion(AABB(838.000,1005.000,282.000,1082.000,3185.000,415.000), Map.Sub);

	Util.AddUseWp("PLANT_Axis_Submarine", "frontsub");
	Util.AddUseWp("PLANT_Axis_Submarine", "midsub");
	Util.AddUseWp("PLANT_Axis_Submarine", "backsub");
	Util.AddUseWp("PLANT_Storage_Door", "insidestoragedoor");
	Util.AddUseWp("PLANT_Storage_Door", "outsidestoragedoor");

	// close up the 9 waypoints used in checkpoint mode
	for ( i = 1; i < 15; i = i + 1 )
	{
		s = "cpspawn" + i;
		Wp.SetWaypointFlag( s, "closed", true );
	}


	OnTrigger( "the Axis Submarine Destroyed.", Map.Axis_Submarine_Destroyed );
	OnTrigger( "Planted at the Axis Submarine.", Map.Submarine_Planted );
	OnTrigger( "Defused at the Axis Submarine.", Map.Submarine_Defused );
	OnTrigger( "Filtration Door Destroyed.", Map.Filtration_Door_Destroyed );
	OnTrigger( "Planted at Filtration Door.", Map.Filtration_Door_Planted );
	OnTrigger( "Storage Door Destroyed.", Map.Storage_Door_Destroyed );
	OnTrigger( "Planted at Storage Door.", Map.Storage_Door_Planted );
	OnTrigger( "Axis reclaims the Central Access Room!", Map.flag_respawn_Axis_Captured );
	OnTrigger( "Allies capture the Central Access Room!", Map.flag_respawn_Allies_Captured );
	OnTrigger( "two minute warning.", Map.two_minute );
	OnTrigger( "thirty second warning.", Map.thirty_second );

	SetMapGoalProperties( "ATTACK_.*", {MinCampTime=15, MaxCampTime=30});
	SetMapGoalProperties( "DEFEND_.*", {MinCampTime=30, MaxCampTime=60});
	SetMapGoalProperties( "PANZER_.*", {MinCampTime=30, MaxCampTime=60});

	Util.SetMaxUsers( 1, "PLANT.*" );
	Util.SetMaxUsers( 1, "CAMP.*" );
	Util.SetMaxUsers( 1, "ATTACK.*" );
	Util.SetMaxUsers( 1, "DEFEND.*" );

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

	rtGrp =
	{
		"ROUTE_centralaccess",
		"ROUTE_mainstorage",
		"ROUTE_filtration",
	};

	SetGoalGroup( rtGrp, "backroutes");
	Util.DisableGroup("backroutes");

	ETUtil.SetPrimaryGoals(1.0);

	//eng camp spots to wait for recharge
	SetGoalPriority( "ATTACK_eng.*", 0.91, TEAM.ALLIES, CLASS.ENGINEER );
	Util.LimitToClass( "ATTACK_eng.*", TEAM.ALLIES, CLASS.ENGINEER );
	SetGoalPriority( "PLANT_Storage_Door", 0.93 );
	Util.LimitToClass( "PLANT_Axis_Command_Post.*", TEAM.ALLIES, CLASS.COVERTOPS);
	Util.LimitToClass( "PLANT_Allied_Command_Post.*", TEAM.AXIS, CLASS.COVERTOPS);

	SetAvailableMapGoals( TEAM.AXIS, true, {
		"DEFEND_rFlag.*",
		"BUILD_Axis_Command_Post",
	});

        SetAvailableMapGoals( TEAM.ALLIES, true, {
        	"ATTACK_eng.*",
        	"ATTACK_bSub.*",
        	Map.Checkpoint_ForwardSpawn_flag,
        	Map.Plant_Filtration_Door,
        	Map.Plant_Storage_Door,
        });

	MapRoutes =
	{
		PLANT_Axis_Submarine =
		{
			ROUTE_flagspawn =
			{
				ROUTE_chamber3 =
				{
					Weight = 4,
					ROUTE_stairs = { ROUTE_storage = {}, },
					ROUTE_front = {},
				},
				ROUTE_water = {},
				ROUTE_centralaccess =
				{
					ROUTE_mainstorage = { ROUTE_filtration = {}, },
				},
			},
			ROUTE_bSpawn1 =
			{
				ROUTE_chamber3 =
				{
					ROUTE_stairs = { ROUTE_storage = {}, },
					ROUTE_front = {},
				},
				ROUTE_mainstorage = { ROUTE_filtration = {}, },
			},
		},
	};

	MapRoutes.ATTACK_bSub1 = MapRoutes.PLANT_Axis_Submarine;
	MapRoutes.ATTACK_bSub2 = MapRoutes.PLANT_Axis_Submarine;
	MapRoutes.ATTACK_bSub3 = MapRoutes.PLANT_Axis_Submarine;
	MapRoutes.ATTACK_bSub4 = MapRoutes.PLANT_Axis_Submarine;
	MapRoutes.ATTACK_bSub5 = MapRoutes.PLANT_Axis_Submarine;
	MapRoutes.ATTACK_bSub6 = MapRoutes.PLANT_Axis_Submarine;
	MapRoutes.ATTACK_bSub7 = MapRoutes.PLANT_Axis_Submarine;
	MapRoutes.PLANT_Axis_Submarine.ROUTE_bSpawn2 = MapRoutes.PLANT_Axis_Submarine.ROUTE_bSpawn1;
	MapRoutes.PLANT_Axis_Submarine.ROUTE_bSpawn3 = MapRoutes.PLANT_Axis_Submarine.ROUTE_bSpawn1;

	Util.Routes(MapRoutes);
	Util.MapDebugPrint( "OnMapLoad" );
};

global OnBotJoin = function( bot )
{
	bot.TargetBreakableDist = 80.0;
};
