Sump/Flashbacks/TalkShowFlashback.cs

One level ("flashback") in a horror game: a TV studio where the player must stand on lit floor marks and perform a demanded action (jump, hold still, twirl, bow) before a timer, then in a finale break the red lights on three cameras and leave through a fire door. Builds the arena with Slab/Part/Human helpers and runs the cue and finale state machines.

Really Bad Code
using System;
using System.Collections.Generic;

/// <summary>
/// TALK-SHOW NIGHTMARE: a game of hitting your marks. A TV studio: a stage with a desk and a
/// sofa under hot lights, three cameras on dollies, an applause sign, an audience in tiers. The
/// host asks questions that get crueller; the audience meter is kept alive by standing on the
/// mark the floor manager lights and doing what the sign demands before the time runs out
/// (jump, hold still, twirl, bow: the keys the player already has). In the last act the audience
/// comes down off the tiers to touch you (the crowd), and the way out is to break the red lights
/// on the three cameras and leave by the fire door while ON AIR is still lit.
///
/// For a misfit that was worshipped and then sold it is dressed as a shopping-channel auction:
/// the host is the seller, the meter is the bidding, and a price ticker runs over the stage.
/// </summary>
public sealed class TalkShowFlashback : Flashback
{
	// ---- Tuning ---------------------------------------------------------------------------------
	/// <summary>Seconds to reach the mark and do the thing: on the first question, and on the last.</summary>
	const float WindowFirst = 10f, WindowLast = 6f;
	/// <summary>Seconds after the question before the sign lights.</summary>
	const float SignDelay = 1.6f;
	const float GapBetween = 2.2f;
	const float MeterStart = 0.6f, MeterGain = 0.14f, MeterLoss = 0.24f, MeterDrain = 0.012f, OffStageDrain = 0.06f;
	const float MarkRadius = 64f;
	const float StillSeconds = 1.4f, TwirlDegrees = 300f;
	/// <summary>Seconds ON AIR stays lit once the audience comes down.</summary>
	const float FinaleSeconds = 85f;
	/// <summary>Seconds between one tier of the audience coming down and the next.</summary>
	const float TierGap = 7f;
	const float CameraTouch = 60f;

	bool Auction => Profile.Contact == FirstContact.WorshippedThenSold;
	public override string GenreTitle => Auction ? "TALK-SHOW NIGHTMARE: THE AUCTION" : "TALK-SHOW NIGHTMARE";
	string HostName => Auction ? "SELLER" : "HOST";

	enum Demand { Jump, Still, Twirl, Bow }

	static readonly Vector3[] Marks =
	{
		new( -620f, -250f, 0f ), new( -500f, 0f, 0f ), new( -620f, 250f, 0f ), new( -330f, 0f, 0f ),
	};
	static readonly string[] MarkNames = { "A", "B", "C", "D" };
	static readonly Vector3 FireDoor = new( 280f, -900f, 0f );
	static readonly Vector3 FatGuySeat = new( 640f, -360f, 0f );

	static readonly string[] ShowQuestions =
	{
		"Ladies and gentlemen, you know it from the clip! Forty million views! Give it up for... what do we call you? {name}? Sure!",
		"So you crossed, what, a galaxy? And the first thing you did was get stuck in a revolving door. For six hours. Walk us through it.",
		"Do the face. The one from the clip. No? Look, it's doing it anyway!",
		"My kids have you on a lunchbox. Do you see a cent of that? No! Isn't that wonderful, folks?",
		"We hear you can't stand {fear}. Should we get some out here? Let's get some out here.",
		"What's that it's clutching? {thing}? Did you steal that? It steals, ladies and gentlemen!",
		"Is there anyone at home? Anyone who came looking for you? ...Anyone at all?",
		"They say you only matter while people are watching. So what happens when we go to a break?",
		"Last question. When we stop laughing... where do you go?",
		"No, really. Where do you go? Does anybody know where it goes?",
	};

