Sump/Hub/HubShrines.cs

Part of the Sump hub that builds a shrine for each healed misfit: a junk plinth, candles, offerings, an arch and banner, the misfit's effigy on top, plus a sit mat and candle light. Placement picks a cavern build site by hashing the profile, spaced away from the market, radio room and other shrines, and saves the result as a state note. Sitting at a shrine says a line, plays a chime, boosts the flame and soothes the player's phobia.

using System;
using System.Collections.Generic;

// The hub's shrines: one for every healed misfit, built round a big rendition of the human
// object it clings to (HubEffigy), on a plinth of junk, ringed with candles and little offerings
// in the misfit's own colours.
public sealed partial class SumpHub
{
	/// <summary>How much bigger than the 60-inch effigy space a shrine's object is built.</summary>
	[Property, Group( "Shrines" )] public float EffigyScale { get; set; } = 1.15f;
	[Property, Group( "Shrines" ), Range( 3, 10 )] public int CandlesPerShrine { get; set; } = 7;
	[Property, Group( "Shrines" ), Range( 0, 8 )] public int OfferingsPerShrine { get; set; } = 5;
	/// <summary>Two shrines (or a shrine and the market or radio room) are never closer than this.</summary>
	[Property, Group( "Shrines" )] public float ShrineSpacing { get; set; } = 260f;

	const float PlinthTop = 46f;

	sealed class Shrine
	{
		public MisfitProfile Profile;
		public GameObject Root;
		public Vector3 Position;
		/// <summary>The mat in front, where one sits.</summary>
		public Vector3 Seat;
		public HubFlame Flame;
	}

	readonly Dictionary<int, Shrine> _shrines = new();

	void ForgetShrines() => _shrines.Clear();

	void EnsureShrine( MisfitProfile profile, bool animate )
	{
		if ( profile is null || _shrines.ContainsKey( profile.Index ) ) return;

		var (at, yaw) = ShrinePlace( profile );
		string name = MisfitVoice.ShortName( profile );
		var b = HubBuilder.At( _root, $"Shrine of {name}", at, yaw );
		var rng = new HubRng( MisfitGenerator.Mix( profile.Seed, 0x5123, profile.Index ) );

		BuildPlinth( b, rng, profile );

		var effigy = b.Group( "Effigy", new Vector3( 0f, 0f, PlinthTop + 1.5f ), Rotation.FromYaw( rng.R( -8f, 8f ) ), EffigyScale );
		HubEffigy.Build( effigy, profile.ClungObject, profile.Primary, profile.Secondary, profile.Seed );

		// A niche behind it: an arch in the misfit's second colour, hung with a strip of its first.
		var arch = HubKit.Shade( profile.Secondary, 0.8f );
		b.Ring( "Arch", new Vector3( -30f, 0f, PlinthTop - 4f ), 46f, 5f, arch, 0f, 180f, 8 );
		b.Box( "Arch Foot", new Vector3( -30f, -46f, PlinthTop * 0.5f - 6f ), new Vector3( 7f, 7f, PlinthTop ), arch );
		b.Box( "Arch Foot", new Vector3( -30f, 46f, PlinthTop * 0.5f - 6f ), new Vector3( 7f, 7f, PlinthTop ), arch );
		b.Rod( "Banner", new Vector3( -30f, -30f, PlinthTop + 30f ), new Vector3( -30f, 30f, PlinthTop + 30f ), 2f, profile.Primary );
		for ( int i = 0; i < 5; i++ )
			b.Box( "Pennant", new Vector3( -30f, -24f + i * 12f, PlinthTop + 24f - (i % 2) * 2f ), new Vector3( 1f, 7f, 10f ), i % 2 == 0 ? profile.Primary : profile.Secondary, false, Rotation.FromRoll( rng.R( -10f, 10f ) ) );

		BuildCandles( b, rng, profile );
		BuildOfferings( b, rng, profile );

		// The mat one sits on, facing the object.
		b.Box( "Mat", new Vector3( 100f, 0f, 1f ), new Vector3( 46f, 60f, 2f ), HubKit.Shade( profile.Primary, 0.45f ), false, Rotation.FromYaw( rng.R( -6f, 6f ) ) );
		b.Box( "Mat Stripe", new Vector3( 100f, 0f, 2.2f ), new Vector3( 8f, 60f, 0.6f ), HubKit.Shade( profile.Secondary, 0.6f ) );

		// One warm light for all the candles.
		var light = b.Light( "Candlelight", new Vector3( 46f, 0f, PlinthTop + 26f ), HubKit.Flame, CandleRadius );
		var flame = AddFlame( light, HubKit.Shade( Color.Lerp( HubKit.Flame, profile.Primary, 0.2f ), 0.75f ), 0.3f, 6f + rng.R( 0f, 3f ) );

		var shrine = new Shrine { Profile = profile, Root = b.Root, Position = at, Seat = b.World( new Vector3( 100f, 0f, 0f ) ), Flame = flame };
		_shrines[profile.Index] = shrine;

		AddSpot( b.Root, b.World( new Vector3( 80f, 0f, 0f ) ), 130f, () => $"E  sit at {name}'s shrine", () => SitAtShrine( shrine ) );
		Finish( b, animate );
	}

