//==========================================================================================
//
//	frost2_final.gm
//
//	Who	When		What
//------------------------------------------------------------------------------------------
//	palota	12 November 2012	Initial Script
//	palota	29 May 2015	PLANTMINE, TRIPMINE, SNIPE, DEFEND_final, DEFEND_truck, DEFEND_village_SW, ATTACK_barrier, role, routes
//
//==========================================================================================
//

global Map =
{
	Debug = 0,
	CastleDoorOpened = false,
	towerDestroyed = 0,
	obj1Taken = false,
	obj2Taken = false,
	objSecured = 0,
	phase = 0,

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

	SetSpawn1 = function(bot)
	{
		if (bot.GetTeam() == TEAM.ALLIES){
			if (Map.phase >= 30 && Map.phase < 50){
				bot.ChangeSpawnPoint(RandInt(1,2)*4-3); //1 and 5, towers
			} else {
				bot.ChangeSpawnPoint(4); //village
			}
		}else{
			r=0;
			if (Map.phase >= 20){ r=2; }  //one tower destroyed
			if (Map.phase >= 30){ r=10; } //both towers destroyed
			if (Map.phase >= 40){ r=3; }  //allies escaping with truck
			if (Map.phase >= 45){ r=0; }  //truck is out of castle
			if (RandInt(0,9) < r || (Map.phase==20 && bot.HasRole(ROLE.DEFENDER1))){
				bot.ChangeSpawnPoint(3); //castle
			} else {
				bot.ChangeSpawnPoint(2); //village
			}
		}
	},

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

	Village_Door_Destroyed = function( trigger )
	{
		Util.EnableGoal("ROUTE_village_door");
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_village_door4");
		if(Map.phase < 20){
			SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_village_SW.*");
		}
		if(Map.phase < 30){
			SetAvailableMapGoals( TEAM.ALLIES, true, "SNIPE_window.*");
		}
		Util.MapDebugPrint( "Village_Door_Destroyed" );
	},

	tower_Destroyed = function()
	{
		sleep(2);
		Map.towerDestroyed += 1;
		SetAvailableMapGoals( TEAM.ALLIES, true, {
			"AMMOCAB_supply4xxx",
			"HEALTHCAB_health3xxx",
		});
		ClearGoalRole("SWITCH_gate", ROLE.INFILTRATOR);

		if(Map.phase < 40)
		{
			SetAvailableMapGoals( TEAM.AXIS, true, {
				"BUILD_Door_Controls",
				"BUILD_Side_Gate",
				"TRIPMINE_castle.*",
			});
			SetAvailableMapGoals( TEAM.ALLIES, true, {
				"FLAG_gold_.*",
				"PLANT_Door_Controls",
				"PLANT_Side_Gate",
			});
			if(Map.towerDestroyed > 1)
			{
				Map.phase = 30;
				SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_castle.*" );
				SetGoalPriority("SWITCH_gate", 0.5);
				SetGoalPriority("DEFEND_truck.*", 0.5);
			} else {
				Map.phase = 20;
				SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_castle.*");
				if(!Map.obj1Taken){
					SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_obj1_.*" );
				}
				if(!Map.obj2Taken){
					SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_obj2_.*" );
				}
				SetGoalPriority("SWITCH_gate", 0.7);
			}
		}

		if(Map.towerDestroyed > 1)
		{
			SetAvailableMapGoals( TEAM.AXIS, false, {
				"DEFEND_tower.*",
				"PLANTMINE_tower.*",
				"SNIPE_tower.*",
			});
			SetAvailableMapGoals( TEAM.ALLIES, false, {
				"ATTACK_tower.*",
				"SNIPE_window.*",
			});
		}else{
			SetAvailableMapGoals( TEAM.AXIS, false, {
				"DEFEND_village_door.*",
				"DEFEND_village_SW.*"
			});
		}

		Map.SetSpawn();
	},

	north_tower_Destroyed = function( trigger )
	{
		Util.EnableGoal("ROUTE_tower_north");
		Map.tower_Destroyed();
		Util.MapDebugPrint( "north_tower_Destroyed" );
	},

	south_tower_Destroyed = function( trigger )
	{
		Util.EnableGoal("ROUTE_tower_south");
		Map.tower_Destroyed();
		Util.MapDebugPrint( "south_tower_Destroyed" );
	},

	castleGate_Open = function( trigger )
	{
		Map.CastleDoorOpened = true;
		Util.LimitToTeam(TEAM.AXIS, "SWITCH_gate");
		ClearGoalRole("SWITCH_gate", ROLE.INFILTRATOR);
		Util.MapDebugPrint( "castleGate_Open" );
	},

	castleGate_Closed = function( trigger )
	{
		Map.CastleDoorOpened = false;
		Util.LimitToTeam(TEAM.ALLIES, "SWITCH_gate");
		if(!Map.towerDestroyed){
			SetGoalRole("SWITCH_gate", ROLE.INFILTRATOR);
		}
		Util.MapDebugPrint( "castleGate_Closed" );
	},

	obj1_Stolen = function( trigger )
	{
		Map.obj1Taken = true;
		sleep(3);
		if(Map.obj1Taken){
			SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_obj1_.*" );
			SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_truck.*" );
		}
		Util.MapDebugPrint( "obj1_Stolen" );
	},

	obj2_Stolen = function( trigger )
	{
		Map.obj2Taken = true;
		sleep(3);
		if(Map.obj2Taken){
			SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_obj2_.*" );
			SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_truck.*" );
		}
		Util.MapDebugPrint( "obj2_Stolen" );
	},

	obj1_Returned = function( trigger )
	{
		Map.obj1Taken = false;
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_obj1_.*" );
		if(!Map.obj2Taken || Map.objSecured > 0){
			SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_truck.*" );
		}
		Util.MapDebugPrint( "obj1_Returned" );
	},

	obj2_Returned = function( trigger )
	{
		Map.obj2Taken = false;
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_obj2_.*" );
		if(!Map.obj1Taken || Map.objSecured > 0){
			SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_truck.*" );
		}
		Util.MapDebugPrint( "obj2_Returned" );
	},

	obj_Secured = function( trigger )
	{
		Map.objSecured += 1;
		if(!Map.obj1Taken || !Map.obj2Taken){
			SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_truck.*" );
		}
		Util.MapDebugPrint( "obj_Secured" );
	},

	doorcontrols_Built = function( trigger )
	{
		Util.DisableGoal("ROUTE_front_gate.*");
		Util.DisableGoal("ROUTE_west_gate");
		SetGoalPriority("DEFEND_obj[12]_4", 0.4);
		Util.MapDebugPrint( "doorcontrols_Built" );
	},

	doorcontrols_Destroyed = function( trigger )
	{
		Util.EnableGoal("ROUTE_front_gate.*");
		Util.EnableGoal("ROUTE_west_gate");
		SetGoalPriority("DEFEND_obj[12]_4", 0.5);
		Util.MapDebugPrint( "doorcontrols_Destroyed" );
	},

	sidegate_Built = function( trigger )
	{
		Util.DisableGoal("ROUTE_side_gate");
		SetGoalPriority("DEFEND_obj2_[56]", 0.4);
		Util.MapDebugPrint( "sidegate_Built" );
	},

	sidegate_Destroyed = function( trigger )
	{
		Util.EnableGoal("ROUTE_side_gate");
		SetGoalPriority("DEFEND_obj2_[56]", 0.5);
		Util.MapDebugPrint( "sidegate_Destroyed" );
	},

	truckEscaping = function( trigger )
	{
		Map.phase = 40;
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"DEFEND_castle.*",
			"BUILD_Door_Controls",
			"BUILD_Side_Gate",
			"TRIPMINE_castle.*",
		});
		SetAvailableMapGoals( TEAM.AXIS, true, {
			"DEFEND_barrier.*",
			"DEFEND_tower[34]",
			"PLANTMINE_end.*",
		});
		SetAvailableMapGoals( TEAM.ALLIES, false, {
			"ATTACK_castle.*",
			"PLANT_Door_Controls",
			"PLANT_Side_Gate",
			"CAPPOINT_truck",
			"FLAG_.*"
		});
		SetAvailableMapGoals( TEAM.ALLIES, true, {
			"BUILD_Truck",
			"ESCORT_truck",
			"ATTACK_barrier.*",
		});
		SetGoalPriority("BUILD_Truck_Barrier_2", 0.9);
		SetGoalPriority("SWITCH_gate", 0.7);
		Util.OnTriggerPosition( "MOVER_truck", "TruckPassedCastle", 200.0, Map.truck_passed_castle );
		Map.SetSpawn();
		Util.MapDebugPrint( "truckEscaping" );
	},

	truck_passed_castle = function( trigger )
	{
		Map.phase = 45;
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_truck.*" );
		SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Truck_Barrier_2" );
		SetGoalPriority("SWITCH_gate", 0);
		Map.SetSpawn();
		Util.MapDebugPrint( "truck_passed_castle" );
	},

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

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

	Truck_Passed_Bridge = function( trigger )
	{
		SetGoalPriority("PLANT_Truck_Barrier_2", 0.95);
		SetAvailableMapGoals( TEAM.AXIS, false, "PLANTMINE_tower.*" );
		Util.OnTriggerPosition( "MOVER_truck", "barrier", 300.0, Map.truck_passed_barrier );
		Util.MapDebugPrint( "Truck_Passed_Bridge" );
	},

	truck_passed_barrier = function( trigger )
	{
		Map.phase = 50;
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"BUILD_Truck_Barrier_2",
			"DEFEND_barrier.*",
			"DEFEND_tower.*",
			"PLANTMINE_end.*",
		});
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_end.*" );
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_barrier.*" );
		SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_end.*" );
		Map.SetSpawn();
		Util.OnTriggerPosition( "MOVER_truck", "truck_final", 300.0, Map.truck_final );
		Util.MapDebugPrint( "truck_passed_barrier" );
	},

	truck_final = function( trigger )
	{
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_final.*" );
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_end[1236]" );
		SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_final.*" );
		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_end.*" );
		Util.MapDebugPrint( "truck_final" );
	},

	alliedEscaped = function( trigger )
	{
		ETUtil.WinningChat( TEAM.ALLIES );
		ETUtil.LosingChat( TEAM.AXIS );
		Util.MapDebugPrint( "alliedEscaped" );
	},
};

