Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AmbientPatrolSpawnPointComponent.c
Go to the documentation of this file.
1 class SCR_AmbientPatrolSpawnPointComponentClass : ScriptComponentClass
2 {
3  [Attribute("{35BD6541CBB8AC08}Prefabs/AI/Waypoints/AIWaypoint_Cycle.et", UIWidgets.ResourceNamePicker, "Cycle waypoint to be used for waypoints in hierarchy.", "et")]
4  protected ResourceName m_sCycleWaypointPrefab;
5 
6  [Attribute("{93291E72AC23930F}Prefabs/AI/Waypoints/AIWaypoint_Defend.et", UIWidgets.ResourceNamePicker, "Waypoint to be used if no waypoints are found in hierarchy.", "et")]
7  protected ResourceName m_sDefaultWaypointPrefab;
8 
9  //------------------------------------------------------------------------------------------------
11  ResourceName GetCycleWaypointPrefab()
12  {
14  }
15 
16  //------------------------------------------------------------------------------------------------
18  ResourceName GetDefaultWaypointPrefab()
19  {
21  }
22 }
23 
24 class SCR_AmbientPatrolSpawnPointComponent : ScriptComponent
25 {
26  [Attribute("0", UIWidgets.ComboBox, enums: ParamEnumArray.FromEnum(SCR_EGroupType))]
27  protected SCR_EGroupType m_eGroupType;
28 
29  [Attribute("0")]
30  protected bool m_bPickRandomGroupType;
31 
32  [Attribute("0", UIWidgets.EditBox, "How often will the group respawn. (seconds, 0 = no respawn)", "0 inf 1")]
33  protected int m_iRespawnPeriod;
34 
35  [Attribute("0.95", desc: "If (CurrentAIs / AILimit) > this value, the group will not be spawned.", params: "0 0.95 0.01")]
36  protected float m_fAILimitThreshold;
37 
38  protected bool m_bSpawned;
39  protected bool m_bActive = false;
40  protected bool m_bPaused;
41 
42  protected int m_iID;
43  protected int m_iMembersAlive = -1;
44 
45  protected WorldTimestamp m_fRespawnTimestamp;
46  protected WorldTimestamp m_fDespawnTimer;
47 
48  protected AIWaypoint m_Waypoint;
49 
50  protected ResourceName m_sPrefab;
51 
52  protected SCR_AIGroup m_Group;
53 
55 
56  //------------------------------------------------------------------------------------------------
58  void SetID(int ID)
59  {
60  m_iID = ID;
61  }
62 
63  //------------------------------------------------------------------------------------------------
65  int GetID()
66  {
67  return m_iID;
68  }
69 
70  //------------------------------------------------------------------------------------------------
72  void SetMembersAlive(int count)
73  {
74  m_iMembersAlive = count;
75  }
76 
77  //------------------------------------------------------------------------------------------------
80  {
81  return m_iMembersAlive;
82  }
83 
84  //------------------------------------------------------------------------------------------------
86  void SetIsSpawned(bool spawned)
87  {
88  m_bSpawned = spawned;
89  }
90 
91  //------------------------------------------------------------------------------------------------
93  bool GetIsSpawned()
94  {
95  return m_bSpawned;
96  }
97 
98  //------------------------------------------------------------------------------------------------
101  void SetIsPaused(bool paused)
102  {
103  m_bPaused = paused;
104  }
105 
106  //------------------------------------------------------------------------------------------------
108  bool GetIsPaused()
109  {
110  return m_bPaused;
111  }
112 
113  //------------------------------------------------------------------------------------------------
116  {
117  return m_fAILimitThreshold;
118  }
119 
120  //------------------------------------------------------------------------------------------------
122  void SetDespawnTimer(WorldTimestamp time)
123  {
124  m_fDespawnTimer = time;
125  }
126 
127  //------------------------------------------------------------------------------------------------
129  WorldTimestamp GetDespawnTimer()
130  {
131  return m_fDespawnTimer;
132  }
133 
134  //------------------------------------------------------------------------------------------------
136  void SetRespawnTimestamp(WorldTimestamp timestamp)
137  {
138  m_fRespawnTimestamp = timestamp;
139  }
140 
141  //------------------------------------------------------------------------------------------------
143  WorldTimestamp GetRespawnTimestamp()
144  {
145  return m_fRespawnTimestamp;
146  }
147 
148  //------------------------------------------------------------------------------------------------
151  {
152  return m_Group;
153  }
154 
155  //------------------------------------------------------------------------------------------------
157  AIWaypoint GetWaypoint()
158  {
159  return m_Waypoint;
160  }
161 
162  //------------------------------------------------------------------------------------------------
163  protected ResourceName GetRandomPrefabByProbability(notnull SCR_EntityCatalog entityCatalog, notnull array<SCR_EntityCatalogEntry> data)
164  {
165  float highestProbability;
166  array<ResourceName> prefabs = {};
167  array<int> eligiblePrefabIds = {};
168  array<float> probabilities = {};
169  SCR_EntityCatalogEntry catalogEntry;
171  float probability;
172 
173  for (int i = 0, count = data.Count(); i < count; i++)
174  {
175  int catalogIndex = data[i].GetCatalogIndex();
176  catalogEntry = entityCatalog.GetCatalogEntry(catalogIndex);
177 
178  if (!catalogEntry)
179  continue;
180 
181  patrolData = SCR_EntityCatalogAmbientPatrolData.Cast(catalogEntry.GetEntityDataOfType(SCR_EntityCatalogAmbientPatrolData));
182 
183  if (!patrolData)
184  continue;
185 
186  probability = patrolData.GetProbabilityOfPresence();
187 
188  prefabs.Insert(catalogEntry.GetPrefab());
189  probabilities.Insert(probability);
190 
191  if (probability > highestProbability)
192  highestProbability = probability
193  }
194 
195  if (prefabs.IsEmpty())
196  return ResourceName.Empty;
197 
198  Math.Randomize(-1);
199  float rand = Math.RandomFloat(0, highestProbability);
200 
201  for (int i = 0, count = probabilities.Count(); i < count; i++)
202  {
203  if (probabilities[i] >= rand)
204  eligiblePrefabIds.Insert(i);
205  }
206 
207  if (eligiblePrefabIds.IsEmpty())
208  return ResourceName.Empty;
209 
210  return prefabs[eligiblePrefabIds.GetRandomElement()];
211  }
212 
213  //------------------------------------------------------------------------------------------------
214  protected void Update(SCR_Faction faction)
215  {
216  if (!m_Waypoint)
218 
219  m_SavedFaction = faction;
220 
221  if (!faction)
222  return;
223 
224  SCR_EntityCatalog entityCatalog = faction.GetFactionEntityCatalogOfType(EEntityCatalogType.GROUP);
225 
226  if (!entityCatalog)
227  return;
228 
229  array<SCR_EntityCatalogEntry> data = {};
230  entityCatalog.GetEntityListWithData(SCR_EntityCatalogAmbientPatrolData, data);
231 
233  {
234  m_sPrefab = GetRandomPrefabByProbability(entityCatalog, data);
235  return;
236  }
237 
238  SCR_EntityCatalogEntry catalogEntry;
240 
241  for (int i = 0, count = data.Count(); i < count; i++)
242  {
243  int catalogIndex = data[i].GetCatalogIndex();
244  catalogEntry = entityCatalog.GetCatalogEntry(catalogIndex);
245 
246  if (!catalogEntry)
247  continue;
248 
249  patrolData = SCR_EntityCatalogAmbientPatrolData.Cast(catalogEntry.GetEntityDataOfType(SCR_EntityCatalogAmbientPatrolData));
250 
251  if (!patrolData)
252  continue;
253 
254  if (patrolData.GetGroupType() != m_eGroupType)
255  continue;
256 
257  m_sPrefab = catalogEntry.GetPrefab();
258  break;
259  }
260  }
261 
262  //------------------------------------------------------------------------------------------------
263  protected AIWaypointCycle SpawnCycleWaypoint(notnull EntitySpawnParams params)
264  {
265  array<AIWaypoint> waypoints = {};
266  array<IEntity> queue = {GetOwner()};
267  AIWaypoint waypoint;
268  IEntity processedEntity;
269  IEntity nextInHierarchy;
270 
271  while (!queue.IsEmpty())
272  {
273  processedEntity = queue[0];
274  queue.Remove(0);
275 
276  waypoint = AIWaypoint.Cast(processedEntity);
277 
278  if (waypoint)
279  waypoints.Insert(waypoint);
280 
281  nextInHierarchy = processedEntity.GetChildren();
282 
283  while (nextInHierarchy)
284  {
285  queue.Insert(nextInHierarchy);
286  nextInHierarchy = nextInHierarchy.GetSibling();
287  }
288  }
289 
290  if (waypoints.IsEmpty())
291  return null;
292 
293  if (waypoints.Count() == 1)
294  {
295  m_Waypoint = waypoints[0];
296  return null;
297  }
298 
300 
301  if (!componentData)
302  return null;
303 
304  Resource waypointResource = Resource.Load(componentData.GetCycleWaypointPrefab());
305 
306  if (!waypointResource || !waypointResource.IsValid())
307  return null;
308 
309  AIWaypointCycle wp = AIWaypointCycle.Cast(GetGame().SpawnEntityPrefab(waypointResource, null, params));
310 
311  if (!wp)
312  {
313  Print("SCR_AmbientPatrolSpawnPointComponent: AIWaypointCycle cast of m_sCycleWaypointPrefab has failed!", LogLevel.ERROR);
314  return null;
315  }
316 
317  wp.SetWaypoints(waypoints);
318 
319  return wp;
320  }
321 
322  //------------------------------------------------------------------------------------------------
325  {
326  EntitySpawnParams params = EntitySpawnParams();
327  params.TransformMode = ETransformMode.WORLD;
328  params.Transform[3] = GetOwner().GetOrigin();
329 
330  AIWaypointCycle predefinedWaypoint = SpawnCycleWaypoint(params);
331 
332  if (predefinedWaypoint)
333  {
334  m_Waypoint = predefinedWaypoint;
335  return;
336  }
337  else if (m_Waypoint)
338  {
339  return;
340  }
341 
343 
344  if (!componentData)
345  return;
346 
347  Resource waypointResource = Resource.Load(componentData.GetDefaultWaypointPrefab());
348 
349  if (!waypointResource || !waypointResource.IsValid())
350  return;
351 
352  AIWaypoint wp = AIWaypoint.Cast(GetGame().SpawnEntityPrefab(waypointResource, null, params));
353 
354  if (!wp)
355  return;
356 
357  m_Waypoint = wp;
358  }
359 
360  //------------------------------------------------------------------------------------------------
362  void SpawnPatrol()
363  {
365 
366  if (!comp)
367  return;
368 
369  SCR_Faction faction = SCR_Faction.Cast(comp.GetAffiliatedFaction());
370 
371  if (!faction)
372  faction = SCR_Faction.Cast(comp.GetDefaultAffiliatedFaction());
373 
374  if (faction != m_SavedFaction || m_iRespawnPeriod > 0)
375  Update(faction);
376 
377  m_bSpawned = true;
378  m_bActive = true;
379 
380  if (m_sPrefab.IsEmpty())
381  return;
382 
383  Resource prefab = Resource.Load(m_sPrefab);
384 
385  if (!prefab || !prefab.IsValid())
386  return;
387 
388  EntitySpawnParams params = EntitySpawnParams();
389  params.TransformMode = ETransformMode.WORLD;
390  params.Transform[3] = GetOwner().GetOrigin();
391  Math.Randomize(-1);
392 
393  if (m_iRespawnPeriod == 0 && m_Waypoint && Math.RandomFloat01() >= 0.5)
394  {
395  AIWaypointCycle cycleWP = AIWaypointCycle.Cast(m_Waypoint);
396 
397  if (cycleWP)
398  {
399  array<AIWaypoint> waypoints = {};
400  cycleWP.GetWaypoints(waypoints);
401  params.Transform[3] = waypoints.GetRandomElement().GetOrigin();
402  }
403  }
404 
405  m_Group = SCR_AIGroup.Cast(GetGame().SpawnEntityPrefab(prefab, null, params));
406 
407  if (!m_Group)
408  return;
409 
410  if (!m_Group.GetSpawnImmediately())
411  {
412  if (m_iMembersAlive > 0)
413  m_Group.SetMaxUnitsToSpawn(m_iMembersAlive);
414 
415  m_Group.SpawnUnits();
416  }
417 
418  m_Group.AddWaypoint(m_Waypoint);
419 
420  if (m_iRespawnPeriod != 0)
421  m_Group.GetOnAgentRemoved().Insert(OnAgentRemoved);
422  }
423 
424  //------------------------------------------------------------------------------------------------
427  {
428  m_fDespawnTimer = null;
429  m_bSpawned = false;
430 
431  if (!m_Group)
432  {
433  m_iMembersAlive = 0;
434  return;
435  }
436 
437  array<AIAgent> units = {};
438  m_Group.GetAgents(units);
439  int count = m_Group.GetAgentsCount();
440  m_iMembersAlive = count;
441  RplComponent.DeleteRplEntity(m_Group, false);
442  }
443 
444  //------------------------------------------------------------------------------------------------
447  {
448  if (m_Group)
449  {
450  m_bActive = true;
451  m_Group.ActivateAllMembers();
452  }
453  }
454  //------------------------------------------------------------------------------------------------
457  {
458  if (m_Group)
459  {
460  m_bActive = false;
461  m_Group.DeactivateAllMembers();
462  }
463  }
464  //------------------------------------------------------------------------------------------------
468  {
469  return m_bActive;
470  }
471  //------------------------------------------------------------------------------------------------
472 
474  {
475  if (!m_Group || m_Group.GetAgentsCount() > 0)
476  return;
477 
478  ChimeraWorld world = GetOwner().GetWorld();
479  if (m_fRespawnTimestamp.GreaterEqual(world.GetServerTimestamp()))
480  return;
481 
482  // Set up respawn timestamp, convert s to ms, reset original group size
483  m_fRespawnTimestamp = world.GetServerTimestamp().PlusSeconds(m_iRespawnPeriod);
484  m_iMembersAlive = -1;
485  m_bSpawned = false;
486  }
487 
488  //------------------------------------------------------------------------------------------------
489  override void OnPostInit(IEntity owner)
490  {
492 
493  if (!factionComponent)
494  {
495  Print("SCR_AmbientPatrolSpawnPointComponent: SCR_FactionAffiliationComponent not found on owner entity. Patrol spawning will not be available.", LogLevel.WARNING);
496  return;
497  }
498 
499  SetEventMask(owner, EntityEvent.INIT);
500  }
501 
502  //------------------------------------------------------------------------------------------------
503  override void EOnInit(IEntity owner)
504  {
505  SCR_AmbientPatrolSystem manager = SCR_AmbientPatrolSystem.GetInstance();
506 
507  if (!manager)
508  return;
509 
510  manager.RegisterPatrol(this);
511  }
512 
513  //------------------------------------------------------------------------------------------------
514  // destructor
516  {
517  SCR_AmbientPatrolSystem manager = SCR_AmbientPatrolSystem.GetInstance();
518 
519  if (!manager)
520  return;
521 
522  manager.UnregisterPatrol(this);
523  }
524 }
SpawnPatrol
void SpawnPatrol()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:362
ChimeraWorld
Definition: ChimeraWorld.c:12
SetIsPaused
void SetIsPaused(bool paused)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:101
SetIsSpawned
void SetIsSpawned(bool spawned)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:86
DespawnPatrol
void DespawnPatrol()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:426
GetAILimitThreshold
float GetAILimitThreshold()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:115
m_SavedFaction
protected Faction m_SavedFaction
Definition: SCR_AmbientPatrolSpawnPointComponent.c:54
m_fRespawnTimestamp
protected WorldTimestamp m_fRespawnTimestamp
Definition: SCR_AmbientPatrolSpawnPointComponent.c:45
ScriptComponent
SCR_SiteSlotEntityClass ScriptComponent
ActivateGroup
void ActivateGroup()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:446
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
GetMembersAlive
int GetMembersAlive()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:79
EEntityCatalogType
EEntityCatalogType
Definition: EEntityCatalogType.c:4
GetWaypoint
AIWaypoint GetWaypoint()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:157
SetID
void SetID(int ID)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:58
m_fDespawnTimer
protected WorldTimestamp m_fDespawnTimer
Definition: SCR_AmbientPatrolSpawnPointComponent.c:46
m_iID
protected int m_iID
Definition: SCR_AmbientPatrolSpawnPointComponent.c:42
SCR_AmbientPatrolSpawnPointComponentClass
Definition: SCR_AmbientPatrolSpawnPointComponent.c:1
m_Group
protected SCR_AIGroup m_Group
Definition: SCR_AmbientPatrolSpawnPointComponent.c:52
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
m_bActive
protected bool m_bActive
Definition: SCR_AmbientPatrolSpawnPointComponent.c:39
m_fAILimitThreshold
protected float m_fAILimitThreshold
Definition: SCR_AmbientPatrolSpawnPointComponent.c:36
GetSpawnedGroup
SCR_AIGroup GetSpawnedGroup()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:150
m_Waypoint
protected AIWaypoint m_Waypoint
Definition: SCR_AmbientPatrolSpawnPointComponent.c:48
GetIsPaused
bool GetIsPaused()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:108
GetRandomPrefabByProbability
protected ResourceName GetRandomPrefabByProbability(notnull SCR_EntityCatalog entityCatalog, notnull array< SCR_EntityCatalogEntry > data)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:163
GetID
int GetID()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:65
SetMembersAlive
void SetMembersAlive(int count)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:72
SCR_AmbientPatrolSystem
Definition: SCR_AmbientPatrolSystem.c:2
m_bSpawned
protected bool m_bSpawned
Definition: SCR_AmbientPatrolSpawnPointComponent.c:38
m_iRespawnPeriod
protected int m_iRespawnPeriod
Definition: SCR_AmbientPatrolSpawnPointComponent.c:33
OnPostInit
override void OnPostInit(IEntity owner)
Called on PostInit when all components are added.
Definition: SCR_AmbientPatrolSpawnPointComponent.c:489
SetDespawnTimer
void SetDespawnTimer(WorldTimestamp time)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:122
GetRespawnTimestamp
WorldTimestamp GetRespawnTimestamp()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:143
GetOwner
IEntity GetOwner()
Owner entity of the fuel tank.
Definition: SCR_FuelNode.c:128
EOnInit
override void EOnInit(IEntity owner)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:503
Faction
Definition: Faction.c:12
PrepareWaypoints
void PrepareWaypoints()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:324
SCR_EGroupType
SCR_EGroupType
Definition: SCR_EntityCatalogAmbientPatrolsData.c:25
SpawnCycleWaypoint
protected AIWaypointCycle SpawnCycleWaypoint(notnull EntitySpawnParams params)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:263
m_sCycleWaypointPrefab
protected ResourceName m_sCycleWaypointPrefab
Definition: SCR_AmbientPatrolSpawnPointComponent.c:3
SCR_EntityCatalog
Definition: SCR_EntityCatalog.c:181
Update
protected void Update(SCR_Faction faction)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:214
GetIsSpawned
bool GetIsSpawned()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:93
SCR_AIGroup
Definition: SCR_AIGroup.c:68
m_bPaused
protected bool m_bPaused
Definition: SCR_AmbientPatrolSpawnPointComponent.c:40
DeactivateGroup
void DeactivateGroup()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:456
~SCR_AmbientPatrolSpawnPointComponent
void ~SCR_AmbientPatrolSpawnPointComponent()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:515
m_iMembersAlive
protected int m_iMembersAlive
Definition: SCR_AmbientPatrolSpawnPointComponent.c:43
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24
SCR_FactionAffiliationComponent
Definition: SCR_FactionAffiliationComponent.c:10
m_bPickRandomGroupType
protected bool m_bPickRandomGroupType
Definition: SCR_AmbientPatrolSpawnPointComponent.c:30
SCR_EntityCatalogAmbientPatrolData
Definition: SCR_EntityCatalogAmbientPatrolsData.c:3
GetDespawnTimer
WorldTimestamp GetDespawnTimer()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:129
IsGroupActive
bool IsGroupActive()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:467
Attribute
SCR_AmbientPatrolSpawnPointComponentClass ScriptComponentClass Attribute("0", UIWidgets.ComboBox, enums:ParamEnumArray.FromEnum(SCR_EGroupType))] protected SCR_EGroupType m_eGroupType
OnAgentRemoved
void OnAgentRemoved()
Definition: SCR_AmbientPatrolSpawnPointComponent.c:473
SCR_Faction
Definition: SCR_Faction.c:6
SCR_EntityCatalogEntry
Definition: SCR_EntityCatalogEntry.c:5
m_sPrefab
protected ResourceName m_sPrefab
Definition: SCR_AmbientPatrolSpawnPointComponent.c:50
m_sDefaultWaypointPrefab
protected ResourceName m_sDefaultWaypointPrefab
Definition: SCR_AmbientPatrolSpawnPointComponent.c:6
SetRespawnTimestamp
void SetRespawnTimestamp(WorldTimestamp timestamp)
Definition: SCR_AmbientPatrolSpawnPointComponent.c:136