Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_CompositionSlotManagerComponent.c
Go to the documentation of this file.
1 void OnCompositionSlotManagerEntityChangedMethod(SCR_CompositionSlotManagerComponent slotManager, IEntity slot);
3 typedef ScriptInvokerBase<OnCompositionSlotManagerEntityChangedMethod> OnCompsitionSlotManagerEntityChanged;
4 
5 [ComponentEditorProps(category: "GameScripted/Compositions", description: "")]
6 class SCR_CompositionSlotManagerComponentClass : ScriptComponentClass
7 {
8 }
9 
10 class SCR_CompositionSlotManagerComponent : ScriptComponent
11 {
12  protected ref map<EntityID, IEntity> m_aOccupiedStatic = new map<EntityID, IEntity>();
13  protected ref map<RplId, IEntity> m_aOccupiedDynamic = new map<RplId, IEntity>();
14  protected ref array<IEntity> m_aQueriedEntities;
15 
17 
18  //------------------------------------------------------------------------------------------------
21  {
22  if (!m_OnEntityChanged)
24 
25  return m_OnEntityChanged;
26  }
27 
28  //------------------------------------------------------------------------------------------------
30  static SCR_CompositionSlotManagerComponent GetInstance()
31  {
32  if (GetGame().GetGameMode())
33  return SCR_CompositionSlotManagerComponent.Cast(GetGame().GetGameMode().FindComponent(SCR_CompositionSlotManagerComponent));
34  else
35  return null;
36  }
37 
38  //------------------------------------------------------------------------------------------------
41  void SetOccupant(IEntity slot, IEntity occupant)
42  {
43  if (Replication.IsClient())
44  return;
45 
46  RplId rplID = Replication.FindId(slot);
47  if (rplID.IsValid())
48  SetOccupiedDynamic(rplID, occupant != null, occupant);
49  else
50  SetOccupiedStatic(slot.GetID(), occupant != null, occupant);
51  }
52 
53  //------------------------------------------------------------------------------------------------
56  void SetOccupant(vector pos, IEntity occupant)
57  {
58  if (Replication.IsClient())
59  return;
60 
61  m_aQueriedEntities = {};
62  GetOwner().GetWorld().QueryEntitiesBySphere(pos, 1, QueryEntity, null, EQueryEntitiesFlags.STATIC);
63  if (!m_aQueriedEntities.IsEmpty())
64  SetOccupant(m_aQueriedEntities[0], occupant);
65 
66  m_aQueriedEntities = null;
67  }
68 
69  //------------------------------------------------------------------------------------------------
72  IEntity GetOccupant(IEntity slot)
73  {
74  IEntity occupant;
75  RplId slotID = Replication.FindId(slot);
76  if (slotID.IsValid())
77  m_aOccupiedDynamic.Find(slotID, occupant);
78  else
79  m_aOccupiedStatic.Find(slot.GetID(), occupant);
80 
81  return occupant;
82  }
83 
84  //------------------------------------------------------------------------------------------------
88  bool IsOccupied(IEntity slot)
89  {
90  RplId slotID = Replication.FindId(slot);
91  if (slotID.IsValid())
92  return m_aOccupiedDynamic.Contains(slotID);
93  else
94  return m_aOccupiedStatic.Contains(slot.GetID());
95  }
96 
97  //------------------------------------------------------------------------------------------------
100  IEntity GetSlot(IEntity occupant)
101  {
102  for (int i = 0, count = m_aOccupiedStatic.Count(); i < count; i++)
103  {
104  if (m_aOccupiedStatic.GetElement(i) == occupant)
105  return GetGame().GetWorld().FindEntityByID(m_aOccupiedStatic.GetKey(i));
106  }
107 
108  for (int i = 0, count = m_aOccupiedDynamic.Count(); i < count; i++)
109  {
110  if (m_aOccupiedDynamic.GetElement(i) == occupant)
111  return IEntity.Cast(Replication.FindItem(m_aOccupiedDynamic.GetKey(i)));
112  }
113 
114  return null;
115  }
116 
117  //------------------------------------------------------------------------------------------------
120  bool IsInSlot(IEntity occupant)
121  {
122  for (int i = 0, count = m_aOccupiedStatic.Count(); i < count; i++)
123  {
124  if (m_aOccupiedStatic.GetElement(i) == occupant)
125  return true;
126  }
127 
128  for (int i = 0, count = m_aOccupiedDynamic.Count(); i < count; i++)
129  {
130  if (m_aOccupiedDynamic.GetElement(i) == occupant)
131  return true;
132  }
133 
134  return false;
135  }
136 
137  //--- Static
138 
139  //------------------------------------------------------------------------------------------------
140  protected void SetOccupiedStatic(EntityID slotID, bool isOccupied, IEntity occupant)
141  {
142  if (Replication.IsServer() && ModifyArray(slotID, isOccupied, occupant))
143  Rpc(SetOccupiedStaticBroadcast, slotID, isOccupied);
144  }
145 
146  //------------------------------------------------------------------------------------------------
147  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
148  protected void SetOccupiedStaticBroadcast(EntityID slotID, bool isOccupied)
149  {
150  ModifyArray(slotID, isOccupied, null);
151  }
152 
153  //--- Dynamic
154 
155  //------------------------------------------------------------------------------------------------
156  protected void SetOccupiedDynamic(RplId slotID, bool isOccupied, IEntity occupant)
157  {
158  if (Replication.IsServer() && ModifyArray(slotID, isOccupied, occupant))
159  Rpc(SetOccupiedDynamicBroadcast, slotID, isOccupied);
160  }
161 
162  //------------------------------------------------------------------------------------------------
163  [RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
164  protected void SetOccupiedDynamicBroadcast(RplId slotID, bool isOccupied)
165  {
166  ModifyArray(slotID, isOccupied, null);
167  }
168 
169  //--- Arrays
170 
171  //------------------------------------------------------------------------------------------------
172  protected bool ModifyArray(EntityID slotID, bool isOccupied, IEntity occupant)
173  {
174  if (isOccupied)
175  {
176  bool isChange = m_aOccupiedStatic.Insert(slotID, occupant);
177  UpdateSlot(GetGame().GetWorld().FindEntityByID(slotID), isOccupied);
178  return isChange;
179  }
180  else
181  {
182  if (m_aOccupiedStatic.Find(slotID, occupant))
183  {
184  m_aOccupiedStatic.Remove(slotID);
185  UpdateSlot(GetGame().GetWorld().FindEntityByID(slotID), isOccupied);
186  return true;
187  }
188  else
189  {
190  return false;
191  }
192  }
193  }
194 
195  //------------------------------------------------------------------------------------------------
196  protected bool ModifyArray(RplId slotID, bool isOccupied, IEntity occupant)
197  {
198  if (isOccupied)
199  {
200  bool isChange = m_aOccupiedDynamic.Insert(slotID, occupant);
201  UpdateSlot(IEntity.Cast(Replication.FindItem(slotID)), isOccupied);
202  return isChange;
203  }
204  else
205  {
206  if (m_aOccupiedDynamic.Find(slotID, occupant))
207  {
208  m_aOccupiedDynamic.Remove(slotID);
209  UpdateSlot(IEntity.Cast(Replication.FindItem(slotID)), isOccupied);
210  return true;
211  }
212  else
213  {
214  return false;
215  }
216  }
217  }
218 
219  //------------------------------------------------------------------------------------------------
220  protected void UpdateSlot(IEntity slot, bool isOccupied)
221  {
222  if (!slot)
223  return;
224 
226  if (!entity)
227  return;
228 
229  if (entity.GetEntityType(slot) == EEditableEntityType.SLOT)
230  entity.SetVisible(!isOccupied);
231 
233  if (core)
234  core.Event_OnEntityExtendedChange.Invoke(entity, isOccupied);
235 
236  if (m_OnEntityChanged)
237  m_OnEntityChanged.Invoke(this, slot);
238  }
239 
240  //------------------------------------------------------------------------------------------------
242  {
243  IEntity slot = GetSlot(entity.GetOwner());
244  if (!slot)
245  return;
246 
247  //--- Ignore when not deleted and position didn't change (e.g., only rotation did)
248  if (entity.IsRegistered())
249  {
250  vector transform[4];
251  entity.GetTransform(transform);
252  if (vector.Distance(transform[3], slot.GetWorldTransformAxis(3)) < 0.1)
253  {
254  if (m_OnEntityChanged)
255  m_OnEntityChanged.Invoke(this, slot);
256 
257  return;
258  }
259  }
260 
261  SetOccupant(slot, null);
262  if (m_OnEntityChanged)
263  m_OnEntityChanged.Invoke(this, slot);
264  }
265 
266  //------------------------------------------------------------------------------------------------
267  protected bool QueryEntity(IEntity entity)
268  {
269  if (entity.IsInherited(SCR_SiteSlotEntity))
270  {
271  m_aQueriedEntities.Insert(entity);
272  return false;
273  }
274  else
275  {
276  return true;
277  }
278  }
279 
280  //------------------------------------------------------------------------------------------------
281  override bool RplSave(ScriptBitWriter writer)
282  {
283  int occupiedCount = m_aOccupiedStatic.Count();
284  writer.WriteInt(occupiedCount);
285  for (int i = 0; i < occupiedCount; i++)
286  {
287  writer.WriteEntityId(m_aOccupiedStatic.GetKey(i));
288  }
289 
290  occupiedCount = m_aOccupiedDynamic.Count();
291  writer.WriteInt(occupiedCount);
292  for (int i = 0; i < occupiedCount; i++)
293  {
294  writer.WriteRplId(m_aOccupiedDynamic.GetKey(i));
295  }
296 
297  return true;
298  }
299 
300  //------------------------------------------------------------------------------------------------
301  override bool RplLoad(ScriptBitReader reader)
302  {
303  int occupiedCount;
304  reader.ReadInt(occupiedCount);
305 
306  EntityID entityID;
307  m_aOccupiedStatic.Clear();
308  for (int i = 0; i < occupiedCount; i++)
309  {
310  reader.ReadEntityId(entityID);
311  m_aOccupiedStatic.Insert(entityID, null);
312  UpdateSlot(GetGame().GetWorld().FindEntityByID(entityID), true);
313  }
314 
315  RplId rplID;
316  reader.ReadInt(occupiedCount);
317  m_aOccupiedDynamic.Clear();
318  for (int i = 0; i < occupiedCount; i++)
319  {
320  reader.ReadRplId(rplID);
321  m_aOccupiedDynamic.Insert(rplID, null);
322  UpdateSlot(IEntity.Cast(Replication.FindItem(rplID)), true);
323  }
324 
325  return true;
326  }
327 
328  //------------------------------------------------------------------------------------------------
329  override void OnPostInit(IEntity owner)
330  {
332  if (core)
333  {
334  core.Event_OnEntityUnregistered.Insert(OnEntityChanged);
335  core.Event_OnEntityTransformChanged.Insert(OnEntityChanged);
336  }
337  }
338 
339  //------------------------------------------------------------------------------------------------
340  override void OnDelete(IEntity owner)
341  {
343  if (core)
344  {
345  core.Event_OnEntityUnregistered.Remove(OnEntityChanged);
346  core.Event_OnEntityTransformChanged.Remove(OnEntityChanged);
347  }
348  }
349 }
m_aOccupiedDynamic
protected ref map< RplId, IEntity > m_aOccupiedDynamic
Definition: SCR_CompositionSlotManagerComponent.c:13
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
SCR_EditableEntityCore
Definition: SCR_EditableEntityCore.c:10
GetOnEntityChanged
OnCompsitionSlotManagerEntityChanged GetOnEntityChanged()
Definition: SCR_CompositionSlotManagerComponent.c:20
GetOccupant
IEntity GetOccupant(IEntity slot)
Definition: SCR_CompositionSlotManagerComponent.c:72
SetOccupant
void SetOccupant(IEntity slot, IEntity occupant)
Definition: SCR_CompositionSlotManagerComponent.c:41
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
GetInstance
SCR_TextsTaskManagerComponentClass ScriptComponentClass GetInstance()
Definition: SCR_TextsTaskManagerComponent.c:50
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
OnDelete
override void OnDelete(IEntity owner)
Definition: SCR_CompositionSlotManagerComponent.c:340
m_OnEntityChanged
protected ref OnCompsitionSlotManagerEntityChanged m_OnEntityChanged
Definition: SCR_CompositionSlotManagerComponent.c:16
RplLoad
override bool RplLoad(ScriptBitReader reader)
Definition: SCR_CompositionSlotManagerComponent.c:301
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
GetSlot
IEntity GetSlot(IEntity occupant)
Definition: SCR_CompositionSlotManagerComponent.c:100
func
func
Definition: SCR_AIThreatSystem.c:5
GetGameMode
SCR_BaseGameMode GetGameMode()
Definition: SCR_BaseGameModeComponent.c:15
UpdateSlot
protected void UpdateSlot(IEntity slot, bool isOccupied)
Definition: SCR_CompositionSlotManagerComponent.c:220
RplSave
override bool RplSave(ScriptBitWriter writer)
Definition: SCR_CompositionSlotManagerComponent.c:281
OnEntityChanged
protected void OnEntityChanged(SCR_EditableEntityComponent entity)
Definition: SCR_CompositionSlotManagerComponent.c:241
SCR_CompositionSlotManagerComponentClass
Definition: SCR_CompositionSlotManagerComponent.c:6
SetOccupiedDynamicBroadcast
protected void SetOccupiedDynamicBroadcast(RplId slotID, bool isOccupied)
Definition: SCR_CompositionSlotManagerComponent.c:164
QueryEntity
protected bool QueryEntity(IEntity entity)
Definition: SCR_CompositionSlotManagerComponent.c:267
OnCompositionSlotManagerEntityChangedMethod
func OnCompositionSlotManagerEntityChangedMethod
Definition: SCR_CompositionSlotManagerComponent.c:2
EEditableEntityType
EEditableEntityType
Defines type of SCR_EditableEntityComponent. Assigned automatically based on IEntity inheritance.
Definition: EEditableEntityType.c:5
EntityID
SCR_CompositionSlotManagerComponentClass EntityID
OnPostInit
override void OnPostInit(IEntity owner)
Called on PostInit when all components are added.
Definition: SCR_CompositionSlotManagerComponent.c:329
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
ModifyArray
protected bool ModifyArray(EntityID slotID, bool isOccupied, IEntity occupant)
Definition: SCR_CompositionSlotManagerComponent.c:172
SCR_EditableEntityComponent
Definition: SCR_EditableEntityComponent.c:13
IsOccupied
bool IsOccupied(IEntity slot)
Definition: SCR_CompositionSlotManagerComponent.c:88
SCR_SiteSlotEntity
Definition: SCR_SiteSlotEntity.c:26
SetOccupiedStatic
protected void SetOccupiedStatic(EntityID slotID, bool isOccupied, IEntity occupant)
Definition: SCR_CompositionSlotManagerComponent.c:140
SetOccupiedDynamic
protected void SetOccupiedDynamic(RplId slotID, bool isOccupied, IEntity occupant)
Definition: SCR_CompositionSlotManagerComponent.c:156
m_aOccupiedStatic
SCR_CompositionSlotManagerComponentClass m_aOccupiedStatic
SetOccupiedStaticBroadcast
protected void SetOccupiedStaticBroadcast(EntityID slotID, bool isOccupied)
Definition: SCR_CompositionSlotManagerComponent.c:148
IsInSlot
bool IsInSlot(IEntity occupant)
Definition: SCR_CompositionSlotManagerComponent.c:120
OnCompsitionSlotManagerEntityChanged
ScriptInvokerBase< OnCompositionSlotManagerEntityChangedMethod > OnCompsitionSlotManagerEntityChanged
Definition: SCR_CompositionSlotManagerComponent.c:3
m_aQueriedEntities
protected ref array< IEntity > m_aQueriedEntities
Definition: SCR_CompositionSlotManagerComponent.c:14
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180