	static readonly string[] AuctionPitches =
	{
		"Welcome back to Heaven's Bargains! Lot forty-four: one genuine sky-visitor, formerly a god. {name}, they called it. Bidding opens at five hundred!",
		"A whole valley knelt to this. Candles, hymns, the lot. Then the harvest failed, and here we are! Who'll give me a thousand?",
		"Show them your good side. It has one. Other way. No. Is that the front? Lovely.",
		"Still blesses! Sort of! Give us a blessing. Any blessing at all.",
		"Now, it is nervous around {fear}, so it's ideal for a quiet home.",
		"Comes with {thing}! It won't let go of it, so that's included at no extra charge.",
		"The elders who sold it cried, folks. Cried all the way to the bank!",
		"Two thousand, do I hear three? It used to be worth a village. Three, and I throw in a ham.",
		"It answered prayers, you know. Never had one of its own answered. Going once...",
		"Going twice...",
	};

	// ---- State ----------------------------------------------------------------------------------
	sealed class Camera
	{
		public GameObject Object;
		public GameObject Tally;
		public PointLight Light;
		public Vector3 Home;      // arena frame
		public float Phase;
		public bool Broken;
	}

	sealed class Seat
	{
		public CrowdPerson Person;
		public Vector3 Post;      // world
		public int Tier;
	}

	readonly List<Camera> _cameras = new();
	readonly List<Seat> _audience = new();
	readonly List<GameObject> _markObjects = new();
	CrowdPerson _host, _manager;
	PointLight _markLight, _signLight, _onAirLight;
	GameObject _applause, _onAir, _ticker, _fireDoor, _keepsake;
	Vector3 _keepsakeAt;

	int _questions, _cue, _mark = -1, _broken, _released;
	Demand _demand;
	float _clock, _cueStarted, _window, _nextCueAt, _meter = MeterStart, _hold, _spin, _lastYaw, _finaleStarted;
	bool _cueActive, _signOn, _finale, _dead, _doorOpen, _escaped, _keepsakeTaken, _fatGuyUp, _memeLine;

	// ---- Building -------------------------------------------------------------------------------

	public override void Build()
	{
		var a = Arena;
		string[] shows = Auction
			? new[] { "Heaven's Bargains, Channel 61", "The Miracle Mart Hour", "Sky Deals Live, a shopping channel out of Tulsa" }
			: new[] { "Up Too Late with Larry Voss, Studio 6", "The Bobby Dane Show, from a basement in Burbank", "Wakey Wakey Nation, live and regrettable" };
		Place = $"{Pick( shows )}. {Rng.Next( 1978, 2012 )}";

		_questions = 8 + Rng.Next( 0, 3 );

		a.Ground( 1300f, 1120f, new Color( 0.08f, 0.08f, 0.1f ) );
		a.Crowd.HalfY = 900f;
		a.Air( new Color( 0.1f, 0.1f, 0.14f ), new Color( 0.01f, 0.01f, 0.015f ), 2500f, 9000f );

		BuildStudio();
		BuildStage();
		BuildFloor();
		BuildAudience();

		Start = a.P( -420f, 0f, 2f );
		StartYaw = a.Yaw( 0f );

		var rules = Rules;
		rules.BoundsRadius = 2600f;
		rules.PinLoseSeconds = 6f;
		rules.PinLoseLine = Auction ? "Every one of them took a piece home." : "Everybody wanted to touch it.";
		rules.Won = () => _escaped;
		rules.Lost = () => _dead ? (Auction ? "No more bids. Lot withdrawn." : "Dead air. They cut to a commercial and wheeled you off.") : null;
		rules.MeterLabel = Auction ? "BIDS" : "AUDIENCE";
		rules.Meter = _meter;
		rules.RewindGrace = FinaleSeconds * 0.9f;
	}

