Client LuaCsForBarotrauma
CharacterAbilityUnlockApprenticeshipTalentTree.cs
1 #nullable enable
2 
3 using System;
5 using System.Collections.Generic;
6 using System.Collections.Immutable;
7 using System.Linq;
8 
9 namespace Barotrauma.Abilities
10 {
11  internal sealed class CharacterAbilityUnlockApprenticeshipTalentTree : CharacterAbility
12  {
13  public override bool AllowClientSimulation => false;
14 
15  public CharacterAbilityUnlockApprenticeshipTalentTree(CharacterAbilityGroup characterAbilityGroup, ContentXElement abilityElement) : base(characterAbilityGroup, abilityElement) { }
16 
17  public override void InitializeAbility(bool addingFirstTime)
18  {
19  if (!addingFirstTime) { return; }
20 
21  // do not run client-side in multiplayer
22  if (GameMain.NetworkMember is { IsClient: true }) { return; }
23 
24  JobPrefab? apprentice = CharacterAbilityApplyStatusEffectsToApprenticeship.GetApprenticeJob(Character, JobPrefab.Prefabs.ToImmutableHashSet());
25  if (apprentice is null)
26  {
27  DebugConsole.ThrowError($"{nameof(CharacterAbilityUnlockApprenticeshipTalentTree)}: Could not find apprentice job for character {Character.Name}",
28  contentPackage: CharacterTalent.Prefab.ContentPackage);
29  return;
30  }
31 
32  if (!TalentTree.JobTalentTrees.TryGet(apprentice.Identifier, out TalentTree? talentTree)) { return; }
33 
34  var characters = Character.GetFriendlyCrew(Character);
35 
36  HashSet<ImmutableHashSet<Identifier>> talentsTrees = new HashSet<ImmutableHashSet<Identifier>>();
37  foreach (TalentSubTree subTree in talentTree.TalentSubTrees)
38  {
39  if (subTree.Type != TalentTreeType.Specialization) { continue; }
40 
41  HashSet<Identifier> identifiers = new HashSet<Identifier>();
42  foreach (TalentOption option in subTree.TalentOptionStages)
43  {
44  foreach (Identifier identifier in option.TalentIdentifiers)
45  {
46  if (IsShowCaseTalent(identifier, option) || TalentTree.IsTalentLocked(identifier, characters)) { continue; }
47 
48  identifiers.Add(identifier);
49  }
50 
51  foreach (var (_, value) in option.ShowCaseTalents)
52  {
53  var ids = value.Where(i => !TalentTree.IsTalentLocked(i, characters)).ToImmutableHashSet();
54  if (ids.Count is 0) { continue; }
55 
56  identifiers.Add(value.GetRandomUnsynced());
57  }
58  }
59 
60  talentsTrees.Add(identifiers.ToImmutableHashSet());
61  }
62 
63  ImmutableHashSet<Identifier>? selectedTalentTree = talentsTrees.GetRandomUnsynced();
64  if (selectedTalentTree != null)
65  {
66  foreach (Identifier identifier in selectedTalentTree)
67  {
68  if (Character.HasTalent(identifier)) { continue; }
69  Character.GiveTalent(identifier);
70  }
71  }
72 
73  static bool IsShowCaseTalent(Identifier identifier, TalentOption option)
74  {
75  foreach (var (_, value) in option.ShowCaseTalents)
76  {
77  if (value.Contains(identifier)) { return true; }
78  }
79 
80  return false;
81  }
82  }
83 
84  protected override void ApplyEffect(AbilityObject abilityObject)
85  {
86  ApplyEffect();
87  }
88  }
89 }
static IEnumerable< Character > GetFriendlyCrew(Character character)
bool GiveTalent(Identifier talentIdentifier, bool addingFirstTime=true)
readonly TalentPrefab Prefab
ContentPackage? ContentPackage
Definition: Prefab.cs:37