Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_PlayersManagerEditorComponent.c
Go to the documentation of this file.
1 [ComponentEditorProps(category: "GameScripted/Editor", description: "", icon: "WBData/ComponentEditorProps/componentEditor.png")]
3 {
4 };
5 
10 {
11  [Attribute(uiwidget: UIWidgets.ResourcePickerThumbnail, params: "et")]
12  protected ResourceName m_DefaultPlayer;
13 
14  protected ref map<int, SCR_EditableEntityComponent> m_MainEntities;
15  protected ref map<int, SCR_EditableEntityComponent> m_PossessedEntities;
16 
17  protected bool m_LocalPlayerHasSpawnedOnce = false;
18 
19  protected ref ScriptInvoker Event_OnDeath = new ScriptInvoker;
20  protected ref ScriptInvoker Event_OnSpawn = new ScriptInvoker;
21  protected ref ScriptInvoker Event_OnPossessed = new ScriptInvoker;
22  protected ref ScriptInvoker Event_OnConnected = new ScriptInvoker;
23  protected ref ScriptInvoker Event_OnDisconnected = new ScriptInvoker;
24 
30  SCR_EditableEntityComponent GetPlayerEntity(int playerID)
31  {
32  if (!m_MainEntities) return null;
33  SCR_EditableEntityComponent entity = null;
34  m_MainEntities.Find(playerID, entity);
35  return entity;
36  }
42  int GetPlayerID(SCR_EditableEntityComponent entity)
43  {
44  if (!m_MainEntities)
45  return 0;
46 
47  int playerID = m_MainEntities.GetKeyByValue(entity);
48  if (playerID != 0)
49  return playerID;
50  else
51  return m_PossessedEntities.GetKeyByValue(entity);
52  }
53  bool IsPossessed(SCR_EditableEntityComponent entity)
54  {
55  return m_PossessedEntities.GetKeyByValue(entity) != 0;
56  }
62  int GetPlayers(notnull map<int, SCR_EditableEntityComponent> players)
63  {
64  if (!m_MainEntities)
65  {
66  players.Clear();
67  return 0;
68  }
69  players.Copy(m_MainEntities);
70  return m_MainEntities.Count();
71  }
76  int GetPlayersCount()
77  {
78  return m_MainEntities.Count();
79  }
80 
81  void TeleportPlayerToPosition(vector position)
82  {
84  if (statesManager && statesManager.GetState() != EEditorState.SELECTING)
85  return;
86 
87  Rpc(TeleportPlayerToPositionServer, position);
88  }
89 
90  [RplRpc(RplChannel.Reliable, RplRcver.Server)]
91  void TeleportPlayerToPositionServer(vector position)
92  {
93  // Verify player has editor open
94  SCR_EditorManagerEntity editorManager = GetManager();
95  if (!editorManager || !editorManager.IsOpened())
96  return;
97 
98  IEntity player = SCR_PossessingManagerComponent.GetPlayerMainEntity(editorManager.GetPlayerID());
99  if (!player)
100  return;
101 
102  CompartmentAccessComponent compartmentAccess = CompartmentAccessComponent.Cast(player.FindComponent(CompartmentAccessComponent));
103  if (compartmentAccess && compartmentAccess.IsInCompartment())
104  TeleportPlayerToPositionOwner(position); //--- Player in a vehicle, execute on server
105  else
106  Rpc(TeleportPlayerToPositionOwner, position); //--- Player not in a vehicle, execute where the player is local
107  }
108 
109  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
110  void TeleportPlayerToPositionOwner(vector position)
111  {
112  SCR_Global.TeleportPlayer(GetManager().GetPlayerID(), position);
113  }
114 
119  bool HasLocalPlayerSpawnedOnce()
120  {
121  return m_LocalPlayerHasSpawnedOnce;
122  }
123 
128  ScriptInvoker GetOnDeath()
129  {
130  return Event_OnDeath;
131  }
136  ScriptInvoker GetOnSpawn()
137  {
138  return Event_OnSpawn;
139  }
144  ScriptInvoker GetOnPossessed()
145  {
146  return Event_OnPossessed;
147  }
152  ScriptInvoker GetOnConnected()
153  {
154  return Event_OnConnected;
155  }
160  ScriptInvoker GetOnDisconnected()
161  {
162  return Event_OnDisconnected;
163  }
164 
168  void Log()
169  {
170  if (!m_MainEntities) return;
171  Print("--------------------------------------------------", LogLevel.DEBUG);
172  Print(string.Format("--- Players (%2)", m_MainEntities.Count()), LogLevel.DEBUG);
173  for (int i = 0; i < m_MainEntities.Count(); i++)
174  {
175  Print("Player ID: " + m_MainEntities.GetKey(i).ToString(), LogLevel.DEBUG);
176 
177  SCR_EditableEntityComponent entity = null;
178  if (m_MainEntities.Find(m_MainEntities.GetKey(i), entity))
179  {
180  entity.Log("", true);
181  }
182  else
183  {
184  Print("Entity not found!", LogLevel.WARNING);
185  }
186  }
187  Print("--------------------------------------------------", LogLevel.DEBUG);
188  }
189 
190  protected void OnSpawnServer(int playerID, IEntity controlledEntity)
191  {
192  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.GetEditableEntity(controlledEntity);
193  if (!entity)
194  return;
195 
196  UpdatePlayerFaction(entity, controlledEntity);
197  UpdatePlayerGroup(entity, controlledEntity, playerID);
198 
199  Rpc(OnSpawnOwner, playerID, Replication.FindId(entity));
200  }
201 
202  protected void UpdatePlayerGroup(notnull SCR_EditableEntityComponent editableEntity, notnull IEntity controlledEntity, int playerID)
203  {
204  SCR_GroupsManagerComponent groupManager = SCR_GroupsManagerComponent.GetInstance();
205  if (!groupManager)
206  return;
207 
208  Faction entityFaction = editableEntity.GetFaction();
209  SCR_AIGroup group = groupManager.GetPlayerGroup(playerID);
210  if (group && group.GetFaction() == entityFaction)
211  return;
212 
213  SCR_AIGroup foundGroup = groupManager.GetFirstNotFullForFaction(entityFaction, null, true);
214  if (!foundGroup)
215  foundGroup = groupManager.CreateNewPlayableGroup(entityFaction);
216 
217  foundGroup.AddPlayer(playerID);
218  }
219 
220  protected void UpdatePlayerFaction(SCR_EditableEntityComponent editableEntity, IEntity controlledEntity)
221  {
222  FactionAffiliationComponent factionAffiliaton = FactionAffiliationComponent.Cast(controlledEntity.FindComponent(FactionAffiliationComponent));
223  if (!factionAffiliaton)
224  return;
225 
226  factionAffiliaton.SetAffiliatedFaction(editableEntity.GetFaction());
227  }
228 
229  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
230  protected void OnSpawnOwner(int playerID, int entityID)
231  {
232  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.Cast(Replication.FindItem(entityID));
233  if (!entity) return;
234 
235  //--- Get previous entity
236  SCR_EditableEntityComponent entityPrev = null;
237  m_MainEntities.Find(playerID, entityPrev);
238 
239  //--- No change, ignore
240  if (entity == entityPrev) return;
241 
242  //Local player was spawned for the first time
243  if (!m_LocalPlayerHasSpawnedOnce && SCR_PlayerController.GetLocalPlayerId() == playerID)
244  m_LocalPlayerHasSpawnedOnce = true;
245 
246  //--- Set and send further
247  m_MainEntities.Set(playerID, entity);
248  Event_OnSpawn.Invoke(playerID, entity, entityPrev);
249  }
250  protected void OnPossessedServer(int playerID, IEntity controlledEntity, IEntity mainEntity, bool isPossessing)
251  {
252  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.GetEditableEntity(controlledEntity);
253  if (!entity) return;
254 
255  Rpc(OnPossessedOwner, playerID, isPossessing, Replication.FindId(entity));
256  }
257  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
258  protected void OnPossessedOwner(int playerID, bool isPossessing, int entityID)
259  {
260  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.Cast(Replication.FindItem(entityID));
261  if (!entity) return;
262 
263  if (isPossessing)
264  m_PossessedEntities.Insert(playerID, entity);
265  else
266  m_PossessedEntities.Remove(playerID);
267 
268  Event_OnPossessed.Invoke(playerID, entity, isPossessing);
269  }
270  protected void OnDeathServer(int playerId, IEntity playerEntity, IEntity killerEntity, notnull Instigator killer)
271  {
272  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.GetEditableEntity(playerEntity);
273  if (!entity) return;
274 
275  SCR_EditableEntityComponent killerEditableEntity = SCR_EditableEntityComponent.GetEditableEntity(killerEntity);
276 
277  Rpc(OnDeathOwner, playerId, Replication.FindId(entity), Replication.FindId(killerEditableEntity));
278  }
279  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
280  protected void OnDeathOwner(int playerID, int entityID, int killerID)
281  {
282  SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.Cast(Replication.FindItem(entityID));
283  if (!entity) return;
284 
285  SCR_EditableEntityComponent killerEntity = SCR_EditableEntityComponent.Cast(Replication.FindItem(killerID));
286 
287  Event_OnDeath.Invoke(playerID, entity, killerEntity);
288  }
289  protected void OnConnectedServer(int playerID)
290  {
291  Rpc(OnConnectedOwner, playerID);
292  }
293  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
294  protected void OnConnectedOwner(int playerID)
295  {
296  m_MainEntities.Insert(playerID, null);
297  Event_OnConnected.Invoke(playerID);
298  }
299  protected void OnDisconnectedServer(int playerID)
300  {
301  if (!Replication.IsRunning()) return;
302 
304  if (!owner) return;
305 
306  SCR_EditorManagerEntity manager = owner.GetManager();
307  if (!manager) return;
308 
309  //--- Don't call the code on its own disconnection
310  if (playerID != manager.GetPlayerID())
311  Rpc(OnDisconnectedOwner, playerID);
312  }
313  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
314  protected void OnDisconnectedOwner(int playerID)
315  {
316  m_MainEntities.Remove(playerID);
317  Event_OnDisconnected.Invoke(playerID);
318  }
319  protected void StopPosessing()
320  {
321  if (!GetManager())
322  return;
323 
324  SCR_PlayerController playerController = SCR_PlayerController.Cast(GetGame().GetPlayerManager().GetPlayerController(GetManager().GetPlayerID()));
325 
326  if (playerController)
327  playerController.SetPossessedEntity(null);
328  }
329  protected void OnLimitedChange(bool isLimited)
330  {
331  if (isLimited)
332  StopPosessing();
333  }
334  protected void OnEntityRegistered(SCR_EditableEntityComponent entity)
335  {
336  //--- Update player entity when it's streamed in
337  int playerID = entity.GetPlayerID();
338  if (playerID != 0)
339  {
340  m_MainEntities.Set(playerID, entity);
341  Event_OnSpawn.Invoke(playerID, entity, null);
342  }
343  }
344  protected void SetServerHandler(bool toAdd)
345  {
346  if (RplSession.Mode() == RplMode.Client) return;
347 
349  if (!gameMode)
350  return;
351 
352  SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
353 
354  if (toAdd)
355  {
356  gameMode.GetOnPlayerSpawned().Insert(OnSpawnServer);
357  gameMode.GetOnPlayerKilled().Insert(OnDeathServer);
358  gameMode.GetOnPlayerConnected().Insert(OnConnectedServer);
359  gameMode.GetOnPlayerDisconnected().Insert(OnDisconnectedServer);
360 
361  if (possessingManager)
362  possessingManager.GetOnPossessed().Insert(OnPossessedServer);
363  }
364  else
365  {
366  gameMode.GetOnPlayerSpawned().Remove(OnSpawnServer);
367  gameMode.GetOnPlayerKilled().Remove(OnDeathServer);
368  gameMode.GetOnPlayerConnected().Remove(OnConnectedServer);
369  gameMode.GetOnPlayerDisconnected().Remove(OnDisconnectedServer);
370 
371  if (possessingManager)
372  possessingManager.GetOnPossessed().Remove(OnPossessedServer);
373  }
374  }
375  override void EOnEditorOpen()
376  {
377  m_MainEntities = new map<int, SCR_EditableEntityComponent>;
378  m_PossessedEntities = new map<int, SCR_EditableEntityComponent>;
379 
380  SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
381 
382  //--- Add already registered players
383  array<int> playerIDs = new array<int>;
384  GetGame().GetPlayerManager().GetPlayers(playerIDs);
385  IEntity owner;
387  foreach (auto playerID: playerIDs)
388  {
389  owner = SCR_PossessingManagerComponent.GetPlayerMainEntity(playerID);
390  entity = SCR_EditableEntityComponent.GetEditableEntity(owner);
391  m_MainEntities.Insert(playerID, entity);
392 
393  if (possessingManager && possessingManager.IsPossessing(playerID))
394  {
395  owner = GetGame().GetPlayerManager().GetPlayerControlledEntity(playerID);
396  entity = SCR_EditableEntityComponent.GetEditableEntity(owner);
397  if (entity)
398  m_PossessedEntities.Insert(playerID, entity);
399  }
400  }
401 
403  core.Event_OnEntityRegistered.Insert(OnEntityRegistered);
404  }
405  override void EOnEditorClose()
406  {
407  m_MainEntities = null;
408  m_PossessedEntities = null;
409 
411  core.Event_OnEntityRegistered.Remove(OnEntityRegistered);
412  }
413 
414  override void EOnEditorOpenServerCallback()
415  {
416  SetServerHandler(true);
417 
418  //--- Stop possessing an entity upon opening the editor
419  StopPosessing();
420  }
421  override void EOnEditorCloseServer()
422  {
423  SetServerHandler(false);
424  }
425  override void EOnEditorInit()
426  {
427  super.EOnEditorInit();
428 
429  if (SCR_PlayerController.GetLocalMainEntity())
430  m_LocalPlayerHasSpawnedOnce = true;
431  }
432  override void EOnEditorInitServer()
433  {
434  //--- Stop possessing an entity upon changing editor mode (even outside of editor; happens when the current mode is removed)
435  GetManager().GetOnModeChange().Insert(StopPosessing);
436  GetManager().GetOnLimitedChange().Insert(OnLimitedChange);
437  }
438  override void EOnEditorDeleteServer()
439  {
440  if (GetManager())
441  {
442  GetManager().GetOnModeChange().Remove(StopPosessing);
443  GetManager().GetOnLimitedChange().Remove(OnLimitedChange);
444  }
445 
446  //--- Stop possessing an entity when editor is deleted
447  StopPosessing();
448  }
449 };
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
SCR_BaseGameMode
Definition: SCR_BaseGameMode.c:137
SCR_EditableEntityCore
Definition: SCR_EditableEntityCore.c:10
SCR_PlayerController
Definition: SCR_PlayerController.c:31
m_MainEntities
SCR_PossessingManagerComponentClass m_MainEntities
SCR_PlayersManagerEditorComponent
Definition: SCR_PlayersManagerEditorComponent.c:9
EEditorState
EEditorState
Unique editor state.
Definition: EEditorState.c:5
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
GetPlayerController
proto external PlayerController GetPlayerController()
Definition: SCR_PlayerDeployMenuHandlerComponent.c:307
Instigator
Definition: Instigator.c:6
GetGameMode
SCR_BaseGameMode GetGameMode()
Definition: SCR_BaseGameModeComponent.c:15
SCR_BaseEditorComponent
Definition: SCR_BaseEditorComponent.c:119
Attribute
typedef Attribute
Post-process effect of scripted camera.
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
Event_OnPossessed
protected ref ScriptInvoker Event_OnPossessed
Definition: SCR_PossessingManagerComponent.c:13
SCR_BaseEditorComponentClass
Definition: SCR_BaseEditorComponent.c:2
SCR_EditableEntityComponent
Definition: SCR_EditableEntityComponent.c:13
Faction
Definition: Faction.c:12
SCR_GroupsManagerComponent
void SCR_GroupsManagerComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_GroupsManagerComponent.c:1320
SCR_Global
Definition: Functions.c:6
SCR_AIGroup
Definition: SCR_AIGroup.c:68
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
SCR_StatesEditorComponent
Definition: SCR_StatesEditorComponent.c:9
SCR_EditorBaseEntity
Definition: SCR_EditorBaseEntity.c:14
position
vector position
Definition: SCR_DestructibleTreeV2.c:30
SCR_PlayersManagerEditorComponentClass
Definition: SCR_PlayersManagerEditorComponent.c:2
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
SCR_EditorManagerEntity
Definition: SCR_EditorManagerEntity.c:26