//script created by palota
global Map =
{

	abbeySpawn = false,
	townSpawn = false,
	docsTaken = false,
	boatAccessible = true,
	abbeyDoorDestroyed = false,
	sideDoorDestroyed = false,
	classRevert = false,

	NavBreak = function(_this)
	{
		if(_this.navigation.done==null)
		{
			_this.AddWeaponRequest(WEAPON.KNIFE);
			_this.BlockForWeaponChange(WEAPON.KNIFE);
			wp = _this.CurrentNavigation;
			wpinfo = Util.WpNameInfo(wp);
			if(wp.Right(1)=="2"){
				wp = wp.Left(wp.Length()-1);
			}else{
				wp = wp + "2";
			}
			pos2 = Util.WpNameInfo(wp).position;
			for(i=0; i<30; i+=1)
			{
				tr = TraceLine(wpinfo.position, pos2, null, TRACE.BREAKABLE, 0, false);
				if(tr.fraction == 1){
					_this.navigation.done=true;
					break;
				}
				_this.AddAimRequest(Priority.Medium, "facing", wpinfo.facing);
				_this.Goto(wpinfo.position);
				_this.Bot.HoldButton(BTN.ATTACK1, 2);
				sleep(2);
			}
		}
		_this.Bot.PressButton(BTN.JUMP);
		sleep(1);
	},

	Navigation =
	{
		east_cp =
		{
			navigate = function(_this) { Map.NavBreak(_this); },
		},
		east_cp_in =
		{
			navigate = function(_this) { Map.NavBreak(_this); },
		},
	},

	SetPriorities = function()
	{
		if(Map.abbeySpawn){
			Util.SetMaxUsersInProgress(1, "CHECKPOINT_cityflag");
			SetGoalPriority("BUILD_Church_Defences", 0.1, TEAM.AXIS);
			SetGoalPriority("BUILD_Abbey_Defences", 0.1, TEAM.AXIS);
			SetGoalPriority("BUILD_Hillside_Defences", 0.95, TEAM.AXIS);
			SetGoalPriority("BUILD_hill_defences", 0.8, TEAM.AXIS);
			SetGoalPriority("BUILD_Command_Post", 0.6, TEAM.AXIS);
			SetGoalPriority("PLANT_Command_Post", 0.51, TEAM.AXIS, CLASS.ENGINEER);
			SetGoalPriority("PLANT_Command_Post", 0.81, TEAM.AXIS, CLASS.COVERTOPS);
			SetGoalPriority("BUILD_Axis_MG", 0.6, TEAM.AXIS);
			SetGoalPriority("REPAIRMG42_Axis_MG", 0.6, TEAM.AXIS);
			SetGoalPriority("DEFEND_docs.*", 0.5, TEAM.AXIS);
			SetGoalPriority("DEFEND_abbey_door.*", 0.1, TEAM.AXIS);
			SetAvailableMapGoals(TEAM.AXIS, false, {
				"MOUNTMG42_Axis_MG",
				"DEFEND_town.*",
				"SNIPE.*",
			});
			SetAvailableMapGoals(TEAM.AXIS, true, "PLANTMINE.*");
			SetGoalPriority("PLANT_Church_Defences", 0.1, TEAM.ALLIES);
			SetGoalPriority("PLANT_Abbey_Defences", 0.1, TEAM.ALLIES);
			SetGoalPriority("PLANT_Hillside_Defences", 0.95, TEAM.ALLIES);
			SetGoalPriority("PLANT_hill_defences", 0.85, TEAM.ALLIES);
			SetGoalPriority("BUILD_Command_Post", 0.8, TEAM.ALLIES);
			SetGoalPriority("PLANT_Command_Post", 0.5, TEAM.ALLIES);
			SetGoalPriority("PLANT_Axis_MG", 0.1, TEAM.ALLIES);
			SetGoalPriority("DEFEND_hillside.*", 0.5, TEAM.ALLIES);
			Util.SetPercentInProgress(70, "FLAG_docs");
			SetAvailableMapGoals(TEAM.ALLIES, false, "CHECKPOINT_cityflag");
		}
		else if(Map.townSpawn){
			SetGoalPriority("BUILD_Hillside_Defences", 0.8, TEAM.AXIS);
			if(Map.abbeyDoorDestroyed){
				SetGoalPriority("BUILD_Abbey_Defences", 0.65, TEAM.AXIS);
				SetGoalPriority("PLANT_Abbey_Defences", 0.7, TEAM.ALLIES);
			}else if(Map.sideDoorDestroyed){
				SetGoalPriority("BUILD_Abbey_Defences", 0.7, TEAM.AXIS);
				SetGoalPriority("PLANT_Abbey_Defences", 0.85, TEAM.ALLIES);
			}else{
				SetGoalPriority("BUILD_Hillside_Defences", 0.58, TEAM.AXIS);
				SetGoalPriority("BUILD_Abbey_Defences", 0.8, TEAM.AXIS);
				SetGoalPriority("PLANT_Abbey_Defences", 0.86, TEAM.ALLIES);
			}
			Util.SetPercentInProgress(20, "CHECKPOINT_cityflag");
			SetGoalPriority("BUILD_Church_Defences", 0.1, TEAM.AXIS);
			SetGoalPriority("BUILD_hill_defences", 0.7, TEAM.AXIS);
			SetGoalPriority("BUILD_Command_Post", 0.3, TEAM.AXIS);
			SetGoalPriority("PLANT_Command_Post", 0.3, TEAM.AXIS);
			SetGoalPriority("BUILD_Axis_MG", 0.8, TEAM.AXIS);
			SetGoalPriority("REPAIRMG42_Axis_MG", 0.65, TEAM.AXIS);
			SetGoalPriority("DEFEND_docs.*", 0.5, TEAM.AXIS);
			SetGoalPriority("DEFEND_abbey_door.*", 0.5, TEAM.AXIS);
			SetAvailableMapGoals(TEAM.AXIS, true, "MOUNTMG42_Axis_MG");
			SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_town.*");
			SetAvailableMapGoals(TEAM.AXIS, !Map.docsTaken, "SNIPE.*");
			SetAvailableMapGoals(TEAM.AXIS, true, "PLANTMINE.*");
			SetGoalPriority("PLANT_Church_Defences", 0.6, TEAM.ALLIES);
			SetGoalPriority("PLANT_Hillside_Defences", 0.3, TEAM.ALLIES);
			SetGoalPriority("PLANT_hill_defences", 0.6, TEAM.ALLIES);
			SetGoalPriority("BUILD_Command_Post", 0.85, TEAM.ALLIES);
			SetGoalPriority("PLANT_Command_Post", 0.7, TEAM.ALLIES);
			SetGoalPriority("PLANT_Axis_MG", 0.51, TEAM.ALLIES);
			SetGoalPriority("BUILD_Abbey_Command_Post", 0.84, TEAM.ALLIES);
			SetGoalPriority("DEFEND_hillside.*", 0.5, TEAM.ALLIES);
			Util.SetPercentInProgress(60, "FLAG_docs");
		}else{
			Util.SetMaxUsersInProgress(99, "CHECKPOINT_cityflag");
			SetGoalPriority("BUILD_Church_Defences", 0.9, TEAM.AXIS);
			SetGoalPriority("BUILD_Abbey_Defences", 0.95, TEAM.AXIS);
			SetGoalPriority("BUILD_Hillside_Defences", 0.6, TEAM.AXIS);
			SetGoalPriority("BUILD_hill_defences", 0.52, TEAM.AXIS);
			SetGoalPriority("BUILD_Command_Post", 0.85, TEAM.AXIS);
			SetGoalPriority("PLANT_Command_Post", 0.8, TEAM.AXIS);
			SetGoalPriority("BUILD_Axis_MG", 0.61, TEAM.AXIS);
			SetGoalPriority("REPAIRMG42_Axis_MG", 0.55, TEAM.AXIS);
			SetGoalPriority("DEFEND_docs.*", 0.1, TEAM.AXIS);
			SetGoalPriority("DEFEND_abbey_door.*", 0.5, TEAM.AXIS);
			SetAvailableMapGoals(TEAM.AXIS, false, "MOUNTMG42_Axis_MG");
			SetAvailableMapGoals(TEAM.AXIS, true, "DEFEND_town.*");
			SetAvailableMapGoals(TEAM.AXIS, false, {
				"SNIPE.*",
				"PLANTMINE.*",
			});
			SetGoalPriority("PLANT_Church_Defences", 0.9, TEAM.ALLIES);
			SetGoalPriority("PLANT_Abbey_Defences", 0.9, TEAM.ALLIES);
			SetGoalPriority("PLANT_Hillside_Defences", 0, TEAM.ALLIES);
			SetGoalPriority("PLANT_hill_defences", 0.1, TEAM.ALLIES);
			SetGoalPriority("BUILD_Command_Post", 0.1, TEAM.ALLIES);
			SetGoalPriority("PLANT_Command_Post", 0.3, TEAM.ALLIES);
			SetGoalPriority("PLANT_Axis_MG", 0.1, TEAM.ALLIES);
			SetGoalPriority("BUILD_Abbey_Command_Post", 0.7, TEAM.ALLIES);
			SetGoalPriority("DEFEND_hillside.*", 0.1, TEAM.ALLIES);
			Util.SetMaxUsersInProgress(1, "FLAG_docs");
			SetAvailableMapGoals(TEAM.ALLIES, true, "CHECKPOINT_cityflag");
		}
		if(Map.docsTaken){
			SetGoalPriority("BUILD_Hillside_Defences", 0.99, TEAM.AXIS);
			SetGoalPriority("PLANT_Hillside_Defences", 0.99, TEAM.ALLIES);
		}
	},

	Abbey_Defences_Built = function()
	{
		Util.DisableGoal("ROUTE_abbey_defences_south");
		SetAvailableMapGoals(TEAM.ALLIES, false, "ROUTE_abbey_defences_east");
		Util.MapDebugPrint( "Abbey_Defences_Built" );
	},
	Abbey_Defences_Destroyed = function()
	{
		Util.EnableGoal("ROUTE_abbey_defences_south");
		SetAvailableMapGoals(TEAM.ALLIES, true, "ROUTE_abbey_defences_east");
		Util.MapDebugPrint( "Abbey_Defences_Destroyed" );
	},

	Church_Defences_Built = function()
	{
		SetAvailableMapGoals(TEAM.ALLIES, false, "ROUTE_church_defences");
		Util.MapDebugPrint( "Church_Defences_Built" );
	},
	Church_Defences_Destroyed = function()
	{
		SetAvailableMapGoals(TEAM.ALLIES, true, "ROUTE_church_defences");
		Util.MapDebugPrint( "Church_Defences_Destroyed" );
	},

	SetHillside = function(b)
	{
		Map.boatAccessible=b;
		SetAvailableMapGoals(TEAM.ALLIES, b, "CAPPOINT_boat");
		SetAvailableMapGoals(TEAM.AXIS, b && Map.docsTaken, "DEFEND_boat_down.*");
		SetAvailableMapGoals(TEAM.AXIS, b && (Map.docsTaken || Map.sideDoorDestroyed), "MOUNTMG42_boat_mg");
		SetAvailableMapGoals(TEAM.AXIS, b, "REPAIRMG42_boat_mg");
		SetAvailableMapGoals(TEAM.AXIS, b || Map.docsTaken, "MOBILEMG42_side_door.*");
	},

	Hillside_Defences_Built = function()
	{
		Map.SetHillside(false);
		if(!Map.classRevert){
			Server.MinClassCount[TEAM.AXIS][CLASS.ENGINEER] -= 1;
			Map.classRevert=true;
		}
		wp="hillside";
		if(RandInt(1,2)==1){ wp="hillside2"; }
		Map.SetGoalPos("PLANT_Hillside_Defences", wp);
		Util.MapDebugPrint( "Hillside_Defences_Built" );
	},
	Hillside_Defences_Destroyed = function()
	{
		Map.SetHillside(true);
		Util.MapDebugPrint( "Hillside_Defences_Destroyed" );
	},

	hill_defences_Built = function()
	{
		Util.DisableGoal("ROUTE_hill_east");
		Util.MapDebugPrint( "hill_defences_Built" );
	},
	hill_defences_Destroyed = function()
	{
		Util.EnableGoal("ROUTE_hill_east");
		Util.MapDebugPrint( "hill_defences_Destroyed" );
	},

	Abbey_Command_Post_Built = function()
	{
		Map.abbeySpawn = true;
		Map.SetPriorities();
		Util.MapDebugPrint( "Abbey_Command_Post_Built" );
	},
	Abbey_Command_Post_Destroyed = function()
	{
		Map.abbeySpawn = false;
		Map.SetPriorities();
		Util.MapDebugPrint( "Abbey_Command_Post_Destroyed" );
	},

	cityflag_Allies_Captured = function()
	{
		Map.townSpawn = true;
		Map.SetPriorities();
		SetAvailableMapGoals(TEAM.AXIS, !Map.abbeyDoorDestroyed || !Map.sideDoorDestroyed, "DEFEND_side_door.*");
		Util.MapDebugPrint( "cityflag_Allies_Captured" );
	},
	cityflag_Axis_Captured = function()
	{
		Map.townSpawn = false;
		Map.SetPriorities();
		Util.MapDebugPrint( "cityflag_Axis_Captured" );
	},

	Door_Destroyed = function(b)
	{
		SetAvailableMapGoals(TEAM.ALLIES, b, {
			"BUILD_Abbey_Command_Post",
			"FLAG_docs",
			"PLANT_Hillside_Defences",
			"PLANT_hill_defences",
			"HEALTHCAB_abbey_healthcabinet",
			"AMMOCAB_abbey_ammocabinet",
			"DEFEND_hillside.*",
		});
		SetAvailableMapGoals(TEAM.AXIS, b, {
			"HEALTHCAB_abbey_healthcabinet",
			"AMMOCAB_abbey_ammocabinet",
			"DEFEND_docs.*",
			"BUILD_hill_defences",
		});
		SetAvailableMapGoals(TEAM.AXIS, b && (!Map.abbeyDoorDestroyed || !Map.sideDoorDestroyed), "DEFEND_side_door.*");
	},

	Abbey_Door_Destroyed = function()
	{
		Map.abbeyDoorDestroyed=true;
		Map.Door_Destroyed(true);
		SetAvailableMapGoals(TEAM.AXIS, false, "DEFEND_abbey_door.*");
		SetAvailableMapGoals(TEAM.ALLIES, false, "ATTACK_abbey_door.*");
		Util.EnableGoal("ROUTE_abbey_door");
		Util.MapDebugPrint( "Abbey_Door_Destroyed" );
	},

	Side_Door_Destroyed = function()
	{
		Map.sideDoorDestroyed=true;
		Map.Door_Destroyed(true);
		SetGoalPriority("PLANTMINE_side.*", 0.7, TEAM.AXIS);
		Util.EnableGoal("ROUTE_side_door.*");
		Util.MapDebugPrint( "Side_Door_Destroyed" );
	},

	Defend_Boat = function(b)
	{
		Map.docsTaken=b;
		SetAvailableMapGoals(TEAM.AXIS, !b, "DEFEND_docs.*");
		SetAvailableMapGoals(TEAM.AXIS, b, "DEFEND_boat.*");
		SetAvailableMapGoals(TEAM.AXIS, b && Map.boatAccessible, "DEFEND_boat_down.*");
		SetAvailableMapGoals(TEAM.AXIS, Map.boatAccessible && (b || Map.sideDoorDestroyed), "MOUNTMG42_boat_mg");
		SetAvailableMapGoals(TEAM.AXIS, !b, "CHECKPOINT_cityflag");
		SetAvailableMapGoals(TEAM.ALLIES, !b, "CHECKPOINT_cityflag");
		SetAvailableMapGoals(TEAM.ALLIES, b, "ATTACK_boat.*");
		SetAvailableMapGoals(TEAM.AXIS, b || Map.boatAccessible, "MOBILEMG42_side_door.*");
		Map.SetPriorities();
	},

	Docs_Taken = function()
	{
		Map.Defend_Boat(true);
		Util.MapDebugPrint( "Docs_Taken" );
	},

	Docs_Returned = function()
	{
		Map.Defend_Boat(false);
		Util.MapDebugPrint( "Docs_Returned" );
	},

	SetGoalPos = function(goal,wpname)
	{
		goal = GetGoal(goal);
		goal.SetPosition(Util.WpNameInfo(wpname).position);
		goal.DynamicPosition = false;
	},
};

