//==========================================================================================
//
//	leningrad_b2.gm
//
//	Who	When		What
//------------------------------------------------------------------------------------------
//	palota	04 July 2010	Initial Script
//	palota	15 August 2010	more goals added
//	palota	29 August 2010	Navigation, more routes, DEFEND_square.*, AIRSTRIKE_barrier1, GRENADE_wall_mg, ATTACK_cp
// palota	28 September 2011	tank_barrier_1 UsePoints, axis role, disabled axis_radios after tank_barrier_2, changed priorities
//==========================================================================================
global Map =
{
	Movers = { "MOVER_tank1" },

	Roles = {
		AXIS = {
			DEFENDER2 = {
				numbots = 4
			}
		}
	},

	phase = 0,
	gateOpened = false,
	ladderBuilt = false,
	cpSecured = false,

	Navigation =
	{
		pole = {
			navigate=function(_this){
				_this.AddAimRequest(Priority.High, "facing", Vector3(-0.4,0,0.6));
				sleep(0.5);
				_this.Bot.HoldButton(BTN.BACKWARD, 4);
				sleep(4);
			}
		},
		window = {
			navigate=function(_this){
				_this.AddAimRequest(Priority.High, "facing", Vector3(0,1,0));
				sleep(0.2);
				_this.Bot.PressButton(BTN.JUMP);
				sleep(0.2);
				_this.Bot.HoldButton(BTN.FORWARD,BTN.CROUCH, 2);
				sleep(3);
			}
		},
	},

	SetWall = function()
	{
		b = Map.phase>=50 || Map.ladderBuilt;
		SetAvailableMapGoals(TEAM.ALLIES, b, {
			"PLANT_power_generator",
			"PLANT_wall",
		});
	},

	SetEscort = function()
	{
		b = Map.phase<50;
		if(Map.phase>=10 && Map.phase<15){ b = Map.gateOpened; }
		SetAvailableMapGoals(TEAM.ALLIES, b, "ESCORT_tank");
	},

	SetLadder = function(enabled)
	{
		Map.ladderBuilt = enabled;
		Map.SetWall();
		Wp.SetWaypointFlag("ladder", "closed", !enabled);
		SetAvailableMapGoals(TEAM.ALLIES, enabled, "ROUTE_ladder");
	},

	SetSpawn1 = function(bot)
	{
		if (bot.GetTeam()==TEAM.AXIS)
		{
			if(Map.phase<30){
				bot.ChangeSpawnPoint(0);
			}else if (bot.HasRole(ROLE.DEFENDER2)){
				bot.ChangeSpawnPoint(4); //bunker
			}else{
				bot.ChangeSpawnPoint(1); //axis spawn
			}
		}
	},

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

	Tank_Built = function()
	{
		Util.MapDebugPrint( "Tank_Built" );
	},

	Tank_Damaged = function()
	{
		Util.MapDebugPrint("Tank_Damaged");
	},

	Tank_At_Barrier1 = function()
	{
		SetGoalPriority("PLANT_tank_barrier_1", 0.95);
		Util.MapDebugPrint("Tank_At_Barrier1");
	},

	Tank_Passed_barrier_1 = function()
	{
		Util.DisableGoal("BUILD_tank_barrier_1"); //why this is not autodetected ?
		Util.DisableGoal(".*barrier1.*");
		SetAvailableMapGoals(TEAM.AXIS, true, {
			"DEFEND_switch.*",
			"MOBILEMG42_switch",
			"BUILD_tank_barrier_2",
		});
		Util.OnTriggerPosition( "MOVER_tank1", "before_gate", 170.0, Map.Tank_At_Gate );
		Util.MapDebugPrint( "Tank_Passed_barrier_1" );
	},

	Spawn_Secured = function()
	{
		Map.cpSecured = true;
		if (Map.phase>=25){
			Util.SetMaxUsers(1, "SWITCH_gate");
		}
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_cp.*");
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_cp");
		SetGoalPriority("PLANT_Command_Post", 0.81, TEAM.ALLIES );
		Util.MapDebugPrint( "Spawn_Secured" );
	},

	Tank_At_Gate = function()
	{
		Map.phase = 10;
		Map.SetEscort();
		SetGoalPriority("SWITCH_gate", 0.7 );
		Util.SetMaxUsersInProgress(9, "SWITCH_gate");
		SetGoalPriority("BUILD_tank_barrier_2", 0.95, TEAM.AXIS);
		SetGoalPriority("PLANT_Command_Post", 0.85, TEAM.ALLIES );
		SetGoalPriority("PLANT_axis_radios", 0.81);
		Util.DisableGoal("MOUNT_tank");
		SetAvailableMapGoals(TEAM.AXIS, true, {
			"AIRSTRIKE_gate",
			"GRENADE_wall_mg",
		});
		SetAvailableMapGoals(TEAM.ALLIES, true, ".*MG42_broken_wall");
		Util.OnTriggerPosition( "MOVER_tank1", "behind_gate", 170.0, Map.Tank_Passed_Gate );
		Util.MapDebugPrint( "Tank_At_Gate" );
	},

	Gate_Opened = function()
	{
		Map.gateOpened = true;
		Util.LimitToTeam(TEAM.AXIS, "SWITCH_gate");
		Map.SetEscort();
		Util.MapDebugPrint("Gate_Opened");
	},

	Gate_Closed = function()
	{
		Map.gateOpened = false;
		Util.LimitToTeam(TEAM.ALLIES, "SWITCH_gate");
		Map.SetEscort();
		Util.MapDebugPrint("Gate_Closed");
	},

	Tank_Passed_Gate = function()
	{
		Map.phase = 15;
		SetGoalPriority("SWITCH_gate", 0.55 );
		Util.DisableGoal(".*_switch.*");
		SetAvailableMapGoals(TEAM.AXIS, false, "AIRSTRIKE_gate");
		SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_barrier2.*");
		SetAvailableMapGoals(TEAM.ALLIES, true, {
			"ATTACK_cp",
			"ATTACK_barrier2.*",
		});
		SetGoalPriority(".*_axis_radios", 0.8);
		Util.EnableGoal("MOUNT_tank");
		Util.OnTriggerPosition( "MOVER_tank1", "barrier2", 170.0, Map.Tank_At_Barrier2 );
		Util.MapDebugPrint( "Tank_Passed_Gate" );
	},

	Tank_At_Barrier2 = function()
	{
		Map.phase=25;
		if (Map.cpSecured){
			Util.SetMaxUsers(1, "SWITCH_gate");
		}else{
			Util.SetMaxUsers(3, "SWITCH_gate");
		}
		SetGoalPriority("PLANT_tank_barrier_2", 0.95);
		SetAvailableMapGoals(TEAM.AXIS, true, "PLANTMINE.*");
		SetGoalPriority("PLANT_Command_Post", 0.6, TEAM.AXIS, CLASS.ENGINEER);
		Util.MapDebugPrint("Tank_At_Barrier2");
	},

	Ladder_Enabled = function()
	{
		//tank passed barrier 2
		Map.phase=30;
		Util.DisableGoal(".*barrier2.*");
		Util.DisableGoal("BUILD_tank_barrier_2");
		Util.DisableGoal("SWITCH_gate");
		Util.DisableGoal(".*MG42_broken_wall");
		Util.DisableGoal(".*_axis_radios");
		SetAvailableMapGoals(TEAM.AXIS, false, "GRENADE_wall_mg");
		SetAvailableMapGoals(TEAM.AXIS, true, {
			".*MG42_park",
			".*MG42_cemetery",
			"DEFEND_square1.*",
			"SNIPE_square_west",
		});
		SetAvailableMapGoals(TEAM.ALLIES, true, {
			"ATTACK_square1.*",
			"BUILD_ladder",
		});
		Util.EnableGoal("AMMOCAB.*");
		Util.EnableGoal("HEALTHCAB.*");
		Util.SetPercentInProgress(50, "CHECKPOINT_westbunker_flag");
		SetGoalPriority("CHECKPOINT_westbunker_flag", 0.8, TEAM.ALLIES);
		Map.SetSpawn();
		Util.OnTriggerPosition( "MOVER_tank1", "square1", 170.0, Map.Tank_At_Square1 );
		Util.MapDebugPrint("Ladder_Enabled");
	},

	Tank_At_Square1 = function()
	{
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_cp.*");
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_cp");
		SetGoalPriority("PLANT_Command_Post", 0.81, TEAM.ALLIES );
		Util.SetMaxUsersInProgress(1, ".*Command_Post");
		Util.OnTriggerPosition( "MOVER_tank1", "square2", 170.0, Map.Tank_At_Square2 );
		Util.MapDebugPrint("Tank_At_Square1");
	},

	Tank_At_Square2 = function()
	{
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_square1.*");
		SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_square2.*");
		SetAvailableMapGoals(TEAM.ALLIES, true, "ATTACK_square2.*");
		Util.OnTriggerPosition( "MOVER_tank1", "square3", 170.0, Map.Tank_At_Square3 );
		Util.MapDebugPrint("Tank_At_Square2");
	},

	Tank_At_Square3 = function()
	{
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_square1.*");
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_square2.*");
		SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_square3.*");
		SetAvailableMapGoals(TEAM.ALLIES, true, "ATTACK_square3.*");
		Util.MapDebugPrint("Tank_At_Square3");
	},

	ladder_Built = function()
	{
		Map.SetLadder(true);
		Util.MapDebugPrint( "ladder_Built" );
	},

	ladder_Destroyed = function()
	{
		Map.SetLadder(false);
		Util.MapDebugPrint( "ladder_Destroyed" );
	},

	wall_Destroyed = function()
	{
		Util.MapDebugPrint( "wall_Destroyed" );
		Util.EnableGoal("ROUTE_brick_wall");
	},

	bunker_flag_Allies_Captured = function()
	{
		SetGoalRole("CHECKPOINT_westbunker_flag", ROLE.DEFENDER2);
		SetAvailableMapGoals(TEAM.AXIS, false, ".*MG42_cemetery");
		SetAvailableMapGoals(TEAM.AXIS, true, "CHECKPOINT_westbunker_flag");
		Util.MapDebugPrint("bunker_flag_Allies_Captured");
	},

	bunker_flag_Axis_Captured = function()
	{
		ClearGoalRole("CHECKPOINT_westbunker_flag", ROLE.DEFENDER2);
		SetAvailableMapGoals(TEAM.AXIS, true, ".*MG42_cemetery");
		Util.SetPercentInProgress(40, "CHECKPOINT_westbunker_flag");
		Util.MapDebugPrint("bunker_flag_Axis_Captured");
	},

	Tunnel_Entrance = function()
	{
		Map.phase=50;
		SetAvailableMapGoals(TEAM.AXIS, false, {
			"DEFEND_square.*",
			"SNIPE_square_west",
		});
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_square.*");
		SetAvailableMapGoals(TEAM.AXIS, true, {
			"DEFEND_docs.*",
			"MOBILEMG42_tunnel",
		});
		SetAvailableMapGoals(TEAM.ALLIES, true, "FLAG_docs");
		Map.SetWall();
		Map.SetEscort();
		SetGoalPriority("BUILD_an_axis_radio", 0.75, TEAM.AXIS);
		SetGoalPriority("BUILD_ladder", 0.9, TEAM.ALLIES);
		SetGoalPriority("PLANT_wall", 0.85, TEAM.ALLIES);
		SetGoalRole("MOUNTMG42_park", ROLE.DEFENDER2);
		Util.SetMaxUsersInProgress(1, ".*MG42_park");
		Util.SetMaxUsersInProgress(1, "MOUNT_tank");
		Util.MapDebugPrint("Tunnel_Entrance");
	},

	docs_Taken = function()
	{
		Util.EnableGoal("DEFEND_transmit.*");
		Util.MapDebugPrint( "docs_Taken" );
	},

	docs_Returned = function()
	{
		Util.DisableGoal("DEFEND_transmit.*");
		Util.MapDebugPrint( "docs_Returned" );
	},
};