	void BuildStudio()
	{
		var a = Arena;
		var dark = new Color( 0.06f, 0.06f, 0.08f );
		const float top = 520f;

		a.Slab( "Back Wall", -1120f, -1100f, -920f, 920f, 0f, top, dark );
		a.Slab( "Far Wall", 1250f, 1270f, -920f, 920f, 0f, top, dark );
		a.Slab( "North Wall", -1120f, 1270f, 900f, 920f, 0f, top, dark );
		a.Slab( "South Wall", -1120f, 200f, -920f, -900f, 0f, top, dark );
		a.Slab( "South Wall", 360f, 1270f, -920f, -900f, 0f, top, dark );
		a.Slab( "Over The Fire Door", 200f, 360f, -920f, -900f, 136f, top, dark );
		a.Slab( "Lighting Grid", -1120f, 1270f, -920f, 920f, top, top + 20f, new Color( 0.03f, 0.03f, 0.04f ), true, null, false );

		// The fire door, and the way out behind it.
		_fireDoor = a.Slab( "Fire Door", 200f, 360f, -916f, -904f, 0f, 136f, new Color( 0.15f, 0.4f, 0.2f ) );
		a.Slab( "Exit Passage Wall", 180f, 200f, -1120f, -920f, 0f, 200f, dark );
		a.Slab( "Exit Passage Wall", 360f, 380f, -1120f, -920f, 0f, 200f, dark );
		a.Slab( "Exit Passage End", 180f, 380f, -1120f, -1100f, 0f, 200f, new Color( 0.9f, 0.95f, 1f ) );
		a.Slab( "Exit Passage Roof", 180f, 380f, -1120f, -920f, 200f, 216f, dark, true, null, false );
		a.Lamp( "Exit Light", new Vector3( 280f, -1040f, 120f ), new Color( 0.7f, 1f, 0.8f ), 420f, 1.2f );
		a.Sign( "EXIT", new Vector3( 280f, -896f, 160f ), 90f, 16f, new Color( 0.3f, 1f, 0.5f ) );

		// ON AIR, over the fire door.
		a.Slab( "On Air Box", 215f, 345f, -900f, -892f, 180f, 222f, new Color( 0.25f, 0.02f, 0.02f ), false, null, false );
		_onAir = a.Sign( "ON AIR", new Vector3( 280f, -888f, 200f ), 90f, 22f, Color.Red );
		_onAirLight = a.Lamp( "On Air Glow", new Vector3( 280f, -860f, 200f ), Color.Red, 360f, 1.2f );
	}

	void BuildStage()
	{
		var a = Arena;
		var carpet = Auction ? new Color( 0.5f, 0.4f, 0.1f ) : new Color( 0.35f, 0.08f, 0.12f );
		a.Slab( "Stage", -950f, -250f, -520f, 520f, 0f, 1.5f, carpet, false, null, false );

		// The set: bright panels behind, a desk for the host, a sofa for the guest.
		var set = Auction ? new Color( 0.95f, 0.8f, 0.3f ) : new Color( 0.2f, 0.35f, 0.75f );
		for ( int i = 0; i < 5; i++ )
			a.Slab( "Set Panel", -1098f, -1090f, -480f + i * 200f, -320f + i * 200f, 20f, 300f + (i % 2) * 60f,
				Color.Lerp( set, Color.White, (i % 2) * 0.35f ), false, null, false );
		a.Slab( "Desk", -800f, -690f, 160f, 340f, 0f, 40f, new Color( 0.4f, 0.26f, 0.14f ) );
		a.Slab( "Desk Mug", -760f, -748f, 200f, 212f, 40f, 52f, Color.White, false, null, false );
		a.Slab( "Sofa Seat", -830f, -750f, -340f, -160f, 0f, 22f, new Color( 0.65f, 0.2f, 0.2f ) );
		a.Slab( "Sofa Back", -850f, -830f, -340f, -160f, 0f, 52f, new Color( 0.6f, 0.18f, 0.18f ) );

		// The marks: tape on the floor. The one wanted is lit.
		for ( int i = 0; i < Marks.Length; i++ )
			_markObjects.Add( a.Block( "Mark " + MarkNames[i], Marks[i] + Vector3.Up * 2f, new Vector3( 46f, 46f, 1f ), new Color( 0.25f, 0.25f, 0.28f ), false, null, false ) );
		_markLight = a.Lamp( "Mark Light", Marks[1] + Vector3.Up * 70f, new Color( 1f, 0.95f, 0.5f ), 260f, 2.5f );
		_markLight.GameObject.Enabled = false;

		// Hot lights.
		var warm = new Color( 1f, 0.92f, 0.78f );
		a.Spot( "Key Light", new Vector3( -200f, 0f, 500f ), new Vector3( -560f, 0f, 0f ), warm, 34f, 3.2f, true );
		a.Spot( "Key Light", new Vector3( -250f, -420f, 500f ), new Vector3( -680f, -250f, 0f ), warm, 30f, 2.6f );
		a.Spot( "Key Light", new Vector3( -250f, 420f, 500f ), new Vector3( -720f, 250f, 0f ), warm, 30f, 2.6f );
		a.Lamp( "Set Glow", new Vector3( -1000f, 0f, 260f ), set, 900f, 0.8f );

		// The price ticker over the stage (the auction only).
		if ( Auction )
		{
			a.Slab( "Ticker Board", -1090f, -1082f, -330f, 330f, 330f, 400f, new Color( 0.02f, 0.02f, 0.02f ), false, null, false );
			_ticker = a.Sign( "LOT 44", new Vector3( -1078f, 0f, 365f ), 0f, 26f, new Color( 1f, 0.8f, 0.2f ) );
		}

		// The host, behind the desk; and the thing the misfit will never put down, on the sofa's arm
		// where a guest before it left it.
		var suit = Auction
			? new HumanLook { Shirt = Color.White, Trousers = new Color( 0.5f, 0.4f, 0.1f ), Coat = new Color( 0.85f, 0.7f, 0.2f ) }
			: new HumanLook { Shirt = Color.White, Trousers = new Color( 0.1f, 0.1f, 0.2f ), Coat = new Color( 0.12f, 0.14f, 0.3f ) };
		_host = a.Human( HostName, new Vector3( -860f, 250f, 0f ), 0f, suit );
		_keepsakeAt = new Vector3( -860f + R( -20f, 30f ), -120f + R( -10f, 40f ), 0f );
		_keepsake = a.Keepsake( _keepsakeAt );
	}

