Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_BaseScoringSystemComponent.c
Go to the documentation of this file.
4
5void OnPlayerEventDelegate(int playerId);
7typedef ScriptInvokerBase<OnPlayerEventDelegate> OnPlayerEventInvoker;
8
9void OnPlayerScoreChangedDelegate(int playerId, SCR_ScoreInfo scoreInfo);
11typedef ScriptInvokerBase<OnPlayerScoreChangedDelegate> OnPlayerScoreChangedInvoker;
12
15typedef ScriptInvokerBase<OnFactionScoreChangedDelegate> OnFactionScoreChangedInvoker;
16
27{
31
34
37
38 //------------------------------------------------------------------------------------------------
47
50
51 //------------------------------------------------------------------------------------------------
60
63
64 //------------------------------------------------------------------------------------------------
73
76
77 //------------------------------------------------------------------------------------------------
86
87 //------------------------------------------------------------------------------------------------
91 override bool RplSave(ScriptBitWriter writer)
92 {
93 super.RplSave(writer);
94
95 int players = m_mPlayerScores.Count();
96 writer.WriteInt(players);
97
98 foreach (int playerId, SCR_ScoreInfo scoreInfo : m_mPlayerScores)
99 {
100 writer.WriteInt(playerId);
101 scoreInfo.RplSave(writer);
102 }
103
104 int factions = m_mFactionScores.Count();
105 writer.WriteInt(factions);
106
107 FactionManager factionManager = GetGame().GetFactionManager();
108 if (!factionManager)
109 {
110 Print("Score serialization fail, no FactionManager present in the world!", LogLevel.WARNING);
111 return false;
112 }
113
114 foreach (Faction faction, SCR_ScoreInfo scoreInfo : m_mFactionScores)
115 {
116 int factionId = factionManager.GetFactionIndex(faction);
117 writer.WriteInt(factionId);
118 scoreInfo.RplSave(writer);
119 }
120
121 return true;
122 }
123
124 //------------------------------------------------------------------------------------------------
128 override bool RplLoad(ScriptBitReader reader)
129 {
130 super.RplLoad(reader);
131
132 int players;
133 if (!reader.ReadInt(players))
134 return false;
135
136 array<int> playerIds = {};
137 array<ref SCR_ScoreInfo> playerScores = {};
138
139 for (int i = 0; i < players; i++)
140 {
141 int playerId;
142 SCR_ScoreInfo scoreInfo = new SCR_ScoreInfo();
143
144 if (!reader.ReadInt(playerId))
145 return false;
146
147 if (!scoreInfo.RplLoad(reader))
148 return false;
149
150 playerIds.Insert(playerId);
151 playerScores.Insert(scoreInfo);
152 }
153
154 int factions;
155 if (!reader.ReadInt(factions))
156 return false;
157
158 array<int> factionIds = {};
159 array<ref SCR_ScoreInfo> factionScores = {};
160
161 SCR_ScoreInfo scoreInfo;
162 for (int i = 0; i < factions; i++)
163 {
164 int factionId;
165 scoreInfo = new SCR_ScoreInfo();
166
167 if (!reader.ReadInt(factionId))
168 return false;
169
170 if (!scoreInfo.RplLoad(reader))
171 return false;
172
173 factionIds.Insert(factionId);
174 factionScores.Insert(scoreInfo);
175 }
176
177 // Fill our instance with newly received data
178 m_mPlayerScores.Clear();
179 for (int i = 0; i < players; i++)
180 {
181 m_mPlayerScores.Insert(playerIds[i], playerScores[i]);
182 }
183
184 FactionManager factionManager = GetGame().GetFactionManager();
185 if (!factionManager)
186 {
187 Print("Score deserialization fail, no FactionManager present in the world, faction scoring will not work properly!", LogLevel.WARNING);
188 return false;
189 }
190
191 array<Faction> _ = {};
192 int facCnt = factionManager.GetFactionsList(_);
193 m_mFactionScores.Clear();
194 Faction faction;
195 for (int i = 0; i < factions; i++)
196 {
197 int factionIndex = factionIds[i];
198 Print(string.Format("idx=%1, facIdx=%2, facMan=%3, facCnt=%4", i, factionIndex, factionManager, facCnt), LogLevel.NORMAL);
199 faction = factionManager.GetFactionByIndex(factionIndex);
200 m_mFactionScores.Insert(faction, factionScores[i]);
201
202 }
203
204 return true;
205 }
206
207 //------------------------------------------------------------------------------------------------
210 private int GetPlayerFactionIndex(int playerId)
211 {
212 SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
213 if (factionManager)
214 {
215 Faction playerFaction = factionManager.GetPlayerFaction(playerId);
216 return factionManager.GetFactionIndex(playerFaction);
217 }
218
219 return -1;
220 }
221
222 //------------------------------------------------------------------------------------------------
225 private Faction GetFactionByIndex(int factionIndex)
226 {
227 if (factionIndex < 0)
228 return null;
229
230 return GetGame().GetFactionManager().GetFactionByIndex(factionIndex);
231 }
232
233 //------------------------------------------------------------------------------------------------
236 private int GetFactionIndex(Faction faction)
237 {
238 if (!faction)
239 return -1;
240
241 return GetGame().GetFactionManager().GetFactionIndex(faction);
242 }
243
244 //------------------------------------------------------------------------------------------------
245 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
246 private void RpcDo_AddKill(int playerId, int factionIdx, int count)
247 {
248 SCR_ScoreInfo playerScore = m_mPlayerScores[playerId];
249 playerScore.m_iKills += count;
250 OnPlayerScoreChanged(playerId, playerScore);
251
252 Faction faction = GetFactionByIndex(factionIdx);
253 if (faction)
254 {
255 SCR_ScoreInfo factionScore = m_mFactionScores[faction];
256 factionScore.m_iKills += count;
257 OnFactionScoreChanged(faction, factionScore);
258 }
259 }
260
261 //------------------------------------------------------------------------------------------------
266 void AddKill(int playerId, int count = 1)
267 {
268 // Server only
269 if (!m_pGameMode.IsMaster())
270 return;
271
272 int factionIdx = GetPlayerFactionIndex(playerId);
273 RpcDo_AddKill(playerId, factionIdx, count);
274 Rpc(RpcDo_AddKill, playerId, factionIdx, count);
275 }
276
277 //------------------------------------------------------------------------------------------------
278 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
279 private void RpcDo_AddDeath(int playerId, int factionIdx, int count)
280 {
281 SCR_ScoreInfo playerScore = m_mPlayerScores[playerId];
282 playerScore.m_iDeaths += count;
283 OnPlayerScoreChanged(playerId, playerScore);
284
285 Faction faction = GetFactionByIndex(factionIdx);
286 if (faction)
287 {
288 SCR_ScoreInfo factionScore = m_mFactionScores[faction];
289 factionScore.m_iDeaths += count;
290 OnFactionScoreChanged(faction, factionScore);
291 }
292 }
293
294 //------------------------------------------------------------------------------------------------
299 void AddDeath(int playerId, int count = 1)
300 {
301 // Server only
302 if (!m_pGameMode.IsMaster())
303 return;
304
305 int factionIdx = GetPlayerFactionIndex(playerId);
306 RpcDo_AddDeath(playerId, factionIdx, count);
307 Rpc(RpcDo_AddDeath, playerId, factionIdx, count);
308 }
309
310 //------------------------------------------------------------------------------------------------
311 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
312 private void RpcDo_AddTeamKill(int playerId, int factionIdx, int count)
313 {
314 SCR_ScoreInfo playerScore = m_mPlayerScores[playerId];
315 playerScore.m_iTeamKills += count;
316 OnPlayerScoreChanged(playerId, playerScore);
317
318 Faction faction = GetFactionByIndex(factionIdx);
319 if (faction)
320 {
321 SCR_ScoreInfo factionScore = m_mFactionScores[faction];
322 factionScore.m_iTeamKills += count;
323 OnFactionScoreChanged(faction, factionScore);
324 }
325 }
326
327 //------------------------------------------------------------------------------------------------
332 void AddTeamKill(int playerId, int count = 1)
333 {
334 // Server only
335 if (!m_pGameMode.IsMaster())
336 return;
337
338 int factionIdx = GetPlayerFactionIndex(playerId);
339 RpcDo_AddTeamKill(playerId, factionIdx, count);
340 Rpc(RpcDo_AddTeamKill, playerId, factionIdx, count);
341 }
342
343 //------------------------------------------------------------------------------------------------
344 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
345 private void RpcDo_AddSuicide(int playerId, int factionIdx, int count)
346 {
347 SCR_ScoreInfo playerScore = m_mPlayerScores[playerId];
348 playerScore.m_iSuicides += count;
349 OnPlayerScoreChanged(playerId, playerScore);
350
351 Faction faction = GetFactionByIndex(factionIdx);
352 if (faction)
353 {
354 SCR_ScoreInfo factionScore = m_mFactionScores[faction];
355 factionScore.m_iSuicides += count;
356 OnFactionScoreChanged(faction, factionScore);
357 }
358 }
359
360 //------------------------------------------------------------------------------------------------
365 void AddSuicide(int playerId, int count = 1)
366 {
367 // Server only
368 if (!m_pGameMode.IsMaster())
369 return;
370
371 int factionIdx = GetPlayerFactionIndex(playerId);
372 RpcDo_AddSuicide(playerId, factionIdx, count);
373 Rpc(RpcDo_AddSuicide, playerId, factionIdx, count);
374 }
375
376 //------------------------------------------------------------------------------------------------
377 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
378 private void RpcDo_AddObjective(int playerId, int factionIdx, int count)
379 {
380 SCR_ScoreInfo playerScore = m_mPlayerScores[playerId];
381 playerScore.m_iObjectives += count;
382 OnPlayerScoreChanged(playerId, playerScore);
383
384 Faction faction = GetFactionByIndex(factionIdx);
385 if (faction)
386 {
387 SCR_ScoreInfo factionScore = m_mFactionScores[faction];
388 factionScore.m_iObjectives += count;
389 OnFactionScoreChanged(faction, factionScore);
390 }
391 }
392
393 //------------------------------------------------------------------------------------------------
399 void AddObjective(int playerId, int count = 1, bool addToFaction = true)
400 {
401 // Server only
402 if (!m_pGameMode.IsMaster())
403 return;
404
405 int factionIdx = -1;
406 if (addToFaction)
407 factionIdx = GetPlayerFactionIndex(playerId);
408
409 RpcDo_AddObjective(playerId, factionIdx, count);
410 Rpc(RpcDo_AddObjective, playerId, factionIdx, count);
411 }
412
413 //------------------------------------------------------------------------------------------------
414 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
415 private void RpcDo_AddFactionObjective(int factionIdx, int count)
416 {
417 Faction faction = GetFactionByIndex(factionIdx);
418 if (faction)
419 {
420 SCR_ScoreInfo factionScore = m_mFactionScores[faction];
421 factionScore.m_iObjectives += count;
422 OnFactionScoreChanged(faction, factionScore);
423 }
424 }
425
426 //------------------------------------------------------------------------------------------------
431 void AddFactionObjective(notnull Faction faction, int count = 1)
432 {
433 // Server only
434 if (!m_pGameMode.IsMaster())
435 return;
436
437 int factionIdx = GetFactionIndex(faction);
438 RpcDo_AddFactionObjective(factionIdx, count);
439 Rpc(RpcDo_AddFactionObjective, factionIdx, count);
440 }
441
442 //------------------------------------------------------------------------------------------------
446 protected void OnPlayerScoreChanged(int playerId, SCR_ScoreInfo scoreInfo)
447 {
449 m_OnPlayerScoreChangedInvoker.Invoke(playerId, scoreInfo);
450
451 GameStatsApi statsApi = GetGame().GetStatsApi();
452 if (statsApi)
453 statsApi.PlayerScore(playerId, scoreInfo);
454 }
455
456 //------------------------------------------------------------------------------------------------
460 protected void OnFactionScoreChanged(Faction faction, SCR_ScoreInfo scoreInfo)
461 {
463 m_OnFactionScoreChangedInvoker.Invoke(faction, scoreInfo);
464 }
465
466 //------------------------------------------------------------------------------------------------
471 protected int CalculateScore(SCR_ScoreInfo info)
472 {
473 int val = (info.m_iKills + info.m_iObjectives) - info.m_iSuicides - info.m_iTeamKills;
474 if (val < 0)
475 return 0;
476
477 return val;
478 }
479
480 //------------------------------------------------------------------------------------------------
482 int GetPlayerScore(int playerId)
483 {
484 SCR_ScoreInfo info = GetPlayerScoreInfo(playerId);
485 if (!info)
486 return false;
487
488 return CalculateScore(info);
489 }
490
491 //------------------------------------------------------------------------------------------------
494 {
495 if (!m_mPlayerScores.Contains(playerId))
496 return null;
497
498 return m_mPlayerScores[playerId];
499 }
500
501 //------------------------------------------------------------------------------------------------
503 int GetFactionScore(notnull Faction faction)
504 {
505 SCR_ScoreInfo info = GetFactionScoreInfo(faction);
506 if (!info)
507 return false;
508
509 return CalculateScore(info);
510 }
511
512 //------------------------------------------------------------------------------------------------
515 {
516 if (!m_mFactionScores.Contains(faction))
517 return null;
518
519 return m_mFactionScores[faction];
520 }
521
522 //------------------------------------------------------------------------------------------------
523 override void OnPlayerRegistered(int playerId)
524 {
525 super.OnPlayerRegistered(playerId);
526
527 // Register player info
528 if (!m_mPlayerScores.Contains(playerId))
529 {
530 SCR_ScoreInfo scoreInfo = new SCR_ScoreInfo();
531 m_mPlayerScores.Insert(playerId, scoreInfo);
533 m_OnPlayerScoreChangedInvoker.Invoke(playerId, scoreInfo);
534 }
535
536 if (m_OnPlayerAdded)
537 m_OnPlayerAdded.Invoke(playerId);
538 }
539
540 //------------------------------------------------------------------------------------------------
541 override void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)
542 {
543 super.OnPlayerDisconnected(playerId, cause, timeout);
544
546 m_OnPlayerRemoved.Invoke(playerId);
547 }
548
549 //------------------------------------------------------------------------------------------------
550 override void EOnDiag(IEntity owner, float timeSlice)
551 {
552 super.EOnDiag(owner, timeSlice);
553
554 #ifdef ENABLE_DIAG
555 if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_SCORING_SYSTEM))
556 {
557 string title = Type().ToString() + " Diagnostics";
558 DbgUI.Begin(title);
559 {
560 DbgUI.Text("--Faction Scoring--");
561 const string factionFormat = "[%1] Score=%2 || Kills=%3 | Deaths=%4 | TeamKills=%5 | Suicides=%6 | Objectives=%7";
562 foreach (Faction faction, SCR_ScoreInfo scoreInfo : m_mFactionScores)
563 {
564 int score = CalculateScore(scoreInfo);
565 string factionText = string.Format(factionFormat, faction.GetFactionKey(), score, scoreInfo.m_iKills, scoreInfo.m_iDeaths, scoreInfo.m_iTeamKills, scoreInfo.m_iSuicides, scoreInfo.m_iObjectives);
566 DbgUI.Text(factionText);
567 }
568
569 DbgUI.Text("--Player Scoring--");
570 const string playerFormat = "pId=%1: \"%2\" Score=%3 || Kills=%4 | Deaths=%5 | TeamKills=%6 | Suicides=%7 | Objectives=%8";
571 foreach (int playerId, SCR_ScoreInfo scoreInfo : m_mPlayerScores)
572 {
573 string name = GetGame().GetPlayerManager().GetPlayerName(playerId);
574 int score = CalculateScore(scoreInfo);
575 string playerText = string.Format(playerFormat, playerId, name, score, scoreInfo.m_iKills, scoreInfo.m_iDeaths, scoreInfo.m_iTeamKills, scoreInfo.m_iSuicides, scoreInfo.m_iObjectives);
576 DbgUI.Text(playerText);
577 }
578 }
579 DbgUI.End();
580
581 int targetId = 1;
582 DbgUI.InputInt("Player Id", targetId);
583
584 int cnt = 1;
585 DbgUI.InputInt("n (count): ", cnt);
586
587 if (DbgUI.Button("Add n Kills"))
588 AddKill(targetId, cnt);
589 if (DbgUI.Button("Add n Deaths"))
590 AddDeath(targetId, cnt);
591 if (DbgUI.Button("Add n TeamKills"))
592 AddTeamKill(targetId, cnt);
593 if (DbgUI.Button("Add n Suicides"))
594 AddSuicide(targetId, cnt);
595 if (DbgUI.Button("Add n Objectives"))
596 AddObjective(targetId, cnt);
597 if (DbgUI.Button("Add n Faction Objectives (Player Faction Only)"))
598 {
599 int facIdx = GetPlayerFactionIndex(targetId);
600 Faction f = GetFactionByIndex(facIdx);
601 AddFactionObjective(f, cnt);
602 }
603 }
604 #endif
605 }
606
607 //------------------------------------------------------------------------------------------------
610 {
611 return -1;
612 }
613
614 //------------------------------------------------------------------------------------------------
615 override void EOnInit(IEntity owner)
616 {
617 super.EOnInit(owner);
618
619 FactionManager factionManager = GetGame().GetFactionManager();
620 if (factionManager)
621 {
622 array<Faction> factions = {};
623 factionManager.GetFactionsList(factions);
624
625 foreach (Faction faction : factions)
626 {
627 m_mFactionScores.Insert(faction, new SCR_ScoreInfo);
628 }
629 }
630
631 #ifdef ENABLE_DIAG
632 DiagMenu.RegisterBool(SCR_DebugMenuID.DEBUGUI_SCORING_SYSTEM, "", "Scoring System", "GameMode");
633 #endif
634 }
635
636 //------------------------------------------------------------------------------------------------
637 override void OnPostInit(IEntity owner)
638 {
639 super.OnPostInit(owner);
640
641 SetEventMask(owner, EntityEvent.INIT);
642 #ifdef ENABLE_DIAG
643 ConnectToDiagSystem(owner);
644 #endif
645 }
646
647 //------------------------------------------------------------------------------------------------
648 override void OnDelete(IEntity owner)
649 {
650 #ifdef ENABLE_DIAG
651 DisconnectFromDiagSystem(owner);
652 #endif
653
654 super.OnDelete(owner);
655 }
656}
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition DebugMenuID.c:4
ArmaReforgerScripted GetGame()
Definition game.c:1398
int GetFactionIndex()
SCR_BaseGameModeComponentClass m_pGameMode
The game mode entity this component is attached to.
void SCR_BaseGameModeComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
ScriptInvokerBase< OnFactionScoreChangedDelegate > OnFactionScoreChangedInvoker
ScriptInvokerBase< OnPlayerEventDelegate > OnPlayerEventInvoker
ScriptInvokerBase< OnPlayerScoreChangedDelegate > OnPlayerScoreChangedInvoker
func OnFactionScoreChangedDelegate
func OnPlayerEventDelegate
func OnPlayerScoreChangedDelegate
Get all prefabs that have the spawner the given labels and are valid in the editor mode param catalogType Type to catalog to get prefabs from param editorMode Editor mode to get valid entries from param faction Faction(Optional)
void SCR_FactionManager(IEntitySource src, IEntity parent)
int Type
Definition DbgUI.c:66
Diagnostic and developer menu system.
Definition DiagMenu.c:18
Statistical Api - Analytics.
override void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)
ref OnPlayerScoreChangedInvoker m_OnPlayerScoreChangedInvoker
This invoker is invoked when score of provided player changes.
override bool RplLoad(ScriptBitReader reader)
SCR_ScoreInfo GetFactionScoreInfo(notnull Faction faction)
ref OnFactionScoreChangedInvoker m_OnFactionScoreChangedInvoker
This invoker is invoked when score of provided faction changes.
OnPlayerScoreChangedInvoker GetOnPlayerScoreChanged()
ref map< int, ref SCR_ScoreInfo > m_mPlayerScores
override void EOnDiag(IEntity owner, float timeSlice)
void OnFactionScoreChanged(Faction faction, SCR_ScoreInfo scoreInfo)
override bool RplSave(ScriptBitWriter writer)
void OnPlayerScoreChanged(int playerId, SCR_ScoreInfo scoreInfo)
OnFactionScoreChangedInvoker GetOnFactionScoreChanged()
ref map< Faction, ref SCR_ScoreInfo > m_mFactionScores
Map of scores per faction.
ref OnPlayerEventInvoker m_OnPlayerRemoved
This invoker is invoked when a player is registered to the scoreboard.
ref OnPlayerEventInvoker m_OnPlayerAdded
This invoker is invoked when a player is registered to the scoreboard.
Definition Types.c:486
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
EntityEvent
Various entity events.
Definition EntityEvent.c:14
void RplRpc(RplChannel channel, RplRcver rcver, RplCondition condition=RplCondition.None, string customConditionName="")
Definition EnNetwork.c:95
RplRcver
Definition RplRcver.c:59
RplChannel
Communication channel. Reliable is guaranteed to be delivered. Unreliable not.
Definition RplChannel.c:14