Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_EquipWeaponAction.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
4 {
5  #ifndef DISABLE_INVENTORY
6 
7  [Attribute( "#AR-Inventory_Equip", desc: "What text should be displayed when the Equip action is available?" )]
8  protected string m_sEquipActionString;
9  [Attribute( "#AR-Inventory_Replaces", desc: "What text should be displayed when the Replace action is available?" )]
10  protected string m_sReplaceActionString;
11 
12  int m_iTargetSlotID = -1;
13  bool m_bWasUnequipping = false;
14  ref SCR_EquipPickedWeapon m_pEquipCB = new SCR_EquipPickedWeapon;
15  ref SCR_RemoveEquippedWeapon m_pRemoveWeaponCB = new SCR_RemoveEquippedWeapon;
16 
17  //------------------------------------------------------------------------------------------------
18  override void OnConfirmed(IEntity pUserEntity)
19  {
20  // If we were unequipping weapon, let's now play pickup gesture and callback back into this method
21  if (m_bWasUnequipping)
22  {
23  m_bWasUnequipping = false;
24  CharacterControllerComponent controller = CharacterControllerComponent.Cast(pUserEntity.FindComponent(CharacterControllerComponent));
25  // If we were unable to play gesture, something happened to character and it cannot equip next weapon now, so make sure to perform cleanup
26  if (!controller.TryPlayItemGesture(EItemGesture.EItemGesturePickUp, this, "Character_ActionGrab"))
27  {
28  m_iTargetSlotID = -1;
29  }
30  return;
31  }
32  IEntity pOwnerEntity = m_Item.GetOwner();
33  SCR_InventoryStorageManagerComponent manager = SCR_InventoryStorageManagerComponent.Cast(pUserEntity.FindComponent(SCR_InventoryStorageManagerComponent));
34  EquipedWeaponStorageComponent weaponStorage = EquipedWeaponStorageComponent.Cast(pUserEntity.FindComponent(EquipedWeaponStorageComponent));
35 
36  if (!manager || !weaponStorage)
37  return;
38  m_pEquipCB.m_pWeapon = pOwnerEntity;
39 
40  // Double check actual state... By this time slot shouldn't contain any weapon
41  if (weaponStorage.GetSlot(m_iTargetSlotID) == null || weaponStorage.GetSlot(m_iTargetSlotID).GetAttachedEntity() != null)
42  {
43  m_iTargetSlotID = -1;
44  return;
45  }
46  if (manager.TryInsertItemInStorage(pOwnerEntity, weaponStorage, m_iTargetSlotID, m_pEquipCB))
47  {
48  // Play sound
49  manager.PlayItemSound(pOwnerEntity, SCR_SoundEvent.SOUND_EQUIP);
50  }
51 
52  m_iTargetSlotID = -1;
53  }
54  //------------------------------------------------------------------------------------------------
56  override void OnRejected(IEntity pUserEntity)
57  {
58  m_bWasUnequipping = false;
59  m_iTargetSlotID = -1;
60  }
61  //------------------------------------------------------------------------------------------------
62  override protected void PerformActionInternal(SCR_InventoryStorageManagerComponent manager, IEntity pOwnerEntity, IEntity pUserEntity)
63  {
64  CharacterControllerComponent controller = CharacterControllerComponent.Cast(pUserEntity.FindComponent(CharacterControllerComponent));
65  if (!controller || controller.IsPlayingGesture())
66  return;
67  if (m_iTargetSlotID > -1)
68  return;
69  m_pEquipCB.m_pController = controller;
70  EquipedWeaponStorageComponent weaponStorage = EquipedWeaponStorageComponent.Cast(pUserEntity.FindComponent(EquipedWeaponStorageComponent));
71 
72  BaseWeaponManagerComponent weaponManager = BaseWeaponManagerComponent.Cast(pUserEntity.FindComponent(BaseWeaponManagerComponent));
73 
74  if (!weaponStorage || !weaponManager)
75  return;
76 
77  WeaponComponent weapon = WeaponComponent.Cast(pOwnerEntity.FindComponent(WeaponComponent));
78  WeaponSlotComponent weaponSlotCurr = weaponManager.GetCurrentSlot();
79 
80  array<WeaponSlotComponent> weaponsList = {};
81  int weaponCount = weaponManager.GetWeaponsSlots(weaponsList);
82  array<int> suitableSlots = {};
83  // Just filtering out suitable slots by weapon type
84  for (int i = 0; i < weaponCount; i++)
85  {
86  WeaponSlotComponent weaponSlot = weaponsList[i];
87  string weaponSlotType = weaponSlot.GetWeaponSlotType();
88 
89  if (weaponSlotType.Compare(weapon.GetWeaponSlotType()) != 0 && !CharacterHandWeaponSlotComponent.Cast(weaponSlot))
90  {
91  continue;
92  }
93  suitableSlots.Insert(i);
94  }
95 
96  if (suitableSlots.Count() == 0)
97  return;
98 
99  int i = 0;
100  // Let's try to find empty slot and equip weapon to it
101  if (suitableSlots.Count() > 1)
102  {
103  for (int j = 0; j < suitableSlots.Count(); j++)
104  {
105  WeaponSlotComponent slot = WeaponSlotComponent.Cast(weaponsList[suitableSlots[j]]);
106 
107  if (slot.GetWeaponEntity() == null)
108  {
109  i = j;
110  break;
111  }
112  // or fall back to currently equipped
113  if (slot == weaponSlotCurr)
114  {
115  i = j;
116  }
117  }
118  }
119  WeaponSlotComponent slot = WeaponSlotComponent.Cast(weaponsList[suitableSlots[i]]);
120  // Target slot has weapon, first detach it
121  if (slot.GetWeaponEntity())
122  {
123  m_pRemoveWeaponCB.m_pController = controller;
124  m_pRemoveWeaponCB.m_pParentAction = this;
125  m_iTargetSlotID = slot.GetWeaponSlotIndex();
126  // Say to callback that we want to immediately place weapon in hands after grab if target slot is currently selected weapon, if it is not then play full cycle of change weapon
127  // situation when we want to play full cycle might happen when we have let's say rifle in hands and we want to equip handgun from the ground
128  m_pEquipCB.m_bNoEquipAnims = weaponSlotCurr == slot;
129  m_bWasUnequipping = true;
130  if (!manager.TryRemoveItemFromStorage(slot.GetWeaponEntity(), weaponStorage, m_pRemoveWeaponCB))
131  {
132  m_iTargetSlotID = -1;
133  m_bWasUnequipping = false;
134  m_pEquipCB.m_bNoEquipAnims = false;
135  }
136  return;
137  }
138  m_bWasUnequipping = false;
139  m_pEquipCB.m_bNoEquipAnims = weaponSlotCurr == null;
140  // Free hands, play gesture right away and grab weapon
141  // If we are equiping to hand slot, just move it into hand directly
142  if (CharacterHandWeaponSlotComponent.Cast(slot))
143  {
144  if (manager.TryInsertItemInStorage(pOwnerEntity, weaponStorage, slot.GetWeaponSlotIndex()))
145  {
146  // Play sound
147  manager.PlayItemSound(pOwnerEntity, SCR_SoundEvent.SOUND_EQUIP);
148  }
149  }
150  else
151  {
152  if (controller.TryPlayItemGesture(EItemGesture.EItemGesturePickUp, this, "Character_ActionGrab"))
153  m_iTargetSlotID = slot.GetWeaponSlotIndex();
154  }
155  }
156 
157  //------------------------------------------------------------------------------------------------
158  override bool CanBePerformedScript(IEntity user)
159  {
160  if ( !super.CanBePerformedScript( user ) )
161  return false;
162  CharacterControllerComponent controller = CharacterControllerComponent.Cast(user.FindComponent(CharacterControllerComponent));
163  if (m_pWeaponOnGroundComponent.CanBeEquipped(controller) == ECanBeEquippedResult.STANCE_TOO_LOW)
164  {
165  SetCannotPerformReason("#AR-Inventory_StanceTooLow");
166  return false;
167  }
168 
169  return CanEquipOrReplaceWeapon( user ) && !controller.IsReloading() && controller.CanPlayItemGesture();
170 
171  }
172 
173  //------------------------------------------------------------------------------------------------
174  override bool GetActionNameScript(out string outName)
175  {
176  if (m_bIsSwappingWeapons)
177  {
178  string replaceAction = string.Format(WidgetManager.Translate(m_sReplaceActionString), WidgetManager.Translate(m_sWeaponToSwapName));
179  outName = string.Format("%1 %2", m_sEquipActionString, replaceAction);
180  }
181  else
182  outName = m_sEquipActionString;
183 
184  return true;
185  }
186 
187  #endif
188 };
189 
190 //------------------------------------------------------------------------------------------------
192 {
193  CharacterControllerComponent m_pController;
194  IEntity m_pWeapon;
195  bool m_bNoEquipAnims;
196  protected override void OnComplete()
197  {
198  m_pController.TryEquipRightHandItem(m_pWeapon, EEquipItemType.EEquipTypeWeapon, m_bNoEquipAnims);
199  }
200 
201 };
202 
203 //------------------------------------------------------------------------------------------------
205 {
206  SCR_EquipWeaponAction m_pParentAction;
207  CharacterControllerComponent m_pController;
208  protected override void OnComplete()
209  {
210  m_pParentAction.OnConfirmed(m_pController.GetOwner());
211  }
212 
213 };
214 
215 //------------------------------------------------------------------------------------------------
218 {
219  #ifndef DISABLE_INVENTORY
220 
221  [Attribute( "#AR-Inventory_PickUp", desc: "What text should be displayed when the Pick up action is available?" )]
222  protected string m_sPickUpActionString;
223  [Attribute( "#AR-Inventory_Replaces", desc: "What text should be displayed when the Replace text is visible?" )]
224  protected string m_sReplaceActionString;
225  [Attribute( "#AR-Inventory_EquipSling", desc: "What text should be displayed when the picked up weapons type is a primary" )]
226  protected string m_sEquipingSlingString;
227  [Attribute( "#AR-Inventory_EquipHolster", desc: "What text should be displayed when the picked up weapons type is a secondary" )]
228  protected string m_sEquipingHolsterString;
229 
230  int m_iTargetSlotID = -1;
231 
232  override void OnConfirmed(IEntity pUserEntity)
233  {
234  IEntity pOwnerEntity = m_Item.GetOwner();
235  SCR_InventoryStorageManagerComponent manager = SCR_InventoryStorageManagerComponent.Cast(pUserEntity.FindComponent(SCR_InventoryStorageManagerComponent));
236  EquipedWeaponStorageComponent weaponStorage = EquipedWeaponStorageComponent.Cast(pUserEntity.FindComponent(EquipedWeaponStorageComponent));
237 
238  if (!manager || !weaponStorage)
239  return;
240 
241  if (weaponStorage.GetSlot(m_iTargetSlotID) != null)
242  {
243  if (weaponStorage.GetSlot(m_iTargetSlotID).GetAttachedEntity() != null)
244  {
245  manager.TryReplaceItem(pOwnerEntity, weaponStorage, m_iTargetSlotID);
246  }
247  else
248  {
249  manager.TryInsertItemInStorage(pOwnerEntity, weaponStorage, m_iTargetSlotID);
250  }
251  }
252  m_iTargetSlotID = -1;
253  // Play sound
254  manager.PlayItemSound(pOwnerEntity, SCR_SoundEvent.SOUND_EQUIP);
255  }
256 
257  //------------------------------------------------------------------------------------------------
258  override protected void PerformActionInternal(SCR_InventoryStorageManagerComponent manager, IEntity pOwnerEntity, IEntity pUserEntity)
259  {
260  CharacterControllerComponent controller = CharacterControllerComponent.Cast(pUserEntity.FindComponent(CharacterControllerComponent));
261  if (!controller || controller.IsChangingItem() || controller.IsPlayingGesture())
262  return;
263  if (m_iTargetSlotID > -1)
264  return;
265 
266  EquipedWeaponStorageComponent weaponStorage = EquipedWeaponStorageComponent.Cast(pUserEntity.FindComponent(EquipedWeaponStorageComponent));
267 
268  BaseWeaponManagerComponent weaponManager = BaseWeaponManagerComponent.Cast(pUserEntity.FindComponent(BaseWeaponManagerComponent));
269 
270  if (!weaponStorage || !weaponManager)
271  return;
272 
273  WeaponComponent weapon = WeaponComponent.Cast(pOwnerEntity.FindComponent(WeaponComponent));
274  WeaponSlotComponent weaponSlotCurr = weaponManager.GetCurrentSlot();
275 
276  array<WeaponSlotComponent> weaponsList = {};
277  int weaponCount = weaponManager.GetWeaponsSlots(weaponsList);
278  array<int> suitableSlots = {};
279  for (int i = 0; i < weaponCount; i++)
280  {
281  WeaponSlotComponent weaponSlot = weaponsList[i];
282  string weaponSlotType = weaponSlot.GetWeaponSlotType();
283 
284  if (weaponSlotType.Compare(weapon.GetWeaponSlotType()) != 0 && !CharacterHandWeaponSlotComponent.Cast(weaponSlot))
285  {
286  continue;
287  }
288  suitableSlots.Insert(i);
289  }
290 
291  if (suitableSlots.Count() == 0)
292  return;
293 
294  WeaponSlotComponent weaponSlotFinal = null;
295  // Default is first found slot
296  int i = 0;
297  // There are several slots that can held current weapon type
298  if (suitableSlots.Count() > 1)
299  {
300  for (int j = 0; j < suitableSlots.Count(); j++)
301  {
302  // If we have weapon equipped, search for the slot that is not selected one
303  if (weaponSlotCurr != null)
304  {
305  if ( weaponSlotCurr != WeaponSlotComponent.Cast(weaponsList[suitableSlots[j]]))
306  {
307  i = j;
308  break;
309  }
310  }
311  else
312  {
313  // otherwise prefer slot that doesn't have any weapon set in it
314  WeaponSlotComponent slotComp = WeaponSlotComponent.Cast(weaponsList[suitableSlots[j]]);
315  if (!slotComp.GetWeaponEntity())
316  {
317  i = j;
318  break;
319  }
320  }
321  }
322  }
323 
324  weaponSlotFinal = WeaponSlotComponent.Cast(weaponsList[suitableSlots[i]]);
325 
326  if (weaponSlotFinal == weaponSlotCurr && weaponSlotCurr != null && weaponSlotCurr.GetWeaponEntity() != null)
327  return;
328 
329  if (controller.TryPlayItemGesture(EItemGesture.EItemGesturePickUp, this, "Character_ActionGrab"))
330  m_iTargetSlotID = weaponSlotFinal.GetWeaponSlotIndex();
331  }
332 
333  //------------------------------------------------------------------------------------------------
334  override bool CanBePerformedScript(IEntity user)
335  {
336  if ( !super.CanBePerformedScript( user ) )
337  return false;
338  CharacterControllerComponent controller = CharacterControllerComponent.Cast(user.FindComponent(CharacterControllerComponent));
339  return CanEquipOrReplaceWeapon( user ) && controller.CanPlayItemGesture();
340  }
341 
342  //------------------------------------------------------------------------------------------------
343  override bool GetActionNameScript(out string outName)
344  {
345  if (m_bIsSwappingWeapons)
346  {
347  string replaceAction = string.Format(WidgetManager.Translate(m_sReplaceActionString), WidgetManager.Translate(m_sWeaponToSwapName));
348  outName = string.Format("%1 %2", m_sPickUpActionString, replaceAction);
349  }
350  else if (m_sWeaponOnGroundType == PRIMARY_WEAPON_TYPE)
351  outName = string.Format("%1 %2", m_sPickUpActionString, m_sEquipingSlingString);
352  else if (m_sWeaponOnGroundType == SECONDARY_WEAPON_TYPE)
353  outName = string.Format("%1 %2", m_sPickUpActionString, m_sEquipingHolsterString);
354  else
355  outName = m_sPickUpActionString;
356 
357  return true;
358  }
359 
360  //------------------------------------------------------------------------------------------------
361  override BaseWeaponComponent GetWeaponToSwap( notnull BaseWeaponManagerComponent weaponManager )
362  {
363  BaseWeaponComponent weaponToSwap;
364  WeaponSlotComponent currentWeaponSlot = weaponManager.GetCurrentSlot();
365  string weaponSlotType;
366 
367  foreach ( WeaponSlotComponent weaponSlot: m_aWeaponSlots )
368  {
369  weaponSlotType = weaponSlot.GetWeaponSlotType();
370 
371  if ( weaponSlotType != m_sWeaponOnGroundType )
372  continue;
373 
374  // If there is only a single slot compatible with the weapon on the ground it will be used
375  if ( m_iSameTypeSlotsCount <= 1 )
376  {
377  if ( weaponSlot == currentWeaponSlot )
378  return null;
379 
380  weaponToSwap = BaseWeaponComponent.Cast( weaponSlot.GetWeaponEntity().FindComponent( BaseWeaponComponent ) );
381  if ( weaponToSwap )
382  return weaponToSwap;
383  }
384 
385  // If the slot is not currently equipped it will be used.
386  if ( weaponSlot != currentWeaponSlot )
387  {
388  if ( weaponSlot.GetWeaponEntity() )
389  {
390  weaponToSwap = BaseWeaponComponent.Cast( weaponSlot.GetWeaponEntity().FindComponent( BaseWeaponComponent ) );
391  if ( weaponToSwap )
392  return weaponToSwap;
393  }
394  }
395  }
396 
397  return null;
398  }
399 
400  #endif
401 };
402 
SCR_RemoveEquippedWeapon
Definition: SCR_EquipWeaponAction.c:204
SCR_EquipWeaponAction
Equip weapon.
Definition: SCR_EquipWeaponAction.c:3
m_Item
NewsFeedItem m_Item
Definition: SCR_NewsSubMenu.c:2
SCR_SoundEvent
Definition: SCR_SoundEvent.c:1
EEquipItemType
EEquipItemType
Definition: EEquipItemType.c:12
ECanBeEquippedResult
ECanBeEquippedResult
Definition: ECanBeEquippedResult.c:12
SCR_EquipPickedWeapon
Definition: SCR_EquipWeaponAction.c:191
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
WeaponSlotComponent
Definition: WeaponSlotComponent.c:12
SCR_BaseWeaponAction
Definition: SCR_BaseWeaponAction.c:1
SCR_EquipWeaponHolsterAction
Pickup weapon to holster position, without equipping it.
Definition: SCR_EquipWeaponAction.c:217
ScriptedInventoryOperationCallback
Definition: ScriptedInventoryOperationCallback.c:12
Attribute
typedef Attribute
Post-process effect of scripted camera.
BaseWeaponComponent
Definition: BaseWeaponComponent.c:12
EItemGesture
EItemGesture
Definition: EItemGesture.c:12