//==========================================================================================
//
//	falkenstein_sw2.gm
//
//	Who			When				What
//------------------------------------------------------------------------------------------
//	MickyP		15 November 2010	Initial Script
//	Mateos		19 December 2011	Missing Strings, script clearing, goals
//
//	maker@bigsighstudios.com 25 July 2018 converted for sw2
//==========================================================================================

global Map =
{
	Debug = 0, // Please set to zero before distributing your script

	generator_Dyno = 0,
	Lower_Entrance_Dyno = 0,
	Upper_Entrance_Dyno = 0,
	Fence_Doors_Dyno = 0,

	NavDoor = function(_this)
	{
		wpinfo = _this.navigation.wpinfo;
		if(wpinfo==null)
		{
			wpinfo = Util.WpNameInfo(_this.CurrentNavigation);
			_this.navigation.wpinfo = wpinfo;
		}
		pos = wpinfo.position;
		vec = wpinfo.facing.Normalize();
		pos1 = pos - vec * 50;
		pos1 = Vector3(pos1.x, pos1.y, pos.z);
		pos2 = pos + vec * 140;
		pos2 = Vector3(pos2.x, pos2.y, pos.z);

		for(i=0; i<4; i+=1)
		{
			if(_this.Goto(pos) == EVENT.PATH_FAILED )
			{
				return;
			}
			_this.AddAimRequest(Priority.High, "facing", wpinfo.facing);
			tr = TraceLine(pos, pos2, null, TRACE.SOLID, 0, true);
			if(tr.fraction == 1)
			{
				_this.Bot.MoveTowards(pos2);
				sleep(0.8);
				break;
			}
			sleep(0.1);
			_this.Bot.PressButton(BTN.USE);
			_this.Bot.MoveTowards(pos1);
			//wait while door opens
			sleep(1);
		}

	},

	Navigation =
	{
		axis_door1 =
		{
			navigate = function(_this) { Map.NavDoor(_this); },
		},
		axis_door2 =
		{
			navigate = function(_this) { Map.NavDoor(_this); },
		},
		axis_door3 =
		{
			navigate = function(_this) { Map.NavDoor(_this); },
		},
		axis_door4 =
		{
			navigate = function(_this) { Map.NavDoor(_this); },
		},

		ladder_pt =
		{
			navigate = function( _this )
			{
				_this.Bot.HoldButton( BTN.FORWARD, 3 );
				sleep( 3 );
			},
		},
	},

	// Generator
	generator_Planted = function( trigger )
	{
		Map.generator_Dyno += 1;

		Util.MapDebugPrint( "generator_Planted" );
	},
	generator_Defused = function( trigger )
	{
		Map.generator_Dyno -= 1;

		Util.MapDebugPrint( "generator_Defused" );
	},
	generator_Destroyed = function( trigger )
	{
		Map.generator_Dyno = 0;

		// Axis suicide, so the whole team is inside
		foreach ( id and bot in BotTable )
		{
			if ( bot.GetTeam == TEAM.AXIS )
			{
				bot.ExecCommand( "kill" );
			}
		}

		// Disable outside goals
		Util.DisableGoal( "CAMP_Def_Depot_.*" );
		Util.DisableGoal( "CHECKPOINT_checkpoint1" );
		Util.DisableGoal( "AMMOCAB_south_ammocabinet" );
		Util.DisableGoal( "HEALTHCAB_south_healthcabinet" );

		// Enable inside goals for Axis right now
		SetAvailableMapGoals( TEAM.AXIS, 1, {
			".*CAB_south_ammocabinet_.*",
			"BUILD_Neutral_Command_Post",
			"CAMP_Def_Caves_.*",
		});

		Util.MapDebugPrint( "generator_Destroyed" );

		// Wait until the gate is opened
		sleep( 18.0 );

		// Now that the gate is opened, enable inside goals for Allies
		SetAvailableMapGoals( TEAM.ALLIES, 1, {
			".*CAB_south_ammocabinet_.*",
			"PLANT_Lower_Entrance",
			"PLANT_Upper_Entrance",
		});

		Util.MapDebugPrint( "generator_gate_opened" );
	},

	// Fence Doors
	Fence_Doors_Built = function( trigger )
	{

		Util.MapDebugPrint( "Fence_Doors_Built" );
	},
	Fence_Doors_Planted = function( trigger )
	{
		Map.Lower_Entrance_Dyno += 1;

		Util.MapDebugPrint( "Fence_Doors_Planted" );
	},
	Fence_Doors_Defused = function( trigger )
	{
		Map.Fence_Doors_Dyno -= 1;

		Util.MapDebugPrint( "Fence_Doors_Defused" );
	},
	Fence_Doors_Destroyed = function( trigger )
	{
		Map.Fence_Doors_Dyno = 0;
		Wp.SetWaypointFlag( "fence_door_wp_1", "closed", 0 );
		Wp.SetWaypointFlag( "fence_door_wp_2", "closed", 0 );

		Util.MapDebugPrint( "Fence_Doors_Destroyed" );
	},

	// Lower Entrance
	Lower_Entrance_Planted = function( trigger )
	{
		Map.Lower_Entrance_Dyno += 1;

		Util.MapDebugPrint( "Lower_Entrance_Planted" );
	},
	Lower_Entrance_Defused = function( trigger )
	{
		Map.Lower_Entrance_Dyno -= 1;

		Util.MapDebugPrint( "Lower_Entrance_Defused" );
	},
	Lower_Entrance_Destroyed = function( trigger )
	{
		Map.Lower_Entrance_Dyno = 0;

		SetAvailableMapGoals( TEAM.ALLIES, 1, {
			"CAPPOINT_Train",
			"FLAG_boxy",
		});

		Wp.SetWaypointFlag( "lower_door_wp_1", "closed", 0 );
		Wp.SetWaypointFlag( "lower_door_wp_2", "closed", 0 );

		Util.MapDebugPrint( "Lower_Entrance_Destroyed" );
	},


	// Upper Entrance
	Upper_Entrance_Planted = function( trigger )
	{
		Map.Upper_Entrance_Dyno += 1;

		Util.MapDebugPrint( "Upper_Entrance_Planted" );
	},
	Upper_Entrance_Defused = function( trigger )
	{
		Map.Upper_Entrance_Dyno -= 1;

		Util.MapDebugPrint( "Upper_Entrance_Defused" );
	},
	Upper_Entrance_Destroyed = function( trigger )
	{
		Map.Upper_Entrance_Dyno = 0;

		SetAvailableMapGoals( TEAM.ALLIES, 1, {
			"CAPPOINT_Train",
			"FLAG_boxy",
		});

		Util.MapDebugPrint( "Upper_Entrance_Destroyed" );
	},


	// Boxy
	boxy_Taken = function( trigger )
	{
		Util.MapDebugPrint( "boxy_Taken" );
	},
	boxy_Returned = function( trigger )
	{
		Util.MapDebugPrint( "boxy_Returned" );
	},

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


	// Checkpoint
	checkpoint1_Axis_Captured = function( trigger )
	{
		Util.MapDebugPrint( "checkpoint1_Axis_Captured" );
	},
	checkpoint1_Allies_Captured = function( trigger )
	{
		Util.MapDebugPrint( "checkpoint1_Allies_Captured" );
	},


	// Neutral Command Post
	CP_Owner = 0,
	CP_Dyno = 0,

	Set_CP = function(_p) // p = TEAM.ALLIES or TEAM.AXIS or 0 (unbuilt)
	{
		switch(_p)
		{
			case 0:
			{
				Map.CP_Owner = 0;
				SetAvailableMapGoals( TEAM.ALLIES, 1, "BUILD_Neutral_Command_Post" );
				SetAvailableMapGoals( TEAM.ALLIES, 0, "PLANT_Neutral_Command_Post" );
				SetAvailableMapGoals( TEAM.AXIS,   1, "BUILD_Neutral_Command_Post" );
				SetAvailableMapGoals( TEAM.AXIS,   0, "PLANT_Neutral_Command_Post" );
			}
			case TEAM.ALLIES:
			{
				Map.CP_Owner = TEAM.ALLIES;
				SetAvailableMapGoals( TEAM.ALLIES, 0, "BUILD_Neutral_Command_Post" );
				SetAvailableMapGoals( TEAM.ALLIES, 0, "PLANT_Neutral_Command_Post" );
				SetAvailableMapGoals( TEAM.AXIS,   1, "PLANT_Neutral_Command_Post" );
				SetAvailableMapGoals( TEAM.AXIS,   0, "BUILD_Neutral_Command_Post" );
			}
			case TEAM.AXIS:
			{
				Map.CP_Owner = TEAM.AXIS;
				SetAvailableMapGoals( TEAM.ALLIES, 0, "BUILD_Neutral_Command_Post" );
				SetAvailableMapGoals( TEAM.ALLIES, 1, "PLANT_Neutral_Command_Post" );
				SetAvailableMapGoals( TEAM.AXIS,   0, "PLANT_Neutral_Command_Post" );
				SetAvailableMapGoals( TEAM.AXIS,   0, "BUILD_Neutral_Command_Post" );
			}
		}
		Util.MapDebugPrint("CP Owner: " + Map.CP_Owner );
	},
	Allied_CP_Built = function( trigger )
	{
		Map.Set_CP(TEAM.ALLIES);
		Util.MapDebugPrint( "Allied_CP_Built" );
	},
	Allied_CP_Destroyed = function( trigger )
	{
		Map.CP_Dyno = 0;
		Map.Set_CP(0);
		Util.MapDebugPrint( "Allied_CP_Destroyed" );
	},
	Axis_CP_Built = function( trigger )
	{
		Map.Set_CP(TEAM.AXIS);
		Util.MapDebugPrint( "Axis_CP_Built" );
	},
	Axis_CP_Destroyed = function( trigger )
	{
		Map.CP_Dyno = 0;
		Map.Set_CP(0);
		Util.MapDebugPrint( "Axis_CP_Destroyed" );
	},
	CP_Planted = function( trigger )
	{
		Map.CP_Dyno += 1;
		Util.MapDebugPrint( "CP_Planted" );
	},
	CP_Defused = function( trigger )
	{
		Map.CP_Dyno -= 1;
		Util.MapDebugPrint( "CP_Defused" );
	},

// Command Post END

};

