//==========================================================================================
//
//	well_d.gm
//
//	Who	When		What
//------------------------------------------------------------------------------------------
//	palota	26 September 2010	Initial Script
//
//==========================================================================================
//

global Map =
{
	bridgeBuilt = false,
	wallDestroyed = false,
	waterTaken = false,

	Switches =
	{
		gate =
		{
			Enabled = true,
			Priority = 0,
			Timeout = 4000,
			WaypointName = "gate_switch",
			AimPosition = Vector3(-7043.5, -816.6, 948.1),
		},
	},

	Roles =
	{
		AXIS =
		{
			DEFENDER1 =
			{
				numbots = 2,
			},
		},
	},

	SetObjective = function()
	{
		b = Map.bridgeBuilt || !Map.Switches.gate.Enabled;
		SetAvailableMapGoals( TEAM.ALLIES, b, "FLAG_objective" );
		SetAvailableMapGoals( TEAM.AXIS, b, ".*_cabinet_.*" );
		SetAvailableMapGoals( TEAM.ALLIES, b, ".*_cabinet_.*" );
		SetAvailableMapGoals( TEAM.ALLIES, Map.bridgeBuilt && Map.Switches.gate.Enabled, "SWITCH_gate_allied" );
	},

	SetGate2 = function()
	{
		sleep(5);
		closed = Map.Switches.gate.Enabled;
		SetAvailableMapGoals( TEAM.AXIS, !closed, "SWITCH_gate_axis" );
		SetAvailableMapGoals( TEAM.ALLIES, !closed, "ROUTE_gate" );
		Map.SetObjective();
		Util.MapDebugPrint( "SetGate2" );
	},

	SetGate = function(closed)
	{
		Map.Switches.gate.Enabled = closed;
		Wp.SetWaypointFlag("gate", "closed", closed);
		thread(Map.SetGate2);
	},

	bridge_Built = function()
	{
		Map.bridgeBuilt = true;
		SetGoalPriority("DEFEND_north.*", 0.5);
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_bridge.*" );
		Map.SetObjective();
		Util.MapDebugPrint( "bridge_Built" );
	},

	bridge_Destroyed = function()
	{
		Map.bridgeBuilt = false;
		SetGoalPriority("DEFEND_north.*", 0.51);
		SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_bridge.*" );
		Map.SetObjective();
		Util.MapDebugPrint( "bridge_Destroyed" );
	},

	bucket_Taken = function()
	{
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"DEFEND_north.*",
			"MOBILEMG42_north.*",
		});
		if (Map.wallDestroyed)
		{
			if (Map.waterTaken){
				SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_exit.*" );
			}else{
				SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_water.*" );
			}
		}
		Util.MapDebugPrint( "bucket_Taken" );
	},

	water_Taken = function()
	{
		Map.waterTaken = true;
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_water.*" );
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_exit.*" );
		SetAvailableMapGoals( TEAM.ALLIES, false, "CAPPOINT_water" );
		SetAvailableMapGoals( TEAM.ALLIES, true, "CAPPOINT_exit" );
		Util.MapDebugPrint( "water_Taken" );
	},

	objective_Returned = function()
	{
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"DEFEND_exit.*",
			"DEFEND_water.*",
		});
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_north.*" );
		if(Map.wallDestroyed){
			SetAvailableMapGoals( TEAM.AXIS, true, "MOBILEMG42_north.*" );
		}
		Util.MapDebugPrint( "objective_Returned" );
	},

	wall_Destroyed = function()
	{
		Map.wallDestroyed = true;
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"DEFEND_wall.*",
			"MOBILEMG42_wall.*",
		});
		SetAvailableMapGoals( TEAM.AXIS, true, "MOBILEMG42_north.*" );
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_wall.*" );
		SetAvailableMapGoals( TEAM.ALLIES, true, "CAPPOINT_water" );
		Util.SetMaxUsersInProgress( 9, "FLAG_objective" );
		Util.SetMaxUsersInProgress( 2, "PLANT_bridge" );
		Util.MapDebugPrint( "wall_Destroyed" );
	},

	gate_Activated = function()
	{
		if(GetTime() - Map.gateTime > 4){
			Map.SetGate(false);
			Util.MapDebugPrint( "gate_Activated" );
		}
	},

	gate_Deactivated = function()
	{
		Map.gateTime = GetTime();
		Map.SetGate(true);
		Util.MapDebugPrint( "gate_Deactivated" );
	},
};

