// Mapname: Facility31_b1
// old waypoints and script file by: Quantum Blade
// updated by palota
global Map =
{
	Debug = 0,
	agentsTaken = 0,
	agentsSecured = 0,
	pitDrain = false,
	cargo = 1,
	agentsTakenTable = {},
	Movers = { "MOVER_cargo_rail", },

	Roles =
	{
		AXIS =
		{
			AllBots = true,
			DEFENDER1 = {
				numbots = 10
			},
			DEFENDER2 = {
				numbots = 10
			},
			DEFENDER3 = {
				numbots = 10
			},
		},
		ALLIES =
		{
			ATTACKER1 = {
				numbots = 2
			},
			ATTACKER2 = {
				numbots = 1,
				crucialClass = CLASS.COVERTOPS
			},
		},
	},

	NavL = function(_this, x,y,z, t)
	{
		_this.AddAimRequest(Priority.High, "facing", Vector3(x,y,z));
		sleep(t);
	},
	NavBox = function(_this, x,y,z)
	{
		if (_this.Bot.GetTeam()==TEAM.ALLIES) {
			_this.AddAimRequest(Priority.High, "position", Vector3(x,y,z));
			sleep(0.1);
			for(i=0; i<3 || i<15 && _this.Bot.GetHighLevelGoalName()=="UseCabinet"; i+=1){
				_this.Bot.PressButton(BTN.USE);
				sleep(1.5);
			}
		}else{
			sleep(2);
		}
	},

	Navigation =
	{
		ladder_cp = {
			navigate = function(_this)
			{
				_this.AddAimRequest(Priority.High, "facing", Vector3(-1,0,0));
				sleep(0.2);
				_this.Bot.HoldButton(BTN.STRAFE_R, 1);
				sleep(3);
			},
		},
		l1 = {navigate=function(t){
			Map.NavL(t, 0, -0.7, 0.7, 5);
		}},
		l2 = {navigate=function(t){
			Map.NavL(t, 0, -0.2, 0.8, 4);
		}},
		l3 = {navigate=function(t){
			sleep(1);
			Map.NavL(t, 0, -0.2, 0.8, 3);
		}},
		l4 = {navigate=function(t){
			Map.NavL(t, 0.2, 0, 0.8, 7);
		}},

		//health and ammo
		box1 = {navigate=function(t){
			Map.NavBox(t, -1970, 4910, -270);
		}},
		box2 = {navigate=function(t){
			Map.NavBox(t, -1888, 4910, -271);
		}},
		box3 = {navigate=function(t){
			Map.NavBox(t, 5709, 5419, 392);
		}},
		box4 = {navigate=function(t){
			Map.NavBox(t, 5814, 5419, 390);
		}},
	},

	RideMovers =
	{
		cargo_west =
		{
			movergoal = "MOVER_cargo_rail",
			rideposition = Vector3(0,125,-50),
			startwp = "cargo_west_in",
			exitwp = "cargo_east_out",
			board = function(_this)
			{
				return Map.cargo == 1;
			},
			leave = function(_this)
			{
				return Map.cargo == 3;
			},
		},
		cargo_east =
		{
			movergoal = "MOVER_cargo_rail",
			rideposition = Vector3(0,125,-50),
			startwp = "cargo_east_in",
			exitwp = "cargo_west_out",
			board = function(_this)
			{
				return Map.cargo == 3;
			},
			leave = function(_this)
			{
				return Map.cargo == 1;
			},
		},
	},

	Cargo_Goto = function(trigger)
	{
		vel = ToVector(trigger.Action);
		if(vel.x == 60){
			if(Map.cargo==1){ Map.cargo=2; } //move from west to east
			else{ sleep(9); Map.cargo=3; } //stop east
		} else if(vel.x == -60){
			if(Map.cargo==3){ Map.cargo=4; } //move from east to west
			else{ sleep(9); Map.cargo=1; } //stop west
		}
		//print("cargo ", Map.cargo);
	},

	OnWallBlown = function(trigger)
	{
		Util.EnableGoal( "ROUTE_wall" );
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_wall.*" );
		SetAvailableMapGoals(TEAM.AXIS, false, {
			"DEFEND_wall.*",
			"MOBILEMG42_wall.*",
			"DEFEND_start.*",
			"DEFEND_sewer.*",
		});
		SetAvailableMapGoals(TEAM.AXIS, true, "TRIPMINE_x.*" );
		if(!Map.agentsTakenTable["a"]){
			SetAvailableMapGoals(TEAM.AXIS, true, "TRIPMINE_a.*" );
		}
		if(!Map.agentsTakenTable["b"]){
			SetAvailableMapGoals(TEAM.AXIS, true, {
				"DEFEND_obj_b.*",
				"TRIPMINE_b.*",
			});
		}
		if(!Map.agentsTakenTable["c"]){
			SetAvailableMapGoals(TEAM.AXIS, true, "TRIPMINE_c.*" );
		}
		if(!Map.pitDrain){
			SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_valve.*" );
			SetAvailableMapGoals(TEAM.ALLIES, true, "SWITCH_valve" );
		}
		SetAvailableMapGoals(TEAM.ALLIES, true, "GRENADE_grate" );

		ClearGoalRole(Map.FLAG_counteragenta, ROLE.ATTACKER2 );

		Util.MapDebugPrint("WallBlown");
	},


	Allies_Capture_Forward_Spawn = function(trigger)
	{
		if(!Map.pitDrain){
			SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_valve.*" );
			SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_start.*" );
		}
		SetGoalRole("CHECKPOINT_forwardspawn1_flag", {ROLE.DEFENDER1,ROLE.DEFENDER2} );
		Util.MapDebugPrint("Allies_Capture_Forward_Spawn");
	},

	Axis_Capture_Forward_Spawn = function(trigger)
	{
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_valve.*" );
		SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_start.*" );
		ClearGoalRole("CHECKPOINT_forwardspawn1_flag", {ROLE.DEFENDER1,ROLE.DEFENDER2} );
		Util.MapDebugPrint("Axis_Capture_Forward_Spawn");
	},

	OnStealCounterAgent = function(trigger)
	{
		name = GetEntName(trigger.Entity);
		if (!name)
		{
			Util.MapDebugPrint("picked up dropped counter agent");
		}
		else
		{
			//136-counteragenta, 147-counteragentb, 148-counteragentc
			which = name.Right(1);
			if(which=="a" || which=="b" || which=="c")
			{
				Map.agentsTaken+=1;
				Map.agentsTakenTable[which] = true;
				SetAvailableMapGoals(TEAM.AXIS, false, {
					"DEFEND_obj_" + which + ".*",
					"TRIPMINE_" + which + ".*",
				});
				SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_secure.*");
				SetAvailableMapGoals(TEAM.ALLIES, true, "ATTACK_secure.*");
				if(Map.agentsTaken+Map.agentsSecured == 3){
					SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_circulation.*");
				}
			}
			Util.MapDebugPrint("stolen counter agent " + which);
		}
	},

	OnReturnedCounterAgent = function(which)
	{
		SetAvailableMapGoals(TEAM.AXIS, true, {
			"DEFEND_obj_" + which + ".*",
			"TRIPMINE_" + which + ".*",
		});
		Map.agentsTaken-=1;
		Map.agentsTakenTable[which] = false;
		if(Map.agentsTaken == 0){
			SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_secure.*");
			SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_secure.*");
		}
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_circulation.*");
		Util.MapDebugPrint("returned counter agent " + which);
	},

	OnReturnedA = function(trigger)
	{
		Map.OnReturnedCounterAgent("a");
	},

	OnReturnedB = function(trigger)
	{
		Map.OnReturnedCounterAgent("b");
	},

	OnReturnedC = function(trigger)
	{
		Map.OnReturnedCounterAgent("c");
	},

	OnSecureCounterAgent = function(trigger)
	{
		Map.agentsSecured+=1;
		Map.agentsTaken-=1;
		if(Map.agentsSecured==2){
			ClearGoalRole("DEFEND_obj_.*", { ROLE.DEFENDER1,ROLE.DEFENDER2,ROLE.DEFENDER3} );
		}
		Util.MapDebugPrint( "SecureCounterAgent" );
	},

	OnAllSecured = function(trigger)
	{
		SetAvailableMapGoals(TEAM.AXIS, false, {
			"DEFEND_secure.*",
			"PLANT_Allied_Command_Post",
		});
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_secure.*");
		SetAvailableMapGoals(TEAM.ALLIES, true, "SWITCH_circulation.*");
		Util.MapDebugPrint( "AllSecured" );
	},

	OnPitDrain = function(trigger)
	{
		Map.pitDrain = true;
		Util.EnableGoal( "ROUTE_c_ladder.*" );
		if(!Map.agentsTakenTable["c"]){
			SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_obj_c.*" );
		}
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_valve.*" );
		SetAvailableMapGoals(TEAM.ALLIES, false, "SWITCH_valve");
		Util.MapDebugPrint("PitDrain");
	},

	grateExploded = function(trigger)
	{
		Util.EnableGoal( "ROUTE_center_shaft" );
		Util.MapDebugPrint( "grateExploded" );
	},

	SetSpawn1 = function(bot)
	{
		if (bot.GetTeam()==TEAM.ALLIES)
		{
			if (RandInt(0,2)==0)
			{
				//command post
				bot.ChangeSpawnPoint(5);
			}else{
				bot.ChangeSpawnPoint(0);
			}
		}
	},

	OnAlliedCPUpgrade2 = function(trigger)
	{
		foreach(bot in BotTable)
		{
			Map.SetSpawn1(bot);
		}
		Util.MapDebugPrint( "AlliedCPUpgrade2" );
	},

	AlliesWin = function(trigger)
	{
		//it is very important do disable switch, otherwise the map will not end
		SetAvailableMapGoals(TEAM.ALLIES, false, "SWITCH_circulation.*");
		ETUtil.WinningChat( TEAM.ALLIES );
		ETUtil.LosingChat( TEAM.AXIS );
		Util.MapDebugPrint( "AlliesWin" );
	},
};