global OnMapLoad = function()
{
	OnTrigger( "Church Defences Built", Map.Church_Defences_Built );
	OnTrigger( "Church Defences DESTROYED!", Map.Church_Defences_Destroyed );
	OnTrigger( "Abbey Defences BUILT!", Map.Abbey_Defences_Built );
	OnTrigger( "Abbey Defences DESTROYED!", Map.Abbey_Defences_Destroyed );
	OnTrigger( "Hill Defences BUILT!", Map.hill_defences_Built );
	OnTrigger( "Hill Defences DESTROYED!", Map.hill_defences_Destroyed );
	OnTrigger( "Back Step Defences Built", Map.Hillside_Defences_Built );
	OnTrigger( "Back Step Defences DESTROYED!", Map.Hillside_Defences_Destroyed );
	OnTrigger( "Allies captured the Church!", Map.cityflag_Allies_Captured );
	OnTrigger( "Axis reclaim the church!", Map.cityflag_Axis_Captured );
	OnTrigger( "Allies have destroyed the Abbey Door", Map.Abbey_Door_Destroyed );
	OnTrigger( "Allies have breached the abbey - side door", Map.Side_Door_Destroyed );
	OnTrigger( "Allied abbey command post built", Map.Abbey_Command_Post_Built );
	OnTrigger( "Axis team has destroyed the Allied abbey Command Post!", Map.Abbey_Command_Post_Destroyed );
	OnTrigger( "Allies have stolen the documents", Map.Docs_Taken );
	OnTrigger( "Axis have returned the documents", Map.Docs_Returned );

	Util.DisableGoal("BUILD_Side_Door");

	SetAvailableMapGoals(TEAM.ALLIES, false, {
		"MOUNTMG.*",
		"REPAIRMG.*",
	});

	SetGoalPriority("PLANT_Abbey_Command_Post", 0.59, TEAM.AXIS, CLASS.ENGINEER);
	SetGoalPriority("MOUNTMG42_Axis_MG", 0.65, TEAM.AXIS);
	SetGoalPriority("MOUNTMG42_boat_mg", 0.66, TEAM.AXIS);
	SetGoalPriority("DEFEND_boat.*", 0.53, TEAM.AXIS);
	SetGoalPriority("PLANTMINE_side.*", 0.71, TEAM.AXIS);
	SetGoalPriority("DEFUSE_Abbey_Defences.*", 0.51, TEAM.AXIS, CLASS.ENGINEER, true);
	SetGoalPriority("PLANT_Abbey_Door", 0.85, TEAM.ALLIES);
	SetGoalPriority("PLANT_Side_Door", 0.75, TEAM.ALLIES);
	SetGoalPriority("CHECKPOINT_cityflag", 0.6, TEAM.ALLIES);
	SetGoalPriority("ATTACK_boat.*", 0.49, TEAM.ALLIES);

	Map.Defend_Boat(false);
	Map.Door_Destroyed(false);

	foreach (id and goal in { ".*MG42.*", "DEFEND_", "SNIPE.*", "PLANTMINE.*",
		"BUILD_Church_Defences", "BUILD_Abbey_Defences", "BUILD_hill_defences", "BUILD_Hillside_Defences",
		"BUILD_Command_Post", "BUILD_Axis_MG",
		"PLANT_Side_Door", "PLANT_Abbey_Defences", "PLANT_Command_Post" }){
		Util.SetMaxUsersInProgress(1, goal);
	}
	Util.SetMaxUsersInProgress(5, "DEFEND_docs5");
	Util.SetMaxUsersInProgress(5, "DEFEND_boat3");

	SetMapGoalProperties("DEFEND_.*", {mincamptime=60, maxcamptime=250} );
	SetMapGoalProperties("MOUNTMG42_Axis_MG", {mincamptime=50, maxcamptime=200} );
	SetMapGoalProperties("MOUNTMG42_boat_mg", {mincamptime=900, maxcamptime=900} );
	SetMapGoalProperties("MOBILEMG.*", {mincamptime=50, maxcamptime=300} );
	SetMapGoalProperties("SNIPE.*", {mincamptime=60, maxcamptime=200} );

	Map.SetGoalPos("BUILD_Hillside_Defences", "hillside1");
	Map.SetGoalPos("PLANT_hill_defences", "hill_defences");
	Map.SetGoalPos("PLANT_Abbey_Defences", "abbey_defences_south");
	Util.AddUseWp("PLANT_Abbey_Door", "abbey_door_out");
	Util.AddUseWp("PLANT_Abbey_Door", "abbey_door");
	Util.SetGoalOffset(-10, 10, -10, "MOUNTMG42_boat_mg");

	Util.DisableGoal("ROUTE_side_door.*");
	Util.DisableGoal("ROUTE_abbey_door");

	MapRoutes =
	{
		CHECKPOINT_cityflag = {
			ROUTE_allies_spawn = {
				ROUTE_church_defences = {},
				ROUTE_allies1 = {
					ROUTE_town2 = { ROUTE_church_yard = { ROUTE_church_door = {}}},
					ROUTE_east_cp = {},
				},
				ROUTE_allies2 = { ROUTE_town3 = { ROUTE_church_tower = {}}},
				ROUTE_allies_south_wall = {ROUTE_east_cp = {}},
			},
			ROUTE_axis_spawn = {
				ROUTE_mg42 = {},
				ROUTE_abbey_defences_east = { Weight=3, ROUTE_church_yard = {}},
			},
		},
		BUILD_Command_Post = {
			ROUTE_allies_spawn = {
				ROUTE_east_cp = {},
			},
		},
		PLANT_Side_Door = {
			ROUTE_allies_spawn = {
				ROUTE_east_cp = { Weight=2, ROUTE_hill_east = {} },
				ROUTE_hill1 = {},
			},
			ROUTE_flag = {
				ROUTE_hill_east = { Weight=2 },
				ROUTE_hill1 = {},
			},
		},
		DEFEND_side_door2 = {
			ROUTE_flag = {
				ROUTE_hill1 = {},
			},
		},
		DEFEND_hillside4 = {
			ROUTE_flag = {
				ROUTE_north_east_wall = {},
				ROUTE_hill1 = {},
			},
		},
		PLANT_Command_Post = {
			ROUTE_axis_spawn = {
				ROUTE_north_east_wall = {},
			},
		},
		PLANT_Abbey_Command_Post = {
			ROUTE_axis_spawn = {
				ROUTE_abbey_door_in = {},
				ROUTE_cathedral_below_axis = {},
			},
		},
		DEFEND_docs1 = {
			ROUTE_axis_spawn = {
				ROUTE_docs_axis = {},
				ROUTE_abbey_door_in = {},
				ROUTE_axis_stairs2 = {},
			},
		},
		FLAG_docs = {
			ROUTE_cp_spawn = {
				ROUTE_abbey_door_in = {
					ROUTE_axis_stairs = {},
					ROUTE_docs4 = {},
				},
				ROUTE_ammo = {},
				ROUTE_cathedral_below = {},
				ROUTE_cloister2 = {},
				ROUTE_abbey_cp2 = { ROUTE_cathedral_stairs = { ROUTE_cloister2 = {}}},
			},
			ROUTE_flag = {
				ROUTE_mg42 = {
					ROUTE_cp_spawn = {
						ROUTE_ammo = {},
						ROUTE_cloister2 = {},
					},
					ROUTE_docs4 = {},
				},
				ROUTE_abbey_defences_east = {
					ROUTE_abbey_cp2 = {},
					ROUTE_axis_stairs = {},
				},
			},
		},
		CAPPOINT_boat = {
			ROUTE_docs = {
				ROUTE_side_door_in = { ROUTE_side_door = {}},
				ROUTE_cloister = {},
				ROUTE_abbey_door = { Weight=3,
					ROUTE_hill_east = { Weight=4 },
					ROUTE_hillside = {},
				},
			},
		},
	};
	MapRoutes.DEFEND_docs2 = MapRoutes.DEFEND_docs1;
	MapRoutes.DEFEND_docs5 = MapRoutes.DEFEND_docs1;
	MapRoutes.ATTACK_boat2 = MapRoutes.CAPPOINT_boat;
	MapRoutes.ATTACK_boat4 = MapRoutes.CAPPOINT_boat;
	MapRoutes.DEFEND_hillside3 = MapRoutes.CAPPOINT_boat;
	MapRoutes.DEFEND_hillside4 = MapRoutes.CAPPOINT_boat;

	Util.Routes(MapRoutes);

	Util.MapDebugPrint( "Omni_bot map script for " + GetMapName() + " executed." );

	Server.MinClassCount[TEAM.AXIS][CLASS.ENGINEER] += 1;
};
