//==========================================================================================
//
//	mia1.gm
//
//	Who	When		What
//------------------------------------------------------------------------------------------
//	palota	18 September 2011	Initial Script
//
//==========================================================================================
//

global Map =
{
	Debug = 0,

	generatorDestroyed = false,
	generatorGateDestroyed = true,
	generatorFenceDestroyed = false,
	objTaken = false,
	elevatorPos = 1,
	callElevatorTime = 0,

	Navigation =
	{
		elevator_up =
		{
			navigate = function(_this)
			{
				//get inside
				_this.Goto(Vector3(4.629, 1229.875, -55.632));
				sleep(8);
				if (Map.elevatorPos==1)
				{
					//failed, get out
					_this.Goto(Vector3(4.387, 971.822, -55.875));
				}else{
					//wait until the elevator is down
					while(Map.elevatorPos<4){
						sleep(0.3);
					}
				}
			},
		},
		elevator_call =
		{
			EvalFunc = function(_this){
				return Map.elevatorPos==4 && _this.Bot.GetTeam()==TEAM.ALLIES && GetTime()-Map.callElevatorTime > 9;
			},
			navigate = function(_this)
			{
				//switch button if the elevator is down
				Map.callElevatorTime = GetTime();
				_this.Goto(Vector3(120.114, 983.871, -55.875));
				if (Map.elevatorPos==4){
					_this.AddAimRequest(Priority.High, "position", Vector3(118.6,1037.8,6.6));
					sleep(0.3);
					_this.Bot.PressButton(BTN.USE);
				}
			}
		},
	},

	Switches =
	{
		elevator =
		{
			Enabled = true,
			Priority = 0,
			WaypointName = "elevator",
			AimPosition = Vector3(118.6,1037.8,6.6)
		},
	},

	Roles =
	{
		ALLIES =
		{
			ATTACKER2 =
			{
				numbots = 3,
			},
		}
	},


	Main_Gate_Destroyed = function( trigger )
	{
		sleep(2);
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"DEFEND_gate.*",
			".*MG42_gate.*",
			".*ARTILLERY.*gate.*",
		});
		SetAvailableMapGoals( TEAM.AXIS, true, {
			".*MG42_Door.*",
			"DEFEND_door.*",
			"SNIPE_door.*",
		});

		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_gate.*" );
		SetAvailableMapGoals( TEAM.ALLIES, true, {
			"PLANT_Door_Lock",
			".*Command_Post",
			"AMMOCAB_ammo",
			"HEALTHCAB_health",
			"ATTACK_door.*",
			"GRENADE_door_mg.*",
		});

		Map.SetDoor(false);
		SetGoalPriority( "DEFEND_window.", 0.79);
		Util.MapDebugPrint( "Main_Gate_Destroyed" );
	},

	SetDoor = function(b)
	{
		SetAvailableMapGoals( TEAM.AXIS, !b, {
			"SNIPE_door.*",
			"DEFEND_door.*",
		});
		SetAvailableMapGoals( TEAM.ALLIES, !b, {
			"ATTACK_door.*",
			"ATTACK_roof.*",
			"SMOKEBOMB_door.*",
		});
		if(!Map.generatorDestroyed){
			SetAvailableMapGoals( TEAM.AXIS, b, "BUILD_generatorgate" );
			SetAvailableMapGoals( TEAM.ALLIES, b, "PLANT_generatorgate" );
		}
	},

	Door_Lock_Destroyed = function( trigger )
	{
		sleep(7);
		Map.SetDoor(true);
		if(!Map.objTaken){ Map.SetObj(false); }

		SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Axis_Lab" );
		SetAvailableMapGoals( TEAM.ALLIES, Map.generatorGateDestroyed, "PLANT_Axis_Generator" );
		Util.EnableGoal( "AMMOCAB_ammo_1" );
		Util.EnableGoal( "HEALTHCAB_health_1" );
		SetGoalPriority( "DEFEND_door.*", 0.45);
		SetGoalPriority( "GRENADE_door.*", 0.49);
		SetGoalPriority( "GRENADE_door.*", 0.65, TEAM.ALLIES, CLASS.FIELDOPS);
		Util.SetGoalRange("BUILD_Neutral_Command_Post", 2700);

		Util.MapDebugPrint( "Door_Lock_Destroyed" );
	},

	Lab_Destroyed = function( trigger )
	{
		SetAvailableMapGoals( TEAM.ALLIES, true, {
			"FLAG_allied_gold",
			"CAPPOINT_truck",
		});
		Util.MapDebugPrint( "Lab_Destroyed" );
	},


	generatorgate_Built = function( trigger )
	{
		Map.generatorGateDestroyed = false;
		if (!Map.generatorFenceDestroyed){
			SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_Axis_Generator" );
		}
		Util.MapDebugPrint( "generatorgate_Built" );
	},

	generatorgate_Destroyed = function( trigger )
	{
		Map.generatorGateDestroyed = true;
		if (!Map.generatorDestroyed) {
			SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Axis_Generator" );
		}
		Util.MapDebugPrint( "generatorgate_Destroyed" );
	},

	generatorFence_Destroyed = function( trigger )
	{
		Map.generatorFenceDestroyed = true;
		SetAvailableMapGoals( TEAM.AXIS, false, "BUILD_generatorgate" );
		SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_generatorgate" );
		if (!Map.generatorDestroyed) {
			SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Axis_Generator" );
		}
		Util.MapDebugPrint( "generatorFence_Destroyed" );
	},

	Generator_Destroyed = function( trigger )
	{
		Map.generatorDestroyed = true;
		SetAvailableMapGoals( TEAM.AXIS, false, "BUILD_generatorgate" );
		SetAvailableMapGoals( TEAM.ALLIES, false, "PLANT_generatorgate" );
		Util.MapDebugPrint( "Generator_Destroyed" );
	},


	SetObj = function(b)
	{
		Map.objTaken = b;
		SetAvailableMapGoals( TEAM.AXIS, !b, {
			"DEFEND_lab.*",
			"TRIPMINE_lab.*",
			"MOBILEMG42_lab.*",
			".*MG42_lower",
		});
		SetAvailableMapGoals( TEAM.AXIS, b, "DEFEND_cap.*" );
		SetAvailableMapGoals( TEAM.ALLIES, !b, "ATTACK_lab.*" );
		SetAvailableMapGoals( TEAM.ALLIES, b, {
			"ATTACK_cap.*",
			"ATTACK_stairs.*",
		});
	},

	allied_gold_Taken = function( trigger )
	{
		Map.SetObj(true);
		Util.MapDebugPrint( "allied_gold_Taken" );
	},

	allied_gold_Returned = function( trigger )
	{
		Map.SetObj(false);
		Util.MapDebugPrint( "allied_gold_Returned" );
	},

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


	window1_Destroyed = function( trigger )
	{
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_window1" );
		Util.MapDebugPrint( "window1_Destroyed" );
	},

	window2_Destroyed = function( trigger )
	{
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_window2" );
		Util.MapDebugPrint( "window2_Destroyed" );
	},

	window3_Destroyed = function( trigger )
	{
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_window3" );
		Util.MapDebugPrint( "window3_Destroyed" );
	},


	elever_goto = function( trigger )
	{
		if (ToVector(trigger.Action).z > 0)
		{
			Map.elevatorPos = 5;
			Util.MapDebugPrint( "elevator moving up" );
		}else{
			Map.elevatorPos = 2;
			Map.Switches.elevator.Enabled = false;
			Util.MapDebugPrint( "elevator door closing" );
		}
	},

	elev_door_topl_goto = function( trigger )
	{
		if (ToVector(trigger.Action).x < 0)
		{
			Map.Switches.elevator.Enabled = true;
			Wp.SetWaypointFlag("elevator_up", "closed", false);
			Map.elevatorPos = 1;
			Util.MapDebugPrint( "elevator up" );
		}else{
			sleep(1.7);
			Map.elevatorPos = 3;
			Util.MapDebugPrint( "elevator moving down" );
			Wp.SetWaypointFlag("elevator_up", "closed", true);
		}
	},

	elev_door_botl_goto = function( trigger )
	{
		if (ToVector(trigger.Action).x <= 0)
		{
			sleep(0.5);
			Map.elevatorPos = 4;
			Util.MapDebugPrint( "elevator down" );
			sleep(15);
		}
	},

};