	/// <summary>Three slabs of junk, each smaller than the one under it, with a cloth over the top.</summary>
	void BuildPlinth( HubBuilder b, HubRng rng, MisfitProfile profile )
	{
		float z = -12f;
		(float Wide, float Tall)[] tiers = { (96f, 26f), (74f, 17f), (56f, 15f) };
		foreach ( var (wide, tall) in tiers )
		{
			var size = new Vector3( wide * rng.R( 0.92f, 1.06f ), wide * rng.R( 0.92f, 1.06f ), tall );
			b.Box( "Plinth Junk", new Vector3( 0f, 0f, z + tall * 0.5f ), size, HubKit.Shade( rng.Pick( HubKit.Junk ), rng.R( 0.75f, 1f ) ), true, Rotation.FromYaw( rng.R( -9f, 9f ) ) );
			z += tall;
		}

		b.Box( "Cloth", new Vector3( 0f, 0f, PlinthTop + 0.6f ), new Vector3( 60f, 60f, 1.2f ), profile.Primary, false, Rotation.FromYaw( 45f ) );
		b.Box( "Cloth Edge", new Vector3( 29f, 0f, PlinthTop - 5f ), new Vector3( 1.2f, 40f, 12f ), profile.Secondary );

		// Odds and ends leaning on the foot of it.
		for ( int i = 0; i < 4; i++ )
		{
			float yaw = 110f + i * 47f + rng.R( -12f, 12f );
			var at = Rotation.FromYaw( yaw ).Forward * rng.R( 58f, 70f );
			var size = new Vector3( rng.R( 12f, 26f ), rng.R( 10f, 22f ), rng.R( 10f, 24f ) );
			b.Box( "Leaning Junk", at + Vector3.Up * (size.z * 0.5f - 3f), size, HubKit.Shade( rng.Pick( HubKit.Junk ), rng.R( 0.6f, 0.95f ) ), false,
				new Angles( rng.R( -14f, 14f ), rng.R( 0f, 360f ), rng.R( -14f, 14f ) ).ToRotation() );
		}
	}

	/// <summary>Candle stubs across the front of the plinth, some on its steps.</summary>
	void BuildCandles( HubBuilder b, HubRng rng, MisfitProfile profile )
	{
		int count = Math.Clamp( CandlesPerShrine, 3, 10 );
		for ( int i = 0; i < count; i++ )
		{
			// Fan them across the front half, so they are seen from the mat.
			float yaw = -80f + 160f * (count == 1 ? 0.5f : i / (float)(count - 1)) + rng.R( -6f, 6f );
			bool onStep = i % 3 == 1;
			float reach = onStep ? 42f : rng.R( 60f, 76f );
			float foot = onStep ? 14f : 0f;
			var at = Rotation.FromYaw( yaw ).Forward * reach + Vector3.Up * foot;
			float tall = rng.R( 5f, 17f );
			var wax = i % 4 == 3 ? HubKit.Shade( profile.Secondary, 0.9f ) : HubKit.Wax;

			b.Box( "Candle", at + Vector3.Up * tall * 0.5f, new Vector3( 4f, 4f, tall ), wax );
			b.Disc( "Wax", at + Vector3.Up * 0.6f, 4.5f, 0.6f, HubKit.Shade( wax, 0.85f ), true );
			b.Ball( "Flame", at + Vector3.Up * (tall + 2.6f), new Vector3( 1.5f, 1.5f, 3f ), HubKit.Flame );
		}
	}

	/// <summary>Little things left at the shrine: bottle caps, coins, folded notes, a ribbon.</summary>
	void BuildOfferings( HubBuilder b, HubRng rng, MisfitProfile profile )
	{
		for ( int i = 0; i < OfferingsPerShrine; i++ )
		{
			float yaw = rng.R( -70f, 70f );
			var at = Rotation.FromYaw( yaw ).Forward * rng.R( 80f, 96f );
			var tint = i % 2 == 0 ? profile.Primary : profile.Secondary;

			switch ( rng.Next( 4 ) )
			{
				case 0: b.Disc( "Bottle Cap", at + Vector3.Up * 0.8f, 2.6f, 0.8f, tint, true ); break;
				case 1: b.Disc( "Coin", at + Vector3.Up * 0.5f, 2f, 0.5f, HubKit.Tin, true ); break;
				case 2: b.Box( "Folded Note", at + Vector3.Up * 1f, new Vector3( 6f, 4f, 2f ), HubKit.Paper, false, Rotation.FromYaw( rng.R( 0f, 180f ) ) ); break;
				default:
					b.Ball( "Bead", at + Vector3.Up * 2f, 2f, tint );
					b.Ball( "Bead", at + new Vector3( 3f, 2f, 1.6f ), 1.6f, HubKit.Shade( tint, 0.7f ) );
					break;
			}
		}
	}

