(965c31410a) Unstable v0.10.4.0
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
internal class NPCSet : IDisposable
|
||||
{
|
||||
private static List<NPCSet>? Sets { get; set; }
|
||||
|
||||
private string Identifier { get; }
|
||||
|
||||
private readonly List<HumanPrefab> Humans = new List<HumanPrefab>();
|
||||
|
||||
private bool Disposed { get; set; }
|
||||
|
||||
private NPCSet(XElement element, string filePath)
|
||||
{
|
||||
Identifier = element.GetAttributeString("identifier", string.Empty);
|
||||
|
||||
foreach (XElement npcElement in element.Elements())
|
||||
{
|
||||
Humans.Add(new HumanPrefab(npcElement, filePath));
|
||||
}
|
||||
}
|
||||
|
||||
public static HumanPrefab? Get(string identifier, string npcidentifier)
|
||||
{
|
||||
HumanPrefab prefab = Sets.Where(set => set.Identifier == identifier).SelectMany(npcSet => npcSet.Humans.Where(npcSetHuman => npcSetHuman.Identifier == npcidentifier)).FirstOrDefault();
|
||||
|
||||
if (prefab == null)
|
||||
{
|
||||
DebugConsole.ThrowError($"Could not find human prefab \"{npcidentifier}\" from \"{identifier}\".");
|
||||
return null;
|
||||
}
|
||||
return new HumanPrefab(prefab.Element, prefab.FilePath);
|
||||
}
|
||||
|
||||
public static void LoadSets()
|
||||
{
|
||||
Sets?.ForEach(set => set.Dispose());
|
||||
Sets = new List<NPCSet>();
|
||||
IEnumerable<ContentFile> files = GameMain.Instance.GetFilesOfType(ContentType.NPCSets);
|
||||
foreach (ContentFile file in files)
|
||||
{
|
||||
XDocument doc = XMLExtensions.TryLoadXml(file.Path);
|
||||
XElement? rootElement = doc?.Root;
|
||||
|
||||
if (doc == null || rootElement == null) { continue; }
|
||||
|
||||
if (doc.Root.IsOverride())
|
||||
{
|
||||
Sets.Clear();
|
||||
DebugConsole.NewMessage($"Overriding all NPC sets with '{file.Path}'", Color.Yellow);
|
||||
}
|
||||
|
||||
foreach (XElement element in rootElement.Elements())
|
||||
{
|
||||
bool isOverride = element.IsOverride();
|
||||
XElement sourceElement = isOverride ? element.FirstElement() : element;
|
||||
string elementName = sourceElement.Name.ToString().ToLowerInvariant();
|
||||
string identifier = sourceElement.GetAttributeString("identifier", null);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(identifier))
|
||||
{
|
||||
DebugConsole.ThrowError($"No identifier defined for the NPC set config '{elementName}' in file '{file.Path}'");
|
||||
continue;
|
||||
}
|
||||
|
||||
var existingParams = Sets.Find(set => set.Identifier == identifier);
|
||||
if (existingParams != null)
|
||||
{
|
||||
if (isOverride)
|
||||
{
|
||||
DebugConsole.NewMessage($"Overriding NPC set config '{identifier}' using the file '{file.Path}'", Color.Yellow);
|
||||
Sets.Remove(existingParams);
|
||||
}
|
||||
else
|
||||
{
|
||||
DebugConsole.ThrowError($"Duplicate NPC set config: '{identifier}' defined in {elementName} of '{file.Path}'");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
Sets.Add(new NPCSet(element, file.Path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (!Disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Humans.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
Disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
using Barotrauma.Extensions;
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class OutpostGenerationParams : ISerializableEntity
|
||||
{
|
||||
public static List<OutpostGenerationParams> Params { get; private set; }
|
||||
|
||||
public string Name { get; private set; }
|
||||
|
||||
public string Identifier { get; private set; }
|
||||
|
||||
private readonly List<string> allowedLocationTypes = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Identifiers of the location types this outpost can appear in. If empty, can appear in all types of locations.
|
||||
/// </summary>
|
||||
public IEnumerable<string> AllowedLocationTypes
|
||||
{
|
||||
get { return allowedLocationTypes; }
|
||||
}
|
||||
|
||||
[Serialize(10, isSaveable: true), Editable(MinValueInt = 1, MaxValueInt = 50)]
|
||||
public int TotalModuleCount
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
[Serialize(200.0f, isSaveable: true), Editable(MinValueFloat = 0.0f, MaxValueFloat = 1000.0f)]
|
||||
public float MinHallwayLength
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, int> moduleCounts = new Dictionary<string, int>();
|
||||
|
||||
public IEnumerable<KeyValuePair<string, int>> ModuleCounts
|
||||
{
|
||||
get { return moduleCounts; }
|
||||
}
|
||||
|
||||
private readonly List<List<HumanPrefab>> humanPrefabLists = new List<List<HumanPrefab>>();
|
||||
|
||||
public Dictionary<string, SerializableProperty> SerializableProperties { get; private set; }
|
||||
|
||||
private OutpostGenerationParams(XElement element, string filePath)
|
||||
{
|
||||
Identifier = element.GetAttributeString("identifier", "");
|
||||
Name = element.GetAttributeString("name", Identifier);
|
||||
allowedLocationTypes = element.GetAttributeStringArray("allowedlocationtypes", Array.Empty<string>()).ToList();
|
||||
SerializableProperties = SerializableProperty.DeserializeProperties(this, element);
|
||||
foreach (XElement subElement in element.Elements())
|
||||
{
|
||||
switch (subElement.Name.ToString().ToLowerInvariant())
|
||||
{
|
||||
case "modulecount":
|
||||
string moduleFlag = (subElement.GetAttributeString("flag", null) ?? subElement.GetAttributeString("moduletype", "")).ToLowerInvariant();
|
||||
moduleCounts[moduleFlag] = subElement.GetAttributeInt("count", 0);
|
||||
break;
|
||||
case "npcs":
|
||||
humanPrefabLists.Add(new List<HumanPrefab>());
|
||||
foreach (XElement npcElement in subElement.Elements())
|
||||
{
|
||||
string from = npcElement.GetAttributeString("from", string.Empty);
|
||||
|
||||
// ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
|
||||
if (!string.IsNullOrWhiteSpace(from))
|
||||
{
|
||||
HumanPrefab prefab = NPCSet.Get(from, npcElement.GetAttributeString("identifier", string.Empty));
|
||||
if (prefab != null)
|
||||
{
|
||||
humanPrefabLists.Last().Add(prefab);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
humanPrefabLists.Last().Add(new HumanPrefab(npcElement, filePath));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int GetModuleCount(string moduleFlag)
|
||||
{
|
||||
if (string.IsNullOrEmpty(moduleFlag) || moduleFlag == "none") { return int.MaxValue; }
|
||||
return moduleCounts.ContainsKey(moduleFlag) ? moduleCounts[moduleFlag] : 0;
|
||||
}
|
||||
|
||||
public void SetModuleCount(string moduleFlag, int count)
|
||||
{
|
||||
if (string.IsNullOrEmpty(moduleFlag) || moduleFlag == "none") { return; }
|
||||
if (count <= 0)
|
||||
{
|
||||
moduleCounts.Remove(moduleFlag);
|
||||
}
|
||||
else
|
||||
{
|
||||
moduleCounts[moduleFlag] = count;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetAllowedLocationTypes(IEnumerable<string> allowedLocationTypes)
|
||||
{
|
||||
this.allowedLocationTypes.Clear();
|
||||
foreach (string locationType in allowedLocationTypes)
|
||||
{
|
||||
if (locationType.Equals("any", StringComparison.OrdinalIgnoreCase)) { continue; }
|
||||
this.allowedLocationTypes.Add(locationType);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<HumanPrefab> GetHumanPrefabs(Rand.RandSync randSync)
|
||||
{
|
||||
if (humanPrefabLists == null || !humanPrefabLists.Any()) { return Enumerable.Empty<HumanPrefab>(); }
|
||||
return humanPrefabLists.GetRandom(randSync);
|
||||
}
|
||||
|
||||
public static void LoadPresets()
|
||||
{
|
||||
Params = new List<OutpostGenerationParams>();
|
||||
var files = GameMain.Instance.GetFilesOfType(ContentType.OutpostConfig);
|
||||
foreach (ContentFile file in files)
|
||||
{
|
||||
XDocument doc = XMLExtensions.TryLoadXml(file.Path);
|
||||
if (doc?.Root == null) { continue; }
|
||||
var mainElement = doc.Root;
|
||||
if (doc.Root.IsOverride())
|
||||
{
|
||||
Params.Clear();
|
||||
DebugConsole.NewMessage($"Overriding all outpost generation parameters with '{file.Path}'", Color.Yellow);
|
||||
}
|
||||
|
||||
foreach (XElement element in mainElement.Elements())
|
||||
{
|
||||
bool isOverride = element.IsOverride();
|
||||
XElement sourceElement = isOverride ? element.FirstElement() : element;
|
||||
string elementName = sourceElement.Name.ToString().ToLowerInvariant();
|
||||
string identifier = sourceElement.GetAttributeString("identifier", null);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(identifier))
|
||||
{
|
||||
DebugConsole.ThrowError($"No identifier defined for the outpost config '{elementName}' in file '{file.Path}'");
|
||||
continue;
|
||||
}
|
||||
var existingParams = Params.Find(p => p.Identifier == identifier);
|
||||
if (existingParams != null)
|
||||
{
|
||||
if (isOverride)
|
||||
{
|
||||
DebugConsole.NewMessage($"Overriding outpost config '{identifier}' using the file '{file.Path}'", Color.Yellow);
|
||||
Params.Remove(existingParams);
|
||||
}
|
||||
else
|
||||
{
|
||||
DebugConsole.ThrowError($"Duplicate outpost config: '{identifier}' defined in {elementName} of '{file.Path}'");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Params.Add(new OutpostGenerationParams(element, file.Path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,167 @@
|
||||
using Barotrauma.Items.Components;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class OutpostModuleInfo : ISerializableEntity
|
||||
{
|
||||
[Flags]
|
||||
public enum GapPosition
|
||||
{
|
||||
None = 0,
|
||||
Right = 1,
|
||||
Left = 2,
|
||||
Top = 4,
|
||||
Bottom = 8
|
||||
}
|
||||
|
||||
private readonly HashSet<string> moduleFlags = new HashSet<string>();
|
||||
public IEnumerable<string> ModuleFlags
|
||||
{
|
||||
get { return moduleFlags; }
|
||||
}
|
||||
|
||||
private readonly HashSet<string> allowAttachToModules = new HashSet<string>();
|
||||
public IEnumerable<string> AllowAttachToModules
|
||||
{
|
||||
get { return allowAttachToModules; }
|
||||
}
|
||||
|
||||
private readonly HashSet<string> allowedLocationTypes = new HashSet<string>();
|
||||
public IEnumerable<string> AllowedLocationTypes
|
||||
{
|
||||
get { return allowedLocationTypes; }
|
||||
}
|
||||
|
||||
[Serialize(100, isSaveable: true, description: "How many instances of this module can be used in one outpost."), Editable]
|
||||
public int MaxCount { get; set; }
|
||||
|
||||
[Serialize(10.0f, isSaveable: true, description: "How likely it is for the module to get picked when selecting from a set of modules during the outpost generation."), Editable]
|
||||
public float Commonness { get; set; }
|
||||
|
||||
[Serialize(GapPosition.None, isSaveable: true, description: "Which sides of the module have gaps on them (i.e. from which sides the module can be attached to other modules). Center = no gaps available.")]
|
||||
public GapPosition GapPositions { get; set; }
|
||||
|
||||
public string Name { get; private set; }
|
||||
|
||||
public Dictionary<string, SerializableProperty> SerializableProperties { get; private set; }
|
||||
|
||||
public OutpostModuleInfo(SubmarineInfo submarineInfo, XElement element)
|
||||
{
|
||||
Name = $"OutpostModuleInfo ({submarineInfo.Name})";
|
||||
SerializableProperties = SerializableProperty.DeserializeProperties(this, element);
|
||||
SetFlags(
|
||||
element.GetAttributeStringArray("flags", null, convertToLowerInvariant: true) ??
|
||||
element.GetAttributeStringArray("moduletypes", new string[0], convertToLowerInvariant: true));
|
||||
SetAllowAttachTo(element.GetAttributeStringArray("allowattachto", new string[0], convertToLowerInvariant: true));
|
||||
allowedLocationTypes = new HashSet<string>(element.GetAttributeStringArray("allowedlocationtypes", new string[0], convertToLowerInvariant: true));
|
||||
}
|
||||
|
||||
public OutpostModuleInfo(SubmarineInfo submarineInfo)
|
||||
{
|
||||
Name = $"OutpostModuleInfo ({submarineInfo.Name})";
|
||||
SerializableProperties = SerializableProperty.DeserializeProperties(this);
|
||||
}
|
||||
public OutpostModuleInfo(OutpostModuleInfo original)
|
||||
{
|
||||
Name = original.Name;
|
||||
moduleFlags = new HashSet<string>(original.moduleFlags);
|
||||
allowAttachToModules = new HashSet<string>(original.allowAttachToModules);
|
||||
allowedLocationTypes = new HashSet<string>(original.allowedLocationTypes);
|
||||
SerializableProperties = new Dictionary<string, SerializableProperty>();
|
||||
GapPositions = original.GapPositions;
|
||||
foreach (KeyValuePair<string, SerializableProperty> kvp in original.SerializableProperties)
|
||||
{
|
||||
SerializableProperties.Add(kvp.Key, kvp.Value);
|
||||
if (SerializableProperty.GetSupportedTypeName(kvp.Value.PropertyType) != null)
|
||||
{
|
||||
kvp.Value.TrySetValue(this, kvp.Value.GetValue(original));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetFlags(IEnumerable<string> newFlags)
|
||||
{
|
||||
moduleFlags.Clear();
|
||||
if (newFlags.Contains("hallwayhorizontal"))
|
||||
{
|
||||
moduleFlags.Add("hallwayhorizontal");
|
||||
return;
|
||||
}
|
||||
if (newFlags.Contains("hallwayvertical"))
|
||||
{
|
||||
moduleFlags.Add("hallwayvertical");
|
||||
return;
|
||||
}
|
||||
if (!newFlags.Any())
|
||||
{
|
||||
moduleFlags.Add("none");
|
||||
}
|
||||
foreach (string flag in newFlags)
|
||||
{
|
||||
if (flag == "none" && newFlags.Count() > 1) { continue; }
|
||||
moduleFlags.Add(flag.ToLowerInvariant());
|
||||
}
|
||||
}
|
||||
public void SetAllowAttachTo(IEnumerable<string> allowAttachTo)
|
||||
{
|
||||
allowAttachToModules.Clear();
|
||||
if (!allowAttachTo.Any())
|
||||
{
|
||||
allowAttachToModules.Add("any");
|
||||
}
|
||||
foreach (string flag in allowAttachTo)
|
||||
{
|
||||
if (flag == "any" && allowAttachTo.Count() > 1) { continue; }
|
||||
allowAttachToModules.Add(flag);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetAllowedLocationTypes(IEnumerable<string> allowedLocationTypes)
|
||||
{
|
||||
this.allowedLocationTypes.Clear();
|
||||
foreach (string locationType in allowedLocationTypes)
|
||||
{
|
||||
if (locationType.Equals("any", StringComparison.OrdinalIgnoreCase)) { continue; }
|
||||
this.allowedLocationTypes.Add(locationType);
|
||||
}
|
||||
}
|
||||
|
||||
public void DetermineGapPositions(Submarine sub)
|
||||
{
|
||||
GapPositions = GapPosition.None;
|
||||
foreach (Gap gap in Gap.GapList)
|
||||
{
|
||||
if (gap.Submarine != sub || gap.linkedTo.Count != 1) { continue; }
|
||||
if (gap.ConnectedDoor != null && !gap.ConnectedDoor.UseBetweenOutpostModules) { continue; }
|
||||
|
||||
//ignore gaps that are at a docking port
|
||||
bool portFound = false;
|
||||
foreach (DockingPort port in DockingPort.List)
|
||||
{
|
||||
if (Submarine.RectContains(gap.WorldRect, port.Item.WorldPosition))
|
||||
{
|
||||
portFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (portFound) { continue; }
|
||||
|
||||
GapPositions |= gap.IsHorizontal ?
|
||||
gap.linkedTo[0].WorldPosition.X < gap.WorldPosition.X ? GapPosition.Right : GapPosition.Left :
|
||||
gap.linkedTo[0].WorldPosition.Y < gap.WorldPosition.Y ? GapPosition.Top : GapPosition.Bottom;
|
||||
}
|
||||
}
|
||||
|
||||
public void Save(XElement element)
|
||||
{
|
||||
SerializableProperty.SerializeProperties(this, element);
|
||||
element.SetAttributeValue("flags", string.Join(",", ModuleFlags));
|
||||
element.SetAttributeValue("allowattachto", string.Join(",", AllowAttachToModules));
|
||||
element.SetAttributeValue("allowedlocationtypes", string.Join(",", AllowedLocationTypes));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user