//==========================================================================================
//
//	ctf_halo.gm
//
//	Who	When		What
//------------------------------------------------------------------------------------------
//	palota	22 November 2011	routes, RideMovers
//
//==========================================================================================
//

global Map =
{
	Movers = { "MOVER_cablecar", "MOVER_cablecar2", "MOVER_cablecar3", "MOVER_cablecar4", "MOVER_cablecar5", "MOVER_cablecar6" },
	SwitchPos = { Vec3(-36.125, 3998.294, 24.595), Vec3(40.125, -3998.634, 32.843), Vec3(-2089.445, -2649.570, 697.125), Vec3(2088.703, 2598.204, 697.125), Vec3(-108.125, 3670.232, 288.961), Vec3(71.986, -3663.445, 289.125) },
	CarPos = {},
	RideMovers = {},

	CarIndexFromEnt = function(ent)
	{
		entName = GetEntName(ent).Right(8);
		foreach(id and name in Map.Movers){
			if(name.Right(8)==entName){
				return id;
			}
		}
	},
};

global OnMapLoad = function()
{
	for(i=0; i<6; i+=1)
	{
		Map.RideMovers["car" + i] = {
			carIndex = i,
			movergoal = Map.Movers[i],
			rideposition = Vector3(0,0,10),
			startwp = "car" + i,
			exitwp = "car" + i + "_out",
			board = function(_this)
			{
				if (Map.CarPos[this.carIndex] != 1){ _this.Finished(); }
				return true;
			},
			leave = function(_this)
			{
				index = this.carIndex;
				if (Map.CarPos[index] == 1)
				{
					Util.MapDebugPrint("board cablecar" + index, true);
					dowhile(Map.CarPos[index] == 1)
					{
						_this.AddAimRequest(Priority.High, "position", Map.SwitchPos[index]);
						_this.Bot.MoveTowards(_this.Pos);
						sleep(0.3);
						_this.Bot.PressButton(BTN.USE);
						sleep(1);
					}
					_this.ReleaseAimRequest();
				}
				if (Map.CarPos[index] != 2){ return false; }
				Util.MapDebugPrint("leave cablecar" + index, true);
				return true;
			},
		};

		trig = "Car" + i + "_Enter";
		Map[trig] =
		{
			carIndex = i,
			Name = trig,
			TriggerOnEntity = GetGoal(Map.Movers[i]).GetEntity(),
			OnEnter = function(ent) {
				i = Map.CarIndexFromEnt(ent);
				Wp.SetWaypointFlag("car" + i, "closed", false);
				sleep(1);
				Map.CarPos[i] = 1;
			},
			OnExit = function(ent) {
				i = Map.CarIndexFromEnt(ent);
				Map.CarPos[i] = -1;
				sleep(1);
				Wp.SetWaypointFlag("car" + i, "closed", true);
			},
		};

		trig = "Car" + i + "_Exit";
		Map[trig] =
		{
			carIndex = i,
			Name = trig,
			TriggerOnEntity = GetGoal(Map.Movers[i]).GetEntity(),
			OnEnter = function(ent) {
				sleep(0.7);
				Map.CarPos[Map.CarIndexFromEnt(ent)] = 2;
			},
			OnExit = function(ent) {
				Map.CarPos[Map.CarIndexFromEnt(ent)] = -1;
			},
		};
		Wp.SetWaypointFlag("car" + i, "closed", false);
		Map.CarPos[i] = 1;
	}//end for

	OnTriggerRegion(AABB(-131.703,3903.125,29.362,-60.128,4032.875,103.362),Map.Car0_Enter);
	OnTriggerRegion(AABB(-133.448,-4032.873,25.015,-74.512,-3903.127,99.125),Map.Car0_Exit);
	OnTriggerRegion(AABB(54.145,-4032.875,29.125,134.476,-3916.392,103.125),Map.Car1_Enter);
	OnTriggerRegion(AABB(41.340,3932.667,25.015,133.144,4022.659,99.015),Map.Car1_Exit);
	OnTriggerRegion(AABB(-2066.366,-2664.875,689.125,-1991.524,-2568.026,763.125),Map.Car2_Enter);
	OnTriggerRegion(AABB(-2071.219,2534.910,689.125,-2004.663,2621.185,763.125),Map.Car2_Exit);
	OnTriggerRegion(AABB(2001.904,2503.125,693.125,2061.722,2608.794,767.125),Map.Car3_Enter);
	OnTriggerRegion(AABB(1986.609,-2616.483,689.125,2066.252,-2527.928,763.125),Map.Car3_Exit);
	OnTriggerRegion(AABB(-210.626,3575.125,281.125,-125.801,3704.875,355.125),Map.Car4_Enter);
	OnTriggerRegion(AABB(-214.822,-3696.875,281.133,-128.728,-3583.336,355.133),Map.Car4_Exit);
	OnTriggerRegion(AABB(88.641,-3696.874,285.314,166.410,-3570.754,359.314),Map.Car5_Enter);
	OnTriggerRegion(AABB(94.413,3575.125,281.213,193.623,3704.875,355.213),Map.Car5_Exit);

	SetGoalPriority( "SNIPE_.*", 0.82);
	Util.SetMaxUsersInProgress(2, ".*Command_Post");

	MapRoutes =
	{
		FLAG_allies_gold_crate = {
			ROUTE_N_spawn = {
				ROUTE_NE_side1 = { Weight=2 },
				ROUTE_NW_side1 = { Weight=2 },
				ROUTE_NE_side2 = {},
				ROUTE_NW_side2 = {},
				ROUTE_E_center1 = {},
				ROUTE_E_center2 = {},
				ROUTE_W_center1 = {},
				ROUTE_W_center2 = {},
				ROUTE_NE_down1 = {},
				ROUTE_NE_down2 = {},
				ROUTE_NW_down1 = {},
				ROUTE_NW_down2 = {},
			}
		},
		FLAG_axis_gold_crate = {
			ROUTE_S_spawn = {
				ROUTE_SE_side1 = { Weight=2 },
				ROUTE_SW_side1 = { Weight=2 },
				ROUTE_SE_side2 = {},
				ROUTE_SW_side2 = {},
				ROUTE_E_center1 = {},
				ROUTE_E_center2 = {},
				ROUTE_W_center1 = {},
				ROUTE_W_center2 = {},
				ROUTE_SE_down1 = {},
				ROUTE_SE_down2 = {},
				ROUTE_SW_down1 = {},
				ROUTE_SW_down2 = {},
			}
		},
		CAPPOINT_N = {
			ROUTE_S_gold = {
				ROUTE_SE_gold_down = {
					ROUTE_SE_down1 = {},
					ROUTE_SE_down2 = {},
				},
				ROUTE_SW_gold_down = {
					ROUTE_SW_down1 = {},
					ROUTE_SW_down2 = {},
				},
				ROUTE_E_bunker = { Weight=2 },
				ROUTE_W_bunker = { Weight=2 },
			}
		},
		CAPPOINT_S = {
			ROUTE_N_gold = {
				ROUTE_NE_gold_down = {
					ROUTE_NE_down1 = {},
					ROUTE_NE_down2 = {},
				},
				ROUTE_NW_gold_down = {
					ROUTE_NW_down1 = {},
					ROUTE_NW_down2 = {},
				},
				ROUTE_E_bunker = { Weight=2 },
				ROUTE_W_bunker = { Weight=2 },
			}
		},
		BUILD_Command_Post = {
			ROUTE_N_spawn = {
				ROUTE_NE_down1 = {},
				ROUTE_NE_down2 = {},
				ROUTE_NW_down1 = {},
				ROUTE_NW_down2 = {},
			},
			ROUTE_S_spawn = {
				ROUTE_SE_down1 = {},
				ROUTE_SE_down2 = {},
				ROUTE_SW_down1 = {},
				ROUTE_SW_down2 = {},
			},
		},
	};
	MapRoutes.PLANT_Command_Post = MapRoutes.BUILD_Command_Post;
	Util.Routes(MapRoutes);

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