//==========================================================================================
//
//	password2.gm
//
//	Who	When		What
//------------------------------------------------------------------------------------------
//	MickyP	05 December 2011	Initial Script
//	palota	08 December 2012	bots can see the password, CHECKPOINT is enabled for Axis
//	palota	22 February 2013	ATTACK goals, MOUNTMG42, route to FLAG_Endoarm, changed goal availabilities
//
//==========================================================================================
//

global Map =
{
	DoorDown = 0,
	PassComplete = 0,
	PartsTaken = 0,
	PassDoorOpen = 0,
	PlayersAtPass = 0,
	digitCount = 1,

	Switches =
	{
		garage_door =
		{
			Enabled = true,
			Priority = 0,
			WaypointName = "switch_2",
			Timeout = 2000,
		},
	},

	PlayerCount =
	{
		Name = "PlayerNumber",
		TriggerOnClass = CLASS.ANYPLAYER,

		OnEnter = function(ent)
		{
			if( GetEntTeam(ent) == TEAM.ALLIES )
			{
				Map.PlayersAtPass += 1;
				Util.MapDebugPrint("Allied players near password: " + Map.PlayersAtPass);
				Map.ReadPassword();
			}
		},
		OnExit = function(ent)
		{
			if( GetEntTeam(ent) == TEAM.ALLIES )
			{
				Map.PlayersAtPass -= 1;
				Util.MapDebugPrint("Allied players near password: " + Map.PlayersAtPass);
			}
		},
	},

	Service_Door_Destroyed = function( trigger )
	{
		Util.EnableGoal( "ROUTE_OverView_Ladder_Access" );
		Util.EnableGoal( "ROUTE_service_shaft" );
		Util.MapDebugPrint( "Service_Door_Destroyed" );
	},

	SetNumSwitch = function()
	{
		SetAvailableMapGoals( TEAM.ALLIES, false, "SWITCH_num.*");
		if (Map.password && Map.digitCount < 5 && Map.PassComplete == 0)
		{
			SetAvailableMapGoals( TEAM.ALLIES, true, "SWITCH_num" + Map.password[Map.digitCount]);
		}
	},

	ReadPassword = function()
	{
		if (Map.PassDoorOpen != 2 || Map.PlayersAtPass == 0 || Map.password){ return; }

		Map.password = {};
		for(i=1; i<=4; i+=1)
		{
			v = GetEntFacing(GetEntityByName("t_disp" + i));
			x=v.x; z=v.z;
			if(x==1){ n=1; }
			else if(x==-1){ n=5; }
			else if(z==1){ n=3; }
			else if(z==-1){ n=7; }
			else if(x>0){
				if(z>0){ n=2; } else { n=8; }
			}else{
				if(z>0){ n=4; } else { n=6; }
			}
			Map.password[i] = n;
		}
		Util.MapDebugPrint( "Password is " + Map.password[1] + Map.password[2] + Map.password[3] + Map.password[4] );

		Map.SetNumSwitch();

		SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_password.*" );
		SetAvailableMapGoals( TEAM.ALLIES, true, "DEFEND_Endoarm_Room_.*" );

		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_Password_Room_.*" );
		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_Endoarm_Room_.*" );

		Util.SetMaxUsersInProgress(1, "SWITCH_Password_Door");
	},

	Password_Entered = function( trigger )
	{
		SetAvailableMapGoals( TEAM.AXIS, true, "SWITCH_Password_Reset" );
		SetAvailableMapGoals( TEAM.AXIS, false, "SWITCH_Password_Door" );

		Util.MapDebugPrint( "Password_Entered" );
	},

	SetPassDoor = function()
	{
		if (Map.PassDoorOpen){
			Util.LimitToTeam(TEAM.AXIS, "SWITCH_Password_Door");
			if(!Map.password){
				Util.SetMaxUsersInProgress(5, "SWITCH_Password_Door");
				SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_password.*" );
			}
		}else{
			SetAvailableMapGoals( TEAM.AXIS, false, "SWITCH_Password_Door" );
			if(!Map.password){
				SetAvailableMapGoals( TEAM.ALLIES, true, "SWITCH_Password_Door" );
				Util.SetMaxUsersInProgress(15, "SWITCH_Password_Door");
				SetAvailableMapGoals( TEAM.ALLIES, false, "ATTACK_password.*" );
			}
		}
	},

	Password_Reset = function( trigger )
	{
		Map.password = null;
		Map.digitCount = 1;
		SetAvailableMapGoals( TEAM.AXIS, false, "SWITCH_Password_Reset" );
		Map.SetPassDoor();

		SetAvailableMapGoals( TEAM.ALLIES, false, "DEFEND_Endoarm_Room_.*" );

		SetAvailableMapGoals( TEAM.AXIS, true, "DEFEND_Password_Room_.*" );
		SetAvailableMapGoals( TEAM.AXIS, false, "DEFEND_Endoarm_Room_.*" );

		Map.ReadPassword();

		Util.MapDebugPrint( "Password_Reset" );
	},

	Password_Sucessful = function( trigger )
	{
		Map.PassComplete = 1;

		Util.DisableGoal("SWITCH_Password_Door");
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"SWITCH_Password_Reset",
			"DEFEND_Password_Room_.*",
		});

		SetAvailableMapGoals( TEAM.ALLIES, false, {
			"ATTACK_password.*",
			"DEFEND_Endoarm_Room_.*",
		});
		SetAvailableMapGoals( TEAM.ALLIES, true, {
			"FLAG_Endoarm",
			"CAPPOINT_Truck",
		});

		Util.MapDebugPrint( "Password_Sucessful" );
	},

	Endoarm_Taken = function( trigger )
	{
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"DEFEND_Endoarm_Room_.*",
			"DEFEND_OverView_.*",
		});
		SetAvailableMapGoals( TEAM.AXIS, true, {
			"DEFEND_Truck_.*",
			"MOUNTMG42_Garage_Ramp",
		});

		SetAvailableMapGoals( TEAM.ALLIES, true, {
			"ATTACK_truck.*",
			".*MG42_Garage_Ramp",
		});

		Util.DisableGoal("CHECKPOINT_allied_flag");
		Map.PartsTaken = 1;
		Util.MapDebugPrint( "Endoarm_Taken" );
	},

	Endoarm_Returned = function( trigger )
	{
		SetAvailableMapGoals( TEAM.AXIS, true, {
			"DEFEND_Endoarm_Room_.*",
			"DEFEND_OverView_.*",
		});
		SetAvailableMapGoals( TEAM.AXIS, false, {
			"DEFEND_Truck_.*",
			"MOUNTMG42_Garage_Ramp",
		});

		SetAvailableMapGoals( TEAM.ALLIES, false, {
			"ATTACK_truck.*",
			".*MG42_Garage_Ramp",
		});

		Util.EnableGoal("CHECKPOINT_allied_flag");
		if(Map.DoorDown){
			SetAvailableMapGoals( TEAM.AXIS, true, "SWITCH_Garage_Door" );
		}
		Map.PartsTaken = 0;
		Util.MapDebugPrint( "Endoarm_Returned" );
	},

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

	allied_flag_Axis_Captured = function( trigger )
	{
		Util.SetMaxUsersInProgress(6, "CHECKPOINT_allied_flag");
		Util.MapDebugPrint( "allied_flag_Axis_Captured" );
	},

	allied_flag_Allies_Captured = function( trigger )
	{
		Util.SetMaxUsersInProgress(1, "CHECKPOINT_allied_flag");
		Util.MapDebugPrint( "allied_flag_Allies_Captured" );
	},


	door_trigger = function( trigger )
	{
		vel = ToVector(trigger.Action);
		if ( vel.z < 0 )
			{
			Map.DoorDown = 0;
			Map.Switches.garage_door.Enabled = true;
			SetAvailableMapGoals( TEAM.AXIS, false, "SWITCH_Garage_Door" );
			Util.MapDebugPrint( "door_closed" );
			}
		else if ( vel.z > 0 )
			{
			Map.DoorDown = 1;
			Map.Switches.garage_door.Enabled = false;
			if ( Map.PartsTaken == 0 )
				{
				SetAvailableMapGoals( TEAM.AXIS, true, "SWITCH_Garage_Door" );
				}
			Util.MapDebugPrint( "door_open" );
			}
	},

	password_trigger = function( trigger )
	{
		if ( Map.PassComplete == 1 )
			{ return; }

		vel = ToVector(trigger.Action);
		// vel.y can be zero if the button is pressed twice
		if ( vel.y < 0 )
		{
			Map.PassDoorOpen = 0;
			Map.SetPassDoor();
			Util.MapDebugPrint( "Pass_door_closed" );
		}
		else if ( vel.y > 0 )
		{
			Util.MapDebugPrint( "Pass_door_opening" );
			Map.PassDoorOpen = 1;
			Map.SetPassDoor();
			sleep(8); // Allow time for door to open
			if (Map.PassDoorOpen == 1) {
				Map.PassDoorOpen = 2;
				Map.ReadPassword();
			}
		}
	},

	password_enter = function( trigger )
	{
		if (ToVector(trigger.Action).z == -135000)
		{
			Util.MapDebugPrint( "Wrong password" );
			Map.digitCount = 1;
		}
		else
		{
			if (Map.digitCount<5) {
				Util.MapDebugPrint( "Password " + Map.digitCount + ". digit entered");
			}
			Map.digitCount += 1;
		}
		Map.SetNumSwitch();
	},

	password_door_button_start = function( trigger )
	{
		//the switch must be pressed only once
		Util.DisableGoal("SWITCH_Password_Door");
	},
};