global OnMapLoad = function()
{
	OnTrigger( "^7>^1Allied team has repaired the tank^7!", Map.Tank_Built );
	OnTrigger( "^7>^1Axis team has disabled the tank^7!", Map.Tank_Damaged );
	OnTrigger( "^7>^1Allies have to build the command post to get the 2nd spawn^7!", Map.Tank_Passed_barrier_1 );
	OnTrigger( "^7>^1Allies 2nd spawn secured^7!", Map.Spawn_Secured );
	OnTrigger( "^7>^1Gate opened^7!", Map.Gate_Opened );
	OnTrigger( "^7>^1Gate closed^7!", Map.Gate_Closed );
	OnTrigger( "^7>^1Allies can now build the assault ladder^7!", Map.Ladder_Enabled );
	OnTrigger( "^7>^1The assault ladder has been constructed^7!", Map.ladder_Built );
	OnTrigger( "^7>^1The assault ladder has been destroyed^7!", Map.ladder_Destroyed );
	OnTrigger( "^7>^1Allies have destroyed the brick wall^7!", Map.wall_Destroyed );
	OnTrigger( "^7>^1Allies captured the bunker^7!", Map.bunker_flag_Allies_Captured );
	OnTrigger( "^7>^1Axis reclaimed the bunker^7!", Map.bunker_flag_Axis_Captured );
	OnTrigger( "^7>^1The tank blown up the tunnel entrance^7!", Map.Tunnel_Entrance );
	OnTrigger( "Allies have stolen The axis documents!", Map.docs_Taken );
	OnTrigger( "Flag returned docs!", Map.docs_Returned );

	OnTriggerRegion(AABB(4499.125,-203.543,505.353,6635.184,-67.317,643.872), RegionTrigger.DisableCombatMovement); //windows above the Ladder

	Util.DisableGoal( ".*", true );
	Util.DisableGoal("ROUTE_brick_wall");
	Util.DisableGoal("ROUTE_tunnel");
	SetAvailableMapGoals( TEAM.AXIS, true, {  "BUILD_an_axis_radio", "BUILD_axis_radios", "BUILD_power_generator", "PLANT_ladder",
		"BUILD_tank_barrier_1", "AIRSTRIKE_barrier1",
		"DEFEND_barrier1.*", "DEFEND_cp.*", "MOBILEMORTAR_barrier1", "SNIPE_barrier1" });
	SetAvailableMapGoals( TEAM.ALLIES, true, { "BUILD_Churchill_Tank", "ESCORT_tank", "MOUNT_tank", "CAPPOINT_transmit",
		"CHECKPOINT_westbunker_flag", "PLANT_an_axis_radio", "PLANT_axis_radios", "SWITCH_gate",
		"ATTACK_barrier1.*", "MOBILEMORTAR_allies_barrier1", "PLANT_tank_barrier.*",
		"SNIPE_allies_barrier1" });
	Util.EnableGoal(".*Command_Post");
	Map.SetLadder(false);

	SetGoalPriority("DEFEND_transmit.*", 0.79);
	SetGoalPriority("CHECKPOINT_westbunker_flag", 0.1);
	SetGoalPriority("PLANTMINE.*", 0.85);
	SetGoalPriority("SNIPE_allies_barrier1", 0.82);
	SetGoalPriority("PLANT_axis_radios", 0.82);
	SetGoalPriority("SNIPE_square_west", 0.85);
	SetGoalPriority("MOUNT_tank", 0.5, TEAM.AXIS);
	SetGoalPriority("BUILD_tank_barrier_1", 0.95, TEAM.AXIS);
	SetGoalPriority("PLANT_ladder", 0.6, TEAM.AXIS, CLASS.ENGINEER);
	SetGoalPriority("BUILD_tank_barrier_2", 0.89, TEAM.AXIS);
	SetGoalPriority("BUILD_ladder", 0.8, TEAM.ALLIES);
	SetGoalPriority("MOUNTMG42_broken_wall", 0.71, TEAM.ALLIES);
	SetGoalPriority("REPAIRMG42_broken_wall", 0.85, TEAM.ALLIES);
	SetGoalPriority("FLAG_docs", 0.75, TEAM.ALLIES);
	SetGoalPriority("ESCORT_tank", 0.69, TEAM.ALLIES);

	SetGoalRole("MOUNTMG42_cemetery", ROLE.DEFENDER2);
	ClearGoalRole({ "MOUNTMG42_park", "CHECKPOINT_westbunker_flag" }, ROLE.DEFENDER2);

	foreach (goal in { ".*MG42_cemetery", ".*MG42_broken_wall", "GRENADE_.*", "SNIPE_.*",
		"BUILD_an_axis_radio", "BUILD_axis_radios", "BUILD_power_generator",  }){
		Util.SetMaxUsersInProgress(1, goal);
	}

	SetMapGoalProperties( "DEFEND_.*", {mincamptime=10, maxcamptime=60} );
	SetMapGoalProperties( "MOUNTMG42_.*", {mincamptime=20, maxcamptime=60} );
	SetMapGoalProperties( "SNIPE_.*", {mincamptime=50, maxcamptime=150} );

	Util.AddUseWp("PLANT_wall", "brick_wall1");
	Util.AddUseWp("PLANT_wall", "brick_wall2");
	Util.AddUsePoint( "PLANT_tank_barrier_1", { Vec3(290.826, 2463.787, 292.125), Vec3(430.341, 1903.612, 292.125) } );

	Util.OnTriggerPosition( "MOVER_tank1", "barrier1", 170.0, Map.Tank_At_Barrier1 );

	MapRoutes =
	{
		BUILD_tank_barrier_1 = {
			ROUTE_house_spawn = {
				ROUTE_barrier1 = {},
			}
		},
		DEFEND_cp1 = {
			ROUTE_house_spawn = {
				ROUTE_cp_door = {},
			}
		},
		PLANT_tank_barrier_1 = {
			ROUTE_allies_spawn = {
				ROUTE_allies1 = {},
				ROUTE_allies2 = {},
				ROUTE_allies3 = {},
				ROUTE_allies4 = {},
				ROUTE_gazebo = { Weight=4 },
			},
		},
		PLANT_tank_barrier_2 = {
			ROUTE_house_spawn = {
				ROUTE_barrier2 = {},
				ROUTE_barrier2_house = { Weight=3 },
			}
		},
		SWITCH_gate = {
			ROUTE_axis_spawn = {
				ROUTE_bridge_east = {},
			}
		},
		CHECKPOINT_westbunker_flag = {
			ROUTE_house_spawn = {
				ROUTE_bunker4 = {},
				ROUTE_bunker5 = {},
			},
			ROUTE_axis_spawn = {
				ROUTE_tunnel = { Weight=9 },
				ROUTE_bunker5 = {},
			}
		},
		MOUNTMG42_park = {
			ROUTE_axis_spawn = {
				ROUTE_tunnel = { Weight=9 },
				ROUTE_square_mg42 = {},
			}
		},
		FLAG_docs = {
			ROUTE_bunker = {
				ROUTE_bunker1 = {
					ROUTE_ladder = { Weight = 2,
						ROUTE_docs_south = {},
						ROUTE_docs_north = {},
						ROUTE_brick_wall = { Weight=4 },
						ROUTE_cave = { Weight=2 },
					},
					ROUTE_tunnel = {
						ROUTE_docs_south = {},
						ROUTE_docs_north = {},
						ROUTE_cave = {},
					},
				},
			},
			ROUTE_house_spawn = {
				ROUTE_square_mg42 = {
					ROUTE_ladder = {
						ROUTE_docs_south = {},
						ROUTE_docs_north = {},
						ROUTE_brick_wall = { Weight=3 },
						ROUTE_cave = {},
					},
				},
				ROUTE_barrier2 = {
					ROUTE_docs_south = {},
					ROUTE_docs_north = {},
					ROUTE_cave = {},
				},
			},
		},
		CAPPOINT_transmit = {
			ROUTE_docs = {
				ROUTE_docs_south = { ROUTE_square_east = {} },
				ROUTE_docs_north = {},
				ROUTE_cave = { Weight=2,
					ROUTE_power_north = {
						ROUTE_square_east = {},
						ROUTE_square_south = {},
					}
				},
			}
		},
	};
	MapRoutes.DEFEND_cp3 = MapRoutes.DEFEND_cp1;
	MapRoutes.ATTACK_barrier1_1 = MapRoutes.PLANT_tank_barrier_1;
	MapRoutes.ATTACK_barrier1_3 = MapRoutes.ATTACK_barrier1_1;
	MapRoutes.PLANT_axis_radios = MapRoutes.PLANT_tank_barrier_1;
	MapRoutes.ATTACK_barrier2_2 = MapRoutes.PLANT_tank_barrier_2;
	MapRoutes.FLAG_docs.ROUTE_bunker.ROUTE_bunker2 = MapRoutes.FLAG_docs.ROUTE_bunker.ROUTE_bunker1;
	MapRoutes.FLAG_docs.ROUTE_bunker.ROUTE_bunker3 = MapRoutes.FLAG_docs.ROUTE_bunker.ROUTE_bunker1;
	MapRoutes.FLAG_docs.ROUTE_bunker.ROUTE_bunker4 = MapRoutes.FLAG_docs.ROUTE_bunker.ROUTE_bunker1;
	Util.Routes(MapRoutes);
	Util.MapDebugPrint( "Omni_bot map script for " + GetMapName() + " executed." );
};

global OnBotJoin = function( bot )
{
	bot.TargetBreakableDist = 200.0;
	bot.MaxViewDistance = 2200;
	Map.SetSpawn1(bot);
};
