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());
64 if (preset.IsValid() && preset.name == presetName)
76 ProcessSlotsEquipment(player, data);
77 ProcessCargoEquipment(player, data);
86 if (!data.HasAttachmentSlotSetsDefined())
88 Debug.Log(
"No non-empty 'attachmentSlotItemSets' array found. Skipping slot spawns",
"n/a",
"n/a",
"ProcessSlotsEquipment");
92 foreach (PlayerSpawnPresetSlotData slotData : data.attachmentSlotItemSets)
94 SelectAndSpawnSlotEquipment(player,slotData);
99 private static bool SelectAndSpawnSlotEquipment(
PlayerBase player, PlayerSpawnPresetSlotData slotData)
102 if (!slotData.TranslateAndValidateSlot(player,slotID))
105 if (!slotData.IsValid())
109 int count = slotData.discreteItemSets.Count();
111 for (
int i = 0; i < count; i++)
113 dis = slotData.discreteItemSets[i];
117 for (
int j = 0; j < dis.spawnWeight; j++)
119 weightedDiscreteSetIndexes.Insert(i);
125 if (weightedDiscreteSetIndexes.Count() > 0)
126 dis = slotData.discreteItemSets.Get(weightedDiscreteSetIndexes.GetRandomElement());
127 return SpawnDiscreteSlotItemSet(player,dis,slotID);
133 if (!data.HasDiscreteUnsortedItemSetsDefined())
135 Debug.Log(
"No non-empty 'discreteUnsortedItemSets' array found. Skipping cargo spawns",
"n/a",
"n/a",
"ProcessCargoEquipment");
139 SelectAndSpawnCargoSet(player,data);
145 int count = data.discreteUnsortedItemSets.Count();
146 PlayerSpawnPresetDiscreteCargoSetData csd;
147 for (
int i = 0; i < count; i++)
149 csd = data.discreteUnsortedItemSets[i];
153 for (
int j = 0; j < csd.spawnWeight; j++)
155 weightedDiscreteSetIndexes.Insert(i);
161 if (weightedDiscreteSetIndexes.Count() > 0)
162 csd = data.discreteUnsortedItemSets.Get(weightedDiscreteSetIndexes.GetRandomElement());
163 return SpawnDiscreteCargoItemSet(player,csd);
166 private static bool SpawnDiscreteCargoItemSet(
PlayerBase player, PlayerSpawnPresetDiscreteCargoSetData csd)
168 SpawnComplexChildrenItems(player,csd);
169 SpawnSimpleChildrenItems(player,csd);
177 Debug.Log(
"No PlayerSpawnPresetDiscreteItemSet found. Skipping spawn for slot: " +
InventorySlots.GetSlotName(slotID),
"n/a",
"n/a",
"SpawnDiscreteSlotItemSet");
183 item =
ItemBase.Cast(player.GetHumanInventory().CreateInHands(dis.itemType));
185 item =
ItemBase.Cast(player.GetInventory().CreateAttachmentEx(dis.itemType,slotID));
189 HandleNewItem(item,dis);
191 else if (dis.itemType !=
string.Empty)
193 Debug.Log(
"FAILED spawning item type: " + dis.itemType +
" into slot: " +
InventorySlots.GetSlotName(slotID) +
" of parent: " + player,
"n/a",
"n/a",
"SpawnDiscreteSlotItemSet");
203 if (!data.complexChildrenTypes || data.complexChildrenTypes.Count() < 1)
210 if (cct.itemType ==
string.Empty)
212 Debug.Log(
"Empty item type found in 'complexChildrenTypes' of parent : " + parent,
"n/a",
"n/a",
"SpawnSimpleChildrenItems");
217 Class.CastTo(item,CreateChildItem(parent,cct.itemType));
221 HandleNewItem(item,cct);
226 if (!
Class.CastTo(wep,parent) || !IsWeaponAndMagazineType(parent,cct.itemType) || !wep.HasInternalMagazine(-1))
227 Debug.Log(
"FAILED spawning item: " + cct.itemType +
" of parent: " + parent,
"n/a",
"n/a",
"SpawnComplexChildrenItems");
236 if (!data || !data.simpleChildrenTypes || data.simpleChildrenTypes.Count() < 1)
241 int count = data.simpleChildrenTypes.Count();
243 for (
int i = 0; i < count; i++)
245 itemType = data.simpleChildrenTypes[i];
246 if (itemType ==
string.
Empty)
248 Debug.Log(
"Empty item type found at idx: " + i.ToString() +
" of 'simpleChildrenTypes' array. Skipping",
"n/a",
"n/a",
"SpawnSimpleChildrenItems");
253 Class.CastTo(item,CreateChildItem(parent,itemType));
257 if (!data.simpleChildrenUseDefaultAttributes)
258 ApplyAttributes(item,data.attributes);
263 if (!
Class.CastTo(wep,parent) || !IsWeaponAndMagazineType(parent,itemType) || !wep.HasInternalMagazine(-1))
264 Debug.Log(
"FAILED spawning item type: " + itemType +
" to parent: " + parent,
"n/a",
"n/a",
"SpawnSimpleChildrenItems");
272 ApplyAttributes(item,data.attributes);
275 if (
Class.CastTo(player,item.GetHierarchyRootPlayer()) && data.GetQuickbarIdx() > -1)
276 player.SetQuickBarEntityShortcut(item,data.GetQuickbarIdx());
278 SpawnComplexChildrenItems(item,data);
279 SpawnSimpleChildrenItems(item,data);
286 if (
Class.CastTo(player,parent))
288 if (
Class.CastTo(newItem,player.GetInventory().CreateInInventory(type)))
291 Debug.Log(
"FAILED spawning item: " + type +
", it fits in no cargo or attachment on any worn item",
"n/a",
"n/a",
"CreateChildItem");
299 if (
Class.CastTo(wep,parent) && wep.SpawnAmmo(type) && !wep.HasInternalMagazine(-1))
302 int muzzleCount = wep.GetMuzzleCount();
303 for (
int i = 0; i < muzzleCount; i++)
305 if (
Class.CastTo(mag,wep.GetMagazine(i)) && mag.GetType() == type)
313 return parent.GetInventory().CreateInInventory(type);
316 private static void ApplyAttributes(
ItemBase item, PlayerSpawnAttributesData attributes)
321 float health01 =
Math.RandomFloatInclusive(attributes.healthMin,attributes.healthMax);
322 item.SetHealth01(
"",
"Health",health01);
324 float quantity01 =
Math.RandomFloatInclusive(attributes.quantityMin,attributes.quantityMax);
325 if (item.IsMagazine())
327 Magazine mag = Magazine.Cast(item);
339 int ammoQuantity = (
int)
Math.Lerp(0,mag.GetAmmoMax(),quantity01);
340 mag.ServerSetAmmoCount(ammoQuantity);
345 float quantityAbsolute =
Math.Lerp(item.GetQuantityMin(),item.GetQuantityMax(),quantity01);
346 quantityAbsolute =
Math.Round(quantityAbsolute);
347 if (quantityAbsolute <= item.GetQuantityMin() && item.ConfigGetBool(
"varQuantityDestroyOnMin"))
349 item.SetQuantity(quantityAbsolute);
354 private static bool IsWeaponAndMagazineType(
EntityAI parent,
string type)