//==========================================================================================
//
//	1944_antwerp_b3.gm
//
//	Who             When			What
//------------------------------------------------------------------------------------------
//	MickyP		27 February 2012	Initial Script
//
//==========================================================================================
//

global Map =
{
	ShowMovers = false,

	RideBoat = 0,
	BoatBuilt = 1,
	DocumentsSecured = 0,
	GeneratorDestroyed = 0,

	Boat_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }
		if (Map.DocumentsSecured == 1 && GeneratorDestroyed == 1 )
			{
				SetAvailableMapGoals( TEAM.AXIS, true, "RIDE_Boat" );
			}

			Map.BoatBuilt = 1;
		Util.MapDebugPrint( "Boat_Built" );
	},
	
	Boat_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Map.RideBoat = 0;
		SetAvailableMapGoals( TEAM.AXIS, false, "RIDE_Boat" );

		Util.MapDebugPrint( "Boat_Destroyed" );
	},

	Command_Post_Built = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "Command_Post_Built" );
	},

	forward_spawn_Axis_Captured = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "forward_spawn_Axis_Captured" );
	},

	forward_spawn_Allies_Captured = function( trigger )
	{
		if ( TestMap )
			{ return; }

		Util.MapDebugPrint( "forward_spawn_Allies_Captured" );
	},

	Allied_Generator_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }
		
		if ( Map.BoatBuilt == 1 )
			{
				SetAvailableMapGoals( TEAM.AXIS, true, "RIDE_Boat" );
			}

		Map.GeneratorDestroyed = 1;

		Util.MapDebugPrint( "Allied_Generator_Destroyed" );
	},

	Command_Post_Destroyed = function( trigger )
	{
		if ( TestMap )
			{ return; }


		Util.MapDebugPrint( "Command_Post_Destroyed" );
	},

	documents_Taken = function( trigger )
	{
		if ( TestMap )
			{ return; }

		// only have the thread running when needed
		if ( !Map.UpdateCappointThread ) {
			Map.UpdateCappointThread = thread(Map.UpdateCappoint);
		}
		
		SetAvailableMapGoals( TEAM.AXIS, true, "BUILD_Boat" );
		
		Util.MapDebugPrint( "documents_Taken" );
	},

	documents_Returned = function( trigger )
	{
		if ( TestMap )
			{ return; }
		// kill the the thread, since updating the position is pointless
		if ( Map.UpdateCappointThread ) {
			threadKill(Map.UpdateCappointThread);
			Map.UpdateCappointThread = null;
		}
		
		Util.MapDebugPrint( "documents_Returned" );
	},

	documents_Secured = function( trigger )
	{
		if ( TestMap )
			{ return; }
		// on successful cap, kill the update thread
		if ( Map.UpdateCappointThread ) {
			threadKill(Map.UpdateCappointThread);
			Map.UpdateCappointThread = null;
		}

		Map.DocumentsSecured = 1;
		
		if ( Map.BoatBuilt == 1 && GeneratorDestroyed == 1 )
			{
				SetAvailableMapGoals( TEAM.AXIS, true, "RIDE_Boat" );
			}
		
		SetAvailableMapGoals( TEAM.AXIS, false, "CAPPOINT_Boat" );
		SetAvailableMapGoals( TEAM.AXIS, true, "BUILD_Boat" );
		Util.MapDebugPrint( "documents_Secured" );
	},

	UpdateCappointThread = null,
	UpdateCappoint = function()
	{
		if(!Map.BoatGoal) { Map.BoatGoal = GetGoal("MOVER_boat"); }

        // / bot sgn 200 true in 0.71 while standing where the deliver spot is will show offset in console
		Map.CappointOffset = Vector3(0,-160,-30);

		if ( !Map.BoatGoal ) {
			if (Map.Debug) { print("UpdateCappoint: Invalid Vehicle Goalname!"); }
			Map.UpdateCappointThread = null;
			return;
		}

		if (!Map.CappointGoal) { Map.CappointGoal = GetGoal("CAPPOINT_Boat"); }

		if (!Map.CappointGoal) {
			if (Map.Debug) { print("UpdateCappoint: Invalid Cappoint Goalname!"); }
			Map.UpdateCappointThread = null;
			return;
		}

		moverEnt = Map.BoatGoal.GetEntity();
		// the main loop that updates the cappoint position
		while(1) {
		moverPos = Map.BoatGoal.GetPosition();

		// update the position only if the Boat has moved
		if ( moverPos != Map.BoatPosition ) {
			Map.BoatPosition = moverPos;
			newCapPos = GetEntityWorldSpace(moverEnt,Map.CappointOffset);
			Util.SetGoalPosition(newCapPos.x, newCapPos.y, newCapPos.z, "CAPPOINT_Boat");

			// toggle availability so they repath
			SetAvailableMapGoals( TEAM.ALLIES, false, "CAPPOINT_Boat" );
			sleep(0.5);
			SetAvailableMapGoals( TEAM.ALLIES, true, "CAPPOINT_Boat" );
		}
		sleep(0.5);
        }
    },
};

