DayZ 1.24
Loading...
Searching...
No Matches
CfgPlayerSpawnHandler.c
Go to the documentation of this file.
2{
3 private static bool m_Initialized;
5
6 static bool LoadData()
7 {
9 if (!spawnGearPresetFiles || (spawnGearPresetFiles && spawnGearPresetFiles.Count() == 0))
10 return false;
11
12 m_Data.presets = {};
13
14 foreach (string spawnPresetFile : spawnGearPresetFiles)
15 {
17 string path = "$mission:" + spawnPresetFile;
18
19 string errorMessage;
21 {
23 return false;
24 }
25
26 if (preset != null)
27 m_Data.presets.Insert(preset);
28 }
29
30 m_Initialized = m_Data.presets.Count() > 0;
31
32 return true;
33 }
34
35 static bool IsInitialized()
36 {
37 return m_Initialized;
38 }
39
41 {
43 int count = m_Data.presets.Count();
45 for (int i = 0; i < count; i++)
46 {
47 p = m_Data.presets[i];
48 if (p.IsValid())
49 {
50 for (int j = 0; j < p.spawnWeight; j++)
52 }
53 }
54
55 return m_Data.presets.Get(weightedPresetIndexes.GetRandomElement());
56 }
57
60 {
61 if (data.IsValid())
62 {
65 }
66
67 return true;
68 }
69
72 {
73 if (!data.HasAttachmentSlotSetsDefined())
74 {
75 Debug.Log("No non-empty 'attachmentSlotItemSets' array found. Skipping slot spawns", "n/a", "n/a", "ProcessSlotsEquipment");
76 return;
77 }
78
79 foreach (PlayerSpawnPresetSlotData slotData : data.attachmentSlotItemSets)
81 }
82
85 {
86 int slotID;
87 if (!slotData.TranslateAndValidateSlot(player, slotID))
88 return false;
89
90 if (!slotData.IsValid())
91 return false;
92
94 int count = slotData.discreteItemSets.Count();
96 for (int i = 0; i < count; i++)
97 {
98 dis = slotData.discreteItemSets[i];
99
100 if (dis.IsValid()) //only when the type exists and spawnWeight is set
101 {
102 for (int j = 0; j < dis.spawnWeight; j++)
104 }
105 }
106
107 dis = null;
108 if (weightedDiscreteSetIndexes.Count() > 0)
109 dis = slotData.discreteItemSets.Get(weightedDiscreteSetIndexes.GetRandomElement());
111 }
112
115 {
116 if (!data.HasDiscreteUnsortedItemSetsDefined())
117 {
118 Debug.Log("No non-empty 'discreteUnsortedItemSets' array found. Skipping cargo spawns", "n/a", "n/a", "ProcessCargoEquipment");
119 return;
120 }
121
123 }
124
126 {
128 int count = data.discreteUnsortedItemSets.Count();
130 for (int i = 0; i < count; i++)
131 {
132 csd = data.discreteUnsortedItemSets[i];
133
134 if (csd.IsValid()) //only when the spawnWeight is set
135 {
136 for (int j = 0; j < csd.spawnWeight; j++)
138 }
139 }
140
141 csd = null;
142 if (weightedDiscreteSetIndexes.Count() > 0)
143 csd = data.discreteUnsortedItemSets.Get(weightedDiscreteSetIndexes.GetRandomElement());
145 }
146
153
155 {
156 if (!dis)
157 {
158 Debug.Log("No PlayerSpawnPresetDiscreteItemSet found. Skipping spawn for slot: " + InventorySlots.GetSlotName(slotID), "n/a", "n/a", "SpawnDiscreteSlotItemSet");
159 return false;
160 }
161
163 if (slotID == InventorySlots.HANDS) //hands exception
164 item = ItemBase.Cast(player.GetHumanInventory().CreateInHands(dis.itemType));
165 else
166 item = ItemBase.Cast(player.GetInventory().CreateAttachmentEx(dis.itemType, slotID));
167
168 if (item)
170 else if (dis.itemType != string.Empty)
171 {
172 Debug.Log("FAILED spawning item type: " + dis.itemType + " into slot: " + InventorySlots.GetSlotName(slotID) + " of parent: " + player, "n/a", "n/a", "SpawnDiscreteSlotItemSet");
173 return false;
174 }
175
176 return item != null;
177 }
178
181 {
182 if (!data.complexChildrenTypes || data.complexChildrenTypes.Count() < 1) //no children defined, still valid!
183 return false;
184
185 foreach (PlayerSpawnPresetComplexChildrenType cct : data.complexChildrenTypes)
186 {
187 //todo: slotID option over nyah?
188 if (cct.itemType == string.Empty)
189 {
190 Debug.Log("Empty item type found in 'complexChildrenTypes' of parent : " + parent, "n/a", "n/a", "SpawnSimpleChildrenItems");
191 continue;
192 }
193
195 Class.CastTo(item, CreateChildItem(parent, cct.itemType));
196
197 if (item)
199 else
200 Debug.Log("FAILED spawning item: " + cct.itemType + " of parent: " + parent, "n/a", "n/a", "SpawnComplexChildrenItems");
201 }
202
203 return true;
204 }
205
207 {
208 if (!data || !data.simpleChildrenTypes || data.simpleChildrenTypes.Count() < 1) //no children defined, still valid!
209 return false;
210
211 int count = data.simpleChildrenTypes.Count();
212 string itemType;
213 for (int i = 0; i < count; i++)
214 {
215 itemType = data.simpleChildrenTypes[i];
216 if (itemType == string.Empty)
217 {
218 Debug.Log("Empty item type found at idx: " + i.ToString() + " of 'simpleChildrenTypes' array. Skipping", "n/a", "n/a", "SpawnSimpleChildrenItems");
219 continue;
220 }
221
223 Class.CastTo(item, CreateChildItem(parent, itemType));
224
225 if (item)
226 {
227 if (!data.simpleChildrenUseDefaultAttributes)
228 ApplyAttributes(item, data.attributes);
229 }
230 else
231 Debug.Log("FAILED spawning item type: " + itemType + " to parent: " + parent, "n/a", "n/a", "SpawnSimpleChildrenItems");
232 }
233 return true;
234 }
235
237 {
238 ApplyAttributes(item, data.attributes);
239
241 if (Class.CastTo(player, item.GetHierarchyRootPlayer()) && data.GetQuickbarIdx() > -1)
242 player.SetQuickBarEntityShortcut(item, data.GetQuickbarIdx());
243
246 }
247
248 private static EntityAI CreateChildItem(EntityAI parent, string type)
249 {
252 if (Class.CastTo(player, parent)) //special behavior
253 {
254 int count = player.GetInventory().AttachmentCount();
255 if (Class.CastTo(newItem, player.GetInventory().CreateInInventory(type)))
256 return newItem;
257
258 Debug.Log("FAILED spawning item: " + type + ", it fits in no cargo or attachment on any worn item", "n/a", "n/a", "CreateChildItem");
259 return null;
260 }
261
262 //weapon magazine exception
263 if (GetGame().ConfigIsExisting(CFG_MAGAZINESPATH + " " + type) && parent.IsWeapon())
264 {
266 if (Class.CastTo(wep, parent))
267 return wep.SpawnAttachedMagazine(type);
268 }
269
270 return parent.GetInventory().CreateInInventory(type);
271 }
272
274 {
275 if (!attributes)
276 return;
277
278 float health01 = Math.RandomFloatInclusive(attributes.healthMin, attributes.healthMax);
279 item.SetHealth01("", "Health", health01);
280
281 float quantity01 = Math.RandomFloatInclusive(attributes.quantityMin, attributes.quantityMax);
282 if (item.IsMagazine())
283 {
284 Magazine mag = Magazine.Cast(item);
285 //todo: magazine bullet customization?
286 /*if (attributes.magazineAmmoOrdered && attributes.magazineAmmoOrdered.Count() > 0)
287 {
288 mag.ServerSetAmmoCount(0);
289 foreach (string bulletType : attributes.magazineAmmoOrdered)
290 {
291 mag.ServerStoreCartridge(health01,bulletType);
292 }
293 mag.SetSynchDirty();
294 }
295 else*/
296 {
297 int ammoQuantity = (int)Math.Lerp(0, mag.GetAmmoMax(), quantity01);
298 mag.ServerSetAmmoCount(ammoQuantity);
299 }
300 }
301 else //'varQuantityDestroyOnMin' quantity safeguard
302 {
303 float quantityAbsolute = Math.Lerp(item.GetQuantityMin(), item.GetQuantityMax(), quantity01);
304 quantityAbsolute = Math.Round(quantityAbsolute); //avoids weird floats
305 if (quantityAbsolute <= item.GetQuantityMin() && item.ConfigGetBool("varQuantityDestroyOnMin"))
307 item.SetQuantity(quantityAbsolute);
308 }
309 }
310}
Param3 int
Empty
Definition Hand_States.c:14
static TStringArray GetPlayerSpawnGearPresetFiles()
Super root of all classes in Enforce script.
Definition EnScript.c:11
Definition Debug.c:14
static void Log(string message=LOG_DEFAULT, string plugin=LOG_DEFAULT, string author=LOG_DEFAULT, string label=LOG_DEFAULT, string entity=LOG_DEFAULT)
Prints debug message with normal prio.
Definition Debug.c:133
provides access to slot configuration
static proto native owned string GetSlotName(int id)
converts slot_id to string
Definition EnMath.c:7
static bool SpawnDiscreteSlotItemSet(PlayerBase player, PlayerSpawnPresetDiscreteItemSetSlotData dis, int slotID)
static void ProcessSlotsEquipment(PlayerBase player, PlayerSpawnPreset data)
iterates over each object and spawns alternatives
static void ApplyAttributes(ItemBase item, PlayerSpawnAttributesData attributes)
static ref PlayerSpawnJsonData m_Data
static bool SpawnSimpleChildrenItems(EntityAI parent, PlayerSpawnPresetItemSetBase data)
static bool SpawnDiscreteCargoItemSet(PlayerBase player, PlayerSpawnPresetDiscreteCargoSetData csd)
static bool SpawnComplexChildrenItems(EntityAI parent, notnull PlayerSpawnPresetItemSetBase data)
could spawn other items recursively. Parent item is guaranteed here.
static bool SelectAndSpawnSlotEquipment(PlayerBase player, PlayerSpawnPresetSlotData slotData)
selects weighted slot equipment variant
static bool ProcessEquipmentData(PlayerBase player, PlayerSpawnPreset data)
equips character with the chosen preset
static bool SelectAndSpawnCargoSet(PlayerBase player, PlayerSpawnPreset data)
static void ProcessCargoEquipment(PlayerBase player, PlayerSpawnPreset data)
chooses one object from the array
static void HandleNewItem(notnull ItemBase item, PlayerSpawnPresetItemSetBase data)
static PlayerSpawnPreset GetRandomCharacterPreset()
static EntityAI CreateChildItem(EntityAI parent, string type)
used for specific hierarchical child spawning
proto native CGame GetGame()
enum ShapeType ErrorEx
static proto bool CastTo(out Class to, Class from)
Try to safely down-cast base class to child class.
static proto float Lerp(float a, float b, float time)
Linearly interpolates between 'a' and 'b' given 'time'.
static float RandomFloatInclusive(float min, float max)
Returns a random float number between and min [inclusive] and max [inclusive].
Definition EnMath.c:106
static proto float Round(float f)
Returns mathematical round of value.
const string CFG_MAGAZINESPATH
Definition constants.c:211