Stadium/StadiumLayout.cs

Static layout table for the colosseum in Scene 2. Holds the ellipse floor and tier measurements, stage, carpet and exit constants, plus helpers for ground height, tier lookup, seat and pit spot generation, and the exit win check.

using System;
using System.Collections.Generic;

/// <summary>
/// The measurements of the colosseum in Scene 2, in one place, so the thing that builds it, the
/// crowd that fills it and the game rules all agree. Units are inches; +X runs from the stage to
/// the exit, +Y is left, +Z up. The arena floor is at z = 0.
///
/// The floor is an ellipse, about 28,000 long and 18,000 wide (a hundred times the old hall's
/// floor). Stands rise round it in tiers: tier t is the band between the ellipse with semi-axes
/// (FloorA + t * TierDepth, FloorB + t * TierDepth) and the next one out. The stage sits at the
/// -X end of the floor; the exit is a tunnel through the stands at the +X end.
/// </summary>
public static class StadiumLayout
{
	// ---- The arena floor (an ellipse) --------------------------------------------------------
	public const float CenterX = 13100f;
	public const float FloorA = 14000f;        // semi-axis along X
	public const float FloorB = 9000f;         // semi-axis along Y
	public static readonly Vector3 Center = new( CenterX, 0f, 0f );

	// ---- Stands ------------------------------------------------------------------------------
	public const int Tiers = 26;
	public const float TierDepth = 220f;       // each tier steps this far out...
	public const float TierHeight = 120f;      // ...and this much up
	public const float WallHeight = 900f;      // the outer wall stands this far above the top tier

	/// <summary>Semi-axes of the outer edge of everything (the back of the top tier).</summary>
	public const float OuterA = FloorA + Tiers * TierDepth;
	public const float OuterB = FloorB + Tiers * TierDepth;

	// ---- Bounding box of the whole place (kept for code that wants plain limits) -------------
	public const float MinX = CenterX - OuterA, MaxX = CenterX + OuterA;
	public const float HalfWidth = OuterB;
	public const float Height = Tiers * TierHeight + WallHeight;

	// ---- Stage, carpet, podium (the same size as ever, at the -X end of the floor) -----------
	public const float StageBackX = -900f;
	public const float StageFrontX = 500f;
	public const float StageHalfWidth = 600f;
	public const float StageTop = 80f;

	public static readonly Vector3 CarpetStart = new( -800f, 0f, StageTop );
	public static readonly Vector3 CarpetEnd = new( 380f, 0f, StageTop );
	public const float CarpetWidth = 130f;
	public static readonly Vector3 Podium = new( 430f, 0f, StageTop );

	/// <summary>Where the player appears, in the envoy's skin, facing down the carpet (+X).</summary>
	public static readonly Vector3 PlayerStart = CarpetStart;

	// ---- Old names for the floor's limits ----------------------------------------------------
	public const float PitEndX = CenterX + FloorA - 300f;   // the floor in front of the exit tunnel
	public const float PitHalfWidth = FloorB;

	// ---- Exit --------------------------------------------------------------------------------
	/// <summary>Half-width of the tunnel through the stands at the +X end, and of the exit doorway.</summary>
	public const float AisleHalfWidth = 400f;
	public const float ExitDoorHeight = 520f;
	public static readonly Vector3 Exit = new( MaxX - 20f, 0f, 0f );

	/// <summary>True once a point is in the exit doorway: the win condition.</summary>
	public static bool InExit( Vector3 p ) => p.x > MaxX - 220f && p.y > -AisleHalfWidth && p.y < AisleHalfWidth && p.z < ExitDoorHeight + 40f;

	/// <summary>True in the exit tunnel through the stands.</summary>
	public static bool InTunnel( Vector3 p ) => p.x > CenterX && p.y > -AisleHalfWidth && p.y < AisleHalfWidth;

	/// <summary>Where the Fat Guy waits before he is needed.</summary>
	public static readonly Vector3 FatGuyStart = new( 2100f, -520f, 0f );

