3 private static bool m_Initialized;
4 static ref PlayerSpawnJsonData m_Data =
new PlayerSpawnJsonData();
9 if (!spawnGearPresetFiles || (spawnGearPresetFiles && spawnGearPresetFiles.Count() == 0))
14 foreach (
string spawnPresetFile : spawnGearPresetFiles)
17 string path =
"$mission:" + spawnPresetFile;
20 if (!JsonFileLoader<PlayerSpawnPreset>.LoadFile(
path, preset, errorMessage))
27 m_Data.presets.Insert(preset);
35 static bool IsInitialized()
43 int count =
m_Data.presets.Count();
45 for (
int i = 0; i < count; i++)
50 for (
int j = 0; j < p.spawnWeight; j++)
52 weightedPresetIndexes.Insert(i);
57 return m_Data.presets.Get(weightedPresetIndexes.GetRandomElement());
65 ProcessSlotsEquipment(player, data);
66 ProcessCargoEquipment(player, data);
75 if (!data.HasAttachmentSlotSetsDefined())
77 Debug.Log(
"No non-empty 'attachmentSlotItemSets' array found. Skipping slot spawns",
"n/a",
"n/a",
"ProcessSlotsEquipment");
81 foreach (PlayerSpawnPresetSlotData slotData : data.attachmentSlotItemSets)
83 SelectAndSpawnSlotEquipment(player,slotData);
88 private static bool SelectAndSpawnSlotEquipment(
PlayerBase player, PlayerSpawnPresetSlotData slotData)
91 if (!slotData.TranslateAndValidateSlot(player,slotID))
94 if (!slotData.IsValid())
98 int count = slotData.discreteItemSets.Count();
100 for (
int i = 0; i < count; i++)
102 dis = slotData.discreteItemSets[i];
106 for (
int j = 0; j < dis.spawnWeight; j++)
108 weightedDiscreteSetIndexes.Insert(i);
114 if (weightedDiscreteSetIndexes.Count() > 0)
115 dis = slotData.discreteItemSets.Get(weightedDiscreteSetIndexes.GetRandomElement());
116 return SpawnDiscreteSlotItemSet(player,dis,slotID);
122 if (!data.HasDiscreteUnsortedItemSetsDefined())
124 Debug.Log(
"No non-empty 'discreteUnsortedItemSets' array found. Skipping cargo spawns",
"n/a",
"n/a",
"ProcessCargoEquipment");
128 SelectAndSpawnCargoSet(player,data);
134 int count = data.discreteUnsortedItemSets.Count();
135 PlayerSpawnPresetDiscreteCargoSetData csd;
136 for (
int i = 0; i < count; i++)
138 csd = data.discreteUnsortedItemSets[i];
142 for (
int j = 0; j < csd.spawnWeight; j++)
144 weightedDiscreteSetIndexes.Insert(i);
150 if (weightedDiscreteSetIndexes.Count() > 0)
151 csd = data.discreteUnsortedItemSets.Get(weightedDiscreteSetIndexes.GetRandomElement());
152 return SpawnDiscreteCargoItemSet(player,csd);
155 private static bool SpawnDiscreteCargoItemSet(
PlayerBase player, PlayerSpawnPresetDiscreteCargoSetData csd)
157 SpawnComplexChildrenItems(player,csd);
158 SpawnSimpleChildrenItems(player,csd);
166 Debug.Log(
"No PlayerSpawnPresetDiscreteItemSet found. Skipping spawn for slot: " +
InventorySlots.GetSlotName(slotID),
"n/a",
"n/a",
"SpawnDiscreteSlotItemSet");
172 item =
ItemBase.Cast(player.GetHumanInventory().CreateInHands(dis.itemType));
174 item =
ItemBase.Cast(player.GetInventory().CreateAttachmentEx(dis.itemType,slotID));
178 HandleNewItem(item,dis);
180 else if (dis.itemType !=
string.Empty)
182 Debug.Log(
"FAILED spawning item type: " + dis.itemType +
" into slot: " +
InventorySlots.GetSlotName(slotID) +
" of parent: " + player,
"n/a",
"n/a",
"SpawnDiscreteSlotItemSet");
192 if (!data.complexChildrenTypes || data.complexChildrenTypes.Count() < 1)
200 if (cct.itemType ==
string.Empty)
202 Debug.Log(
"Empty item type found in 'complexChildrenTypes' of parent : " + parent,
"n/a",
"n/a",
"SpawnSimpleChildrenItems");
207 Class.CastTo(item,CreateChildItem(parent,cct.itemType));
211 HandleNewItem(item,cct);
215 Debug.Log(
"FAILED spawning item: " + cct.itemType +
" of parent: " + parent,
"n/a",
"n/a",
"SpawnComplexChildrenItems");
224 if (!data || !data.simpleChildrenTypes || data.simpleChildrenTypes.Count() < 1)
229 int count = data.simpleChildrenTypes.Count();
231 for (
int i = 0; i < count; i++)
233 itemType = data.simpleChildrenTypes[i];
234 if (itemType ==
string.
Empty)
236 Debug.Log(
"Empty item type found at idx: " + i.ToString() +
" of 'simpleChildrenTypes' array. Skipping",
"n/a",
"n/a",
"SpawnSimpleChildrenItems");
241 Class.CastTo(item,CreateChildItem(parent,itemType));
245 if (!data.simpleChildrenUseDefaultAttributes)
246 ApplyAttributes(item,data.attributes);
250 Debug.Log(
"FAILED spawning item type: " + itemType +
" to parent: " + parent,
"n/a",
"n/a",
"SpawnSimpleChildrenItems");
258 ApplyAttributes(item,data.attributes);
261 if (
Class.CastTo(player,item.GetHierarchyRootPlayer()) && data.GetQuickbarIdx() > -1)
262 player.SetQuickBarEntityShortcut(item,data.GetQuickbarIdx());
264 SpawnComplexChildrenItems(item,data);
265 SpawnSimpleChildrenItems(item,data);
272 if (
Class.CastTo(player,parent))
274 int count = player.GetInventory().AttachmentCount();
275 if (
Class.CastTo(newItem,player.GetInventory().CreateInInventory(type)))
278 Debug.Log(
"FAILED spawning item: " + type +
", it fits in no cargo or attachment on any worn item",
"n/a",
"n/a",
"CreateChildItem");
286 if (
Class.CastTo(wep,parent))
287 return wep.SpawnAttachedMagazine(type);
290 return parent.GetInventory().CreateInInventory(type);
293 private static void ApplyAttributes(
ItemBase item, PlayerSpawnAttributesData attributes)
298 float health01 =
Math.RandomFloatInclusive(attributes.healthMin,attributes.healthMax);
299 item.SetHealth01(
"",
"Health",health01);
301 float quantity01 =
Math.RandomFloatInclusive(attributes.quantityMin,attributes.quantityMax);
302 if (item.IsMagazine())
304 Magazine mag = Magazine.Cast(item);
317 int ammoQuantity = (
int)
Math.Lerp(0,mag.GetAmmoMax(),quantity01);
318 mag.ServerSetAmmoCount(ammoQuantity);
323 float quantityAbsolute =
Math.Lerp(item.GetQuantityMin(),item.GetQuantityMax(),quantity01);
324 quantityAbsolute =
Math.Round(quantityAbsolute);
325 if (quantityAbsolute <= item.GetQuantityMin() && item.ConfigGetBool(
"varQuantityDestroyOnMin"))
327 item.SetQuantity(quantityAbsolute);