Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AISwitchMagazine.c
Go to the documentation of this file.
2 {
3  protected static const string PORT_MAGAZINE_COMPONENT = "MagazineComponent";
4 
5  // Used for reloading of turret
6  // Acrual reload timer is not implemented in gamecode yet for AI, so we fake the reload time
7  protected bool m_bReloadingTimer;
8  protected float m_fReloadFinishTime_ms;
9 
10  //--------------------------------------------------------------------------------------------
11  override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
12  {
13  if (!m_WeaponMgrComp || !m_ControlComp || !m_InventoryMgr)
14  return ENodeResult.FAIL;
15 
16  BaseMagazineComponent newMagazineComp = null;
17  GetVariableIn(PORT_MAGAZINE_COMPONENT, newMagazineComp);
18 
19  if (!newMagazineComp)
20  {
21  #ifdef AI_DEBUG
22  AddDebugMessage("Skipped magazine switch, no magazine was provided");
23  #endif
24  return ENodeResult.SUCCESS;
25  }
26 
27  // Resolve which weapon manager to use
28  BaseCompartmentSlot compartmentSlot = m_CompartmentAccessComp.GetCompartment();
29  BaseWeaponManagerComponent weaponMgr;
30  if (compartmentSlot)
31  weaponMgr = BaseWeaponManagerComponent.Cast(compartmentSlot.GetOwner().FindComponent(BaseWeaponManagerComponent));
32  else
33  weaponMgr = m_WeaponMgrComp;
34 
35  // Check if we already have this magazine
36  BaseMagazineComponent currentMagazineComp = SCR_AIWeaponHandling.GetCurrentMagazineComponent(weaponMgr);
37  if (currentMagazineComp == newMagazineComp)
38  {
39  if (!compartmentSlot)
40  {
41  if (m_ControlComp.IsReloading())
42  return ENodeResult.RUNNING;
43 
44  if (!SCR_AIWeaponHandling.IsCurrentMuzzleChambered(weaponMgr))
45  {
46  #ifdef AI_DEBUG
47  AddDebugMessage("Muzzle is not chambered, requesting reload");
48  #endif
49 
50  m_ControlComp.ReloadWeapon();
51  return ENodeResult.RUNNING;
52  }
53  }
54 
55 
56  #ifdef AI_DEBUG
57  AddDebugMessage("Magazine switch completed");
58  #endif
59  return ENodeResult.SUCCESS;
60  }
61 
62  // Switch magazine
63  if (compartmentSlot)
64  {
65  //-----------------------------------------
66  // Turret magazine switching
67 
68  IEntity compartmentParentEntity = compartmentSlot.GetOwner();
69  TurretControllerComponent turretController = TurretControllerComponent.Cast(compartmentParentEntity.FindComponent(TurretControllerComponent));
70  InventoryStorageManagerComponent turretInventoryMgr = turretController.GetInventoryManager();
71 
72  if (turretInventoryMgr.Contains(newMagazineComp.GetOwner()))
73  {
74  if (!m_bReloadingTimer)
75  {
76  // Start reloading timer
77  m_bReloadingTimer = true;
78  float reloadDuration_ms = 1000 * turretController.GetReloadDuration();
79  m_fReloadFinishTime_ms = GetGame().GetWorld().GetWorldTime() + reloadDuration_ms;
80 
81  #ifdef AI_DEBUG
82  AddDebugMessage(string.Format("Started turret reload timer: %1s %2 %3 %4", reloadDuration_ms/1000.0, newMagazineComp, newMagazineComp.GetOwner(), newMagazineComp.GetOwner().GetPrefabData().GetPrefabName()));
83  #endif
84  }
85  else
86  {
87  float currentTime_ms = GetGame().GetWorld().GetWorldTime();
88  if (currentTime_ms >= m_fReloadFinishTime_ms)
89  {
90  m_bReloadingTimer = false;
91 
92  #ifdef AI_DEBUG
93  AddDebugMessage(string.Format("StartMagazineSwitchTurret() %1 %2 %3", newMagazineComp, newMagazineComp.GetOwner(), newMagazineComp.GetOwner().GetPrefabData().GetPrefabName()));
94  #endif
95  SCR_AIWeaponHandling.StartMagazineSwitchTurret(turretController, newMagazineComp);
96  }
97  }
98 
99  return ENodeResult.RUNNING;
100  }
101  else
102  {
103  #ifdef AI_DEBUG
104  AddDebugMessage("Failed magazine switch, the magazine was not found in turret inventory");
105  #endif
106  return ENodeResult.FAIL;
107  }
108  }
109  else
110  {
111  //-----------------------------------------
112  // Character magazine switching
113 
114  if (m_ControlComp.IsReloading())
115  {
116  return ENodeResult.RUNNING;
117  }
118  else
119  {
120  // Start magazine change
121  // Ensure that the magazine is indeed in our inventory
122  // This check prevents AI equipping a rocket he just shot
123  if (m_InventoryMgr.Contains(newMagazineComp.GetOwner()))
124  {
125  #ifdef AI_DEBUG
126  AddDebugMessage(string.Format("StartMagazineSwitchCharacter() %1 %2 %3", newMagazineComp, newMagazineComp.GetOwner(), newMagazineComp.GetOwner().GetPrefabData().GetPrefabName()));
127  #endif
128  SCR_AIWeaponHandling.StartMagazineSwitchCharacter(m_ControlComp, newMagazineComp);
129  return ENodeResult.RUNNING;
130  }
131  else
132  {
133  #ifdef AI_DEBUG
134  AddDebugMessage("Failed magazine switch, the magazine was not found in inventory");
135  #endif
136  return ENodeResult.FAIL;
137  }
138  }
139  }
140 
141  return ENodeResult.FAIL;
142  }
143 
144  //--------------------------------------------------------------------------------------------
145  protected static ref TStringArray s_aVarsIn = {PORT_MAGAZINE_COMPONENT};
146  override TStringArray GetVariablesIn() { return s_aVarsIn; }
147 
148  override bool VisibleInPalette() { return true; }
149 }
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
InventoryStorageManagerComponent
Definition: InventoryStorageManagerComponent.c:12
SCR_AIWeaponHandlingBase
Base class for nodes which handle magazine switching.
Definition: SCR_AIWeaponHandlingBase.c:2
TurretControllerComponent
Definition: TurretControllerComponent.c:12
SCR_AISwitchMagazine
Definition: SCR_AISwitchMagazine.c:1