""
using System;
using System.Collections.Generic;
/// <summary>
/// Fear of crowds: the stadium's hands. Every body near the lizard adds to the dread, more if it
/// is close, in front, or closing in. Bodies are unhealed misfits and anything tagged "crowd",
/// "misfit" or "sump_body". While the player is tagged "sump_unseen" nobody can see it, and the
/// dread does not rise.
/// What it does: the camera shakes with the head-count and closeness, the look is tugged toward
/// the nearest body, running comes in surges, and a panic attack bolts away from them.
/// </summary>
[Title( "Phobia: Crowds" ), Category( "Sump" ), Icon( "groups" )]
public sealed class CrowdsPhobia : PhobiaBase
{
public override Phobia Kind => Phobia.Crowds;
/// <summary>Bodies further away than this do not count, in inches.</summary>
[Property, Group( "Crowds" )] public float CrowdRadius { get; set; } = 420f;
/// <summary>Roughly how many bodies right on top of the lizard make full dread.</summary>
[Property, Group( "Crowds" )] public float CrowdTolerance { get; set; } = 2.5f;
/// <summary>Extra weight for a body straight in front of the lizard (0 = facing does not matter).</summary>
[Property, Group( "Crowds" )] public float FrontBonus { get; set; } = 0.6f;
/// <summary>Extra weight for a body coming closer at 200 inches per second or more.</summary>
[Property, Group( "Crowds" )] public float ClosingBonus { get; set; } = 0.8f;
/// <summary>Camera shake with a full crowd on top of the lizard, in inches.</summary>
[Property, Group( "Effects" )] public float ShakeMax { get; set; } = 3.5f;
/// <summary>How fast the look is pulled toward the nearest body at full dread, in degrees per second.</summary>
[Property, Group( "Effects" )] public float LookTug { get; set; } = 16f;
/// <summary>How much faster it runs at the top of a surge when fully shaken.</summary>
[Property, Group( "Effects" )] public float SurgeSpeed { get; set; } = 1.35f;
/// <summary>Bodies counted at the last look.</summary>
public int HeadCount { get; private set; }
public override string FirstLine => "Too many. Too close. The stadium had hands like these, and not one had been washed. Keep them at arm's length.";
public override string AfterLine => "...I ran. I always run. Everything swinging. One at a time, and never with my back to a wall.";
Dictionary<GameObject, float> _distance = new();
Dictionary<GameObject, float> _distanceNext = new();
readonly HashSet<GameObject> _seen = new();
readonly List<Vector3> _counted = new();
Vector3 _nearestDir;
float _time;
protected override float Sense()
{
_seen.Clear();
_counted.Clear();
_distanceNext.Clear();
HeadCount = 0;
_nearestDir = Vector3.Zero;
// Hidden (a graft, a hiding place): they cannot see us, so they are only shapes.
if ( GameObject.Tags.Has( PlayerPhobia.UnseenTag ) )
{
_distance.Clear();
return 0f;
}
var center = Center;
var facing = Player.WorldRotation.Forward;
float radius = MathF.Max( CrowdRadius, 1f );
float total = 0f, nearest = float.MaxValue;
var pull = Vector3.Zero;
void Consider( GameObject go )
{
if ( !go.IsValid() || !go.Active || _seen.Count > 96 ) return;
if ( !_seen.Add( go ) ) return;
if ( IsUnder( go, GameObject ) ) return; // grafts are part of us
var to = go.WorldPosition - center;
float distance = to.Length;
if ( distance > radius ) return;
// One body is often several tagged objects; count each spot once.
foreach ( var spot in _counted )
if ( (spot - go.WorldPosition).LengthSquared < 24f * 24f ) return;
_counted.Add( go.WorldPosition );
float near = 1f - distance / radius;
float weight = near * (0.35f + 0.65f * near);
var dir = distance > 0.01f ? to / distance : facing;
weight *= 1f + FrontBonus * MathF.Max( 0f, Vector3.Dot( facing, dir ) );
_distanceNext[go] = distance;
if ( _distance.TryGetValue( go, out float before ) )
{
float closing = (before - distance) / MathF.Max( SenseInterval, 0.05f );
weight *= 1f + ClosingBonus * Math.Clamp( closing / 200f, 0f, 1f );
}
total += weight;
pull += dir * weight;
HeadCount++;
if ( distance < nearest ) { nearest = distance; _nearestDir = dir; }
}
foreach ( var misfit in Scene.GetAllComponents<Misfit>() )
{
if ( misfit.Profile is { Healed: true } ) continue; // friends now
Consider( misfit.GameObject );
}
foreach ( var body in Scene.GetAllComponents<MisfitBody>() )
{
if ( body.Profile is { Healed: true } ) continue;
Consider( body.GameObject );
}
foreach ( var go in Scene.FindAllWithTag( "misfit" ) ) Consider( go );
foreach ( var go in Scene.FindAllWithTag( "crowd" ) ) Consider( go );
foreach ( var go in Scene.FindAllWithTag( PlayerPhobia.BodyTag ) ) Consider( go );
(_distance, _distanceNext) = (_distanceNext, _distance);
if ( HeadCount == 0 ) return 0f;
CauseDir = pull.Length > 0.001f ? pull.Normal * 100f : Vector3.Zero;
RawCause = HeadCount == 1 ? "someone too close" : "the crowd";
return total / MathF.Max( CrowdTolerance, 0.1f );
}
public override void Modify( ref PhobiaMods mods, float senses, float body, bool attack, float dt )
{
float pressure = Pressure;
if ( pressure <= 0.01f ) return;
_time += dt;
// Camera: shakes with how many and how near, before calm has even gone.
mods.Shake += ShakeMax * pressure;
if ( attack ) return;
// Input: the eyes keep going back to the nearest one.
if ( LookTug > 0f && _nearestDir.LengthSquared > 0.0001f )
{
float want = MathF.Atan2( _nearestDir.y, _nearestDir.x ) * 180f / MathF.PI;
float off = want - Player.Look.yaw;
off -= MathF.Round( off / 360f ) * 360f;
float step = LookTug * pressure * dt;
mods.LookYaw += Math.Clamp( off, -step, step );
}
// Input: it wants out, and runs in surges.
float drive = MathF.Max( body, pressure * 0.5f );
mods.Speed *= 1f + (SurgeSpeed - 1f) * drive * (0.5f + 0.5f * MathF.Sin( _time * 5f ));
mods.Wobble += body;
}
public override void Flinch( float amount, float speed, float kick )
{
if ( CauseDir.LengthSquared > 0.0001f ) Player.Lunge( -CauseDir.Normal, speed * amount );
Player.Kick( RandomUnit() * kick * 1.5f );
}
// Keep running from wherever they are now.
public override Vector3? AttackSteer( Vector3? current, float dt )
{
if ( CauseDir.LengthSquared < 0.0001f ) return current;
return Vector3.Lerp( current ?? -CauseDir.Normal, -CauseDir.Normal, Math.Clamp( dt * 3f, 0f, 1f ) );
}
}