Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/SharedSource/Characters/NPCPersonalityTrait.cs
T
Markus Isberg 15d18e6ff6 Build 0.17.15.0
2022-04-27 23:32:17 +09:00

60 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace Barotrauma
{
class NPCPersonalityTrait
{
public readonly Identifier Name;
public readonly List<string> AllowedDialogTags;
private readonly float commonness;
public float Commonness
{
get { return commonness; }
}
public static IEnumerable<NPCPersonalityTrait> GetAll(LanguageIdentifier language)
{
if (language != TextManager.DefaultLanguage && !NPCConversationCollection.Collections.ContainsKey(language))
{
DebugConsole.AddWarning($"Could not find NPC personality traits for the language \"{language}\". Using \"{TextManager.DefaultLanguage}\" instead..");
language = TextManager.DefaultLanguage;
}
return NPCConversationCollection.Collections[language]
.SelectMany(cc => cc.PersonalityTraits.Values);
}
public static NPCPersonalityTrait Get(LanguageIdentifier language, Identifier traitName)
{
if (language != TextManager.DefaultLanguage && !NPCConversationCollection.Collections.ContainsKey(language))
{
DebugConsole.AddWarning($"Could not find NPC personality traits for the language \"{language}\". Using \"{TextManager.DefaultLanguage}\" instead..");
language = TextManager.DefaultLanguage;
}
return NPCConversationCollection.Collections[language]
.FirstOrDefault(cc => cc.PersonalityTraits.ContainsKey(traitName))
.PersonalityTraits[traitName];
}
public NPCPersonalityTrait(XElement element)
{
Name = element.GetAttributeIdentifier("name", "");
AllowedDialogTags = new List<string>(element.GetAttributeStringArray("alloweddialogtags", Array.Empty<string>()));
commonness = element.GetAttributeFloat("commonness", 1.0f);
}
public static NPCPersonalityTrait GetRandom(string seed)
{
#warning TODO: implement NPCPersonality content type and revise this for determinism
var rand = new MTRandom(ToolBox.StringToInt(seed));
var list = GetAll(GameSettings.CurrentConfig.Language);
return ToolBox.SelectWeightedRandom(list, t => t.commonness, rand);
}
}
}