The radio state used by the Sump hub. Tracks which dial frequencies are open, the tuned position, dial labels derived from a saved per-misfit number, and the text heard through a settable Decoder or a default static-with-orders fallback. Sync reconciles open frequencies against the healed misfit list and restores the last tuned position.
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// The radio in the radio room, as other modules see it. The hub builds the room and works the
/// dial; this holds what is open, what is tuned, and what is heard. Reach it as
/// <see cref="SumpHub.Radio"/> (never null; a new one is made each time the scene starts).
///
/// Every healed misfit opens one frequency. "Position" below always means a place on the dial:
/// 0 is the first frequency that was opened, 1 the second, and so on. <see cref="Frequencies"/>
/// turns a position into the misfit who opened it.
///
/// To supply what the radio says, set <see cref="Decoder"/>: it is given the dial position and
/// how many frequencies are open, and returns the words heard. Until then the radio gives static
/// with the misfit's own remembered orders under it, a little clearer for each frequency open.
/// </summary>
public sealed class SumpRadio
{
readonly List<int> _frequencies = new();
/// <summary>Index (into SumpState.Misfits) of each healed misfit who opened a frequency, in the order they were opened.</summary>
public IReadOnlyList<int> Frequencies => _frequencies;
/// <summary>Dial position the set is tuned to, or -1 while no frequency is open.</summary>
public int Tuned { get; private set; } = -1;
/// <summary>The dial was turned; gives the new position.</summary>
public event Action<int> TunedChanged;
/// <summary>A healed misfit opened a new frequency; gives its dial position.</summary>
public event Action<int> FrequencyOpened;
/// <summary>(dial position, frequencies open) to the text heard there. Null for the default.</summary>
public Func<int, int, string> Decoder { get; set; }
/// <summary>True while the player is sat at the set with the headphones on.</summary>
public bool PlayerListening { get; internal set; }
/// <summary>True once the radio room stands (somebody is healed).</summary>
public bool Built { get; internal set; }
/// <summary>Where the working set is, in the world. Only meaningful once <see cref="Built"/>.</summary>
public Vector3 Position { get; internal set; }
/// <summary>Middle of the floor in front of the set, where listeners gather.</summary>
public Vector3 ListeningSpot { get; internal set; }
/// <summary>The misfit whose frequency is tuned, or null.</summary>
public MisfitProfile TunedMisfit => MisfitAt( Tuned );
/// <summary>The misfit who opened the frequency at a dial position, or null.</summary>
public MisfitProfile MisfitAt( int position )
{
if ( position < 0 || position >= _frequencies.Count ) return null;
int index = _frequencies[position];
return index >= 0 && index < SumpState.Misfits.Count ? SumpState.Misfits[index] : null;
}
/// <summary>Tenths of a megahertz for a dial position, 881 to 1079 (88.1 to 107.9).</summary>
public int Tenths( int position )
{
var profile = MisfitAt( position );
if ( profile is null ) return 881;
// Saved, so a frequency keeps its number for the life of the Sump.
string key = $"radio_freq_{profile.Index}";
if ( int.TryParse( SumpState.GetNote( key ), out int saved ) && saved >= 881 && saved <= 1079 ) return saved;
int tenths = 881 + MisfitGenerator.Mix( profile.Seed, 0xF3E9, profile.Index ) % 199;
SumpState.SetNote( key, tenths.ToString() );
return tenths;
}
/// <summary>The dial label for a position: "94.7".</summary>
public string Label( int position )
{
int tenths = Tenths( position );
return $"{tenths / 10}.{tenths % 10}";
}
/// <summary>Turns the dial to a position (clamped to what is open).</summary>
public void Tune( int position )
{
position = _frequencies.Count == 0 ? -1 : Math.Clamp( position, 0, _frequencies.Count - 1 );
if ( position == Tuned ) return;
Tuned = position;
if ( position >= 0 ) SumpState.SetNote( "radio_tuned", position.ToString() );
TunedChanged?.Invoke( position );
}
/// <summary>Turns the dial one frequency up or down, wrapping round the ends.</summary>
public void Step( int direction )
{
if ( _frequencies.Count == 0 ) return;
int count = _frequencies.Count;
Tune( ((Math.Max( Tuned, 0 ) + Math.Sign( direction )) % count + count) % count );
}
/// <summary>What is coming out of the set right now.</summary>
public string Heard()
{
if ( Tuned < 0 ) return "Static. Every channel. Nobody has ever been on the other end.";
try
{
var text = Decoder?.Invoke( Tuned, _frequencies.Count );
if ( !string.IsNullOrEmpty( text ) ) return text;
}
catch ( Exception e ) { Log.Warning( $"Sump radio decoder failed: {e.Message}" ); }
return DefaultDecoder( Tuned, _frequencies.Count );
}
static readonly string[] Hiss = { "kssh", "ffff", "...", "tk-tk", "shhhh" };
/// <summary>
/// Static, with the misfit's own orders under it. The more frequencies are open, the fewer
/// words the static eats.
/// </summary>
public string DefaultDecoder( int position, int open )
{
var profile = MisfitAt( position );
if ( profile is null ) return "Static.";
string orders = MisfitVoice.OrdersLine( profile );
float clear = 0.3f + 0.7f * Math.Clamp( open / (float)SumpState.MisfitCount, 0f, 1f );
var rng = new Random( MisfitGenerator.Mix( profile.Seed, position, open ) );
var words = orders.Split( ' ' );
for ( int i = 0; i < words.Length; i++ )
if ( rng.NextDouble() > clear ) words[i] = Hiss[rng.Next( Hiss.Length )];
return $"{Label( position )}. Static, and under it {MisfitVoice.ShortName( profile )}'s own voice, years old: \"{string.Join( " ", words )}\" Then static. Nobody answers.";
}
// ---- For the hub ------------------------------------------------------------------------------
/// <summary>Brings the open frequencies in line with who is healed, in the order given. Returns true if any were added.</summary>
internal bool Sync( IReadOnlyList<int> healedInOrder )
{
bool added = false;
_frequencies.RemoveAll( index => !healedInOrder.Contains( index ) );
foreach ( int index in healedInOrder )
{
if ( _frequencies.Contains( index ) ) continue;
_frequencies.Add( index );
Tenths( _frequencies.Count - 1 );
added = true;
FrequencyOpened?.Invoke( _frequencies.Count - 1 );
}
if ( _frequencies.Count == 0 ) Tuned = -1;
else if ( Tuned < 0 || Tuned >= _frequencies.Count )
{
int.TryParse( SumpState.GetNote( "radio_tuned", "0" ), out int saved );
Tuned = Math.Clamp( saved, 0, _frequencies.Count - 1 );
}
return added;
}
}