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) : this(jobPrefab, randSync: Rand.RandSync.Unsynced, variant: 0) { }
25 
26  public Job(JobPrefab jobPrefab, 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, 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(Rand.RandSync randSync)
78  {
79  var prefab = JobPrefab.Random(randSync);
80  var variant = Rand.Range(0, prefab.Variants, randSync);
81  return new Job(prefab, 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, WayPoint spawnPoint = null)
132  {
133  if (!prefab.ItemSets.TryGetValue(Variant, out var spawnItems)) { return; }
134 
135  foreach (XElement itemElement in spawnItems.GetChildElements("Item"))
136  {
137  InitializeJobItem(character, itemElement, spawnPoint);
138  }
139 
140  if (GameMain.GameSession is { TraitorsEnabled: true } && character.IsSecurity)
141  {
142  var traitorGuidelineItem = ItemPrefab.Prefabs.Find(ip => ip.Tags.Contains(Tags.TraitorGuidelinesForSecurity));
143  Entity.Spawner.AddItemToSpawnQueue(traitorGuidelineItem, character.Inventory);
144  }
145  }
146 
147  private void InitializeJobItem(Character character, XElement itemElement, WayPoint spawnPoint = null, Item parentItem = null)
148  {
149  ItemPrefab itemPrefab;
150  if (itemElement.Attribute("name") != null)
151  {
152  string itemName = itemElement.Attribute("name").Value;
153  DebugConsole.ThrowErrorLocalized("Error in Job config (" + Name + ") - use item identifiers instead of names to configure the items.");
154  itemPrefab = MapEntityPrefab.FindByName(itemName) as ItemPrefab;
155  if (itemPrefab == null)
156  {
157  DebugConsole.ThrowErrorLocalized("Tried to spawn \"" + Name + "\" with the item \"" + itemName + "\". Matching item prefab not found.");
158  return;
159  }
160  }
161  else
162  {
163  string itemIdentifier = itemElement.GetAttributeString("identifier", "");
164  itemPrefab = MapEntityPrefab.FindByIdentifier(itemIdentifier.ToIdentifier()) as ItemPrefab;
165  if (itemPrefab == null)
166  {
167  DebugConsole.ThrowErrorLocalized("Tried to spawn \"" + Name + "\" with the item \"" + itemIdentifier + "\". Matching item prefab not found.");
168  return;
169  }
170  }
171 
172  Item item = new Item(itemPrefab, character.Position, null);
173 
174 #if SERVER
175  if (GameMain.Server != null && Entity.Spawner != null)
176  {
177  if (GameMain.Server.EntityEventManager.UniqueEvents.Any(ev => ev.Entity == item))
178  {
179  string errorMsg = $"Error while spawning job items. Item {item.Name} created network events before the spawn event had been created.";
180  DebugConsole.ThrowError(errorMsg);
181  GameAnalyticsManager.AddErrorEventOnce("Job.InitializeJobItem:EventsBeforeSpawning", GameAnalyticsManager.ErrorSeverity.Error, errorMsg);
182  GameMain.Server.EntityEventManager.UniqueEvents.RemoveAll(ev => ev.Entity == item);
183  GameMain.Server.EntityEventManager.Events.RemoveAll(ev => ev.Entity == item);
184  }
185 
186  Entity.Spawner.CreateNetworkEvent(new EntitySpawner.SpawnEntity(item));
187  }
188 #endif
189 
190  if (itemElement.GetAttributeBool("equip", false))
191  {
192  //if the item is both pickable and wearable, try to wear it instead of picking it up
193  List<InvSlotType> allowedSlots =
194  item.GetComponents<Pickable>().Count() > 1 ?
195  new List<InvSlotType>(item.GetComponent<Wearable>()?.AllowedSlots ?? item.GetComponent<Pickable>().AllowedSlots) :
196  new List<InvSlotType>(item.AllowedSlots);
197  allowedSlots.Remove(InvSlotType.Any);
198  character.Inventory.TryPutItem(item, null, allowedSlots);
199  }
200  else
201  {
202  character.Inventory.TryPutItem(item, null, item.AllowedSlots);
203  }
204 
205  Wearable wearable = item.GetComponent<Wearable>();
206  if (wearable != null)
207  {
208  if (Variant > 0 && Variant <= wearable.Variants)
209  {
210  wearable.Variant = Variant;
211  }
212  else
213  {
214  wearable.Variant = wearable.Variant; //force server event
215  if (wearable.Variants > 0 && Variant == 0)
216  {
217  //set variant to the same as the wearable to get the rest of the character's gear
218  //to use the same variant (if possible)
219  Variant = wearable.Variant;
220  }
221  }
222  }
223 
224  IdCard idCardComponent = item.GetComponent<IdCard>();
225  idCardComponent?.Initialize(spawnPoint, character);
226 
227  foreach (WifiComponent wifiComponent in item.GetComponents<WifiComponent>())
228  {
229  wifiComponent.TeamID = character.TeamID;
230  }
231 
232  if (parentItem != null) { parentItem.Combine(item, user: null); }
233 
234  foreach (XElement childItemElement in itemElement.Elements())
235  {
236  InitializeJobItem(character, childItemElement, spawnPoint, item);
237  }
238  }
239 
240  public XElement Save(XElement parentElement)
241  {
242  XElement jobElement = new XElement("job");
243 
244  jobElement.Add(new XAttribute("name", Name));
245  jobElement.Add(new XAttribute("identifier", prefab.Identifier));
246 
247  foreach (KeyValuePair<Identifier, Skill> skill in skills)
248  {
249  jobElement.Add(new XElement("skill", new XAttribute("identifier", skill.Value.Identifier), new XAttribute("level", skill.Value.Level)));
250  }
251 
252  parentElement.Add(jobElement);
253  return jobElement;
254  }
255  }
256 }
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:240
Skill GetSkill(Identifier skillIdentifier)
Definition: Job.cs:96
Job(JobPrefab jobPrefab)
Definition: Job.cs:24
static Job Random(Rand.RandSync randSync)
Definition: Job.cs:77
Job(JobPrefab jobPrefab, Rand.RandSync randSync, int variant, params Skill[] s)
Definition: Job.cs:26
void OverrideSkills(Dictionary< Identifier, float > newSkills)
Definition: Job.cs:103
void GiveJobItems(Character character, WayPoint spawnPoint=null)
Definition: Job.cs:131
LocalizedString Name
Definition: Job.cs:14
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
static MapEntityPrefab FindByIdentifier(Identifier identifier)
readonly Identifier Identifier
Definition: Skill.cs:7
float Level
Definition: Skill.cs:19
readonly Identifier Identifier
Definition: SkillPrefab.cs:8