global OnMapLoad = function()
{
	OnTrigger("The Allies have breached the Warehouse Wall!", Map.OnWallBlown);
	OnTrigger("Allies capture the Forward Spawn!", Map.Allies_Capture_Forward_Spawn);
	OnTrigger("Axis reclaim the Forward Spawn!", Map.Axis_Capture_Forward_Spawn);
	OnTrigger("Allies have stolen Chemical Counter-Agent!", Map.OnStealCounterAgent);
	OnTrigger("The Allied Team has captured the first container of counter-agent!", Map.OnSecureCounterAgent);
	OnTrigger("The Allied Team has captured the second container of counter-agent!", Map.OnSecureCounterAgent);
	OnTrigger("The Allied Team has captured the final container of counter-agent!", Map.OnAllSecured);
	OnTrigger("The Allied Team has Drained the Western Chemical Pit", Map.OnPitDrain);
	//OnTrigger("The Allies have constructed a Command Post!", Map.OnAlliedCPBuilt);
	//OnTrigger("The Allies have upgraded their Command Post!", Map.OnAlliedCPUpgrade);
	OnTrigger("The Allies have constructed a Forward Spawn!", Map.OnAlliedCPUpgrade2);
	OnTrigger("Flag returned counteragenta!", Map.OnReturnedA);
	OnTrigger("Flag returned counteragentb!", Map.OnReturnedB);
	OnTrigger("Flag returned counteragentc!", Map.OnReturnedC);
	OnTrigger("cargo_rail_goto", Map.Cargo_Goto);
	OnTrigger("The Axis Chemical Supply has been Destroyed!", Map.AlliesWin);
	OnTrigger("GRENADE_grate Exploded.", Map.grateExploded);

	OnTriggerRegion(AABB(-962.220,6935.126,-151.347,-395.234,7264.658,118.846),RegionTrigger.DisableBotPush); //valve
	OnTriggerRegion(AABB(3311.637,4800,-406.445,5376.875,6621.153,600),RegionTrigger.DisableBotPush); //cargo and pipe
	OnTriggerRegion(AABB(-2189.669,4031.125,-429.614,-1579.789,4360.875,-156.528),RegionTrigger.DisableBotPush); //pit

	if (GetGoal("FLAG_counteragenta"))
	{
		//map with patch
		Map.FLAG_counteragenta = "FLAG_counteragenta";
	}else{
		//map without patch
		Map.FLAG_counteragenta = "FLAG_Chemical_Counter_Agent";
	}

	Util.DisableGoal( ".*", true ); // all but routes
	Util.DisableGoal( "ROUTE_c_ladder.*" );
	Util.DisableGoal( "ROUTE_wall" );
	Util.DisableGoal( "ROUTE_center_shaft" );
	Util.EnableGoal( "CHECKPOINT_forwardspawn1_flag" );

	SetAvailableMapGoals(TEAM.AXIS, true, {
		"DEFEND_obj_a.*",
		"DEFEND_wall.*",
		"DEFEND_start.*",
		"DEFEND_sewer.*",
	});
	//SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_valve.*" );
	SetAvailableMapGoals(TEAM.AXIS, true, {
		"PLANT_Allied_Command_Post",
		"MOBILEMG42_wall.*",
		"TRIPMINE_a.*",
	});

	SetAvailableMapGoals(TEAM.ALLIES, true, {
		"BUILD_Allied_Command_Post",
		"CAPPOINT_securePoint",
		"FLAG_.*",
		"PLANT_Warehouse_Wall",
		"ATTACK_wall.*",
		"AMMOCAB.*allied.*",
		"HEALTHCAB.*allied.*",
	});

	SetGoalPriority( "FLAG_.*", 0.6, TEAM.ALLIES );
	SetGoalPriority( "FLAG.*", 0.51, TEAM.AXIS, 0, true );
	SetGoalPriority( "CHECKPOINT_.*", 0.69 );
	SetGoalPriority( "BUILD_Allied_Command_Post", 0.7 );
	SetGoalPriority( "DEFEND_circulation.*", 0.49 );
	SetGoalPriority( "ATTACK_wall.*", 0.75 );

	SetGoalRole({ "DEFEND_obj_a.*", "DEFEND_sewer.*" }, ROLE.DEFENDER1 );
	SetGoalRole({ "DEFEND_obj_b.*", "DEFEND_wall.*" }, ROLE.DEFENDER2 );
	SetGoalRole({ "DEFEND_obj_c.*", "DEFEND_valve.*", "DEFEND_start.*" }, ROLE.DEFENDER3 );
	SetGoalRole("ATTACK_wall.*", ROLE.ATTACKER1 );
	SetGoalRole(Map.FLAG_counteragenta, ROLE.ATTACKER2 );

	Util.SetMaxUsers(1, "GRENADE_.*");
	Util.SetMaxUsersInUse(1, "PLANT_Warehouse_Wall.*");
	Util.SetMaxUsersInProgress(4, "SWITCH_circulation.*");

	SetMapGoalProperties("DEFEND_.*", {mincamptime=20, maxcamptime=60} );

	if(GetGoal("MOVER_cargo_rail").GetPosition().x == 3296){
		//old mods return wrong mover position
		Map.RideMovers.cargo_west.rideposition = Vec3(0,-100,-50);
		Map.RideMovers.cargo_east.rideposition = Vec3(0,-100,-50);
	}

	MapRoutes =
	{
		FLAG_counteragentb = {
			ROUTE_flag_spawn = {
				ROUTE_cargo = {
					ROUTE_b_cargo = { Weight=2 },
					ROUTE_b_north = {},
				},
				ROUTE_b_pipe = {
					ROUTE_b_ladder1 = {},
					ROUTE_b_ladder2 = {},
				},
			},
			ROUTE_cp_spawn = {
				ROUTE_center_north_west = { ROUTE_b_pipe = {}},
				ROUTE_center_south_east = { ROUTE_cargo = {}},
			},
		},
		FLAG_counteragentc = {
			ROUTE_flag_spawn = {
				ROUTE_c_ladder1 = {},
				ROUTE_c_ladder2 = {
					ROUTE_c_ladder2_1 = { Weight=3 },
					ROUTE_c_ladder2_2 = {},
				},
				ROUTE_center_shaft = {
					ROUTE_bridge = {
						ROUTE_north_tunnel = {
							ROUTE_c_east = {
								ROUTE_c_east_north = {},
								ROUTE_c_east_south = {},
							},
						},
						ROUTE_north_room = { ROUTE_c_east = {}},
						ROUTE_north_shaft = { Weight=2, ROUTE_c_east = {}},
					}
				},
				ROUTE_flag_stairs = { ROUTE_bridge = {}},
				ROUTE_cargo_down = { Weight=0.5, ROUTE_bridge = {}},
			},
			ROUTE_cp_spawn = {
				ROUTE_c_ladder1 = {},
				ROUTE_c_ladder2 = {},
			},
		},
		CAPPOINT_securePoint = {
			ROUTE_a_obj = {
				ROUTE_a_west = { ROUTE_flag_west = {}},
				ROUTE_a_north_east = { ROUTE_flag_stairs = {}},
			},
			ROUTE_b_obj = {
				ROUTE_b_cargo = {
					ROUTE_cargo_down = { ROUTE_center_south_west = {}},
					ROUTE_center_shaft = { Weight=0.5 },
					ROUTE_flag_stairs = {},
				},
				ROUTE_b_pipe = { Weight=2,
					ROUTE_center_north_west = { ROUTE_center_south_west = {}},
					ROUTE_center_south_east = {},
				},
			},
			ROUTE_c_obj = {
				ROUTE_c_ladder1 = { ROUTE_cp_spawn = {}},
				ROUTE_c_ladder2 = { ROUTE_cp_spawn = {}},
				ROUTE_dam_spawn = {},
				ROUTE_c_east = {
					ROUTE_north_tunnel = { ROUTE_bridge = {}},
					ROUTE_north_room = { ROUTE_bridge = {}},
				},
			},
			ROUTE_flag_spawn = {
				ROUTE_center_shaft = {},
				ROUTE_flag_stairs = { Weight=2 },
			},
		},
		CHECKPOINT_forwardspawn1_flag = {
			ROUTE_allied_spawn = {
				ROUTE_wall = { Weight=9 },
				ROUTE_waterfall = {},
				ROUTE_a_down = { ROUTE_flag_shaft = {}},
			},
			ROUTE_axis_spawn = {
				ROUTE_north_tunnel = {},
				ROUTE_north_room = { Weight=0.7 },
				ROUTE_dam_spawn = {},
			},
		},
		PLANT_Allied_Command_Post = {
			ROUTE_axis_spawn = {
				ROUTE_c_ladder1 = {},
				ROUTE_c_ladder2 = {},
				ROUTE_center_south_east = {},
				ROUTE_dam_spawn = {},
			},
		},
		PLANT_Warehouse_Wall = {
			ROUTE_allied_spawn = {
				ROUTE_allied1 = { ROUTE_allied5 = {}},
				ROUTE_allied3 = { ROUTE_allied6 = {}},
				ROUTE_allied4 = { ROUTE_allied7 = {}},
			}
		},
		SWITCH_valve = {
			ROUTE_flag_spawn = {
				ROUTE_waterfall = {
					ROUTE_valve_north = {},
					ROUTE_valve_south = {},
				}
			}
		},
		SWITCH_circulation1 = {
			ROUTE_flag_spawn = {
				ROUTE_center_shaft = { ROUTE_center_south_west = {}},
				ROUTE_flag_stairs = { ROUTE_center_north_east = {}},
			}
		},
	};
	MapRoutes[Map.FLAG_counteragenta] = {
		ROUTE_allied_spawn = {
			ROUTE_a_ladder1 = {},
			ROUTE_a_ladder2 = {},
			ROUTE_a_ladder3 = {},
			ROUTE_a_ladder4 = {},
		},
		ROUTE_flag_spawn = {
			ROUTE_a_west = {},
			ROUTE_a_north_east = {
				ROUTE_a_obj = {},
				ROUTE_a_down = {
					ROUTE_a_ladder1 = {},
					ROUTE_a_ladder2 = {},
					ROUTE_a_ladder3 = {},
				},
			},
		},
	};
	MapRoutes["DEFEND_obj_a.*"] = {
		ROUTE_axis_spawn = {
			ROUTE_north_tunnel = {
				ROUTE_bridge = {
					ROUTE_flag_stairs = {
						ROUTE_flag_west = {},
						ROUTE_a_north_east = {},
					},
				},
			},
			ROUTE_north_room = {
				ROUTE_bridge = {},
			},
			ROUTE_dam_spawn = {
				ROUTE_flag_stairs = {},
			},
		}
	};
	MapRoutes["DEFEND_obj_b.*"] = {
		ROUTE_axis_spawn = {
			ROUTE_b_north_east = {},
			ROUTE_b_north = {},
		}
	};
	MapRoutes["DEFEND_obj_c.*"] = {
		ROUTE_axis_spawn = {
			ROUTE_c_east_north = {},
			ROUTE_c_east_south = {},
		}
	};
	MapRoutes["ATTACK_secure.*"] = MapRoutes.CAPPOINT_securePoint;
	MapRoutes["ATTACK_wall.*"] = MapRoutes.PLANT_Warehouse_Wall;
	Util.Routes(MapRoutes);

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

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