Client LuaCsForBarotrauma
CampaignSettings.cs
1 #nullable enable
2 
3 using Microsoft.Xna.Framework;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Xml.Linq;
7 
8 namespace Barotrauma
9 {
10  internal class CampaignSettings : INetSerializableStruct, ISerializableEntity
11  {
12  public static CampaignSettings Empty => new CampaignSettings(element: null);
13 
14 #if CLIENT
15  public static CampaignSettings CurrentSettings = new CampaignSettings(GameSettings.CurrentConfig.SavedCampaignSettings);
16 #endif
17  public string Name => "CampaignSettings";
18 
19  public const string LowerCaseSaveElementName = "campaignsettings";
20 
21  [Serialize("Normal", IsPropertySaveable.Yes)]
22  public string PresetName { get; set; } = string.Empty;
23 
24  [Serialize(true, IsPropertySaveable.Yes)]
25  public bool TutorialEnabled { get; set; }
26 
27  [Serialize(false, IsPropertySaveable.Yes), NetworkSerialize]
28  public bool RadiationEnabled { get; set; }
29 
30  public const int DefaultMaxMissionCount = 2;
31  public const int MaxMissionCountLimit = 10;
32  public const int MinMissionCountLimit = 1;
33 
34  private int maxMissionCount;
35 
36  [Serialize(DefaultMaxMissionCount, IsPropertySaveable.Yes), NetworkSerialize(MinValueInt = MinMissionCountLimit, MaxValueInt = MaxMissionCountLimit)]
37  public int MaxMissionCount
38  {
39  get => maxMissionCount;
40  set => maxMissionCount = MathHelper.Clamp(value, MinMissionCountLimit, MaxMissionCountLimit);
41  }
42 
43  public int TotalMaxMissionCount => MaxMissionCount + GetAddedMissionCount();
44 
45  [Serialize(WorldHostilityOption.Medium, IsPropertySaveable.Yes), NetworkSerialize]
46  public WorldHostilityOption WorldHostility { get; set; }
47 
48  [Serialize("normal", IsPropertySaveable.Yes), NetworkSerialize]
49  public Identifier StartItemSet { get; set; }
50 
51  [Serialize(StartingBalanceAmountOption.Medium, IsPropertySaveable.Yes), NetworkSerialize]
52  public StartingBalanceAmountOption StartingBalanceAmount { get; set; }
53 
54  private int? _initialMoney;
55  public const int DefaultInitialMoney = 8000;
56 
57  public int InitialMoney
58  {
59  get
60  {
61  if (_initialMoney is int alreadyCachedValue)
62  {
63  return alreadyCachedValue;
64  }
65  else
66  {
67  _initialMoney = DefaultInitialMoney;
68  Identifier settingDefinitionIdentifier = nameof(StartingBalanceAmount).ToIdentifier();
69  Identifier attributeIdentifier = StartingBalanceAmount.ToIdentifier();
70  if (CampaignModePresets.TryGetAttribute(settingDefinitionIdentifier, attributeIdentifier, out XAttribute? attribute))
71  {
72  _initialMoney = attribute.GetAttributeInt(DefaultInitialMoney);
73  }
74  else
75  {
76  DebugConsole.ThrowError($"CampaignSettings: Can't find value for {attributeIdentifier} in {settingDefinitionIdentifier}");
77  }
78  return _initialMoney ?? DefaultInitialMoney;
79  }
80  }
81  }
82 
83  private float? _extraEventManagerDifficulty;
84  private const float defaultExtraEventManagerDifficulty = 0;
85 
86  public float ExtraEventManagerDifficulty
87  {
88  get
89  {
90  if (_extraEventManagerDifficulty is float alreadyCachedValue)
91  {
92  return alreadyCachedValue;
93  }
94  else
95  {
96  _extraEventManagerDifficulty = defaultExtraEventManagerDifficulty;
97  Identifier settingDefinitionIdentifier = nameof(ExtraEventManagerDifficulty).ToIdentifier();
98  Identifier attributeIdentifier = WorldHostility.ToIdentifier();
99  if (CampaignModePresets.TryGetAttribute(settingDefinitionIdentifier, attributeIdentifier, out XAttribute? attribute))
100  {
101  _extraEventManagerDifficulty = attribute.GetAttributeFloat(defaultExtraEventManagerDifficulty);
102  }
103  else
104  {
105  DebugConsole.ThrowError($"CampaignSettings: Can't find value for {attributeIdentifier} in {settingDefinitionIdentifier}");
106  }
107  return _extraEventManagerDifficulty ?? defaultExtraEventManagerDifficulty;
108  }
109  }
110  }
111 
112  private float? _levelDifficultyMultiplier;
113  private const float defaultLevelDifficultyMultiplier = 1.0f;
114 
115  public float LevelDifficultyMultiplier
116  {
117  get
118  {
119  if (_levelDifficultyMultiplier is float alreadyCachedValue)
120  {
121  return alreadyCachedValue;
122  }
123  else
124  {
125  _levelDifficultyMultiplier = defaultLevelDifficultyMultiplier;
126  Identifier settingDefinitionIdentifier = nameof(LevelDifficultyMultiplier).ToIdentifier();
127  Identifier attributeIdentifier = WorldHostility.ToIdentifier();
128  if (CampaignModePresets.TryGetAttribute(settingDefinitionIdentifier, attributeIdentifier, out XAttribute? attribute))
129  {
130  _levelDifficultyMultiplier = attribute.GetAttributeFloat(defaultLevelDifficultyMultiplier);
131  }
132  else
133  {
134  DebugConsole.ThrowError($"CampaignSettings: Can't find value for {attributeIdentifier} in {settingDefinitionIdentifier}");
135  }
136  return _levelDifficultyMultiplier ?? defaultLevelDifficultyMultiplier;
137  }
138  }
139  }
140 
141  private static readonly Dictionary<string, MultiplierSettings> _multiplierSettings = new Dictionary<string, MultiplierSettings>
142  {
143  { "default", new MultiplierSettings { Min = 0.2f, Max = 2.0f, Step = 0.1f } },
144  { nameof(CrewVitalityMultiplier), new MultiplierSettings { Min = 0.5f, Max = 2.0f, Step = 0.1f } },
145  { nameof(NonCrewVitalityMultiplier),new MultiplierSettings { Min = 0.5f, Max = 3.0f, Step = 0.1f } },
146  { nameof(MissionRewardMultiplier), new MultiplierSettings { Min = 0.5f, Max = 2.0f, Step = 0.1f } },
147  { nameof(RepairFailMultiplier), new MultiplierSettings { Min = 0.5f, Max = 5.0f, Step = 0.5f } },
148  { nameof(ShopPriceMultiplier), new MultiplierSettings { Min = 0.1f, Max = 3.0f, Step = 0.1f } },
149  { nameof(ShipyardPriceMultiplier), new MultiplierSettings { Min = 0.1f, Max = 3.0f, Step = 0.1f } }
150  // Add overrides for default values here
151  };
152 
153  [Serialize(1.0f, IsPropertySaveable.Yes), NetworkSerialize]
154  public float CrewVitalityMultiplier { get; set; }
155 
156  [Serialize(1.0f, IsPropertySaveable.Yes), NetworkSerialize]
157  public float NonCrewVitalityMultiplier { get; set; }
158 
159  [Serialize(1.0f, IsPropertySaveable.Yes), NetworkSerialize]
160  public float OxygenMultiplier { get; set; }
161 
162  [Serialize(1.0f, IsPropertySaveable.Yes), NetworkSerialize]
163  public float FuelMultiplier { get; set; }
164 
165  [Serialize(1.0f, IsPropertySaveable.Yes), NetworkSerialize]
166  public float MissionRewardMultiplier { get; set; }
167 
168  [Serialize(1.0f, IsPropertySaveable.Yes), NetworkSerialize]
169  public float ShopPriceMultiplier { get; set; }
170 
171  [Serialize(1.0f, IsPropertySaveable.Yes), NetworkSerialize]
172  public float ShipyardPriceMultiplier { get; set; }
173 
174  [Serialize(1.0f, IsPropertySaveable.Yes), NetworkSerialize]
175  public float RepairFailMultiplier { get; set; }
176 
177  [Serialize(true, IsPropertySaveable.Yes), NetworkSerialize]
178  public bool ShowHuskWarning { get; set; }
179 
180  [Serialize(PatdownProbabilityOption.Medium, IsPropertySaveable.Yes), NetworkSerialize]
181  public PatdownProbabilityOption PatdownProbability { get; set; }
182 
183  private float? _minPatdownProbability;
184  private float? _maxPatdownProbability;
185  public const float DefaultMinPatdownProbability = 0.2f;
186  public const float DefaultMaxPatdownProbability = 0.9f;
187 
188  public float PatdownProbabilityMin
189  {
190  get
191  {
192  if (_minPatdownProbability is float alreadyCachedValue)
193  {
194  return alreadyCachedValue;
195  }
196  else
197  {
198  _minPatdownProbability = DefaultMinPatdownProbability;
199  Identifier settingDefinitionIdentifier = nameof(PatdownProbabilityMin).ToIdentifier();
200  Identifier attributeIdentifier = PatdownProbability.ToIdentifier();
201  if (CampaignModePresets.TryGetAttribute(settingDefinitionIdentifier, attributeIdentifier, out XAttribute? attribute))
202  {
203  _minPatdownProbability = attribute.GetAttributeFloat(DefaultMinPatdownProbability);
204  }
205  else
206  {
207  DebugConsole.ThrowError($"CampaignSettings: Can't find value for {attributeIdentifier} in {settingDefinitionIdentifier}");
208  }
209  return _minPatdownProbability ?? DefaultMinPatdownProbability;
210  }
211  }
212  }
213 
214  public float PatdownProbabilityMax
215  {
216  get
217  {
218  if (_maxPatdownProbability is float alreadyCachedValue)
219  {
220  return alreadyCachedValue;
221  }
222  else
223  {
224  _maxPatdownProbability = DefaultMaxPatdownProbability;
225  Identifier settingDefinitionIdentifier = nameof(PatdownProbabilityMax).ToIdentifier();
226  Identifier attributeIdentifier = PatdownProbability.ToIdentifier();
227  if (CampaignModePresets.TryGetAttribute(settingDefinitionIdentifier, attributeIdentifier, out XAttribute? attribute))
228  {
229  _maxPatdownProbability = attribute.GetAttributeFloat(DefaultMaxPatdownProbability);
230  }
231  else
232  {
233  DebugConsole.ThrowError($"CampaignSettings: Can't find value for {attributeIdentifier} in {settingDefinitionIdentifier}");
234  }
235  return _maxPatdownProbability ?? DefaultMaxPatdownProbability;
236  }
237  }
238  }
239 
240  public Dictionary<Identifier, SerializableProperty> SerializableProperties { get; private set; }
241 
242  // required for INetSerializableStruct
243  public CampaignSettings()
244  {
245  SerializableProperties = SerializableProperty.GetProperties(this);
246  }
247 
248  public CampaignSettings(XElement? element = null)
249  {
250  SerializableProperties = SerializableProperty.DeserializeProperties(this, element);
251  }
252 
253  public XElement Save()
254  {
255  XElement saveElement = new XElement(LowerCaseSaveElementName);
256  SerializableProperty.SerializeProperties(this, saveElement, saveIfDefault: true);
257  return saveElement;
258  }
259 
260  private static int GetAddedMissionCount()
261  {
262  var characters = GameSession.GetSessionCrewCharacters(CharacterType.Both);
263  if (!characters.Any()) { return 0; }
264  return characters.Max(static character => (int)character.GetStatValue(StatTypes.ExtraMissionCount));
265  }
266 
267  public struct MultiplierSettings
268  {
269  public float Min { get; set; }
270  public float Max { get; set; }
271  public float Step { get; set; }
272  }
273 
274  public static MultiplierSettings GetMultiplierSettings(string multiplierName)
275  {
276  if (_multiplierSettings.TryGetValue(multiplierName, out MultiplierSettings value))
277  {
278  return value;
279  }
280 
281  return _multiplierSettings["default"];
282  }
283  }
284 }
StartingBalanceAmountOption
Definition: Enums.cs:718
WorldHostilityOption
Definition: Enums.cs:733
PatdownProbabilityOption
Definition: Enums.cs:725
CharacterType
Definition: Enums.cs:711
StatTypes
StatTypes are used to alter several traits of a character. They are mostly used by talents.
Definition: Enums.cs:195