Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_LoadoutManager.c
Go to the documentation of this file.
1[EntityEditorProps(category: "GameScripted/GameMode", description: "temp loadout selection.", color: "0 0 255 255")]
2class SCR_LoadoutManagerClass : GenericEntityClass
3{
4}
5
7{
8 protected static const int INVALID_LOADOUT_INDEX = -1;
9
10 [Attribute("", UIWidgets.Object, category: "Loadout Manager")]
11 protected ref array<ref SCR_BasePlayerLoadout> m_aPlayerLoadouts;
12
14 [RplProp(onRplName: "OnPlayerLoadoutInfoChanged")]
15 protected ref array<ref SCR_PlayerLoadoutInfo> m_aPlayerLoadoutInfo = {};
16
19
21 protected ref set<int> m_ChangedLoadouts = new set<int>();
22
25
28
30
31 //------------------------------------------------------------------------------------------------
36 {
38 if (m_MappedPlayerLoadoutInfo.Find(playerId, info))
39 {
40 int loadoutIndex = info.GetLoadoutIndex();
41 return GetLoadoutByIndex(loadoutIndex);
42 }
43
44 return null;
45 }
46
47 //------------------------------------------------------------------------------------------------
54 static SCR_BasePlayerLoadout SGetPlayerLoadout(int playerId)
55 {
56 SCR_LoadoutManager loadoutManager = GetGame().GetLoadoutManager();
57 return loadoutManager.GetPlayerLoadout(playerId);
58 }
59
60 //------------------------------------------------------------------------------------------------
65 {
66 int localPlayerId = SCR_PlayerController.GetLocalPlayerId();
67 return GetPlayerLoadout(localPlayerId);
68 }
69
70 //------------------------------------------------------------------------------------------------
76 static SCR_BasePlayerLoadout SGetLocalPlayerLoadout()
77 {
78 SCR_LoadoutManager loadoutManager = GetGame().GetLoadoutManager();
79 return loadoutManager.GetLocalPlayerLoadout();
80 }
81
82 //------------------------------------------------------------------------------------------------
87 {
88 if (!loadout)
89 return 0;
90
91 int playerCount;
92 m_PlayerCount.Find(GetLoadoutIndex(loadout), playerCount);
93 return playerCount;
94 }
95
96 //------------------------------------------------------------------------------------------------
103 static int SGetLoadoutPlayerCount(SCR_BasePlayerLoadout loadout)
104 {
105 SCR_LoadoutManager loadoutManager = GetGame().GetLoadoutManager();
106 return loadoutManager.GetLoadoutPlayerCount(loadout);
107 }
108
109 //------------------------------------------------------------------------------------------------
112 {
113 // Store previous loadouts, so we can raise events
114 m_ChangedLoadouts.Clear();
116 foreach (int id, SCR_PlayerLoadoutInfo loadoutInfo : m_MappedPlayerLoadoutInfo)
117 {
118 int loadoutIndex = -1;
119 if (loadoutInfo)
120 loadoutIndex = loadoutInfo.GetLoadoutIndex();
121
122 m_PreviousPlayerLoadouts.Set(id, loadoutIndex);
123 }
124
125 // Clear all records and rebuild them from scratch
127 m_PlayerCount.Clear();
128 for (int i = 0, cnt = m_aPlayerLoadoutInfo.Count(); i < cnt; i++)
129 {
130 int playerId = m_aPlayerLoadoutInfo[i].GetPlayerId();
132
133 // Resolve player-loadout count
134 int playerLoadoutIndex = m_aPlayerLoadoutInfo[i].GetLoadoutIndex();
135 if (playerLoadoutIndex != -1)
136 {
137 int previousCount;
138 m_PlayerCount.Find(playerLoadoutIndex, previousCount);
139 m_PlayerCount.Set(playerLoadoutIndex, previousCount + 1);
140 }
141
142 // If player count changed, append to temp list
143 int previousLoadoutIndex;
144 if (!m_PreviousPlayerLoadouts.Find(playerId, previousLoadoutIndex))
145 previousLoadoutIndex = -1; // If player had no affiliated loadout previously, always assume none instead
146
147 if (previousLoadoutIndex != playerLoadoutIndex)
148 {
149 m_ChangedLoadouts.Insert(previousLoadoutIndex);
150 m_ChangedLoadouts.Insert(playerLoadoutIndex);
151 }
152 }
153
154 // Raise callback for all loadouts of which the count has changed
155 foreach (int loadoutIndex : m_ChangedLoadouts)
156 {
157 // Null loadout
158 if (loadoutIndex == -1)
159 continue;
160
162 int count;
163 m_PlayerCount.Find(loadoutIndex, count);
165 }
166 }
167
168 //------------------------------------------------------------------------------------------------
177 {
180
181 #ifdef _ENABLE_RESPAWN_LOGS
182 ResourceName res;
183 if (loadout) res = loadout.GetLoadoutResource();
184 PrintFormat("%1::OnPlayerLoadoutCountChanged(loadout: %2 [%3], count: %4)", Type().ToString(), loadout, res, newCount);
185 #endif
186 }
187
188 //------------------------------------------------------------------------------------------------
192 {
193 #ifdef _ENABLE_RESPAWN_LOGS
194 PrintFormat("%1::OnPlayerLoadoutSet_S(playerId: %2, loadout: %3)", Type().ToString(), playerComponent.GetPlayerId(), loadout);
195 #endif
196 }
197
198 //------------------------------------------------------------------------------------------------
203 {
204 int targetPlayerId = playerLoadoutComponent.GetPlayerController().GetPlayerId();
205 SCR_BasePlayerLoadout targetLoadout = playerLoadoutComponent.GetAssignedLoadout();
206 int targetLoadoutIndex = GetLoadoutIndex(targetLoadout);
207
208 // See if we have a record of player in the map
209 SCR_PlayerLoadoutInfo foundInfo;
210 if (m_MappedPlayerLoadoutInfo.Find(targetPlayerId, foundInfo))
211 {
212 // Adjust player counts
213 SCR_BasePlayerLoadout previousLoadout = GetPlayerLoadout(targetPlayerId);
214 if (previousLoadout)
215 {
216 // But only if previous entry was valid
217 int previousIndex = GetLoadoutIndex(previousLoadout);
218 if (previousIndex != -1)
219 {
220 int previousCount;
221 m_PlayerCount.Find(previousIndex, previousCount); // Will not set value if not found
222 int newCount = previousCount - 1;
223 m_PlayerCount.Set(previousIndex, newCount); // Remove this player
224 OnPlayerLoadoutCountChanged(previousLoadout, newCount);
225 }
226 }
227
228 // Update existing record
229 foundInfo.SetLoadoutIndex(targetLoadoutIndex);
230
231 // If new loadout is valid, add to player count
232 if (targetLoadoutIndex != -1)
233 {
234 int previousCount;
235 m_PlayerCount.Find(targetLoadoutIndex, previousCount); // Will not set value if not found
236 int newCount = previousCount + 1;
237 m_PlayerCount.Set(targetLoadoutIndex, newCount); // Remove this player
238 OnPlayerLoadoutCountChanged(targetLoadout, newCount);
239 }
240
241 // Raise authority callback
242 OnPlayerLoadoutSet_S(playerLoadoutComponent, targetLoadout);
243
244 Replication.BumpMe();
245 return;
246 }
247
248 // Insert new record
249 SCR_PlayerLoadoutInfo newInfo = SCR_PlayerLoadoutInfo.Create(targetPlayerId);
250 newInfo.SetLoadoutIndex(targetLoadoutIndex);
251 m_aPlayerLoadoutInfo.Insert(newInfo);
252 // And map it
253 m_MappedPlayerLoadoutInfo.Set(targetPlayerId, newInfo);
254
255 // And since this player was not assigned, increment the count of players for target faction
256 if (targetLoadoutIndex != -1)
257 {
258 int previousCount;
259 m_PlayerCount.Find(targetLoadoutIndex, previousCount);
260 int newCount = previousCount + 1;
261 m_PlayerCount.Set(targetLoadoutIndex, newCount);
262 OnPlayerLoadoutCountChanged(targetLoadout, newCount);
263 }
264
265 // Raise authority callback
266 OnPlayerLoadoutSet_S(playerLoadoutComponent, targetLoadout);
267
268 Replication.BumpMe();
269 }
270
271 //------------------------------------------------------------------------------------------------
279 {
280 return true;
281 }
282
283 //------------------------------------------------------------------------------------------------
285 array<ref SCR_BasePlayerLoadout> GetPlayerLoadouts()
286 {
287 return m_aPlayerLoadouts;
288 }
289
290 //------------------------------------------------------------------------------------------------
293 {
295 return 0;
296
297 return m_aPlayerLoadouts.Count();
298 }
299
300 //------------------------------------------------------------------------------------------------
304 {
305 foreach (int i, SCR_BasePlayerLoadout inst : m_aPlayerLoadouts)
306 {
307 if (inst == loadout)
308 return i;
309 }
310
311 return -1;
312 }
313
314 //------------------------------------------------------------------------------------------------
319 {
321 return null;
322
323 return m_aPlayerLoadouts[index];
324 }
325
326 //------------------------------------------------------------------------------------------------
330 SCR_BasePlayerLoadout GetLoadoutByName(string name, FactionKey faction = string.Empty)
331 {
332 foreach (SCR_BasePlayerLoadout candidate : m_aPlayerLoadouts)
333 {
334 if (candidate.GetLoadoutName() == name)
335 {
336 auto factionLoadout = SCR_FactionPlayerLoadout.Cast(candidate);
337 if (faction && factionLoadout && factionLoadout.GetFactionKey() != faction)
338 continue;
339
340 return candidate;
341 }
342 }
343
344 return null;
345 }
346
347 //------------------------------------------------------------------------------------------------
351 {
352 array<ref SCR_BasePlayerLoadout> loadouts = {};
353 if (GetPlayerLoadoutsByFaction(faction, loadouts) == 0)
354 return null;
355
356 return loadouts.GetRandomElement();
357 }
358
359 //------------------------------------------------------------------------------------------------
361 {
362 FactionKey factionKey = faction.GetFactionKey();
363 SCR_PlayerArsenalLoadout playerLoadout;
364 foreach (SCR_BasePlayerLoadout baseLoadout : m_aPlayerLoadouts)
365 {
366 playerLoadout = SCR_PlayerArsenalLoadout.Cast(baseLoadout);
367 if (!playerLoadout)
368 continue;
369
370 if (playerLoadout.GetFactionKey() == factionKey)
371 return true;
372 }
373
374 return false;
375 }
376
377 //------------------------------------------------------------------------------------------------
381 int GetPlayerLoadoutsByFaction(Faction faction, out notnull array<ref SCR_BasePlayerLoadout> outLoadouts)
382 {
383 outLoadouts.Clear();
384
386 return 0;
387
388 ArmaReforgerScripted game = GetGame();
389 if (!game)
390 return 0;
391
392 FactionManager factionManager = game.GetFactionManager();
393 if (!factionManager)
394 return 0;
395
396 int outCount = 0;
397
398 int count = m_aPlayerLoadouts.Count();
399 for (int i = 0; i < count; i++)
400 {
401#ifdef DISABLE_ARSENAL_LOADOUTS
403 continue;
404#endif
406 if (factionLoadout)
407 {
408 Faction ldFaction = factionManager.GetFactionByKey(factionLoadout.m_sAffiliatedFaction);
409 if (faction == ldFaction)
410 {
411 outLoadouts.Insert(factionLoadout);
412 outCount++;
413 }
414 }
415 }
416
417 return outCount;
418 }
419
420 //------------------------------------------------------------------------------------------------
425 int GetPlayerLoadoutsByGroup(notnull SCR_AIGroup group, notnull Faction faction, out notnull array<ref SCR_BasePlayerLoadout> outLoadouts)
426 {
427 outLoadouts.Clear();
428
430 return 0;
431
432 ArmaReforgerScripted game = GetGame();
433 if (!game)
434 return 0;
435
436 FactionManager factionManager = game.GetFactionManager();
437 if (!factionManager)
438 return 0;
439
440 SCR_Faction scrFaction = SCR_Faction.Cast(faction);
441 if (!scrFaction)
442 return 0;
443
444 SCR_FactionPlayerLoadout factionLoadout;
445 Faction ldFaction;
446 int outCount;
448 {
449#ifdef DISABLE_ARSENAL_LOADOUTS
451 continue;
452#endif
453 factionLoadout = SCR_FactionPlayerLoadout.Cast(loadout);
454 if (!factionLoadout)
455 continue;
456
457 ldFaction = factionManager.GetFactionByKey(factionLoadout.m_sAffiliatedFaction);
458 if (faction == ldFaction && group.IsLoadoutInGroup(factionLoadout))
459 {
460 outLoadouts.Insert(factionLoadout);
461 outCount++;
462 }
463 }
464
465 return outCount;
466 }
467
468 //------------------------------------------------------------------------------------------------
471 int GetPlayerLoadouts(out notnull array<SCR_BasePlayerLoadout> outLoadouts)
472 {
473 int outCount = 0;
474 outLoadouts.Clear();
475
477 return 0;
478
479 int count = m_aPlayerLoadouts.Count();
480 for (int i = 0; i < count; i++)
481 {
482 outLoadouts.Insert(m_aPlayerLoadouts[i]);
483 outCount++;
484 }
485
486 return outCount;
487 }
488
489 //------------------------------------------------------------------------------------------------
493 {
495 return -1;
496
497 array<ref SCR_BasePlayerLoadout> loadouts = {};
498 int count = GetPlayerLoadoutsByFaction(faction, loadouts);
499 if (count <= 0)
500 return -1;
501
502 return Math.RandomInt(0, count);
503 }
504
505
506 //------------------------------------------------------------------------------------------------
509 {
511 return -1;
512
513 int count = m_aPlayerLoadouts.Count();
514 if (count <= 0)
515 return -1;
516
517 return Math.RandomInt(0, count);
518 }
519
520 //------------------------------------------------------------------------------------------------
523 {
524 int randomIndex = GetRandomLoadoutIndex();
525 if (randomIndex < 0)
526 return null;
527
528 return m_aPlayerLoadouts[randomIndex];
529 }
530
531 //------------------------------------------------------------------------------------------------
540
541 #ifdef ENABLE_DIAG
542 //------------------------------------------------------------------------------------------------
546 protected override void EOnDiag(IEntity owner, float timeSlice)
547 {
548 super.EOnDiag(owner, timeSlice);
549
550 if (!DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_PLAYER_LOADOUT_DIAG))
551 return;
552
553 DbgUI.Begin("SCR_LoadoutManager");
554 {
555 DbgUI.Text("* Loadout Player Count *");
556 foreach (SCR_BasePlayerLoadout ld : m_aPlayerLoadouts)
557 {
558 DbgUI.Text(string.Format("%1: %2 player(s)", ld.GetLoadoutName(), GetLoadoutPlayerCount(ld)));
559 }
560 }
561 DbgUI.End();
562 }
563 #endif
564
565 //------------------------------------------------------------------------------------------------
566 // constructor
570 {
571 GetGame().RegisterLoadoutManager(this);
572
573 #ifdef ENABLE_DIAG
574 ConnectToDiagSystem();
575 #endif
576 }
577
578 //------------------------------------------------------------------------------------------------
581 {
582 #ifdef ENABLE_DIAG
583 DisconnectFromDiagSystem();
584 #endif
585 GetGame().UnregisterLoadoutManager(this);
586 }
587}
ref DSGameConfig game
Definition DSConfig.c:81
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition DebugMenuID.c:4
ArmaReforgerScripted GetGame()
Definition game.c:1398
ref map< UUID, ref SCR_ArsenalPlayerLoadout > m_aPlayerLoadouts
SCR_CacheNoteComponentClass ScriptComponentClass RplProp()] protected ref array< string > m_aLines
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
ref map< int, int > m_PlayerCount
Mapping of faction id : player count.
int GetLoadoutPlayerCount(SCR_BasePlayerLoadout loadout)
ref ScriptInvoker< SCR_BasePlayerLoadout, int > m_OnMappedPlayerLoadoutInfoChanged
ScriptInvoker GetOnMappedPlayerLoadoutInfoChanged()
int GetLoadoutIndex(SCR_BasePlayerLoadout loadout)
void OnPlayerLoadoutCountChanged(SCR_BasePlayerLoadout loadout, int newCount)
int GetPlayerLoadoutsByFaction(Faction faction, out notnull array< ref SCR_BasePlayerLoadout > outLoadouts)
SCR_BasePlayerLoadout GetPlayerLoadout(int playerId)
ref map< int, ref SCR_PlayerLoadoutInfo > m_MappedPlayerLoadoutInfo
Local mapping of playerId to player loadout info.
SCR_BasePlayerLoadout GetLocalPlayerLoadout()
SCR_LoadoutManagerClass INVALID_LOADOUT_INDEX
SCR_BasePlayerLoadout GetRandomLoadout()
SCR_BasePlayerLoadout GetLoadoutByName(string name, FactionKey faction=string.Empty)
bool IsFactionSupportingCsutomLoadouts(notnull Faction faction)
SCR_BasePlayerLoadout GetRandomFactionLoadout(Faction faction)
void OnPlayerLoadoutInfoChanged()
Update local player loadout mapping.
void SCR_LoadoutManager(IEntitySource src, IEntity parent)
SCR_BasePlayerLoadout GetLoadoutByIndex(int index)
int GetRandomLoadoutIndex()
void OnPlayerLoadoutSet_S(SCR_PlayerLoadoutComponent playerComponent, SCR_BasePlayerLoadout loadout)
void ~SCR_LoadoutManager()
destructor
ref array< ref SCR_PlayerLoadoutInfo > m_aPlayerLoadoutInfo
List of all player loadout infos in no particular order. Maintained by the authority.
ref set< int > m_ChangedLoadouts
List of indices of loadouts whose count has changed since last update.
ref map< int, int > m_PreviousPlayerLoadouts
Map of previous player <playerId : loadoutIndex>.
int GetPlayerLoadoutsByGroup(notnull SCR_AIGroup group, notnull Faction faction, out notnull array< ref SCR_BasePlayerLoadout > outLoadouts)
bool CanAssignLoadout_S(SCR_PlayerLoadoutComponent playerLoadoutComponent, SCR_BasePlayerLoadout loadout)
void UpdatePlayerLoadout_S(SCR_PlayerLoadoutComponent playerLoadoutComponent)
array< ref SCR_BasePlayerLoadout > GetPlayerLoadouts()
int GetLoadoutCount()
Returns the number of loadouts provided by this manager or 0 if none.
int Type
enum EVehicleType IEntity
Definition Math.c:13
Main replication API.
Definition Replication.c:14
static int GetLocalPlayerId()
Returns either a valid ID of local player or 0.
SCR_BasePlayerLoadout GetAssignedLoadout()
static SCR_PlayerLoadoutInfo Create(int playerId)
void SetLoadoutIndex(int loadoutIndex)
Definition Types.c:486
override void EOnDiag(IEntity owner, float timeSlice)
proto void PrintFormat(string fmt, void param1=NULL, void param2=NULL, void param3=NULL, void param4=NULL, void param5=NULL, void param6=NULL, void param7=NULL, void param8=NULL, void param9=NULL, LogLevel level=LogLevel.NORMAL)
SCR_FieldOfViewSettings Attribute
proto external string ToString()
Plain C++ pointer, no weak pointers, no memory management.
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134