Client LuaCsForBarotrauma
TraitorEvent.cs
1 #nullable enable
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 namespace Barotrauma
8 {
9  partial class TraitorEvent : ScriptedEvent
10  {
11  public enum State
12  {
13  Incomplete,
14  Completed,
15  Failed
16  }
17 
18  public Action? OnStateChanged;
19 
20  private new readonly TraitorEventPrefab prefab;
21 
22  public new TraitorEventPrefab Prefab => prefab;
23 
24  private LocalizedString codeWord;
25 
26  private State currentState;
28  {
29  get { return currentState; }
30  set
31  {
32  if (currentState == value) { return; }
33  currentState = value;
34  OnStateChanged?.Invoke();
35  }
36  }
37 
38  private Client? traitor;
39 
40  public Client? Traitor => traitor;
41 
42  private readonly HashSet<Client> secondaryTraitors = new HashSet<Client>();
43 
44  public IEnumerable<Client> SecondaryTraitors => secondaryTraitors;
45 
46  public override string ToString()
47  {
48  return $"{nameof(TraitorEvent)} ({prefab.Identifier})";
49  }
50 
51  private readonly static HashSet<Identifier> nonActionChildElementNames = new HashSet<Identifier>()
52  {
53  "icon".ToIdentifier(),
54  "reputationrequirement".ToIdentifier(),
55  "missionrequirement".ToIdentifier(),
56  "levelrequirement".ToIdentifier()
57  };
58  protected override IEnumerable<Identifier> NonActionChildElementNames => nonActionChildElementNames;
59 
60  public TraitorEvent(TraitorEventPrefab prefab, int seed) : base(prefab, seed)
61  {
62  this.prefab = prefab;
63  codeWord = string.Empty;
64  }
65 
66  protected override void InitEventSpecific(EventSet? parentSet = null)
67  {
68  if (traitor == null)
69  {
70  DebugConsole.ThrowError($"Error when initializing event \"{prefab.Identifier}\": traitor not set.\n" + Environment.StackTrace);
71  }
72  }
73 
75  {
76  if (codeWord.IsNullOrEmpty())
77  {
78  //store the code word so the same random word is used in all the actions in the event
79  codeWord = TextManager.Get("traitor.codeword");
80  }
81 
82  return str
83  .Replace("[traitor]", traitor?.Name ?? "none")
84  .Replace("[target]", (GetTargets("target".ToIdentifier()).FirstOrDefault() as Character)?.DisplayName ?? "none")
85  .Replace("[codeword]", codeWord.Value);
86  }
87 
88  public void SetTraitor(Client traitor)
89  {
90  if (traitor.Character == null)
91  {
92  throw new InvalidOperationException($"Tried to set a client who's not controlling a character (\"{traitor.Name}\") as the traitor.");
93  }
94  this.traitor = traitor;
95  traitor.Character.IsTraitor = true;
96  AddTarget(Tags.Traitor, traitor.Character);
97  AddTarget(Tags.AnyTraitor, traitor.Character);
98  AddTargetPredicate(Tags.NonTraitor, TargetPredicate.EntityType.Character, e => e is Character c && (c.IsPlayer || c.IsBot) && !c.IsTraitor && c.TeamID == traitor.TeamID && !c.IsIncapacitated);
99  AddTargetPredicate(Tags.NonTraitorPlayer, TargetPredicate.EntityType.Character, e => e is Character c && c.IsPlayer && !c.IsTraitor && c.IsOnPlayerTeam && !c.IsIncapacitated);
100  }
101 
102  public void SetSecondaryTraitors(IEnumerable<Client> traitors)
103  {
104  int index = 0;
105  foreach (var traitor in traitors)
106  {
107  if (traitor.Character == null)
108  {
109  throw new InvalidOperationException($"Tried to set a client who's not controlling a character (\"{traitor.Name}\") as a secondary traitor.");
110  }
111  if (this.traitor == traitor)
112  {
113  DebugConsole.ThrowError($"Tried to assign the main traitor {traitor.Name} as a secondary traitor.");
114  continue;
115  }
116  secondaryTraitors.Add(traitor);
117  traitor.Character.IsTraitor = true;
118  AddTarget(Tags.SecondaryTraitor, traitor.Character);
119  AddTarget((Tags.SecondaryTraitor.ToString() + index).ToIdentifier(), traitor.Character);
120  AddTarget(Tags.AnyTraitor, traitor.Character);
121  index++;
122  }
123  }
124  }
125 }
Event sets are sets of random events that occur within a level (most commonly, monster spawns and scr...
Definition: EventSet.cs:31
LocalizedString Replace(Identifier find, LocalizedString replace, StringComparison stringComparison=StringComparison.Ordinal)
sealed record TargetPredicate(TargetPredicate.EntityType Type, Predicate< Entity > Predicate)
void AddTargetPredicate(Identifier tag, TargetPredicate.EntityType entityType, Predicate< Entity > predicate)
void AddTarget(Identifier tag, Entity target)
IEnumerable< Entity > GetTargets(Identifier tag)
IEnumerable< Client > SecondaryTraitors
Definition: TraitorEvent.cs:44
TraitorEvent(TraitorEventPrefab prefab, int seed)
Definition: TraitorEvent.cs:60
override void InitEventSpecific(EventSet? parentSet=null)
Definition: TraitorEvent.cs:66
override IEnumerable< Identifier > NonActionChildElementNames
Definition: TraitorEvent.cs:58
void SetSecondaryTraitors(IEnumerable< Client > traitors)
override string ToString()
Definition: TraitorEvent.cs:46
void SetTraitor(Client traitor)
Definition: TraitorEvent.cs:88
override LocalizedString ReplaceVariablesInEventText(LocalizedString str)
Definition: TraitorEvent.cs:74