Client LuaCsForBarotrauma
AbilityConditionHasLevel.cs
1 #nullable enable
2 
3 using System;
4 
5 namespace Barotrauma.Abilities
6 {
7  internal sealed class AbilityConditionHasLevel : AbilityConditionDataless
8  {
9  private readonly Option<int> matchedLevel;
10  private readonly Option<int> minLevel;
11  private readonly Option<int> maxLevel;
12 
13  public AbilityConditionHasLevel(CharacterTalent characterTalent, ContentXElement conditionElement) : base(characterTalent, conditionElement)
14  {
15  matchedLevel = conditionElement.GetAttributeInt("levelequals", 0) is var match and not 0
16  ? Option<int>.Some(match)
17  : Option<int>.None();
18 
19  minLevel = conditionElement.GetAttributeInt("minlevel", 0) is var min and not 0
20  ? Option<int>.Some(min)
21  : Option<int>.None();
22 
23  maxLevel = conditionElement.GetAttributeInt("maxlevel", 0) is var max and not 0
24  ? Option<int>.Some(max)
25  : Option<int>.None();
26 
27  if (matchedLevel.IsNone() && minLevel.IsNone() && maxLevel.IsNone())
28  {
29  throw new Exception($"{nameof(AbilityConditionHasLevel)} must have either \"levelequals\", \"minlevel\" or \"maxlevel\" attribute.");
30  }
31  }
32 
33  protected override bool MatchesConditionSpecific()
34  {
35  var currentLevel = character.Info.GetCurrentLevel();
36  if (matchedLevel.TryUnwrap(out int match))
37  {
38  return currentLevel == match;
39  }
40 
41  if (minLevel.TryUnwrap(out int min))
42  {
43  return currentLevel >= min;
44  }
45 
46  if (maxLevel.TryUnwrap(out int max))
47  {
48  return currentLevel <= max;
49  }
50 
51  return false;
52  }
53  }
54 }