Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_MPDestructionManager.c
Go to the documentation of this file.
1 #define ENABLE_BASE_DESTRUCTION
2 //------------------------------------------------------------------------------------------------
3 [EntityEditorProps(category: "GameScripted/Destruction", description: "Manager for destructibles", color: "0 0 255 255")]
4 class SCR_MPDestructionManagerClass: GenericEntityClass
5 {
6 };
7 
8 //------------------------------------------------------------------------------------------------
10 {
11  protected EntityID m_EntityID = EntityID.INVALID;
12  protected RplId m_RplId = RplId.Invalid();
13  protected int m_iIndex = -1;
14 
15  //------------------------------------------------------------------------------------------------
16  void SetIndex(int index)
17  {
18  m_iIndex = index;
19  }
20 
21  //------------------------------------------------------------------------------------------------
22  void SetRplId(RplId rplId)
23  {
24  m_RplId = rplId;
25  }
26 
27  //------------------------------------------------------------------------------------------------
28  void SetEntityID(EntityID entityID)
29  {
30  m_EntityID = entityID;
31  }
32 
33  //------------------------------------------------------------------------------------------------
34  RplId GetRplId(out int index)
35  {
36  index = m_iIndex;
37  return m_RplId;
38  }
39 
40  //------------------------------------------------------------------------------------------------
41  IEntity GetEntity()
42  {
43  if (m_EntityID != EntityID.INVALID)
44  {
45  BaseWorld world = GetGame().GetWorld();
46  if (world)
47  return world.FindEntityByID(m_EntityID);
48  }
49 
50  return null;
51  }
52 
53  //################################################################################################
55  //------------------------------------------------------------------------------------------------
56  static void Encode(SSnapSerializerBase snapshot, ScriptCtx ctx, ScriptBitSerializer packet)
57  {
58  snapshot.Serialize(packet, 16);
59  }
60 
61  //------------------------------------------------------------------------------------------------
62  static bool Decode(ScriptBitSerializer packet, ScriptCtx ctx, SSnapSerializerBase snapshot)
63  {
64  return snapshot.Serialize(packet, 16);
65  }
66 
67  //------------------------------------------------------------------------------------------------
68  static bool SnapCompare(SSnapSerializerBase lhs, SSnapSerializerBase rhs, ScriptCtx ctx)
69  {
70  return lhs.CompareSnapshots(rhs, 16);
71  }
72 
73  //------------------------------------------------------------------------------------------------
74  static bool PropCompare(SCR_DestructibleIdentificator prop, SSnapSerializerBase snapshot, ScriptCtx ctx)
75  {
76  return snapshot.Compare(prop.m_EntityID, 8)
77  && snapshot.Compare(prop.m_RplId, 4)
78  && snapshot.Compare(prop.m_iIndex, 4);
79  }
80 
81  //------------------------------------------------------------------------------------------------
82  static bool Extract(SCR_DestructibleIdentificator prop, ScriptCtx ctx, SSnapSerializerBase snapshot)
83  {
84  snapshot.SerializeBytes(prop.m_EntityID, 8);
85  snapshot.SerializeBytes(prop.m_RplId, 4);
86  snapshot.SerializeBytes(prop.m_iIndex, 4);
87 
88  return true;
89  }
90 
91  //------------------------------------------------------------------------------------------------
92  static bool Inject(SSnapSerializerBase snapshot, ScriptCtx ctx, SCR_DestructibleIdentificator prop)
93  {
94  snapshot.SerializeBytes(prop.m_EntityID, 8);
95  snapshot.SerializeBytes(prop.m_RplId, 4);
96  snapshot.SerializeBytes(prop.m_iIndex, 4);
97 
98  return true;
99  }
100 };
101 
102 //------------------------------------------------------------------------------------------------
104 {
105 #ifdef ENABLE_BASE_DESTRUCTION
106  [Attribute("", UIWidgets.Auto, desc: "AudioSourceConfiguration shared by all MPDDestruction sounds")]
107  ref SCR_AudioSourceConfiguration m_AudioSourceConfiguration;
108 
109  static const string ENTITY_SIZE_SIGNAL_NAME = "EntitySize";
110  static const string PHASES_TO_DESTROYED_PHASE_SIGNAL_NAME = "PhasesToDestroyed";
111  static const string COLLISIONDV_SIGNAL_NAME = "CollisionDV";
112  static const ResourceName DESTRUCTION_MANAGER_PREFAB = "{9BB369F2803C6F71}Prefabs/MP/MPDestructionManager.et";
113  static private SCR_MPDestructionManager s_Instance = null;
114  static bool s_bInitialized = false;
115 
116  protected ref array<EntityID> m_ChangedDestructibles = new array<EntityID>();
117  protected ref array<EntityID> m_DeletedDestructibles = new array<EntityID>();
118  protected ref map<RplId, ref array<SCR_DestructionDamageManagerComponent>> m_mDynamicallySpawnedDestructibles = new map<RplId, ref array<SCR_DestructionDamageManagerComponent>>();
119  protected ref map<RplId, ref array<int>> m_mChangedDynamicallySpawnedDestructibles = new map<RplId, ref array<int>>();
120  protected ref map<RplId, ref array<int>> m_mDeletedDynamicallySpawnedDestructibles = new map<RplId, ref array<int>>();
121  protected ref array<SCR_DestructionBaseHandler> m_aDestroyInFrame = {};
122  protected RplComponent m_RplComponent;
123 
124  //------------------------------------------------------------------------------------------------
125  static bool IsInitialized()
126  {
127  return s_bInitialized;
128  }
129 
130  //------------------------------------------------------------------------------------------------
131  static notnull SCR_MPDestructionManager InitializeDestructionManager()
132  {
133  if (!s_Instance)
134  {
135  Resource resource = Resource.Load(DESTRUCTION_MANAGER_PREFAB);
136  if (!resource)
137  {
138  Print("SCR_MPDestructionManager::CreateInstance(): Failed to create instance! Destruction of objects will not be synched!", LogLevel.ERROR);
140  }
141 
142  GetGame().SpawnEntityPrefab(resource);
143  }
144 
145  s_bInitialized = true;
146  return s_Instance;
147  }
148 
149  //------------------------------------------------------------------------------------------------
151  static SCR_MPDestructionManager GetInstance()
152  {
153  return s_Instance;
154  }
155 
156  //------------------------------------------------------------------------------------------------
158  bool IsProxy()
159  {
160  return (m_RplComponent && m_RplComponent.IsProxy());
161  }
162 
163  //------------------------------------------------------------------------------------------------
164  protected void DestroyDelayed()
165  {
166  for (int i = m_aDestroyInFrame.Count() - 1; i >= 0; i--)
167  {
168  SCR_DestructionBaseHandler handler = m_aDestroyInFrame[i];
169  if (handler)
170  handler.HandleDestruction();
171 
172  m_aDestroyInFrame.Remove(i);
173  }
174  }
175 
176  //------------------------------------------------------------------------------------------------
177  void DestroyInFrame(SCR_DestructionBaseHandler handler)
178  {
179  m_aDestroyInFrame.Insert(handler);
180  SetEventMask(EntityEvent.FRAME);
181  }
182 
183  //------------------------------------------------------------------------------------------------
185  override bool RplLoad(ScriptBitReader reader)
186  {
187  SCR_DestructionDamageManagerComponent.SetReadingInit(true);
188 
189  // First read IDs for deleted destructibles on the server and delete them locally
190  int numDeleted;
191  reader.ReadInt(numDeleted); // Read num deleted
192  for (int i = 0; i < numDeleted; i++)
193  {
194  EntityID entID;
195  reader.ReadEntityId(entID); // Read entity ID of each deleted destructible
196 
197  IEntity entity = GetWorld().FindEntityByID(entID);
198  if (!entity)
199  continue;
200 
201  SCR_DestructionDamageManagerComponent destructible = SCR_DestructionDamageManagerComponent.Cast(entity.FindComponent(SCR_DestructionDamageManagerComponent));
202  if (!destructible)
203  continue;
204 
205  destructible.DeleteDestructible();
206  }
207 
208  // Now pass data from server destructibles to local ones
209  int numDestructibles;
210  reader.ReadInt(numDestructibles); // Read num destructibles sent
211  for (int i = 0; i < numDestructibles; i++)
212  {
213  EntityID entID;
214  reader.ReadEntityId(entID); // Read entity ID of each destructible to be read
215 
216  IEntity entity = GetWorld().FindEntityByID(entID);
217  if (!entity)
218  continue;
219 
220  SCR_DestructionDamageManagerComponent destructible = SCR_DestructionDamageManagerComponent.Cast(entity.FindComponent(SCR_DestructionDamageManagerComponent));
221  if (!destructible)
222  continue;
223 
224  destructible.NetReadInit(reader);
225  }
226 
227  SCR_DestructionDamageManagerComponent.SetReadingInit(false);
228  return true;
229  }
230 
231  //------------------------------------------------------------------------------------------------
233  override bool RplSave(ScriptBitWriter writer)
234  {
235  // First send all the destructibles that have been deleted
236  int numDeleted = m_DeletedDestructibles.Count();
237  writer.WriteInt(numDeleted); // Write num deleted
238  for (int i = 0; i < numDeleted; i++)
239  {
240  writer.WriteEntityId(m_DeletedDestructibles[i]); // Write entity ID of each deleted destructible
241  }
242 
243  // Send destructibles' data
244  int numChanged = m_ChangedDestructibles.Count();
245  writer.WriteInt(numChanged); // Write num destructibles
246  for (int i = 0; i < numChanged; i++)
247  {
248  EntityID entID = m_ChangedDestructibles[i];
249  IEntity entity = GetWorld().FindEntityByID(entID);
250  if (!entity)
251  continue;
252 
253  SCR_DestructionDamageManagerComponent destructible = SCR_DestructionDamageManagerComponent.Cast(entity.FindComponent(SCR_DestructionDamageManagerComponent));
254  if (!destructible)
255  continue;
256 
257  writer.WriteEntityId(entID); // Write entity ID of each destructible to be sent
258  destructible.NetWriteInit(writer); // Tell destructible to write its data
259  }
260 
261  return true;
262  }
263 
264  //------------------------------------------------------------------------------------------------
265  SCR_AudioSourceConfiguration GetAudioSourceConfiguration()
266  {
268  }
269 
270  //------------------------------------------------------------------------------------------------
271  protected override void EOnFrame(IEntity owner, float timeSlice)
272  {
273  ClearEventMask(EntityEvent.FRAME);
274  DestroyDelayed();
275  }
276 
277  //------------------------------------------------------------------------------------------------
278  override void EOnInit(IEntity owner)
279  {
280  m_RplComponent = RplComponent.Cast(FindComponent(RplComponent));
281  }
282 
283  //------------------------------------------------------------------------------------------------
284  int RegisterDynamicallySpawnedDestructible(notnull SCR_DestructionDamageManagerComponent destructible, RplId rplId)
285  {
286  array<SCR_DestructionDamageManagerComponent> destructibles = m_mDynamicallySpawnedDestructibles.Get(rplId);
287  if (!destructibles)
288  {
289  destructibles = new array<SCR_DestructionDamageManagerComponent>();
290  m_mDynamicallySpawnedDestructibles.Insert(rplId, destructibles);
291  }
292 
293  return destructibles.Insert(destructible);
294  }
295 
296  //------------------------------------------------------------------------------------------------
297  SCR_DestructionDamageManagerComponent FindDynamicallySpawnedDestructibleByIndex(RplId rplId, int index)
298  {
299  array<SCR_DestructionDamageManagerComponent> destructibles = m_mDynamicallySpawnedDestructibles.Get(rplId);
300  if (!destructibles)
301  return null;
302 
303  if (index >= 0 && index < destructibles.Count())
304  return destructibles[index];
305 
306  return null;
307  }
308 
309  //------------------------------------------------------------------------------------------------
310  int FindDynamicallySpawnedDestructibleIndex(RplId rplId, SCR_DestructionDamageManagerComponent destructible)
311  {
312  array<SCR_DestructionDamageManagerComponent> destructibles = m_mDynamicallySpawnedDestructibles.Get(rplId);
313  if (!destructibles)
314  return -1;
315 
316  return destructibles.Find(destructible);
317  }
318 
319  //------------------------------------------------------------------------------------------------
320  void RegisterDeletedDynamicallySpawnedDestructible(notnull SCR_DestructionDamageManagerComponent destructible, RplId rplId)
321  {
322  int index = FindDynamicallySpawnedDestructibleIndex(rplId, destructible);
323  if (index != -1)
324  {
325  array<int> deletedDestructibles = m_mDeletedDynamicallySpawnedDestructibles.Get(rplId);
326  if (!deletedDestructibles)
327  {
328  deletedDestructibles = new array<int>();
329  m_mDeletedDynamicallySpawnedDestructibles.Insert(rplId, deletedDestructibles);
330  }
331 
332  deletedDestructibles.Insert(index);
333  }
334  }
335 
336  //------------------------------------------------------------------------------------------------
337  void RegisterChangedDynamicallySpawnedDestructible(notnull SCR_DestructionDamageManagerComponent destructible, RplId rplId)
338  {
339  int index = FindDynamicallySpawnedDestructibleIndex(rplId, destructible);
340  if (index != -1)
341  {
342  array<int> changedDestructibles = m_mChangedDynamicallySpawnedDestructibles.Get(rplId);
343  if (!changedDestructibles)
344  {
345  changedDestructibles = new array<int>();
346  m_mChangedDynamicallySpawnedDestructibles.Insert(rplId, changedDestructibles);
347  }
348 
349  changedDestructibles.Insert(index);
350  }
351  }
352 
353  //------------------------------------------------------------------------------------------------
354  void SCR_MPDestructionManager(IEntitySource src, IEntity parent)
355  {
356  s_Instance = this;
357 
358  SetEventMask(EntityEvent.INIT);
359 
360  #ifdef ENABLE_DIAG
361  DiagMenu.RegisterBool(SCR_DebugMenuID.DEBUGUI_SOUNDS_MPDESTRUCTION_SHOW_IMPULSEVALUES, "", "Show MPD Impulse Values", "Sounds");
362  #endif
363  }
364 
365  //------------------------------------------------------------------------------------------------
367  {
368  #ifdef ENABLE_DIAG
369  DiagMenu.Unregister(SCR_DebugMenuID.DEBUGUI_SOUNDS_MPDESTRUCTION_SHOW_IMPULSEVALUES);
370  #endif
371  }
372 #endif
373 };
SpawnEntity
protected IEntity SpawnEntity(ResourceName entityResourceName, notnull IEntity slotOwner)
Definition: SCR_CatalogEntitySpawnerComponent.c:1008
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
s_Instance
SCR_SpawnerSlotManagerClass s_Instance
Class used for managing changes and removals of slots present in world.
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_MPDestructionManager
Definition: SCR_MPDestructionManager.c:103
SCR_DestructionBaseHandler
Definition: SCR_DestructionBaseHandler.c:7
EntityID
SCR_CompositionSlotManagerComponentClass EntityID
destructible
SCR_DestructionDamageManagerComponent destructible
Definition: SCR_DestructionSynchronizationComponent.c:22
SCR_MPDestructionManagerClass
Definition: SCR_MPDestructionManager.c:4
m_RplComponent
protected RplComponent m_RplComponent
Definition: SCR_CampaignBuildingManagerComponent.c:42
m_iIndex
private int m_iIndex
Definition: SCR_StressTestGroupActivation.c:11
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
m_AudioSourceConfiguration
ref SCR_AudioSourceConfiguration m_AudioSourceConfiguration
Definition: SCR_ParticleContactComponent.c:3
SCR_DestructibleIdentificator
Definition: SCR_MPDestructionManager.c:9
SCR_DebugMenuID
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition: DebugMenuID.c:3
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180