Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_PlayersManagerEditorComponent.c
Go to the documentation of this file.
1[ComponentEditorProps(category: "GameScripted/Editor", description: "", icon: "WBData/ComponentEditorProps/componentEditor.png")]
5
10{
11 [Attribute(uiwidget: UIWidgets.ResourcePickerThumbnail, params: "et")]
13
16
17 protected bool m_LocalPlayerHasSpawnedOnce = false;
18
24
31 {
32 if (!m_MainEntities) return null;
33 SCR_EditableEntityComponent entity = null;
34 m_MainEntities.Find(playerID, entity);
35 return entity;
36 }
37
43 {
44 if (!m_MainEntities)
45 return 0;
46
47 int playerID = SCR_MapHelper<int, SCR_EditableEntityComponent>.GetKeyByValue(m_MainEntities,entity);
48 if (playerID != 0)
49 return playerID;
50 else
51 return SCR_MapHelper<int, SCR_EditableEntityComponent>.GetKeyByValue(m_PossessedEntities,entity);
52 }
54 {
55 return SCR_MapHelper<int, SCR_EditableEntityComponent>.GetKeyByValue(m_PossessedEntities,entity) != 0;
56 }
57
63 {
64 if (!m_MainEntities)
65 {
66 players.Clear();
67 return 0;
68 }
69 players.Copy(m_MainEntities);
70 return m_MainEntities.Count();
71 }
72
77 {
78 return m_MainEntities.Count();
79 }
80
82 {
84 if (statesManager && statesManager.GetState() != EEditorState.SELECTING)
85 return;
86
88 }
89
90 [RplRpc(RplChannel.Reliable, RplRcver.Server)]
92 {
93 // Verify player has editor open
94 SCR_EditorManagerEntity editorManager = GetManager();
95 if (!editorManager || !editorManager.IsOpened())
96 return;
97
99 return;
100
101 IEntity player = SCR_PossessingManagerComponent.GetPlayerMainEntity(editorManager.GetPlayerID());
102 if (!player)
103 return;
104
105 IEntity parent = player.GetRootParent();
106 if (parent == player)
107 // Player not attached to anything, execute where the player is local
108 Rpc(TeleportPlayerToPositionOwner, editorManager.GetPlayerID(), position);
109 else
110 // Player attached to something, execute on the server
111 TeleportPlayerToPositionServer(player, editorManager.GetPlayerID(), position);
112 }
113
115 {
116 if (!SCR_Global.TeleportPlayer(playerId, position))
117 return;
118
119 /* Commented because the owner would not get instant reponse.
120 The original idea was to let the NwkMovementComponent replicate the change.
121
122 // If the player is inside a vehicle we don't need to broadcast if NwkMovementComponent is present and active.
123 // It will take care of the sync.
124 SCR_CompartmentAccessComponent compartmentAccess = SCR_CompartmentAccessComponent.Cast(player.FindComponent(SCR_CompartmentAccessComponent));
125 if (compartmentAccess)
126 {
127 IEntity vehicle = compartmentAccess.GetVehicle();
128 if (vehicle)
129 {
130 NwkMovementComponent nwk = NwkMovementComponent.Cast(vehicle.FindComponent(NwkMovementComponent));
131 if (nwk != null && nwk.IsSimulationEnabled())
132 return;
133 }
134 }*/
135
137 }
138
139 [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
144
145 [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
147 {
149 }
150
159
165 {
166 return Event_OnDeath;
167 }
168
173 {
174 return Event_OnSpawn;
175 }
176
184
192
200
204 void Log()
205 {
206 if (!m_MainEntities) return;
207 Print("--------------------------------------------------", LogLevel.DEBUG);
208 Print(string.Format("--- Players (%2)", m_MainEntities.Count()), LogLevel.DEBUG);
209 for (int i = 0; i < m_MainEntities.Count(); i++)
210 {
211 Print("Player ID: " + m_MainEntities.GetKey(i).ToString(), LogLevel.DEBUG);
212
213 SCR_EditableEntityComponent entity = null;
214 if (m_MainEntities.Find(m_MainEntities.GetKey(i), entity))
215 {
216 entity.Log("", true);
217 }
218 else
219 {
220 Print("Entity not found!", LogLevel.WARNING);
221 }
222 }
223 Print("--------------------------------------------------", LogLevel.DEBUG);
224 }
225
226 protected void OnSpawnServer(int playerID, IEntity controlledEntity)
227 {
229 if (!entity)
230 return;
231
232 UpdatePlayerFaction(entity, controlledEntity);
233 UpdatePlayerGroup(entity, controlledEntity, playerID);
234
235 Rpc(OnSpawnOwner, playerID, Replication.FindItemId(entity));
236 }
237
238 protected void UpdatePlayerGroup(notnull SCR_EditableEntityComponent editableEntity, notnull IEntity controlledEntity, int playerID)
239 {
240 Faction entityFaction = editableEntity.GetFaction();
241
242 SCR_PlayerController controller = SCR_PlayerController.Cast(GetGame().GetPlayerManager().GetPlayerController(playerID));
243 if (!controller)
244 return;
245
246 SCR_PlayerControllerGroupComponent groupComponent = SCR_PlayerControllerGroupComponent.Cast(controller.FindComponent(SCR_PlayerControllerGroupComponent));
247 if (!groupComponent)
248 return;
249
250 if (groupComponent.GetGroupID() == -1)
251 {
252 // player does not have a player group, yet
253 groupComponent.CreateAndJoinGroup(entityFaction);
254 }
255 else
256 {
257 // player may have player group, but the new editableEntity has different than his old player group
258 SCR_AIGroup currentPlayerGroup = groupComponent.GetPlayersGroup();
259 if (!currentPlayerGroup)
260 return;
261
262 Faction playerOldFaction = currentPlayerGroup.GetFaction();
263 if (playerOldFaction != entityFaction)
264 groupComponent.CreateAndJoinGroup(entityFaction);
265 }
266 }
267
268 protected void UpdatePlayerFaction(SCR_EditableEntityComponent editableEntity, IEntity controlledEntity)
269 {
270 FactionAffiliationComponent factionAffiliaton = FactionAffiliationComponent.Cast(controlledEntity.FindComponent(FactionAffiliationComponent));
271 if (!factionAffiliaton)
272 return;
273
274 factionAffiliaton.SetAffiliatedFaction(factionAffiliaton.GetAffiliatedFaction());
275 }
276
277 [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
278 protected void OnSpawnOwner(int playerID, int entityID)
279 {
281 if (!entity) return;
282
283 //--- Get previous entity
284 SCR_EditableEntityComponent entityPrev = null;
285 m_MainEntities.Find(playerID, entityPrev);
286
287 //--- No change, ignore
288 if (entity == entityPrev) return;
289
290 //Local player was spawned for the first time
293
294 //--- Set and send further
295 m_MainEntities.Set(playerID, entity);
296 Event_OnSpawn.Invoke(playerID, entity, entityPrev);
297 }
298 protected void OnPossessedServer(int playerID, IEntity controlledEntity, IEntity mainEntity, bool isPossessing)
299 {
301 if (!entity) return;
302
303 Rpc(OnPossessedOwner, playerID, isPossessing, Replication.FindItemId(entity));
304 }
305 [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
306 protected void OnPossessedOwner(int playerID, bool isPossessing, int entityID)
307 {
309 if (!entity) return;
310
311 if (isPossessing)
312 m_PossessedEntities.Insert(playerID, entity);
313 else
314 m_PossessedEntities.Remove(playerID);
315
316 Event_OnPossessed.Invoke(playerID, entity, isPossessing);
317 }
318 protected void OnDeathServer(notnull SCR_InstigatorContextData instigatorContextData)
319 {
320 SCR_EditableEntityComponent entity = SCR_EditableEntityComponent.GetEditableEntity(instigatorContextData.GetVictimEntity());
321 if (!entity)
322 return;
323
324 SCR_EditableEntityComponent killerEditableEntity = SCR_EditableEntityComponent.GetEditableEntity(instigatorContextData.GetKillerEntity());
325
326 Rpc(OnDeathOwner, instigatorContextData.GetVictimPlayerID(), Replication.FindItemId(entity), Replication.FindItemId(killerEditableEntity));
327 }
328 [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
329 protected void OnDeathOwner(int playerID, int entityID, int killerID)
330 {
332 if (!entity) return;
333
334 SCR_EditableEntityComponent killerEntity = SCR_EditableEntityComponent.Cast(Replication.FindItem(killerID));
335
336 Event_OnDeath.Invoke(playerID, entity, killerEntity);
337 }
338 protected void OnConnectedServer(int playerID)
339 {
340 Rpc(OnConnectedOwner, playerID);
341 }
342 [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
343 protected void OnConnectedOwner(int playerID)
344 {
345 m_MainEntities.Insert(playerID, null);
346 Event_OnConnected.Invoke(playerID);
347 }
348 protected void OnDisconnectedServer(int playerID)
349 {
350 if (!Replication.IsRunning()) return;
351
353 if (!owner) return;
354
355 SCR_EditorManagerEntity manager = owner.GetManager();
356 if (!manager) return;
357
358 //--- Don't call the code on its own disconnection
359 if (playerID != manager.GetPlayerID())
360 Rpc(OnDisconnectedOwner, playerID);
361 }
362 [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
363 protected void OnDisconnectedOwner(int playerID)
364 {
365 m_MainEntities.Remove(playerID);
366 Event_OnDisconnected.Invoke(playerID);
367 }
368 protected void StopPosessing()
369 {
370 if (!GetManager())
371 return;
372
373 SCR_PlayerController playerController = SCR_PlayerController.Cast(GetGame().GetPlayerManager().GetPlayerController(GetManager().GetPlayerID()));
374
375 if (playerController)
376 playerController.SetPossessedEntity(null);
377 }
378 protected void OnLimitedChange(bool isLimited)
379 {
380 if (isLimited)
382 }
384 {
385 //--- Update player entity when it's streamed in
386 int playerID = entity.GetPlayerID();
387 if (playerID != 0)
388 {
389 m_MainEntities.Set(playerID, entity);
390 Event_OnSpawn.Invoke(playerID, entity, null);
391 }
392 }
393 protected void SetServerHandler(bool toAdd)
394 {
395 if (RplSession.Mode() == RplMode.Client) return;
396
398 if (!gameMode)
399 return;
400
401 SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
402
403 if (toAdd)
404 {
405 gameMode.GetOnPlayerSpawned().Insert(OnSpawnServer);
406 gameMode.GetOnPlayerKilled().Insert(OnDeathServer);
407 gameMode.GetOnPlayerConnected().Insert(OnConnectedServer);
409
410 if (possessingManager)
411 possessingManager.GetOnPossessed().Insert(OnPossessedServer);
412 }
413 else
414 {
415 gameMode.GetOnPlayerSpawned().Remove(OnSpawnServer);
416 gameMode.GetOnPlayerKilled().Remove(OnDeathServer);
417 gameMode.GetOnPlayerConnected().Remove(OnConnectedServer);
419
420 if (possessingManager)
421 possessingManager.GetOnPossessed().Remove(OnPossessedServer);
422 }
423 }
424 override void EOnEditorOpen()
425 {
428
429 SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
430
431 //--- Add already registered players
432 array<int> playerIDs = new array<int>;
433 GetGame().GetPlayerManager().GetPlayers(playerIDs);
434 IEntity owner;
436 foreach (auto playerID: playerIDs)
437 {
438 owner = SCR_PossessingManagerComponent.GetPlayerMainEntity(playerID);
440 m_MainEntities.Insert(playerID, entity);
441
442 if (possessingManager && possessingManager.IsPossessing(playerID))
443 {
444 owner = GetGame().GetPlayerManager().GetPlayerControlledEntity(playerID);
446 if (entity)
447 m_PossessedEntities.Insert(playerID, entity);
448 }
449 }
450
452 core.Event_OnEntityRegistered.Insert(OnEntityRegistered);
453 }
454 override void EOnEditorClose()
455 {
456 m_MainEntities = null;
457 m_PossessedEntities = null;
458
460 core.Event_OnEntityRegistered.Remove(OnEntityRegistered);
461 }
462
464 {
465 SetServerHandler(true);
466
467 //--- Stop possessing an entity upon opening the editor
469 }
470 override void EOnEditorCloseServer()
471 {
472 SetServerHandler(false);
473 }
474 override void EOnEditorInit()
475 {
476 super.EOnEditorInit();
477
480 }
481 override void EOnEditorInitServer()
482 {
483 //--- Stop possessing an entity upon changing editor mode (even outside of editor; happens when the current mode is removed)
484 GetManager().GetOnModeChange().Insert(StopPosessing);
485 GetManager().GetOnLimitedChange().Insert(OnLimitedChange);
486 }
487 override void EOnEditorDeleteServer()
488 {
489 if (GetManager())
490 {
491 GetManager().GetOnModeChange().Remove(StopPosessing);
492 GetManager().GetOnLimitedChange().Remove(OnLimitedChange);
493 }
494
495 //--- Stop possessing an entity when editor is deleted
497 }
498};
ArmaReforgerScripted GetGame()
Definition game.c:1398
RplMode
Mode of replication.
Definition RplMode.c:9
enum EAIGroupCombatMode ComponentEditorProps(category:"GameScripted/AI", description:"Component for utility AI system for groups")
SCR_BaseGameMode GetGameMode()
vector position
override int GetPlayerID()
void SCR_EditorManagerEntity(IEntitySource src, IEntity parent)
void Rpc(func method, void p0=NULL, void p1=NULL, void p2=NULL, void p3=NULL, void p4=NULL, void p5=NULL, void p6=NULL, void p7=NULL)
proto external Managed FindComponent(typename typeName)
proto external IEntity GetRootParent()
Main replication API.
Definition Replication.c:14
Faction GetFaction()
void SCR_BaseEditorComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
SCR_EditorManagerEntity GetManager()
ScriptInvokerBase< SCR_BaseGameMode_OnPlayerDisconnected > GetOnPlayerDisconnected()
ScriptInvokerBase< SCR_BaseGameMode_PlayerIdAndEntity > GetOnPlayerSpawned()
ScriptInvokerBase< SCR_BaseGameMode_PlayerId > GetOnPlayerConnected()
ScriptInvokerBase< SCR_BaseGameMode_OnControllableDestroyed > GetOnPlayerKilled()
void Log(string prefix="", bool onlyDirect=false, LogLevel logLevel=LogLevel.DEBUG)
static SCR_EditableEntityComponent GetEditableEntity(IEntity owner)
static bool IsPositionWithinTerrainBounds(vector pos)
Definition Functions.c:1981
static bool TeleportPlayer(int playerId, vector worldPosition, SCR_EPlayerTeleportedReason teleportReason=SCR_EPlayerTeleportedReason.DEFAULT)
Definition Functions.c:1638
void SetPossessedEntity(IEntity entity)
static int GetLocalPlayerId()
Returns either a valid ID of local player or 0.
static IEntity GetLocalMainEntity()
void OnEntityRegistered(SCR_EditableEntityComponent entity)
void TeleportPlayerToPositionBroadcast(int playerId, vector position)
void TeleportPlayerToPositionServer(IEntity player, int playerId, vector position)
void OnDeathServer(notnull SCR_InstigatorContextData instigatorContextData)
bool IsPossessed(SCR_EditableEntityComponent entity)
void UpdatePlayerGroup(notnull SCR_EditableEntityComponent editableEntity, notnull IEntity controlledEntity, int playerID)
void OnSpawnServer(int playerID, IEntity controlledEntity)
void OnDeathOwner(int playerID, int entityID, int killerID)
void UpdatePlayerFaction(SCR_EditableEntityComponent editableEntity, IEntity controlledEntity)
void OnPossessedServer(int playerID, IEntity controlledEntity, IEntity mainEntity, bool isPossessing)
int GetPlayers(notnull map< int, SCR_EditableEntityComponent > players)
ref map< int, SCR_EditableEntityComponent > m_MainEntities
ref map< int, SCR_EditableEntityComponent > m_PossessedEntities
void OnPossessedOwner(int playerID, bool isPossessing, int entityID)
void TeleportPlayerToPositionOwner(int playerId, vector position)
SCR_EditableEntityComponent GetPlayerEntity(int playerID)
int GetPlayerID(SCR_EditableEntityComponent entity)
proto external GenericEntity GetOwner()
Get owner entity.
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
SCR_FieldOfViewSettings Attribute
EEditorState
Unique editor state.
Definition EEditorState.c:6
proto external PlayerController GetPlayerController()
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
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134