Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_SpawnHandlerComponent.c
Go to the documentation of this file.
1 class SCR_SpawnHandlerComponentClass : ScriptComponentClass
2 {
3 }
4 
13 class SCR_SpawnHandlerComponent : ScriptComponent
14 {
15  private SCR_RespawnSystemComponent m_RespawnSystemComponent;
16 
17  //------------------------------------------------------------------------------------------------
19  protected SCR_RespawnSystemComponent GetRespawnSystemComponent()
20  {
22  }
23 
24  [Attribute(category: "Respawn Handler", desc: "If enabled, previous controlled entity prior to respawn will be destroyed (killed).")]
26 
27  [Attribute(category: "Respawn Handler", desc: "If enabled, previous controlled entity prior to respawn will be destroyed (killed).")]
29 
30  //------------------------------------------------------------------------------------------------
33  protected override void OnPostInit(IEntity owner)
34  {
35  super.OnPostInit(owner);
36 
37  m_RespawnSystemComponent = SCR_RespawnSystemComponent.Cast(owner.FindComponent(SCR_RespawnSystemComponent));
39  Debug.Error(string.Format("%1 could not find %2! (should be a child of)", Type().ToString(), SCR_RespawnSystemComponent));
40  }
41 
42  //------------------------------------------------------------------------------------------------
46  SCR_ESpawnResult CanHandleRequest_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data)
47  {
48  int playerId = requestComponent.GetPlayerController().GetPlayerId();
49  #ifdef _ENABLE_RESPAWN_LOGS
50  Print(string.Format("%1::CanHandleRequest_S(playerId: %2, data: %3)", Type().ToString(),
51  playerId,
52  data), LogLevel.NORMAL);
53  #endif
54 
55  // The authority can activate and deactivate respawn handlers at will, but
56  // only the active ones should resolve incoming requests. This should allow
57  // the user to enable and disable individual respawning methods on the fly
58  if (!IsActive())
59  return SCR_ESpawnResult.UNSUPPORTED_SPAWN_METHOD;
60 
61  // Now that data is updated, validate the data prior to
62  // doing any game specific checks. An invalid request
63  // is dropped without ever reaching the game.
64  if (!ValidateData_S(requestComponent, data))
65  return SCR_ESpawnResult.CANNOT_VALIDATE;
66 
67  // See if player can be spawned with provided data,
68  // this can be e.g. based on gamemodes
69  SCR_ESpawnResult requestResult;
70  if (!CanRequestSpawn_S(requestComponent, data, requestResult))
71  return requestResult;
72 
73  return SCR_ESpawnResult.OK;
74  }
75 
76  //------------------------------------------------------------------------------------------------
82  SCR_ESpawnResult HandleRequest_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, out IEntity spawnedEntity)
83  {
84  int playerId = requestComponent.GetPlayerController().GetPlayerId();
85  #ifdef _ENABLE_RESPAWN_LOGS
86  Print(string.Format("%1::HandleRequest_S(playerId: %2, data: %3)", Type().ToString(),
87  playerId,
88  data), LogLevel.NORMAL);
89  #endif
90 
91  // Validate whether spawn can happen
92  SCR_ESpawnResult canSpawn = CanHandleRequest_S(requestComponent, data);
93  if (canSpawn != SCR_ESpawnResult.OK)
94  return canSpawn;
95 
96  #ifdef ENABLE_DIAG
97  if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_COMPONENT_TIME))
98  Debug.BeginTimeMeasure();
99  #endif
100 
101  // Process with spawn
102  SCR_ESpawnResult result = SpawnEntity_S(requestComponent, data, spawnedEntity);
103  if (result == SCR_ESpawnResult.OK && !spawnedEntity)
104  {
105  Debug.Error(string.Format("Bad %1 implementation! Result is %2, but entity is %3!",
106  Type().ToString(), typename.EnumToString(SCR_ESpawnResult, result), spawnedEntity));
107 
108  result = SCR_ESpawnResult.INTERNAL_ERROR;
109  }
110 
111  #ifdef ENABLE_DIAG
112  if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_COMPONENT_TIME))
113  Debug.EndTimeMeasure("SpawnEntity_S");
114  #endif
115 
116  return result;
117  }
118 
119  //------------------------------------------------------------------------------------------------
121  void OnFinalizeBegin_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, IEntity entity)
122  {
123  if (SCR_BaseGameMode.Cast(GetGame().GetGameMode()).CanStartSpawnPreload())
124  requestComponent.StartSpawnPreload(data.GetPosition());
125  }
126 
127  //------------------------------------------------------------------------------------------------
133  bool CanFinalize_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, IEntity entity)
134  {
135  return !requestComponent.IsPreloading();
136  }
137 
138  //------------------------------------------------------------------------------------------------
144  SCR_ESpawnResult FinalizeRequest_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, IEntity entity)
145  {
146  int playerId = requestComponent.GetPlayerId();
147  #ifdef _ENABLE_RESPAWN_LOGS
148  Print(string.Format("%1::FinalizeRequest_S(playerId: %2, data: %3)", Type().ToString(),
149  playerId,
150  data), LogLevel.NORMAL);
151  #endif
152 
153  if (!AssignEntity_S(requestComponent, entity, data))
154  return SCR_ESpawnResult.CANNOT_ASSIGN;
155 
156  SCR_RespawnSystemComponent respawnSystem = GetRespawnSystemComponent();
157  respawnSystem.OnPlayerSpawnFinalize_S(requestComponent, this, data, entity);
158  OnFinalizeDone_S(requestComponent, data, entity);
159  return SCR_ESpawnResult.OK;
160  }
161 
162  //------------------------------------------------------------------------------------------------
167  void OnFinalizeDone_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, IEntity entity);
168 
169  //------------------------------------------------------------------------------------------------
174  protected bool ValidateData_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data)
175  {
176  #ifdef _ENABLE_RESPAWN_LOGS
177  Print(string.Format("%1::ValidateData_S(playerId: %1, data: %2)", Type().ToString(), requestComponent.GetPlayerId(), data), LogLevel.NORMAL);
178  #endif
179  return data.IsValid();
180  }
181 
182  //------------------------------------------------------------------------------------------------
190  bool CanRequestSpawn_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, out SCR_ESpawnResult result)
191  {
192  #ifdef _ENABLE_RESPAWN_LOGS
193  Print(string.Format("%1::CanRequestSpawn_S(playerId: %2, data: %3)", Type().ToString(),
194  requestComponent.GetPlayerId(), data), LogLevel.NORMAL);
195  #endif
196 
197  SCR_RespawnSystemComponent respawnSystem = SCR_RespawnSystemComponent.GetInstance();
198  return respawnSystem.CanRequestSpawn_S(requestComponent, this, data, result);
199  }
200 
201  //------------------------------------------------------------------------------------------------
211  protected SCR_ESpawnResult SpawnEntity_S(SCR_SpawnRequestComponent requestComponent, notnull SCR_SpawnData data, out IEntity spawnedEntity)
212  {
213  int playerId = requestComponent.GetPlayerId();
214  #ifdef _ENABLE_RESPAWN_LOGS
215  Print(string.Format("%1::SpawnEntity_S(playerId: %2, data: %3)", Type().ToString(),
216  playerId,
217  data), LogLevel.NORMAL);
218  #endif
219 
220  ResourceName prefab = data.GetPrefab();
221  if (!ValidatePrefab_S(prefab))
222  return SCR_ESpawnResult.INVALID_PREFAB;
223 
224  IEntity previousEntity = GetGame().GetPlayerManager().GetPlayerControlledEntity(playerId);
225 
226  // Transformation
227  EntitySpawnParams params = new EntitySpawnParams();
228  params.TransformMode = ETransformMode.WORLD;
229  vector angles = data.GetAngles();
230  vector yawPitchRoll = Vector(angles[1], angles[0], angles[2]);
231  Math3D.AnglesToMatrix(yawPitchRoll, params.Transform);
232  params.Transform[3] = data.GetPosition();
233 
234  #ifdef ENABLE_DIAG
235  if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_COMPONENT_TIME))
236  Debug.BeginTimeMeasure();
237  #endif
238 
239  // Spawn
240  Resource res = Resource.Load(prefab);
241 
242  #ifdef ENABLE_DIAG
243  if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_COMPONENT_TIME))
244  {
245  Debug.EndTimeMeasure("Resource::Load");
246  Debug.BeginTimeMeasure();
247  }
248  #endif
249 
250  IEntity entity = GetGame().SpawnEntityPrefab(res, params: params);
251 
252  #ifdef ENABLE_DIAG
253  if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_RESPAWN_COMPONENT_TIME))
254  Debug.EndTimeMeasure("Game::SpawnEntityPrefab");
255  #endif
256 
257  if (!PrepareEntity_S(requestComponent, entity, data))
258  {
259  HandleSpawnEntityFailure_S(requestComponent, entity, data, SCR_ESpawnResult.CANNOT_PREPARE);
260  return SCR_ESpawnResult.CANNOT_PREPARE;
261  }
262 
263  spawnedEntity = entity;
264  return SCR_ESpawnResult.OK;
265  }
266 
267  //------------------------------------------------------------------------------------------------
271  protected bool ValidatePrefab_S(ResourceName resourceName)
272  {
273  #ifdef _ENABLE_RESPAWN_LOGS
274  Print(string.Format("%1::ValidatePrefab_S(pref: %2)", Type().ToString(), resourceName), LogLevel.NORMAL);
275  #endif
276 
277  if (resourceName.IsEmpty())
278  return false;
279 
280  Resource res = Resource.Load(resourceName);
281  if (!res.IsValid())
282  return false;
283 
284  IEntityComponentSource rplComponent = SCR_BaseContainerTools.FindComponentSource(res, RplComponent);
285  if (!rplComponent)
286  return false;
287 
288  IEntityComponentSource baseControllerComponent = SCR_BaseContainerTools.FindComponentSource(res, RplComponent);
289  if (!baseControllerComponent)
290  return false;
291 
292  return true;
293  }
294 
295  //------------------------------------------------------------------------------------------------
302  protected bool PrepareEntity_S(SCR_SpawnRequestComponent requestComponent, IEntity entity, SCR_SpawnData data)
303  {
304  #ifdef _ENABLE_RESPAWN_LOGS
305  Print(string.Format("%1::PrepareEntity_S(playerId: %2, entity: %3, data: %4)", Type().ToString(),
306  requestComponent.GetPlayerController().GetPlayerId(),
307  entity,
308  data), LogLevel.NORMAL);
309  #endif
310 
311  return GetRespawnSystemComponent().PreparePlayerEntity_S(requestComponent, this, data, entity);
312  }
313 
314  //------------------------------------------------------------------------------------------------
320  protected bool AssignEntity_S(SCR_SpawnRequestComponent requestComponent, IEntity entity, SCR_SpawnData data)
321  {
322  int playerId = requestComponent.GetPlayerController().GetPlayerId();
323  #ifdef _ENABLE_RESPAWN_LOGS
324  Print(string.Format("%1::AssignEntity_S(playerId: %2, entity: %3, data: %4)", Type().ToString(),
325  playerId,
326  entity,
327  data), LogLevel.NORMAL);
328  #endif
329 
330  SCR_PlayerController playerController = SCR_PlayerController.Cast(GetGame().GetPlayerManager().GetPlayerController(playerId));
331 
332  // Already controlling!
333  IEntity previous = playerController.GetControlledEntity();
334  if (previous == entity)
335  return false;
336 
337  playerController.SetInitialMainEntity(entity);
338 
339  // Notify the system of change
340  HandleEntityChange_S(requestComponent, previous, entity, data);
341  return true;
342  }
343 
344  //------------------------------------------------------------------------------------------------
352  protected void HandleSpawnEntityFailure_S(SCR_SpawnRequestComponent requestComponent, IEntity entity, SCR_SpawnData data, SCR_ESpawnResult reason)
353  {
354  #ifdef _ENABLE_RESPAWN_LOGS
355  Print(string.Format("%1::HandleSpawnEntityFailure_S(playerId: %2, entity: %2, data: %3, res: %4)", Type().ToString(),
356  requestComponent.GetPlayerId(), entity, data, typename.EnumToString(SCR_ESpawnResult, reason)), LogLevel.NORMAL);
357  #endif
358 
359  SCR_RespawnSystemComponent respawnSystem = GetRespawnSystemComponent();
360  respawnSystem.OnSpawnPlayerEntityFailure_S(requestComponent, this, entity, data, reason);
361 
362  if (entity && ShouldDeleteEntityOnSpawnFailure_S(requestComponent, entity, data, reason))
363  RplComponent.DeleteRplEntity(entity, false);
364  }
365 
366  //------------------------------------------------------------------------------------------------
373  protected bool ShouldDeleteEntityOnSpawnFailure_S(SCR_SpawnRequestComponent requestComponent, IEntity entity, SCR_SpawnData data, SCR_ESpawnResult reason)
374  {
375  return true;
376  }
377 
378  //------------------------------------------------------------------------------------------------
384  protected void HandleEntityChange_S(SCR_SpawnRequestComponent requestComponent, IEntity previousEntity, IEntity newEntity, SCR_SpawnData data)
385  {
386  #ifdef _ENABLE_RESPAWN_LOGS
387  Print(string.Format("%1::HandleEntityChange_S(playerId: %2, previousEntity: %3, newEntity: %4, data: %5)", Type().ToString(),
388  requestComponent.GetPlayerController().GetPlayerId(),
389  previousEntity,
390  newEntity,
391  data), LogLevel.NORMAL);
392  #endif
393 
394  // Notify manager
395  SCR_RespawnSystemComponent respawnSystem = GetRespawnSystemComponent();
396  respawnSystem.OnPlayerEntityChange_S(requestComponent, this, previousEntity, newEntity, data);
397 
398  // No need to resolve
399  if (!previousEntity)
400  return;
401 
403  {
404  DamageManagerComponent damageManager = DamageManagerComponent.Cast(previousEntity.FindComponent(DamageManagerComponent));
405  if (damageManager)
406  damageManager.SetHealthScaled(0.0);
407  }
408 
410  RplComponent.DeleteRplEntity(previousEntity, false);
411  }
412 }
SCR_BaseGameMode
Definition: SCR_BaseGameMode.c:137
SCR_PlayerController
Definition: SCR_PlayerController.c:31
SCR_ESpawnResult
SCR_ESpawnResult
Definition: SCR_ESpawnResult.c:8
m_bDeletePreviousControlledEntity
protected bool m_bDeletePreviousControlledEntity
Definition: SCR_SpawnHandlerComponent.c:28
m_RespawnSystemComponent
SCR_SpawnHandlerComponentClass m_RespawnSystemComponent
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
ShouldDeleteEntityOnSpawnFailure_S
protected bool ShouldDeleteEntityOnSpawnFailure_S(SCR_SpawnRequestComponent requestComponent, IEntity entity, SCR_SpawnData data, SCR_ESpawnResult reason)
Definition: SCR_SpawnHandlerComponent.c:373
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_SpawnHandlerComponentClass
Definition: SCR_SpawnHandlerComponent.c:1
ValidatePrefab_S
protected bool ValidatePrefab_S(ResourceName resourceName)
Definition: SCR_SpawnHandlerComponent.c:271
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
m_bDestroyPreviousControlledEntity
protected bool m_bDestroyPreviousControlledEntity
Definition: SCR_SpawnHandlerComponent.c:25
FinalizeRequest_S
SCR_ESpawnResult FinalizeRequest_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, IEntity entity)
Definition: SCR_SpawnHandlerComponent.c:144
GetPlayerController
proto external PlayerController GetPlayerController()
Definition: SCR_PlayerDeployMenuHandlerComponent.c:307
CanFinalize_S
bool CanFinalize_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, IEntity entity)
Definition: SCR_SpawnHandlerComponent.c:133
GetGameMode
SCR_BaseGameMode GetGameMode()
Definition: SCR_BaseGameModeComponent.c:15
SCR_SpawnData
Definition: SCR_SpawnData.c:9
OnFinalizeBegin_S
void OnFinalizeBegin_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, IEntity entity)
Called when the finalisation process begins.
Definition: SCR_SpawnHandlerComponent.c:121
ValidateData_S
protected bool ValidateData_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data)
Definition: SCR_SpawnHandlerComponent.c:174
HandleRequest_S
SCR_ESpawnResult HandleRequest_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, out IEntity spawnedEntity)
Definition: SCR_SpawnHandlerComponent.c:82
Attribute
typedef Attribute
Post-process effect of scripted camera.
OnFinalizeDone_S
void OnFinalizeDone_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, IEntity entity)
Definition: SCR_PossessSpawnHandlerComponent.c:104
OnPostInit
protected override void OnPostInit(IEntity owner)
Editable Mine.
Definition: SCR_SpawnHandlerComponent.c:33
CanRequestSpawn_S
bool CanRequestSpawn_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data, out SCR_ESpawnResult result)
Definition: SCR_SpawnHandlerComponent.c:190
SCR_BaseContainerTools
Definition: SCR_BaseContainerTools.c:3
AssignEntity_S
protected bool AssignEntity_S(SCR_SpawnRequestComponent requestComponent, IEntity entity, SCR_SpawnData data)
Definition: SCR_SpawnHandlerComponent.c:320
CanHandleRequest_S
SCR_ESpawnResult CanHandleRequest_S(SCR_SpawnRequestComponent requestComponent, SCR_SpawnData data)
Definition: SCR_SpawnHandlerComponent.c:46
PrepareEntity_S
protected bool PrepareEntity_S(SCR_SpawnRequestComponent requestComponent, IEntity entity, SCR_SpawnData data)
Definition: SCR_SpawnHandlerComponent.c:302
HandleSpawnEntityFailure_S
protected void HandleSpawnEntityFailure_S(SCR_SpawnRequestComponent requestComponent, IEntity entity, SCR_SpawnData data, SCR_ESpawnResult reason)
Definition: SCR_SpawnHandlerComponent.c:352
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
DamageManagerComponent
Definition: DamageManagerComponent.c:12
SCR_DebugMenuID
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition: DebugMenuID.c:3
HandleEntityChange_S
protected void HandleEntityChange_S(SCR_SpawnRequestComponent requestComponent, IEntity previousEntity, IEntity newEntity, SCR_SpawnData data)
Definition: SCR_SpawnHandlerComponent.c:384
IsActive
bool IsActive()
Definition: SCR_LoadoutSaveBlackListHolder.c:82
SpawnEntity_S
protected SCR_ESpawnResult SpawnEntity_S(SCR_SpawnRequestComponent requestComponent, notnull SCR_SpawnData data, out IEntity spawnedEntity)
Definition: SCR_SpawnHandlerComponent.c:211
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
GetRespawnSystemComponent
protected SCR_RespawnSystemComponent GetRespawnSystemComponent()
Definition: SCR_SpawnHandlerComponent.c:19