Client LuaCsForBarotrauma
Job.cs
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Xml.Linq;
5 
6 namespace Barotrauma
7 {
8  class Job
9  {
10  private readonly JobPrefab prefab;
11 
12  private readonly Dictionary<Identifier, Skill> skills;
13 
14  public LocalizedString Name => prefab.Name;
15 
16  public LocalizedString Description => prefab.Description;
17 
18  public JobPrefab Prefab => prefab;
19 
20  public int Variant;
21 
22  public Skill PrimarySkill { get; private set; }
23 
24  public Job(JobPrefab jobPrefab, bool isPvP) : this(jobPrefab, isPvP, randSync: Rand.RandSync.Unsynced, variant: 0) { }
25 
26  public Job(JobPrefab jobPrefab, bool isPvP, Rand.RandSync randSync, int variant, params Skill[] s)
27  {
28  prefab = jobPrefab;
29  Variant = variant;
30 
31  skills = new Dictionary<Identifier, Skill>();
32  foreach (var skill in s) { skills.Add(skill.Identifier, skill); }
33  foreach (SkillPrefab skillPrefab in prefab.Skills)
34  {
35  Skill skill;
36  if (skills.ContainsKey(skillPrefab.Identifier))
37  {
38  skill = skills[skillPrefab.Identifier];
39  skills[skillPrefab.Identifier] = new Skill(skill.Identifier, skill.Level);
40  }
41  else
42  {
43  skill = new Skill(skillPrefab, isPvP, randSync);
44  skills.Add(skillPrefab.Identifier, skill);
45  }
46  if (skillPrefab.IsPrimarySkill) { PrimarySkill = skill; }
47  }
48  }
49 
50  public Job(ContentXElement element)
51  {
52  Identifier identifier = element.GetAttributeIdentifier("identifier", "");
53  JobPrefab p;
54  if (!JobPrefab.Prefabs.ContainsKey(identifier))
55  {
56  DebugConsole.ThrowError($"Could not find the job {identifier}. Giving the character a random job.",
57  contentPackage: element.ContentPackage);
58  p = JobPrefab.Random(Rand.RandSync.Unsynced);
59  }
60  else
61  {
62  p = JobPrefab.Prefabs[identifier];
63  }
64  prefab = p;
65  skills = new Dictionary<Identifier, Skill>();
66  foreach (var subElement in element.Elements())
67  {
68  if (subElement.NameAsIdentifier() != "skill") { continue; }
69  Identifier skillIdentifier = subElement.GetAttributeIdentifier("identifier", "");
70  if (skillIdentifier.IsEmpty) { continue; }
71  var skill = new Skill(skillIdentifier, subElement.GetAttributeFloat("level", 0));
72  skills.Add(skillIdentifier, skill);
73  if (skillIdentifier == prefab.PrimarySkill?.Identifier) { PrimarySkill = skill; }
74  }
75  }
76 
77  public static Job Random(bool isPvP, Rand.RandSync randSync)
78  {
79  var prefab = JobPrefab.Random(randSync);
80  int variant = Rand.Range(0, prefab.Variants, randSync);
81  return new Job(prefab, isPvP, randSync, variant);
82  }
83 
84  public IEnumerable<Skill> GetSkills()
85  {
86  return skills.Values;
87  }
88 
89  public float GetSkillLevel(Identifier skillIdentifier)
90  {
91  if (skillIdentifier.IsEmpty) { return 0.0f; }
92  skills.TryGetValue(skillIdentifier, out Skill skill);
93  return skill?.Level ?? 0.0f;
94  }
95 
96  public Skill GetSkill(Identifier skillIdentifier)
97  {
98  if (skillIdentifier.IsEmpty) { return null; }
99  skills.TryGetValue(skillIdentifier, out Skill skill);
100  return skill;
101  }
102 
103  public void OverrideSkills(Dictionary<Identifier, float> newSkills)
104  {
105  skills.Clear();
106  foreach (var newSkillInfo in newSkills)
107  {
108  var newSkill = new Skill(newSkillInfo.Key, newSkillInfo.Value);
109  if (PrimarySkill != null && newSkill.Identifier == PrimarySkill.Identifier)
110  {
111  PrimarySkill = newSkill;
112  }
113  skills.Add(newSkillInfo.Key, newSkill);
114  }
115  }
116 
117  public void IncreaseSkillLevel(Identifier skillIdentifier, float increase, bool increasePastMax)
118  {
119  if (skills.TryGetValue(skillIdentifier, out Skill skill))
120  {
121  skill.IncreaseSkill(increase, increasePastMax);
122  }
123  else
124  {
125  skills.Add(
126  skillIdentifier,
127  new Skill(skillIdentifier, increase));
128  }
129  }
130 
131  public void GiveJobItems(Character character, bool isPvPMode, WayPoint spawnPoint = null)
132  {
133  if (!prefab.JobItems.TryGetValue(Variant, out var spawnItems)) { return; }
134 
135  foreach (JobPrefab.JobItem jobItem in spawnItems)
136  {
137  //spawn the "root items" here, InitializeJobItem goes through the children recursively
138  if (jobItem.ParentItem != null) { continue; }
139  for (int i = 0; i < jobItem.Amount; i++)
140  {
141  InitializeJobItem(character, isPvPMode, jobItem, spawnItems, spawnPoint);
142  }
143  }
144 
145  if (GameMain.GameSession is { TraitorsEnabled: true } && character.IsSecurity)
146  {
147  var traitorGuidelineItem = ItemPrefab.Prefabs.Find(ip => ip.Tags.Contains(Tags.TraitorGuidelinesForSecurity));
148  Entity.Spawner.AddItemToSpawnQueue(traitorGuidelineItem, character.Inventory);
149  }
150  }
151 
152  private void InitializeJobItem(Character character, bool isPvPMode, JobPrefab.JobItem jobItem, IEnumerable<JobPrefab.JobItem> allJobItems, WayPoint spawnPoint = null, Item parentItem = null)
153  {
154  Identifier itemIdentifier = jobItem.GetItemIdentifier(character.TeamID, isPvPMode);
155  if (itemIdentifier.IsEmpty) { return; }
156  if ((MapEntityPrefab.FindByIdentifier(itemIdentifier) ?? MapEntityPrefab.FindByName(itemIdentifier.Value)) is not ItemPrefab itemPrefab)
157  {
158  DebugConsole.ThrowErrorLocalized($"Tried to spawn \"{Name}\" with the item \"{itemIdentifier}\". Matching item prefab not found.");
159  return;
160  }
161 
162  Item item = new Item(itemPrefab, character.Position, null);
163 
164 #if SERVER
165  if (GameMain.Server != null && Entity.Spawner != null)
166  {
167  if (GameMain.Server.EntityEventManager.UniqueEvents.Any(ev => ev.Entity == item))
168  {
169  string errorMsg = $"Error while spawning job items. Item {item.Name} created network events before the spawn event had been created.";
170  DebugConsole.ThrowError(errorMsg);
171  GameAnalyticsManager.AddErrorEventOnce("Job.InitializeJobItem:EventsBeforeSpawning", GameAnalyticsManager.ErrorSeverity.Error, errorMsg);
172  GameMain.Server.EntityEventManager.UniqueEvents.RemoveAll(ev => ev.Entity == item);
173  GameMain.Server.EntityEventManager.Events.RemoveAll(ev => ev.Entity == item);
174  }
175 
176  Entity.Spawner.CreateNetworkEvent(new EntitySpawner.SpawnEntity(item));
177  }
178 #endif
179 
180  if (jobItem.Equip)
181  {
182  //if the item is both pickable and wearable, try to wear it instead of picking it up
183  List<InvSlotType> allowedSlots =
184  item.GetComponents<Pickable>().Count() > 1 ?
185  new List<InvSlotType>(item.GetComponent<Wearable>()?.AllowedSlots ?? item.GetComponent<Pickable>().AllowedSlots) :
186  new List<InvSlotType>(item.AllowedSlots);
187  allowedSlots.Remove(InvSlotType.Any);
188  character.Inventory.TryPutItem(item, null, allowedSlots);
189  }
190  else
191  {
192  character.Inventory.TryPutItem(item, null, item.AllowedSlots);
193  }
194 
195  Wearable wearable = item.GetComponent<Wearable>();
196  if (wearable != null)
197  {
198  if (Variant > 0 && Variant <= wearable.Variants)
199  {
200  wearable.Variant = Variant;
201  }
202  else
203  {
204  wearable.Variant = wearable.Variant; //force server event
205  if (wearable.Variants > 0 && Variant == 0)
206  {
207  //set variant to the same as the wearable to get the rest of the character's gear
208  //to use the same variant (if possible)
209  Variant = wearable.Variant;
210  }
211  }
212  }
213 
214  IdCard idCardComponent = item.GetComponent<IdCard>();
215  idCardComponent?.Initialize(spawnPoint, character);
216 
217  foreach (WifiComponent wifiComponent in item.GetComponents<WifiComponent>())
218  {
219  wifiComponent.TeamID = character.TeamID;
220  }
221 
222  parentItem?.Combine(item, user: null);
223 
224  foreach (JobPrefab.JobItem childItem in allJobItems)
225  {
226  if (childItem.ParentItem == jobItem)
227  {
228  for (int i = 0; i < childItem.Amount; i++)
229  {
230  InitializeJobItem(character, isPvPMode, childItem, allJobItems, spawnPoint, parentItem: item);
231  }
232  }
233  }
234  }
235 
236  public XElement Save(XElement parentElement)
237  {
238  XElement jobElement = new XElement("job");
239 
240  jobElement.Add(new XAttribute("name", Name));
241  jobElement.Add(new XAttribute("identifier", prefab.Identifier));
242 
243  foreach (KeyValuePair<Identifier, Skill> skill in skills)
244  {
245  jobElement.Add(new XElement("skill", new XAttribute("identifier", skill.Value.Identifier), new XAttribute("level", skill.Value.Level)));
246  }
247 
248  parentElement.Add(jobElement);
249  return jobElement;
250  }
251  }
252 }
override bool TryPutItem(Item item, Character user, IEnumerable< InvSlotType > allowedSlots=null, bool createNetworkEvent=true, bool ignoreCondition=false)
If there is room, puts the item in the inventory and returns true, otherwise returns false
ContentPackage? ContentPackage
Identifier GetAttributeIdentifier(string key, string def)
static EntitySpawner Spawner
Definition: Entity.cs:31
void AddItemToSpawnQueue(ItemPrefab itemPrefab, Vector2 worldPosition, float? condition=null, int? quality=null, Action< Item > onSpawned=null)
static GameSession?? GameSession
Definition: GameMain.cs:88
static readonly PrefabCollection< ItemPrefab > Prefabs
List< InvSlotType > AllowedSlots
Definition: Pickable.cs:25
Skill PrimarySkill
Definition: Job.cs:22
IEnumerable< Skill > GetSkills()
Definition: Job.cs:84
int Variant
Definition: Job.cs:20
LocalizedString Description
Definition: Job.cs:16
XElement Save(XElement parentElement)
Definition: Job.cs:236
void GiveJobItems(Character character, bool isPvPMode, WayPoint spawnPoint=null)
Definition: Job.cs:131
Skill GetSkill(Identifier skillIdentifier)
Definition: Job.cs:96
void OverrideSkills(Dictionary< Identifier, float > newSkills)
Definition: Job.cs:103
Job(JobPrefab jobPrefab, bool isPvP)
Definition: Job.cs:24
Job(JobPrefab jobPrefab, bool isPvP, Rand.RandSync randSync, int variant, params Skill[] s)
Definition: Job.cs:26
LocalizedString Name
Definition: Job.cs:14
static Job Random(bool isPvP, Rand.RandSync randSync)
Definition: Job.cs:77
Job(ContentXElement element)
Definition: Job.cs:50
float GetSkillLevel(Identifier skillIdentifier)
Definition: Job.cs:89
void IncreaseSkillLevel(Identifier skillIdentifier, float increase, bool increasePastMax)
Definition: Job.cs:117
static JobPrefab Random(Rand.RandSync sync, Func< JobPrefab, bool > predicate=null)
static readonly PrefabCollection< JobPrefab > Prefabs
readonly Identifier Identifier
Definition: Skill.cs:7
float Level
Definition: Skill.cs:19
readonly Identifier Identifier
Definition: SkillPrefab.cs:7