Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_CompartmentAccessComponent.c
Go to the documentation of this file.
1 void OnPlayerEnterCompartmentMethod(ChimeraCharacter playerCharacter, IEntity compartmentEntity);
3 typedef ScriptInvokerBase<OnPlayerEnterCompartmentMethod> OnPlayerEnterCompartment;
4 
5 void OnPlayerExitCompartmentMethod(ChimeraCharacter playerCharacter, IEntity compartmentEntity);
7 typedef ScriptInvokerBase<OnPlayerEnterCompartmentMethod> OnPlayerExitCompartment;
8 
9 [EntityEditorProps(category: "GameScripted/Vehicle", description: "CompartmentAccessComponent", color: "0 0 255 255")]
11 {
12 };
13 
14 //------------------------------------------------------------------------------------------------
15 class SCR_CompartmentAccessComponent : CompartmentAccessComponent
16 {
18  private ref ScriptInvoker m_OnCompartmentEntered;
19  private ref ScriptInvoker m_OnCompartmentLeft;
20 
21  protected ref OnPlayerEnterCompartment m_OnPlayerEnterCompartment;
22  protected ref OnPlayerExitCompartment m_OnPlayerExitCompartment;
23 
24  protected const int WAIT_FOR_VEHICLE_MAX_TRIES = 10;
25  protected static int s_iWaitForVehicleTries = 0;
26 
27  //------------------------------------------------------------------------------------------------
28  ScriptInvoker GetOnCompartmentEntered(bool createNew = true)
29  {
30  if (!m_OnCompartmentEntered && createNew)
31  m_OnCompartmentEntered = new ScriptInvoker();
32  return m_OnCompartmentEntered;
33  }
34 
35  //------------------------------------------------------------------------------------------------
36  ScriptInvoker GetOnCompartmentLeft(bool createNew = true)
37  {
38  if (!m_OnCompartmentLeft && createNew)
39  m_OnCompartmentLeft = new ScriptInvoker();
40  return m_OnCompartmentLeft;
41  }
42 
43  //------------------------------------------------------------------------------------------------
44  OnPlayerEnterCompartment GetOnPlayerCompartmentEnter(bool createNew = true)
45  {
46  if (!m_OnPlayerEnterCompartment && createNew)
47  m_OnPlayerEnterCompartment = new OnPlayerEnterCompartment();
48 
49  return m_OnPlayerEnterCompartment;
50  }
51 
52  //------------------------------------------------------------------------------------------------
53  OnPlayerExitCompartment GetOnPlayerCompartmentExit(bool createNew = true)
54  {
55  if (!m_OnPlayerExitCompartment && createNew)
56  m_OnPlayerExitCompartment = new OnPlayerExitCompartment();
57 
58  return m_OnPlayerExitCompartment;
59  }
60 
61  //------------------------------------------------------------------------------------------------
62  override void OnCompartmentEntered(IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
63  {
64  if (m_OnCompartmentEntered)
65  m_OnCompartmentEntered.Invoke( targetEntity, manager, mgrID, slotID, move );
66 
67  PlayerManager playerManager = GetGame().GetPlayerManager();
68  ChimeraCharacter character = ChimeraCharacter.Cast(GetOwner());
69 
70  if (!playerManager || !character)
71  return;
72 
73  SCR_CharacterControllerComponent controller = SCR_CharacterControllerComponent.Cast(character.GetCharacterController());
74  if (controller)
75  controller.m_OnLifeStateChanged.Insert(OnLifeStateChanged);
76 
77  //--- Check if the character is a player
78  int playerId = playerManager.GetPlayerIdFromControlledEntity(character);
79  if (playerId == 0)
80  return;
81 
82  if (m_OnPlayerEnterCompartment)
83  m_OnPlayerEnterCompartment.Invoke(character, targetEntity);
84  }
85 
86  //------------------------------------------------------------------------------------------------
87  override void OnCompartmentLeft(IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
88  {
89  if (m_OnCompartmentLeft)
90  m_OnCompartmentLeft.Invoke( targetEntity, manager, mgrID, slotID, move );
91 
92  PlayerManager playerManager = GetGame().GetPlayerManager();
93  ChimeraCharacter character = ChimeraCharacter.Cast(GetOwner());
94 
95  if (!playerManager || !character)
96  return;
97 
98  //--- Check if the character is a player
99  int playerId = playerManager.GetPlayerIdFromControlledEntity(character);
100  if (playerId == 0)
101  return;
102 
103  if (m_OnPlayerExitCompartment)
104  m_OnPlayerExitCompartment.Invoke(character, targetEntity);
105  }
106 
107  //------------------------------------------------------------------------------------------------
108  void OnLifeStateChanged(ECharacterLifeState previousLifeState, ECharacterLifeState newLifeState)
109  {
110  IEntity vehicle = GetVehicle();
111  if (!vehicle)
112  return;
113 
115  if (!vehicleFactionAff)
116  return;
117 
118  vehicleFactionAff.OnOccupantLifeStateChanged(previousLifeState, newLifeState);
119  }
120 
127  BaseCompartmentSlot FindFreeAndAccessibleCompartment(IEntity vehicle, ECompartmentType compartmentType, BaseCompartmentSlot customSlot = null)
128  {
129  BaseCompartmentManagerComponent compartmentManager = BaseCompartmentManagerComponent.Cast(vehicle.FindComponent(BaseCompartmentManagerComponent));
130  if (!compartmentManager)
131  return null;
132 
133  array<BaseCompartmentSlot> compartments = {};
134  compartmentManager.GetCompartments(compartments);
135 
136  if (customSlot)
137  {
138  if (!customSlot.IsCompartmentAccessible())
139  return null;
140  if (customSlot.IsGetInLockedFor(GetOwner()))
141  return null;
142  if (customSlot.GetOccupant())
143  return null;
144  if (compartmentType != customSlot.GetType())
145  return null;
146 
147  return customSlot;
148  }
149 
150  foreach (BaseCompartmentSlot compartment: compartments)
151  {
152  if (!compartment.IsCompartmentAccessible())
153  continue;
154  if (compartment.IsGetInLockedFor(GetOwner()))
155  continue;
156  if (compartment.GetOccupant())
157  continue;
158  if (compartmentType != compartment.GetType())
159  continue;
160 
161  return compartment;
162  }
163 
164  //--- Scan compartments in child entities (ToDo: Remove once GetCompartments is recursive)
165  BaseCompartmentSlot childSlot;
166  IEntity child = vehicle.GetChildren();
167  while (child)
168  {
169  childSlot = FindFreeAndAccessibleCompartment(child, compartmentType);
170  if (childSlot)
171  return childSlot;
172 
173  child = child.GetSibling();
174  }
175 
176  return null;
177  }
178 
186  bool MoveInVehicle(IEntity vehicle, ECompartmentType compartmentType, BaseCompartmentSlot customSlot = null)
187  {
188  if (!vehicle)
189  return false;
190 
191  //--- Find suitable slot
192  BaseCompartmentSlot slot = FindFreeAndAccessibleCompartment(vehicle, compartmentType, customSlot);
193  if (!slot)
194  return false;
195 
196  //--- Move character in the slot (must be called where character is local)
197  IEntity slotEntity = slot.GetOwner();
198  RplComponent slotRplComponent = RplComponent.Cast(slotEntity.FindComponent(RplComponent));
199  RplId slotEntityID = slotRplComponent.Id();
200  int slotID = slot.GetCompartmentSlotID();
201  Rpc(MoveInVehicleOwner, slotEntityID, slotID);
202 
203  //--- Lock the slot for a frame, so any other MoveInVehicle calls in this frame will skip it
204  slot.SetCompartmentAccessible(false);
205  GetGame().GetCallqueue().CallLater(slot.SetCompartmentAccessible, 1, false, true);
206  return true;
207  }
208 
209  [RplRpc(RplChannel.Reliable, RplRcver.Owner)]
210  protected void MoveInVehicleOwner(RplId slotEntityID, int slotID)
211  {
212  RplComponent slotRplComponent = RplComponent.Cast(Replication.FindItem(slotEntityID));
213  if (!slotRplComponent)
214  {
215  if (s_iWaitForVehicleTries >= WAIT_FOR_VEHICLE_MAX_TRIES)
216  {
217  // Time out, abort
218  s_iWaitForVehicleTries = 0;
219  return;
220  }
221 
222  GetGame().GetCallqueue().CallLater(MoveInVehicleOwner, 1000, false, slotEntityID, slotID);
223  s_iWaitForVehicleTries++;
224  return;
225  }
226 
227  s_iWaitForVehicleTries = 0;
228 
229  IEntity slotEntity = slotRplComponent.GetEntity();
230 
231  BaseCompartmentManagerComponent slotCompartmentManager = BaseCompartmentManagerComponent.Cast(slotEntity.FindComponent(BaseCompartmentManagerComponent));
232  BaseCompartmentSlot slot = slotCompartmentManager.FindCompartment(slotID);
233 
234  //--- Use slot's owner, not the vehicle - the slot may be in vehicle's child entity
235  MoveInVehicle(slotEntity, slot);
236  }
237 
244  bool MoveInVehicleAny(IEntity vehicle)
245  {
246  if (!vehicle)
247  return false;
248 
249  if (MoveInVehicle(vehicle, ECompartmentType.Pilot))
250  return true;
251 
252  if (MoveInVehicle(vehicle, ECompartmentType.Turret))
253  return true;
254 
255  if (MoveInVehicle(vehicle, ECompartmentType.Cargo))
256  return true;
257 
258  return false;
259  }
260 
267  IEntity GetVehicle()
268  {
269  if (GetCompartment())
270  return GetCompartment().GetVehicle();
271  else
272  return null;
273  }
274  IEntity GetVehicle(out int compartmentID)
275  {
276  if (GetCompartment())
277  return GetCompartment().GetVehicle(compartmentID);
278  else
279  return null;
280  }
281 
283  {
284  ChimeraCharacter char = ChimeraCharacter.Cast(GetOwner());
285  if (!char)
286  return;
287 
289  if (!controller)
290  return;
291 
292  controller.m_OnLifeStateChanged.Remove(OnLifeStateChanged);
293  }
294 
300  [Obsolete("Use GetType() on the compartment instance directly.")]
301  static ECompartmentType GetCompartmentType(notnull BaseCompartmentSlot compartment)
302  {
303  return compartment.GetType();
304  }
305 };
GetCharacterController
protected SCR_CharacterControllerComponent GetCharacterController(IEntity from)
Definition: SCR_ItemPlacementComponent.c:50
SCR_VehicleFactionAffiliationComponent
Definition: SCR_VehicleFactionAffiliationComponent.c:6
SCR_CompartmentAccessComponent
Definition: SCR_CompartmentAccessComponent.c:15
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
ECharacterLifeState
ECharacterLifeState
Definition: ECharacterLifeState.c:12
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
ECompartmentType
ECompartmentType
Definition: ECompartmentType.c:7
RplRpc
SCR_AchievementsHandlerClass ScriptComponentClass RplRpc(RplChannel.Reliable, RplRcver.Owner)] void UnlockOnClient(AchievementId achievement)
Definition: SCR_AchievementsHandler.c:11
func
func
Definition: SCR_AIThreatSystem.c:5
SCR_CharacterControllerComponent
Definition: SCR_CharacterControllerComponent.c:35
SCR_CompartmentAccessComponentClass
Definition: SCR_CompartmentAccessComponent.c:10
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
OnPlayerExitCompartment
ScriptInvokerBase< OnPlayerEnterCompartmentMethod > OnPlayerExitCompartment
Definition: SCR_CompartmentAccessComponent.c:7
OnPlayerExitCompartmentMethod
func OnPlayerExitCompartmentMethod
Definition: SCR_CompartmentAccessComponent.c:6
GetCompartment
CompartmentAccessComponentClass GameComponentClass GetCompartment()
Returns the compartent we're in.
OnPlayerEnterCompartment
ScriptInvokerBase< OnPlayerEnterCompartmentMethod > OnPlayerEnterCompartment
Definition: SCR_CompartmentAccessComponent.c:3
PlayerManager
Definition: PlayerManager.c:12
CompartmentAccessComponentClass
Definition: CompartmentAccessComponent.c:12
Obsolete
RespawnSystemComponentClass GameComponentClass Obsolete()] proto external GenericEntity DoSpawn(string prefab
RespawnSystemComponent should be attached to a gamemode to handle player spawning and respawning.
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
OnPlayerEnterCompartmentMethod
func OnPlayerEnterCompartmentMethod
Definition: SCR_CompartmentAccessComponent.c:2