File size: 9,537 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
class PlayerSpawnHandler
{
	private static bool m_Initialized;
	static ref PlayerSpawnJsonData m_Data = new PlayerSpawnJsonData();
	
	static bool LoadData()
	{
		array<string> spawnGearPresetFiles = CfgGameplayHandler.GetPlayerSpawnGearPresetFiles();
		if (!spawnGearPresetFiles || (spawnGearPresetFiles && spawnGearPresetFiles.Count() == 0))
			return false;

		m_Data.presets = {};

		foreach (string spawnPresetFile : spawnGearPresetFiles)
		{
			PlayerSpawnPreset preset;
			string path = "$mission:" + spawnPresetFile;

			string errorMessage;
			if (!JsonFileLoader<PlayerSpawnPreset>.LoadFile(path, preset, errorMessage))
			{
				ErrorEx(errorMessage);
				return false;
			}
			
			if (preset != null)
				m_Data.presets.Insert(preset);
		}

		m_Initialized = m_Data.presets.Count() > 0;
		
		return true;
	}
	
	static bool IsInitialized()
	{
		return m_Initialized;
	}
	
	static PlayerSpawnPreset GetRandomCharacterPreset()
	{
		array<int> weightedPresetIndexes = new array<int>();
		int count = m_Data.presets.Count();
		PlayerSpawnPreset p;
		for (int i = 0; i < count; i++)
		{
			p = m_Data.presets[i];
			if (p.IsValid())
			{
				for (int j = 0; j < p.spawnWeight; j++)
				{
					weightedPresetIndexes.Insert(i);
				}
			}
		}
		
		return m_Data.presets.Get(weightedPresetIndexes.GetRandomElement());
	} 
	
	//! equips character with the chosen preset
	static bool ProcessEquipmentData(PlayerBase player, PlayerSpawnPreset data)
	{
		if (data.IsValid())
		{
			ProcessSlotsEquipment(player, data);
			ProcessCargoEquipment(player, data);
		}
		
		return true;
	}
	
	//! iterates over each object and spawns alternatives
	private static void ProcessSlotsEquipment(PlayerBase player, PlayerSpawnPreset data)
	{
		if (!data.HasAttachmentSlotSetsDefined())
		{
			Debug.Log("No non-empty 'attachmentSlotItemSets' array found. Skipping slot spawns","n/a","n/a","ProcessSlotsEquipment");
			return;
		}
		
		foreach (PlayerSpawnPresetSlotData slotData : data.attachmentSlotItemSets)
		{
			SelectAndSpawnSlotEquipment(player,slotData);
		}
	}
	
	//! selects weighted slot equipment variant
	private static bool SelectAndSpawnSlotEquipment(PlayerBase player, PlayerSpawnPresetSlotData slotData)
	{
		int slotID;
		if (!slotData.TranslateAndValidateSlot(player,slotID))
			return false;
		
		if (!slotData.IsValid())
			return false;
		
		array<int> weightedDiscreteSetIndexes = new array<int>();
		int count = slotData.discreteItemSets.Count();
		PlayerSpawnPresetDiscreteItemSetSlotData dis;
		for (int i = 0; i < count; i++)
		{
			dis = slotData.discreteItemSets[i];
			
			if (dis.IsValid()) //only when the type exists and spawnWeight is set
			{
				for (int j = 0; j < dis.spawnWeight; j++)
				{
					weightedDiscreteSetIndexes.Insert(i);
				}
			}
		}
		
		dis = null;
		if (weightedDiscreteSetIndexes.Count() > 0)
			dis = slotData.discreteItemSets.Get(weightedDiscreteSetIndexes.GetRandomElement());
		return SpawnDiscreteSlotItemSet(player,dis,slotID);
	}
	