	/// <summary>
	/// How far out a point is, as an ellipse: 1 on the ellipse with these semi-axes, less inside.
	/// </summary>
	public static float EllipseRadius( Vector3 p, float a, float b )
	{
		float x = (p.x - CenterX) / a, y = p.y / b;
		return MathF.Sqrt( x * x + y * y );
	}

	/// <summary>A point on the ellipse with these semi-axes, at an angle in radians (0 is +X, the exit).</summary>
	public static Vector3 EllipsePoint( float a, float b, float angle, float z = 0f ) =>
		new( CenterX + MathF.Cos( angle ) * a, MathF.Sin( angle ) * b, z );

	/// <summary>True on the arena floor (inside the floor ellipse).</summary>
	public static bool OnFloor( Vector3 p ) => EllipseRadius( p, FloorA, FloorB ) < 1f;

	/// <summary>Which tier a point is over: -1 on the floor, Tiers or more beyond the stands.</summary>
	public static int TierAt( Vector3 p )
	{
		if ( OnFloor( p ) ) return -1;
		for ( int t = 0; t < Tiers; t++ )
			if ( EllipseRadius( p, FloorA + (t + 1) * TierDepth, FloorB + (t + 1) * TierDepth ) < 1f ) return t;
		return Tiers;
	}

	/// <summary>
	/// Height of the solid ground at a spot: the stage top, a stand tier, or the floor. Nothing
	/// that walks should ever be below this, so it is the last word on "has it clipped through".
	/// </summary>
	public static float GroundHeight( Vector3 p )
	{
		float ay = p.y < 0f ? -p.y : p.y;
		if ( p.x > StageBackX && p.x < StageFrontX && ay < StageHalfWidth ) return StageTop;
		if ( InTunnel( p ) ) return 0f;

		int tier = TierAt( p );
		if ( tier < 0 ) return 0f;
		return TierTop( tier >= Tiers ? Tiers - 1 : tier );
	}

	/// <summary>Standing height of tier <paramref name="tier"/> (0 is the lowest).</summary>
	public static float TierTop( int tier ) => TierHeight * (tier + 1);

	/// <summary>
	/// Every seat in the stands: a spot in the middle of a tier, all the way round, leaving the
	/// exit tunnel clear. <paramref name="spacing"/> is the gap between neighbours along a row.
	/// </summary>
	public static IEnumerable<Vector3> Seats( float spacing = 110f )
	{
		for ( int tier = 0; tier < Tiers; tier++ )
		{
			float a = FloorA + (tier + 0.5f) * TierDepth, b = FloorB + (tier + 0.5f) * TierDepth;
			int count = (int)(MathF.PI * (a + b) / spacing);   // near enough the way round
			for ( int i = 0; i < count; i++ )
			{
				var p = EllipsePoint( a, b, i * MathF.PI * 2f / count, TierTop( tier ) );
				if ( p.x > CenterX && MathF.Abs( p.y ) < AisleHalfWidth + 60f ) continue;
				yield return p;
			}
		}
	}

	/// <summary>Standing spots on the arena floor, in a grid, clear of the stage.</summary>
	public static IEnumerable<Vector3> PitSpots( float spacing = 150f )
	{
		for ( float x = CenterX - FloorA; x < CenterX + FloorA; x += spacing )
			for ( float y = -FloorB; y < FloorB; y += spacing )
			{
				var p = new Vector3( x, y, 0f );
				if ( EllipseRadius( p, FloorA - 80f, FloorB - 80f ) >= 1f ) continue;
				if ( x < StageFrontX + 60f && MathF.Abs( y ) < StageHalfWidth + 60f ) continue;
				yield return p;
			}
	}

	/// <summary>Where the guards stand: along the front of the stage and at the mouth of the exit tunnel.</summary>
	public static IEnumerable<Vector3> GuardPosts()
	{
		for ( float y = -450f; y <= 450f; y += 300f ) yield return new Vector3( StageFrontX + 90f, y, 0f );
		yield return new Vector3( PitEndX - 60f, AisleHalfWidth + 40f, 0f );
		yield return new Vector3( PitEndX - 60f, -AisleHalfWidth - 40f, 0f );
	}
}