Client LuaCsForBarotrauma
CaveGenerationParams.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using System.Globalization;
5 using System.Linq;
6 using System.Xml.Linq;
7 
8 namespace Barotrauma
9 {
11  {
13 
14  public string Name => Identifier.Value;
15 
16  private int minWidth, maxWidth;
17  private int minHeight, maxHeight;
18 
19  private int minBranchCount, maxBranchCount;
20 
21  public Dictionary<Identifier, SerializableProperty> SerializableProperties
22  {
23  get;
24  set;
25  }
26 
31  public readonly Dictionary<Identifier, float> OverrideCommonness = new Dictionary<Identifier, float>();
32 
34  public float Commonness
35  {
36  get;
37  private set;
38  }
39 
40  [Serialize(8000, IsPropertySaveable.Yes), Editable(MinValueInt = 1000, MaxValueInt = 100000)]
41  public int MinWidth
42  {
43  get { return minWidth; }
44  set { minWidth = Math.Max(value, 1000); }
45  }
46 
47  [Serialize(10000, IsPropertySaveable.Yes), Editable(MinValueInt = 1000, MaxValueInt = 1000000)]
48  public int MaxWidth
49  {
50  get { return maxWidth; }
51  set { maxWidth = Math.Max(value, minWidth); }
52  }
53 
54  [Serialize(8000, IsPropertySaveable.Yes), Editable(MinValueInt = 1000, MaxValueInt = 100000)]
55  public int MinHeight
56  {
57  get { return minHeight; }
58  set { minHeight = Math.Max(value, 1000); }
59  }
60 
61  [Serialize(10000, IsPropertySaveable.Yes), Editable(MinValueInt = 1000, MaxValueInt = 1000000)]
62  public int MaxHeight
63  {
64  get { return maxHeight; }
65  set { maxHeight = Math.Max(value, minHeight); }
66  }
67 
68  [Serialize(2, IsPropertySaveable.Yes, description: "Minimum number of tunnel branches in the cave."), Editable(MinValueInt = 0, MaxValueInt = 10)]
69  public int MinBranchCount
70  {
71  get { return minBranchCount; }
72  set { minBranchCount = Math.Max(value, 0); }
73  }
74 
75  [Serialize(4, IsPropertySaveable.Yes, description: "Maximum number of tunnel branches in the cave."), Editable(MinValueInt = 0, MaxValueInt = 10)]
76  public int MaxBranchCount
77  {
78  get { return maxBranchCount; }
79  set { maxBranchCount = Math.Max(value, minBranchCount); }
80  }
81 
82  [Serialize(50, IsPropertySaveable.Yes, description: "Total amount of level objects in the cave."), Editable(MinValueInt = 0, MaxValueInt = 10000)]
83  public int LevelObjectAmount
84  {
85  get;
86  set;
87  }
88 
89  [Serialize(0.1f, IsPropertySaveable.Yes, description: "What portion of the empty cells in the cave should be turned into destructible walls? For example, 0.1 = 10%."), Editable(MinValueFloat = 0, MaxValueFloat = 1.0f, DecimalCount = 2 )]
90  public float DestructibleWallRatio
91  {
92  get;
93  set;
94  }
95 
96  public readonly Sprite WallSprite;
97  public readonly Sprite WallEdgeSprite;
98 
99  public static CaveGenerationParams GetRandom(Level level, bool abyss, Rand.RandSync rand)
100  {
101  var caveParams = CaveParams.OrderBy(p => p.UintIdentifier).ToList();
102  if (caveParams.All(p => p.GetCommonness(level.LevelData, abyss) <= 0.0f))
103  {
104  return caveParams.First();
105  }
106  return ToolBox.SelectWeightedRandom(caveParams.ToList(), caveParams.Select(p => p.GetCommonness(level.LevelData, abyss)).ToList(), rand);
107  }
108 
109  public float GetCommonness(LevelData levelData, bool abyss)
110  {
111  if (levelData.GenerationParams != null && levelData.GenerationParams.Identifier != Identifier.Empty &&
112  OverrideCommonness.TryGetValue(abyss ? "abyss".ToIdentifier() : levelData.GenerationParams.Identifier, out float commonness))
113  {
114  return commonness;
115  }
116  if (levelData?.Biome != null)
117  {
118  if (OverrideCommonness.TryGetValue(levelData.Biome.Identifier, out float biomeCommonness))
119  {
120  return biomeCommonness;
121  }
122  }
123 
124  return Commonness;
125  }
126 
127  public CaveGenerationParams(ContentXElement element, CaveGenerationParametersFile file) : base(file, element.GetAttributeIdentifier("identifier", ""))
128  {
130 
131  foreach (var subElement in element.Elements())
132  {
133  switch (subElement.Name.ToString().ToLowerInvariant())
134  {
135  case "wall":
136  WallSprite = new Sprite(subElement);
137  break;
138  case "walledge":
139  WallEdgeSprite = new Sprite(subElement);
140  break;
141  case "overridecommonness":
142  Identifier levelType = subElement.GetAttributeIdentifier("leveltype", "");
143  if (!OverrideCommonness.ContainsKey(levelType))
144  {
145  OverrideCommonness.Add(levelType, subElement.GetAttributeFloat("commonness", 1.0f));
146  }
147  break;
148  }
149  }
150  }
151 
152  public void Save(XElement element)
153  {
154  SerializableProperty.SerializeProperties(this, element, true);
155  foreach (KeyValuePair<Identifier, float> overrideCommonness in OverrideCommonness)
156  {
157  bool elementFound = false;
158  foreach (var subElement in element.Elements())
159  {
160  if (subElement.NameAsIdentifier() == "overridecommonness"
161  && subElement.GetAttributeIdentifier("leveltype", "") == overrideCommonness.Key)
162  {
163  subElement.Attribute("commonness").Value = overrideCommonness.Value.ToString("G", CultureInfo.InvariantCulture);
164  elementFound = true;
165  break;
166  }
167  }
168  if (!elementFound)
169  {
170  element.Add(new XElement("overridecommonness",
171  new XAttribute("leveltype", overrideCommonness.Key),
172  new XAttribute("commonness", overrideCommonness.Value.ToString("G", CultureInfo.InvariantCulture))));
173  }
174  }
175  }
176 
177  public override void Dispose()
178  {
180  }
181  }
182 }
static CaveGenerationParams GetRandom(Level level, bool abyss, Rand.RandSync rand)
float GetCommonness(LevelData levelData, bool abyss)
readonly Dictionary< Identifier, float > OverrideCommonness
Overrides the commonness of the object in a specific level type. Key = name of the level type,...
Dictionary< Identifier, SerializableProperty > SerializableProperties
static readonly PrefabCollection< CaveGenerationParams > CaveParams
CaveGenerationParams(ContentXElement element, CaveGenerationParametersFile file)
LevelGenerationParams GenerationParams
Definition: LevelData.cs:28
readonly Biome Biome
Definition: LevelData.cs:26
readonly Identifier Identifier
Definition: Prefab.cs:34
Prefab that has a property serves as a deterministic hash of a prefab's identifier....
static Dictionary< Identifier, SerializableProperty > DeserializeProperties(object obj, XElement element=null)
static void SerializeProperties(ISerializableEntity obj, XElement element, bool saveIfDefault=false, bool ignoreEditable=false)