Client LuaCsForBarotrauma
Factions.cs
1 #nullable enable
2 using Microsoft.Xna.Framework;
3 using System.Collections.Generic;
4 using System.Collections.Immutable;
5 
6 namespace Barotrauma
7 {
8  public enum FactionAffiliation
9  {
10  Positive,
11  Neutral,
12  Negative
13  }
14 
15  class Faction
16  {
17  public Reputation Reputation { get; }
18  public FactionPrefab Prefab { get; }
19 
20  public Faction(CampaignMetadata? metadata, FactionPrefab prefab)
21  {
22  Prefab = prefab;
23  Reputation = new Reputation(metadata, this, prefab.MinReputation, prefab.MaxReputation, prefab.InitialReputation);
24  }
25 
31  {
32  if (GameMain.GameSession?.Campaign?.Factions is not { } factions) { return FactionAffiliation.Neutral; }
33 
34  bool isHighest = true;
35  foreach (Faction otherFaction in factions)
36  {
37  if (otherFaction == faction || otherFaction.Reputation.Value < faction.Reputation.Value) { continue; }
38 
39  isHighest = false;
40  break;
41  }
42 
43  return isHighest ? FactionAffiliation.Positive : FactionAffiliation.Negative;
44  }
45 
46  public override string ToString()
47  {
48  return $"{base.ToString()} ({Prefab?.Identifier.ToString() ?? "null"})";
49  }
50  }
51 
52  internal class FactionPrefab : Prefab
53  {
54  public readonly static PrefabCollection<FactionPrefab> Prefabs = new PrefabCollection<FactionPrefab>();
55 
56  public LocalizedString Name { get; }
57 
58  public LocalizedString Description { get; }
59  public LocalizedString ShortDescription { get; }
60 
61  public Identifier OpposingFaction { get; }
62 
63  public class HireableCharacter
64  {
65  public readonly Identifier NPCSetIdentifier;
66  public readonly Identifier NPCIdentifier;
67  public readonly float MinReputation;
68 
70  {
71  NPCSetIdentifier = element.GetAttributeIdentifier("from", element.GetAttributeIdentifier("npcsetidentifier", Identifier.Empty));
72  NPCIdentifier = element.GetAttributeIdentifier("identifier", element.GetAttributeIdentifier("npcidentifier", Identifier.Empty));
73  MinReputation = element.GetAttributeFloat("minreputation", 0.0f);
74  }
75  }
76 
77  public ImmutableArray<HireableCharacter> HireableCharacters;
78 
79  public class AutomaticMission
80  {
81  public readonly Identifier MissionTag;
82  public readonly LevelData.LevelType LevelType;
83  public readonly float MinReputation, MaxReputation;
84  public readonly float MinProbability, MaxProbability;
85  public readonly int MaxDistanceFromFactionOutpost;
87 
88  public AutomaticMission(ContentXElement element, string parentDebugName)
89  {
90  MissionTag = element.GetAttributeIdentifier("missiontag", Identifier.Empty);
91  LevelType = element.GetAttributeEnum("leveltype", LevelData.LevelType.LocationConnection);
92  MinReputation = element.GetAttributeFloat("minreputation", 0.0f);
93  MaxReputation = element.GetAttributeFloat("maxreputation", 0.0f);
94  if (MinReputation > MaxReputation)
95  {
96  DebugConsole.ThrowError($"Error in faction prefab \"{parentDebugName}\": MinReputation cannot be larger than MaxReputation.");
97  }
98  float probability = element.GetAttributeFloat("probability", 0.0f);
99  MinProbability = element.GetAttributeFloat("minprobability", probability);
100  MaxProbability = element.GetAttributeFloat("maxprobability", probability);
103  }
104  }
105 
106  public ImmutableArray<AutomaticMission> AutomaticMissions;
107 
108  public bool StartOutpost { get; }
109 
110 
111  public int MenuOrder { get; }
112 
116  public int MinReputation { get; }
117 
121  public int MaxReputation { get; }
122 
126  public int InitialReputation { get; }
127 
128  public float ControlledOutpostPercentage { get; }
129 
130  public float SecondaryControlledOutpostPercentage { get; }
131 
132 #if CLIENT
133  public Sprite? Icon { get; private set; }
134 
135  public Sprite? IconSmall { get; private set; }
136 
137  public Sprite? BackgroundPortrait { get; private set; }
138 #endif
139 
140  public Color IconColor { get; }
141 
142  public FactionPrefab(ContentXElement element, FactionsFile file) : base(file, element.GetAttributeIdentifier("identifier", string.Empty))
143  {
144  MenuOrder = element.GetAttributeInt("menuorder", 0);
145  StartOutpost = element.GetAttributeBool("startoutpost", false);
146  MinReputation = element.GetAttributeInt("minreputation", -100);
147  MaxReputation = element.GetAttributeInt("maxreputation", 100);
148  InitialReputation = element.GetAttributeInt("initialreputation", 0);
149  ControlledOutpostPercentage = element.GetAttributeFloat("controlledoutpostpercentage", 0);
150  SecondaryControlledOutpostPercentage = element.GetAttributeFloat("secondarycontrolledoutpostpercentage", 0);
151  Name = element.GetAttributeString("name", null) ?? TextManager.Get($"faction.{Identifier}").Fallback("Unnamed");
152  Description = element.GetAttributeString("description", null) ?? TextManager.Get($"faction.{Identifier}.description").Fallback("");
153  ShortDescription = element.GetAttributeString("shortdescription", null) ?? TextManager.Get($"faction.{Identifier}.shortdescription").Fallback("");
154  OpposingFaction = element.GetAttributeIdentifier(nameof(OpposingFaction), Identifier.Empty);
155 
156  List<HireableCharacter> hireableCharacters = new List<HireableCharacter>();
157  List<AutomaticMission> automaticMissions = new List<AutomaticMission>();
158  foreach (var subElement in element.Elements())
159  {
160  var subElementId = subElement.NameAsIdentifier();
161  if (subElementId == "icon")
162  {
163  IconColor = subElement.GetAttributeColor("color", Color.White);
164 #if CLIENT
165  Icon = new Sprite(subElement);
166 #endif
167  }
168  else if (subElementId == "iconsmall")
169  {
170 #if CLIENT
171  IconSmall = new Sprite(subElement);
172 #endif
173  }
174  else if (subElementId == "portrait")
175  {
176 #if CLIENT
177  BackgroundPortrait = new Sprite(subElement);
178 #endif
179  }
180  else if (subElementId == "hireable")
181  {
182  hireableCharacters.Add(new HireableCharacter(subElement));
183  }
184  else if (subElementId == "mission" || subElementId == "automaticmission")
185  {
186  automaticMissions.Add(new AutomaticMission(subElement, Identifier.ToString()));
187  }
188  }
189  HireableCharacters = hireableCharacters.ToImmutableArray();
190  AutomaticMissions = automaticMissions.ToImmutableArray();
191  }
192 
193  public override string ToString()
194  {
195  return $"{base.ToString()} ({Identifier})";
196  }
197 
198  public override void Dispose()
199  {
200 #if CLIENT
201  Icon?.Remove();
202  Icon = null;
203 #endif
204  }
205  }
206 }
float GetAttributeFloat(string key, float def)
bool GetAttributeBool(string key, bool def)
int GetAttributeInt(string key, int def)
Identifier GetAttributeIdentifier(string key, string def)
override string ToString()
Definition: Factions.cs:46
Faction(CampaignMetadata? metadata, FactionPrefab prefab)
Definition: Factions.cs:20
static FactionAffiliation GetPlayerAffiliationStatus(Faction faction)
Get what kind of affiliation this faction has towards the player depending on who they chose to side ...
Definition: Factions.cs:30
Reputation Reputation
Definition: Factions.cs:17
readonly LevelData.LevelType LevelType
Definition: Factions.cs:82
readonly bool DisallowBetweenOtherFactionOutposts
Definition: Factions.cs:86
AutomaticMission(ContentXElement element, string parentDebugName)
Definition: Factions.cs:88
HireableCharacter(ContentXElement element)
Definition: Factions.cs:69
static GameSession?? GameSession
Definition: GameMain.cs:88
readonly Identifier Identifier
Definition: Prefab.cs:34
float??????? Value
Definition: Reputation.cs:44
FactionAffiliation
Definition: Factions.cs:9