Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_PossessingManagerComponent.c
Go to the documentation of this file.
1void ScriptInvokerOnPossessedProxyMethod(int playerID, bool isPossessing, RplId mainEntityID);
3typedef ScriptInvokerBase<ScriptInvokerOnPossessedProxyMethod> ScriptInvokerOnPossessedProxy;
4
5[ComponentEditorProps(category: "GameScripted/GameMode/Components", description: "")]
9
10class SCR_PossessingManagerComponent : SCR_BaseGameModeComponent
11{
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 //------------------------------------------------------------------------------------------------
40
41 //------------------------------------------------------------------------------------------------
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 //------------------------------------------------------------------------------------------------
94 {
95 if (IsPossessing(iPlayerId))
96 return GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerId);
97 else
98 return null;
99 }
100
101 //------------------------------------------------------------------------------------------------
106 {
107 if (IsPossessing(iPlayerId))
108 return GetRplId(GetGame().GetPlayerManager().GetPlayerControlledEntity(iPlayerId));
109 else
110 return RplId.Invalid();
111 }
112
113 //------------------------------------------------------------------------------------------------
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 //------------------------------------------------------------------------------------------------
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 //------------------------------------------------------------------------------------------------
148 {
149 int iPlayerId = GetGame().GetPlayerManager().GetPlayerIdFromControlledEntity(entity);
150 if (iPlayerId > 0)
151 return iPlayerId;
152 else
153 return GetIdFromMainEntity(entity);
154 }
155
156 //------------------------------------------------------------------------------------------------
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
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(notnull SCR_InstigatorContextData instigatorContextData)
338 {
339 int pid = instigatorContextData.GetVictimPlayerID();
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 instigator)
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, instigator);
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}
ArmaReforgerScripted GetGame()
Definition game.c:1398
override bool RplLoad(ScriptBitReader reader)
enum EAIGroupCombatMode ComponentEditorProps(category:"GameScripted/AI", description:"Component for utility AI system for groups")
void SCR_BaseGameModeComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
override bool RplSave(ScriptBitWriter writer)
int GetPlayers(out notnull array< int > outPlayers)
void SCR_EditorManagerEntity(IEntitySource src, IEntity parent)
void OnControllableDestroyed(IEntity entity, IEntity killerEntity, Instigator instigator, notnull SCR_InstigatorContextData instigatorContextData)
int GetIdFromMainRplId(RplId rplId)
int GetIdFromMainEntity(IEntity entity)
void SetMainEntityBroadcast(int playerID, bool isPossessing, RplId mainEntityID)
int GetIdFromControlledEntity(IEntity entity)
IEntity GetPossessedEntity(int iPlayerId)
ScriptInvoker GetOnPossessed()
SCR_PossessingManagerComponentClass m_MainEntities
void SetMainEntity(int playerID, IEntity controlledEntity, IEntity mainEntity, bool isPossessing)
int GetIdFromControlledRplId(RplId rplId)
ScriptInvokerOnPossessedProxy GetOnPossessedProxy()
RplId GetRplId(IEntity entity)
ref ScriptInvoker Event_OnPossessed
RplId GetMainRplId(int iPlayerId)
ScriptInvokerBase< ScriptInvokerOnPossessedProxyMethod > ScriptInvokerOnPossessedProxy
IEntity GetMainEntity(int iPlayerId)
override bool HandlePlayerKilled(int playerId, IEntity playerEntity, IEntity killerEntity, notnull Instigator instigator)
func ScriptInvokerOnPossessedProxyMethod
ref ScriptInvokerOnPossessedProxy Event_OnPossessedProxy
override void OnControllableDeleted(IEntity entity)
bool IsPossessing(int iPlayerId)
RplId GetPossessedRplId(int iPlayerId)
void SCR_RespawnComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
enum EVehicleType IEntity
proto external Managed FindComponent(typename typeName)
Main replication API.
Definition Replication.c:14
Replication item identifier.
Definition RplId.c:14
Core component to manage SCR_EditorManagerEntity.
SCR_EditorManagerEntity GetEditorManager()
void SetPossessedEntity(IEntity entity)
Definition Types.c:486
override void OnPlayerDisconnected(int playerId, KickCauseCode cause, int timeout)
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
int RplId
Definition EnNetwork.c:33
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134