Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_DestructibleTreesSynchManager.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
2 class SCR_DestructibleTreesSynchManagerClass: ScriptComponentClass
3 {
4 };
5 
7 {
8  [Attribute("0")]
9  protected bool m_bPrintTreeDamage;
10 
11 #ifdef ENABLE_DESTRUCTION
12  static SCR_DestructibleTreesSynchManager instance = null;
13 
14  private IEntity m_OwnerEntity;
15  private BaseSoundComponent m_SoundComponent;
16 
17  private ref array<ref SCR_DestroyedTreesData> m_aDestroyedTreesData = new array<ref SCR_DestroyedTreesData>();
18  private ref array<ref SCR_ActiveTreeData> m_aActiveTreesData = new array<ref SCR_ActiveTreeData>();
19 
20  protected RplComponent m_RplComponent;
21 
22  //*********************//
23  //PUBLIC MEMBER METHODS//
24  //*********************//
25 
26  //------------------------------------------------------------------------------------------------
27  SCR_ActiveTreeData GetActiveTreeData(int index)
28  {
29  if (!m_aActiveTreesData || m_aActiveTreesData.Count() <= index)
30  return null;
31 
32  return m_aActiveTreesData[index];
33  }
34 
35  //------------------------------------------------------------------------------------------------
36  int AddActiveTree()
37  {
38  if (!m_aActiveTreesData)
39  return -3;
40 
41  return m_aActiveTreesData.Insert(new SCR_ActiveTreeData());
42  }
43 
44  //------------------------------------------------------------------------------------------------
45  void SynchronizeTreePartTransform(int treePartIdx, float quat[4], vector position, vector velocity, vector angularVelocity, EntityID treeID)
46  {
47  if (RplSession.Mode() == RplMode.Client)
48  return;
49 
50  int treeIdx = FindTreeIndex(treeID);
51  if (treeIdx != -1)
52  {
53  int treePartArrayIdx = FindTreePartIndex(treeIdx, treePartIdx);
54  if (treePartArrayIdx != -1)
55  {
56  m_aDestroyedTreesData[treeIdx].m_aRotations[treePartArrayIdx*4] = quat[0];
57  m_aDestroyedTreesData[treeIdx].m_aRotations[treePartArrayIdx*4 + 1] = quat[1];
58  m_aDestroyedTreesData[treeIdx].m_aRotations[treePartArrayIdx*4 + 2] = quat[2];
59  m_aDestroyedTreesData[treeIdx].m_aRotations[treePartArrayIdx*4 + 3] = quat[3];
60  m_aDestroyedTreesData[treeIdx].m_aPositions[treePartArrayIdx] = position;
61  }
62  }
63 
65  data.m_iTreePartIndex = treePartIdx;
66  Math3D.QuatCopy(quat, data.m_fQuaternion);
67  data.m_vPosition = position;
68  data.m_vVelocity = velocity;
69  data.m_vAngularVelocity = angularVelocity;
70  data.m_TreeID = treeID;
71 
72  Rpc(RPC_SynchronizeTreePart, data);
73  }
74 
75  //------------------------------------------------------------------------------------------------
76  void SynchronizeSetToBreak(int treePartIdx, vector positionVector, vector impulseVector, EDamageType damageType, EntityID treeID)
77  {
78  if (RplSession.Mode() == RplMode.Client)
79  return;
80 
81  SCR_DestructibleTreeV2 tree = FindTree(treeID);
82 
83  if (!tree)
84  return;
85 
86  int treeIdx = FindTreeIndex(treeID);
87  if (treeIdx != -1)
88  {
89  int treePartArrayIdx = FindTreePartIndex(treeIdx, treePartIdx);
90  if (treePartArrayIdx == -1)
91  {
92  m_aDestroyedTreesData[treeIdx].m_aDestroyedPartsIndexes.Insert(treePartIdx);
93  m_aDestroyedTreesData[treeIdx].m_aPositions.Insert("0 0 0");
94  m_aDestroyedTreesData[treeIdx].m_aRotations.Insert(0);
95  m_aDestroyedTreesData[treeIdx].m_aRotations.Insert(0);
96  m_aDestroyedTreesData[treeIdx].m_aRotations.Insert(0);
97  m_aDestroyedTreesData[treeIdx].m_aRotations.Insert(0);
98  m_aDestroyedTreesData[treeIdx].m_aIsTreePartDynamic.Insert(false);
99  m_aDestroyedTreesData[treeIdx].m_aIsParented.Insert(true);
100  }
101  }
102 
103  //tree.ServerSetToBreak(treePartIdx, positionVector, impulseVector, damageType);
104  Rpc(RPC_SetToBreak, treePartIdx, treeID);
105  }
106 
107  //------------------------------------------------------------------------------------------------
108  void SynchronizeRemoveFromParent(int treePartIdx, EntityID treeID)
109  {
110  if (RplSession.Mode() == RplMode.Client)
111  return;
112 
113  int treeIdx = FindTreeIndex(treeID);
114  if (treeIdx != -1)
115  {
116  int treePartArrayIdx = FindTreePartIndex(treeIdx, treePartIdx);
117  if (treePartArrayIdx != -1)
118  {
119  m_aDestroyedTreesData[treeIdx].m_aIsParented[treePartArrayIdx] = false;
120  }
121  }
122 
123  Rpc(RPC_RemoveFromParent, treePartIdx, treeID);
124  }
125 
126  //------------------------------------------------------------------------------------------------
127  void SynchronizeSetToDestroy(EntityID treeID)
128  {
129  if (RplSession.Mode() == RplMode.Client)
130  return;
131 
132  SCR_DestructibleTreeV2 tree = FindTree(treeID);
133 
134  if (!tree)
135  return;
136 
137  int treeIdx = FindTreeIndex(treeID);
138  if (treeIdx == -1)
139  {
141  data.treeID = treeID;
142  m_aDestroyedTreesData.Insert(data);
143  }
144 
145  Rpc(RPC_SendSetToDestroy, treeID);
146  }
147 
148  //------------------------------------------------------------------------------------------------
149  void SynchronizeSwitchTreePartToStatic(int treePartIdx, EntityID treeID)
150  {
151  if (IsProxy())
152  return;
153 
154  int treeIdx = FindTreeIndex(treeID);
155  if (treeIdx != -1)
156  {
157  int treePartArrayIdx = FindTreePartIndex(treeIdx, treePartIdx);
158  if (treePartArrayIdx != -1)
159  {
160  m_aDestroyedTreesData[treeIdx].m_aIsTreePartDynamic[treePartArrayIdx] = false;
161  }
162  }
163 
164  Rpc(RPC_SendSwitchTreePartToStatic, treePartIdx, treeID);
165  }
166 
167  //------------------------------------------------------------------------------------------------
168  void SynchronizeStaticPosition(int treePartIdx, vector position, EntityID treeID)
169  {
170  if (RplSession.Mode() == RplMode.Client)
171  return;
172 
173  int treeIdx = FindTreeIndex(treeID);
174  if (treeIdx != -1)
175  {
176  int treePartArrayIdx = FindTreePartIndex(treeIdx, treePartIdx);
177  if (treePartArrayIdx != -1)
178  {
179  m_aDestroyedTreesData[treeIdx].m_aPositions[treePartArrayIdx] = position;
180  }
181  }
182 
183  Rpc(RPC_SendStaticPosition, treePartIdx, position , treeID);
184  }
185 
186  //------------------------------------------------------------------------------------------------
187  void SynchronizeStaticRotation(int treePartIdx, float quat[4], EntityID treeID)
188  {
189  if (RplSession.Mode() == RplMode.Client)
190  return;
191 
192  int treeIdx = FindTreeIndex(treeID);
193  if (treeIdx != -1)
194  {
195  int treePartArrayIdx = FindTreePartIndex(treeIdx, treePartIdx);
196  if (treePartArrayIdx != -1)
197  {
198  m_aDestroyedTreesData[treeIdx].m_aRotations[treePartArrayIdx*4] = quat[0];
199  m_aDestroyedTreesData[treeIdx].m_aRotations[treePartArrayIdx*4 + 1] = quat[1];
200  m_aDestroyedTreesData[treeIdx].m_aRotations[treePartArrayIdx*4 + 2] = quat[2];
201  m_aDestroyedTreesData[treeIdx].m_aRotations[treePartArrayIdx*4 + 3] = quat[3];
202  }
203  }
204 
205  Rpc(RPC_SendStaticRotation, treePartIdx, quat[0], quat[1], quat[2], quat[3], treeID);
206  }
207 
208  //------------------------------------------------------------------------------------------------
209  SCR_DestructibleTreeV2 FindTree(EntityID treeID)
210  {
211  IEntity treeEnt = m_OwnerEntity.GetWorld().FindEntityByID(treeID);
212  if (treeEnt)
213  {
214  SCR_DestructibleTreeV2 tree = SCR_DestructibleTreeV2.Cast(treeEnt);
215  return tree;
216  }
217  return null;
218  }
219 
220  //------------------------------------------------------------------------------------------------
221  int FindTreeIndex(EntityID treeID)
222  {
223  if (!treeID)
224  {
225 // Print("Tree ID is null!");
226  return -1;
227  }
228 
229  int count = m_aDestroyedTreesData.Count();
230  for (int i = 0; i < count; i++)
231  {
232  if (m_aDestroyedTreesData[i].treeID == treeID)
233  return i;
234  }
235 
236  return -1;
237  }
238 
239  //------------------------------------------------------------------------------------------------
240  int FindTreePartIndex(int treeIdx, int treePartIdx)
241  {
242  int count = m_aDestroyedTreesData[treeIdx].m_aDestroyedPartsIndexes.Count();
243 
244  for (int i = 0; i < count; i++)
245  {
246  if (m_aDestroyedTreesData[treeIdx].m_aDestroyedPartsIndexes[i] == treePartIdx)
247  return i;
248  }
249 
250  return -1;
251  }
252 
253  //------------------------------------------------------------------------------------------------
254  BaseSoundComponent GetSoundComponent()
255  {
256  return m_SoundComponent;
257  }
258 
259  //****************************//
260  //PROTECTED MEMBER RPC METHODS//
261  //****************************//
262 
263  //------------------------------------------------------------------------------------------------
265  protected bool IsProxy()
266  {
267  return (m_RplComponent && m_RplComponent.IsProxy());
268  }
269 
270  //------------------------------------------------------------------------------------------------
271  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
272  protected void RPC_RemoveFromParent(int treePartIndex, EntityID treeID)
273  {
274  SCR_DestructibleTreeV2 tree = FindTree(treeID);
275 
276  if (!tree)
277  return;
278 
279  tree.RemoveTreePartFromParent(treePartIndex);
280  }
281 
282  //------------------------------------------------------------------------------------------------
283  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
284  private void RPC_SynchronizeTreePart(SCR_TreePartSynchronizationData data)
285  {
286 // Print("------DATA RECEIVED------");
287  SCR_DestructibleTreeV2 tree = FindTree(data.m_TreeID);
288  if (!tree)
289  return;
290 
291  tree.UpdateTransformOfTreePart(data.m_iTreePartIndex, data.m_vPosition, data.m_fQuaternion, data.m_vVelocity, data.m_vAngularVelocity);
292  }
293 
294  //------------------------------------------------------------------------------------------------
295  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
296  private void RPC_SetToBreak(int treePartIdx, EntityID treeID)
297  {
298  SCR_DestructibleTreeV2 tree = FindTree(treeID);
299 
300  if (!tree)
301  return;
302 
303  tree.SetToBreak(treePartIdx);
304  }
305 
306  //------------------------------------------------------------------------------------------------
307  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
308  private void RPC_SendSetToDestroy(EntityID treeID)
309  {
310  SCR_DestructibleTreeV2 tree = FindTree(treeID);
311 // Print("Client set to destroy " + treeID);
312 
313  if (!tree)
314  {
315 // Print("Tree not found");
316  return;
317  }
318 
319 // Print(treeID);
320  tree.SetToDestroy();
321  }
322 
323  //------------------------------------------------------------------------------------------------
324  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
325  void RPC_SendSwitchTreePartToStatic(int treePartIdx, EntityID treeID)
326  {
327  SCR_DestructibleTreeV2 tree = FindTree(treeID);
328 
329  if (!tree)
330  return;
331 
332  tree.ClientSwitchTreePartToStatic(treePartIdx);
333  }
334 
335  //------------------------------------------------------------------------------------------------
336  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
337  void RPC_SendStaticPosition(int treePartIdx, vector position, EntityID treeID)
338  {
339  SCR_DestructibleTreeV2 tree = FindTree(treeID);
340 
341  if (!tree)
342  return;
343 
344  tree.SetPositionOfTreePart(treePartIdx, position);
345  }
346 
347  //------------------------------------------------------------------------------------------------
348  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
349  void RPC_SendStaticRotation(int treePartIdx, float q0, float q1, float q2, float q3, EntityID treeID)
350  {
351  float quat[4];
352  quat[0] = q0;
353  quat[1] = q1;
354  quat[2] = q2;
355  quat[3] = q3;
356 
357  SCR_DestructibleTreeV2 tree = FindTree(treeID);
358 
359  if (!tree)
360  return;
361 
362  tree.SetRotationOfTreePart(treePartIdx, quat);
363  }
364 
365  //***********************//
366  //OVERRIDE MEMBER METHODS//
367  //***********************//
368 
369  //------------------------------------------------------------------------------------------------
370  override bool RplLoad(ScriptBitReader reader)
371  {
372  int count;
373  reader.ReadInt(count);
374 // Print(count);
375 
376  for (int i = 0; i < count; i++)
377  {
378  EntityID treeID;
379  reader.ReadEntityId(treeID);
380  SCR_DestructibleTreeV2 tree = FindTree(treeID);
381 
382 // Print(treeID);
383  int treePartsCount;
384  reader.ReadInt(treePartsCount);
385 
386  if (treePartsCount == 0)
387  continue;
388 
389  tree.SetToDestroy();
390 
391 // Print(treePartsCount);
392 
393  for (int x = 0; x < treePartsCount; x++)
394  {
395  int treePartIdx;
396  reader.ReadInt(treePartIdx);
397 // Print(treePartIdx);
398 
399  vector position;
400  reader.ReadVector(position);
401 // Print(position);
402 
403  float rotation[4];
404  reader.ReadQuaternion(rotation);
405 // Print(rotation);
406 
407  bool isDynamic;
408  reader.ReadBool(isDynamic);
409 // Print(isDynamic);
410 
411  bool isParented;
412  reader.ReadBool(isParented);
413 // Print(isParented);
414 
415  tree.CacheDestroyedTreePart(treePartIdx, position, rotation, isDynamic, isParented);
416  }
417  }
418 
419  return true;
420  }
421 
422  //------------------------------------------------------------------------------------------------
423  override bool RplSave(ScriptBitWriter writer)
424  {
425  int count = m_aDestroyedTreesData.Count();
426  writer.WriteInt(count);
427 // Print(count);
428 
429  for (int i = 0; i < count; i++)
430  {
431  writer.WriteEntityId(m_aDestroyedTreesData[i].treeID);
432 // Print(m_aDestroyedTreesData[i].treeID);
433 
434  int treePartsCount = m_aDestroyedTreesData[i].m_aDestroyedPartsIndexes.Count();
435  writer.WriteInt(treePartsCount);
436 // Print(treePartsCount);
437 
438  for (int x = 0; x < treePartsCount; x++)
439  {
440  writer.WriteInt(m_aDestroyedTreesData[i].m_aDestroyedPartsIndexes[x]);
441 // Print(m_aDestroyedTreesData[i].destroyedTreeParts[x]);
442 
443  writer.WriteVector(m_aDestroyedTreesData[i].m_aPositions[x]);
444 // Print(m_aDestroyedTreesData[i].positions[x][0]);
445 // Print(m_aDestroyedTreesData[i].positions[x][1]);
446 // Print(m_aDestroyedTreesData[i].positions[x][2]);
447 
448  writer.WriteQuaternion(m_aDestroyedTreesData[i].m_aRotations[x*4]);
449 // Print(m_aDestroyedTreesData[i].rotations[x*4]);
450 // Print(m_aDestroyedTreesData[i].rotations[x*4 + 1]);
451 // Print(m_aDestroyedTreesData[i].rotations[x*4 + 2]);
452 // Print(m_aDestroyedTreesData[i].rotations[x*4 + 3]);
453 
454  writer.WriteBool(m_aDestroyedTreesData[i].m_aIsTreePartDynamic[x]);
455 // Print(m_aDestroyedTreesData[i].isTreePartDynamic[x]);
456 
457  writer.WriteBool(m_aDestroyedTreesData[i].m_aIsParented[x]);
458 // Print(m_aDestroyedTreesData[i].isParented[x]);
459  }
460  }
461 
462  return true;
463  }
464 
465  //------------------------------------------------------------------------------------------------
466  override void OnPostInit(IEntity owner)
467  {
468  super.OnPostInit(owner);
469  SetEventMask(owner, EntityEvent.INIT);
470  }
471 
472  //------------------------------------------------------------------------------------------------
473  override void EOnInit(IEntity owner)
474  {
475  if (!GetGame() || !GetGame().InPlayMode())
476  return;
477 
478  m_RplComponent = RplComponent.Cast(owner.FindComponent(RplComponent));
479 
480  SCR_DestructibleTreeV2.s_bPrintTreeDamage = m_bPrintTreeDamage;
481 
482  if (!instance)
483  {
484  instance = this;
485  SCR_DestructibleTreeV2.synchManager = instance;
486  SCR_TreePartV2.synchManager = instance;
487  }
488 
489  m_OwnerEntity = owner;
490 
491  m_SoundComponent = BaseSoundComponent.Cast(owner.FindComponent(BaseSoundComponent));
492  }
493 
494  //************************//
495  //CONSTRUCTOR & DESTRUCTOR//
496  //************************//
497 
498  //------------------------------------------------------------------------------------------------
499  void SCR_DestructibleTreesSynchManager(IEntityComponentSource src, IEntity ent, IEntity parent)
500  {
501  }
502 
503  //------------------------------------------------------------------------------------------------
505  {
506  m_aDestroyedTreesData.Clear();
507  m_aDestroyedTreesData = null;
508  }
509 #endif
510 };
SCR_DestructibleTreesSynchManagerClass
Definition: SCR_DestructibleTreesSynchManager.c:2
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
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
RplLoad
override bool RplLoad(ScriptBitReader reader)
Definition: SCR_AIGroupInfoComponent.c:106
RplSave
override bool RplSave(ScriptBitWriter writer)
Definition: SCR_CampaignBuildingCompositionComponent.c:490
IsProxy
protected bool IsProxy()
Definition: SCR_CampaignBuildingCompositionComponent.c:456
SCR_TreePartV2
Definition: SCR_TreePartV2.c:8
Attribute
typedef Attribute
Post-process effect of scripted camera.
EntityID
SCR_CompositionSlotManagerComponentClass EntityID
SCR_DestructibleTreeV2
Encapsulates the functionality of a destructible tree entity in the world.
Definition: SCR_DestructibleTreeV2.c:55
SCR_DestroyedTreesData
Definition: DestroyedTreesData.c:1
rotation
RespawnSystemComponentClass GameComponentClass vector vector rotation
Definition: RespawnSystemComponent.c:23
SCR_DestructibleTreesSynchManager
Definition: SCR_DestructibleTreesSynchManager.c:6
OnPostInit
override void OnPostInit(IEntity owner)
Called on PostInit when all components are added.
Definition: SCR_AIConfigComponent.c:72
m_SoundComponent
protected SoundComponent m_SoundComponent
Definition: SCR_RotorDamageManagerComponent.c:9
EOnInit
override void EOnInit(IEntity owner)
Definition: SCR_AIConfigComponent.c:79
m_RplComponent
protected RplComponent m_RplComponent
Definition: SCR_CampaignBuildingManagerComponent.c:42
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
EDamageType
EDamageType
Definition: EDamageType.c:12
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305
SCR_TreePartSynchronizationData
Definition: TreePartSynchronizationData.c:1
position
vector position
Definition: SCR_DestructibleTreeV2.c:30
m_OwnerEntity
SCR_AIUtilityComponentClass m_OwnerEntity