global OnMapLoad = function()
{
	if ( TestMapOn )
		{ Util.AutoTestMap(); }

	// Register callback functions
	OnTrigger( "The Boat has been repaired!", Map.Boat_Built );
	OnTrigger( "The Boat has been damaged!", Map.Boat_Destroyed );
	OnTrigger( "Allied Command Post constructed. Charge speed increased!", Map.Command_Post_Built );
	OnTrigger( "Axis Command Post constructed. Charge speed increased!", Map.Command_Post_Built );
	OnTrigger( "the Generator has been destroyed, the Boat is ready to move!", Map.Allied_Generator_Destroyed );
	OnTrigger( "Axis team has destroyed the Allied Command Post!", Map.Command_Post_Destroyed );
	OnTrigger( "Allied team has destroyed the Axis Command Post!", Map.Command_Post_Destroyed );
	OnTrigger( "Axis have stolen the documents!", Map.documents_Taken );
	OnTrigger( "Documents returned!", Map.documents_Returned );
	OnTrigger( "Axis have secured the documents!", Map.documents_Secured );
	OnTrigger( "Axis capture the Forward Spawn!", Map.forward_spawn_Axis_Captured );
	OnTrigger( "Allies capture the Forward Spawn!", Map.forward_spawn_Allies_Captured );


	Util.DisableGoal( ".*", true ); // all but routes
	
	//~SetAvailableMapGoals( TEAM.AXIS, true, "ATTACK_.*" );
	//~SetAvailableMapGoals( TEAM.ALLIES, true, "ATTACK_.*" );
	
	SetAvailableMapGoals( TEAM.AXIS, true, "BUILD_Command_Post" );
	SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_Command_Post" );
	SetAvailableMapGoals( TEAM.AXIS, true, "CAPPOINT_Boat" );
	SetAvailableMapGoals( TEAM.AXIS, true, "FLAG_documents" );
	SetAvailableMapGoals( TEAM.AXIS, true, "PLANT_Allied_Generator" );
	SetAvailableMapGoals( TEAM.AXIS, true, "CHECKPOINT_forward_spawn" );
	
	SetAvailableMapGoals( TEAM.ALLIES, true, "BUILD_Command_Post" );
	SetAvailableMapGoals( TEAM.ALLIES, true, "PLANT_Command_Post" );
	SetAvailableMapGoals( TEAM.ALLIES, true, "CHECKPOINT_forward_spawn" );

	// Max users per goal
	Util.SetMaxUsers( 1, "ATTACK_.*" );
	Util.SetMaxUsers( 1, "DEFEND_.*" );
	Util.SetMaxUsers( 1, "GRENADE_.*" );
	Util.SetMaxUsers( 1, "SNIPE_.*" );
	Util.SetMaxUsers( 1, "MOUNTMG42_.*" );
	Util.SetMaxUsers( 1, "MOBILEMG42_.*" );
	Util.SetMaxUsers( 1, "MOBILEMORTAR_.*" );
	Util.SetMaxUsers( 1, "AIRSTRIKE_.*" );
	Util.SetMaxUsers( 1, "CALLARTILLERY_.*" );
	Util.SetMaxUsers( 1, "BUILD_Command_Post" );
	Util.SetMaxUsers( 1, "PLANT_Command_Post" );

	// Camp times
	SetMapGoalProperties( "MOUNTMG42_.*", {MinCampTime=15, MaxCampTime=90} );
	SetMapGoalProperties( "MOBILEMG42_.*", {MinCampTime=10, MaxCampTime=60} );


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

global OnBotJoin = function( bot )
{
	// Uncomment for shootable breakables
	bot.TargetBreakableDist = 90.0;
	// Only set MaxViewDistance on maps with limited sight (e.g. fog)
	//~bot.MaxViewDistance = 2400;
};