	void BuildFloor()
	{
		var a = Arena;

		// Three cameras on dollies between the stage and the audience, each with a red light on top.
		for ( int i = 0; i < 3; i++ )
		{
			var home = new Vector3( -40f + R( -30f, 30f ), -420f + i * 420f, 0f );
			var go = a.Holder( "Camera " + (i + 1), home );
			var grey = new Color( 0.3f, 0.3f, 0.33f );
			a.Part( go, "Dolly", false, new Vector3( 0f, 0f, 5f ), new Vector3( 50f, 46f, 10f ), new Color( 0.1f, 0.1f, 0.1f ) );
			a.Part( go, "Column", false, new Vector3( 0f, 0f, 38f ), new Vector3( 8f, 8f, 58f ), grey );
			a.Part( go, "Head", false, new Vector3( 0f, 0f, 76f ), new Vector3( 46f, 24f, 24f ), grey );
			a.Part( go, "Lens", true, new Vector3( -27f, 0f, 76f ), new Vector3( 16f ), new Color( 0.05f, 0.05f, 0.08f ) );
			var tally = a.Part( go, "Red Light", true, new Vector3( -6f, 0f, 93f ), new Vector3( 9f ), Color.Red );
			var light = a.Lamp( "Red Light Glow", home + Vector3.Up * 100f, Color.Red, 170f, 1.4f, go );
			_cameras.Add( new Camera { Object = go, Tally = tally, Light = light, Home = home, Phase = R( 0f, 6f ) } );
		}

		// The applause sign, over the front of the audience, facing the stage.
		a.Slab( "Sign Box", 596f, 606f, -150f, 150f, 330f, 396f, new Color( 0.05f, 0.05f, 0.05f ), false, null, false );
		_applause = a.Sign( "", new Vector3( 590f, 0f, 363f ), 180f, 26f, new Color( 1f, 0.9f, 0.3f ) );
		_signLight = a.Lamp( "Sign Glow", new Vector3( 560f, 0f, 360f ), new Color( 1f, 0.85f, 0.3f ), 700f, 1.6f );
		_signLight.GameObject.Enabled = false;

		// The floor manager: headset, clipboard, pointing at where you should be.
		var blacks = new HumanLook { Shirt = new Color( 0.06f, 0.06f, 0.07f ), Trousers = new Color( 0.06f, 0.06f, 0.07f ), Hat = new Color( 0.3f, 0.3f, 0.32f ) };
		_manager = a.Human( "FLOOR MANAGER", new Vector3( -150f, -600f, 0f ), 150f, blacks );
	}