	//! chooses one object from the array
	private static void ProcessCargoEquipment(PlayerBase player, PlayerSpawnPreset data)
	{
		if (!data.HasDiscreteUnsortedItemSetsDefined())
		{
			Debug.Log("No non-empty 'discreteUnsortedItemSets' array found. Skipping cargo spawns","n/a","n/a","ProcessCargoEquipment");
			return;
		}
		
		SelectAndSpawnCargoSet(player,data);
	}
	
	private static bool SelectAndSpawnCargoSet(PlayerBase player, PlayerSpawnPreset data)
	{
		array<int> weightedDiscreteSetIndexes = new array<int>();
		int count = data.discreteUnsortedItemSets.Count();
		PlayerSpawnPresetDiscreteCargoSetData csd;
		for (int i = 0; i < count; i++)
		{
			csd = data.discreteUnsortedItemSets[i];
			
			if (csd.IsValid()) //only when the spawnWeight is set
			{
				for (int j = 0; j < csd.spawnWeight; j++)
				{
					weightedDiscreteSetIndexes.Insert(i);
				}
			}
		}
		
		csd = null;
		if (weightedDiscreteSetIndexes.Count() > 0)
			csd = data.discreteUnsortedItemSets.Get(weightedDiscreteSetIndexes.GetRandomElement());
		return SpawnDiscreteCargoItemSet(player,csd);
	}
	
	private static bool SpawnDiscreteCargoItemSet(PlayerBase player, PlayerSpawnPresetDiscreteCargoSetData csd)
	{
		SpawnComplexChildrenItems(player,csd);
		SpawnSimpleChildrenItems(player,csd);
		return true;
	}
	
	private static bool SpawnDiscreteSlotItemSet(PlayerBase player, PlayerSpawnPresetDiscreteItemSetSlotData dis, int slotID)
	{
		if (!dis)
		{
			Debug.Log("No PlayerSpawnPresetDiscreteItemSet found. Skipping spawn for slot: " + InventorySlots.GetSlotName(slotID),"n/a","n/a","SpawnDiscreteSlotItemSet");
			return false;
		}
		
		ItemBase item;
		if (slotID == InventorySlots.HANDS) //hands exception
			item = ItemBase.Cast(player.GetHumanInventory().CreateInHands(dis.itemType));
		else
			item = ItemBase.Cast(player.GetInventory().CreateAttachmentEx(dis.itemType,slotID));
		
		if (item)
		{
			HandleNewItem(item,dis);
		}
		else if (dis.itemType != string.Empty)
		{
			Debug.Log("FAILED spawning item type: " + dis.itemType + " into slot: " + InventorySlots.GetSlotName(slotID) + " of parent: " + player,"n/a","n/a","SpawnDiscreteSlotItemSet");
			return false;
		}
		
		return item != null;
	}
	
	//! could spawn other items recursively. Parent item is guaranteed here.
	private static bool SpawnComplexChildrenItems(EntityAI parent, notnull PlayerSpawnPresetItemSetBase data)
	{
		if (!data.complexChildrenTypes || data.complexChildrenTypes.Count() < 1) //no children defined, still valid!
		{
			return false;
		}
		
		foreach (PlayerSpawnPresetComplexChildrenType cct : data.complexChildrenTypes)
		{
			//todo: slotID option over nyah?
			if (cct.itemType == string.Empty)
			{
				Debug.Log("Empty item type found in 'complexChildrenTypes' of parent : " + parent,"n/a","n/a","SpawnSimpleChildrenItems");
				continue;
			}
			
			ItemBase item;
			Class.CastTo(item,CreateChildItem(parent,cct.itemType));
			
			if (item)
			{
				HandleNewItem(item,cct);
			}
			else
			{
				Debug.Log("FAILED spawning item: " + cct.itemType + " of parent: " + parent,"n/a","n/a","SpawnComplexChildrenItems");
			}
		}
		
		return true;
	}
	
