Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_SupportStationManagerComponent.c
Go to the documentation of this file.
1 [ComponentEditorProps(category: "GameScripted/GameMode/Components", description: "Manager that holds a reference to all Support Stations.")]
2 class SCR_SupportStationManagerComponentClass : ScriptComponentClass
3 {
4 }
5 
6 //~ ScriptInvokers
7 void SupportStation_OnSupportStationExecuted(SCR_BaseSupportStationComponent supportStation, ESupportStationType supportStationType, IEntity actionOwner, IEntity actionUser, SCR_BaseUseSupportStationAction action);
9 
11 {
12  // List of all support stations with range
13  protected ref map<ESupportStationType, ref array<SCR_BaseSupportStationComponent>> m_mSupportStations = new map<ESupportStationType, ref array<SCR_BaseSupportStationComponent>>();
14 
15  protected static SCR_SupportStationManagerComponent s_Instance;
16 
17  //~ Script invoker
18  protected ref ScriptInvokerBase<SupportStation_OnSupportStationExecuted> m_OnSupportStationExecutedSuccessfully = new ScriptInvokerBase<SupportStation_OnSupportStationExecuted>();
19 
20  //------------------------------------------------------------------------------------------------
23  static SCR_SupportStationManagerComponent GetInstance()
24  {
25  return s_Instance;
26  }
27 
28  //------------------------------------------------------------------------------------------------
35  static void OnSupportStationExecutedSuccessfully(notnull SCR_BaseSupportStationComponent supportStation, ESupportStationType supportStationType, IEntity actionUser, IEntity actionOwner, SCR_BaseUseSupportStationAction action)
36  {
37  SCR_SupportStationManagerComponent instance = GetInstance();
38  if (!instance)
39  return;
40 
41  instance.GetOnSupportStationExecutedSuccessfully().Invoke(supportStation, supportStationType, actionOwner, actionUser, action);
42  }
43 
44  //------------------------------------------------------------------------------------------------
48  ScriptInvokerBase<SupportStation_OnSupportStationExecuted> GetOnSupportStationExecutedSuccessfully()
49  {
50  return m_OnSupportStationExecutedSuccessfully;
51  }
52 
53  //------------------------------------------------------------------------------------------------
54  //~ Get array of all Support stations of given type
55  protected int GetArrayOfSupportStationType(ESupportStationType type, notnull out array<SCR_BaseSupportStationComponent> supportStations)
56  {
57  supportStations.Clear();
58 
59  //~ Add new type to make sure it is never null
60  if (!m_mSupportStations.Find(type, supportStations))
61  {
62  m_mSupportStations.Insert(type, {});
63  supportStations = m_mSupportStations[type];
64  }
65 
66  return supportStations.Count();
67  }
68 
69  //------------------------------------------------------------------------------------------------
79  SCR_BaseSupportStationComponent GetClosestValidSupportStation(ESupportStationType type, notnull IEntity actionUser, notnull IEntity actionOwner, SCR_BaseUseSupportStationAction action, vector actionPosition, out ESupportStationReasonInvalid reasonInvalid, out int supplyCost)
80  {
81  array<SCR_BaseSupportStationComponent> supportStations = {};
82 
83  GetArrayOfSupportStationType(type, supportStations);
84 
85  reasonInvalid = ESupportStationReasonInvalid.NOT_IN_RANGE;
86  ESupportStationReasonInvalid newReasonInvalid;
87 
88  SCR_BaseSupportStationComponent closestSupportStation = null;
89  ESupportStationPriority validPriority = -1;
90  float closestDistance = float.MAX;
91  int minSuppliesRequired = int.MAX;
92  int suppliesRequired, validSupplyCost;
93 
94  foreach (SCR_BaseSupportStationComponent station : supportStations)
95  {
96  if (!station)
97  continue;
98 
99  //~ Found all support stations of same priority and closest support station is already set
100  if (closestSupportStation != null && station.GetSupportStationPriority() < validPriority)
101  break;
102 
103  //~ Check if support station is valid
104  if (station.IsValid(actionOwner, actionUser, action, actionPosition, newReasonInvalid, suppliesRequired))
105  {
106  //~ station is valid so make sure to save the priority level which makes sure on the same priority stations are ever checked
107  if (validPriority < 0)
108  validPriority = station.GetSupportStationPriority();
109 
110  //~ Get distance to user
111  float distancesq = vector.DistanceSq(actionPosition, station.GetPosition());
112 
113  //~ Support station is closer to user so set this as valid return
114  if (distancesq < closestDistance)
115  {
116  closestDistance = distancesq;
117  closestSupportStation = station;
118  validSupplyCost = suppliesRequired;
119  }
120  }
121  //~ Support station was invalid set new invalid reason if it has an higher priority
122  else if (newReasonInvalid > reasonInvalid)
123  {
124  reasonInvalid = newReasonInvalid;
125  }
126 
127  //~ Supply station was valid but not enough supplies
128  if (newReasonInvalid == ESupportStationReasonInvalid.NO_SUPPLIES && minSuppliesRequired > suppliesRequired)
129  minSuppliesRequired = suppliesRequired;
130  }
131 
132  //~ Set supply cost to closest station cost
133  if (closestSupportStation)
134  supplyCost = validSupplyCost;
135  //~ No valid supply station as not enough supplies to execute. Still set min supplies needed to execute
136  else if (newReasonInvalid == ESupportStationReasonInvalid.NO_SUPPLIES)
137  supplyCost = minSuppliesRequired;
138  //~ No valid supply station and reason was not lack of supplies
139  else
140  supplyCost = -1;
141 
142  return closestSupportStation;
143  }
144 
145  //------------------------------------------------------------------------------------------------
148  void AddSupportStation(notnull SCR_BaseSupportStationComponent supportStation)
149  {
150  if (supportStation.GetSupportStationType() == ESupportStationType.NONE)
151  {
152  Print("Cannot add SupportStation of type NONE! Make sure to overwrite GetSupportStationType() in inherited class.", LogLevel.ERROR);
153  return;
154  }
155 
156  array<SCR_BaseSupportStationComponent> supportStations = {};
157  GetArrayOfSupportStationType(supportStation.GetSupportStationType(), supportStations);
158 
159  //~ Already in list
160  if (supportStations.Contains(supportStation))
161  return;
162 
163  for (int i = 0, count = supportStations.Count(); i < count; i++)
164  {
165  //~ Make sure it is added near the at priority in the array
166  if (supportStation.GetSupportStationPriority() >= supportStations[i].GetSupportStationPriority())
167  {
168  //~ Insert in list at the correct location
169  m_mSupportStations[supportStation.GetSupportStationType()].InsertAt(supportStation, i);
170  return;
171  }
172  }
173 
174  //~ Add to last position in array as loop above did not add it
175  m_mSupportStations[supportStation.GetSupportStationType()].Insert(supportStation);
176  }
177 
178  //------------------------------------------------------------------------------------------------
181  void RemoveSupportStation(notnull SCR_BaseSupportStationComponent supportStation)
182  {
183  if (supportStation.GetSupportStationType() == ESupportStationType.NONE)
184  return;
185 
186  array<SCR_BaseSupportStationComponent> supportStations = {};
187  GetArrayOfSupportStationType(supportStation.GetSupportStationType(), supportStations);
188 
189  int index = supportStations.Find(supportStation);
190 
191  //~ Not in list
192  if (index < 0)
193  return;
194 
195  //~ Remove from list
196  m_mSupportStations[supportStation.GetSupportStationType()].RemoveOrdered(index);
197  }
198 
199  //------------------------------------------------------------------------------------------------
207  static void GetCombinedHitZonesStateForDamageSupportStation(notnull SCR_DamageManagerComponent damageManagerComponent, notnull array<HitZone> hitZones, float maxHealPercentage, out float roughHealPercentageStatus, out bool allHitZonesMaxHealth = true)
208  {
209  HitZone defaultHitZone = damageManagerComponent.GetDefaultHitZone();
210 
211  //~ Out values
212  allHitZonesMaxHealth = true;
213  roughHealPercentageStatus = 0;
214 
215  int hitZonesChecked = 0;
216  bool defaultHitZoneIncluded = false;
217 
218  foreach (HitZone hitZone : hitZones)
219  {
220  if (defaultHitZone == hitZone)
221  {
222  defaultHitZoneIncluded = true;
223  continue;
224  }
225 
226  //~ Get percentage of hitZone and add it to the pool
227  hitZonesChecked++;
228  roughHealPercentageStatus += hitZone.GetHealthScaled();
229 
230  //~ Check if entity can be healed more by this support station if not yet set false
231  if (allHitZonesMaxHealth && hitZone.GetHealthScaled() < maxHealPercentage)
232  allHitZonesMaxHealth = false;
233  }
234 
235  //~ Get average health of all hitZones that can be healed
236  if (hitZonesChecked > 0)
237  roughHealPercentageStatus = roughHealPercentageStatus / hitZonesChecked;
238  else
239  roughHealPercentageStatus = 1;
240 
241  if (defaultHitZoneIncluded && defaultHitZone)
242  {
243  //~ If parts heal is higher than defualt hitZone health use default hitZone instead
244  if (roughHealPercentageStatus > defaultHitZone.GetHealthScaled())
245  roughHealPercentageStatus = defaultHitZone.GetHealthScaled();
246  }
247  }
248 
249  //------------------------------------------------------------------------------------------------
250  override void OnPostInit(IEntity owner)
251  {
252  if (s_Instance)
253  return;
254 
255  BaseGameMode gameMode = GetGame().GetGameMode();
256  if (!gameMode)
257  return;
258 
260  }
261 }
ComponentEditorProps
SCR_FragmentEntityClass ComponentEditorProps
ESupportStationPriority
ESupportStationPriority
Definition: ESupportStationPriority.c:2
SupportStation_OnSupportStationExecuted
func SupportStation_OnSupportStationExecuted
Definition: SCR_SupportStationManagerComponent.c:8
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
HitZone
Definition: HitZone.c:12
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_mSupportStations
protected ref map< ESupportStationType, SCR_BaseSupportStationComponent > m_mSupportStations
Definition: SCR_SupportStationGadgetComponent.c:14
func
func
Definition: SCR_AIThreatSystem.c:5
ESupportStationReasonInvalid
ESupportStationReasonInvalid
Definition: ESupportStationReasonInvalid.c:3
SCR_SupportStationManagerComponent
Definition: SCR_SupportStationManagerComponent.c:10
s_Instance
SCR_SpawnerSlotManagerClass s_Instance
Class used for managing changes and removals of slots present in world.
ESupportStationType
ESupportStationType
Definition: ESupportStationType.c:2
SCR_SupportStationManagerComponentClass
Definition: SCR_SupportStationManagerComponent.c:2
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
type
EDamageType type
Definition: SCR_DestructibleTreeV2.c:32
SCR_BaseUseSupportStationAction
Definition: SCR_BaseUseSupportStationAction.c:1
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180