	void BuildAudience()
	{
		var a = Arena;
		var step = new Color( 0.12f, 0.12f, 0.15f );
		var look = new HumanLook { RunSpeed = 130f };
		int perTier = 6 + Rng.Next( 0, 3 );

		for ( int tier = 0; tier < 3; tier++ )
		{
			float x0 = 700f + tier * 140f, height = 34f * (tier + 1);
			a.Slab( "Tier " + (tier + 1), x0, x0 + 140f, -640f, 640f, 0f, height, step );

			for ( int n = 0; n < perTier; n++ )
			{
				float y = -560f + n * (1120f / (perTier - 1)) + R( -20f, 20f );
				var at = new Vector3( x0 + 70f + R( -20f, 20f ), y, height );
				var person = a.Human( "Audience", at, 180f, look );
				person.LookAt = a.P( Marks[1] );
				_audience.Add( new Seat { Person = person, Post = a.P( at ) + Vector3.Up * 2f, Tier = tier } );
			}
		}
		a.Lamp( "House Lights", new Vector3( 850f, 0f, 300f ), new Color( 0.3f, 0.35f, 0.6f ), 1100f, 0.5f );

		// The Fat Guy: front row, on the floor in front of the first tier. He stands up later.
		a.FatGuyCameo( FatGuySeat, 180f, true );
	}

	// ---- Playing --------------------------------------------------------------------------------

	public override void Begin()
	{
		_lastYaw = Arena.Player.Look.yaw;
		_nextCueAt = _clock + 3.5f;
		Sump.Sfx( "studio_theme", Arena.P( 0f, 0f, 200f ) );
		Say( "FLOOR MANAGER", "And we're live in three, two... Stand on the lit mark. Do what the sign says. Do NOT look scared. Scared tests badly." );
	}

	protected override void Tick( float dt )
	{
		var a = Arena;
		var local = a.PlayerLocal;
		_clock += dt;

		// How far the camera has been turned this frame, for the twirl.
		float yaw = a.Player.Look.yaw;
		float turned = MathF.Abs( AngleDifference( _lastYaw, yaw ) );
		_lastYaw = yaw;

		if ( _host.IsValid() ) _host.LookAt = a.Player.WorldPosition;
		DollyCameras( dt );
		Keepsake( local );

		if ( _finale ) { Finale( local ); return; }

		// The meter is always leaking, and faster if you wander off the stage.
		bool onStage = local.x > -960f && local.x < -240f && MathF.Abs( local.y ) < 530f;
		_meter -= (MeterDrain + (onStage ? 0f : OffStageDrain)) * dt;

		if ( !_cueActive )
		{
			Rules.Objective = Auction ? "Keep the bidding alive: stay on the stage and wait for the seller" : "Keep the audience alive: stay on the stage and wait for the question";
			if ( _clock >= _nextCueAt ) StartCue();
		}
		else RunCue( dt, local, turned );

		_meter = Math.Clamp( _meter, 0f, 1f );
		Rules.Meter = _meter;
		if ( _ticker.IsValid() ) FlashbackArena.SetSign( _ticker, $"LOT 44   ${Price():n0}", new Color( 1f, 0.8f, 0.2f ) );
		if ( _meter <= 0f ) _dead = true;
	}

	int Price() => (int)(500f + _meter * 3200f + _cue * 310f);

	void StartCue()
	{
		var a = Arena;
		if ( _cue >= _questions ) { StartFinale(); return; }

		// Every third question is somewhere to rewind to.
		if ( _cue > 0 && _cue % 3 == 0 ) Rules.Reach( "question" + _cue, a.Player.WorldPosition, a.Player.Look.yaw, _cue );

		_cueActive = true;
		_signOn = false;
		_cueStarted = _clock;
		_hold = 0f;
		_spin = 0f;
		float progress = _questions > 1 ? _cue / (float)(_questions - 1) : 1f;
		_window = (WindowFirst + (WindowLast - WindowFirst) * progress) * a.Body.Patience;

		int mark;
		do mark = Game.Random.Next( Marks.Length ); while ( mark == _mark );
		_mark = mark;
		_demand = (Demand)Game.Random.Next( 4 );

		var lines = Auction ? AuctionPitches : ShowQuestions;
		Say( HostName, lines[Math.Min( _cue, lines.Length - 1 )], 6f );
		LightMark( _mark );
		if ( _manager.IsValid() ) _manager.LookAt = a.P( Marks[_mark] );
		Sump.Sfx( "studio_cue", a.P( Marks[_mark] ) );
	}