global OnMapLoad = function()
{
	OnTrigger( "Allies breach the Service Door!", Map.Service_Door_Destroyed );
	OnTrigger( "Process started with valid password!", Map.Password_Entered );
	OnTrigger( "Process aborted...", Map.Password_Reset );
	OnTrigger( "Process finished!", Map.Password_Sucessful );
	OnTrigger( "Allies have stolen the Endoarm!", Map.Endoarm_Taken );
	OnTrigger( "Flag returned Endoarm!", Map.Endoarm_Returned );
	OnTrigger( "Allies escaped with the Endoarm!", Map.Endoarm_Secured );
	OnTrigger( "Allies capture the Service Halls!", Map.allied_flag_Allies_Captured );
	OnTrigger( "Axis reclaims the Service Halls!", Map.allied_flag_Axis_Captured );
	OnTrigger( "lift_start", Map.door_trigger );
	OnTrigger( "password_door_goto", Map.password_trigger );
	OnTrigger( "input_disp_start", Map.password_enter );
	OnTrigger( "password_door_button_start", Map.password_door_button_start);

	OnTriggerRegion(AABB(-2000.875,2530.848,769.125,-1600.125,2784.873,875.125),Map.PlayerCount);

	Util.DisableGoal( ".*", true ); // all but routes

	Util.DisableGoal( "ROUTE_OverView_Ladder_Access" );
	Util.DisableGoal( "ROUTE_service_shaft" );

	SetAvailableMapGoals( TEAM.ALLIES, true, {
		"PLANT_Service_Door",
		"CHECKPOINT_allied_flag",
		"SWITCH_Password_Door",
	});

	SetAvailableMapGoals( TEAM.AXIS, true, {
		"CHECKPOINT_allied_flag",
		"DEFEND_OverView_.*",
		"DEFEND_Password_Room_.*",
	});

	SetGoalPriority( "SWITCH_Password_Reset", 1.0 );
	SetGoalPriority( "FLAG_Endoarm", 0.7 );
	SetGoalPriority( "ATTACK_password.*", 0.7 );

	// Max users per goal
	Util.SetMaxUsersInProgress(15, "SWITCH_Password_Door");
	Util.SetMaxUsersInProgress(5, "SWITCH_Password_Reset");
	Util.SetMaxUsers(1, "DEFEND_.*" );
	Util.SetMaxUsersInProgress(2, "DEFEND_Endoarm_Room_.*" );
	Util.SetMaxUsersInProgress(7, "ATTACK_password.*" );

	SetMapGoalProperties( "DEFEND_.*", {mincamptime=10, maxcamptime=30} );

	Util.UpdateSwitchData();

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

global InitializeRoutes = function()
{
	MapRoutes =
	{
		CHECKPOINT_allied_flag =
		{
			ROUTE_Allied_Spawn = {
				ROUTE_Main_Entrance = {},
				ROUTE_Side_Entrance = {
					ROUTE_Service_Door = {},
				},
			},
		},
		SWITCH_Password_Door =
		{
			ROUTE_Allied_Spawn = {
				ROUTE_Flag_Spawn = {},
				ROUTE_Service_Door = {},
			},
			ROUTE_Flag_Spawn = {
				ROUTE_West_Stairs = {
					ROUTE_West_Stairs_Exit = {
						ROUTE_PW_Room_Stairs = {},
						ROUTE_Far_Road_Area = {
							ROUTE_Under_PW_Room = {
								ROUTE_PW_Room_Back_Door = {},
							},
						},
					},
					ROUTE_East_Stairs_Exit = {
						ROUTE_PW_Room_Stairs = {},
						ROUTE_Far_Road_Area = {},
					},
				},
				ROUTE_East_Stairs = {
					ROUTE_East_Stairs_Exit = {},
					ROUTE_West_Stairs_Exit = {},
				},
				ROUTE_Service_Door = {
					ROUTE_OverView_Ladder_Access = {
						ROUTE_OverView = {},
					},
				},
			},
			ROUTE_Axis_Spawn =
			{
				ROUTE_Axis_Tunnel = {
					ROUTE_Under_PW_Room = {
						ROUTE_PW_Room_Back_Door = {},
					},
					ROUTE_PW_Room_Stairs = {},
				},
				ROUTE_Axis_Ramp = {
					ROUTE_OverView = {
						ROUTE_PW_Room_Side_Door = {},
					},
				},
				ROUTE_PW_Room_Back_Door = {},
			},
		},
		FLAG_Endoarm = {
			ROUTE_Allied_Spawn = {
				ROUTE_Flag_Spawn = {},
				ROUTE_Service_Door = {},
			},
			ROUTE_Flag_Spawn = {
				ROUTE_West_Stairs = {
					ROUTE_West_Stairs_Exit = {
						ROUTE_stairs_exit = {},
						ROUTE_west_ladder = { Weight=2 },
					},
					ROUTE_East_Stairs_Exit = {
						ROUTE_stairs_exit = {},
						ROUTE_west_ladder = {},
					},
				},
				ROUTE_East_Stairs = {
					ROUTE_East_Stairs_Exit = {},
					ROUTE_West_Stairs_Exit = {},
				},
				ROUTE_Service_Door = { Weight=2,
					ROUTE_OverView_Ladder_Access = {
						ROUTE_OverView = {},
					},
					ROUTE_service_shaft = {},
				},
			},
		},
		CAPPOINT_Truck =
		{
			ROUTE_EndoArm = {
				ROUTE_East_Stairs = {},
				ROUTE_Garage_Ramp = {},
				ROUTE_OverView_Ladder_Access = {
					ROUTE_Service_Door = {
						ROUTE_MG_Drop = {},
						ROUTE_Side_Entrance = {},
						ROUTE_MG_Exit = {},
					},
				},
			},
		},
	};
	MapRoutes["ATTACK_password.*"] = MapRoutes.SWITCH_Password_Door;
	MapRoutes["DEFEND_Password_Room_.*"] = MapRoutes.SWITCH_Password_Door;
	MapRoutes["DEFEND_Endoarm_Room_.*"] = MapRoutes.FLAG_Endoarm;
	Util.Routes(MapRoutes);
};
