(d9829ac) v0.9.4.0

This commit is contained in:
Regalis
2019-10-24 18:05:42 +02:00
parent 9aa12bcac2
commit b39922a074
319 changed files with 12516 additions and 6815 deletions
@@ -86,8 +86,6 @@ namespace Barotrauma
}
}
private static Dictionary<string, XDocument> cachedConfigs = new Dictionary<string, XDocument>();
private static ushort idCounter;
public string Name;
@@ -128,14 +126,24 @@ namespace Barotrauma
}
}
public string SpeciesName => SourceElement.GetAttributeString("name", string.Empty);
private string _speciesName;
public string SpeciesName
{
get
{
if (_speciesName == null)
{
_speciesName = CharacterConfigElement.GetAttributeString("speciesname", string.Empty).ToLowerInvariant();
}
return _speciesName;
}
set { _speciesName = value; }
}
/// <summary>
/// Note: Can be null.
/// </summary>
public Character Character;
public readonly string File;
public Job Job;
@@ -190,7 +198,7 @@ namespace Barotrauma
{
if (portraitBackground == null)
{
var portraitBackgroundElement = SourceElement.Element("portraitbackground");
var portraitBackgroundElement = CharacterConfigElement.Element("portraitbackground");
if (portraitBackgroundElement != null)
{
portraitBackground = new Sprite(portraitBackgroundElement.Element("sprite"));
@@ -229,7 +237,7 @@ namespace Barotrauma
}
}
public XElement SourceElement { get; set; }
public XElement CharacterConfigElement { get; set; }
public readonly string ragdollFileName = string.Empty;
@@ -329,7 +337,7 @@ namespace Barotrauma
if (ragdoll == null)
{
string speciesName = SpeciesName;
bool isHumanoid = SourceElement.GetAttributeBool("humanoid", false);
bool isHumanoid = CharacterConfigElement.GetAttributeBool("humanoid", speciesName.Equals(Character.HumanSpeciesName, StringComparison.OrdinalIgnoreCase));
ragdoll = isHumanoid
? HumanRagdollParams.GetRagdollParams(speciesName, ragdollFileName)
: RagdollParams.GetRagdollParams<FishRagdollParams>(speciesName, ragdollFileName) as RagdollParams;
@@ -342,16 +350,21 @@ namespace Barotrauma
public bool IsAttachmentsLoaded => HairIndex > -1 && BeardIndex > -1 && MoustacheIndex > -1 && FaceAttachmentIndex > -1;
// Used for creating the data
public CharacterInfo(string file, string name = "", JobPrefab jobPrefab = null, string ragdollFileName = null)
public CharacterInfo(string speciesName, string name = "", JobPrefab jobPrefab = null, string ragdollFileName = null)
{
if (speciesName.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
{
speciesName = Path.GetFileNameWithoutExtension(speciesName).ToLowerInvariant();
}
ID = idCounter;
idCounter++;
File = file;
_speciesName = speciesName;
SpriteTags = new List<string>();
XDocument doc = GetConfig(file);
SourceElement = doc.Root;
XDocument doc = Character.GetConfigFile(_speciesName);
if (doc == null) { return; }
CharacterConfigElement = doc.Root.IsOverride() ? doc.Root.FirstElement() : doc.Root;
head = new HeadInfo();
HasGenders = doc.Root.GetAttributeBool("genders", false);
HasGenders = CharacterConfigElement.GetAttributeBool("genders", false);
if (HasGenders)
{
Head.gender = GetRandomGender();
@@ -367,16 +380,16 @@ namespace Barotrauma
else
{
name = "";
if (doc.Root.Element("name") != null)
if (CharacterConfigElement.Element("name") != null)
{
string firstNamePath = doc.Root.Element("name").GetAttributeString("firstname", "");
string firstNamePath = CharacterConfigElement.Element("name").GetAttributeString("firstname", "");
if (firstNamePath != "")
{
firstNamePath = firstNamePath.Replace("[GENDER]", (Head.gender == Gender.Female) ? "female" : "male");
Name = ToolBox.GetRandomLine(firstNamePath);
}
string lastNamePath = doc.Root.Element("name").GetAttributeString("lastname", "");
string lastNamePath = CharacterConfigElement.Element("name").GetAttributeString("lastname", "");
if (lastNamePath != "")
{
lastNamePath = lastNamePath.Replace("[GENDER]", (Head.gender == Gender.Female) ? "female" : "male");
@@ -395,18 +408,30 @@ namespace Barotrauma
}
// Used for loading the data
public CharacterInfo(XElement element)
public CharacterInfo(XElement infoElement)
{
ID = idCounter;
idCounter++;
Name = element.GetAttributeString("name", "");
string genderStr = element.GetAttributeString("gender", "male").ToLowerInvariant();
File = element.GetAttributeString("file", "");
SourceElement = GetConfig(File).Root;
HasGenders = SourceElement.GetAttributeBool("genders", false);
Salary = element.GetAttributeInt("salary", 1000);
Enum.TryParse(element.GetAttributeString("race", "White"), true, out Race race);
Enum.TryParse(element.GetAttributeString("gender", "None"), true, out Gender gender);
Name = infoElement.GetAttributeString("name", "");
string genderStr = infoElement.GetAttributeString("gender", "male").ToLowerInvariant();
Salary = infoElement.GetAttributeInt("salary", 1000);
Enum.TryParse(infoElement.GetAttributeString("race", "White"), true, out Race race);
Enum.TryParse(infoElement.GetAttributeString("gender", "None"), true, out Gender gender);
_speciesName = infoElement.GetAttributeString("speciesname", null);
XDocument doc = null;
if (_speciesName != null)
{
doc = Character.GetConfigFile(_speciesName);
}
else
{
// Backwards support (human only)
string file = infoElement.GetAttributeString("file", "");
doc = XMLExtensions.TryLoadXml(file);
}
if (doc == null) { return; }
CharacterConfigElement = doc.Root.IsOverride() ? doc.Root.FirstElement() : doc.Root;
HasGenders = CharacterConfigElement.GetAttributeBool("genders", false);
if (HasGenders && gender == Gender.None)
{
gender = GetRandomGender();
@@ -416,26 +441,26 @@ namespace Barotrauma
gender = Gender.None;
}
RecreateHead(
element.GetAttributeInt("headspriteid", 1),
infoElement.GetAttributeInt("headspriteid", 1),
race,
gender,
element.GetAttributeInt("hairindex", -1),
element.GetAttributeInt("beardindex", -1),
element.GetAttributeInt("moustacheindex", -1),
element.GetAttributeInt("faceattachmentindex", -1));
infoElement.GetAttributeInt("hairindex", -1),
infoElement.GetAttributeInt("beardindex", -1),
infoElement.GetAttributeInt("moustacheindex", -1),
infoElement.GetAttributeInt("faceattachmentindex", -1));
if (string.IsNullOrEmpty(Name))
{
if (SourceElement.Element("name") != null)
if (CharacterConfigElement.Element("name") != null)
{
string firstNamePath = SourceElement.Element("name").GetAttributeString("firstname", "");
string firstNamePath = CharacterConfigElement.Element("name").GetAttributeString("firstname", "");
if (firstNamePath != "")
{
firstNamePath = firstNamePath.Replace("[GENDER]", (Head.gender == Gender.Female) ? "female" : "male");
Name = ToolBox.GetRandomLine(firstNamePath);
}
string lastNamePath = SourceElement.Element("name").GetAttributeString("lastname", "");
string lastNamePath = CharacterConfigElement.Element("name").GetAttributeString("lastname", "");
if (lastNamePath != "")
{
lastNamePath = lastNamePath.Replace("[GENDER]", (Head.gender == Gender.Female) ? "female" : "male");
@@ -445,15 +470,14 @@ namespace Barotrauma
}
}
StartItemsGiven = element.GetAttributeBool("startitemsgiven", false);
string personalityName = element.GetAttributeString("personality", "");
ragdollFileName = element.GetAttributeString("ragdoll", string.Empty);
StartItemsGiven = infoElement.GetAttributeBool("startitemsgiven", false);
string personalityName = infoElement.GetAttributeString("personality", "");
ragdollFileName = infoElement.GetAttributeString("ragdoll", string.Empty);
if (!string.IsNullOrEmpty(personalityName))
{
personalityTrait = NPCPersonalityTrait.List.Find(p => p.Name == personalityName);
}
foreach (XElement subElement in element.Elements())
foreach (XElement subElement in infoElement.Elements())
{
if (subElement.Name.ToString().ToLowerInvariant() != "job") continue;
Job = new Job(subElement);
@@ -462,20 +486,9 @@ namespace Barotrauma
LoadHeadAttachments();
}
private XDocument GetConfig(string file)
{
if (!cachedConfigs.TryGetValue(file, out XDocument doc))
{
doc = XMLExtensions.TryLoadXml(file);
if (doc == null) { return null; }
cachedConfigs.Add(file, doc);
}
return doc;
}
public int SetRandomHead() => HeadSpriteId = GetRandomHeadID();
public Gender GetRandomGender() => (Rand.Range(0.0f, 1.0f, Rand.RandSync.Server) < SourceElement.GetAttributeFloat("femaleratio", 0.5f)) ? Gender.Female : Gender.Male;
public Gender GetRandomGender() => (Rand.Range(0.0f, 1.0f, Rand.RandSync.Server) < CharacterConfigElement.GetAttributeFloat("femaleratio", 0.5f)) ? Gender.Female : Gender.Male;
public Race GetRandomRace() => new Race[] { Race.White, Race.Black, Race.Asian }.GetRandom(Rand.RandSync.Server);
public int GetRandomHeadID() => Head.headSpriteRange != Vector2.Zero ? Rand.Range((int)Head.headSpriteRange.X, (int)Head.headSpriteRange.Y + 1, Rand.RandSync.Server) : 0;
@@ -491,7 +504,7 @@ namespace Barotrauma
{
if (wearables == null)
{
var attachments = SourceElement.Element("HeadAttachments");
var attachments = CharacterConfigElement.Element("HeadAttachments");
if (attachments != null)
{
wearables = attachments.Elements("Wearable");
@@ -503,6 +516,7 @@ namespace Barotrauma
public IEnumerable<XElement> FilterByTypeAndHeadID(IEnumerable<XElement> elements, WearableType targetType)
{
if (elements == null) { return elements; }
return elements.Where(e =>
{
if (Enum.TryParse(e.GetAttributeString("type", ""), true, out WearableType type) && type != targetType) { return false; }
@@ -522,8 +536,8 @@ namespace Barotrauma
private void CalculateHeadSpriteRange()
{
if (SourceElement == null) { return; }
Head.headSpriteRange = SourceElement.GetAttributeVector2("headidrange", Vector2.Zero);
if (CharacterConfigElement == null) { return; }
Head.headSpriteRange = CharacterConfigElement.GetAttributeVector2("headidrange", Vector2.Zero);
// If range is defined, we use it as it is
// Else we calculate the range from the wearables.
if (Head.headSpriteRange == Vector2.Zero)
@@ -582,11 +596,13 @@ namespace Barotrauma
public void LoadHeadSprite()
{
// TODO: use ragdollparams instead?
foreach (XElement limbElement in Ragdoll.MainElement.Elements())
{
if (limbElement.GetAttributeString("type", "").ToLowerInvariant() != "head") continue;
if (limbElement.GetAttributeString("type", "").ToLowerInvariant() != "head") { continue; }
XElement spriteElement = limbElement.Element("sprite");
if (spriteElement == null) { continue; }
string spritePath = spriteElement.Attribute("texture").Value;
@@ -605,7 +621,7 @@ namespace Barotrauma
}
string fileWithoutTags = Path.GetFileNameWithoutExtension(file);
fileWithoutTags = fileWithoutTags.Split('[', ']').First();
if (fileWithoutTags != fileName) continue;
if (fileWithoutTags != fileName) { continue; }
HeadSprite = new Sprite(spriteElement, "", file);
Portrait = new Sprite(spriteElement, "", file) { RelativeOrigin = Vector2.Zero };
@@ -788,7 +804,7 @@ namespace Barotrauma
charElement.Add(
new XAttribute("name", Name),
new XAttribute("file", File),
new XAttribute("speciesname", SpeciesName),
new XAttribute("gender", Head.gender == Gender.Male ? "male" : "female"),
new XAttribute("race", Head.race.ToString()),
new XAttribute("salary", Salary),