	void RunCue( float dt, Vector3 local, float turned )
	{
		var a = Arena;
		float into = _clock - _cueStarted;
		bool onMark = (local - Marks[_mark]).WithZ( 0f ).Length < MarkRadius;

		if ( !_signOn && into >= SignDelay )
		{
			_signOn = true;
			ShowSign( DemandSign( _demand ) );
			Sump.Sfx( "studio_sign", a.P( 590f, 0f, 360f ) );
		}

		float left = MathF.Max( _window - into, 0f );
		Rules.Objective = $"{(Auction ? "Lot" : "Question")} {_cue + 1}/{_questions}: stand on mark {MarkNames[_mark]} (the lit one), then {DemandWords( _demand )}  ({left:0.0}s)";
		Sump.Prompt = !onMark ? $"Get to mark {MarkNames[_mark]}: the lit square on the stage" : _signOn ? DemandPrompt( _demand ) : "Wait for the sign...";

		bool done = false;
		if ( _signOn && onMark )
		{
			switch ( _demand )
			{
				case Demand.Jump: done = Input.Pressed( "Jump" ); break;
				case Demand.Bow: done = Input.Pressed( "Use" ) || Input.Pressed( "Duck" ); break;
				case Demand.Still:
					_hold = a.PlayerSpeed < 18f ? _hold + dt : 0f;
					done = _hold >= StillSeconds;
					break;
				case Demand.Twirl:
					_spin += turned;
					done = _spin >= TwirlDegrees;
					break;
			}
		}
		else { _hold = 0f; }

		if ( done ) EndCue( true );
		else if ( into >= _window ) EndCue( false );
	}

	void EndCue( bool hit )
	{
		var a = Arena;
		_cueActive = false;
		_cue++;
		_nextCueAt = _clock + GapBetween;
		ShowSign( "" );
		LightMark( -1 );

		if ( hit )
		{
			_meter += MeterGain;
			Sump.Sfx( Auction ? "studio_bid" : "studio_applause", a.P( 850f, 0f, 80f ) );
			if ( Game.Random.Float( 0f, 1f ) < 0.4f )
				Say( HostName, Pick( Auction
					? new[] { "And the phones light up! Thank you, caller!", "Look at that. Worth every penny. Do I hear more?" }
					: new[] { "There it is! That's the clip!", "They love it! Look at them, they LOVE it!" } ), 3f );
		}
		else
		{
			_meter -= MeterLoss;
			Sump.Sfx( "studio_boo", a.P( 850f, 0f, 80f ) );
			a.Player.Kick( Vector3.Random * 5f );
			Say( HostName, Pick( Auction
				? new[] { "No bids? Come on, it used to be a GOD. Work with me.", "It's not doing anything. Poke it. Somebody poke it. Not there, Gary.", "The line's gone quiet. Lower the price." }
				: new[] { "Dead air! It's dying out here. Much like its career.", "Hit your mark, sweetheart, the camera can't find you.", "Is it broken? Somebody hit it. We have a guy for that." } ), 3.5f );
		}
	}

	// ---- The last act ---------------------------------------------------------------------------

	void StartFinale()
	{
		var a = Arena;
		_finale = true;
		_finaleStarted = _clock;
		_released = 0;
		_cueActive = false;
		ShowSign( Auction ? "COLLECT IT" : "TOUCH IT" );
		LightMark( -1 );

		Rules.Meter = -1f;
		Rules.Reach( "finale", a.P( Marks[3] ), a.Yaw( 0f ), _questions );
		Rules.TimeLimit = Rules.Elapsed + FinaleSeconds * a.Body.Patience;
		Rules.TimeLabel = "ON AIR FOR";
		Rules.TimeUpLine = "The ON AIR light went out. The doors lock when it does.";

		Sump.Sfx( "studio_sting", a.P( 0f, 0f, 200f ) );
		Say( HostName, Auction ? "SOLD! To... all of you! Come on down and collect your piece!" : "Now, who wants to TOUCH it? Come on down! Don't be shy, it doesn't bite!", 6f );
		Say( "FLOOR MANAGER", "That's not in the rundown. That is NOT in the... keep rolling. Keep rolling!", 5f );
		Say( Who, "The cameras. While their red lights are on, this is still happening. Break the lights. Then the fire door.", 8f );
	}