global OnMapLoad = function()
{
	OnTrigger( "The Bridge has been constructed", Map.bridge_Built );
	OnTrigger( "Axis have destroyed the Bridge", Map.bridge_Destroyed );
	OnTrigger( "Allies have stolen The Objective!", Map.bucket_Taken );
	OnTrigger( "The Allies have got the water!!!", Map.water_Taken );
	OnTrigger( "Flag returned objective!", Map.objective_Returned );
	OnTrigger( "GRENADE_wall1 Exploded.", Map.wall_Destroyed );
	OnTrigger( "t41 activated", Map.gate_Activated );
	OnTrigger( "t44 activated", Map.gate_Deactivated );

	Util.DisableGoal( ".*", true );
	SetAvailableMapGoals( TEAM.AXIS, true, {
		"PLANT_bridge",
		"DEFEND_north.*",
		"DEFEND_center.*",
		"DEFEND_wall.*",
		"MOBILEMG42_wall.*",
		"TRIPMINE_x.*",
	});
	SetAvailableMapGoals( TEAM.ALLIES, true, {
		"BUILD_bridge",
		"GRENADE.*",
		"ATTACK.*",
	});

	SetGoalPriority("ATTACK_wall.*", 0.55);
	SetGoalPriority("ATTACK_bridge.*", 0.54);
	SetGoalPriority("DEFEND_water.*", 0.9);
	SetGoalPriority("DEFEND_exit.*", 0.85);
	SetGoalPriority("DEFEND_wall.*", 0.56);
	SetGoalPriority("DEFEND_north.*", 0.52);
	SetGoalPriority("DEFEND_center1", 0.52);
	SetGoalPriority("DEFEND_center4", 0.52);
	SetGoalPriority("DEFEND_center5", 0.52);
	SetGoalPriority("GRENADE_floor", 0.7);

	// Max users per goal
	Util.SetMaxUsers( 1, "DEFEND_.*" );
	Util.SetMaxUsersInProgress( 5, "DEFEND_exit.*" );
	Util.SetMaxUsersInProgress( 5, "DEFEND_water.*" );
	Util.SetMaxUsersInProgress( 1, "FLAG_objective" );
	Util.SetMaxUsersInProgress( 1, "PLANT_bridge" );

	// Camp times
	SetMapGoalProperties( "DEFEND_.*", {mincamptime=20, maxcamptime=90} );
	SetMapGoalProperties( "MOBILEMG42_.*", {MinCampTime=30, MaxCampTime=100} );

	Util.UpdateSwitchData();
	Map.SetGate(true);

	MapRoutes =
	{
		BUILD_bridge = {
			ROUTE_allied_spawn = {
				ROUTE_sewer1 = { Weight=2, ROUTE_sewer2 = {} },
				ROUTE_allies_north = { Weight=3 },
				ROUTE_axis_south = {},
			}
		},
		FLAG_objective = {
			ROUTE_allied_spawn = {
				ROUTE_sewer1 = { ROUTE_sewer2 = {} },
				ROUTE_allies_north = {},
				ROUTE_gate = {},
			}
		},
		DEFEND_water1 = {
			ROUTE_axis_spawn = {
				ROUTE_axis_north ={},
				ROUTE_axis_south ={},
			}
		},
		DEFEND_wall1 = {
			ROUTE_axis_spawn = {
				ROUTE_axis_north = { ROUTE_center1 = {} },
				ROUTE_axis_south = { ROUTE_center2 = {} },
			}
		},
	};
	MapRoutes.ATTACK_bridge1 = MapRoutes.BUILD_bridge;
	MapRoutes.ATTACK_bridge2 = MapRoutes.BUILD_bridge;
	MapRoutes.ATTACK_bridge3 = MapRoutes.BUILD_bridge;
	MapRoutes.DEFEND_water2 = MapRoutes.DEFEND_water1;
	MapRoutes.DEFEND_center1 = MapRoutes.DEFEND_water1;
	MapRoutes.DEFEND_center2 = MapRoutes.DEFEND_water1;
	MapRoutes.DEFEND_center3 = MapRoutes.DEFEND_water1;
	MapRoutes.DEFEND_center4 = MapRoutes.DEFEND_water1;
	MapRoutes.DEFEND_center5 = MapRoutes.DEFEND_water1;
	MapRoutes.DEFEND_center6 = MapRoutes.DEFEND_water1;
	MapRoutes.DEFEND_center7 = MapRoutes.DEFEND_water1;
	MapRoutes.DEFEND_center8 = MapRoutes.DEFEND_water1;
	MapRoutes.DEFEND_wall2 = MapRoutes.DEFEND_wall1;
	MapRoutes.DEFEND_wall3 = MapRoutes.DEFEND_wall1;
	MapRoutes.DEFEND_wall4 = MapRoutes.DEFEND_wall1;
	MapRoutes.DEFEND_wall5 = MapRoutes.DEFEND_wall1;
	Util.Routes(MapRoutes);

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

global OnBotJoin = function( bot )
{
	// cs: can we isolate this to a region? see RegionTrigger.BreakableDistance
	if (bot.GetTeam()==TEAM.ALLIES)
	{
		bot.TargetBreakableDist = 400;
	}
};
