Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AttachItemFromInventoryAction.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
4 {
5  #ifndef DISABLE_INVENTORY
6  SCR_InventoryStorageManagerComponent m_InventoryManager;
7  InventoryItemComponent m_ItemComponent;
8  BaseWeaponComponent m_WeaponComponent;
9 
10  //------------------------------------------------------------------------------------------------
11  override void Init(IEntity pOwnerEntity, GenericComponent pManagerComponent)
12  {
13  GenericEntity owner = GenericEntity.Cast(pOwnerEntity);
14  if (owner)
15  {
16  m_ItemComponent = InventoryItemComponent.Cast(owner.FindComponent(InventoryItemComponent));
17  }
18  }
19 
20  //------------------------------------------------------------------------------------------------
21  override protected void PerformActionInternal(SCR_InventoryStorageManagerComponent manager, IEntity pOwnerEntity, IEntity pUserEntity)
22  {
23  WeaponAttachmentsStorageComponent storage = WeaponAttachmentsStorageComponent.Cast(m_WeaponComponent.GetOwner().FindComponent(WeaponAttachmentsStorageComponent));
24  if (!m_InventoryManager.CanMoveItemToStorage(GetOwner(), storage, -1))
25  {
26  BaseMagazineComponent magazine = BaseMagazineComponent.Cast(pOwnerEntity.FindComponent(BaseMagazineComponent));
27  CharacterControllerComponent ctrl = CharacterControllerComponent.Cast(pUserEntity.FindComponent(CharacterControllerComponent));
28  if (ctrl && magazine)
29  {
30  bool bEmpty = storage.Get(0) == null;
31  ctrl.ReloadWeaponWith(pOwnerEntity, !bEmpty);
32  }
33  else
34  {
35  manager.TrySwapItemStorages(pOwnerEntity, storage.Get(0));
36  }
37  manager.PlayItemSound(pOwnerEntity, "SOUND_SWITCH");
38  }
39  else
40  {
41  // Check if we want to reload a magazine, and use ReloadWeapopnWith if so.
42  // This prevents a race condition were the old magazine appears to be still in the weapon
43  // due to network lag
44  BaseMagazineComponent magazine = BaseMagazineComponent.Cast(pOwnerEntity.FindComponent(BaseMagazineComponent));
45  CharacterControllerComponent ctrl = CharacterControllerComponent.Cast(pUserEntity.FindComponent(CharacterControllerComponent));
46  if (ctrl && magazine)
47  {
48  bool bEmpty = storage.Get(0) == null;
49  ctrl.ReloadWeaponWith(pOwnerEntity, !bEmpty);
50  }
51  else
52  {
53  manager.EquipWeaponAttachment(pOwnerEntity, pUserEntity);
54  }
55  }
56  }
57 
58  //------------------------------------------------------------------------------------------------
59  override bool CanBeShownScript(IEntity user)
60  {
61  if (!m_ItemComponent)
62  return false;
63 
64  if (!m_ItemComponent.GetParentSlot())
65  return false;
66 
67  ChimeraCharacter character = ChimeraCharacter.Cast(SCR_PlayerController.GetLocalControlledEntity());
68  if(!character)
69  return false;
70 
71  CharacterControllerComponent controller = character.GetCharacterController();
72  if(!controller)
73  return false;
74 
75  if(!controller.GetInspect())
76  return false;
77 
79  m_InventoryManager = SCR_InventoryStorageManagerComponent.Cast(character.FindComponent(SCR_InventoryStorageManagerComponent));
81  return false;
82 
83  BaseWeaponManagerComponent weaponManager = controller.GetWeaponManagerComponent();
84  if(!weaponManager)
85  return false;
86 
87  m_WeaponComponent = controller.GetWeaponManagerComponent().GetCurrentWeapon();
88  if(!m_WeaponComponent || m_WeaponComponent.GetOwner() == m_ItemComponent.GetParentSlot().GetOwner())
89  return false;
90 
91  auto storage = WeaponAttachmentsStorageComponent.Cast(m_WeaponComponent.GetOwner().FindComponent(WeaponAttachmentsStorageComponent));
92  if (!storage)
93  return false;
94 
95  bool result = m_InventoryManager.CanMoveItemToStorage(GetOwner(), storage, -1);
96  if (!result)
97  {
98  IEntity existingAttachment = storage.Get(0);
99  if (existingAttachment)
100  result = m_InventoryManager.CanSwapItemStorages(GetOwner(), existingAttachment);
101  }
102 
103  if (!result)
104  {
105  // When we get here, it can still be a grenade to be inserted into an attached muzzle
106  BaseMuzzleComponent muzzleComp = m_WeaponComponent.GetCurrentMuzzle();
107  auto xstorage = WeaponAttachmentsStorageComponent.Cast(muzzleComp.GetOwner().FindComponent(WeaponAttachmentsStorageComponent));
108  if (!xstorage)
109  return false;
110  result = m_InventoryManager.CanMoveItemToStorage(GetOwner(), xstorage, -1);
111  if (!result)
112  {
113  IEntity existingAttachment = xstorage.Get(0);
114  if (existingAttachment)
115  {
116  if (existingAttachment == GetOwner())
117  result = false;
118  else
119  result = m_InventoryManager.CanSwapItemStorages(GetOwner(), existingAttachment);
120  }
121  }
122  }
123 
124  return result;
125  }
126 
127  override bool GetActionNameScript(out string outName)
128  {
129  if (!m_ItemComponent)
130  return false;
131  UIInfo actionInfo = GetUIInfo();
132  UIInfo itemInfo = m_ItemComponent.GetUIInfo();
133  if (actionInfo && itemInfo)
134  {
135  outName = string.Format("%1%2", actionInfo.GetName(), itemInfo.GetName());
136  return true;
137  }
138  else
139  {
140  return false;
141  }
142  }
143  #endif
144 };
SCR_InventoryAction
modded version for to be used with the inventory 2.0
Definition: SCR_InventoryAction.c:3
SCR_PlayerController
Definition: SCR_PlayerController.c:31
UIInfo
UIInfo - declare object, allows to define UI elements.
Definition: UIInfo.c:13
m_InventoryManager
SCR_InventoryStorageManagerComponent m_InventoryManager
Definition: SCR_AttachementAction.c:15
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
GetUIInfo
SCR_UIInfo GetUIInfo()
Definition: SCR_EditableEntityCampaignBuildingModeLabelSetting.c:27
BaseWeaponComponent
Definition: BaseWeaponComponent.c:12
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
InventoryItemComponent
Definition: InventoryItemComponent.c:12
BaseMuzzleComponent
Definition: BaseMuzzleComponent.c:12
SCR_AttachItemFromInventoryAction
Equip weapon attachment.
Definition: SCR_AttachItemFromInventoryAction.c:3