Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
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")]
5
6//------------------------------------------------------------------------------------------------
7class 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;
31
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
96 foreach (int i, SCR_PlayerScoreInfoFiringRange info : m_aAllPlayersInfo)
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();
143 foreach (SCR_PlayerScoreInfoFiringRange info : m_aAllPlayersInfo)
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 {
160 SCR_PlayerScoreInfoFiringRange info = m_aAllPlayersInfo.Get(i);
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();
183 foreach (SCR_PlayerScoreInfoFiringRange info : m_aAllPlayersInfo)
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 {
200 SCR_PlayerScoreInfoFiringRange info = m_aAllPlayersInfo.Get(i);
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();
223 foreach (SCR_PlayerScoreInfoFiringRange info : m_aAllPlayersInfo)
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 {
238 SCR_PlayerScoreInfoFiringRange info = m_aAllPlayersInfo.Get(i);
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
362 foreach (SCR_PlayerScoreInfoFiringRange info : m_aAllPlayersInfo)
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
384 SCR_BaseGameMode gameMode = SCR_BaseGameMode.Cast(GetGame().GetGameMode());
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 {
465 }
466
467 //------------------------------------------------------------------------------------------------
469 static SCR_FiringRangeManager GetMainManagerInstance()
470 {
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 SCR_PlayerNamesFilterCache.GetInstance().GetPlayerDisplayName(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.FindItemId(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.FindItemId(firingRangeController), localCoordOfHit, localVectorOfHit);
536 RpcAsk_AddIndicator(Replication.FindItemId(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.FindItemId(firingRangeController), light, mode);
550 RpcAsk_ControllerLight(Replication.FindItemId(firingRangeController), light, mode);
551 }
552
553 //------------------------------------------------------------------------------------------------
554 void SetControllerCounter(notnull IEntity firingRangeController, EControlerSection type, int value)
555 {
556 Rpc(RpcAsk_SetControllerCounter, Replication.FindItemId(firingRangeController), type, value);
557 RpcAsk_SetControllerCounter(Replication.FindItemId(firingRangeController), type, value);
558 }
559
560 //------------------------------------------------------------------------------------------------
561 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
562 protected void RpcAsk_RemoveIndicators(RplId controllerReplicationId)
563 {
566 m_LineController.RemoveIndicators();
567 }
568
569 //------------------------------------------------------------------------------------------------
570 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
571 void RpcAsk_AddIndicator(RplId controllerReplicationId, vector localCoordOfHit, vector localVectorOfHit)
572 {
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 {
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 {
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
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)
633
635 }
636
637 //------------------------------------------------------------------------------------------------
639 {
640 if (m_wRoot)
641 m_wRoot.RemoveFromHierarchy();
642
643 if (s_FiringRangeManagerMain == this)
645 }
646};
ref DSGameConfig game
Definition DSConfig.c:81
ArmaReforgerScripted GetGame()
Definition game.c:1398
SCR_BaseGameMode GetGameMode()
SCR_CacheNoteComponentClass ScriptComponentClass RplProp()] protected ref array< string > m_aLines
override void OnDeactivate()
override void OnActivate()
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
EDamageType type
override int GetPlayerID()
SCR_FiringRangeLineAreaClass m_LineController
Firing range controller.
Widget GetRootWidget()
enum EVehicleType IEntity
proto external Managed FindComponent(typename typeName)
proto external IEntity GetParent()
Main replication API.
Definition Replication.c:14
Replication item identifier.
Definition RplId.c:14
ScriptInvokerBase< SCR_BaseGameMode_PlayerId > GetOnPlayerConnected()
void RpcAsk_SetControllerCounter(RplId controllerReplicationId, EControlerSection type, int value)
void RpcAsk_AddIndicator(RplId controllerReplicationId, vector localCoordOfHit, vector localVectorOfHit)
void SCR_FiringRangeManager(IEntitySource src, IEntity parent)
static SCR_FiringRangeManager s_FiringRangeManagerMain
void RpcAsk_RemoveIndicators(RplId controllerReplicationId)
void RpcAsk_ControllerLight(RplId controllerReplicationId, ControllerLightType light, bool mode)
SCR_FiringRangeManager m_FiringRangeManagerInstance
bool CheckMasterOnlyMethod(string methodName)
void RpcAsk_ClearWidgets(int playerID)
static int GetLocalPlayerId()
Returns either a valid ID of local player or 0.
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute
EntityEvent
Various entity events.
Definition EntityEvent.c:14
void RplRpc(RplChannel channel, RplRcver rcver, RplCondition condition=RplCondition.None, string customConditionName="")
Definition EnNetwork.c:95
int RplIdentity
Definition EnNetwork.c:32
RplRcver
Definition RplRcver.c:59
RplChannel
Communication channel. Reliable is guaranteed to be delivered. Unreliable not.
Definition RplChannel.c:14
int RplId
Definition EnNetwork.c:33