	void Finale( Vector3 local )
	{
		var a = Arena;
		float into = _clock - _finaleStarted;

		// The audience comes down a tier at a time.
		while ( _released < 3 && into >= _released * TierGap )
		{
			foreach ( var seat in _audience )
				if ( seat.Tier == _released && seat.Person.IsValid() ) seat.Person.Passive = false;
			_released++;
			Sump.Sfx( "studio_crowd_surge", a.P( 800f, 0f, 60f ) );
		}

		// Touch a camera to put its light out.
		foreach ( var camera in _cameras )
		{
			if ( camera.Broken || !camera.Object.IsValid() ) continue;
			if ( (a.Local( camera.Object.WorldPosition ) - local).WithZ( 0f ).Length > CameraTouch ) continue;
			BreakCamera( camera );
		}

		// The Fat Guy gets up for the buffet and stands in front of the fire door. Not across it: beside enough to pass.
		if ( !_fatGuyUp && (_broken >= 2 || into > 28f) && a.FatGuy.IsValid() )
		{
			_fatGuyUp = true;
			a.FatGuy.CameoWalkTo( a.P( FireDoor.x + 22f, FireDoor.y + 150f ) );
			Say( "MAN IN THE FRONT ROW", "Oh, are we done? Is that the way to the buffet? I was promised a buffet.", 5f );
		}

		if ( _broken >= _cameras.Count && !_doorOpen )
		{
			_doorOpen = true;
			if ( _fireDoor.IsValid() ) _fireDoor.Enabled = false;
			a.Nav?.MarkDirty( _fireDoor );
			Sump.Sfx( "studio_fire_door", a.P( FireDoor ) );
			Say( "FLOOR MANAGER", "We've lost picture. We've lost ALL picture. Who opened the fire door?", 5f );
		}

		// ON AIR flickers as its time runs out.
		float left = Rules.TimeLeft;
		bool lit = left > 15f || MathF.Sin( _clock * (left > 6f ? 9f : 22f) ) > 0f;
		if ( _onAirLight.IsValid() ) _onAirLight.GameObject.Enabled = lit;
		FlashbackArena.SetSign( _onAir, "ON AIR", lit ? Color.Red : new Color( 0.25f, 0.02f, 0.02f ) );

		Rules.Objective = _doorOpen
			? "GET OUT: the fire door, under the ON AIR light"
			: $"Break the red lights on the three cameras ({_broken}/{_cameras.Count}): run into them. Do not let the audience hold you";

		if ( _doorOpen && local.y < -958f && local.x > 196f && local.x < 364f && !a.Crowd.Pinned ) _escaped = true;
	}

	void BreakCamera( Camera camera )
	{
		camera.Broken = true;
		_broken++;
		if ( camera.Light.IsValid() ) camera.Light.GameObject.Enabled = false;
		FlashbackArena.Tint( camera.Tally, new Color( 0.12f, 0.02f, 0.02f ) );
		Sump.Sfx( "studio_camera_smash", camera.Object.WorldPosition );
		Arena.Player.Kick( Vector3.Random * 9f );

		if ( Profile.Ability == TwistedAbility.OnlyWhileWatched && !_memeLine )
		{
			_memeLine = true;
			Say( Who, "With every light that goes out there is a little less of me. I did not know that yet. I broke them anyway.", 7f );
		}
		else if ( _broken < _cameras.Count ) Say( "FLOOR MANAGER", $"Camera {_broken} is down! Stay on it with the others!", 3f );
	}

	// ---- Small things ---------------------------------------------------------------------------

	void DollyCameras( float dt )
	{
		// They track from side to side while the show runs; a broken one stays where it died.
		foreach ( var camera in _cameras )
		{
			if ( camera.Broken || !camera.Object.IsValid() ) continue;
			float slide = MathF.Sin( _clock * 0.45f + camera.Phase ) * (_finale ? 40f : 110f);
			camera.Object.WorldPosition = Arena.P( camera.Home.x, camera.Home.y + slide, 0f );
		}
	}

