Sump/Trauma/LightPhobia.cs
using System;
using System.Collections.Generic;
/// <summary>
/// Fear of light: the white flash of the stage lights. A few times a second it finds the enabled
/// point and spot lights that reach the lizard (distance, radius, brightness, spot cone, and a
/// line-of-sight trace for the strongest few), plus anything tagged "sump_bright". A sudden jump
/// is a flash and costs calm at once.
/// What it does: bright light hurts (the camera's bloom is driven up hard), the eyes squint (the
/// picture darkens and narrows), the look goes heavy, it flinches away from the source, and a
/// panic attack dives for the darkest place nearby.
/// </summary>
[Title( "Phobia: Light" ), Category( "Sump" ), Icon( "flare" )]
public sealed class LightPhobia : PhobiaBase
{
public override Phobia Kind => Phobia.Light;
/// <summary>A light whose colour times intensity has this brightness counts as fully bright.</summary>
[Property, Group( "Light" )] public float BrightReference { get; set; } = 1f;
/// <summary>How far a "sump_bright" object lights the lizard, in inches.</summary>
[Property, Group( "Light" )] public float BrightTagRadius { get; set; } = 350f;
/// <summary>A jump in light this big between two looks is a flash.</summary>
[Property, Group( "Light" ), Range( 0.05f, 1f )] public float FlashJump { get; set; } = 0.35f;
/// <summary>Calm lost at once to a flash.</summary>
[Property, Group( "Light" ), Range( 0f, 1f )] public float FlashHit { get; set; } = 0.2f;
/// <summary>How far it looks around for somewhere darker to run to, in inches.</summary>
[Property, Group( "Light" )] public float DarkProbe { get; set; } = 240f;
/// <summary>Bloom strength added in full light.</summary>
[Property, Group( "Effects" )] public float BloomPain { get; set; } = 5f;
/// <summary>How much the picture darkens when the eyes are screwed shut against full light (0 = not at all).</summary>
[Property, Group( "Effects" ), Range( 0f, 0.9f )] public float SquintDarken { get; set; } = 0.35f;
/// <summary>Share of mouse look lost in full light.</summary>
[Property, Group( "Effects" ), Range( 0f, 0.9f )] public float LookDamping { get; set; } = 0.6f;
/// <summary>How lit the player was at the last look, 0 to 1.</summary>
public float Lit { get; private set; }
public override string FirstLine => "That light. The stage lights went white and then they all saw me. All of me. Stay in the dark.";
public override string AfterLine => "...I went blind again. Shadows. Keep to the shadows.";
struct LightSample
{
public Vector3 Position;
public float Radius;
public float Bright;
public bool Spot;
public Vector3 Forward;
public float ConeInner, ConeOuter;
public float Felt;
}
readonly List<LightSample> _lights = new();
float _flash; // a flash still ringing in the eyes, 1 fading to 0
float _time;
protected override float Sense()
{
_lights.Clear();
var center = Center;
foreach ( var light in Scene.GetAllComponents<PointLight>() )
{
if ( !light.IsValid() || !light.Active || IsUnder( light.GameObject, GameObject ) ) continue;
AddLight( center, new LightSample
{
Position = light.WorldPosition,
Radius = light.Radius,
Bright = Brightness( light.LightColor, light.Intensity ),
} );
}
foreach ( var light in Scene.GetAllComponents<SpotLight>() )
{
if ( !light.IsValid() || !light.Active || IsUnder( light.GameObject, GameObject ) ) continue;
AddLight( center, new LightSample
{
Position = light.WorldPosition,
Radius = light.Radius,
Bright = Brightness( light.LightColor, light.Intensity ),
Spot = true,
Forward = light.WorldRotation.Forward,
ConeInner = light.ConeInner,
ConeOuter = light.ConeOuter,
} );
}
int looked = 0;
foreach ( var go in Scene.FindAllWithTag( PlayerPhobia.BrightTag ) )
{
if ( ++looked > 64 ) break;
if ( !go.IsValid() || !go.Active || IsUnder( go, GameObject ) ) continue;
AddLight( center, new LightSample { Position = go.WorldPosition, Radius = BrightTagRadius, Bright = 1f } );
}
// Strongest first, and only the strongest few get a line-of-sight trace.
_lights.Sort( ( a, b ) => b.Felt.CompareTo( a.Felt ) );
if ( _lights.Count > 6 ) _lights.RemoveRange( 6, _lights.Count - 6 );
float lit = LitAt( center, out int strongest );
// A sudden jump is a flash: it hits calm at once, on top of the steady drain.
if ( lit - Lit > FlashJump )
{
Shock( FlashHit );
_flash = 1f;
Player.Kick( RandomUnit() * 8f );
RawCause = "the flash";
}
Lit = lit;
if ( strongest < 0 || lit <= 0.01f ) return 0f;
CauseDir = _lights[strongest].Position - center;
if ( string.IsNullOrEmpty( RawCause ) ) RawCause = "the light";
return lit;
}
float Brightness( Color colour, float intensity )
{
float luminance = colour.r * 0.3f + colour.g * 0.59f + colour.b * 0.11f;
float bright = luminance * MathF.Max( intensity, 0f ) / MathF.Max( BrightReference, 0.01f );
return float.IsNaN( bright ) ? 0f : Math.Clamp( bright, 0f, 1.5f );
}
void AddLight( Vector3 center, LightSample light )
{
light.Felt = Falloff( light, center );
if ( light.Felt > 0.01f ) _lights.Add( light );
}
/// <summary>How much of a light reaches a point, ignoring anything in the way.</summary>
static float Falloff( in LightSample light, Vector3 point )
{
if ( light.Radius <= 1f || light.Bright <= 0f ) return 0f;
var to = point - light.Position;
float distance = to.Length;
if ( distance >= light.Radius ) return 0f;
float near = 1f - distance / light.Radius;
float felt = near * near * light.Bright;
if ( light.Spot && distance > 1f )
{
float angle = MathF.Acos( Math.Clamp( Vector3.Dot( light.Forward, to / distance ), -1f, 1f ) ) * 180f / MathF.PI;
if ( angle >= light.ConeOuter ) return 0f;
if ( angle > light.ConeInner && light.ConeOuter > light.ConeInner )
felt *= 1f - (angle - light.ConeInner) / (light.ConeOuter - light.ConeInner);
}
return felt;
}
/// <summary>How lit a point is by the lights found at the last look, counting what stands in the way.</summary>
float LitAt( Vector3 point, out int strongest )
{
strongest = -1;
float best = 0f, sum = 0f;
for ( int i = 0; i < _lights.Count; i++ )
{
var light = _lights[i];
float felt = Falloff( light, point );
if ( felt <= 0.01f ) continue;
// Start a little way out of the light so its own fitting does not block it.
var to = point - light.Position;
float length = to.Length;
if ( length > 12f )
{
var from = light.Position + to / length * 6f;
var tr = Scene.Trace.Ray( from, point ).IgnoreGameObjectHierarchy( GameObject ).WithoutTags( "debris", "crowd" ).Run();
if ( tr.Hit && !tr.StartedSolid && tr.Distance < length - 6f - Player.Radius * 1.5f ) continue; // in shadow
}
sum += felt;
if ( felt > best ) { best = felt; strongest = i; }
}
return Math.Clamp( sum, 0f, 1f );
}
/// <summary>The way toward the darkest place nearby, or away from the light if nowhere is darker.</summary>
public Vector3 DarkestDirection()
{
var center = Center;
var away = CauseDir.LengthSquared > 0.0001f ? -CauseDir.Normal : -Player.WorldRotation.Forward;
if ( _lights.Count == 0 ) return away;
// Look round along the surface we are on.
var up = Player.Up;
var first = Flatten( away, up );
if ( first.Length < 0.01f ) first = Flatten( Player.WorldRotation.Forward, up );
if ( first.Length < 0.01f ) return away;
first = first.Normal;
var best = away;
float bestScore = float.MaxValue;
for ( int i = 0; i < 8; i++ )
{
var dir = Rotation.FromAxis( up, i * 45f ) * first;
var tr = Ray( center, center + dir * DarkProbe );
float reach = tr.Hit ? tr.Distance - Player.Radius * 2f : DarkProbe;
if ( reach < 40f ) continue; // a wall right there: nowhere to go
float lit = LitAt( center + dir * reach, out _ );
// Among equally dark places, prefer the one further from the light.
float score = lit - 0.05f * Vector3.Dot( dir, away );
if ( score < bestScore ) { bestScore = score; best = dir; }
}
return best;
}
public override void Modify( ref PhobiaMods mods, float senses, float body, bool attack, float dt )
{
_time += dt;
_flash = MathF.Max( 0f, _flash - dt * 0.8f );
// How much the light hurts right now: the light itself, and a flash still ringing.
float pain = Math.Clamp( MathF.Max( Pressure, _flash * Keep ), 0f, 1f );
if ( pain <= 0.01f && body <= 0.01f ) return;
// Post-processing: bloom pain, and a squint that darkens and narrows the picture.
mods.Bloom += BloomPain * pain * pain;
mods.Squint = MathF.Max( mods.Squint, pain );
mods.Brightness *= 1f - SquintDarken * pain;
if ( attack ) return;
// Input: the look goes heavy, as if turning the head hurt; shaken, it stutters and wanders.
mods.LookDamp = MathF.Max( mods.LookDamp, LookDamping * pain );
mods.Speed *= 1f + 0.35f * body * MathF.Sin( _time * 9f ) * MathF.Sin( _time * 2.3f );
mods.Wobble += 1.6f * body;
}
public override void Flinch( float amount, float speed, float kick )
{
Player.Lunge( DarkestDirection(), speed * 1.3f * amount );
Player.Kick( RandomUnit() * kick * 1.5f );
}
public override Vector3? AttackStart() => DarkestDirection();
}