	// ---- Where it goes ------------------------------------------------------------------------------

	/// <summary>
	/// Where a misfit's shrine stands. Read from the save if it was placed before; otherwise picked
	/// from the cavern's build sites by hashing its clung object with its seed, stepping on past any
	/// site already taken, and saved so it is in the same place on every later visit.
	/// </summary>
	(Vector3 At, float Yaw) ShrinePlace( MisfitProfile profile )
	{
		string key = $"shrine_{profile.Index}";
		if ( HubKit.ReadPlace( SumpState.GetNote( key ), out var saved, out float savedYaw ) ) return (saved, savedYaw);

		var taken = TakenPlaces( profile.Index );
		var sites = SumpCavern.BuildSites;
		Vector3? found = null;

		if ( sites.Count > 0 )
		{
			int roll = MisfitGenerator.Mix( MisfitGenerator.Hash( profile.ClungObject ), profile.Seed, 0x5A17E );
			int start = roll % sites.Count;
			// A step that shares no factor with the count visits every site once.
			int step = 1;
			foreach ( int candidate in new[] { 5, 3, 7, 11, 13 } )
				if ( Gcd( candidate, sites.Count ) == 1 ) { step = candidate; break; }

			for ( int n = 0; n < sites.Count && found is null; n++ )
			{
				int i = (start + n * step) % sites.Count;
				if ( i == _radioSite || i == _marketSite || IsTaken( sites[i], taken ) ) continue;
				found = sites[i];
			}
		}

		// Every site gone (or none given): beside the misfit's own hiding place, or on a ring round the landing.
		if ( found is null )
		{
			var home = Misfit.Find( profile )?.Home;
			for ( int n = 0; n < 12 && found is null; n++ )
			{
				var centre = home ?? Sump.HubSpawn;
				float reach = home is null ? 1250f : 200f + n * 40f;
				var candidate = HubKit.OnFloor( HubKit.Around( centre, profile.Index * 40f + n * 67f, reach ) );
				if ( !IsTaken( candidate, taken ) ) found = candidate;
			}
			found ??= HubKit.OnFloor( HubKit.Around( Sump.HubSpawn, profile.Index * 40f, 1250f ) );
		}

		var at = found.Value;
		float yaw = FaceLanding( at );
		SumpState.SetNote( key, HubKit.WritePlace( at, yaw ) );
		return (at, yaw);
	}

	/// <summary>Everywhere a shrine may not go: the radio room, the market, and every other shrine placed so far.</summary>
	List<Vector3> TakenPlaces( int exceptIndex )
	{
		var taken = new List<Vector3> { _radioAt, _marketAt };
		for ( int i = 0; i < SumpState.Misfits.Count; i++ )
		{
			if ( i == exceptIndex ) continue;
			if ( HubKit.ReadPlace( SumpState.GetNote( $"shrine_{i}" ), out var at, out _ ) ) taken.Add( at );
		}
		return taken;
	}

	bool IsTaken( Vector3 place, List<Vector3> taken )
	{
		foreach ( var other in taken )
			if ( (other - place).WithZ( 0f ).Length < ShrineSpacing ) return true;
		return false;
	}

	static int Gcd( int a, int b )
	{
		while ( b != 0 ) (a, b) = (b, a % b);
		return Math.Abs( a );
	}

	// ---- Using one ----------------------------------------------------------------------------------

	/// <summary>Sitting at a shrine: its owner's words about the object, the candles flare, and the player's own fear eases.</summary>
	void SitAtShrine( Shrine shrine )
	{
		var profile = shrine.Profile;
		Sump.Say( MisfitVoice.ShortName( profile ), MisfitVoice.AboutObject( profile ), 7f );
		Sump.Sfx( "shrine_chime", shrine.Position, 0.7f );
		if ( shrine.Flame is not null ) shrine.Flame.Boost = 1f;

		var phobia = Sump.Player.IsValid() ? Sump.Player.Components.Get<PlayerPhobia>() : null;
		if ( phobia.IsValid() ) phobia.Soothe( ShrineSoothe );
	}
}