Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_PlayerFactionAffiliationComponent.c
Go to the documentation of this file.
5
8typedef ScriptInvokerBase<PlayerFactionRequestDelegate> OnPlayerFactionRequestInvoker;
9
10void PlayerFactionResponseDelegate(SCR_PlayerFactionAffiliationComponent component, int factionIndex, bool response);
12typedef ScriptInvokerBase<PlayerFactionResponseDelegate> OnPlayerFactionResponseInvoker;
13
16typedef ScriptInvokerBase<PlayerFactionChangedDelegate> OnPlayerFactionChangedInvoker;
17
21{
22 private PlayerController m_PlayerController;
23 private RplComponent m_RplComponent;
24 private SCR_RespawnComponent m_RespawnComponent;
25 private SCR_SpawnLockComponent m_Lock;
26
27 //------------------------------------------------------------------------------------------------
29 PlayerController GetPlayerController()
30 {
31 return m_PlayerController;
32 }
33
34 //------------------------------------------------------------------------------------------------
36 int GetPlayerId()
37 {
38 return GetPlayerController().GetPlayerId();
39 }
40
41 //------------------------------------------------------------------------------------------------
43 protected SCR_SpawnLockComponent GetLock()
44 {
45 return m_Lock;
46 }
47
48 // ON CAN FACTION REQUEST
50
51 //------------------------------------------------------------------------------------------------
57
59
60 //------------------------------------------------------------------------------------------------
66
67 // ON CAN FACTION RESPONSE
69
70 //------------------------------------------------------------------------------------------------
76
78
79 //------------------------------------------------------------------------------------------------
85
86 // ON FACTION REQUEST
88
89 //------------------------------------------------------------------------------------------------
95
97
98 //------------------------------------------------------------------------------------------------
104
105 //------------------------------------------------------------------------------------------------
106 // ON FACTION RESPONSE
108
109 //------------------------------------------------------------------------------------------------
115
117 //------------------------------------------------------------------------------------------------
123
125 //------------------------------------------------------------------------------------------------
131
132 //------------------------------------------------------------------------------------------------
133 protected bool IsOwner()
134 {
135 return !m_RplComponent || m_RplComponent.IsOwner();
136 }
137
138 //------------------------------------------------------------------------------------------------
139 protected bool IsProxy()
140 {
141 return m_RplComponent && m_RplComponent.IsProxy();
142 }
143
144 //------------------------------------------------------------------------------------------------
145 protected override void OnPostInit(IEntity owner)
146 {
147 super.OnPostInit(owner);
148 m_PlayerController = PlayerController.Cast(owner);
149 if (!m_PlayerController)
150 Debug.Error(string.Format("%1 is not attached to a %2", Type().ToString(), PlayerController));
151
152 m_RespawnComponent = SCR_RespawnComponent.Cast(m_PlayerController.GetRespawnComponent());
153 m_Lock = SCR_SpawnLockComponent.Cast(owner.FindComponent(SCR_SpawnLockComponent));
154 m_RplComponent = RplComponent.Cast(owner.FindComponent(RplComponent));
155
156 #ifdef ENABLE_DIAG
157 DiagMenu.RegisterBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_PLAYER_FACTION_DIAG, "", "Player Factions", "GameMode");
158 GetGame().GetCallqueue().CallLater(OnDiag, 0, true);
159 #endif
160 }
161
162 //------------------------------------------------------------------------------------------------
169 {
170 #ifdef _ENABLE_RESPAWN_LOGS
171 Print(string.Format("%1::RequestFaction(faction: %2)", Type().ToString(), faction), LogLevel.NORMAL);
172 #endif
173
174 // Lock this
175 int factionIndex = GetGame().GetFactionManager().GetFactionIndex(faction);
176 SCR_SpawnLockComponent lock = GetLock();
177 if (lock && !lock.TryLock(this, false))
178 {
179 Print("SCR_PlayerFactionAffiliationComponent::RequestFaction - Caught request on locked player!", LogLevel.DEBUG);
180 return false;
181 }
182
183 // Notify owner
184 if (IsOwner())
185 GetOnPlayerFactionRequestInvoker_O().Invoke(this, factionIndex);
186
187 if (!IsProxy())
188 GetOnPlayerFactionRequestInvoker_S().Invoke(this, factionIndex);
189
190 Rpc(Rpc_RequestFaction_S, factionIndex);
191 return true;
192 }
193
194 //------------------------------------------------------------------------------------------------
197 [RplRpc(RplChannel.Reliable, RplRcver.Server)]
198 protected void Rpc_RequestFaction_S(int factionIndex)
199 {
200 #ifdef _ENABLE_RESPAWN_LOGS
201 Print(string.Format("%1::Rpc_RequestFaction_S(factionIdx: %2)", Type().ToString(), factionIndex), LogLevel.NORMAL);
202 #endif
203
204 // Lock server
205 SCR_SpawnLockComponent lock = GetLock();
206 if (lock && !lock.TryLock(this, true))
207 {
208 Print("SCR_PlayerFactionAffiliationComponent::Rpc_RequestFaction_S - Caught request on locked player!", LogLevel.DEBUG);
209 return;
210 }
211
212 Faction originalFaction = GetAffiliatedFaction();
213
214 // Notify server
215 GetOnPlayerFactionRequestInvoker_S().Invoke(this, factionIndex);
216 Faction faction = GetGame().GetFactionManager().GetFactionByIndex(factionIndex);
217 if (CanRequestFaction_S(faction))
218 {
219 if (SetFaction_S(faction))
220 {
221 // Respond
222 SendRequestFactionResponse_S(factionIndex, true);
223
224 // Server side logging of faction switches for extra security.
225 if (faction)
226 LogFactionChange(faction, originalFaction);
227
228 return;
229 }
230 }
231
232 // Failure
233 SendRequestFactionResponse_S(factionIndex, false);
234 }
235
236 //------------------------------------------------------------------------------------------------
237 protected void LogFactionChange(notnull Faction newFaction, Faction originalFaction = null)
238 {
239 if (!m_PlayerController)
240 return;
241
242 string playerIdentity = SCR_PlayerIdentityUtils.GetPlayerLogInfo(m_PlayerController.GetPlayerId());
243
244 if (!originalFaction)
245 PrintFormat("INFO: Faction: player %1 has joined faction %2 (%3)", playerIdentity, newFaction.GetFactionName(), newFaction.GetFactionKey(), LogLevel.NORMAL);
246 else
247 PrintFormat("INFO: Faction: player %1 has switched from faction %2 (%3) to faction %4 (%5).", playerIdentity, originalFaction.GetFactionName(), originalFaction.GetFactionKey(), newFaction.GetFactionName(), newFaction.GetFactionKey(), LogLevel.NORMAL);
248 }
249
250 //------------------------------------------------------------------------------------------------
255 bool SetFaction_S(Faction faction)
256 {
257 #ifdef _ENABLE_RESPAWN_LOGS
258 Print(string.Format("%1::RequestFaction_S(Faction: %2)", Type().ToString(), faction), LogLevel.NORMAL);
259 #endif
260
261 // Assign faction
262 SetAffiliatedFaction(faction);
263 if (GetAffiliatedFaction() == faction)
264 {
265 // Update faction manager
266 SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
267 factionManager.UpdatePlayerFaction_S(this);
268
269 // Notify game mode
271 gameMode.OnPlayerFactionSet_S(this, faction);
272 return true;
273 }
274
275 return false;
276 }
277
278 //------------------------------------------------------------------------------------------------
283 protected void SendRequestFactionResponse_S(int factionIndex, bool response)
284 {
285 #ifdef _ENABLE_RESPAWN_LOGS
286 Print(string.Format("%1::SendRequestFactionResponse_S(factionIdx: %2, response: %3)", Type().ToString(), factionIndex, response), LogLevel.NORMAL);
287 #endif
288
289 // Unlock server
290 SCR_SpawnLockComponent lock = GetLock();
291 if (lock)
292 {
293 lock.Unlock(this, true);
294 // And unlock its "requester/proxy" side in case request came from self
295 lock.Unlock(this, false);
296 }
297
298 // Notify this
299 GetOnPlayerFactionResponseInvoker_S().Invoke(this, factionIndex, response);
300
301 // Notify owner
302 Rpc(RequestFactionResponse_O, factionIndex, response);
303 }
304
305 //------------------------------------------------------------------------------------------------
310 [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
311 protected void RequestFactionResponse_O(int factionIndex, bool response)
312 {
313 #ifdef _ENABLE_RESPAWN_LOGS
314 Print(string.Format("%1::RequestFactionResponse_O(factionIdx: %2, response: %3)", Type().ToString(), factionIndex, response), LogLevel.NORMAL);
315 #endif
316
317 // Unlock this
318 SCR_SpawnLockComponent lock = GetLock();
319 if (lock)
320 lock.Unlock(this, false);
321
322 // Notify this
323 GetOnPlayerFactionResponseInvoker_O().Invoke(this, factionIndex, response);
324 }
325
326 //------------------------------------------------------------------------------------------------
333 {
334 #ifdef _ENABLE_RESPAWN_LOGS
335 Print(string.Format("%1::CanRequestFaction(faction: %2)", Type().ToString(), faction), LogLevel.NORMAL);
336 #endif
337
338 // Lock this
339 SCR_SpawnLockComponent lock = GetLock();
340 if (lock && !lock.TryLock(this, false))
341 {
342 Print("SCR_PlayerFactionAffiliationComponent::CanRequestFaction - Caught request on locked player!", LogLevel.DEBUG);
343 return false;
344 }
345
346 int factionIndex = GetGame().GetFactionManager().GetFactionIndex(faction);
347
348 // Notify owner
349 if (IsOwner())
350 GetOnCanPlayerFactionRequestInvoker_O().Invoke(this, factionIndex);
351
352 // Notify au
353 if (!IsProxy())
354 GetOnCanPlayerFactionRequestInvoker_S().Invoke(this, factionIndex);
355
356 Rpc(Rpc_CanRequestFaction_S, factionIndex);
357 return true;
358 }
359
360 //------------------------------------------------------------------------------------------------
363 [RplRpc(RplChannel.Reliable, RplRcver.Server)]
364 protected void Rpc_CanRequestFaction_S(int factionIndex)
365 {
366 #ifdef _ENABLE_RESPAWN_LOGS
367 Print(string.Format("%1::Rpc_CanRequestFaction_S(factionIdx: %2)", Type().ToString(), factionIndex), LogLevel.NORMAL);
368 #endif
369
370 // Lock server
371 SCR_SpawnLockComponent lock = GetLock();
372 if (lock && !lock.TryLock(this, true))
373 {
374 Print("SCR_PlayerFactionAffiliationComponent::Rpc_RequestFaction_S - Caught request on locked player!", LogLevel.DEBUG);
375 return;
376 }
377
378 // Notify server
379 GetOnCanPlayerFactionRequestInvoker_S().Invoke(this, factionIndex);
380
381 Faction faction = GetGame().GetFactionManager().GetFactionByIndex(factionIndex);
382 if (!CanRequestFaction_S(faction))
383 {
384 SendCanRequestFactionResponse_S(factionIndex, false);
385 return;
386 }
387
388 SendCanRequestFactionResponse_S(factionIndex, true);
389 }
390
391 //------------------------------------------------------------------------------------------------
396 protected bool CanRequestFaction_S(Faction faction)
397 {
398 #ifdef _ENABLE_RESPAWN_LOGS
399 Print(string.Format("%1::CanRequestFaction_S(Faction: %2)", Type().ToString(), faction), LogLevel.NORMAL);
400 #endif
401
402 // Do not allow to set faction to faction that is already set,
403 // that will just lead to spam and invokes of irrelevant stuff
404 if (GetAffiliatedFaction() == faction)
405 return false;
406
407 // Any arbitrary logic
408 return true;
409 }
410
411 //------------------------------------------------------------------------------------------------
416 protected void SendCanRequestFactionResponse_S(int factionIndex, bool response)
417 {
418 #ifdef _ENABLE_RESPAWN_LOGS
419 Print(string.Format("%1::SendCanRequestFactionResponse_S(factionIdx: %2, response: %3)", Type().ToString(), factionIndex, response), LogLevel.NORMAL);
420 #endif
421
422 // Unlock server
423 SCR_SpawnLockComponent lock = GetLock();
424 if (lock)
425 {
426 lock.Unlock(this, true);
427 // Unlock the server's request ("proxy") side too
428 lock.Unlock(this, false);
429 }
430
431 // Notify owner
432 GetOnCanPlayerFactionResponseInvoker_S().Invoke(this, factionIndex, response);
433
434 // Notify user
435 Rpc(CanRequestFactionResponse_O, factionIndex, response);
436 }
437
438 //------------------------------------------------------------------------------------------------
443 [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
444 protected void CanRequestFactionResponse_O(int factionIndex, bool response)
445 {
446 #ifdef _ENABLE_RESPAWN_LOGS
447 Print(string.Format("%1::CanRequestFactionResponse_O(factionIdx: %2, response: %3)", Type().ToString(), factionIndex, response), LogLevel.NORMAL);
448 #endif
449
450 // Unlock this
451 SCR_SpawnLockComponent lock = GetLock();
452 if (lock)
453 lock.Unlock(this, false);
454
455 // Notify owner
456 GetOnCanPlayerFactionResponseInvoker_O().Invoke(this, factionIndex, response);
457 }
458
459 //------------------------------------------------------------------------------------------------
460 override protected void OnFactionChanged(Faction previous, Faction current)
461 {
462 super.OnFactionChanged(previous, current);
463 SCR_LogitechLEDManager.SetFactionColor(current);
464 GetOnPlayerFactionChangedInvoker().Invoke(this, previous, current);
465 }
466
467
468 #ifdef ENABLE_DIAG
469 //------------------------------------------------------------------------------------------------
471 protected void OnDiag()
472 {
473 if (!DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_PLAYER_FACTION_DIAG))
474 return;
475
476 int playerId = GetPlayerController().GetPlayerId();
477 DbgUI.Begin(string.Format("PlayerFaction (id: %1)", playerId));
478 {
479 FactionKey factionKey = "Unassigned";
480
481 Faction faction = GetAffiliatedFaction();
482 if (faction)
483 factionKey = faction.GetFactionKey();
484
485 DbgUI.Text(string.Format("Current: %1 (%2)", factionKey, faction));
486
487 FactionManager factionManager = GetGame().GetFactionManager();
488 string availableFactions = "Available Factions: ";
489 array<Faction> factions = {};
490 factionManager.GetFactionsList(factions);
491 if (factions.Count() > 0)
492 {
493 string fk = factions[0].GetFactionKey();
494 availableFactions += fk;
495
496 for (int i = 1; i < factions.Count(); i++)
497 {
498 fk = factions[i].GetFactionKey();
499 availableFactions += string.Format(", %1", fk);
500 }
501 }
502
503 if (availableFactions.IsEmpty())
504 availableFactions += "None";
505
506 DbgUI.Text(availableFactions);
507
508 string wanted;
509 DbgUI.InputText("Wanted Faction Key", wanted);
510
511 Faction wantedFaction = factionManager.GetFactionByKey(wanted);
512 if (wantedFaction)
513 {
514 DbgUI.Text(string.Format("Wanted: %1 (%2)", wantedFaction.GetFactionKey(), wantedFaction));
515 if (DbgUI.Button("CanRequest"))
516 CanRequestFaction(wantedFaction);
517 if (DbgUI.Button("Request"))
518 RequestFaction(wantedFaction);
519 }
520 }
521 DbgUI.End();
522 }
523 #endif
524}
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition DebugMenuID.c:4
ArmaReforgerScripted GetGame()
Definition game.c:1398
enum EAIGroupCombatMode ComponentEditorProps(category:"GameScripted/AI", description:"Component for utility AI system for groups")
SCR_BaseGameMode GetGameMode()
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)
ScriptInvokerBase< PlayerFactionResponseDelegate > OnPlayerFactionResponseInvoker
ScriptInvokerBase< PlayerFactionChangedDelegate > OnPlayerFactionChangedInvoker
ScriptInvokerBase< PlayerFactionRequestDelegate > OnPlayerFactionRequestInvoker
void SCR_RespawnComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
int Type
Definition DbgUI.c:66
Definition Debug.c:13
Diagnostic and developer menu system.
Definition DiagMenu.c:18
proto external Managed FindComponent(typename typeName)
void OnPlayerFactionSet_S(SCR_PlayerFactionAffiliationComponent factionComponent, Faction faction)
ref OnPlayerFactionRequestInvoker m_OnCanPlayerFactionRequestInvoker_O
OnPlayerFactionResponseInvoker GetOnPlayerFactionResponseInvoker_S()
void CanRequestFactionResponse_O(int factionIndex, bool response)
void RequestFactionResponse_O(int factionIndex, bool response)
ref OnPlayerFactionChangedInvoker m_OnPlayerFactionChangedInvoker
OnPlayerFactionResponseInvoker GetOnCanPlayerFactionResponseInvoker_O()
ref OnPlayerFactionResponseInvoker m_OnCanPlayerFactionResponseInvoker_S
OnPlayerFactionResponseInvoker GetOnPlayerFactionResponseInvoker_O()
void SendCanRequestFactionResponse_S(int factionIndex, bool response)
ref OnPlayerFactionResponseInvoker m_OnPlayerFactionResponseInvoker_S
ref OnPlayerFactionResponseInvoker m_OnCanPlayerFactionResponseInvoker_O
void SendRequestFactionResponse_S(int factionIndex, bool response)
OnPlayerFactionRequestInvoker GetOnPlayerFactionRequestInvoker_O()
OnPlayerFactionResponseInvoker GetOnCanPlayerFactionResponseInvoker_S()
ref OnPlayerFactionRequestInvoker m_OnCanPlayerFactionRequestInvoker_S
OnPlayerFactionRequestInvoker GetOnCanPlayerFactionRequestInvoker_S()
ref OnPlayerFactionRequestInvoker m_OnPlayerFactionRequestInvoker_S
ref OnPlayerFactionRequestInvoker m_OnPlayerFactionRequestInvoker_O
void LogFactionChange(notnull Faction newFaction, Faction originalFaction=null)
ref OnPlayerFactionResponseInvoker m_OnPlayerFactionResponseInvoker_O
OnPlayerFactionRequestInvoker GetOnCanPlayerFactionRequestInvoker_O()
void OnFactionChanged(Faction previous, Faction current)
proto external void SetAffiliatedFaction(Faction faction)
SCR_CampaignFaction GetAffiliatedFaction()
Returns the affiliated faction or null if none.
void OnDiag(IEntity owner, float timeslice)
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
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)
proto external PlayerController GetPlayerController()
proto external int GetPlayerId()
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
proto external string ToString()
Plain C++ pointer, no weak pointers, no memory management.