2 using System.Collections.Generic;
12 private readonly Dictionary<Identifier, Skill> skills;
24 public Job(
JobPrefab jobPrefab) : this(jobPrefab, randSync: Rand.RandSync.Unsynced, variant: 0) { }
31 skills =
new Dictionary<Identifier, Skill>();
32 foreach (var skill
in s) { skills.Add(skill.Identifier, skill); }
36 if (skills.ContainsKey(skillPrefab.
Identifier))
43 skill =
new Skill(skillPrefab, randSync);
56 DebugConsole.ThrowError($
"Could not find the job {identifier}. Giving the character a random job.",
65 skills =
new Dictionary<Identifier, Skill>();
66 foreach (var subElement
in element.Elements())
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; }
80 var variant = Rand.Range(0, prefab.Variants, randSync);
81 return new Job(prefab, randSync, variant);
91 if (skillIdentifier.IsEmpty) {
return 0.0f; }
92 skills.TryGetValue(skillIdentifier, out
Skill skill);
93 return skill?.Level ?? 0.0f;
98 if (skillIdentifier.IsEmpty) {
return null; }
99 skills.TryGetValue(skillIdentifier, out
Skill skill);
106 foreach (var newSkillInfo
in newSkills)
108 var newSkill =
new Skill(newSkillInfo.Key, newSkillInfo.Value);
113 skills.Add(newSkillInfo.Key, newSkill);
119 if (skills.TryGetValue(skillIdentifier, out
Skill skill))
121 skill.IncreaseSkill(increase, increasePastMax);
127 new Skill(skillIdentifier, increase));
133 if (!prefab.ItemSets.TryGetValue(
Variant, out var spawnItems)) {
return; }
135 foreach (XElement itemElement
in spawnItems.GetChildElements(
"Item"))
137 InitializeJobItem(character, itemElement, spawnPoint);
142 var traitorGuidelineItem =
ItemPrefab.
Prefabs.Find(ip => ip.Tags.Contains(Tags.TraitorGuidelinesForSecurity));
147 private void InitializeJobItem(
Character character, XElement itemElement,
WayPoint spawnPoint =
null,
Item parentItem =
null)
150 if (itemElement.Attribute(
"name") !=
null)
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.");
155 if (itemPrefab ==
null)
157 DebugConsole.ThrowErrorLocalized(
"Tried to spawn \"" +
Name +
"\" with the item \"" + itemName +
"\". Matching item prefab not found.");
163 string itemIdentifier = itemElement.GetAttributeString(
"identifier",
"");
164 itemPrefab = MapEntityPrefab.
FindByIdentifier(itemIdentifier.ToIdentifier()) as ItemPrefab;
165 if (itemPrefab ==
null)
167 DebugConsole.ThrowErrorLocalized(
"Tried to spawn \"" +
Name +
"\" with the item \"" + itemIdentifier +
"\". Matching item prefab not found.");
175 if (GameMain.Server !=
null && Entity.Spawner !=
null)
177 if (GameMain.Server.EntityEventManager.UniqueEvents.Any(ev => ev.Entity == item))
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);
186 Entity.Spawner.CreateNetworkEvent(
new EntitySpawner.SpawnEntity(item));
190 if (itemElement.GetAttributeBool(
"equip",
false))
193 List<InvSlotType> allowedSlots =
194 item.GetComponents<
Pickable>().Count() > 1 ?
206 if (wearable !=
null)
225 idCardComponent?.
Initialize(spawnPoint, character);
232 if (parentItem !=
null) { parentItem.Combine(item, user:
null); }
234 foreach (XElement childItemElement
in itemElement.Elements())
236 InitializeJobItem(character, childItemElement, spawnPoint, item);
240 public XElement
Save(XElement parentElement)
242 XElement jobElement =
new XElement(
"job");
244 jobElement.Add(
new XAttribute(
"name",
Name));
245 jobElement.Add(
new XAttribute(
"identifier", prefab.Identifier));
247 foreach (KeyValuePair<Identifier, Skill> skill
in skills)
249 jobElement.Add(
new XElement(
"skill",
new XAttribute(
"identifier", skill.Value.Identifier),
new XAttribute(
"level", skill.Value.Level)));
252 parentElement.Add(jobElement);
CharacterInventory Inventory
override Vector2 Position
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
void AddItemToSpawnQueue(ItemPrefab itemPrefab, Vector2 worldPosition, float? condition=null, int? quality=null, Action< Item > onSpawned=null)
static GameSession?? GameSession
static readonly PrefabCollection< ItemPrefab > Prefabs
void Initialize(WayPoint spawnPoint, Character character)
List< InvSlotType > AllowedSlots
IEnumerable< Skill > GetSkills()
LocalizedString Description
XElement Save(XElement parentElement)
Skill GetSkill(Identifier skillIdentifier)
static Job Random(Rand.RandSync randSync)
Job(JobPrefab jobPrefab, Rand.RandSync randSync, int variant, params Skill[] s)
void OverrideSkills(Dictionary< Identifier, float > newSkills)
void GiveJobItems(Character character, WayPoint spawnPoint=null)
Job(ContentXElement element)
float GetSkillLevel(Identifier skillIdentifier)
void IncreaseSkillLevel(Identifier skillIdentifier, float increase, bool increasePastMax)
static JobPrefab Random(Rand.RandSync sync, Func< JobPrefab, bool > predicate=null)
static readonly PrefabCollection< JobPrefab > Prefabs
static MapEntityPrefab FindByName(string name)
static MapEntityPrefab FindByIdentifier(Identifier identifier)
readonly Identifier Identifier
readonly Identifier Identifier