global OnMapLoad = function()
{
	OnTrigger( "The allies destroyed the Main Gate. Deffend the Door Lock!", Map.Main_Gate_Destroyed );
	OnTrigger( "The allied team has destroyed the door locking mechanism and gained acc" /*ess to the lower base!"*/, Map.Door_Lock_Destroyed );
	OnTrigger( "allies have destroyed the lab!", Map.Lab_Destroyed );
	OnTrigger( "The Axis team has built the generatorgate!", Map.generatorgate_Built );
	OnTrigger( "The Allied team has destroyed the generatorgate!", Map.generatorgate_Destroyed );
	OnTrigger( "allies have blowen the generator!", Map.Generator_Destroyed );
	OnTrigger( "Allies have taken the Axis gold!", Map.allied_gold_Taken );
	OnTrigger( "Flag returned allied_gold!", Map.allied_gold_Returned );
	OnTrigger( "The Allies have secured the Axis gold", Map.allied_gold_Secured );
	OnTrigger( "GRENADE_window1 Exploded.", Map.window1_Destroyed );
	OnTrigger( "GRENADE_window2 Exploded.", Map.window2_Destroyed );
	OnTrigger( "GRENADE_window3 Exploded.", Map.window3_Destroyed );
	OnTrigger( "GRENADE_generator Exploded.", Map.generatorFence_Destroyed );
	OnTrigger( "elever_goto", Map.elever_goto );
	OnTrigger( "elev_door_topl_goto", Map.elev_door_topl_goto );
	OnTrigger( "elev_door_botl_goto", Map.elev_door_botl_goto );

	OnTriggerRegion(AABB(-563.898,-626.583,609.125,578.451,825.866,721.349),RegionTrigger.DisableCombatMovement); //roof

	Util.DisableGoal( ".*", true ); // all but routes
	SetAvailableMapGoals( TEAM.AXIS, true, {
		".*MG42_gate.*",
		".*Command_Post",
		"AMMOCAB_ammo",
		"DEFEND_gate.*",
		"DEFEND_window.",
		".*ARTILLERY.*gate.*",
	});

	SetAvailableMapGoals( TEAM.ALLIES, true, {
		"PLANT_Axis_Gate",
		"ATTACK_gate.*",
	});

	SetGoalPriority( "DEFEND_window.", 0.1);
	SetGoalPriority( "FLAG_allied_gold", 0.61);
	SetGoalPriority( "GRENADE.*", 0.6);
	SetGoalPriority( "ATTACK_roof.*", 0.61);
	SetGoalPriority( "DEFUSE_Neutral_Command_Post.*", 0.8, TEAM.ALLIES, CLASS.ENGINEER, true);
	SetGoalPriority( "REPAIRMG42_Door.*", 0.82);
	SetGoalPriority( "TRIPMINE.*", 0.82 );
	SetGoalPriority( "REPAIRMG42_lower", 0.86);
	SetGoalPriority( "SNIPE.*", 0.81);
	SetGoalPriority( "MOUNTMG42.*", 0.6, TEAM.AXIS);
	SetGoalPriority( "MOUNTMG42_Door.*", 0.4, TEAM.AXIS, CLASS.MEDIC);
	SetGoalPriority( "MOUNTMG42_Door.*", 0.4, TEAM.AXIS, CLASS.FIELDOPS);
	SetGoalPriority( "MOUNTMG42_Door.*", 0.4, TEAM.AXIS, CLASS.COVERTOPS);
	SetGoalPriority( "PLANT_generatorgate", 0.75);

	Util.SetRoleForGoals("ATTACK_roof.*", ROLE.ATTACKER2);
	Util.SetGoalRange("BUILD_Neutral_Command_Post", 0);

	// Max users per goal
	foreach (goal in { "DEFEND_.*", "ATTACK_.*", ".*MG42_.*", "GRENADE_.*",
		"SNIPE_.*", ".*Command_Post", "BUILD_generatorgate", "REPAIRMG42_lower" }){
		Util.SetMaxUsers(1, goal);
	}
	Util.SetMaxUsersInUse(1, "PLANT_Axis_Gate");
	Util.SetMaxUsersInUse(1, "PLANT_Door_Lock");
	Util.SetMaxUsersInProgress(2, "DEFEND_cap.*");
	Util.SetMaxUsersInProgress(2, "DEFEND_gate4");

	// Camp times
	SetMapGoalProperties( "MOUNTMG42_Door.*", {MinCampTime=15, MaxCampTime=25} );
	SetMapGoalProperties( "MOUNTMG42_lower", {MinCampTime=40, MaxCampTime=100} );
	SetMapGoalProperties( "MOBILEMG42_.*", {MinCampTime=30, MaxCampTime=90} );
	SetMapGoalProperties( "DEFEND_door.*", {MinCampTime=15, MaxCampTime=70} );
	SetMapGoalProperties( "ATTACK_stairs.*", {MinCampTime=10, MaxCampTime=40} );

	Util.SetGoalOffset(0, 10, -10, "MOUNTMG42_lower");
	Util.AddUsePoint( "PLANT_Axis_Lab", { Vec3(2049.633, 277.719, -2431.875), Vec3(2277.201, 272.825, -2431.875) });
	Wp.SetWaypointFlag("elevator_up", "closed", false);

	Util.UpdateSwitchData();

	MapRoutes =
	{
		PLANT_Axis_Gate = {
			ROUTE_allied_spawn = {
				ROUTE_allied_main = {},
				ROUTE_allied_west = { Weight=2.5,
					ROUTE_allied_center = {},
					ROUTE_allied_east = {},
				},
			}
		},
		MOUNTMG42_Door1 = {
			ROUTE_lower_spawn = {
				ROUTE_stairs_e = {},
				ROUTE_stairs_w = {},
			}
		},
		PLANT_Door_Lock = {
			ROUTE_allied_spawn = {
				ROUTE_side = { Weight=6,
					ROUTE_hall1 = {},
					ROUTE_hall2 = {},
					ROUTE_hall3 = {},
					ROUTE_hall4 = {},
				},
				ROUTE_hall5 = { Weight=2 },
				ROUTE_hall6 = {},
				ROUTE_hall7 = {},
				ROUTE_hall8 = { Weight=2 },
			},
		},
		ATTACK_door1 = {
			ROUTE_allied_spawn = {
				ROUTE_side = {}
			},
		},
		ATTACK_door3 = {
			ROUTE_allied_spawn = {
				ROUTE_roof = {}
			},
		},
		PLANT_Neutral_Command_Post = {
			ROUTE_axis_spawn = {
				ROUTE_side = {},
			}
		},
		FLAG_allied_gold = {
			ROUTE_cp_spawn = {
				ROUTE_side = {
					ROUTE_stairs_e = {
						ROUTE_lab1 = {},
						ROUTE_lab2 = {},
						ROUTE_lab_lower = { Weight=2 },
					},
					ROUTE_elevator = {
						ROUTE_lower_back = { Weight=2 },
						ROUTE_lab1 = {},
						ROUTE_lab2 = {},
					},
					ROUTE_stairs_w = { Weight=1.6,
						ROUTE_lower_back = {},
					},
				},
				ROUTE_stairs_e = { Weight=0.6, },
				ROUTE_elevator = {},
				ROUTE_stairs_w = {},
			},
			ROUTE_allied_spawn = {
				ROUTE_stairs_e = {},
				ROUTE_stairs_w = {},
				ROUTE_elevator = {},
			},
		},
		CAPPOINT_truck = {
			ROUTE_gold = {
				ROUTE_stairs_e = {
					ROUTE_allied_east = { ROUTE_allied_west = {}},
				},
				ROUTE_stairs_w = {},
			},
		},
	};
	MapRoutes.ATTACK_gate1 = MapRoutes.PLANT_Axis_Gate;
	MapRoutes.ATTACK_gate2 = MapRoutes.ATTACK_gate1;
	MapRoutes.ATTACK_gate3 = MapRoutes.ATTACK_gate1;
	MapRoutes.ATTACK_gate4 = MapRoutes.ATTACK_gate1;
	MapRoutes.ATTACK_gate5 = MapRoutes.ATTACK_gate1;
	MapRoutes.GRENADE_door_mg1 = MapRoutes.PLANT_Door_Lock;
	MapRoutes.GRENADE_door_mg2 = MapRoutes.GRENADE_door_mg1;
	MapRoutes.GRENADE_door_mg3 = MapRoutes.GRENADE_door_mg1;
	MapRoutes.ATTACK_door5 = MapRoutes.PLANT_Door_Lock;
	MapRoutes.ATTACK_door2 = MapRoutes.ATTACK_door1;
	MapRoutes.BUILD_Neutral_Command_Post = MapRoutes.PLANT_Neutral_Command_Post;
	MapRoutes.DEFEND_door12 = MapRoutes.PLANT_Neutral_Command_Post;
	MapRoutes.DEFEND_door10 = MapRoutes.DEFEND_door12;
	MapRoutes.DEFEND_door8 = MapRoutes.DEFEND_door12;
	MapRoutes.SNIPE_door4 = MapRoutes.DEFEND_door12;
	MapRoutes.MOUNTMG42_Door2 = MapRoutes.MOUNTMG42_Door1;
	MapRoutes.MOUNTMG42_Door_Lock = MapRoutes.MOUNTMG42_Door1;
	MapRoutes.REPAIRMG42_Door1 = MapRoutes.MOUNTMG42_Door1;
	MapRoutes.REPAIRMG42_Door2 = MapRoutes.MOUNTMG42_Door2;
	MapRoutes.REPAIRMG42_Door_Lock = MapRoutes.MOUNTMG42_Door_Lock;
	MapRoutes.PLANT_Axis_Lab = MapRoutes.FLAG_allied_gold;
	MapRoutes.ATTACK_lab1 = MapRoutes.FLAG_allied_gold;
	MapRoutes.ATTACK_lab2 = MapRoutes.ATTACK_lab1;
	MapRoutes.ATTACK_lab3 = MapRoutes.ATTACK_lab1;
	MapRoutes.ATTACK_lab4 = MapRoutes.ATTACK_lab1;
	Util.Routes(MapRoutes);

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

global OnBotJoin = function( bot )
{
	bot.TargetBreakableDist = 250.0;
	foreach ( weap in { WEAPON.THOMPSON,WEAPON.MP40,WEAPON.SHOTGUN } )
	{
		w = bot.GetWeapon(weap);
		if(w){ w.PrimaryFire.SetTargetBias(CLASS.BREAKABLE, 1.2); }
	}
};
