Sump/Trauma/WeightPhobia.cs
using System;

/// <summary>
/// Fear of weight: the Fat Guy who sat on them. Dread rises under low ceilings and overhangs, in
/// tight gaps, under anything tagged "sump_heavy", and most of all when something has hold of the
/// lizard (<see cref="Held"/>). Clinging under a ceiling counts as being under it.
/// What it does: it is slowed hard when held or under something, the camera sinks and crowds in,
/// the grip slips more, and a panic attack freezes it flat instead of bolting.
/// </summary>
[Title( "Phobia: Weight" ), Category( "Sump" ), Icon( "fitness_center" )]
public sealed class WeightPhobia : PhobiaBase
{
	public override Phobia Kind => Phobia.Weight;

	/// <summary>Open height the lizard wants above it, in inches. Anything lower frightens.</summary>
	[Property, Group( "Weight" )] public float CeilingComfort { get; set; } = 150f;

	/// <summary>A gap narrower than this (wall to wall) feels tight, in inches.</summary>
	[Property, Group( "Weight" )] public float GapComfort { get; set; } = 70f;

	/// <summary>How far to the side a "sump_heavy" thing overhead still counts, in inches.</summary>
	[Property, Group( "Weight" )] public float HeavyRadius { get; set; } = 260f;

	/// <summary>How far above a "sump_heavy" thing still counts, in inches.</summary>
	[Property, Group( "Weight" )] public float HeavyHeight { get; set; } = 700f;

	/// <summary>How near a gripping hand (a jointed "crowd" object) has to be to count as holding, in inches.</summary>
	[Property, Group( "Weight" )] public float ArmsReach { get; set; } = 80f;

	/// <summary>Speed left under something at full dread.</summary>
	[Property, Group( "Effects" ), Range( 0.05f, 1f )] public float DragSpeed { get; set; } = 0.45f;

	/// <summary>Speed left while something has hold of the lizard: a crawl.</summary>
	[Property, Group( "Effects" ), Range( 0.05f, 1f )] public float HeldSpeed { get; set; } = 0.22f;

	/// <summary>Camera height left when fully pressed down.</summary>
	[Property, Group( "Effects" ), Range( 0.1f, 1f )] public float SunkCamera { get; set; } = 0.4f;

	/// <summary>True while something has hold of the player: leashed, pinned, or gripped by a crowd's joint.</summary>
	public bool Held { get; private set; }

	public override bool HidingHelps => false;   // a ceiling is no comfort here
	public override string FirstLine => "Something over me. He sat on me, in the stadium, and the air went out of both ends. I need open height.";
	public override string AfterLine => "...I could not move. Nothing was even on me. Stay out from under things. Pink things most of all.";

	float _sink;

	/// <summary>1 when the ray hits right away, 0 when it reaches its full length.</summary>
	float Closeness( Vector3 from, Vector3 direction, float length, out SceneTraceResult result )
	{
		result = Ray( from, from + direction * length );
		if ( !result.Hit ) return 0f;
		if ( result.StartedSolid ) return 1f;
		return Math.Clamp( 1f - result.Distance / MathF.Max( length, 1f ), 0f, 1f );
	}

	/// <summary>
	/// Whether anything has hold of the player, found without knowing who does the holding: the
	/// controller's leash or pin, or a spring joint on a "crowd" object within arm's reach whose
	/// other end is the player (or right on top of it).
	/// </summary>
	bool SenseHeld( Vector3 center )
	{
		// In a memory, being strapped or posted somewhere by the scene is not being held: only
		// real hands on the body count there (the joints below).
		bool staged = Sump.Mode == SumpMode.Flashback || Sump.Mode == SumpMode.Arrival;
		if ( !staged && (Player.Leash.HasValue || Player.IsPinned) ) return true;

		int looked = 0;
		foreach ( var joint in Scene.GetAllComponents<SpringJoint>() )
		{
			if ( ++looked > 128 ) break;
			if ( !joint.IsValid() || !joint.Active ) continue;
			var hand = joint.GameObject;
			if ( !hand.Tags.Has( "crowd" ) ) continue;
			if ( (hand.WorldPosition - center).Length > ArmsReach ) continue;

			var held = joint.Body;
			if ( !held.IsValid() ) continue;
			if ( IsUnder( held, GameObject ) || (held.WorldPosition - center).Length < 40f ) return true;
		}
		return false;
	}

