Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_FiringRangeManager.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/FiringRange", description: "Handles score and MP synchronization. Only one per world.", color: "0 0 255 255")]
3 {
4 };
5 
6 //------------------------------------------------------------------------------------------------
7 class SCR_FiringRangeManager : ScriptedGameTriggerEntity
8 {
10  [Attribute("", UIWidgets.ResourcePickerThumbnail, "Entity to spawn as communication entity.", "et")]
11  private ResourceName m_ComEnt;
12 
13  protected Widget m_wRoot;
14  protected Widget m_wTable
15  private SCR_FiringRangeScoringComponent m_ScoringSystem;
16  private ref array<int> m_aPlayerScores = new array<int>;
17  private ref array<int> m_aPlayerScoresMax = new array<int>;
18  private ref array<string> m_aPlayerNames = new array<string>;
19  private ref array<SCR_PlayerScoreInfoFiringRange> m_aAllPlayersInfo = new array<SCR_PlayerScoreInfoFiringRange>;
20  private ref array<Widget> m_aPlayerWidgets = new array<Widget>;
21  [RplProp()]
22  private ref array<int> m_aPlayersInArea = new array<int>;
23 
24  // array contains all firing line controllers
25  private ref array<IEntity> m_aFiringLineControllers = new array<IEntity>;
26 
27  private int m_iLocalPlayerID = -1;
28  private int m_iLastSelectedTargetId = -1;
29  private int m_iSelectedIndex = -1;
30  protected static SCR_FiringRangeManager s_FiringRangeManagerMain;
31 
32  protected SCR_FiringRangeManager m_FiringRangeManagerInstance;
33 
34  // RPL component
35  private RplComponent m_RplComponent;
36 
37  private int m_iPlayersInGameCount = -1;
38  private int m_iPlayersInAreaCount = -1;
39 
40  //------------------------------------------------------------------------------------------------
41  //-------------------------------------- SCORE PART START ----------------------------------------
42  //------------------------------------------------------------------------------------------------
43  void ClearPlayerScore(int playerID)
44  {
45  #ifdef ENABLE_DIAG
46  if (!CheckMasterOnlyMethod("ClearPlayerScore()"))
47  return;
48  #endif
49 
50  if (!m_ScoringSystem)
51  return;
52 
53  m_ScoringSystem.ClearScore(playerID);
54  }
55 
56  //------------------------------------------------------------------------------------------------
57  void CountPlayerScore(int scoringPlayer,int scorePoints)
58  {
59  #ifdef ENABLE_DIAG
60  if (!CheckMasterOnlyMethod("CountPlayerScore()"))
61  return;
62  #endif
63 
64  if (!m_ScoringSystem)
65  return;
66 
67  m_ScoringSystem.AddScore(scoringPlayer,scorePoints);
68  }
69 
70  //------------------------------------------------------------------------------------------------
71  void SetPlayerScoreMax(int playerID, float maxScore)
72 
73  {
74  #ifdef ENABLE_DIAG
75  if (!CheckMasterOnlyMethod("SetPlayerScoreMax()"))
76  return;
77  #endif
78 
79  if (!m_ScoringSystem)
80  return;
81  m_ScoringSystem.SetScoreMax(playerID,maxScore);
82  }
83 
84  //------------------------------------------------------------------------------------------------
85  void UpdateScoreboardData()
86  {
87  if (!m_ScoringSystem)
88  return;
89 
90  if (m_aPlayerWidgets.IsEmpty())
91  return;
92 
93  if (m_aPlayerWidgets.Count() != m_aAllPlayersInfo.Count())
94  return;
95 
97  {
98  TextWidget nameWidget = TextWidget.Cast(m_aPlayerWidgets[i].FindAnyWidget("Name"));
99  if (nameWidget)
100  nameWidget.SetText(info.GetName());
101 
102  TextWidget scoreWidget = TextWidget.Cast(m_aPlayerWidgets[i].FindAnyWidget("Score"));
103  if (scoreWidget)
104  scoreWidget.SetText(info.GetScore(m_ScoringSystem).ToString());
105 
106 
107  TextWidget maxScoreWidget = TextWidget.Cast(m_aPlayerWidgets[i].FindAnyWidget("MaxScore"));
108  if (maxScoreWidget)
109  maxScoreWidget.SetText(info.GetScoreMax().ToString());
110  }
111  }
112 
113  //------------------------------------------------------------------------------------------------
114  int GetHighScoreInfo(array<SCR_PlayerScoreInfoFiringRange> infos)
115  {
116  int highScore = int.MIN;
117  int highIndex = -1;
118 
119  foreach (int i, SCR_PlayerScoreInfoFiringRange info : infos)
120  {
121  if (info)
122  {
123  int score = info.GetScore();
124  if (score > highScore)
125  {
126  highScore = score;
127  highIndex = i;
128  }
129  }
130  }
131 
132  return highIndex;
133  }
134 
135  //------------------------------------------------------------------------------------------------
136  bool ScoreChanged()
137  {
138  int count = m_aAllPlayersInfo.Count();
139  if (count != m_aPlayerScores.Count())
140  {
141  // Update faction array
142  m_aPlayerScores.Clear();
144  {
145  int score = -1000;
146  if (info)
147  {
148  score = info.GetScore();
149  }
150 
151  m_aPlayerScores.Insert(score);
152  }
153 
154  return true;
155  }
156 
157  bool changed = false;
158  for (int i = 0; i < count; i++)
159  {
161  if (!info)
162  continue;
163 
164  int score = info.GetScore();
165  if (score != m_aPlayerScores.Get(i))
166  {
167  m_aPlayerScores.Set(i, score);
168  changed = true;
169  }
170  }
171 
172  return changed;
173  }
174 
175  //------------------------------------------------------------------------------------------------
176  bool ScoreMaxChanged()
177  {
178  int count = m_aAllPlayersInfo.Count();
179  if (count != m_aPlayerScoresMax.Count())
180  {
181  // Update faction array
182  m_aPlayerScoresMax.Clear();
184  {
185  int scoreMax = -1000;
186  if (info)
187  {
188  scoreMax = info.GetScoreMax();
189  }
190 
191  m_aPlayerScoresMax.Insert(scoreMax);
192  }
193 
194  return true;
195  }
196 
197  bool changed = false;
198  for (int i = 0; i < count; i++)
199  {
201  if (!info)
202  continue;
203 
204  int scoreMax = info.GetScoreMax();
205  if (scoreMax != m_aPlayerScoresMax.Get(i))
206  {
207  m_aPlayerScoresMax.Set(i, scoreMax);
208  changed = true;
209  }
210  }
211 
212  return changed;
213  }
214 
215  //------------------------------------------------------------------------------------------------
216  bool NameChanged()
217  {
218  int count = m_aAllPlayersInfo.Count();
219  if (count != m_aPlayerNames.Count())
220  {
221  // Update faction array
222  m_aPlayerNames.Clear();
224  {
225  string name = "";
226  if (info)
227  name = info.GetName();
228 
229  m_aPlayerNames.Insert(name);
230  }
231 
232  return true;
233  }
234 
235  bool changed = false;
236  for (int i = 0; i < count; i++)
237  {
239  if (!info)
240  continue;
241 
242  string name = info.GetName();
243  if (name != m_aPlayerNames.Get(i))
244  {
245  m_aPlayerNames.Set(i, name);
246  changed = true;
247  }
248  }
249 
250  return changed;
251  }
252 
253  //------------------------------------------------------------------------------------------------
254  // Check if the count of players in game has changed.
255  bool PlayerCountChangedInArea()
256  {
257  // Check if the array exists
258  if (!m_aPlayersInArea)
259  return false;
260 
261  // Count how many players is in game and compare it to older value
262  int currentPlayersCountInArea = m_aPlayersInArea.Count();
263  if (currentPlayersCountInArea == m_iPlayersInAreaCount)
264  return false;
265 
266  m_iPlayersInAreaCount = currentPlayersCountInArea;
267  return true;
268  }
269 
270  //------------------------------------------------------------------------------------------------
271  //------------------------------------ SCORE PART END --------------------------------------------
272  //------------------------------------------------------------------------------------------------
273  override void EOnFrame(IEntity owner, float timeSlice)
274  {
275 
276  if (!m_ScoringSystem)
277  return;
278 
279  m_ScoringSystem.GetAllPlayersScoreInfo(m_aAllPlayersInfo);
280 
281  if (PlayerCountChangedInArea())
282  {
283  GenerateRows();
284  UpdateScoreboardData();
285  }
286 
287  if (ScoreChanged() || NameChanged() || ScoreMaxChanged())
288  UpdateScoreboardData();
289 
290  }
291 
292  //------------------------------------------------------------------------------------------------
293  override void OnActivate(IEntity ent)
294  {
295  int iLocalPlayerID = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(ent);
296  RegisterPlayerInArea(iLocalPlayerID);
297 
298  // Local player ID on client
299  IEntity playerEnt = SCR_PlayerController.GetLocalControlledEntity();
300 
301  if (playerEnt == ent)
302  {
303  // Create widget
304  m_wRoot = GetGame().GetWorkspace().CreateWidgets("{1B8C7223B314408B}UI/layouts/HUD/FiringRangeScoreboard/FireRangeTable.layout");
305  if (!m_wRoot)
306  return;
307 
308  m_wTable = m_wRoot.FindAnyWidget("Table");
309  }
310  }
311 
312  //------------------------------------------------------------------------------------------------
313  override void OnDeactivate(IEntity ent)
314  {
315  int iLocalPlayerID = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(ent);
316  RemovePlayerFromArea(iLocalPlayerID);
317 
318  // Make sure widget is created only on local machine
319  int playerID = SCR_PlayerController.GetLocalPlayerId();
320 
321  // Local player ID on client
322  IEntity playerEnt = SCR_PlayerController.GetLocalControlledEntity();
323 
324  if (playerEnt == ent)
325  {
326  // Find local player controller
327  PlayerController playerController = GetGame().GetPlayerController();
328  if (!playerController)
329  return;
330 
331  // Find firing range network entity to send RPC to server and Clear player score
332  SCR_FiringRangeNetworkEntity firingRangeNetworkEntity = SCR_FiringRangeNetworkEntity.GetInstance();
333  if (!firingRangeNetworkEntity)
334  return;
335  }
336  }
337 
338  //------------------------------------------------------------------------------------------------
340  bool IsProxy()
341  {
342  if (!m_RplComponent)
343  m_RplComponent = RplComponent.Cast(FindComponent(RplComponent));
344 
345  return (m_RplComponent && m_RplComponent.IsProxy());
346  }
347 
348  //------------------------------------------------------------------------------------------------
349  void GenerateRows()
350  {
351  if (!m_wTable)
352  return;
353 
354  // Delete and generate new rows for current players
355  foreach (Widget w : m_aPlayerWidgets)
356  {
357  w.RemoveFromHierarchy();
358  }
359  m_aPlayerWidgets.Clear();
360 
361 
363  {
364  Widget w = GetGame().GetWorkspace().CreateWidgets("{8643D226A0A0A299}UI/layouts/HUD/FiringRangeScoreboard/FireRangeRow.layout", m_wTable);
365  m_aPlayerWidgets.Insert(w);
366 
367  // if the player isn't in Firing range area, hide his line
368  int id = info.GetPlayerID();
369  if (!IsPlayerInFiringRangeArea(id))
370  w.SetVisible(false);
371  }
372 
373  // when it's done, update also the data in the rows for the first time.
374  UpdateScoreboardData();
375  }
376 
377  //------------------------------------------------------------------------------------------------
378  override void EOnInit(IEntity owner)
379  {
380  ArmaReforgerScripted game = GetGame();
381  if (!game)
382  return;
383 
385  if (!gameMode)
386  return;
387 
388  m_ScoringSystem = SCR_FiringRangeScoringComponent.Cast(owner.FindComponent(SCR_FiringRangeScoringComponent));
389 
390  if (s_FiringRangeManagerMain == this)
391  {
392  gameMode.GetOnPlayerConnected().Insert(SpawnCommunicationPrefab);
393 
394  array<int> playerIds = new array<int>();
395  GetGame().GetPlayerManager().GetPlayers(playerIds);
396 
397  foreach (int id : playerIds)
398  {
399  SpawnCommunicationPrefab(id);
400  }
401  }
402  }
403 
404  //------------------------------------------------------------------------------------------------
407  void RegisterFiringRangeController(IEntity firingLineController)
408  {
409  m_aFiringLineControllers.Insert(firingLineController);
410  }
411 
412  //------------------------------------------------------------------------------------------------
414  void RemoveAssignedPlayerFromFireline(int playerID)
415  {
416  // check who is the owner of the target and if it's player, reset it to default
417  foreach (IEntity currentElement: m_aFiringLineControllers)
418  {
419  SCR_FiringRangeController controller = SCR_FiringRangeController.Cast(currentElement);
420  if (controller.GetFiringLineOwnerId() == playerID)
421  controller.BackToDefaultTarget();
422  }
423  }
424 
425  //------------------------------------------------------------------------------------------------
427  int GetPlayerID(IEntity ent)
428  {
429  return GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(ent);
430  }
431 
432  //------------------------------------------------------------------------------------------------
434  void RegisterPlayerInArea(int playerID)
435  {
436  m_aPlayersInArea.Insert(playerID);
437  Replication.BumpMe();
438  }
439 
440  //------------------------------------------------------------------------------------------------
442  void RemovePlayerFromArea(int playerID)
443  {
444  m_aPlayersInArea.RemoveItem(playerID);
445  Replication.BumpMe();
446 
447  RpcAsk_ClearWidgets(playerID);
448  Rpc(RpcAsk_ClearWidgets, playerID);
449  }
450 
451  //------------------------------------------------------------------------------------------------
453  bool IsPlayerInFiringRangeArea(int playerID)
454  {
455  if (m_aPlayersInArea.Find(playerID) != -1)
456  return true;
457  return false;
458  }
459 
460  //------------------------------------------------------------------------------------------------
462  SCR_FiringRangeManager GetInstance()
463  {
464  return m_FiringRangeManagerInstance;
465  }
466 
467  //------------------------------------------------------------------------------------------------
469  static SCR_FiringRangeManager GetMainManagerInstance()
470  {
471  return s_FiringRangeManagerMain;
472  }
473 
474  //------------------------------------------------------------------------------------------------
476  Widget GetRootWidget()
477  {
478  return m_wRoot;
479  }
480 
481  //------------------------------------------------------------------------------------------------
483  string GetPlayerName(IEntity ent)
484  {
485  auto pm = GetGame().GetPlayerManager();
486  int iPlayerID = pm.GetPlayerIdFromControlledEntity(ent);
487  return pm.GetPlayerName(iPlayerID);
488  }
489 
490  //------------------------------------------------------------------------------------------------
491  void SpawnCommunicationPrefab(int playerID)
492  {
493  // network entity resource
494  Resource resource = Resource.Load(m_ComEnt);
495  if (!resource.IsValid())
496  return;
497 
498  IEntity ent = GetGame().SpawnEntityPrefab(resource, GetGame().GetWorld());
499  if (!ent)
500  return;
501 
502  RplComponent rpl = RplComponent.Cast(ent.FindComponent(RplComponent));
503  if (!rpl)
504  return;
505 
506  SCR_FiringRangeNetworkEntity firingRangeNetworkEntity = SCR_FiringRangeNetworkEntity.Cast(ent);
507  if (!firingRangeNetworkEntity)
508  return;
509 
510  PlayerController playerController = GetGame().GetPlayerManager().GetPlayerController(playerID);
511  if (!playerController)
512  return;
513 
514  RplIdentity playerRplID = playerController.GetRplIdentity();
515  if (playerRplID == Replication.INVALID_IDENTITY)
516  return;
517 
518  rpl.Give(playerRplID);
519  firingRangeNetworkEntity.RegisterCommEntity(Replication.FindId(ent));
520  }
521 
522  //------------------------------------------------------------------------------------------------
523  //------------------------------------------ MP --------------------------------------------------
524  //------------------------------------------------------------------------------------------------
525  //------------------------------------------------------------------------------------------------
526  void AddIndicator(vector localCoordOfHit, vector localVectorOfHit, IEntity pOwnerEntity)
527 
528  {
529  // find the target controller
530  IEntity firingRangeController = pOwnerEntity.GetParent();
531  if (!firingRangeController)
532  return;
533 
534  // run it on all clients
535  Rpc(RpcAsk_AddIndicator, Replication.FindId(firingRangeController), localCoordOfHit, localVectorOfHit);
536  RpcAsk_AddIndicator(Replication.FindId(firingRangeController),localCoordOfHit,localVectorOfHit);
537  }
538 
539  //------------------------------------------------------------------------------------------------
540  void RemoveIndicators(RplId controllerReplicationId)
541  {
542  Rpc(RpcAsk_RemoveIndicators, controllerReplicationId);
543  RpcAsk_RemoveIndicators(controllerReplicationId);
544  }
545 
546  //------------------------------------------------------------------------------------------------
547  void ControllerLight(notnull IEntity firingRangeController, ControllerLightType light, bool mode)
548  {
549  Rpc(RpcAsk_ControllerLight, Replication.FindId(firingRangeController), light, mode);
550  RpcAsk_ControllerLight(Replication.FindId(firingRangeController), light, mode);
551  }
552 
553  //------------------------------------------------------------------------------------------------
554  void SetControllerCounter(notnull IEntity firingRangeController, EControlerSection type, int value)
555  {
556  Rpc(RpcAsk_SetControllerCounter, Replication.FindId(firingRangeController), type, value);
557  RpcAsk_SetControllerCounter(Replication.FindId(firingRangeController), type, value);
558  }
559 
560  //------------------------------------------------------------------------------------------------
561  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
562  protected void RpcAsk_RemoveIndicators(RplId controllerReplicationId)
563  {
564  SCR_FiringRangeController m_LineController = SCR_FiringRangeController.Cast(Replication.FindItem(controllerReplicationId));
565  if (m_LineController)
566  m_LineController.RemoveIndicators();
567  }
568 
569  //------------------------------------------------------------------------------------------------
570  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
571  void RpcAsk_AddIndicator(RplId controllerReplicationId, vector localCoordOfHit, vector localVectorOfHit)
572  {
573  SCR_FiringRangeController m_LineController = SCR_FiringRangeController.Cast(Replication.FindItem(controllerReplicationId));
574  if (m_LineController)
575  m_LineController.AddIndicator(localCoordOfHit,localVectorOfHit);
576 
577  }
578 
579  //------------------------------------------------------------------------------------------------
580  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
581  protected void RpcAsk_ClearWidgets(int playerID)
582  {
583  // Local player ID on client
584  int localPlayerID = SCR_PlayerController.GetLocalPlayerId();
585  if (localPlayerID == playerID)
586  {
587  // Remove all widgets
588  m_aPlayerWidgets.Clear();
589  if (!m_wRoot)
590  return;
591  m_wRoot.RemoveFromHierarchy();
592  }
593  }
594 
595  //------------------------------------------------------------------------------------------------
596  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
597  protected void RpcAsk_ControllerLight(RplId controllerReplicationId, ControllerLightType light, bool mode)
598  {
599  SCR_FiringRangeController m_LineController = SCR_FiringRangeController.Cast(Replication.FindItem(controllerReplicationId));
600  if (m_LineController)
601  m_LineController.SetControllerLight(light, mode);
602  }
603 
604  //------------------------------------------------------------------------------------------------
605  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
606  protected void RpcAsk_SetControllerCounter(RplId controllerReplicationId, EControlerSection type, int value)
607  {
608  SCR_FiringRangeController m_LineController = SCR_FiringRangeController.Cast(Replication.FindItem(controllerReplicationId));
609  if (m_LineController)
610  m_LineController.SetControllerCounter(type, value);
611  }
612 
613  //------------------------------------------------------------------------------------------------
614  protected bool CheckMasterOnlyMethod(string methodName)
615  {
616  if (IsProxy())
617  {
618  Print("Master-only method (SCR_FiringRangeController." + methodName + ") called on proxy. Some functionality might be broekn!", LogLevel.WARNING);
619  return false;
620  }
621 
622  return true;
623  }
624 
625  //------------------------------------------------------------------------------------------------
626 
627  void SCR_FiringRangeManager(IEntitySource src, IEntity parent)
628  {
629  SetEventMask(EntityEvent.INIT | EntityEvent.FRAME);
630  //If this instance is the 1st FiringRangeManager in the world, make it main.
631  if (s_FiringRangeManagerMain == null)
632  s_FiringRangeManagerMain = this;
633 
634  m_FiringRangeManagerInstance = this;
635  }
636 
637  //------------------------------------------------------------------------------------------------
638  void ~SCR_FiringRangeManager()
639  {
640  if (m_wRoot)
641  m_wRoot.RemoveFromHierarchy();
642 
643  if (s_FiringRangeManagerMain == this)
644  s_FiringRangeManagerMain == null;
645  }
646 };
SCR_BaseGameMode
Definition: SCR_BaseGameMode.c:137
SCR_PlayerController
Definition: SCR_PlayerController.c:31
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
RplProp
SCR_RplTestEntityClass RplProp
Used for handling entity spawning requests for SCR_CatalogEntitySpawnerComponent and inherited classe...
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
EControlerSection
EControlerSection
Definition: SCR_FiringRangeController.c:744
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
SCR_PlayerScoreInfoFiringRange
Holds scoring data of players.
Definition: SCR_PlayerScoreInfoFiringRange.c:3
GetGameMode
SCR_BaseGameMode GetGameMode()
Definition: SCR_BaseGameModeComponent.c:15
ScriptedGameTriggerEntityClass
Definition: ScriptedGameTriggerEntity.c:12
SCR_FiringRangeManager
Definition: SCR_FiringRangeManager.c:7
Attribute
typedef Attribute
Post-process effect of scripted camera.
m_aAllPlayersInfo
protected ref array< ref SCR_PlayerScoreInfoFiringRange > m_aAllPlayersInfo
Contains score info of all the players.
Definition: SCR_FiringRangeScoringComponent.c:11
ControllerLightType
ControllerLightType
Definition: SCR_FiringRangeController.c:758
SCR_FiringRangeManagerClass
Definition: SCR_FiringRangeManager.c:2
SCR_FiringRangeNetworkEntity
Definition: SCR_FiringRangeNetworkEntity.c:7
m_RplComponent
protected RplComponent m_RplComponent
Definition: SCR_CampaignBuildingManagerComponent.c:42
SCR_FiringRangeScoringComponent
void SCR_FiringRangeScoringComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_FiringRangeScoringComponent.c:182
type
EDamageType type
Definition: SCR_DestructibleTreeV2.c:32
SCR_FiringRangeController
Definition: SCR_FiringRangeController.c:9
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180