Client LuaCsForBarotrauma
Biome.cs
1 using System;
3 using System.Collections.Generic;
4 using System.Collections.Immutable;
5 
6 namespace Barotrauma
7 {
9  {
11 
12  public readonly Identifier OldIdentifier;
13  public readonly LocalizedString DisplayName;
14  public readonly LocalizedString Description;
15 
16  public readonly bool IsEndBiome;
17  public readonly int EndBiomeLocationCount;
18 
19  public readonly float MinDifficulty;
20  private readonly float maxDifficulty;
21  public float ActualMaxDifficulty => maxDifficulty;
22  public float AdjustedMaxDifficulty => maxDifficulty - 0.1f;
23 
24 
25  public readonly ImmutableHashSet<int> AllowedZones;
26 
27  private readonly SubmarineAvailability? submarineAvailability;
28  private readonly ImmutableHashSet<SubmarineAvailability> submarineAvailabilityOverrides;
29 
30  public readonly record struct SubmarineAvailability(
32  SubmarineClass Class = SubmarineClass.Undefined,
33  int MaxTier = 0);
34 
35  public Biome(ContentXElement element, LevelGenerationParametersFile file) : base(file, ParseIdentifier(element))
36  {
37  OldIdentifier = element.GetAttributeIdentifier("oldidentifier", Identifier.Empty);
38 
39  DisplayName =
40  TextManager.Get("biomename." + Identifier).Fallback(
41  element.GetAttributeString("name", "Biome"));
42 
43  Description =
44  TextManager.Get("biomedescription." + Identifier).Fallback(
45  element.GetAttributeString("description", ""));
46 
47  IsEndBiome = element.GetAttributeBool("endbiome", false);
48  EndBiomeLocationCount = Math.Max(1, element.GetAttributeInt("endbiomelocationcount", 1));
49 
50  AllowedZones = element.GetAttributeIntArray("AllowedZones", new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }).ToImmutableHashSet();
51  MinDifficulty = element.GetAttributeFloat("MinDifficulty", 0);
52  maxDifficulty = element.GetAttributeFloat("MaxDifficulty", 100);
53 
54  var submarineAvailabilityOverrides = new HashSet<SubmarineAvailability>();
55  if (element.GetChildElement("submarines") is ContentXElement availabilityElement)
56  {
57  submarineAvailability = GetAvailability(availabilityElement);
58  foreach (var overrideElement in availabilityElement.GetChildElements("override"))
59  {
60  var availabilityOverride = GetAvailability(overrideElement);
61  submarineAvailabilityOverrides.Add(availabilityOverride);
62  }
63  }
64  this.submarineAvailabilityOverrides = submarineAvailabilityOverrides.ToImmutableHashSet();
65 
66  static SubmarineAvailability GetAvailability(ContentXElement element)
67  {
68  return new SubmarineAvailability(
69  LocationType: element.GetAttributeIdentifier("locationtype", Identifier.Empty),
70  Class: element.GetAttributeEnum("class", SubmarineClass.Undefined),
71  MaxTier: element.GetAttributeInt("maxtier", 0));
72  }
73  }
74 
76  {
77  Identifier identifier = element.GetAttributeIdentifier("identifier", "");
78  if (identifier.IsEmpty)
79  {
80  identifier = element.GetAttributeIdentifier("name", "");
81  DebugConsole.ThrowError("Error in biome \"" + identifier + "\": identifier missing, using name as the identifier.");
82  }
83  return identifier;
84  }
85 
86  public int HighestSubmarineTierAvailable(SubmarineClass subClass, Identifier locationType)
87  {
88  if (!submarineAvailability.HasValue)
89  {
90  // If the availability is not explicitly defined, make all subs available
92  }
93  int maxTier = submarineAvailability.Value.MaxTier;
94  if (submarineAvailabilityOverrides.FirstOrNull(a => a.LocationType == locationType && a.Class == subClass) is SubmarineAvailability locationAndClassOverride)
95  {
96  maxTier = locationAndClassOverride.MaxTier;
97  }
98  else if (submarineAvailabilityOverrides.FirstOrNull(a => a.LocationType == locationType && a.Class == SubmarineClass.Undefined) is SubmarineAvailability locationOverride)
99  {
100  maxTier = locationOverride.MaxTier;
101  }
102  else if (submarineAvailabilityOverrides.FirstOrNull(a => a.LocationType == Identifier.Empty && a.Class == subClass) is SubmarineAvailability classOverride)
103  {
104  maxTier = classOverride.MaxTier;
105  }
106  return maxTier;
107  }
108 
109  public bool IsSubmarineAvailable(SubmarineInfo info, Identifier locationType) => info.Tier <= HighestSubmarineTierAvailable(info.SubmarineClass, locationType);
110 
111  public override void Dispose() { }
112  }
113 }
readonly LocalizedString Description
Definition: Biome.cs:14
readonly ImmutableHashSet< int > AllowedZones
Definition: Biome.cs:25
readonly bool IsEndBiome
Definition: Biome.cs:16
override void Dispose()
Definition: Biome.cs:111
static readonly PrefabCollection< Biome > Prefabs
Definition: Biome.cs:10
static Identifier ParseIdentifier(ContentXElement element)
Definition: Biome.cs:75
int HighestSubmarineTierAvailable(SubmarineClass subClass, Identifier locationType)
Definition: Biome.cs:86
bool IsSubmarineAvailable(SubmarineInfo info, Identifier locationType)
readonly float MinDifficulty
Definition: Biome.cs:19
float AdjustedMaxDifficulty
Definition: Biome.cs:22
readonly Identifier OldIdentifier
Definition: Biome.cs:12
readonly LocalizedString DisplayName
Definition: Biome.cs:13
readonly record struct SubmarineAvailability(Identifier LocationType, SubmarineClass Class=SubmarineClass.Undefined, int MaxTier=0)
Biome(ContentXElement element, LevelGenerationParametersFile file)
Definition: Biome.cs:35
float ActualMaxDifficulty
Definition: Biome.cs:21
readonly int EndBiomeLocationCount
Definition: Biome.cs:17
string? GetAttributeString(string key, string? def)
int?[] GetAttributeIntArray(string key, int[]? def)
float GetAttributeFloat(string key, float def)
ContentXElement? GetChildElement(string name)
bool GetAttributeBool(string key, bool def)
int GetAttributeInt(string key, int def)
Identifier GetAttributeIdentifier(string key, string def)
LocalizedString Fallback(LocalizedString fallback, bool useDefaultLanguageIfFound=true)
Use this text instead if the original text cannot be found.
readonly Identifier Identifier
Definition: Prefab.cs:34
Prefab that has a property serves as a deterministic hash of a prefab's identifier....