Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_PossessingManagerComponent.c
Go to the documentation of this file.
1 void ScriptInvokerOnPossessedProxyMethod(int playerID, bool isPossessing, RplId mainEntityID);
3 typedef ScriptInvokerBase<ScriptInvokerOnPossessedProxyMethod> ScriptInvokerOnPossessedProxy;
4 
5 [ComponentEditorProps(category: "GameScripted/GameMode/Components", description: "")]
7 {
8 }
9 
10 class SCR_PossessingManagerComponent : SCR_BaseGameModeComponent
11 {
12  protected ref map<int, RplId> m_MainEntities = new map<int, RplId>();
13  protected ref ScriptInvoker Event_OnPossessed;
15 
17  //--- Public methods
19 
20  //------------------------------------------------------------------------------------------------
22  static SCR_PossessingManagerComponent GetInstance()
23  {
24  BaseGameMode gameMode = GetGame().GetGameMode();
25  if (gameMode)
26  return SCR_PossessingManagerComponent.Cast(gameMode.FindComponent(SCR_PossessingManagerComponent));
27  else
28  return null;
29  }
30 
31  //------------------------------------------------------------------------------------------------
33  ScriptInvoker GetOnPossessed()
34  {
35  if (!Event_OnPossessed)
36  Event_OnPossessed = new ScriptInvoker();
37 
38  return Event_OnPossessed;
39  }
40 
41  //------------------------------------------------------------------------------------------------
44  {
47 
49  }
50 
51  //------------------------------------------------------------------------------------------------
57  IEntity GetMainEntity(int iPlayerId)
58  {
59  RplId rplId;
60  if (m_MainEntities.Find(iPlayerId, rplId))
61  {
62  RplComponent rplComponent = RplComponent.Cast(Replication.FindItem(rplId));
63  if (rplComponent)
64  return rplComponent.GetEntity();
65  else
66  return null;
67  }
68  else
69  {
70  return GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerId);
71  }
72  }
73 
74  //------------------------------------------------------------------------------------------------
80  RplId GetMainRplId(int iPlayerId)
81  {
82  RplId rplId;
83  if (m_MainEntities.Find(iPlayerId, rplId))
84  return rplId;
85  else
86  return GetRplId(GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerId));
87  }
88 
89  //------------------------------------------------------------------------------------------------
93  IEntity GetPossessedEntity(int iPlayerId)
94  {
95  if (IsPossessing(iPlayerId))
96  return GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerId);
97  else
98  return null;
99  }
100 
101  //------------------------------------------------------------------------------------------------
105  RplId GetPossessedRplId(int iPlayerId)
106  {
107  if (IsPossessing(iPlayerId))
108  return GetRplId(GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerId));
109  else
110  return RplId.Invalid();
111  }
112 
113  //------------------------------------------------------------------------------------------------
117  int GetIdFromMainEntity(IEntity entity)
118  {
119  array<int> players = {};
120  for (int i = 0, count = GetGame().GetPlayerManager().GetPlayers(players); i < count; i++)
121  {
122  if (GetMainEntity(players[i]) == entity)
123  return players[i];
124  }
125  return 0;
126  }
127 
128  //------------------------------------------------------------------------------------------------
132  int GetIdFromMainRplId(RplId rplId)
133  {
134  array<int> players = {};
135  for (int i = 0, count = GetGame().GetPlayerManager().GetPlayers(players); i < count; i++)
136  {
137  if (GetMainRplId(players[i]) == rplId)
138  return players[i];
139  }
140  return 0;
141  }
142 
143  //------------------------------------------------------------------------------------------------
147  int GetIdFromControlledEntity(IEntity entity)
148  {
149  int iPlayerId = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(entity);
150  if (iPlayerId > 0)
151  return iPlayerId;
152  else
153  return GetIdFromMainEntity(entity);
154  }
155 
156  //------------------------------------------------------------------------------------------------
160  int GetIdFromControlledRplId(RplId rplId)
161  {
162  int iPlayerId = GetGame().GetPlayerManager().GetPlayerIdFromEntityRplId(rplId);
163  if (iPlayerId > 0)
164  return iPlayerId;
165  else
166  return GetIdFromMainRplId(rplId);
167  }
168 
169  //------------------------------------------------------------------------------------------------
172  bool IsPossessing(int iPlayerId)
173  {
174  return m_MainEntities.Contains(iPlayerId);
175  }
176 
177  //------------------------------------------------------------------------------------------------
182  void SetMainEntity(int playerID, IEntity controlledEntity, IEntity mainEntity, bool isPossessing)
183  {
184  RplId mainEntityID = RplId.Invalid();
185  if (mainEntity)
186  {
187  RplComponent rpl = RplComponent.Cast(mainEntity.FindComponent(RplComponent));
188  if (rpl)
189  mainEntityID = rpl.Id();
190  }
191 
192  SetMainEntityBroadcast(playerID, isPossessing, mainEntityID);
193  Rpc(SetMainEntityBroadcast, playerID, isPossessing, mainEntityID);
194 
195  if (Event_OnPossessed)
196  Event_OnPossessed.Invoke(playerID, controlledEntity, mainEntity, isPossessing);
197  }
198 
200  //--- Protected methods
202 
203  //------------------------------------------------------------------------------------------------
204  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
205  protected void SetMainEntityBroadcast(int playerID, bool isPossessing, RplId mainEntityID)
206  {
207  if (isPossessing)
208  m_MainEntities.Set(playerID, mainEntityID);
209  else
210  m_MainEntities.Remove(playerID);
211 
213  Event_OnPossessedProxy.Invoke(playerID, isPossessing, mainEntityID);
214  }
215 
217  //--- Static methods
219 
220  //------------------------------------------------------------------------------------------------
226  static IEntity GetPlayerMainEntity(int iPlayerId)
227  {
228  SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
229  if (possessingManager)
230  return possessingManager.GetMainEntity(iPlayerId);
231  else
232  return GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerId);
233  }
234 
235  //------------------------------------------------------------------------------------------------
241  static RplId GetPlayerMainRplId(int iPlayerId)
242  {
243  SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
244  if (possessingManager)
245  return possessingManager.GetMainRplId(iPlayerId);
246  else
247  return possessingManager.GetRplId(GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerId));
248  }
249 
250  //------------------------------------------------------------------------------------------------
254  static int GetPlayerIdFromMainEntity(IEntity entity)
255  {
256  SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
257  if (possessingManager)
258  return possessingManager.GetIdFromMainEntity(entity);
259  else
260  return GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(entity);
261  }
262 
263  //------------------------------------------------------------------------------------------------
267  static int GetPlayerIdFromMainRplId(RplId rplId)
268  {
269  SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
270  if (possessingManager)
271  return possessingManager.GetIdFromMainRplId(rplId);
272  else
273  return GetGame().GetPlayerManager().GetPlayerIdFromEntityRplId(rplId);
274  }
275 
276  //------------------------------------------------------------------------------------------------
280  static int GetPlayerIdFromControlledEntity(IEntity entity)
281  {
282  SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
283  if (possessingManager)
284  return possessingManager.GetIdFromControlledEntity(entity);
285  else
286  return GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(entity);
287  }
288 
289  //------------------------------------------------------------------------------------------------
293  static int GetPlayerIdFromControlledEntity(RplId rplId)
294  {
295  SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
296  if (possessingManager)
297  return possessingManager.GetIdFromControlledRplId(rplId);
298  else
299  return GetGame().GetPlayerManager().GetPlayerIdFromEntityRplId(rplId);
300  }
301 
303  //--- Overrides
305 
306  //------------------------------------------------------------------------------------------------
307  protected override void OnControllableDeleted(IEntity entity)
308  {
309 // super.OnControllableDeleted(entity);
310 // //--- Switch to main entity when the possessed one is deleted (ToDo: Better deletion detection than ChimeraCharacter event?)
311 // int playerID = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(entity);
312 // if (playerID > 0)
313 // {
314 // if (IsPossessing(playerID))
315 // {
316 // SCR_PlayerController playerController = SCR_PlayerController.Cast(GetGame().GetPlayerManager().GetPlayerController(playerID));
317 // if (playerController)
318 // playerController.SetPossessedEntity(null);
319 // }
320 // }
321  }
322 
323  //------------------------------------------------------------------------------------------------
324  protected RplId GetRplId(IEntity entity)
325  {
326  if (entity)
327  {
328  RplComponent rpl = RplComponent.Cast(entity.FindComponent(RplComponent));
329  if (rpl)
330  return rpl.Id();
331  }
332 
333  return RplId.Invalid();
334  }
335 
336  //------------------------------------------------------------------------------------------------
337  override void OnControllableDestroyed(IEntity entity, IEntity killerEntity, notnull Instigator killer)
338  {
339  int pid = GetPlayerIdFromMainEntity(entity);
340  if (pid > 0)
341  {
342  SCR_RespawnComponent rc = SCR_RespawnComponent.Cast(GetGame().GetPlayerManager().GetPlayerRespawnComponent(pid));
343  if (rc)
344  rc.NotifyReadyForSpawn_S();
345  }
346  }
347 
348  //------------------------------------------------------------------------------------------------
349  override bool HandlePlayerKilled(int playerId, IEntity playerEntity, IEntity killerEntity, notnull Instigator killer)
350  {
351  if (playerId > 0)
352  {
353  //--- Controlled entity
354  if (playerEntity != GetMainEntity(playerId))
355  {
356  //--- Switch to main entity when the possessed one dies
357  SCR_PlayerController playerController = SCR_PlayerController.Cast(GetGame().GetPlayerManager().GetPlayerController(playerId));
358  if (playerController)
359  {
360  //--- Open editor, assume it will stop possessing (ToDo: No direct editor reference here)
362  if (core)
363  {
364  SCR_EditorManagerEntity editorManager = core.GetEditorManager(playerId);
365  if (editorManager)
366  {
367  editorManager.Open();
368  // Don't handle kill automatically
369  return false;
370  }
371  }
372 
373  //--- No editor, stop possessing right now
374  playerController.SetPossessedEntity(null);
375  // Don't handle this kill automatically
376  return false;
377  }
378  }
379  }
380 
381  // Main entity, handle kill as usual
382  return super.HandlePlayerKilled(playerId, playerEntity, killerEntity, killer);
383  }
384 
385  //------------------------------------------------------------------------------------------------
386  override void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)
387  {
388  m_MainEntities.Remove(playerId);
389  }
390 
391  //------------------------------------------------------------------------------------------------
392  override void OnPostInit(IEntity owner)
393  {
394  super.OnPostInit(owner);
395  }
396 
397  //------------------------------------------------------------------------------------------------
398  override void OnDelete(IEntity owner)
399  {
400  super.OnDelete(owner);
401  }
402 
403  //------------------------------------------------------------------------------------------------
404  override bool RplSave(ScriptBitWriter writer)
405  {
406  int mainEntityCount = m_MainEntities.Count();
407  writer.WriteInt(mainEntityCount);
408 
409  for (int i = 0; i < mainEntityCount; i++)
410  {
411  writer.WriteInt(m_MainEntities.GetKey(i));
412  writer.WriteRplId(m_MainEntities.GetElement(i));
413  }
414 
415  return true;
416  }
417 
418  //------------------------------------------------------------------------------------------------
419  override bool RplLoad(ScriptBitReader reader)
420  {
421  int mainEntityCount;
422  reader.ReadInt(mainEntityCount);
423 
424  int playerID;
425  RplId rplID;
426  for (int i = 0; i < mainEntityCount; i++)
427  {
428  reader.ReadInt(playerID);
429  reader.ReadRplId(rplID);
430 
431  m_MainEntities.Insert(playerID, rplID);
432  }
433 
434  return true;
435  }
436 }
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
SCR_RespawnComponent
void SCR_RespawnComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_RespawnComponent.c:576
GetIdFromMainRplId
int GetIdFromMainRplId(RplId rplId)
Definition: SCR_PossessingManagerComponent.c:132
SCR_PlayerController
Definition: SCR_PlayerController.c:31
IsPossessing
bool IsPossessing(int iPlayerId)
Definition: SCR_PossessingManagerComponent.c:172
m_MainEntities
SCR_PossessingManagerComponentClass m_MainEntities
OnControllableDeleted
protected override void OnControllableDeleted(IEntity entity)
Definition: SCR_PossessingManagerComponent.c:307
GetPlayers
protected int GetPlayers(out notnull array< int > outPlayers)
Definition: SCR_DataCollectorComponent.c:233
GetInstance
SCR_TextsTaskManagerComponentClass ScriptComponentClass GetInstance()
Definition: SCR_TextsTaskManagerComponent.c:50
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
ScriptInvokerOnPossessedProxy
ScriptInvokerBase< ScriptInvokerOnPossessedProxyMethod > ScriptInvokerOnPossessedProxy
Definition: SCR_PossessingManagerComponent.c:3
OnDelete
override void OnDelete(IEntity owner)
Definition: SCR_PossessingManagerComponent.c:398
GetPossessedEntity
IEntity GetPossessedEntity(int iPlayerId)
Definition: SCR_PossessingManagerComponent.c:93
RplLoad
override bool RplLoad(ScriptBitReader reader)
Definition: SCR_PossessingManagerComponent.c:419
GetIdFromControlledEntity
int GetIdFromControlledEntity(IEntity entity)
Definition: SCR_PossessingManagerComponent.c:147
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
GetMainRplId
RplId GetMainRplId(int iPlayerId)
Definition: SCR_PossessingManagerComponent.c:80
SetMainEntityBroadcast
protected void SetMainEntityBroadcast(int playerID, bool isPossessing, RplId mainEntityID)
Definition: SCR_PossessingManagerComponent.c:205
func
func
Definition: SCR_AIThreatSystem.c:5
GetOnPossessed
ScriptInvoker GetOnPossessed()
Definition: SCR_PossessingManagerComponent.c:33
KickCauseCode
KickCauseCode
Definition: KickCauseCode.c:19
GetPlayerController
proto external PlayerController GetPlayerController()
Definition: SCR_PlayerDeployMenuHandlerComponent.c:307
Instigator
Definition: Instigator.c:6
GetIdFromMainEntity
int GetIdFromMainEntity(IEntity entity)
Definition: SCR_PossessingManagerComponent.c:117
GetRplId
protected RplId GetRplId(IEntity entity)
Definition: SCR_PossessingManagerComponent.c:324
GetIdFromControlledRplId
int GetIdFromControlledRplId(RplId rplId)
Definition: SCR_PossessingManagerComponent.c:160
SCR_EditorManagerCore
Core component to manage SCR_EditorManagerEntity.
Definition: SCR_EditorManagerCore.c:5
GetOnPossessedProxy
ScriptInvokerOnPossessedProxy GetOnPossessedProxy()
Definition: SCR_PossessingManagerComponent.c:43
OnPostInit
override void OnPostInit(IEntity owner)
Editable Mine.
Definition: SCR_PossessingManagerComponent.c:392
GetPossessedRplId
RplId GetPossessedRplId(int iPlayerId)
Definition: SCR_PossessingManagerComponent.c:105
Event_OnPossessedProxy
protected ref ScriptInvokerOnPossessedProxy Event_OnPossessedProxy
Definition: SCR_PossessingManagerComponent.c:14
Event_OnPossessed
protected ref ScriptInvoker Event_OnPossessed
Definition: SCR_PossessingManagerComponent.c:13
OnControllableDestroyed
override void OnControllableDestroyed(IEntity entity, IEntity killerEntity, notnull Instigator killer)
Definition: SCR_PossessingManagerComponent.c:337
GetMainEntity
IEntity GetMainEntity(int iPlayerId)
Definition: SCR_PossessingManagerComponent.c:57
ScriptInvokerOnPossessedProxyMethod
func ScriptInvokerOnPossessedProxyMethod
Definition: SCR_PossessingManagerComponent.c:2
RplSave
override bool RplSave(ScriptBitWriter writer)
Definition: SCR_PossessingManagerComponent.c:404
HandlePlayerKilled
override bool HandlePlayerKilled(int playerId, IEntity playerEntity, IEntity killerEntity, notnull Instigator killer)
Definition: SCR_PossessingManagerComponent.c:349
SetMainEntity
void SetMainEntity(int playerID, IEntity controlledEntity, IEntity mainEntity, bool isPossessing)
Definition: SCR_PossessingManagerComponent.c:182
OnPlayerDisconnected
override void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)
Definition: SCR_PossessingManagerComponent.c:386
SCR_BaseGameModeComponentClass
Definition: SCR_BaseGameModeComponent.c:2
SCR_PossessingManagerComponentClass
Definition: SCR_PossessingManagerComponent.c:6
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
SCR_BaseGameModeComponent
void SCR_BaseGameModeComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_BaseGameModeComponent.c:199
SCR_EditorManagerEntity
Definition: SCR_EditorManagerEntity.c:26