13 protected ref array<SCR_MilitaryBaseComponent> m_aBases = {};
14 protected ref array<SCR_MilitaryBaseLogicComponent> m_aLogicComponents = {};
15 protected ref array<int> m_aAvailableCallsignIds;
24 World world =
GetGame().GetWorld();
33 override event protected void OnCleanup()
38 core.Event_OnEntityTransformChanged.Remove(OnEntityTransformChanged);
42 void OnBaseFactionChanged(
Faction faction, SCR_MilitaryBaseComponent base)
44 if (m_OnBaseFactionChanged)
45 m_OnBaseFactionChanged.Invoke(base, faction);
49 void OnLogicRegisteredInBase(SCR_MilitaryBaseLogicComponent logic, SCR_MilitaryBaseComponent base)
51 if (m_OnLogicRegisteredInBase)
52 m_OnLogicRegisteredInBase.Invoke(base, logic);
56 void OnLogicUnregisteredInBase(SCR_MilitaryBaseLogicComponent logic, SCR_MilitaryBaseComponent base)
58 if (m_OnLogicUnregisteredInBase)
59 m_OnLogicUnregisteredInBase.Invoke(base, logic);
65 if (!m_OnBaseFactionChanged)
68 return m_OnBaseFactionChanged;
74 if (!m_OnLogicRegisteredInBase)
77 return m_OnLogicRegisteredInBase;
83 if (!m_OnLogicUnregisteredInBase)
86 return m_OnLogicUnregisteredInBase;
90 protected void SetupInvokers()
95 core.Event_OnEntityTransformChanged.Insert(OnEntityTransformChanged);
102 array<IEntity> queue = {editComponent.GetOwner()};
103 SCR_MilitaryBaseComponent baseComponent;
104 SCR_MilitaryBaseLogicComponent logicComponent;
105 IEntity processedEntity;
106 IEntity nextInHierarchy;
107 bool checkeEditableEntityComponent;
109 while (!queue.IsEmpty())
111 processedEntity = queue[0];
116 baseComponent = SCR_MilitaryBaseComponent.Cast(processedEntity.FindComponent(SCR_MilitaryBaseComponent));
117 logicComponent = SCR_MilitaryBaseLogicComponent.Cast(processedEntity.FindComponent(SCR_MilitaryBaseLogicComponent));
120 OnBaseTransformChanged(baseComponent);
123 OnLogicTransformChanged(logicComponent);
126 nextInHierarchy = processedEntity.GetChildren();
128 while (nextInHierarchy)
130 queue.Insert(nextInHierarchy);
131 nextInHierarchy = nextInHierarchy.GetSibling();
138 protected void OnBaseTransformChanged(notnull SCR_MilitaryBaseComponent baseComponent)
140 vector
position = baseComponent.GetOwner().GetOrigin();
141 int distanceLimit = baseComponent.GetRadius() * baseComponent.GetRadius();
143 foreach (SCR_MilitaryBaseLogicComponent logicComponent : m_aLogicComponents)
145 if (vector.DistanceSqXZ(
position, logicComponent.GetOwner().GetOrigin()) > distanceLimit)
147 baseComponent.UnregisterLogicComponent(logicComponent);
148 logicComponent.UnregisterBase(baseComponent);
152 baseComponent.RegisterLogicComponent(logicComponent);
153 logicComponent.RegisterBase(baseComponent);
160 protected void OnLogicTransformChanged(notnull SCR_MilitaryBaseLogicComponent logicComponent)
162 vector
position = logicComponent.GetOwner().GetOrigin();
165 foreach (SCR_MilitaryBaseComponent baseComponent :
m_aBases)
167 radius = baseComponent.GetRadius();
169 if (vector.DistanceSqXZ(
position, baseComponent.GetOwner().GetOrigin()) > (radius * radius))
171 baseComponent.UnregisterLogicComponent(logicComponent);
172 logicComponent.UnregisterBase(baseComponent);
176 baseComponent.RegisterLogicComponent(logicComponent);
177 logicComponent.RegisterBase(baseComponent);
183 void LoadAvailableCallsigns()
185 FactionManager fManager =
GetGame().GetFactionManager();
190 m_aAvailableCallsignIds = {};
191 array<Faction> allFactions = {};
192 array<SCR_Faction> playableFactions = {};
193 fManager.GetFactionsList(allFactions);
194 array<int> callsignIndexesSource = {};
196 foreach (
Faction faction : allFactions)
200 if (factionCast && factionCast.IsPlayable())
201 playableFactions.Insert(factionCast);
204 if (playableFactions.IsEmpty())
209 callsignIndexesSource = playableFactions[0].GetBaseCallsignIndexes();
212 foreach (
int indexSource : callsignIndexesSource)
217 foreach (
int i,
SCR_Faction faction : playableFactions)
223 if (!faction.GetBaseCallsignByIndex(indexSource))
233 m_aAvailableCallsignIds.Insert(indexSource);
238 void RegisterBase(notnull SCR_MilitaryBaseComponent base)
242 vector basePosition = base.GetOwner().GetOrigin();
243 int baseRadius = base.GetRadius();
244 int distanceLimit = baseRadius * baseRadius;
246 if (Replication.IsServer())
249 if (!m_aAvailableCallsignIds)
250 LoadAvailableCallsigns();
253 if (m_aAvailableCallsignIds && !m_aAvailableCallsignIds.IsEmpty())
255 int i = m_aAvailableCallsignIds.GetRandomIndex();
257 base.SetCallsignIndexAutomatic(m_aAvailableCallsignIds[i]);
258 m_aAvailableCallsignIds.Remove(i);
262 foreach (SCR_MilitaryBaseLogicComponent component : m_aLogicComponents)
264 if (vector.DistanceSqXZ(component.GetOwner().GetOrigin(), basePosition) <= distanceLimit)
266 base.RegisterLogicComponent(component);
267 component.RegisterBase(base);
273 void UnregisterBase(notnull SCR_MilitaryBaseComponent base)
275 if (m_aAvailableCallsignIds)
276 m_aAvailableCallsignIds.Insert(base.GetCallsign());
282 int GetBases(notnull out array<SCR_MilitaryBaseComponent> bases)
294 void RegisterLogicComponent(notnull SCR_MilitaryBaseLogicComponent component)
296 m_aLogicComponents.Insert(component);
298 vector
position = component.GetOwner().GetOrigin();
301 foreach (SCR_MilitaryBaseComponent base :
m_aBases)
303 radius = base.GetRadius();
305 if (vector.DistanceSqXZ(base.GetOwner().GetOrigin(),
position) <= (radius * radius))
307 base.RegisterLogicComponent(component);
308 component.RegisterBase(base);
314 void UnregisterLogicComponent(notnull SCR_MilitaryBaseLogicComponent component)
316 m_aLogicComponents.RemoveItem(component);