Client LuaCsForBarotrauma
CharacterAbilityGivePermanentStat.cs
1 using System;
2 
3 namespace Barotrauma.Abilities
4 {
6  {
7  None,
10  }
11 
13  {
14  private readonly Identifier statIdentifier;
15  private readonly StatTypes statType;
16  private readonly float value;
17  private readonly float maxValue;
18  private readonly bool targetAllies;
19  private readonly bool removeOnDeath;
20  private readonly bool giveOnAddingFirstTime;
21  private readonly bool setValue;
22  private readonly PermanentStatPlaceholder placeholder;
23 
28  private readonly bool targetAbilityTarget = false;
29 
30  public override bool AllowClientSimulation => true;
31  public override bool AppliesEffectOnIntervalUpdate => true;
32 
33  public CharacterAbilityGivePermanentStat(CharacterAbilityGroup characterAbilityGroup, ContentXElement abilityElement) : base(characterAbilityGroup, abilityElement)
34  {
35  statIdentifier = abilityElement.GetAttributeIdentifier("statidentifier", Identifier.Empty);
36  if (statIdentifier.IsEmpty)
37  {
38  DebugConsole.ThrowError($"Error in talent \"{CharacterTalent.DebugIdentifier}\" - stat identifier not defined.",
39  contentPackage: abilityElement.ContentPackage);
40  }
41  string statTypeName = abilityElement.GetAttributeString("stattype", string.Empty);
42  statType = string.IsNullOrEmpty(statTypeName) ? StatTypes.None : CharacterAbilityGroup.ParseStatType(statTypeName, CharacterTalent.DebugIdentifier);
43  value = abilityElement.GetAttributeFloat("value", 0f);
44  maxValue = abilityElement.GetAttributeFloat("maxvalue", float.MaxValue);
45  targetAllies = abilityElement.GetAttributeBool("targetallies", false);
46  removeOnDeath = abilityElement.GetAttributeBool("removeondeath", false);
47  giveOnAddingFirstTime = abilityElement.GetAttributeBool("giveonaddingfirsttime", characterAbilityGroup.AbilityEffectType == AbilityEffectType.None);
48  setValue = abilityElement.GetAttributeBool("setvalue", false);
49  placeholder = abilityElement.GetAttributeEnum("placeholder", PermanentStatPlaceholder.None);
50  targetAbilityTarget = abilityElement.GetAttributeBool(nameof(targetAbilityTarget), false);
51  }
52 
53  public override void InitializeAbility(bool addingFirstTime)
54  {
55  if (giveOnAddingFirstTime && addingFirstTime)
56  {
57  ApplyEffectSpecific(abilityObject: null);
58  }
59  }
60 
61  protected override void ApplyEffect(AbilityObject abilityObject)
62  {
63  ApplyEffectSpecific(abilityObject);
64  }
65 
66  protected override void ApplyEffect()
67  {
68  ApplyEffectSpecific(abilityObject: null);
69  }
70 
71  private void ApplyEffectSpecific(AbilityObject abilityObject)
72  {
73  Identifier identifier = HandlePlaceholders(placeholder, statIdentifier);
74  if (targetAllies)
75  {
77  {
78  c?.Info?.ChangeSavedStatValue(statType, value, identifier, removeOnDeath, maxValue: maxValue, setValue: setValue);
79  }
80  }
81  else
82  {
83  Character targetCharacter =
84  targetAbilityTarget ?
85  (abilityObject as IAbilityCharacter)?.Character ?? Character :
86  Character;
87  if (targetCharacter == null)
88  {
89  DebugConsole.ThrowError($"Error in {nameof(CharacterAbilityGivePermanentStat.ApplyEffectSpecific)}: character was null.\n{Environment.StackTrace.CleanupStackTrace()}");
90  return;
91  }
92  if (targetCharacter?.Info == null)
93  {
94  DebugConsole.AddWarning($"Error in {nameof(CharacterAbilityGivePermanentStat.ApplyEffectSpecific)}: character {targetCharacter} has no CharacterInfo. Are you trying to use the condition on a non-player character?\n{Environment.StackTrace.CleanupStackTrace()}");
95  return;
96  }
97  targetCharacter.Info.ChangeSavedStatValue(statType, value, identifier, removeOnDeath, maxValue: maxValue, setValue: setValue);
98  }
99  }
100 
101  public static Identifier HandlePlaceholders(PermanentStatPlaceholder placeholder, Identifier original)
102  {
103  if (GameMain.GameSession?.Campaign?.Map is not { } map) { return original; }
104 
105  switch (placeholder)
106  {
107  case PermanentStatPlaceholder.LocationName when map.CurrentLocation is { } location:
108  return original.Replace("[placeholder]", location.NameIdentifier.Value);
109  case PermanentStatPlaceholder.LocationIndex:
110  return original.Replace("[placeholder]", map.CurrentLocationIndex.ToString());
111  }
112 
113  return original;
114  }
115  }
116 }
static Identifier HandlePlaceholders(PermanentStatPlaceholder placeholder, Identifier original)
CharacterAbilityGivePermanentStat(CharacterAbilityGroup characterAbilityGroup, ContentXElement abilityElement)
static StatTypes ParseStatType(string statTypeString, string debugIdentifier)
static IEnumerable< Character > GetFriendlyCrew(Character character)
void ChangeSavedStatValue(StatTypes statType, float value, Identifier statIdentifier, bool removeOnDeath, float maxValue=float.MaxValue, bool setValue=false)
string? GetAttributeString(string key, string? def)
float GetAttributeFloat(string key, float def)
ContentPackage? ContentPackage
bool GetAttributeBool(string key, bool def)
Identifier GetAttributeIdentifier(string key, string def)
static GameSession?? GameSession
Definition: GameMain.cs:88
AbilityEffectType
Definition: Enums.cs:140
StatTypes
StatTypes are used to alter several traits of a character. They are mostly used by talents.
Definition: Enums.cs:195