Npcs/Tasks/SeatIdle.cs
using Sandbox.Npcs.Layers;

namespace Sandbox.Npcs.Tasks;

/// <summary>
/// Idles in the seat for a randomised duration, occasionally saying a line.
/// Runs indefinitely until the schedule is interrupted externally (e.g. by fleeing).
/// </summary>
public class SeatIdle : TaskBase
{
	private readonly string[] _lines;
	private readonly float _minWait;
	private readonly float _maxWait;
	private readonly float _commentChance;

	private TimeUntil _nextAction;

	public SeatIdle( string[] lines, float minWait = 3f, float maxWait = 8f, float commentChance = 0.35f )
	{
		_lines         = lines;
		_minWait       = minWait;
		_maxWait       = maxWait;
		_commentChance = commentChance;
	}

	protected override void OnStart()
	{
		_nextAction = Game.Random.Float( _minWait, _maxWait );
	}

	protected override TaskStatus OnUpdate()
	{
		if ( !_nextAction ) return TaskStatus.Running;

		// Occasionally say something
		var speech = Npc.Speech;
		if ( speech is not null && speech.CanSpeak && Game.Random.Float() < _commentChance )
		{
			var line = _lines[Game.Random.Int( 0, _lines.Length - 1 )];
			speech.Say( line, 2.5f );
		}

		_nextAction = Game.Random.Float( _minWait, _maxWait );

		// Never returns Success on its own — runs until interrupted
		return TaskStatus.Running;
	}
}