2 using System.Collections.Generic;
12 private readonly Dictionary<Identifier, Skill> skills;
24 public Job(
JobPrefab jobPrefab,
bool isPvP) : this(jobPrefab, isPvP, randSync: Rand.RandSync.Unsynced, variant: 0) { }
26 public Job(
JobPrefab jobPrefab,
bool isPvP, Rand.RandSync randSync,
int variant, params
Skill[] s)
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, isPvP, 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; }
77 public static Job Random(
bool isPvP, Rand.RandSync randSync)
80 int variant = Rand.Range(0, prefab.Variants, randSync);
81 return new Job(prefab, isPvP, 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.JobItems.TryGetValue(
Variant, out var spawnItems)) {
return; }
138 if (jobItem.ParentItem !=
null) {
continue; }
139 for (
int i = 0; i < jobItem.Amount; i++)
141 InitializeJobItem(character, isPvPMode, jobItem, spawnItems, spawnPoint);
147 var traitorGuidelineItem =
ItemPrefab.
Prefabs.Find(ip => ip.Tags.Contains(Tags.TraitorGuidelinesForSecurity));
152 private void InitializeJobItem(
Character character,
bool isPvPMode,
JobPrefab.
JobItem jobItem, IEnumerable<JobPrefab.JobItem> allJobItems,
WayPoint spawnPoint =
null,
Item parentItem =
null)
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)
158 DebugConsole.ThrowErrorLocalized($
"Tried to spawn \"{Name}\" with the item \"{itemIdentifier}\". Matching item prefab not found.");
165 if (GameMain.Server !=
null && Entity.Spawner !=
null)
167 if (GameMain.Server.EntityEventManager.UniqueEvents.Any(ev => ev.Entity == item))
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);
176 Entity.Spawner.CreateNetworkEvent(
new EntitySpawner.SpawnEntity(item));
183 List<InvSlotType> allowedSlots =
184 item.GetComponents<
Pickable>().Count() > 1 ?
196 if (wearable !=
null)
215 idCardComponent?.
Initialize(spawnPoint, character);
222 parentItem?.Combine(item, user:
null);
224 foreach (JobPrefab.JobItem childItem in allJobItems)
226 if (childItem.ParentItem == jobItem)
228 for (
int i = 0; i < childItem.Amount; i++)
230 InitializeJobItem(character, isPvPMode, childItem, allJobItems, spawnPoint, parentItem: item);
236 public XElement
Save(XElement parentElement)
238 XElement jobElement =
new XElement(
"job");
240 jobElement.Add(
new XAttribute(
"name",
Name));
241 jobElement.Add(
new XAttribute(
"identifier", prefab.Identifier));
243 foreach (KeyValuePair<Identifier, Skill> skill
in skills)
245 jobElement.Add(
new XElement(
"skill",
new XAttribute(
"identifier", skill.Value.Identifier),
new XAttribute(
"level", skill.Value.Level)));
248 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)
void GiveJobItems(Character character, bool isPvPMode, WayPoint spawnPoint=null)
Skill GetSkill(Identifier skillIdentifier)
void OverrideSkills(Dictionary< Identifier, float > newSkills)
Job(JobPrefab jobPrefab, bool isPvP)
Job(JobPrefab jobPrefab, bool isPvP, Rand.RandSync randSync, int variant, params Skill[] s)
static Job Random(bool isPvP, Rand.RandSync randSync)
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
readonly Identifier Identifier
readonly Identifier Identifier