	protected override float Sense()
	{
		var center = Center;
		float comfort = MathF.Max( CeilingComfort, 10f );

		try { Held = SenseHeld( center ); }
		catch ( Exception ) { Held = Sump.Mode != SumpMode.Flashback && (Player.Leash.HasValue || Player.IsPinned); }

		float above = Closeness( center, Vector3.Up, comfort, out var up );
		bool resting = false;
		if ( up.Hit && up.GameObject.IsValid() )
		{
			// Something loose, or marked heavy, right over us is worse than rock.
			resting = up.GameObject.Tags.Has( PlayerPhobia.HeavyTag )
				|| up.GameObject.Components.Get<Rigidbody>( FindMode.EverythingInSelfAndAncestors ).IsValid();
		}

		float overhang = 0f;
		for ( int i = 0; i < 4; i++ )
		{
			float a = i * MathF.PI * 0.5f + 0.4f;
			var slant = new Vector3( MathF.Cos( a ), MathF.Sin( a ), 1f ).Normal;
			overhang += Closeness( center, slant, comfort, out _ ) * 0.25f;
		}

		// A gap is tight only when both sides are close.
		float gap = MathF.Max( GapComfort, 10f );
		float squeeze = 0f;
		for ( int i = 0; i < 2; i++ )
		{
			float a = i * MathF.PI * 0.5f + 0.4f;
			var side = new Vector3( MathF.Cos( a ), MathF.Sin( a ), 0f );
			float one = Closeness( center, side, gap, out _ );
			float other = Closeness( center, -side, gap, out _ );
			squeeze = MathF.Max( squeeze, MathF.Min( one, other ) * 2f );
		}
		squeeze = Math.Clamp( squeeze, 0f, 1f );

		float heavy = 0f;
		var heavyDir = Vector3.Zero;
		int looked = 0;
		foreach ( var go in Scene.FindAllWithTag( PlayerPhobia.HeavyTag ) )
		{
			if ( ++looked > 64 ) break;
			if ( !go.IsValid() || !go.Active || IsUnder( go, GameObject ) ) continue;
			var to = go.WorldPosition - center;
			if ( to.z <= 0f || to.z > HeavyHeight ) continue;
			float sideways = to.WithZ( 0f ).Length;
			if ( sideways > HeavyRadius ) continue;
			float h = (1f - sideways / MathF.Max( HeavyRadius, 1f )) * (1f - 0.5f * to.z / MathF.Max( HeavyHeight, 1f ));
			if ( h > heavy ) { heavy = h; heavyDir = to; }
		}

		float dread = above * 0.75f + overhang * 0.35f + squeeze * 0.5f + heavy * 0.9f + (resting ? 0.35f : 0f);
		if ( Held ) dread = 1f;
		if ( dread <= 0.01f ) return 0f;

		CauseDir = Vector3.Up * 40f;
		if ( Held ) RawCause = "what has hold of you";
		else if ( heavy > above && heavy > squeeze ) { RawCause = "what hangs above"; CauseDir = heavyDir; }
		else if ( resting ) RawCause = "the weight on you";
		else if ( squeeze > above ) RawCause = "the squeeze";
		else RawCause = above > 0.01f ? "the low ceiling" : "the overhang";
		return dread;
	}

	public override void Modify( ref PhobiaMods mods, float senses, float body, bool attack, float dt )
	{
		float keep = Keep;
		float under = Felt;

		// How pressed down it is: all the way when held or frozen, else by what is overhead.
		float want = attack || Held ? keep : MathF.Max( under, body * 0.6f * keep );
		_sink += (want - _sink) * Math.Clamp( dt * 6f, 0f, 1f );
		if ( _sink <= 0.005f ) return;

		// Camera: sinks toward the floor and crowds in.
		mods.CameraHeight *= 1f + (SunkCamera - 1f) * _sink;
		mods.CameraDistance *= 1f - 0.3f * _sink;
		mods.Vignette += 0.25f * _sink;
		mods.Saturation *= 1f - 0.25f * _sink;

		if ( attack ) return;

		// Input: slowed hard. Held is a crawl, straight away; under something it drags with the dread.
		float speed = 1f + (DragSpeed - 1f) * MathF.Max( under, body * keep );
		if ( Held ) speed = MathF.Min( speed, 1f + (HeldSpeed - 1f) * keep );
		mods.Speed *= speed;
		mods.Slip += 0.8f * body;
	}

	// A duck, not a dash: the head goes down.
	public override void Flinch( float amount, float speed, float kick ) =>
		Player.Kick( Vector3.Down * kick * 4f * amount );

	// Freeze. Off a wall or ceiling the grip goes first and it drops flat.
	public override Vector3? AttackStart()
	{
		if ( Player.Attached && Player.Up.z < 0.7f && !Player.IsPinned ) Player.ShakeLoose( Vector3.Down * 80f );
		Player.Kick( Vector3.Down * 20f );
		return null;
	}
}