Client LuaCsForBarotrauma
EventPrefab.cs
1 using System;
2 using System.Linq;
3 using System.Reflection;
4 
5 namespace Barotrauma
6 {
8  {
10 
11  public readonly ContentXElement ConfigElement;
12  public readonly Type EventType;
13 
17  public readonly float Probability;
18 
22  public readonly bool TriggerEventCooldown;
23 
28  public readonly float Commonness;
29 
33  public readonly Identifier BiomeIdentifier;
34 
38  public readonly Identifier RequiredLayer;
39 
43  public readonly Identifier Faction;
44 
45  public readonly LocalizedString Name;
46 
50  public readonly bool UnlockPathEvent;
51 
55  public readonly string UnlockPathTooltip;
56 
60  public readonly int UnlockPathReputation;
61 
62  public static EventPrefab Create(ContentXElement element, RandomEventsFile file, Identifier fallbackIdentifier = default)
63  {
64  if (element.NameAsIdentifier() == nameof(TraitorEvent))
65  {
66  return new TraitorEventPrefab(element, file, fallbackIdentifier);
67  }
68  else
69  {
70  return new EventPrefab(element, file, fallbackIdentifier);
71  }
72  }
73 
74  public EventPrefab(ContentXElement element, RandomEventsFile file, Identifier fallbackIdentifier = default)
75  : base(file, element.GetAttributeIdentifier("identifier", fallbackIdentifier))
76  {
77  ConfigElement = element;
78 
79  try
80  {
81  EventType = Type.GetType("Barotrauma." + ConfigElement.Name, true, true);
82  if (EventType == null)
83  {
84  DebugConsole.ThrowError("Could not find an event class of the type \"" + ConfigElement.Name + "\".",
85  contentPackage: element.ContentPackage);
86  }
87  }
88  catch
89  {
90  DebugConsole.ThrowError("Could not find an event class of the type \"" + ConfigElement.Name + "\".",
91  contentPackage: element.ContentPackage);
92  }
93 
94  Name = TextManager.Get($"eventname.{Identifier}").Fallback(Identifier.ToString());
95 
98  Commonness = element.GetAttributeFloat("commonness", 1.0f);
99  Probability = Math.Clamp(element.GetAttributeFloat(1.0f, "probability", "spawnprobability"), 0, 1);
100  TriggerEventCooldown = element.GetAttributeBool("triggereventcooldown", EventType != typeof(ScriptedEvent));
101 
103 
104  UnlockPathEvent = element.GetAttributeBool("unlockpathevent", false);
105  UnlockPathTooltip = element.GetAttributeString("unlockpathtooltip", "lockedpathtooltip");
106  UnlockPathReputation = element.GetAttributeInt("unlockpathreputation", 0);
107  }
108 
109  public bool TryCreateInstance<T>(int seed, out T instance) where T : Event
110  {
111  instance = CreateInstance(seed) as T;
112  return instance is not null;
113  }
114 
115  public Event CreateInstance(int seed)
116  {
117  ConstructorInfo constructor = EventType.GetConstructor(new[] { GetType(), typeof(int) });
118  Event instance = null;
119  try
120  {
121  instance = constructor.Invoke(new object[] { this, seed }) as Event;
122  }
123  catch (Exception ex)
124  {
125  DebugConsole.ThrowError(ex.InnerException != null ? ex.InnerException.ToString() : ex.ToString());
126  }
127  if (instance != null && !instance.LevelMeetsRequirements()) { return null; }
128  return instance;
129  }
130 
131  public override void Dispose() { }
132 
133  public override string ToString()
134  {
135  return $"{nameof(EventPrefab)} ({Identifier})";
136  }
137 
138  public static EventPrefab GetUnlockPathEvent(Identifier biomeIdentifier, Faction faction)
139  {
140  var unlockPathEvents = Prefabs.OrderBy(p => p.Identifier).Where(e => e.UnlockPathEvent);
141  if (faction != null && unlockPathEvents.Any(e => e.Faction == faction.Prefab.Identifier))
142  {
143  unlockPathEvents = unlockPathEvents.Where(e => e.Faction == faction.Prefab.Identifier);
144  }
145  return
146  unlockPathEvents.FirstOrDefault(ep => ep.BiomeIdentifier == biomeIdentifier) ??
147  unlockPathEvents.FirstOrDefault(ep => ep.BiomeIdentifier == Identifier.Empty);
148  }
149  }
150 }
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)
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:33
readonly LocalizedString Name
Definition: EventPrefab.cs:45
readonly Identifier RequiredLayer
If set, this layer must be present somewhere in the level.
Definition: EventPrefab.cs:38
Event CreateInstance(int seed)
Definition: EventPrefab.cs:115
readonly bool UnlockPathEvent
If set, this event is used as an event that can unlock a path to the next biome.
Definition: EventPrefab.cs:50
readonly string UnlockPathTooltip
Only valid if UnlockPathEvent is set to true. The tooltip displayed on the pathway this event is bloc...
Definition: EventPrefab.cs:55
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:28
EventPrefab(ContentXElement element, RandomEventsFile file, Identifier fallbackIdentifier=default)
Definition: EventPrefab.cs:74
override string ToString()
Definition: EventPrefab.cs:133
static EventPrefab Create(ContentXElement element, RandomEventsFile file, Identifier fallbackIdentifier=default)
Definition: EventPrefab.cs:62
readonly bool TriggerEventCooldown
When this event occurs, should it trigger the event cooldown during which no new events are triggered...
Definition: EventPrefab.cs:22
bool TryCreateInstance< T >(int seed, out T instance)
Definition: EventPrefab.cs:109
static EventPrefab GetUnlockPathEvent(Identifier biomeIdentifier, Faction faction)
Definition: EventPrefab.cs:138
readonly ContentXElement ConfigElement
Definition: EventPrefab.cs:11
readonly Type EventType
Definition: EventPrefab.cs:12
readonly Identifier Faction
If set, the event set can only be chosen in locations that belong to this faction.
Definition: EventPrefab.cs:43
readonly int UnlockPathReputation
Only valid if UnlockPathEvent is set to true. The reputation requirement displayed on the pathway thi...
Definition: EventPrefab.cs:60
readonly float Probability
The probability for the event to do something if it gets selected. For example, the probability for a...
Definition: EventPrefab.cs:17
static readonly PrefabCollection< EventPrefab > Prefabs
Definition: EventPrefab.cs:9
override void Dispose()
Definition: EventPrefab.cs:131
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