global OnMapLoad = function()
{
	OnTrigger( "^3Allies have destroyed the North Tower Wall!", Map.north_tower_Destroyed );
	OnTrigger( "^3Allies have destroyed the South Tower Wall!", Map.south_tower_Destroyed );
	OnTrigger( "^3Castle Gate is now open!", Map.castleGate_Open );
	OnTrigger( "^1Castle Gate is now closed!", Map.castleGate_Closed );
	OnTrigger( "^3Allied team has stolen Prototype X!", Map.obj1_Stolen );
	OnTrigger( "^3Allied team has stolen Prototype Y!", Map.obj2_Stolen );
	OnTrigger( "Flag returned gold_crate!", Map.obj1_Returned );
	OnTrigger( "Flag returned gold_crate2!", Map.obj2_Returned );
	OnTrigger( "^3Allied team has secured a Radar Prototype!", Map.obj_Secured );
	OnTrigger( "^2Allied team is escaping with the Radar Prototypes!", Map.truckEscaping );
	OnTrigger( "^3Allied team escapes with the Radar Prototypes!", Map.alliedEscaped );
	OnTrigger( "^3The Truck has passed the Bridge!", Map.Truck_Passed_Bridge );
	OnTrigger( "^3Allies have destroyed the Village Door!", Map.Village_Door_Destroyed );
	OnTrigger( "^1Axis have built the Door Controls!", Map.doorcontrols_Built );
	OnTrigger( "^3Allies have destroyed the Door Controls!", Map.doorcontrols_Destroyed );
	OnTrigger( "^1The Side Gate has been constructed !", Map.sidegate_Built );
	OnTrigger( "^3The Side Gate has been destroyed.", Map.sidegate_Destroyed );
	OnTrigger( "^1The Truck Barrier has been constructed.", Map.barrier_Built );
	OnTrigger( "^3The Truck Barrier has been destroyed.", Map.barrier_Destroyed );

	Util.DisableGoal( ".*", true );
	Util.DisableGoal({ "ROUTE_front_gate.*", "ROUTE_west_gate", "ROUTE_side_gate",
		"ROUTE_village_door", "ROUTE_tower_north", "ROUTE_tower_south" });

	Util.EnableGoal({
		".*_Command_Post",
		"HEALTHCAB_keg",
	});

	SetAvailableMapGoals( TEAM.AXIS, true, {
		"AMMOCAB_supply4xxx",
		"HEALTHCAB_health3xxx",
		"DEFEND_tower.*",
		"DEFEND_village_door.*",
		"BUILD_Truck_Barrier_2",
		"PLANTMINE_tower.*",
		"SNIPE_tower.*",
	});

	SetAvailableMapGoals( TEAM.ALLIES, true, {
		"PLANT_North_Tower",
		"PLANT_South_Tower",
		"PLANT_Village_Door",
		"CAPPOINT_truck",
		"SWITCH_gate",
		"ATTACK_tower.*",
	});

	SetGoalPriority("PLANT_Village_Door", 0.9);
	SetGoalPriority("PLANT_.*Tower", 0.85);
	SetGoalPriority("DEFEND_obj[12]_4", 0.4);
	SetGoalPriority("DEFEND_obj2_[56]", 0.4);
	SetGoalPriority("BUILD_Command_Post", 0.81, TEAM.ALLIES);
	SetGoalPriority("PLANT_Command_Post", 0.8, TEAM.ALLIES, CLASS.ENGINEER);
	SetGoalPriority("PLANT_Door_Controls", 0.8, TEAM.ALLIES, CLASS.ENGINEER);
	SetGoalPriority("BUILD_Truck_Barrier_2", 0.7);
	SetGoalPriority("ESCORT_truck", 0.6);
	SetGoalPriority("SNIPE_window.*", 0.81);
	SetGoalPriority("SWITCH_gate", 0.75);
	SetGoalPriority("DEFEND_truck.*", 0.55);
	SetGoalPriority("DEFUSE_Village_Door.*", 0.9, TEAM.AXIS, CLASS.ENGINEER, true);

	Util.SetMaxUsers(1, ".*_Command_Post");
	Util.SetMaxUsers(1, "BUILD_Side_Gate");
	Util.SetMaxUsers(1, "PLANT_Village_Door");

	Util.AddUsePoint("PLANT_South_Tower", Vec3(-828.747, -1074.035, 696.615));
	Util.AddUsePoint("PLANT_Truck_Barrier_2", { Vec3(2245.260, 31.715, 742.658), Vec3(2515.809, -1.275, 733.071) });

	SetGoalRole("SWITCH_gate", ROLE.INFILTRATOR);
	SetGoalRole({ "DEFEND_obj[12]_.*", "DEFEND_village_door.*", "DEFEND_village_SW.*" }, ROLE.DEFENDER1);

	SetMapGoalProperties( "DEFEND_.*", {mincamptime=15, maxcamptime=55} );
	SetMapGoalProperties( "SNIPE_.*", {mincamptime=20, maxcamptime=50} );

	Map.SetSpawn();

	Util.FixMoverPos("truck", Vec3(-1742.500, -239.000, 764.500), Vec3(-1888.000, -240.000, 708.000));

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

global InitializeRoutes = function()
{
	MapRoutes =
	{
		FLAG_gold_crate =
		{
			ROUTE_tower_north_spawn = {
				ROUTE_castle_north = {
					ROUTE_shaft = {},
					ROUTE_west_gate = {},
				},
				ROUTE_castle_south = {
					ROUTE_side_gate = { Weight=6 },
					ROUTE_west_gate = { Weight=3 },
					ROUTE_radar = { ROUTE_castle_spawn = {}, },
					ROUTE_radar2 = { ROUTE_castle_spawn = {}, },
				},
				ROUTE_front_gate = { ROUTE_front_gate2 = {
					ROUTE_side_gate = { Weight=6 },
					ROUTE_west_gate = { Weight=3 },
					ROUTE_castle_spawn = {},
					ROUTE_shaft = {},
				}},
			},
			ROUTE_allied_spawn =
			{
				ROUTE_village_door =
				{
					Weight=2,
					ROUTE_house_ladder =
					{
						ROUTE_bridge1 =
						{
							ROUTE_tower_north =
							{
							},
							ROUTE_tower_south =
							{
							},
						},
						ROUTE_center_S =
						{
							ROUTE_tower_north =
							{
							},
							ROUTE_tower_south =
							{
							},
						},
					},
					ROUTE_house_stairs =
					{
						ROUTE_center_S =
						{
						},
						ROUTE_bridge2 =
						{
							ROUTE_tower_north =
							{
							},
							ROUTE_tower_south =
							{
							},
						},
					},
				},
				ROUTE_village_door_N =
				{
					ROUTE_barrier3 =
					{
						ROUTE_center_N =
						{
							ROUTE_tower_north =
							{
							},
							ROUTE_tower_south =
							{
							},
						},
					},
				},
			},
		},
		PLANT_Side_Gate =
		{
			ROUTE_tower_north_spawn = {
				ROUTE_castle_south = {},
				ROUTE_front_gate = { ROUTE_front_gate2 = {}},
			},
		},
		BUILD_Door_Controls =
		{
			ROUTE_castle_spawn =
			{
				ROUTE_front_axisdoor =
				{
					Weight=2
				},
				ROUTE_castle_south =
				{
				},
			},
		},
		CAPPOINT_truck = {
			ROUTE_obj1 = {
				ROUTE_castle_north = {
				},
				ROUTE_castle_south = {
				},
				ROUTE_front_gate2 = { ROUTE_front_gate = {}},
			},
		},
		BUILD_Command_Post = {
			ROUTE_axis_spawn = {
				ROUTE_cp1 = {},
				ROUTE_cp2 = {},
			},
			ROUTE_allied_spawn = {
				ROUTE_cp1 = {},
				ROUTE_cp2 = {},
			},
			ROUTE_castle_spawn = {
				ROUTE_cp1 = {},
				ROUTE_cp2 = {},
			},
		},
		PLANT_Village_Door = {
			ROUTE_allied_spawn = {
				ROUTE_village_door_N = {},
				ROUTE_village_door_S = {},
			},
		},
		PLANT_North_Tower = {
			ROUTE_allied_spawn = {
				ROUTE_village_door =
				{
					Weight=2,
					ROUTE_house_ladder =
					{
						ROUTE_bridge1 =
						{
						},
						ROUTE_center_N =
						{
						},
					},
					ROUTE_house_stairs =
					{
						ROUTE_center_S =
						{
						},
						ROUTE_center_N =
						{
						},
						ROUTE_bridge2 =
						{
						},
					},
				},
				ROUTE_village_door_N =
				{
					ROUTE_barrier1 =
					{
						ROUTE_center_N =
						{
						},
					},
					ROUTE_barrier3 =
					{
						Weight=2,
						ROUTE_center_N =
						{
						},
						ROUTE_bridge1 =
						{
						},
					},
				},
			},
		},
		PLANT_South_Tower = {
			ROUTE_allied_spawn = {
				ROUTE_village_door =
				{
					Weight=2,
					ROUTE_house_ladder =
					{
						ROUTE_center_S =
						{
						},
						ROUTE_bridge1 =
						{
						},
					},
					ROUTE_house_stairs =
					{
						ROUTE_center_S =
						{
						},
						ROUTE_bridge2 =
						{
						},
					},
				},
				ROUTE_village_door_N =
				{
					ROUTE_barrier3 =
					{
						ROUTE_center_S =
						{
						},
						ROUTE_bridge1 =
						{
						},
					},
				},
			},
		},
	};
	MapRoutes["DEFEND_village_door.*"] =
		{
			ROUTE_axis_spawn =
			{
				ROUTE_axis_S =
				{
					Weight=1.5,
				},
				ROUTE_axis_SW =
				{
				},
			},
		};
	MapRoutes["DEFEND_tower.*"] =
		{
			ROUTE_axis_spawn =
			{
				ROUTE_axis_N =
				{
				},
				ROUTE_axis_NW =
				{
					ROUTE_center_NE =
					{
					},
				},
				ROUTE_barrier1 = {},
				ROUTE_barrier2 = {},
				ROUTE_barrier3 = {},
			},
		};
	MapRoutes["ATTACK_end.*"] =
		{
			ROUTE_allied_spawn =
			{
				ROUTE_allies_N = {},
				ROUTE_allies_W = {},
			},
		};
	MapRoutes.FLAG_gold_crate.ROUTE_tower_south_spawn = MapRoutes.FLAG_gold_crate.ROUTE_tower_north_spawn;
	MapRoutes.FLAG_gold_crate.ROUTE_tower_south = MapRoutes.FLAG_gold_crate.ROUTE_tower_south_spawn;
	MapRoutes.FLAG_gold_crate.ROUTE_tower_north = MapRoutes.FLAG_gold_crate.ROUTE_tower_north_spawn;
	MapRoutes.PLANT_Side_Gate.ROUTE_tower_south_spawn = MapRoutes.PLANT_Side_Gate.ROUTE_tower_north_spawn;
	MapRoutes.FLAG_gold_crate2 = MapRoutes.FLAG_gold_crate;
	MapRoutes.CAPPOINT_truck.ROUTE_obj2 = MapRoutes.CAPPOINT_truck.ROUTE_obj1;
	MapRoutes.PLANT_Command_Post = MapRoutes.BUILD_Command_Post;
	MapRoutes["ATTACK_tower[236]"] = MapRoutes.PLANT_North_Tower;
	MapRoutes["ATTACK_tower[148]"] = MapRoutes.PLANT_South_Tower;
	MapRoutes["DEFEND_village_SW.*"] = MapRoutes["DEFEND_village_door.*"];
	Util.Routes(MapRoutes);
};

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