Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_PlayerFactionAffiliationComponent.c
Go to the documentation of this file.
1 [ComponentEditorProps(category: "GameScripted/Respawn/PlayerController")]
3 {
4 }
5 
8 typedef ScriptInvokerBase<PlayerFactionRequestDelegate> OnPlayerFactionRequestInvoker;
9 
10 void PlayerFactionResponseDelegate(SCR_PlayerFactionAffiliationComponent component, int factionIndex, bool response);
12 typedef ScriptInvokerBase<PlayerFactionResponseDelegate> OnPlayerFactionResponseInvoker;
13 
17 {
18  private PlayerController m_PlayerController;
19  private RplComponent m_RplComponent;
20  private SCR_RespawnComponent m_RespawnComponent;
21  private SCR_SpawnLockComponent m_Lock;
22 
23  //------------------------------------------------------------------------------------------------
25  PlayerController GetPlayerController()
26  {
27  return m_PlayerController;
28  }
29 
30  //------------------------------------------------------------------------------------------------
32  int GetPlayerId()
33  {
34  return GetPlayerController().GetPlayerId();
35  }
36 
37  //------------------------------------------------------------------------------------------------
39  protected SCR_SpawnLockComponent GetLock()
40  {
41  return m_Lock;
42  }
43 
44  // ON CAN FACTION REQUEST
45  protected ref OnPlayerFactionRequestInvoker m_OnCanPlayerFactionRequestInvoker_O = new OnPlayerFactionRequestInvoker();
46 
47  //------------------------------------------------------------------------------------------------
49  OnPlayerFactionRequestInvoker GetOnCanPlayerFactionRequestInvoker_O()
50  {
51  return m_OnCanPlayerFactionRequestInvoker_O;
52  }
53 
54  protected ref OnPlayerFactionRequestInvoker m_OnCanPlayerFactionRequestInvoker_S = new OnPlayerFactionRequestInvoker();
55 
56  //------------------------------------------------------------------------------------------------
58  OnPlayerFactionRequestInvoker GetOnCanPlayerFactionRequestInvoker_S()
59  {
60  return m_OnCanPlayerFactionRequestInvoker_S;
61  }
62 
63  // ON CAN FACTION RESPONSE
64  protected ref OnPlayerFactionResponseInvoker m_OnCanPlayerFactionResponseInvoker_O = new OnPlayerFactionResponseInvoker();
65 
66  //------------------------------------------------------------------------------------------------
68  OnPlayerFactionResponseInvoker GetOnCanPlayerFactionResponseInvoker_O()
69  {
70  return m_OnCanPlayerFactionResponseInvoker_O;
71  }
72 
73  protected ref OnPlayerFactionResponseInvoker m_OnCanPlayerFactionResponseInvoker_S = new OnPlayerFactionResponseInvoker();
74 
75  //------------------------------------------------------------------------------------------------
77  OnPlayerFactionResponseInvoker GetOnCanPlayerFactionResponseInvoker_S()
78  {
79  return m_OnCanPlayerFactionResponseInvoker_S;
80  }
81 
82  // ON FACTION REQUEST
83  protected ref OnPlayerFactionRequestInvoker m_OnPlayerFactionRequestInvoker_O = new OnPlayerFactionRequestInvoker();
84 
85  //------------------------------------------------------------------------------------------------
87  OnPlayerFactionRequestInvoker GetOnPlayerFactionRequestInvoker_O()
88  {
89  return m_OnPlayerFactionRequestInvoker_O;
90  }
91 
92  protected ref OnPlayerFactionRequestInvoker m_OnPlayerFactionRequestInvoker_S = new OnPlayerFactionRequestInvoker();
93 
94  //------------------------------------------------------------------------------------------------
96  OnPlayerFactionRequestInvoker GetOnPlayerFactionRequestInvoker_S()
97  {
98  return m_OnPlayerFactionRequestInvoker_S;
99  }
100 
101  //------------------------------------------------------------------------------------------------
102  // ON FACTION RESPONSE
103  protected ref OnPlayerFactionResponseInvoker m_OnPlayerFactionResponseInvoker_O = new OnPlayerFactionResponseInvoker();
104 
105  //------------------------------------------------------------------------------------------------
107  OnPlayerFactionResponseInvoker GetOnPlayerFactionResponseInvoker_O()
108  {
109  return m_OnPlayerFactionResponseInvoker_O;
110  }
111  protected ref OnPlayerFactionResponseInvoker m_OnPlayerFactionResponseInvoker_S = new OnPlayerFactionResponseInvoker();
112  //------------------------------------------------------------------------------------------------
114  OnPlayerFactionResponseInvoker GetOnPlayerFactionResponseInvoker_S()
115  {
116  return m_OnPlayerFactionResponseInvoker_S;
117  }
118 
119  //------------------------------------------------------------------------------------------------
120  protected bool IsOwner()
121  {
122  return !m_RplComponent || m_RplComponent.IsOwner();
123  }
124 
125  //------------------------------------------------------------------------------------------------
126  protected bool IsProxy()
127  {
128  return m_RplComponent && m_RplComponent.IsProxy();
129  }
130 
131  //------------------------------------------------------------------------------------------------
132  protected override void OnPostInit(IEntity owner)
133  {
134  super.OnPostInit(owner);
135  m_PlayerController = PlayerController.Cast(owner);
136  if (!m_PlayerController)
137  Debug.Error(string.Format("%1 is not attached to a %2", Type().ToString(), PlayerController));
138 
139  m_RespawnComponent = SCR_RespawnComponent.Cast(m_PlayerController.GetRespawnComponent());
140  m_Lock = SCR_SpawnLockComponent.Cast(owner.FindComponent(SCR_SpawnLockComponent));
141  m_RplComponent = RplComponent.Cast(owner.FindComponent(RplComponent));
142 
143  #ifdef ENABLE_DIAG
144  DiagMenu.RegisterBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_PLAYER_FACTION_DIAG, "", "Player Factions", "GameMode");
145  GetGame().GetCallqueue().CallLater(OnDiag, 0, true);
146  #endif
147  }
148 
149  //------------------------------------------------------------------------------------------------
155  bool RequestFaction(Faction faction)
156  {
157  #ifdef _ENABLE_RESPAWN_LOGS
158  Print(string.Format("%1::RequestFaction(faction: %2)", Type().ToString(), faction), LogLevel.NORMAL);
159  #endif
160 
161  // Lock this
162  int factionIndex = GetGame().GetFactionManager().GetFactionIndex(faction);
163  SCR_SpawnLockComponent lock = GetLock();
164  if (lock && !lock.TryLock(this, false))
165  {
166  Debug.Error("Caught request on locked player!");
167  return false;
168  }
169 
170  // Notify owner
171  if (IsOwner())
172  GetOnPlayerFactionRequestInvoker_O().Invoke(this, factionIndex);
173 
174  if (!IsProxy())
175  GetOnPlayerFactionRequestInvoker_S().Invoke(this, factionIndex);
176 
177  Rpc(Rpc_RequestFaction_S, factionIndex);
178  return true;
179  }
180 
181  //------------------------------------------------------------------------------------------------
184  [RplRpc(RplChannel.Reliable, RplRcver.Server)]
185  protected void Rpc_RequestFaction_S(int factionIndex)
186  {
187  #ifdef _ENABLE_RESPAWN_LOGS
188  Print(string.Format("%1::Rpc_RequestFaction_S(factionIdx: %2)", Type().ToString(), factionIndex), LogLevel.NORMAL);
189  #endif
190 
191  // Lock server
192  SCR_SpawnLockComponent lock = GetLock();
193  if (lock && !lock.TryLock(this, true))
194  {
195  Debug.Error("Caught request on locked player!");
196  return;
197  }
198 
199  // Notify server
200  GetOnPlayerFactionRequestInvoker_S().Invoke(this, factionIndex);
201 
202  Faction faction = GetGame().GetFactionManager().GetFactionByIndex(factionIndex);
203  if (CanRequestFaction_S(faction))
204  {
205  if (SetFaction_S(faction))
206  {
207  // Respond
208  SendRequestFactionResponse_S(factionIndex, true);
209  return;
210  }
211  }
212 
213  // Failure
214  SendRequestFactionResponse_S(factionIndex, false);
215  }
216 
217  //------------------------------------------------------------------------------------------------
222  protected bool SetFaction_S(Faction faction)
223  {
224  #ifdef _ENABLE_RESPAWN_LOGS
225  Print(string.Format("%1::RequestFaction_S(Faction: %2)", Type().ToString(), faction), LogLevel.NORMAL);
226  #endif
227 
228  // Assign faction
229  SetAffiliatedFaction(faction);
230  if (GetAffiliatedFaction() == faction)
231  {
232  // Update faction manager
233  SCR_FactionManager factionManager = SCR_FactionManager.Cast(GetGame().GetFactionManager());
234  factionManager.UpdatePlayerFaction_S(this);
235 
236  // Notify game mode
238  gameMode.OnPlayerFactionSet_S(this, faction);
239  return true;
240  }
241 
242  return false;
243  }
244 
245  //------------------------------------------------------------------------------------------------
250  protected void SendRequestFactionResponse_S(int factionIndex, bool response)
251  {
252  #ifdef _ENABLE_RESPAWN_LOGS
253  Print(string.Format("%1::SendRequestFactionResponse_S(factionIdx: %2, response: %3)", Type().ToString(), factionIndex, response), LogLevel.NORMAL);
254  #endif
255 
256  // Unlock server
257  SCR_SpawnLockComponent lock = GetLock();
258  if (lock)
259  {
260  lock.Unlock(this, true);
261  // And unlock its "requester/proxy" side in case request came from self
262  lock.Unlock(this, false);
263  }
264 
265  // Notify this
266  GetOnPlayerFactionResponseInvoker_S().Invoke(this, factionIndex, response);
267 
268  // Notify owner
269  Rpc(RequestFactionResponse_O, factionIndex, response);
270  }
271 
272  //------------------------------------------------------------------------------------------------
277  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
278  protected void RequestFactionResponse_O(int factionIndex, bool response)
279  {
280  #ifdef _ENABLE_RESPAWN_LOGS
281  Print(string.Format("%1::RequestFactionResponse_O(factionIdx: %2, response: %3)", Type().ToString(), factionIndex, response), LogLevel.NORMAL);
282  #endif
283 
284  // Unlock this
285  SCR_SpawnLockComponent lock = GetLock();
286  if (lock)
287  lock.Unlock(this, false);
288 
289  // Notify this
290  GetOnPlayerFactionResponseInvoker_O().Invoke(this, factionIndex, response);
291  }
292 
293  //------------------------------------------------------------------------------------------------
299  bool CanRequestFaction(Faction faction)
300  {
301  #ifdef _ENABLE_RESPAWN_LOGS
302  Print(string.Format(Format("%1::CanRequestFaction(faction: %2)", Type().ToString(), faction), LogLevel.NORMAL);
303  #endif
304 
305  // Lock this
306  SCR_SpawnLockComponent lock = GetLock();
307  if (lock && !lock.TryLock(this, false))
308  {
309  Debug.Error("Caught request on locked player!");
310  return false;
311  }
312 
313  int factionIndex = GetGame().GetFactionManager().GetFactionIndex(faction);
314 
315  // Notify owner
316  if (IsOwner())
317  GetOnCanPlayerFactionRequestInvoker_O().Invoke(this, factionIndex);
318 
319  // Notify au
320  if (!IsProxy())
321  GetOnCanPlayerFactionRequestInvoker_S().Invoke(this, factionIndex);
322 
323  Rpc(Rpc_CanRequestFaction_S, factionIndex);
324  return true;
325  }
326 
327  //------------------------------------------------------------------------------------------------
330  [RplRpc(RplChannel.Reliable, RplRcver.Server)]
331  protected void Rpc_CanRequestFaction_S(int factionIndex)
332  {
333  #ifdef _ENABLE_RESPAWN_LOGS
334  Print(string.Format("%1::Rpc_CanRequestFaction_S(factionIdx: %2)", Type().ToString(), factionIndex), LogLevel.NORMAL);
335  #endif
336 
337  // Lock server
338  SCR_SpawnLockComponent lock = GetLock();
339  if (lock && !lock.TryLock(this, true))
340  {
341  Debug.Error("Caught request on locked player!");
342  return;
343  }
344 
345  // Notify server
346  GetOnCanPlayerFactionRequestInvoker_S().Invoke(this, factionIndex);
347 
348  Faction faction = GetGame().GetFactionManager().GetFactionByIndex(factionIndex);
349  if (!CanRequestFaction_S(faction))
350  {
351  SendCanRequestFactionResponse_S(factionIndex, false);
352  return;
353  }
354 
355  SendCanRequestFactionResponse_S(factionIndex, true);
356  }
357 
358  //------------------------------------------------------------------------------------------------
363  protected bool CanRequestFaction_S(Faction faction)
364  {
365  #ifdef _ENABLE_RESPAWN_LOGS
366  Print(string.Format("%1::CanRequestFaction_S(Faction: %2)", Type().ToString(), faction), LogLevel.NORMAL);
367  #endif
368 
369  // Do not allow to set faction to faction that is already set,
370  // that will just lead to spam and invokes of irrelevant stuff
371  if (GetAffiliatedFaction() == faction)
372  return false;
373 
374  // Any arbitrary logic
375  return true;
376  }
377 
378  //------------------------------------------------------------------------------------------------
383  protected void SendCanRequestFactionResponse_S(int factionIndex, bool response)
384  {
385  #ifdef _ENABLE_RESPAWN_LOGS
386  Print(string.Format("%1::SendCanRequestFactionResponse_S(factionIdx: %2, response: %3)", Type().ToString(), factionIndex, response), LogLevel.NORMAL);
387  #endif
388 
389  // Unlock server
390  SCR_SpawnLockComponent lock = GetLock();
391  if (lock)
392  {
393  lock.Unlock(this, true);
394  // Unlock the server's request ("proxy") side too
395  lock.Unlock(this, false);
396  }
397 
398  // Notify owner
399  GetOnCanPlayerFactionResponseInvoker_S().Invoke(this, factionIndex, response);
400 
401  // Notify user
402  Rpc(CanRequestFactionResponse_O, factionIndex, response);
403  }
404 
405  //------------------------------------------------------------------------------------------------
410  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
411  protected void CanRequestFactionResponse_O(int factionIndex, bool response)
412  {
413  #ifdef _ENABLE_RESPAWN_LOGS
414  Print(string.Format("%1::CanRequestFactionResponse_O(factionIdx: %2, response: %3)", Type().ToString(), factionIndex, response), LogLevel.NORMAL);
415  #endif
416 
417  // Unlock this
418  SCR_SpawnLockComponent lock = GetLock();
419  if (lock)
420  lock.Unlock(this, false);
421 
422  // Notify owner
423  GetOnCanPlayerFactionResponseInvoker_O().Invoke(this, factionIndex, response);
424  }
425 
426  #ifdef ENABLE_DIAG
427  //------------------------------------------------------------------------------------------------
429  protected void OnDiag()
430  {
431  if (!DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_PLAYER_FACTION_DIAG))
432  return;
433 
434  int playerId = GetPlayerController().GetPlayerId();
435  DbgUI.Begin(string.Format("PlayerFaction (id: %1)", playerId));
436  {
437  FactionKey factionKey = "Unassigned";
438 
439  Faction faction = GetAffiliatedFaction();
440  if (faction)
441  factionKey = faction.GetFactionKey();
442 
443  DbgUI.Text(string.Format("Current: %1 (%2)", factionKey, faction));
444 
445  FactionManager factionManager = GetGame().GetFactionManager();
446  string availableFactions = "Available Factions: ";
447  array<Faction> factions = {};
448  factionManager.GetFactionsList(factions);
449  if (factions.Count() > 0)
450  {
451  string fk = factions[0].GetFactionKey();
452  availableFactions += fk;
453 
454  for (int i = 1; i < factions.Count(); i++)
455  {
456  fk = factions[i].GetFactionKey();
457  availableFactions += string.Format(", %1", fk);
458  }
459  }
460 
461  if (availableFactions.IsEmpty())
462  availableFactions += "None";
463 
464  DbgUI.Text(availableFactions);
465 
466  string wanted;
467  DbgUI.InputText("Wanted Faction Key", wanted);
468 
469  Faction wantedFaction = factionManager.GetFactionByKey(wanted);
470  if (wantedFaction)
471  {
472  DbgUI.Text(string.Format("Wanted: %1 (%2)", wantedFaction.GetFactionKey(), wantedFaction));
473  if (DbgUI.Button("CanRequest"))
474  CanRequestFaction(wantedFaction);
475  if (DbgUI.Button("Request"))
476  RequestFaction(wantedFaction);
477  }
478  }
479  DbgUI.End();
480  }
481  #endif
482 }
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
SCR_BaseGameMode
Definition: SCR_BaseGameMode.c:137
SCR_RespawnComponent
void SCR_RespawnComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_RespawnComponent.c:576
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
GetFactionIndex
int GetFactionIndex()
Definition: SCR_EditableFactionComponent.c:57
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
func
func
Definition: SCR_AIThreatSystem.c:5
PlayerFactionRequestDelegate
func PlayerFactionRequestDelegate
Definition: SCR_PlayerFactionAffiliationComponent.c:7
SetAffiliatedFaction
proto external void SetAffiliatedFaction(Faction faction)
GetGameMode
SCR_BaseGameMode GetGameMode()
Definition: SCR_BaseGameModeComponent.c:15
GetAffiliatedFaction
proto external Faction GetAffiliatedFaction()
Returns the affiliated faction or null if none.
PlayerFactionResponseDelegate
func PlayerFactionResponseDelegate
Definition: SCR_PlayerFactionAffiliationComponent.c:11
m_RespawnComponent
SCR_PlayerDeployMenuHandlerComponentClass m_RespawnComponent
Component responsible for deploy menu management.
OnPlayerFactionRequestInvoker
ScriptInvokerBase< PlayerFactionRequestDelegate > OnPlayerFactionRequestInvoker
Definition: SCR_PlayerFactionAffiliationComponent.c:8
OnDiag
event protected void OnDiag(IEntity owner, float timeslice)
Faction
Definition: Faction.c:12
m_RplComponent
protected RplComponent m_RplComponent
Definition: SCR_CampaignBuildingManagerComponent.c:42
SCR_PlayerFactionAffiliationComponent
Definition: SCR_PlayerFactionAffiliationComponent.c:16
m_PlayerController
SCR_CampaignNetworkComponentClass m_PlayerController
Takes care of Campaign-specific server <> client communication and requests.
SCR_PlayerFactionAffiliationComponentClass
Definition: SCR_PlayerFactionAffiliationComponent.c:2
OnPlayerFactionResponseInvoker
ScriptInvokerBase< PlayerFactionResponseDelegate > OnPlayerFactionResponseInvoker
Definition: SCR_PlayerFactionAffiliationComponent.c:12
SCR_FactionAffiliationComponent
Definition: SCR_FactionAffiliationComponent.c:10
SCR_FactionManager
void SCR_FactionManager(IEntitySource src, IEntity parent)
Definition: SCR_FactionManager.c:461
SCR_DebugMenuID
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition: DebugMenuID.c:3
SCR_FactionAffiliationComponentClass
Definition: SCR_FactionAffiliationComponent.c:1
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180