Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_LoadoutManager.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/GameMode", description: "temp loadout selection.", color: "0 0 255 255")]
2 class 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 
18  protected ref map<int, int> m_PreviousPlayerLoadouts = new map<int, int>();
19 
21  protected ref set<int> m_ChangedLoadouts = new set<int>();
22 
24  protected ref map<int, ref SCR_PlayerLoadoutInfo> m_MappedPlayerLoadoutInfo = new map<int, ref SCR_PlayerLoadoutInfo>();
25 
27  protected ref map<int, int> m_PlayerCount = new map<int, int>();
28 
29  protected ref ScriptInvoker<SCR_BasePlayerLoadout, int> m_OnMappedPlayerLoadoutInfoChanged;
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  //------------------------------------------------------------------------------------------------
111  protected void OnPlayerLoadoutInfoChanged()
112  {
113  // Store previous loadouts, so we can raise events
114  m_ChangedLoadouts.Clear();
115  m_PreviousPlayerLoadouts.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();
131  m_MappedPlayerLoadoutInfo.Insert(playerId, m_aPlayerLoadoutInfo[i]);
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  {
294  if (!m_aPlayerLoadouts)
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  {
320  if (index < 0 || index >= m_aPlayerLoadouts.Count())
321  return null;
322 
323  return m_aPlayerLoadouts[index];
324  }
325 
326  //------------------------------------------------------------------------------------------------
331  {
332  int count = m_aPlayerLoadouts.Count();
333  for (int i = 0; i < count; i++)
334  {
336 
337  if (candidate.GetLoadoutName() == name)
338  return candidate;
339  }
340 
341  return null;
342  }
343 
344  //------------------------------------------------------------------------------------------------
348  {
349  array<ref SCR_BasePlayerLoadout> loadouts = {};
350  if (GetPlayerLoadoutsByFaction(faction, loadouts) == 0)
351  return null;
352 
353  return loadouts.GetRandomElement();
354  }
355 
356  //------------------------------------------------------------------------------------------------
360  int GetPlayerLoadoutsByFaction(Faction faction, out notnull array<ref SCR_BasePlayerLoadout> outLoadouts)
361  {
362  outLoadouts.Clear();
363 
364  if (!m_aPlayerLoadouts)
365  return 0;
366 
367  ArmaReforgerScripted game = GetGame();
368  if (!game)
369  return 0;
370 
371  FactionManager factionManager = game.GetFactionManager();
372  if (!factionManager)
373  return 0;
374 
375  int outCount = 0;
376 
377  int count = m_aPlayerLoadouts.Count();
378  for (int i = 0; i < count; i++)
379  {
380 #ifdef DISABLE_ARSENAL_LOADOUTS
382  continue;
383 #endif
385  if (factionLoadout)
386  {
387  Faction ldFaction = factionManager.GetFactionByKey(factionLoadout.m_sAffiliatedFaction);
388  if (faction == ldFaction)
389  {
390  outLoadouts.Insert(factionLoadout);
391  outCount++;
392  }
393  }
394  }
395 
396  return outCount;
397  }
398 
399  //------------------------------------------------------------------------------------------------
402  int GetPlayerLoadouts(out notnull array<SCR_BasePlayerLoadout> outLoadouts)
403  {
404  int outCount = 0;
405  outLoadouts.Clear();
406 
407  if (!m_aPlayerLoadouts)
408  return 0;
409 
410  int count = m_aPlayerLoadouts.Count();
411  for (int i = 0; i < count; i++)
412  {
413  outLoadouts.Insert(m_aPlayerLoadouts[i]);
414  outCount++;
415  }
416 
417  return outCount;
418  }
419 
420  //------------------------------------------------------------------------------------------------
424  {
425  if (!m_aPlayerLoadouts)
426  return -1;
427 
428  array<ref SCR_BasePlayerLoadout> loadouts = {};
429  int count = GetPlayerLoadoutsByFaction(faction, loadouts);
430  if (count <= 0)
431  return -1;
432 
433  return Math.RandomInt(0, count);
434  }
435 
436 
437  //------------------------------------------------------------------------------------------------
440  {
441  if (!m_aPlayerLoadouts)
442  return -1;
443 
444  int count = m_aPlayerLoadouts.Count();
445  if (count <= 0)
446  return -1;
447 
448  return Math.RandomInt(0, count);
449  }
450 
451  //------------------------------------------------------------------------------------------------
454  {
455  int randomIndex = GetRandomLoadoutIndex();
456  if (randomIndex < 0)
457  return null;
458 
459  return m_aPlayerLoadouts[randomIndex];
460  }
461 
462  //------------------------------------------------------------------------------------------------
465  {
467  m_OnMappedPlayerLoadoutInfoChanged = new ScriptInvoker();
468 
470  }
471 
472  #ifdef ENABLE_DIAG
473  //------------------------------------------------------------------------------------------------
477  protected override void EOnDiag(IEntity owner, float timeSlice)
478  {
479  super.EOnDiag(owner, timeSlice);
480 
481  if (!DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_PLAYER_LOADOUT_DIAG))
482  return;
483 
484  DbgUI.Begin("SCR_LoadoutManager");
485  {
486  DbgUI.Text("* Loadout Player Count *");
488  {
489  DbgUI.Text(string.Format("%1: %2 player(s)", ld.GetLoadoutName(), GetLoadoutPlayerCount(ld)));
490  }
491  }
492  DbgUI.End();
493  }
494  #endif
495 
496  //------------------------------------------------------------------------------------------------
497  // constructor
500  void SCR_LoadoutManager(IEntitySource src, IEntity parent)
501  {
502  GetGame().RegisterLoadoutManager(this);
503 
504  #ifdef ENABLE_DIAG
505  ConnectToDiagSystem();
506  #endif
507  }
508 
509  //------------------------------------------------------------------------------------------------
512  {
513  #ifdef ENABLE_DIAG
514  DisconnectFromDiagSystem();
515  #endif
516  GetGame().UnregisterLoadoutManager(this);
517  }
518 }
SCR_PlayerLoadoutComponent
Definition: SCR_PlayerLoadoutComponent.c:16
GetRandomFactionLoadout
SCR_BasePlayerLoadout GetRandomFactionLoadout(Faction faction)
Definition: SCR_LoadoutManager.c:347
OnPlayerLoadoutSet_S
protected void OnPlayerLoadoutSet_S(SCR_PlayerLoadoutComponent playerComponent, SCR_BasePlayerLoadout loadout)
Definition: SCR_LoadoutManager.c:191
GetRandomLoadout
SCR_BasePlayerLoadout GetRandomLoadout()
Definition: SCR_LoadoutManager.c:453
SCR_PlayerController
Definition: SCR_PlayerController.c:31
m_OnMappedPlayerLoadoutInfoChanged
protected ref ScriptInvoker< SCR_BasePlayerLoadout, int > m_OnMappedPlayerLoadoutInfoChanged
Definition: SCR_LoadoutManager.c:29
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
SCR_PlayerLoadoutInfo
Definition: SCR_PlayerLoadoutInfo.c:1
GetLocalPlayerLoadout
SCR_BasePlayerLoadout GetLocalPlayerLoadout()
Definition: SCR_LoadoutManager.c:64
GetLoadoutByName
SCR_BasePlayerLoadout GetLoadoutByName(string name)
Definition: SCR_LoadoutManager.c:330
m_aPlayerLoadoutInfo
protected ref array< ref SCR_PlayerLoadoutInfo > m_aPlayerLoadoutInfo
List of all player loadout infos in no particular order. Maintained by the authority.
Definition: SCR_LoadoutManager.c:15
RplProp
SCR_RplTestEntityClass RplProp
Used for handling entity spawning requests for SCR_CatalogEntitySpawnerComponent and inherited classe...
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
GetPlayerLoadout
SCR_BasePlayerLoadout GetPlayerLoadout(int playerId)
Definition: SCR_LoadoutManager.c:35
loadout
string loadout
Definition: SCR_ArsenalManagerComponent.c:2
GetLoadoutCount
int GetLoadoutCount()
Returns the number of loadouts provided by this manager or 0 if none.
Definition: SCR_LoadoutManager.c:292
SCR_LoadoutManager
void SCR_LoadoutManager(IEntitySource src, IEntity parent)
Definition: SCR_LoadoutManager.c:500
GetLoadoutByIndex
SCR_BasePlayerLoadout GetLoadoutByIndex(int index)
Definition: SCR_LoadoutManager.c:318
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
INVALID_LOADOUT_INDEX
SCR_LoadoutManagerClass INVALID_LOADOUT_INDEX
OnPlayerLoadoutCountChanged
protected void OnPlayerLoadoutCountChanged(SCR_BasePlayerLoadout loadout, int newCount)
Definition: SCR_LoadoutManager.c:176
m_PreviousPlayerLoadouts
protected ref map< int, int > m_PreviousPlayerLoadouts
Map of previous player <playerId : loadoutIndex>.
Definition: SCR_LoadoutManager.c:18
GetLoadoutPlayerCount
int GetLoadoutPlayerCount(SCR_BasePlayerLoadout loadout)
Definition: SCR_LoadoutManager.c:86
UpdatePlayerLoadout_S
void UpdatePlayerLoadout_S(SCR_PlayerLoadoutComponent playerLoadoutComponent)
Definition: SCR_LoadoutManager.c:202
Attribute
typedef Attribute
Post-process effect of scripted camera.
m_MappedPlayerLoadoutInfo
protected ref map< int, ref SCR_PlayerLoadoutInfo > m_MappedPlayerLoadoutInfo
Local mapping of playerId to player loadout info.
Definition: SCR_LoadoutManager.c:24
SCR_FactionPlayerLoadout
Definition: SCR_PlayerFactionLoadout.c:2
m_ChangedLoadouts
protected ref set< int > m_ChangedLoadouts
List of indices of loadouts whose count has changed since last update.
Definition: SCR_LoadoutManager.c:21
GetRandomLoadoutIndex
int GetRandomLoadoutIndex(Faction faction)
Definition: SCR_LoadoutManager.c:423
EOnDiag
override void EOnDiag(IEntity owner, float timeSlice)
Definition: SCR_AIGroupUtilityComponent.c:426
~SCR_LoadoutManager
void ~SCR_LoadoutManager()
destructor
Definition: SCR_LoadoutManager.c:511
Faction
Definition: Faction.c:12
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
SCR_LoadoutManagerClass
Definition: SCR_LoadoutManager.c:2
OnPlayerLoadoutInfoChanged
protected void OnPlayerLoadoutInfoChanged()
Update local player loadout mapping.
Definition: SCR_LoadoutManager.c:111
SCR_PlayerArsenalLoadout
Definition: SCR_PlayerArsenalLoadout.c:2
GetOnMappedPlayerLoadoutInfoChanged
ScriptInvoker GetOnMappedPlayerLoadoutInfoChanged()
Definition: SCR_LoadoutManager.c:464
GetPlayerLoadoutsByFaction
int GetPlayerLoadoutsByFaction(Faction faction, out notnull array< ref SCR_BasePlayerLoadout > outLoadouts)
Definition: SCR_LoadoutManager.c:360
m_PlayerCount
protected ref map< int, int > m_PlayerCount
Mapping of loadout id:player count.
Definition: SCR_LoadoutManager.c:27
SCR_DebugMenuID
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition: DebugMenuID.c:3
CanAssignLoadout_S
bool CanAssignLoadout_S(SCR_PlayerLoadoutComponent playerLoadoutComponent, SCR_BasePlayerLoadout loadout)
Definition: SCR_LoadoutManager.c:278
m_aPlayerLoadouts
protected ref array< ref SCR_BasePlayerLoadout > m_aPlayerLoadouts
Definition: SCR_LoadoutManager.c:11
GetPlayerLoadouts
array< ref SCR_BasePlayerLoadout > GetPlayerLoadouts()
Definition: SCR_LoadoutManager.c:285
GetLoadoutIndex
int GetLoadoutIndex(SCR_BasePlayerLoadout loadout)
Definition: SCR_LoadoutManager.c:303
SCR_BasePlayerLoadout
Definition: SCR_BasePlayerLoadout.c:2
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180