//==========================================================================================
//
//	sfx_endor.gm
//
//	Who	When		What
//------------------------------------------------------------------------------------------
//	palota	31 March 2012	Initial Script
//
//==========================================================================================
//

global Map =
{
	Debug = 0,
	entranceDestroyed = false,
	generatorDestroyed = false,
	flagAllied = false,

	Roles =
	{
		ALLIES =
		{
			ATTACKER2 =
			{
				numbots = 3,
			},
		},
	},

	Allied_Out =
	{
		Name = "Allied_Out",
		TriggerOnClass = CLASS.ANYPLAYER,
		OnEnter = function(ent)
		{
			if (GetEntTeam(ent)==TEAM.ALLIES)
			{
				bot = Util.IsBot(ent);
				if(bot){
					bot.ClearRoles(ROLE.ATTACKER3);
				}
			}
		},
	},

	Allied_In =
	{
		Name = "Allied_In",
		TriggerOnClass = CLASS.ANYPLAYER,
		OnEnter = function(ent)
		{
			if (GetEntTeam(ent)==TEAM.ALLIES)
			{
				Map.numEngineerAtRocket += 1;
				bot = Util.IsBot(ent);
				if(bot){
					bot.SetRoles(ROLE.ATTACKER3);
				}
			}
		},
	},

	SetSpawn1 = function(bot)
	{
		if (bot.GetTeam()==TEAM.AXIS) {
			bot.ChangeSpawnPoint(RandInt(1,2));
		}
	},

	SetSpawn = function()
	{
		foreach(bot in BotTable)
		{
			Map.SetSpawn1(bot);
		}
	},

	SetGoals = function()
	{
		tbl = { "PLANT_Backup_Generator", "PLANT_Shield_Generator", "CHECKPOINT_westbunker_flag", "ATTACK_inside.*", "ATTACK_backup.*" };
		if (Map.entranceDestroyed){
			Util.ClearRoleForTable(tbl, {ROLE.ATTACKER3, ROLE.INFILTRATOR});
		}else{
			Util.SetRoleForTable(tbl, {ROLE.ATTACKER3, ROLE.INFILTRATOR});
		}
		if (Map.flagAllied){
			Util.ClearRoleForGoals( "CHECKPOINT_westbunker_flag", {ROLE.ATTACKER3, ROLE.INFILTRATOR});
		}
		b = !Map.flagAllied;
		SetAvailableMapGoals( TEAM.AXIS, b, {
			"DEFEND_entrance.*",
			"MOBILEMG42_entrance.*",
			"SNIPE_x.*",
		});
		SetAvailableMapGoals( TEAM.AXIS, !b, "SNIPE_backup.*");
		SetAvailableMapGoals( TEAM.ALLIES, !b, "DEFEND_flag.*");
		b = !Map.entranceDestroyed;
		SetAvailableMapGoals( TEAM.ALLIES, b, {
			"ATTACK_entrance.*",
			"ATTACK_platform.*",
		});
	},

	Entrance_Door_Destroyed = function( trigger )
	{
		Map.entranceDestroyed = true;
		sleep(1);
		Map.SetGoals();
		Util.EnableGoal(".*CAB_eastbunker_.*cabinet");
		SetAvailableMapGoals( TEAM.AXIS, true, {
			"DEFEND_backup.*",
			"DEFEND_shield.*",
		});
		Util.MapDebugPrint( "Entrance_Door_Destroyed" );
	},

	Entrance_Door_Built = function( trigger )
	{
		Map.entranceDestroyed = false;
		Map.SetGoals();
		Util.MapDebugPrint( "Entrance_Door_Built" );
	},

	westbunker_flag_Allies_Captured = function( trigger )
	{
		Map.flagAllied = true;
		Map.SetGoals();
		SetGoalPriority("PLANT_Entrance_Door", 0.81);
		SetAvailableMapGoals( TEAM.AXIS, false, "BUILD_Entrance_Door" );
		SetAvailableMapGoals( TEAM.ALLIES, true, {
			".*_Charge_Booster",
			"ATTACK_backup.*",
		});
		Util.SetMaxUsersInProgress(4, "CHECKPOINT_westbunker_flag");
		Util.MapDebugPrint( "westbunker_flag_Allies_Captured" );
	},

	westbunker_flag_Axis_Captured = function( trigger )
	{
		Map.flagAllied = false;
		Map.SetGoals();
		SetAvailableMapGoals( TEAM.AXIS, true, "BUILD_Entrance_Door" );
		SetAvailableMapGoals( TEAM.ALLIES, false, ".*_Charge_Booster" );
		SetGoalPriority("PLANT_Entrance_Door", 0.85);
		Util.SetMaxUsersInProgress(10, "CHECKPOINT_westbunker_flag");
		Map.SetSpawn();
		Util.MapDebugPrint( "westbunker_flag_Axis_Captured" );
	},

	Backup_Generator_Destroyed = function( trigger )
	{
		Map.generatorDestroyed = true;
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_shield.*" );
		sleep(12);
		if(Map.generatorDestroyed){
			SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_inside.*" );
			SetAvailableMapGoals( TEAM.ALLIES, true, {
				"ATTACK_inside.*",
				"PLANT_Shield_Generator",
			});
			Util.MapDebugPrint( "Backup_Generator_Destroyed" );
		}
	},

	Backup_Generator_Built = function( trigger )
	{
		Map.generatorDestroyed = false;
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"DEFEND_inside.*",
			"DEFEND_shield.*",
		});
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_inside.*" );
		Util.MapDebugPrint( "Backup_Generator_Built" );
		sleep(5);
		SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_Shield_Generator" );
	},

	Shield_Generator_Destroyed = function( trigger )
	{
		ETUtil.WinningChat( TEAM.ALLIES );
		ETUtil.LosingChat( TEAM.AXIS );
		Util.MapDebugPrint( "Shield_Generator_Destroyed" );
	},
};

