Text generation helpers for the misfits' dialogue. Speak() runs a line through the misfit's speech tic (stutter, echo, jingle, backwards, whisper, prices) with a seeded Random, and the other methods return polished one-liners for greetings, its clung object, its fear, its orders, the help request, the thank-you after healing, plus a Dossier summary string for the HUD.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// How a misfit talks. Speak() pushes a plain sentence through its speech tic; the other methods
/// return ready-made lines (tic already applied), always the same for the same misfit.
/// </summary>
public static partial class MisfitVoice
{
// ------------------------------------------------------------------ the tic
/// <summary>Says a plain line the way this misfit says things. Healed misfits speak more gently.</summary>
public static string Speak( MisfitProfile p, string plainLine ) => Speak( p, plainLine, p is not null && p.Healed );
/// <summary>As Speak, choosing whether the tic is at full strength or nearly gone.</summary>
public static string Speak( MisfitProfile p, string plainLine, bool gentle )
{
if ( p is null || string.IsNullOrWhiteSpace( plainLine ) ) return plainLine ?? "";
var line = plainLine.Trim();
var rng = new Random( MisfitGenerator.Mix( p.Seed, MisfitGenerator.Hash( line ), 0x71C ) );
switch ( p.SpeechTic )
{
case "stutter": return Stutter( line, rng, gentle );
case "echo": return Echo( line, rng, gentle );
case "jingle": return gentle ? line : Jingle( line, rng );
case "backwards": return Backwards( line, gentle );
case "whisper": return gentle ? line : Whisper( line );
case "prices": return gentle ? line + " No charge." : Prices( line, rng );
default: return line;
}
}
static string Stutter( string line, Random rng, bool gentle )
{
var words = line.Split( ' ' );
int left = gentle ? 1 : Math.Max( 2, words.Length / 4 );
for ( int i = 0; i < words.Length && left > 0; i++ )
{
var w = words[i];
if ( w.Length == 0 || !char.IsLetter( w[0] ) ) continue;
if ( i != 0 && rng.Next( 4 ) != 0 ) continue;
if ( gentle && i != 0 ) break;
int take = 1;
if ( w.Length > 2 )
{
var two = w.Substring( 0, 2 ).ToLowerInvariant();
if ( two == "th" || two == "sh" || two == "ch" || two == "wh" ) take = 2;
}
var start = w.Substring( 0, take );
// The worst words catch twice.
words[i] = !gentle && rng.Next( 3 ) == 0 ? $"{start}-{start}-{w}" : $"{start}-{w}";
left--;
}
return string.Join( " ", words );
}
static string Echo( string line, Random rng, bool gentle )
{
var sentences = SplitSentences( line );
for ( int i = 0; i < sentences.Count; i++ )
{
bool last = i == sentences.Count - 1;
if ( !last && (gentle || rng.Next( 2 ) == 0) ) continue;
SplitEnd( sentences[i], out var body, out var end );
var word = LastWord( body );
if ( word.Length == 0 ) continue;
if ( word != "I" ) word = word.ToLowerInvariant();
sentences[i] = gentle ? $"{body}... {word}{end}" : $"{body}... {word}... {word}{end}";
}
return string.Join( " ", sentences );
}
static readonly string[] BuiltInJingles =
{
"Now with real feelings!", "Batteries not included!", "Ask your doctor if it is right for you!", "Only while stocks last!",
"Nine out of ten humans agree!", "Call now, operators are standing by!", "New and improved!", "Part of a balanced breakfast!",
"Terms and conditions apply!", "But wait, there is more!", "For a limited time only!", "As seen on TV!",
"Whiter whites, guaranteed!", "Just add water!", "Collect all nine!", "May cause wind!",
"Now in a family-size tub!", "Do not apply to the face!", "Side effects include everything!", "Free ham with every purchase!",
"Tested on volunteers who regret it!", "Fits most holes!", "Wipes clean!", "Now thirty percent less sad!",
"The itch means it is working!",
};
static string Jingle( string line, Random rng )
{
var sentences = SplitSentences( line );
int first = rng.Next( sentences.Count );
int a = rng.Next( Jingles.Length );
sentences[first] += " " + Jingles[a];
if ( sentences.Count >= 3 && first != sentences.Count - 1 )
{
int b = (a + 1 + rng.Next( Jingles.Length - 1 )) % Jingles.Length;
sentences[sentences.Count - 1] += " " + Jingles[b];
}
var said = string.Join( " ", sentences );
return rng.Next( 2 ) == 0 ? said + " Sorry." : said;
}
static string Backwards( string line, bool gentle )
{
var sentences = SplitSentences( line );
for ( int i = 0; i < sentences.Count; i++ )
{
// Nearly better: only the last thing it says still comes out the wrong way round.
if ( gentle && (i != sentences.Count - 1 || sentences.Count == 1) ) continue;
sentences[i] = ReverseSentence( sentences[i] );
}
return string.Join( " ", sentences );
}
static string ReverseSentence( string sentence )
{
SplitEnd( sentence, out var body, out var end );
var words = body.Split( ' ', StringSplitOptions.RemoveEmptyEntries ).Select( w => w.TrimEnd( ',', ';', ':' ) ).Where( w => w.Length > 0 ).ToList();
if ( words.Count < 2 ) return sentence;
if ( words.Count <= 7 )
{
// Short sentences come out entirely back to front.
if ( words[0] != "I" && !words[0].StartsWith( "I'" ) ) words[0] = LowerFirst( words[0] );
words.Reverse();
words[0] = UpperFirst( words[0] );
}
else
{
// Long ones start well and lose their footing at the end.
words.Reverse( words.Count - 4, 4 );
}
return string.Join( " ", words ) + end;
}
static string Whisper( string line )
{
var sb = new StringBuilder();
var sentences = SplitSentences( line );
for ( int i = 0; i < sentences.Count; i++ )
{
SplitEnd( sentences[i], out var body, out var end );
if ( i > 0 ) sb.Append( ' ' );
sb.Append( body.ToLowerInvariant() );
sb.Append( end.Contains( '?' ) ? "...?" : "..." );
}
return sb.ToString();
}
static readonly string[] BuiltInPriceTags =
{
"$4.99", "$19.95", "two for $5", "$0.50, as is", "$120 or best offer", "free to a good home",
"$7, slightly damaged", "price on request", "$3.99 with coupon", "$1, was $40", "$65 the pair", "$2.50, no box",
"$9.99 plus feelings", "three for a tenner", "$0.01, final offer", "make me an offer, please", "$45, smells a bit", "swap for a ham",
"$14.99, some assembly required", "half price, half missing",
};
static readonly string[] BuiltInPriceTails = { "No refunds.", "All sales final.", "Was more. Now less.", "Cash only.", "Sold as seen.", "Manager's special.", "Dignity not included." };
static readonly HashSet<string> NotForSale = new( StringComparer.OrdinalIgnoreCase )
{
"there", "their", "would", "could", "should", "about", "every", "never", "still", "nobody", "because", "before", "after",
"where", "which", "think", "thank", "please", "hello", "sorry", "always", "until", "myself", "other", "these", "those",
"being", "really", "again", "everything", "anything", "something", "someone", "somebody", "anyone", "perhaps", "little",
};
static string Prices( string line, Random rng )
{
var sentences = SplitSentences( line );
int tagged = 0;
int start = rng.Next( sentences.Count );
for ( int n = 0; n < sentences.Count && tagged < 2; n++ )
{
int i = (start + n) % sentences.Count;
var words = sentences[i].Split( ' ' );
var candidates = new List<int>();
for ( int w = 0; w < words.Length; w++ )
{
var core = words[w].Trim( '.', ',', '!', '?', ';', ':', '\'', '"' );
if ( core.Length >= 5 && core.All( char.IsLetter ) && !NotForSale.Contains( core ) ) candidates.Add( w );
}
if ( candidates.Count == 0 ) continue;
int pick = candidates[rng.Next( candidates.Count )];
var word = words[pick];
int cut = word.Length;
while ( cut > 0 && !char.IsLetter( word[cut - 1] ) ) cut--;
words[pick] = $"{word.Substring( 0, cut )} ({PriceTags[rng.Next( PriceTags.Length )]}){word.Substring( cut )}";
sentences[i] = string.Join( " ", words );
tagged++;
}
var said = string.Join( " ", sentences );
return rng.Next( 3 ) == 0 ? said + " " + PriceTails[rng.Next( PriceTails.Length )] : said;
}
// ------------------------------------------------------------------ ready-made lines
static readonly string[] BuiltInGreetings =
{
"Oh. A visitor. I was not hiding. I was standing very still on purpose.",
"You are not from the surface, are you? You do not smell of lawn.",
"Hello. I am supposed to say 'greetings'. The last time I said it, it went badly.",
"Please do not make any sudden kindness. I am not used to it.",
"You can stay. Most things down here do, in the end.",
"I came as {arole}. I am sorry, I do not have a card any more.",
"You have the look of someone who landed badly. Sit down. We all did.",
"Mind my {obj}. Everything else you may step on.",
"Hello. Excuse the smell. Some of it is the cave, and the rest is nerves.",
"Do not mind me. I was only screaming quietly. It is a hobby.",
"You are the one who fell down the hole. We heard. You bounced twice. It was the best thing in years.",
"Is that a second tail, at the front? No. Do not tell me. Everybody down here has something.",
"Welcome to the Sump. The toilet is everywhere. That is the whole problem.",
"I came as {arole}. These days I am mostly a cautionary tale.",
"I would offer you a seat, but that one is mine, and so is that one. I get nervous without a spare.",
};
static string[] BuiltInGreetingsFor( FirstContact c ) => c switch
{
FirstContact.ShotOnFarm => new[]
{
"You are not holding anything long and metal. Good. That is a good start.",
"If you hear a click, tell me. Before, if possible.",
"You do not smell of gunpowder. You smell of something, but not that.",
"I was told humans greet you with a firm handshake. I got a firm everything else.",
},
FirstContact.Dissected => new[]
{
"You may look. Looking is fine. Just keep your hands where I can count them.",
"I am all here today. I checked this morning. Hello.",
"Before you ask: no, you may not see inside. Everybody has seen inside. Inside is closed.",
"If you need my temperature, I will tell you what it is. Out loud. From over here.",
},
FirstContact.WorshippedThenSold => new[]
{
"Please do not kneel. And please do not make an offer.",
"Welcome. Entry is free. That is not a figure of speech, down here.",
"I used to do blessings. Now I only do small nods. Here is a small nod.",
"You may not touch the hem. There is no hem. They sold the hem.",
},
FirstContact.BecameMeme => new[]
{
"You are looking at me. Thank you. Please do not stop too suddenly.",
"If you recognise me, be kind and pretend that you do not.",
"Yes, it is me. No, I will not do the noise. The noise was an accident.",
"If you are filming, my left side is the one that did not go round the world.",
},
_ => new[]
{
"Is it after school yet? No. No, of course. Hello.",
"You came back. No. You are someone else. That is all right. Hello anyway.",
"Are you a grown-up? You look like you pay for things. Hello.",
"I may stay up as late as I like now. It is horrible. Hello.",
},
};
/// <summary>A hello. Different variants give different lines; the first few belong to its own story.</summary>
public static string Greeting( MisfitProfile p, int variant )
{
if ( p is null ) return "";
var own = GreetingsFor( p.Contact );
var lines = own.Concat( Greetings ).ToArray();
// Stepping by 3 visits every line before repeating, unless the count divides by 3.
int step = lines.Length % 3 == 0 ? 1 : 3;
int at = (Pick( p, 0x61, lines.Length ) + Math.Abs( variant % lines.Length ) * step) % lines.Length;
return Say( p, lines[at] );
}
static readonly string[] BuiltInObjectLines =
{
"This is my {obj}. {note}",
"You are looking at my {obj}. You may look. {note}",
"My {obj}. It is the one thing from up there that never did anything to me. {note}",
"Do not touch my {obj}, please. {note}",
"I keep my {obj} where I can feel it. {note}",
"Have you met my {obj}? Say hello. It will not say it back. It is shy. {note}",
"Careful. That is my {obj}. I would swap any three of my limbs for it. {note}",
"Everybody down here has a thing. Mine is my {obj}. {note}",
};
/// <summary>What it says about the human thing it clings to.</summary>
public static string AboutObject( MisfitProfile p )
{
if ( p is null ) return "";
return Say( p, ObjectLines[Pick( p, 0x62, ObjectLines.Length )] );
}
static string[] BuiltInFearLines( string kind ) => kind switch
{
"light" => new[]
{
"It is {fear}. When it gets bright, somebody is about to find me.",
"I keep away from {fear}. The dark is where nothing has happened to me yet.",
"Do you know what I cannot bear? It is {fear}. Everything bad was very well lit.",
"It is {fear}. I do not have the right organs to wet myself, and yet.",
},
"noise" => new[]
{
"It is {fear}. Loud things come just before the bad part.",
"I listen for {fear} all the time. That is the trouble. I never stop listening.",
"It is {fear}. I heard it once, and after that nothing was the same shape.",
"It is {fear}. My whole body clenches, and I am mostly clench to begin with.",
},
"touch" => new[]
{
"It is {fear}. The last hands that reached for me did not ask.",
"Please stay an arm away. It is {fear}. It is not you. It is every hand before you.",
"It is {fear}. I know what comes after it. I have known for years.",
"It is {fear}. People keep checking whether I am squishy. I am. That is private.",
},
"crowds" => new[]
{
"It is {fear}. One human is a person. Forty humans is a decision.",
"I keep away from {fear}. When they gather, they agree about me. It is never good, what they agree.",
"It is {fear}. I was so popular once. I would not wish it on anyone.",
"It is {fear}. A crowd is one animal with too many elbows and no manners.",
},
"open" => new[]
{
"It is {fear}. Out there everything can see you, and you cannot see all of it back.",
"I need a wall behind me. It is {fear}. That is how they got me the first time.",
"It is {fear}. I like a low ceiling. A low ceiling has never let me down.",
"It is {fear}. Too much sky makes my bottom feel exposed. I use the word 'bottom' loosely.",
},
_ => new[]
{
"It is {fear}. Being seen was the worst thing that ever happened to me, and it happened so many times.",
"Do not stare, please. It is {fear}. I can feel a look like a hand.",
"It is {fear}. I know when I am being looked at. It is the one thing I am good at.",
"It is {fear}. When they stare I forget how to stand, and I only learned last year.",
},
};
/// <summary>What it says about the thing it cannot bear.</summary>
public static string AboutFear( MisfitProfile p )
{
if ( p is null ) return "";
var lines = FearLines( p.FearKind );
return Say( p, lines[Pick( p, 0x63, lines.Length )] );
}
static string[] BuiltInHelpLines( FirstContact c ) => c switch
{
FirstContact.ShotOnFarm => new[]
{
"There is a field in me that I never got out of. It is always that night. Will you walk it with me, and get me to the fence this time?",
"I can still hear him loading. If you came into that night with me, perhaps one of us could reach the trees.",
"I go back to the farm every time I close up. I never get past the barn. Come with me. You look like you run well.",
"I left my nerve in that field, and possibly a kidney. Come back there with me. Help me cross it without being shot in anything.",
},
FirstContact.Dissected => new[]
{
"Part of me is still on that table. If I open the door, will you go in and help me walk out of the building? All of me, this time.",
"I never finished escaping. I got out, but I did not get out. Come back to the laboratory with me. I know where the vents are.",
"There is a room with a bright lamp, and I am still in it. I can open the way. I cannot go alone.",
"In there they are always about to start on me. Come in with me. I have seen your teeth. Help me break out.",
},
FirstContact.WorshippedThenSold => new[]
{
"They are still bidding on me, somewhere in here. Will you come to the sale with me, and help me say that I am not for sale?",
"I remember the lights, and the applause, and a man reading out my price. Come back there with me. This time I would like to answer.",
"First they sang to me, then they sold me, and all of it was on a stage. I can take you there. Help me walk off it.",
"In my head I am still on a shopping channel, between a foot spa and a ham. Come with me. Help me walk off before they bundle me with the ham.",
},
FirstContact.BecameMeme => new[]
{
"The show is still on, in here. They are still laughing. Will you come on with me? I think I could bear it with somebody in the other chair.",
"I am stuck in my nine days. Same studio, same host, same face. Come and sit beside me, and help me say something of my own.",
"There is an audience in my head that never went home. If you came on as my guest, we could end the episode. Properly.",
"In here the red light is always on and the host is always winking. Come on the show with me. Help me break something expensive.",
},
_ => new[]
{
"There is a farmhouse where I am still waiting in a box. Will you come, and help me find the way out before the school bus comes back empty again?",
"I have been waiting since after school. If you came into that house with me, I think I could stop waiting and leave.",
"It is always the last night of summer in there, and I am very small. Come with me. Help me get out of the toy box by myself.",
"In there it is moving day, and I am not on the list. Come with me. Help me leave that house on my own legs, or whatever these are.",
},
};
/// <summary>The line that invites the player into its first contact.</summary>
public static string AskForHelp( MisfitProfile p )
{
if ( p is null ) return "";
var lines = HelpLines( p.Contact );
return Say( p, lines[Pick( p, 0x64, lines.Length )] );
}
static string[] BuiltInThanksLines( FirstContact c ) => c switch
{
FirstContact.ShotOnFarm => new[]
{
"I reached the fence. I did not know there was a fence. Thank you.",
"Nobody fired. I keep waiting for it, and nobody fires. Thank you for running with me.",
"I can stand in the open for a moment now. A short moment. It is mine. Thank you.",
"I crossed the whole field and nothing went into me. Not one thing. I am going to stand up straight now. Thank you.",
},
FirstContact.Dissected => new[]
{
"I counted on the way out. It is all here. All of me walked out. Thank you.",
"The lamp is off. I switched it off myself. Thank you for holding the door.",
"I think I can be touched now. Not today. But I think so. Thank you.",
"I am shut. Properly shut, with everything on the inside. If you find anything left over, keep it. Thank you.",
},
FirstContact.WorshippedThenSold => new[]
{
"I said no, and the hammer did not come down. I am not for sale. Thank you.",
"Nobody is bidding. It is so quiet. I am worth nothing at all, and it is wonderful. Thank you.",
"You did not kneel and you did not make an offer. You just came. Thank you.",
"I am free. Not half price. Not two for one. Free. I may have blessed you by accident. It wears off. Thank you.",
},
FirstContact.BecameMeme => new[]
{
"They stopped laughing and I was still there. I did not know I would still be there. Thank you.",
"The episode is over. I said my own line. Nobody clipped it. Thank you.",
"You looked at me, and it was only looking. Thank you.",
"We broke every camera and I am still here. If anyone asks, the noise was the chair. Thank you.",
},
_ => new[]
{
"I got out of the box by myself. Well. With you. That counts. Thank you.",
"I am not waiting any more. I checked, and I am just here. Thank you.",
"She forgot me, and I am still real. I did not know both of those could be true. Thank you.",
"I left before they could leave me. It is a small difference. It is the whole difference. Thank you.",
},
};
static readonly string[] BuiltInThanksTails =
{
"",
" Take a piece of me with you. I can spare it now.",
" I am coming with you. Carry some of me, and I will carry the {obj}.",
" Here. A bit of me, for you. It is the good bit. Do not ask which bit it was.",
" Have a piece of me. It grows back. I think. We will find out together.",
};
/// <summary>What it says once healed. The tic is gentler, or gone.</summary>
public static string Thanks( MisfitProfile p )
{
if ( p is null ) return "";
var lines = ThanksLines( p.Contact );
var line = lines[Pick( p, 0x65, lines.Length )] + ThanksTails[Pick( p, 0x66, ThanksTails.Length )];
return Speak( p, Fill( p, line ), true );
}
static readonly string[] BuiltInOrderLines =
{
"My orders were to {orders}. I am still ready. Nobody ever called.",
"I was to {orders}. I listened for the signal every night, for years. There was never a signal.",
"They told me to {orders}. I do not think they forgot me. I think the message never arrived. I prefer to think that.",
"I came as {arole}. I was to {orders}. I would still do it, if anyone asked. Nobody asks.",
"Officially I am still on duty. I was to {orders}. The radio only ever gives me weather.",
"I was to {orders}. I wrote it down so that I would not forget. The paper is gone. I have not forgotten.",
"I was to {orders}. I have been ready since I landed. I have also been in a hole. Both are true.",
"My orders were to {orders}. I sent sixty-one status reports. The last one only said 'hello?'.",
"As {arole}, I was to {orders}. I rehearse it in a puddle every morning. The puddle thinks I am doing well.",
};
/// <summary>What it still thinks its orders were, and that they never came.</summary>
public static string OrdersLine( MisfitProfile p )
{
if ( p is null ) return "";
return Say( p, OrderLines[Pick( p, 0x67, OrderLines.Length )] );
}
// ------------------------------------------------------------------ descriptions
/// <summary>The nickname if it has one, otherwise its alien name: short enough for a subtitle.</summary>
public static string ShortName( MisfitProfile p )
{
if ( p is null || string.IsNullOrEmpty( p.Name ) ) return "";
int open = p.Name.IndexOf( '"' );
int close = p.Name.LastIndexOf( '"' );
if ( open >= 0 && close > open + 1 ) return p.Name.Substring( open + 1, close - open - 1 );
const string called = ", called ";
int at = p.Name.IndexOf( called, StringComparison.Ordinal );
return at >= 0 ? p.Name.Substring( at + called.Length ) : p.Name;
}
static string BuiltInAbilityName( TwistedAbility a ) => a switch
{
TwistedAbility.BodyDoor => "Body Door",
TwistedAbility.OnlyWhileWatched => "Only While Watched",
TwistedAbility.BulletMagnet => "Bullet Magnet",
TwistedAbility.PriceTag => "Price Tag",
_ => "Toy Stillness",
};
static string BuiltInAbilityDescription( TwistedAbility a ) => a switch
{
TwistedAbility.BodyDoor => "They opened it so often that it learned to open itself. Its body is a door now, and the door goes somewhere else. It would like people to knock.",
TwistedAbility.OnlyWhileWatched => "It exists only while somebody is looking at it. Look away and it is not there. This makes going to the toilet complicated.",
TwistedAbility.BulletMagnet => "Anything thrown or fired bends toward it. It has stopped taking this personally. Pigeons are another matter.",
TwistedAbility.PriceTag => "It can put a price on anything, and the thing obeys the price. It is currently $3.99 itself, and at peace with that.",
_ => "It shrinks to a toy and cannot be noticed until somebody picks it up. It has been through the wash twice.",
};
static string BuiltInContactDescription( FirstContact c ) => c switch
{
FirstContact.ShotOnFarm => "Shot on a farm. It landed in a field at night and was fired on before it could say hello. Twice, to be sure.",
FirstContact.Dissected => "Dissected. It was caught, labelled and opened on a table by people who took notes and never once said sorry.",
FirstContact.WorshippedThenSold => "Worshipped, then sold. It was a god for one summer, a lot number by autumn and free postage by winter.",
FirstContact.BecameMeme => "Became a meme. Millions of people watched its worst moment, laughed, and scrolled on to a dog.",
_ => "Adopted, then forgotten. A child loved it for one summer and then discovered other things.",
};
static string BuiltInTicDescription( string tic ) => tic switch
{
"stutter" => "stutters",
"echo" => "repeats the last word it says",
"jingle" => "slips into advertising jingles, and is sorry about it",
"backwards" => "says its sentences back to front",
"whisper" => "can only whisper",
"prices" => "quotes a price for everything, itself included",
_ => "speaks plainly",
};
/// <summary>A short summary for a HUD panel, one fact per line.</summary>
public static string Dossier( MisfitProfile p )
{
if ( p is null ) return "";
var role = MisfitTables.FindRole( p.Role );
var thing = MisfitTables.FindThing( p.ClungObject );
string size = p.Size < 0.85f ? "small" : p.Size > 1.3f ? "large" : "middling";
var sb = new StringBuilder();
sb.Append( p.Name ).Append( '\n' );
sb.Append( UpperFirst( MisfitTables.PlanName( p.Plan ) ) ).Append( ", " ).Append( size ).Append( ". Came as " ).Append( role?.A ?? WithArticle( p.Role ) ).Append( ".\n" );
sb.Append( "First contact: " ).Append( ContactDescription( p.Contact ) ).Append( '\n' );
sb.Append( "Afraid of: " ).Append( p.Fear ).Append( ".\n" );
sb.Append( "Twitch: " ).Append( p.Twitch ).Append( ".\n" );
sb.Append( "Speech: " ).Append( TicDescription( p.SpeechTic ) ).Append( ".\n" );
sb.Append( "Clings to: " ).Append( thing?.With ?? p.ClungObject ).Append( ".\n" );
sb.Append( "Ability: " ).Append( AbilityName( p.Ability ) ).Append( ". " ).Append( AbilityDescription( p.Ability ) ).Append( '\n' );
sb.Append( p.Healed ? "Healed. It travels with you." : "Not healed yet." );
return sb.ToString();
}
// ------------------------------------------------------------------ helpers
static string Say( MisfitProfile p, string template ) => Speak( p, Fill( p, template ) );
static string Fill( MisfitProfile p, string template )
{
var role = MisfitTables.FindRole( p.Role );
var thing = MisfitTables.FindThing( p.ClungObject );
return template
.Replace( "{obj}", string.IsNullOrEmpty( p.ClungObject ) ? "things" : p.ClungObject )
.Replace( "{note}", thing?.Note ?? "It came down here with me. It is the one thing from up there that stayed." )
.Replace( "{fear}", string.IsNullOrEmpty( p.Fear ) ? "all of it" : p.Fear )
.Replace( "{arole}", role?.A ?? WithArticle( p.Role ) )
.Replace( "{orders}", role?.Orders ?? "wait for further instructions" );
}
static int Pick( MisfitProfile p, int salt, int count ) => count <= 0 ? 0 : MisfitGenerator.Mix( p.Seed, salt, p.Index ) % count;
static string WithArticle( string noun )
{
if ( string.IsNullOrEmpty( noun ) ) return "a visitor";
return ("aeiouAEIOU".IndexOf( noun[0] ) >= 0 ? "an " : "a ") + noun;
}
static string UpperFirst( string s ) => string.IsNullOrEmpty( s ) ? s : char.ToUpperInvariant( s[0] ) + s.Substring( 1 );
static string LowerFirst( string s ) => string.IsNullOrEmpty( s ) ? s : char.ToLowerInvariant( s[0] ) + s.Substring( 1 );
/// <summary>Cuts a line into sentences, each keeping its full stop, question mark or exclamation mark.</summary>
static List<string> SplitSentences( string line )
{
var list = new List<string>();
int from = 0;
for ( int i = 0; i < line.Length; i++ )
{
char ch = line[i];
if ( ch != '.' && ch != '!' && ch != '?' ) continue;
bool atEnd = i == line.Length - 1;
if ( !atEnd && line[i + 1] != ' ' ) continue;
// "a.m. to ask" and "Mr. Buttons" are not the end of anything.
if ( !atEnd && i + 2 < line.Length && char.IsLower( line[i + 2] ) ) continue;
var piece = line.Substring( from, i + 1 - from ).Trim();
if ( piece.Length > 0 ) list.Add( piece );
from = i + 1;
}
if ( from < line.Length )
{
var rest = line.Substring( from ).Trim();
if ( rest.Length > 0 ) list.Add( rest );
}
if ( list.Count == 0 ) list.Add( line );
return list;
}
/// <summary>Separates a sentence from the punctuation that ends it ("." if it has none).</summary>
static void SplitEnd( string sentence, out string body, out string end )
{
int cut = sentence.Length;
while ( cut > 0 && (sentence[cut - 1] == '.' || sentence[cut - 1] == '!' || sentence[cut - 1] == '?') ) cut--;
body = sentence.Substring( 0, cut ).TrimEnd();
end = cut < sentence.Length ? sentence.Substring( cut ) : ".";
}
static string LastWord( string body )
{
int space = body.LastIndexOf( ' ' );
var word = space >= 0 ? body.Substring( space + 1 ) : body;
return word.Trim( ',', ';', ':', '\'', '"', '(', ')' );
}
}