Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
remotelyactivateditembehaviour.c
Go to the documentation of this file.
2 {
3  protected EntityAI m_Parent;
4  protected bool m_IsTrigger;
5  protected EntityAI m_PairDevice;
6  protected int m_PairDeviceNetIdLow;
7  protected int m_PairDeviceNetIdHigh;
8  protected int m_PersistentPairID = int.MIN;
9 
10  protected static ref map<int,EntityAI> m_RemoteReceivers = new map<int,EntityAI>();
11 
12  void RemotelyActivatedItemBehaviour(notnull EntityAI pParent)
13  {
14  m_Parent = pParent;
15 
16  m_PairDeviceNetIdLow = -1;
17  m_PairDeviceNetIdHigh = -1;
18  }
19 
21  {
22  if (m_Parent)
23  Unpair();
24  }
25 
26  void SetTrigger()
27  {
28  m_IsTrigger = true;
29  }
30 
31  void OnVariableSynchronized()
32  {
33  Pair();
34  }
35 
36  void OnStoreSave(ParamsWriteContext ctx)
37  {
38  ctx.Write(m_PersistentPairID);
39  }
40 
41  bool OnStoreLoad(ParamsReadContext ctx, int version)
42  {
43  if (!ctx.Read(m_PersistentPairID))
44  return false;
45 
46  if (m_PersistentPairID == int.MIN)//default value, no point in going further
47  return true;
48 
49  if (m_IsTrigger)//trigger
50  {
51  EntityAI receiver = m_RemoteReceivers.Get(m_PersistentPairID);
52  if (receiver)
53  Pair(receiver);
54  }
55  else // remotely controlled device
56  {
57  m_RemoteReceivers.Insert(m_PersistentPairID, m_Parent);//receivers will register themselves upon being loaded from the storage
58  }
59 
60  return true;
61  }
62 
63  void OnAfterLoad()
64  {
65  if (m_IsTrigger && m_PersistentPairID != int.MIN && !m_PairDevice)
66  {
67  EntityAI receiver = m_RemoteReceivers.Get(m_PersistentPairID);
68  if (receiver)
69  {
70  //if both the receiver and trigger are somewhere in the world outside of the player inventory, there is no guarantee
71  //that the pairing is going to be succesful during the 'OnStoreLoad' event as that requires the receiver to already be loaded when the trigger is being loaded
72  //therefore, it's necessary to also perform pairing in this event, which happens at the end of the storage loading process
73  //do note that this event is not called when the entity is being loaded as part of the player inventory, therefore, we need to pair both in 'OnStoreLoad' and here to handle both situations
74  //(when the trigger is in the player's inventory, it always loads correctly after the receiver, which is always in the world)
75  Pair(receiver);
76  }
77  }
78  }
79 
80 
81  static int GeneratePersistentID()
82  {
83  int randomID = Math.RandomInt(0, int.MAX);
84  if (m_RemoteReceivers.Contains(randomID))
85  {
86  //it's very unlikely to have a collision here, but lets handle it anyway
87  return GeneratePersistentID();
88  }
89  else
90  return randomID;
91  }
92 
93  void SetPersistentPairID(int id)
94  {
95  m_PersistentPairID = id;
96 
97  if (!m_IsTrigger)
98  m_RemoteReceivers.Insert(id,m_Parent);
99  }
100 
101  void Pair()
102  {
103  EntityAI device = EntityAI.Cast(GetGame().GetObjectByNetworkId(GetPairDeviceNetIdLow(), GetPairDeviceNetIdHigh()));
104  if (device)
105  {
106  Pair(device);
107  }
108  }
109 
110  void Pair(notnull EntityAI device)
111  {
112  m_PairDevice = device;
113  SetPairDeviceNetIds(device);
114 
115  if (device != m_Parent && (!m_Parent.GetPairDevice() || m_Parent.GetPairDevice() != m_PairDevice))
116  m_PairDevice.PairRemote(m_Parent);
117 
118  m_PairDevice.SetSynchDirty();
119  m_Parent.SetSynchDirty();
120  }
121 
122  void Unpair()
123  {
124  m_PairDeviceNetIdLow = -1;
125  m_PairDeviceNetIdHigh = -1;
126 
127  if (m_PairDevice)
128  {
129  m_PairDevice.SetSynchDirty();
130  m_PairDevice = null;
131  }
132 
133  if (m_PersistentPairID != int.MIN)
134  {
135  if (m_RemoteReceivers.Contains(m_PersistentPairID))
136  m_RemoteReceivers.Remove(m_PersistentPairID);
137  }
138 
139  m_PersistentPairID = int.MIN;
140  m_Parent.SetSynchDirty();
141  }
142 
143  EntityAI GetPairDevice()
144  {
145  return m_PairDevice;
146  }
147 
148  bool IsPaired()
149  {
150  return m_PairDevice != null;
151  }
152 
153  void SetPairDeviceNetIds(notnull EntityAI device)
154  {
155  device.GetNetworkID(m_PairDeviceNetIdLow, m_PairDeviceNetIdHigh);
156  }
157 
158  int GetPairDeviceNetIdLow()
159  {
160  return m_PairDeviceNetIdLow;
161  }
162 
163  int GetPairDeviceNetIdHigh()
164  {
165  return m_PairDeviceNetIdHigh;
166  }
167 }
GetGame
proto native CGame GetGame()
RemotelyActivatedItemBehaviour
Definition: remotelyactivateditembehaviour.c:1
m_Parent
protected Widget m_Parent
Definition: sizetochild.c:92
MIN
const int MIN
Definition: enconvert.c:28
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
map
map
Definition: controlsxboxnew.c:3
Math
Definition: enmath.c:6
MAX
const int MAX
Definition: enconvert.c:27
EntityAI
Definition: building.c:5