	private static bool SpawnSimpleChildrenItems(EntityAI parent, PlayerSpawnPresetItemSetBase data)
	{
		if (!data || !data.simpleChildrenTypes || data.simpleChildrenTypes.Count() < 1) //no children defined, still valid!
		{
			return false;
		}
		
		int count = data.simpleChildrenTypes.Count();
		string itemType;
		for (int i = 0; i < count; i++)
		{
			itemType = data.simpleChildrenTypes[i];
			if (itemType == string.Empty)
			{
				Debug.Log("Empty item type found at idx: " + i.ToString() + " of 'simpleChildrenTypes' array. Skipping","n/a","n/a","SpawnSimpleChildrenItems");
				continue;
			}
			
			ItemBase item;
			Class.CastTo(item,CreateChildItem(parent,itemType));
			
			if (item)
			{
				if (!data.simpleChildrenUseDefaultAttributes)
					ApplyAttributes(item,data.attributes);
			}
			else
			{
				Debug.Log("FAILED spawning item type: " + itemType + " to parent: " + parent,"n/a","n/a","SpawnSimpleChildrenItems");
			}
		}
		return true;
	}
	
	private static void HandleNewItem(notnull ItemBase item, PlayerSpawnPresetItemSetBase data)
	{
		ApplyAttributes(item,data.attributes);
		
		PlayerBase player;
		if (Class.CastTo(player,item.GetHierarchyRootPlayer()) && data.GetQuickbarIdx() > -1)
			player.SetQuickBarEntityShortcut(item,data.GetQuickbarIdx());
		
		SpawnComplexChildrenItems(item,data);
		SpawnSimpleChildrenItems(item,data);
	}
	
	private static EntityAI CreateChildItem(EntityAI parent, string type)
	{
		PlayerBase player;
		ItemBase newItem;
		if (Class.CastTo(player,parent)) //special behavior
		{
			int count = player.GetInventory().AttachmentCount();
			if (Class.CastTo(newItem,player.GetInventory().CreateInInventory(type)))
				return newItem;
			
			Debug.Log("FAILED spawning item: " + type + ", it fits in no cargo or attachment on any worn item","n/a","n/a","CreateChildItem");
			return null;
		}
		
		//weapon magazine exception
		if (GetGame().ConfigIsExisting(CFG_MAGAZINESPATH + " " + type) && parent.IsWeapon())
		{
			Weapon_Base wep
			if (Class.CastTo(wep,parent))
				return wep.SpawnAttachedMagazine(type);
		}
		
		return parent.GetInventory().CreateInInventory(type);
	}
	
	private static void ApplyAttributes(ItemBase item, PlayerSpawnAttributesData attributes)
	{
		if (!attributes)
			return;
		
		float health01 = Math.RandomFloatInclusive(attributes.healthMin,attributes.healthMax);
		item.SetHealth01("","Health",health01);
		
		float quantity01 = Math.RandomFloatInclusive(attributes.quantityMin,attributes.quantityMax);
		if (item.IsMagazine())
		{
			Magazine mag = Magazine.Cast(item);
			//todo: magazine bullet customization?
			/*if (attributes.magazineAmmoOrdered && attributes.magazineAmmoOrdered.Count() > 0)
			{
				mag.ServerSetAmmoCount(0);
				foreach (string bulletType : attributes.magazineAmmoOrdered)
				{
					mag.ServerStoreCartridge(health01,bulletType);
				}
				mag.SetSynchDirty();
			}
			else*/
			{
				int ammoQuantity = (int)Math.Lerp(0,mag.GetAmmoMax(),quantity01);
				mag.ServerSetAmmoCount(ammoQuantity);
			}
		}
		else //'varQuantityDestroyOnMin' quantity safeguard
		{
			float quantityAbsolute = Math.Lerp(item.GetQuantityMin(),item.GetQuantityMax(),quantity01);
			quantityAbsolute = Math.Round(quantityAbsolute); //avoids weird floats
			if (quantityAbsolute <= item.GetQuantityMin() && item.ConfigGetBool("varQuantityDestroyOnMin"))
				quantityAbsolute++;
			item.SetQuantity(quantityAbsolute);
		}
	}
}