// This script contains functionality to trigger any available switches as set up in the global map table.

// This goal uses the global blackboard, so let's register a key for it.
if(!BB.SWITCHUSER)
{
	BB.SWITCHUSER = Blackboard.MakeKey();
}

// These parameters are required
this.Name = "UseSwitch_PT";		// The name of the goal.
this.Parent = "LowLevel";		// PathThrough states must be low level or they will not be used.

this.Initialize = function()
{
};

//this.OnSpawn = function()
//{
//};

this.OnPathThrough = function(data)
{
	if (this.IsActive())
		{ return; }
		
	//print("OnPathThrough:",data);
		
	switch = Map.Switches[data];
	if(switch)
	{
		this.CurrentSwitch = data;
		
		if( !switch.Timeout )
		{
			switch.Timeout = 2000;
		}
		
		this.counter = 0;
	
		if(switch.Enabled)
		{
			//print("OnPathThrough going for switch", this.CurrentSwitch);
			return true; // script will activate
		}
		else
		{
			//print("OnPathThrough switch not available", data);
		}
	}
	else
	{
		print("OnPathThrough no switch", data);
	}
	
	this.CurrentSwitch = null;
	return false;  // script will not activate
};

this.Enter = function()
{
	switch = Map.Switches[this.CurrentSwitch];
	
	bbRecord =
	{
		Owner = this.Bot.GetGameId(),
		Target = switch.Serial,
	};
	Blackboard.PostRecord(BB.SWITCHUSER, bbRecord);
	
	//print(this.Bot.Name, "UseSwitch Enter", threadId());
};

this.Exit = function()
{
	// Remove myself from the blackboard for using this switch
	Blackboard.RemoveByPoster(this.Bot.GetGameId(), BB.SWITCHUSER);
	
	//print(this.Bot.Name, "UseSwitch Exit", threadId());
};

this.Update = function()
{
	//print(this.Bot.Name, "UseSwitch Update", threadId());
	
	switch = Map.Switches[this.CurrentSwitch];
        	
	wpPos = switch.Waypoint.position;
	radius = switch.Waypoint.radius;
	wpFacing = switch.Waypoint.facing;
	
	if( this.Goto(wpPos, radius) == EVENT.PATH_SUCCESS )
	{
		switch = Map.Switches[this.CurrentSwitch];
		pos = switch.Waypoint.position;
		facing = switch.Waypoint.facing;
		
		assert(pos && facing);
		this.AddAimRequest(Priority.High, "facing", facing);

		exitCondition = false;
		while( !exitCondition && switch.Enabled && this.counter < switch.Timeout )
		{
			this.counter += 250;
			this.Bot.MoveTowards(pos);
		
			//print(this.Bot.Name, "BTN.USE(", threadId(), ") t(", sysTime(),") LimitTeam", switch.LimitTeam);
			this.Bot.PressButton( BTN.USE);

			if ( switch.ExitConditions )
			{
				foreach ( id and func in switch.ExitConditions )
				{
					if ( func(this.Bot) == true )
					{ 
						if ( switch.Debug )
							{ print(this.Bot.Name, "^2exit condition met for", switch.WaypointName); }

						exitCondition = true;
						break;
					}
				}				
			}

			sleep(0.25);
		}     
		
		this.ReleaseAimRequest();
		if ( !exitCondition && switch.Wait && this.counter < switch.Timeout)
		{
			while ( switch.Wait() != true )
			{
				this.Goto(this.Bot.GetPosition());
				yield();
			}
		}             
	}
	else
	{
		this.BlackboardDelay(30, switch.Serial);
	}
	
        // Finish the state.
	this.Finished();
};