global OnMapLoad = function()
{
	OnTrigger( "^2Troopers ^7have destroyed the Entrance Door.", Map.Entrance_Door_Destroyed );
	OnTrigger( "The Entrance Door has been constructed.", Map.Entrance_Door_Built );
	OnTrigger( "^2Troopers ^7capture the First Sas!", Map.westbunker_flag_Allies_Captured );
	OnTrigger( "^1Raiders ^7capture the First Sas!", Map.westbunker_flag_Axis_Captured );
	OnTrigger( "Backup Generator destroyed - opening Shield Generator Door", Map.Backup_Generator_Destroyed );
	OnTrigger( "Raiders have constructed the Backup Generator!", Map.Backup_Generator_Built );
	OnTrigger( "^2Troopers ^7have destroyed the Shield Generator!", Map.Shield_Generator_Destroyed );

	OnTriggerRegion(AABB(2088.277,-3231.430,289.868,2957.664,-2563.592,592.559),Map.Allied_Out);
	OnTriggerRegion(AABB(-1240.345,-3157.072,93.209,-983.595,-2113.338,330.605),Map.Allied_Out);
	OnTriggerRegion(AABB(-873.679,-3150.716,153.566,553.674,-2483.125,350.350),Map.Allied_In);

	Util.DisableGoal( ".*", true );
	Util.EnableGoal(".*CAB_mainbunker_.*cabinet");
	Util.EnableGoal("CHECKPOINT_westbunker_flag");

	Map.SetGoals();

	SetAvailableMapGoals( TEAM.AXIS, true, {
		"BUILD_.*",
		"PLANT_Charge_Booster",
		"PLANTMINE_entrance.*",
		"TRIPMINE_x.*",
		"DEFEND_backup.*",
	});
	SetAvailableMapGoals( TEAM.ALLIES, true, {
		"PLANT_Entrance_Door",
		"PLANT_Backup_Generator",
	});

	SetGoalPriority("BUILD_Backup_Generator", 0.95);
	SetGoalPriority("BUILD_Entrance_Door", 0.93);
	SetGoalPriority("PLANT_Shield_Generator", 0.95);
	SetGoalPriority("PLANT_Entrance_Door", 0.85);
	SetGoalPriority("PLANTMINE_.*", 0.7);
	SetGoalPriority("DEFEND_inside.", 0.71);
	SetGoalPriority("ATTACK_inside.", 0.6);
	SetGoalPriority("DEFEND_backup.*", 0.45);

	Util.SetRoleForTable({ "ATTACK_platform.*", "DEFEND_flag.*"}, ROLE.ATTACKER2);

	Util.SetMaxUsers( 1, "BUILD_Charge_Booster" );

	SetMapGoalProperties("DEFEND_.*", {mincamptime=15, maxcamptime=40} );
	SetMapGoalProperties("SNIPE_.*", {MinCampTime=20, MaxCampTime=60} );
	SetMapGoalProperties("MOBILEMG42_.*", {MinCampTime=15, MaxCampTime=60} );

	Util.AddUsePoint("PLANT_Entrance_Door", {Vec3(-981.077, -2816.229, 192.125), Vec3(-899.249, -2822.478, 200.125)});

	MapRoutes =
	{
		PLANT_Entrance_Door = {
			ROUTE_allied_spawn = {
				ROUTE_south = { Weight=2 },
				ROUTE_center1 = {},
				ROUTE_center2 = {},
				ROUTE_center3 = {},
			}
		},
		PLANT_Backup_Generator = {
			ROUTE_allied_spawn = {
				ROUTE_shield_C = {
					ROUTE_backup_E = {},
					ROUTE_backup_W = {},
				},
				ROUTE_shield_E = {},
				ROUTE_shield_W = {},
			}
		},
		PLANT_Shield_Generator = {
			ROUTE_allied_spawn = {
				ROUTE_shield_C = {},
				ROUTE_shield_E = {},
				ROUTE_shield_W = {},
			}
		},
		CHECKPOINT_westbunker_flag = {
			ROUTE_axis_spawn = {
				ROUTE_axis_entrance = { Weight=2 },
				ROUTE_shield_C = {},
				ROUTE_shield_E = {},
				ROUTE_shield_W = {},
			},
		},
	};
	MapRoutes["ATTACK_platform.*"] = {
		ROUTE_allied_spawn = {
			ROUTE_teleport_east = { Weight=2 },
			ROUTE_center_teleport = {},
		}
	};
	MapRoutes["DEFEND_backup.*"] = {
		ROUTE_axis_spawn = {
			ROUTE_backup_E = {},
			ROUTE_backup_W = {},
		}
	};
	MapRoutes.PLANT_Shield_Generator.ROUTE_flag_spawn = MapRoutes.PLANT_Shield_Generator.ROUTE_allied_spawn;
	MapRoutes.PLANT_Backup_Generator.ROUTE_flag_spawn = MapRoutes.PLANT_Backup_Generator.ROUTE_allied_spawn;
	MapRoutes.CHECKPOINT_westbunker_flag.ROUTE_allied_spawn = MapRoutes.PLANT_Entrance_Door.ROUTE_allied_spawn;
	MapRoutes.BUILD_Backup_Generator = MapRoutes.PLANT_Backup_Generator;
	MapRoutes["ATTACK_entrance[123]"] = MapRoutes.PLANT_Entrance_Door;
	MapRoutes["ATTACK_backup.*"] = MapRoutes.PLANT_Backup_Generator;
	MapRoutes["ATTACK_inside.*"] = MapRoutes.PLANT_Shield_Generator;
	Util.Routes(MapRoutes);

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

global OnBotJoin = function( bot )
{
	Map.SetSpawn1(bot);
};
