Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_MilitaryBaseSystem.c
Go to the documentation of this file.
1void OnBaseFactionChangedDelegate(SCR_MilitaryBaseComponent base, Faction faction);
2void OnLogicPresenceChangedDelegate(SCR_MilitaryBaseComponent base, SCR_MilitaryBaseLogicComponent logic);
3void OnBaseRegisteredDelegate(SCR_MilitaryBaseComponent base);
4void OnBaseUnregisteredDelegate(SCR_MilitaryBaseComponent base);
5
10
11typedef ScriptInvokerBase<OnBaseFactionChangedDelegate> OnBaseFactionChangedInvoker;
12typedef ScriptInvokerBase<OnLogicPresenceChangedDelegate> OnLogicPresenceChangedInvoker;
13typedef ScriptInvokerBase<OnBaseRegisteredDelegate> OnBaseRegisteredInvoker;
14typedef ScriptInvokerBase<OnBaseUnregisteredDelegate> OnBaseUnregisteredInvoker;
15
16//------------------------------------------------------------------------------------------------
18{
19 override static void InitInfo(WorldSystemInfo outInfo)
20 {
21 outInfo
22 .SetAbstract(false)
23 .AddPoint(ESystemPoint.FixedFrame);
24 }
25
26 protected ref array<SCR_MilitaryBaseComponent> m_aBases = {};
27 protected ref array<SCR_MilitaryBaseLogicComponent> m_aLogicComponents = {};
28 protected ref array<int> m_aAvailableCallsignIds;
29
35
36 //------------------------------------------------------------------------------------------------
38 {
39 World world = GetGame().GetWorld();
40
41 if (!world)
42 return null;
43
44 return SCR_MilitaryBaseSystem.Cast(world.FindSystem(SCR_MilitaryBaseSystem));
45 }
46
47 //------------------------------------------------------------------------------------------------
55
56 //------------------------------------------------------------------------------------------------
64
65 //------------------------------------------------------------------------------------------------
66 override event protected void OnCleanup()
67 {
69
70 if (core)
71 core.Event_OnEntityTransformChanged.Remove(OnEntityTransformChanged);
72 }
73
74 //------------------------------------------------------------------------------------------------
75 void OnBaseFactionChanged(Faction faction, SCR_MilitaryBaseComponent base)
76 {
78 m_OnBaseFactionChanged.Invoke(base, faction);
79 }
80
81 //------------------------------------------------------------------------------------------------
82 void OnLogicRegisteredInBase(SCR_MilitaryBaseLogicComponent logic, SCR_MilitaryBaseComponent base)
83 {
85 m_OnLogicRegisteredInBase.Invoke(base, logic);
86 }
87
88 //------------------------------------------------------------------------------------------------
89 void OnLogicUnregisteredInBase(SCR_MilitaryBaseLogicComponent logic, SCR_MilitaryBaseComponent base)
90 {
92 m_OnLogicUnregisteredInBase.Invoke(base, logic);
93 }
94
95 //------------------------------------------------------------------------------------------------
103
104 //------------------------------------------------------------------------------------------------
112
113 //------------------------------------------------------------------------------------------------
121
122 //------------------------------------------------------------------------------------------------
123 override event protected void OnInit()
124 {
126
127 if (core)
128 core.Event_OnEntityTransformChanged.Insert(OnEntityTransformChanged);
129 }
130
131 //------------------------------------------------------------------------------------------------
133 protected void OnEntityTransformChanged(notnull SCR_EditableEntityComponent editComponent)
134 {
135 array<IEntity> queue = {editComponent.GetOwner()};
136 SCR_MilitaryBaseComponent baseComponent;
137 SCR_MilitaryBaseLogicComponent logicComponent;
138 IEntity processedEntity;
139 IEntity nextInHierarchy;
140 const bool checkeEditableEntityComponent; // TODO: check for good const usage
141
142 while (!queue.IsEmpty())
143 {
144 processedEntity = queue[0];
145 queue.Remove(0);
146
147 if (!processedEntity)
148 continue;
149
150 if (!checkeEditableEntityComponent || !SCR_EditableEntityComponent.Cast(processedEntity.FindComponent(SCR_EditableEntityComponent)))
151 {
152 baseComponent = SCR_MilitaryBaseComponent.Cast(processedEntity.FindComponent(SCR_MilitaryBaseComponent));
153 logicComponent = SCR_MilitaryBaseLogicComponent.Cast(processedEntity.FindComponent(SCR_MilitaryBaseLogicComponent));
154
155 if (baseComponent)
156 OnBaseTransformChanged(baseComponent);
157
158 if (logicComponent)
159 OnLogicTransformChanged(logicComponent);
160 }
161
162 nextInHierarchy = processedEntity.GetChildren();
163
164 while (nextInHierarchy)
165 {
166 queue.Insert(nextInHierarchy);
167 nextInHierarchy = nextInHierarchy.GetSibling();
168 }
169 }
170 }
171
172 //------------------------------------------------------------------------------------------------
174 protected void OnBaseTransformChanged(notnull SCR_MilitaryBaseComponent baseComponent)
175 {
176 vector position = baseComponent.GetOwner().GetOrigin();
177 int distanceLimit = baseComponent.GetRadius() * baseComponent.GetRadius();
178
179 foreach (SCR_MilitaryBaseLogicComponent logicComponent : m_aLogicComponents)
180 {
181 if (vector.DistanceSqXZ(position, logicComponent.GetOwner().GetOrigin()) > distanceLimit)
182 {
183 baseComponent.UnregisterLogicComponent(logicComponent);
184 logicComponent.UnregisterBase(baseComponent);
185 }
186 else
187 {
188 baseComponent.RegisterLogicComponent(logicComponent);
189 logicComponent.RegisterBase(baseComponent);
190 }
191 }
192 }
193
194 //------------------------------------------------------------------------------------------------
196 protected void OnLogicTransformChanged(notnull SCR_MilitaryBaseLogicComponent logicComponent)
197 {
198 vector position = logicComponent.GetOwner().GetOrigin();
199 int radius;
200
201 foreach (SCR_MilitaryBaseComponent baseComponent : m_aBases)
202 {
203 radius = baseComponent.GetRadius();
204
205 if (vector.DistanceSqXZ(position, baseComponent.GetOwner().GetOrigin()) > (radius * radius))
206 {
207 baseComponent.UnregisterLogicComponent(logicComponent);
208 logicComponent.UnregisterBase(baseComponent);
209 }
210 else
211 {
212 baseComponent.RegisterLogicComponent(logicComponent);
213 logicComponent.RegisterBase(baseComponent);
214 }
215 }
216 }
217
218 //------------------------------------------------------------------------------------------------
220 {
221 FactionManager fManager = GetGame().GetFactionManager();
222
223 if (!fManager)
224 return;
225
227 array<Faction> allFactions = {};
228 array<SCR_Faction> playableFactions = {};
229 fManager.GetFactionsList(allFactions);
230 array<int> callsignIndexesSource = {};
231
232 foreach (Faction faction : allFactions)
233 {
234 SCR_Faction factionCast = SCR_Faction.Cast(faction);
235
236 if (factionCast && factionCast.IsPlayable())
237 playableFactions.Insert(factionCast);
238 }
239
240 if (playableFactions.IsEmpty())
241 return;
242
243 // All playable factions need to have a callsign with the same ID registered in order for it to be used
244 // Grab the indexes available from the first valid faction in the list so we can compare it with the other factions
245 callsignIndexesSource = playableFactions[0].GetBaseCallsignIndexes();
246 bool canAdd;
247
248 foreach (int indexSource : callsignIndexesSource)
249 {
250 canAdd = true;
251
252 // Ignore the index unless it's registered in all other factions
253 foreach (int i, SCR_Faction faction : playableFactions)
254 {
255 // No need to check it against the original source faction
256 if (i == 0)
257 continue;
258
259 if (!faction.GetBaseCallsignByIndex(indexSource))
260 {
261 canAdd = false;
262 break;
263 }
264 }
265
266 if (!canAdd)
267 continue;
268
269 m_aAvailableCallsignIds.Insert(indexSource);
270 }
271 }
272
273 //------------------------------------------------------------------------------------------------
274 void RegisterBase(notnull SCR_MilitaryBaseComponent base)
275 {
276 if (m_aBases.Contains(base))
277 return; // dont register same base more than once
278
279 m_aBases.Insert(base);
280
281 vector basePosition = base.GetOwner().GetOrigin();
282 int baseRadius = base.GetRadius();
283 int distanceLimit = baseRadius * baseRadius;
284
285 if (Replication.IsServer())
286 {
287 // If not done already, grab all available callsign IDs
290
291 // Assign random available callsign
293 {
294 int i = m_aAvailableCallsignIds.GetRandomIndex();
295
296 base.SetCallsignIndexAutomatic(m_aAvailableCallsignIds[i]);
297 m_aAvailableCallsignIds.Remove(i);
298 }
299 }
300
301 foreach (SCR_MilitaryBaseLogicComponent component : m_aLogicComponents)
302 {
303 if (vector.DistanceSqXZ(component.GetOwner().GetOrigin(), basePosition) <= distanceLimit)
304 {
305 base.RegisterLogicComponent(component);
306 component.RegisterBase(base);
307 }
308 }
309
311 m_OnBaseRegistered.Invoke(base);
312 }
313
314 //------------------------------------------------------------------------------------------------
315 void UnregisterBase(notnull SCR_MilitaryBaseComponent base)
316 {
317 if (!m_aBases.Contains(base))
318 return;
319
321 m_aAvailableCallsignIds.Insert(base.GetCallsign());
322
323 m_aBases.RemoveItem(base);
324
327 }
328
329 //------------------------------------------------------------------------------------------------
330 int GetBases(notnull out array<SCR_MilitaryBaseComponent> bases)
331 {
332 return bases.Copy(m_aBases);
333 }
334
335 //------------------------------------------------------------------------------------------------
337 {
338 return m_aBases.Count();
339 }
340
341 //------------------------------------------------------------------------------------------------
342 void RegisterLogicComponent(notnull SCR_MilitaryBaseLogicComponent component)
343 {
344 if (m_aLogicComponents.Contains(component))
345 return; // dont register same component more than once
346
347 m_aLogicComponents.Insert(component);
348
349 vector position = component.GetOwner().GetOrigin();
350 int radius;
351
352 foreach (SCR_MilitaryBaseComponent base : m_aBases)
353 {
354 radius = base.GetRadius();
355
356 if (vector.DistanceSqXZ(base.GetOwner().GetOrigin(), position) <= (radius * radius))
357 {
358 base.RegisterLogicComponent(component);
359 component.RegisterBase(base);
360 }
361 }
362 }
363
364 //------------------------------------------------------------------------------------------------
365 void UnregisterLogicComponent(notnull SCR_MilitaryBaseLogicComponent component)
366 {
367 m_aLogicComponents.RemoveItem(component);
368 }
369}
ArmaReforgerScripted GetGame()
Definition game.c:1398
vector position
ScriptInvokerBase< OnBaseFactionChangedDelegate > OnBaseFactionChangedInvoker
func OnBaseFactionChangedDelegate
func OnBaseUnregisteredDelegate
func OnBaseRegisteredDelegate
ScriptInvokerBase< OnBaseRegisteredDelegate > OnBaseRegisteredInvoker
ScriptInvokerBase< OnBaseUnregisteredDelegate > OnBaseUnregisteredInvoker
ScriptInvokerBase< OnLogicPresenceChangedDelegate > OnLogicPresenceChangedInvoker
func OnLogicPresenceChangedDelegate
proto external Managed FindComponent(typename typeName)
proto external IEntity GetChildren()
proto external IEntity GetSibling()
Main replication API.
Definition Replication.c:14
bool IsPlayable()
void OnLogicUnregisteredInBase(SCR_MilitaryBaseLogicComponent logic, SCR_MilitaryBaseComponent base)
OnLogicPresenceChangedInvoker GetOnLogicUnregisteredInBase()
int GetBases(notnull out array< SCR_MilitaryBaseComponent > bases)
void OnBaseTransformChanged(notnull SCR_MilitaryBaseComponent baseComponent)
Base has been moved, recalculate members in radius.
OnBaseFactionChangedInvoker GetOnBaseFactionChanged()
ref OnBaseUnregisteredInvoker m_OnBaseUnregistered
OnBaseRegisteredInvoker GetOnBaseRegistered()
ref array< SCR_MilitaryBaseLogicComponent > m_aLogicComponents
void OnLogicTransformChanged(notnull SCR_MilitaryBaseLogicComponent logicComponent)
Logic has been moved, recalculate members in radius of bases.
void RegisterBase(notnull SCR_MilitaryBaseComponent base)
void UnregisterBase(notnull SCR_MilitaryBaseComponent base)
ref array< SCR_MilitaryBaseComponent > m_aBases
ref OnBaseRegisteredInvoker m_OnBaseRegistered
static SCR_MilitaryBaseSystem GetInstance()
OnBaseUnregisteredInvoker GetOnBaseUnregistered()
ref OnLogicPresenceChangedInvoker m_OnLogicUnregisteredInBase
ref OnBaseFactionChangedInvoker m_OnBaseFactionChanged
void OnEntityTransformChanged(notnull SCR_EditableEntityComponent editComponent)
When a base or logic component is moved, recalculate registered members.
void RegisterLogicComponent(notnull SCR_MilitaryBaseLogicComponent component)
void OnBaseFactionChanged(Faction faction, SCR_MilitaryBaseComponent base)
void UnregisterLogicComponent(notnull SCR_MilitaryBaseLogicComponent component)
ref array< int > m_aAvailableCallsignIds
OnLogicPresenceChangedInvoker GetOnLogicRegisteredInBase()
void OnLogicRegisteredInBase(SCR_MilitaryBaseLogicComponent logic, SCR_MilitaryBaseComponent base)
ref OnLogicPresenceChangedInvoker m_OnLogicRegisteredInBase
Definition World.c:16
Structure holding world system meta-information required by the engine.
WorldSystemPoint ESystemPoint
Definition gameLib.c:7