global OnMapLoad = function()
{
	// Register callback functions
	// The announcements are trimmed to 71 chars, the maximum supported by Omni-Bot.

	OnTrigger( "Planted at the Generator.", Map.generator_Planted );
	OnTrigger( "Defused at the Generator.", Map.generator_Defused );
	OnTrigger( "Allies gained access to the base", Map.generator_Destroyed );

	OnTrigger( "Axis have built the Fence Door!", Map.Fence_Doors_Built );
	OnTrigger( "Planted at the Fence Doors.", Map.Fence_Doors_Planted );
	OnTrigger( "Defused at the Fence Doors.", Map.Fence_Doors_Defused );
	OnTrigger( "Allies have breached the Fence Door!", Map.Fence_Doors_Destroyed );

	OnTrigger( "Planted at Lower Entrance.", Map.Lower_Entrance_Planted );
	OnTrigger( "Defused at the Lower Entrance.", Map.Lower_Entrance_Defused );
	OnTrigger( "Allies destroyed the lower entrance!", Map.Lower_Entrance_Destroyed );

	OnTrigger( "Planted at the Upper Entrance.", Map.Upper_Entrance_Planted );
	OnTrigger( "Defused at the Upper Entrance.", Map.Upper_Entrance_Defused );
	OnTrigger( "Allies destroyed the upper entrance!", Map.Upper_Entrance_Destroyed );

	OnTrigger( "Allies have stolen the objective!", Map.boxy_Taken );
	OnTrigger( "Axis have returned the objective!", Map.boxy_Returned );
	OnTrigger( "ALLIES ESCAPED WITH THE objective!", Map.boxy_Secured );

	OnTrigger( "Axis captured the Depot Yard!", Map.checkpoint1_Axis_Captured );
	OnTrigger( "Allies captured the Depot Yard!", Map.checkpoint1_Allies_Captured );


	OnTrigger( "Allied Command Post constructed. Charge speed increased!",  Map.Allied_CP_Built );
	OnTrigger( "Axis team has destroyed the Allied Command Post!",          Map.Allied_CP_Destroyed );
	OnTrigger( "Axis Command Post constructed. Charge speed increased!",    Map.Axis_CP_Built );
	OnTrigger( "Allied team has destroyed the Axis Command Post!",          Map.Axis_CP_Destroyed );
	OnTrigger( "Planted at the Command Post.",                              Map.CP_Planted  );
	OnTrigger( "Defused at the Command Post.",                              Map.CP_Defused  );


  Util.DisableGoal( ".*", 1 ); // All but Routes

	Util.EnableGoal( "CHECKPOINT_checkpoint1" );

	// Allies
	SetAvailableMapGoals( TEAM.ALLIES, 1, {
		"PLANT_generator",
		"BUILD_Neutral_Command_Post",
	});

	// Axis
	SetAvailableMapGoals( TEAM.AXIS, 1, {
		"BUILD_Fence_Doors",
		"BUILD_Neutral_Command_Post",
		"CAMP_Def_Depot_.*",
	});

	SetGoalPriority( "CHECKPOINT_checkpoint1", 0.9 );
	SetGoalPriority( "BUILD_Neutral_Command_Post", 0.8 );

	Util.LimitToClass( "BUILD_Neutral_Command_Post", 0, 3 ); //engys
	Util.ExcludeClass( "CHECKPOINT_checkpoint1", 0, 3 ); //engys

	Util.LimitToClass( "PLANT_Neutral_Command_Post", 0, 5 ); //covys
	Util.LimitToClass( "PLANT_Fence_Doors", 2, 5 ); //covys


	// Unreachable otherwise
	//Util.SetGoalOffset( -50, 0, 0, "PLANT_generator" );
	Util.AddUsePoint( "PLANT_Generator", Vec3(4996,600,72) );

	Util.AddUsePoint( "BUILD_Fence_Doors", Vec3(6607,-3584,88) );
	Util.SetGoalPosition( 6617, -3568, 88, "BUILD_Fence_Doors" );
	Wp.SetWaypointFlag( "fence_door_wp_1", "closed", 1 );
	Wp.SetWaypointFlag( "fence_door_wp_2", "closed", 1 );

	Wp.SetWaypointFlag( "lower_door_wp_1", "closed", 1 );
	Wp.SetWaypointFlag( "lower_door_wp_2", "closed", 1 );

	if ( Map.Debug > 0 ) { Util.ShowActiveGoals(); }


	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 = 2500;
};

global InitializeRoutes = function()
{
	MapRoutes =
	{
		CHECKPOINT_checkpoint1 =
		{
		},
		BUILD_Neutral_Command_Post =
		{
		},
		PLANT_Neutral_Command_Post =
		{
		},
		PLANT_Upper_Entrance =
		{
		},
		PLANT_Lower_Entrance =
		{
		},
		BUILD_Fence_Doors =
		{
		},
		PLANT_Fence_Doors =
		{
		},

		FLAG_boxy = {},
		CAPPOINT_Train = {},

	};
	Util.Routes(MapRoutes);
};