	void Keepsake( Vector3 local )
	{
		if ( _keepsakeTaken || (local - _keepsakeAt).WithZ( 0f ).Length > 55f ) return;
		_keepsakeTaken = true;
		if ( _keepsake.IsValid() ) _keepsake.Destroy();
		_meter = MathF.Min( _meter + 0.15f, 1f );
		Sump.Sfx( "flashback_keepsake", Arena.Player.WorldPosition );
		Say( Who, Auction
			? "{thing}. It was on the sofa, left by whatever they sold before me. It was the only thing in that room without a price on it."
			: "{thing}. Some guest before me left it on the sofa. It was the only thing in that room nobody was laughing at.", 7f );
	}

	void LightMark( int mark )
	{
		for ( int i = 0; i < _markObjects.Count; i++ )
			FlashbackArena.Tint( _markObjects[i], i == mark ? new Color( 1f, 0.95f, 0.4f ) : new Color( 0.25f, 0.25f, 0.28f ) );
		if ( !_markLight.IsValid() ) return;
		_markLight.GameObject.Enabled = mark >= 0;
		if ( mark >= 0 ) _markLight.GameObject.WorldPosition = Arena.P( Marks[mark] + Vector3.Up * 70f );
	}

	void ShowSign( string text )
	{
		FlashbackArena.SetSign( _applause, text, new Color( 1f, 0.9f, 0.3f ) );
		if ( _signLight.IsValid() ) _signLight.GameObject.Enabled = !string.IsNullOrEmpty( text );
	}

	string DemandSign( Demand demand ) => demand switch
	{
		Demand.Jump => Auction ? "BUY NOW" : "APPLAUSE",
		Demand.Still => Auction ? "CLOSE-UP" : "QUIET",
		Demand.Twirl => Auction ? "ALL SIDES" : "LAUGH",
		_ => Auction ? "THANK THEM" : "BOW",
	};

	static string DemandWords( Demand demand ) => demand switch
	{
		Demand.Jump => "JUMP for them",
		Demand.Still => "HOLD perfectly STILL",
		Demand.Twirl => "TWIRL (swing the camera all the way round)",
		_ => "take a BOW",
	};

	static string DemandPrompt( Demand demand ) => demand switch
	{
		Demand.Jump => "The sign is lit: JUMP (SPACE)",
		Demand.Still => "The sign is lit: HOLD STILL. Do not move",
		Demand.Twirl => "The sign is lit: TWIRL. Swing the mouse all the way round",
		_ => "The sign is lit: BOW (E)",
	};

	static float AngleDifference( float from, float to )
	{
		float d = (to - from) % 360f;
		if ( d > 180f ) d -= 360f;
		if ( d < -180f ) d += 360f;
		return d;
	}

	public override void Won()
	{
		if ( _onAirLight.IsValid() ) _onAirLight.GameObject.Enabled = false;
		Say( HostName, Auction ? "...We'll, ah. We'll be issuing refunds. Stay tuned for cookware." : "...We'll be right back. Somebody get that thing's agent on the phone.", 6f );
	}

	public override void Rewind( FlashbackCheckpoint to )
	{
		_dead = false;
		_cueActive = false;
		_meter = MeterStart;
		_nextCueAt = _clock + 3f;
		ShowSign( "" );
		LightMark( -1 );

		// Everybody back in their seats.
		foreach ( var seat in _audience )
		{
			if ( !seat.Person.IsValid() || seat.Person.State == CrowdState.Dead ) continue;
			seat.Person.Passive = true;
			seat.Person.WorldPosition = seat.Post;
			var body = seat.Person.Components.Get<Rigidbody>();
			if ( body.IsValid() ) { body.Velocity = Vector3.Zero; body.AngularVelocity = Vector3.Zero; }
		}

		if ( to.Name == "finale" )
		{
			// The last act again: the cameras already broken stay broken.
			_cue = _questions;
			_finale = true;
			_finaleStarted = _clock;
			_released = 0;
			ShowSign( Auction ? "COLLECT IT" : "TOUCH IT" );
			Rules.Meter = -1f;
			return;
		}

		_finale = false;
		_cue = to.Stage;
		Rules.Meter = _meter;
	}
}