Client LuaCsForBarotrauma
OutpostModuleInfo.cs
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Xml.Linq;
7 
8 namespace Barotrauma
9 {
11  {
12  [Flags]
13  public enum GapPosition
14  {
15  None = 0,
16  Right = 1,
17  Left = 2,
18  Top = 4,
19  Bottom = 8
20  }
21 
22  private readonly HashSet<Identifier> moduleFlags = new HashSet<Identifier>();
23  public IEnumerable<Identifier> ModuleFlags
24  {
25  get { return moduleFlags; }
26  }
27 
28  private readonly HashSet<Identifier> allowAttachToModules = new HashSet<Identifier>();
29  public IEnumerable<Identifier> AllowAttachToModules
30  {
31  get { return allowAttachToModules; }
32  }
33 
34  private readonly HashSet<Identifier> allowedLocationTypes = new HashSet<Identifier>();
35  public IEnumerable<Identifier> AllowedLocationTypes
36  {
37  get { return allowedLocationTypes; }
38  }
39 
40  [Serialize(100, IsPropertySaveable.Yes, description: "How many instances of this module can be used in one outpost."), Editable]
41  public int MaxCount { get; set; }
42 
43  [Serialize(10.0f, IsPropertySaveable.Yes, description: "How likely it is for the module to get picked when selecting from a set of modules during the outpost generation."), Editable]
44  public float Commonness { get; set; }
45 
46  [Serialize(GapPosition.None, IsPropertySaveable.Yes, 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.")]
47  public GapPosition GapPositions { get; set; }
48 
49  [Serialize(GapPosition.Right | GapPosition.Left | GapPosition.Bottom | GapPosition.Top, IsPropertySaveable.Yes, description: "Which sides of this module are allowed to attach to the previously placed module. E.g. if you want a module to always attach to the left side of the docking module, you could set this to Right.")]
50  public GapPosition CanAttachToPrevious { get; set; }
51 
52  public string Name { get; private set; }
53 
54  public Dictionary<Identifier, SerializableProperty> SerializableProperties { get; private set; }
55 
56  public OutpostModuleInfo(SubmarineInfo submarineInfo, XElement element)
57  {
58  Name = $"OutpostModuleInfo ({submarineInfo.Name})";
60  SetFlags(
61  element.GetAttributeIdentifierArray("flags", null) ??
62  element.GetAttributeIdentifierArray("moduletypes", Array.Empty<Identifier>()));
63  SetAllowAttachTo(element.GetAttributeIdentifierArray("allowattachto", Array.Empty<Identifier>()));
64  allowedLocationTypes = new HashSet<Identifier>(element.GetAttributeIdentifierArray("allowedlocationtypes", Array.Empty<Identifier>()));
65  }
66 
67  public OutpostModuleInfo(SubmarineInfo submarineInfo)
68  {
69  Name = $"OutpostModuleInfo ({submarineInfo.Name})";
71  }
73  {
74  Name = original.Name;
75  moduleFlags = new HashSet<Identifier>(original.moduleFlags);
76  allowAttachToModules = new HashSet<Identifier>(original.allowAttachToModules);
77  allowedLocationTypes = new HashSet<Identifier>(original.allowedLocationTypes);
78  SerializableProperties = new Dictionary<Identifier, SerializableProperty>();
79  GapPositions = original.GapPositions;
80  foreach (KeyValuePair<Identifier, SerializableProperty> kvp in original.SerializableProperties)
81  {
82  SerializableProperties.Add(kvp.Key, kvp.Value);
83  if (SerializableProperty.GetSupportedTypeName(kvp.Value.PropertyType) != null)
84  {
85  kvp.Value.TrySetValue(this, kvp.Value.GetValue(original));
86  }
87  }
88  }
89 
90  public void SetFlags(IEnumerable<Identifier> newFlags)
91  {
92  moduleFlags.Clear();
93  if (newFlags.Contains("hallwayhorizontal".ToIdentifier()))
94  {
95  moduleFlags.Add("hallwayhorizontal".ToIdentifier());
96  if (newFlags.Contains("ruin".ToIdentifier())) { moduleFlags.Add("ruin".ToIdentifier()); }
97  }
98  if (newFlags.Contains("hallwayvertical".ToIdentifier()))
99  {
100  moduleFlags.Add("hallwayvertical".ToIdentifier());
101  if (newFlags.Contains("ruin".ToIdentifier())) { moduleFlags.Add("ruin".ToIdentifier()); }
102  }
103  if (!newFlags.Any())
104  {
105  moduleFlags.Add("none".ToIdentifier());
106  }
107  foreach (Identifier flag in newFlags)
108  {
109  if (flag == "none" && newFlags.Count() > 1) { continue; }
110  moduleFlags.Add(flag);
111  }
112  }
113  public void SetAllowAttachTo(IEnumerable<Identifier> allowAttachTo)
114  {
115  allowAttachToModules.Clear();
116  if (!allowAttachTo.Any())
117  {
118  allowAttachToModules.Add("any".ToIdentifier());
119  }
120  foreach (Identifier flag in allowAttachTo)
121  {
122  if (flag == "any" && allowAttachTo.Count() > 1) { continue; }
123  allowAttachToModules.Add(flag);
124  }
125  }
126 
127  public void SetAllowedLocationTypes(IEnumerable<Identifier> allowedLocationTypes)
128  {
129  this.allowedLocationTypes.Clear();
130  foreach (Identifier locationType in allowedLocationTypes)
131  {
132  if (locationType == "any") { continue; }
133  this.allowedLocationTypes.Add(locationType);
134  }
135  }
137  {
138  return allowedLocationTypes.None() || allowedLocationTypes.Contains("Any".ToIdentifier());
139  }
140 
141  public bool IsAllowedInLocationType(LocationType locationType)
142  {
143  if (locationType == null || IsAllowedInAnyLocationType()) { return true; }
144  return allowedLocationTypes.Contains(locationType.Identifier);
145  }
146 
148  {
149  GapPositions = GapPosition.None;
150  foreach (Gap gap in Gap.GapList)
151  {
152  if (gap.Submarine != sub || gap.linkedTo.Count != 1) { continue; }
153  if (gap.ConnectedDoor != null && !gap.ConnectedDoor.UseBetweenOutpostModules) { continue; }
154 
155  //ignore gaps that are at a docking port
156  bool portFound = false;
157  foreach (DockingPort port in DockingPort.List)
158  {
160  {
161  portFound = true;
162  break;
163  }
164  }
165  if (portFound) { continue; }
166 
167  GapPositions |= gap.IsHorizontal ?
168  gap.linkedTo[0].WorldPosition.X < gap.WorldPosition.X ? GapPosition.Right : GapPosition.Left :
169  gap.linkedTo[0].WorldPosition.Y < gap.WorldPosition.Y ? GapPosition.Top : GapPosition.Bottom;
170  }
171  }
172 
173  public void Save(XElement element)
174  {
176  element.SetAttributeValue("flags", string.Join(",", ModuleFlags));
177  element.SetAttributeValue("allowattachto", string.Join(",", AllowAttachToModules));
178  element.SetAttributeValue("allowedlocationtypes", string.Join(",", AllowedLocationTypes));
179  }
180  }
181 }
virtual Vector2 WorldPosition
Definition: Entity.cs:49
Submarine Submarine
Definition: Entity.cs:53
OutpostModuleInfo(SubmarineInfo submarineInfo)
void DetermineGapPositions(Submarine sub)
void SetFlags(IEnumerable< Identifier > newFlags)
IEnumerable< Identifier > AllowedLocationTypes
IEnumerable< Identifier > AllowAttachToModules
IEnumerable< Identifier > ModuleFlags
void SetAllowAttachTo(IEnumerable< Identifier > allowAttachTo)
OutpostModuleInfo(OutpostModuleInfo original)
bool IsAllowedInLocationType(LocationType locationType)
void SetAllowedLocationTypes(IEnumerable< Identifier > allowedLocationTypes)
OutpostModuleInfo(SubmarineInfo submarineInfo, XElement element)
void Save(XElement element)
Dictionary< Identifier, SerializableProperty > SerializableProperties
readonly Identifier Identifier
Definition: Prefab.cs:34
static Dictionary< Identifier, SerializableProperty > DeserializeProperties(object obj, XElement element=null)
static string GetSupportedTypeName(Type type)
static void SerializeProperties(ISerializableEntity obj, XElement element, bool saveIfDefault=false, bool ignoreEditable=false)
static bool RectContains(Rectangle rect, Vector2 pos, bool inclusive=false)