Client LuaCsForBarotrauma
EventPrefab.cs
2 using System;
3 using System.Collections.Immutable;
4 using System.Linq;
5 using System.Reflection;
6 
7 namespace Barotrauma
8 {
10  {
12 
13  public readonly ContentXElement ConfigElement;
14  public readonly Type EventType;
15 
16  private readonly ImmutableHashSet<Identifier> tags;
17  public ImmutableHashSet<Identifier> Tags => tags;
18 
22  public readonly float Probability;
23 
27  public readonly bool TriggerEventCooldown;
28 
33  public readonly float Commonness;
34 
38  public readonly Identifier BiomeIdentifier;
39 
43  public readonly Identifier RequiredLayer;
44 
49 
53  public readonly Identifier Faction;
54 
55  public readonly LocalizedString Name;
56 
60  public readonly bool UnlockPathEvent;
61 
65  public readonly string UnlockPathTooltip;
66 
70  public readonly int UnlockPathReputation;
71 
72  public static EventPrefab Create(ContentXElement element, RandomEventsFile file, Identifier fallbackIdentifier = default)
73  {
74  if (element.NameAsIdentifier() == nameof(TraitorEvent))
75  {
76  return new TraitorEventPrefab(element, file, fallbackIdentifier);
77  }
78  else
79  {
80  return new EventPrefab(element, file, fallbackIdentifier);
81  }
82  }
83 
84  public EventPrefab(ContentXElement element, RandomEventsFile file, Identifier fallbackIdentifier = default)
85  : base(file, element.GetAttributeIdentifier("identifier", fallbackIdentifier))
86  {
87  ConfigElement = element;
88 
89  try
90  {
91  EventType = Type.GetType("Barotrauma." + ConfigElement.Name, true, true);
92  if (EventType == null)
93  {
94  DebugConsole.ThrowError("Could not find an event class of the type \"" + ConfigElement.Name + "\".",
95  contentPackage: element.ContentPackage);
96  }
97  }
98  catch
99  {
100  DebugConsole.ThrowError("Could not find an event class of the type \"" + ConfigElement.Name + "\".",
101  contentPackage: element.ContentPackage);
102  }
103 
104  Name = TextManager.Get($"eventname.{Identifier}").Fallback(Identifier.ToString());
105 
106  tags = ConfigElement.GetAttributeIdentifierImmutableHashSet(nameof(tags), ImmutableHashSet<Identifier>.Empty);
109  Commonness = element.GetAttributeFloat("commonness", 1.0f);
110  Probability = Math.Clamp(element.GetAttributeFloat(1.0f, "probability", "spawnprobability"), 0, 1);
111  TriggerEventCooldown = element.GetAttributeBool("triggereventcooldown", EventType != typeof(ScriptedEvent));
112 
115 
116  UnlockPathEvent = element.GetAttributeBool("unlockpathevent", false);
117  UnlockPathTooltip = element.GetAttributeString("unlockpathtooltip", "lockedpathtooltip");
118  UnlockPathReputation = element.GetAttributeInt("unlockpathreputation", 0);
119  }
120 
121  public bool TryCreateInstance<T>(int seed, out T instance) where T : Event
122  {
123  instance = CreateInstance(seed) as T;
124  return instance is not null;
125  }
126 
127  public Event CreateInstance(int seed)
128  {
129  ConstructorInfo constructor = EventType.GetConstructor(new[] { GetType(), typeof(int) });
130  Event instance = null;
131  try
132  {
133  instance = constructor.Invoke(new object[] { this, seed }) as Event;
134  }
135  catch (Exception ex)
136  {
137  DebugConsole.ThrowError(ex.InnerException != null ? ex.InnerException.ToString() : ex.ToString());
138  }
139  if (instance != null && !instance.LevelMeetsRequirements()) { return null; }
140  return instance;
141  }
142 
143  public override void Dispose() { }
144 
145  public override string ToString()
146  {
147  return $"{nameof(EventPrefab)} ({Identifier})";
148  }
149 
150  public static EventPrefab GetUnlockPathEvent(Identifier biomeIdentifier, Faction faction)
151  {
152  var unlockPathEvents = Prefabs.OrderBy(p => p.Identifier).Where(e => e.UnlockPathEvent);
153  if (faction != null && unlockPathEvents.Any(e => e.Faction == faction.Prefab.Identifier))
154  {
155  unlockPathEvents = unlockPathEvents.Where(e => e.Faction == faction.Prefab.Identifier);
156  }
157  return
158  unlockPathEvents.FirstOrDefault(ep => ep.BiomeIdentifier == biomeIdentifier) ??
159  unlockPathEvents.FirstOrDefault(ep => ep.BiomeIdentifier == Identifier.Empty);
160  }
161 
167  public static EventPrefab FindEventPrefab(Identifier identifier, Identifier tag, ContentPackage source)
168  {
169  EventPrefab eventPrefab = null;
170  if (!identifier.IsEmpty)
171  {
172  eventPrefab = EventSet.GetEventPrefab(identifier);
173  if (eventPrefab == null)
174  {
175  DebugConsole.ThrowError($"Failed to find an event prefab with the identifier {identifier}.",
176  contentPackage: source);
177  }
178  }
179  else if (!tag.IsEmpty)
180  {
181  eventPrefab = EventSet.GetAllEventPrefabs().Where(e => e.Tags.Contains(tag)).GetRandomUnsynced();
182  if (eventPrefab == null)
183  {
184  DebugConsole.ThrowError($"Failed to find an event prefab with the tag {tag}.",
185  contentPackage: source);
186  }
187  }
188  else
189  {
190  DebugConsole.ThrowError($"Failed to find an event prefab: neither an identifier or tag were defined.",
191  contentPackage: source);
192  }
193  return eventPrefab;
194  }
195  }
196 }
string? GetAttributeString(string key, string? def)
float GetAttributeFloat(string key, float def)
ContentPackage? ContentPackage
Identifier NameAsIdentifier()
bool GetAttributeBool(string key, bool def)
int GetAttributeInt(string key, int def)
ImmutableHashSet< Identifier > GetAttributeIdentifierImmutableHashSet(string key, ImmutableHashSet< Identifier >? def, bool trim=true)
Identifier GetAttributeIdentifier(string key, string def)
virtual bool LevelMeetsRequirements()
Definition: Event.cs:79
readonly Identifier BiomeIdentifier
If set, the event set can only be chosen in this biome.
Definition: EventPrefab.cs:38
readonly LocalizedString Name
Definition: EventPrefab.cs:55
readonly Identifier RequiredLayer
If set, this layer must be present somewhere in the level.
Definition: EventPrefab.cs:43
Event CreateInstance(int seed)
Definition: EventPrefab.cs:127
readonly bool UnlockPathEvent
If set, this event is used as an event that can unlock a path to the next biome.
Definition: EventPrefab.cs:60
readonly string UnlockPathTooltip
Only valid if UnlockPathEvent is set to true. The tooltip displayed on the pathway this event is bloc...
Definition: EventPrefab.cs:65
readonly float Commonness
The commonness of the event (i.e. how likely it is for this specific event to be chosen from the even...
Definition: EventPrefab.cs:33
EventPrefab(ContentXElement element, RandomEventsFile file, Identifier fallbackIdentifier=default)
Definition: EventPrefab.cs:84
override string ToString()
Definition: EventPrefab.cs:145
ImmutableHashSet< Identifier > Tags
Definition: EventPrefab.cs:17
static EventPrefab Create(ContentXElement element, RandomEventsFile file, Identifier fallbackIdentifier=default)
Definition: EventPrefab.cs:72
readonly bool TriggerEventCooldown
When this event occurs, should it trigger the event cooldown during which no new events are triggered...
Definition: EventPrefab.cs:27
bool TryCreateInstance< T >(int seed, out T instance)
Definition: EventPrefab.cs:121
static EventPrefab FindEventPrefab(Identifier identifier, Identifier tag, ContentPackage source)
Finds an event prefab with the specified identifier, or if it isn't defined, a random event prefab wi...
Definition: EventPrefab.cs:167
readonly Identifier RequiredSpawnPointTag
If set, this spawn point tag must be present somewhere in the level.
Definition: EventPrefab.cs:48
static EventPrefab GetUnlockPathEvent(Identifier biomeIdentifier, Faction faction)
Definition: EventPrefab.cs:150
readonly ContentXElement ConfigElement
Definition: EventPrefab.cs:13
readonly Type EventType
Definition: EventPrefab.cs:14
readonly Identifier Faction
If set, the event set can only be chosen in locations that belong to this faction.
Definition: EventPrefab.cs:53
readonly int UnlockPathReputation
Only valid if UnlockPathEvent is set to true. The reputation requirement displayed on the pathway thi...
Definition: EventPrefab.cs:70
readonly float Probability
The probability for the event to do something if it gets selected. For example, the probability for a...
Definition: EventPrefab.cs:22
static readonly PrefabCollection< EventPrefab > Prefabs
Definition: EventPrefab.cs:11
override void Dispose()
Definition: EventPrefab.cs:143
Event sets are sets of random events that occur within a level (most commonly, monster spawns and scr...
Definition: EventSet.cs:31
static EventPrefab GetEventPrefab(Identifier identifier)
Definition: EventSet.cs:80
static List< EventPrefab > GetAllEventPrefabs()
Definition: EventSet.cs:64
FactionPrefab Prefab
Definition: Factions.cs:18
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