Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_AmbientPatrolSpawnPointComponent.c
Go to the documentation of this file.
2{
3 [Attribute("{35BD6541CBB8AC08}Prefabs/AI/Waypoints/AIWaypoint_Cycle.et", UIWidgets.ResourceNamePicker, "Cycle waypoint to be used for waypoints in hierarchy.", "et")]
5
6 [Attribute("{93291E72AC23930F}Prefabs/AI/Waypoints/AIWaypoint_Defend.et", UIWidgets.ResourceNamePicker, "Waypoint to be used if no waypoints are found in hierarchy.", "et")]
9 //------------------------------------------------------------------------------------------------
12 {
14 }
16 //------------------------------------------------------------------------------------------------
19 {
21 }
22}
23
24class 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("-1", desc: "Overrides the system’s spawn distance for this spawn point (in meters). Use -1 to inherit the default. Value is clamped by min/max spawn distances.")]
34
35 [Attribute("-1", desc: "Overrides the system’s despawn distance for this spawn point (in meters). Use -1 to inherit the default. Value is clamped by min/max despawn distances.")]
37
38 [Attribute("0", UIWidgets.EditBox, "How often will the group respawn. (seconds, 0 = no respawn)", "0 inf 1")]
39 protected int m_iRespawnPeriod;
40
41 [Attribute("0.95", desc: "If (CurrentAIs / AILimit) > this value, the group will not be spawned.", params: "0 0.95 0.01")]
42 protected float m_fAILimitThreshold;
43
44 protected bool m_bSpawned;
45 protected bool m_bPaused;
46 protected bool m_bGroupActive;
47 protected int m_iMembersAlive = -1; //How many were alive during despawn to respawn again later
48 protected AIWaypoint m_Waypoint;
54
55 //------------------------------------------------------------------------------------------------
60
61 //------------------------------------------------------------------------------------------------
66
67 //------------------------------------------------------------------------------------------------
69 void SetMembersAlive(int count)
70 {
71 m_iMembersAlive = count;
72 }
73
74 //------------------------------------------------------------------------------------------------
77 {
78 return m_iMembersAlive;
79 }
80
81 //------------------------------------------------------------------------------------------------
83 void SetIsSpawned(bool spawned)
84 {
85 m_bSpawned = spawned;
86 }
87
88 //------------------------------------------------------------------------------------------------
91 {
92 return m_bSpawned;
93 }
94
95 //------------------------------------------------------------------------------------------------
98 void SetIsPaused(bool paused)
99 {
100 m_bPaused = paused;
101 }
102
103 //------------------------------------------------------------------------------------------------
106 {
107 return m_bPaused;
108 }
109
110 //------------------------------------------------------------------------------------------------
113 {
114 return m_fAILimitThreshold;
115 }
116
117 //------------------------------------------------------------------------------------------------
120 {
121 m_DespawnTimestamp = time;
122 }
123
124 //------------------------------------------------------------------------------------------------
130
131 //------------------------------------------------------------------------------------------------
134 {
135 m_RespawnTimestamp = timestamp;
136 }
137
138 //------------------------------------------------------------------------------------------------
144
145 //------------------------------------------------------------------------------------------------
148 {
149 return m_Group;
150 }
151
152 //------------------------------------------------------------------------------------------------
154 {
155 m_Group = group;
156
157 if (m_iRespawnPeriod != 0)
158 m_Group.GetOnAgentRemoved().Insert(OnAgentRemoved);
159 }
160
161 //------------------------------------------------------------------------------------------------
163 AIWaypoint GetWaypoint()
164 {
165 return m_Waypoint;
166 }
167
168 //------------------------------------------------------------------------------------------------
169 void SetWaypoint(AIWaypoint wp)
170 {
171 m_Waypoint = wp;
172 }
173
174 //------------------------------------------------------------------------------------------------
175 protected ResourceName GetRandomPrefabByProbability(notnull SCR_EntityCatalog entityCatalog, notnull array<SCR_EntityCatalogEntry> data)
176 {
177 float highestProbability;
178 array<ResourceName> prefabs = {};
179 array<int> eligiblePrefabIds = {};
180 array<float> probabilities = {};
181 SCR_EntityCatalogEntry catalogEntry;
183 float probability;
184
185 for (int i = 0, count = data.Count(); i < count; i++)
186 {
187 int catalogIndex = data[i].GetCatalogIndex();
188 catalogEntry = entityCatalog.GetCatalogEntry(catalogIndex);
189
190 if (!catalogEntry)
191 continue;
192
194
195 if (!patrolData)
196 continue;
197
198 probability = patrolData.GetProbabilityOfPresence();
199
200 prefabs.Insert(catalogEntry.GetPrefab());
201 probabilities.Insert(probability);
202
203 if (probability > highestProbability)
204 highestProbability = probability
205 }
206
207 if (prefabs.IsEmpty())
208 return ResourceName.Empty;
209
210 float rand = Math.RandomFloat(0, highestProbability);
211
212 for (int i = 0, count = probabilities.Count(); i < count; i++)
213 {
214 if (probabilities[i] >= rand)
215 eligiblePrefabIds.Insert(i);
216 }
217
218 if (eligiblePrefabIds.IsEmpty())
219 return ResourceName.Empty;
220
221 return prefabs[eligiblePrefabIds.GetRandomElement()];
222 }
223
224 //------------------------------------------------------------------------------------------------
225 protected void Update(SCR_Faction faction)
226 {
227 if (!m_Waypoint)
229
230 m_SavedFaction = faction;
231 if (!faction)
232 return;
233
235 if (!entityCatalog)
236 return;
237
238 array<SCR_EntityCatalogEntry> data = {};
239 entityCatalog.GetEntityListWithData(SCR_EntityCatalogAmbientPatrolData, data);
240
242 {
244 return;
245 }
246
247 SCR_EntityCatalogEntry catalogEntry;
249
250 for (int i = 0, count = data.Count(); i < count; i++)
251 {
252 int catalogIndex = data[i].GetCatalogIndex();
253 catalogEntry = entityCatalog.GetCatalogEntry(catalogIndex);
254 if (!catalogEntry)
255 continue;
256
258 if (!patrolData)
259 continue;
260
261 if (patrolData.GetGroupType() != m_eGroupType)
262 continue;
263
264 m_sPrefab = catalogEntry.GetPrefab();
265 break;
266 }
267 }
268
269 //------------------------------------------------------------------------------------------------
270 protected AIWaypointCycle SpawnCycleWaypoint(notnull EntitySpawnParams params)
271 {
272 array<AIWaypoint> waypoints = {};
273 array<IEntity> queue = {GetOwner()};
274 AIWaypoint waypoint;
275 IEntity processedEntity;
276 IEntity nextInHierarchy;
277
278 while (!queue.IsEmpty())
279 {
280 processedEntity = queue[0];
281 queue.Remove(0);
282
283 waypoint = AIWaypoint.Cast(processedEntity);
284
285 if (waypoint)
286 waypoints.Insert(waypoint);
287
288 nextInHierarchy = processedEntity.GetChildren();
289
290 while (nextInHierarchy)
291 {
292 queue.Insert(nextInHierarchy);
293 nextInHierarchy = nextInHierarchy.GetSibling();
294 }
295 }
296
297 if (waypoints.IsEmpty())
298 return null;
299
300 if (waypoints.Count() == 1)
301 {
302 m_Waypoint = waypoints[0];
303 return null;
304 }
305
307 if (!componentData)
308 return null;
309
310 AIWaypointCycle wp = AIWaypointCycle.Cast(GetGame().SpawnEntityPrefabEx(componentData.GetCycleWaypointPrefab(), false, params: params));
311 if (!wp)
312 return null;
313
314 wp.SetWaypoints(waypoints);
315
316 return wp;
317 }
318
319 //------------------------------------------------------------------------------------------------
322 {
324 params.TransformMode = ETransformMode.WORLD;
325 params.Transform[3] = GetOwner().GetOrigin();
326
327 AIWaypointCycle predefinedWaypoint = SpawnCycleWaypoint(params);
328 if (predefinedWaypoint)
329 {
330 m_Waypoint = predefinedWaypoint;
331 return;
332 }
333 else if (m_Waypoint)
334 {
335 return;
336 }
337
339 if (!componentData)
340 return;
341
342 AIWaypoint wp = AIWaypoint.Cast(GetGame().SpawnEntityPrefabEx(componentData.GetDefaultWaypointPrefab(), false, params: params));
343 if (!wp)
344 return;
345
346 m_Waypoint = wp;
347 }
348
349 //------------------------------------------------------------------------------------------------
352 {
354 if (!comp)
355 return;
356
357 SCR_Faction faction = SCR_Faction.Cast(comp.GetAffiliatedFaction());
358 if (!faction)
359 faction = SCR_Faction.Cast(comp.GetDefaultAffiliatedFaction());
360
361 if (faction != m_SavedFaction || m_iRespawnPeriod > 0)
362 Update(faction);
363
364 m_bSpawned = true;
365 m_bGroupActive = true;
366
367 if (m_sPrefab.IsEmpty())
368 return;
369
371 params.TransformMode = ETransformMode.WORLD;
372 params.Transform[3] = GetOwner().GetOrigin();
373
374 if (m_iRespawnPeriod == 0 && m_Waypoint && Math.RandomFloat01() >= 0.5)
375 {
376 AIWaypointCycle cycleWP = AIWaypointCycle.Cast(m_Waypoint);
377 if (cycleWP)
378 {
379 array<AIWaypoint> waypoints = {};
380 cycleWP.GetWaypoints(waypoints);
381 params.Transform[3] = waypoints.GetRandomElement().GetOrigin();
382 }
383 }
384
386 if (!m_Group)
387 return;
388
390
391 if (!m_Group.GetSpawnImmediately())
392 {
393 if (m_iMembersAlive > 0)
394 m_Group.SetMaxUnitsToSpawn(m_iMembersAlive);
395
396 m_Group.SpawnUnits();
397 }
398
399 m_Group.AddWaypoint(m_Waypoint);
400 }
401
402 //------------------------------------------------------------------------------------------------
405 {
406 m_DespawnTimestamp = null;
407 m_bSpawned = false;
408
409 if (!m_Group)
410 {
411 m_iMembersAlive = 0;
412 return;
413 }
414
415 array<AIAgent> units = {};
416 m_Group.GetAgents(units);
417 int count = m_Group.GetAgentsCount();
418 m_iMembersAlive = count;
419 RplComponent.DeleteRplEntity(m_Group, false);
420 }
421
422 //------------------------------------------------------------------------------------------------
425 {
426 if (m_Group)
427 {
428 m_bGroupActive = true;
429 m_Group.ActivateAllMembers();
430 }
431 }
432 //------------------------------------------------------------------------------------------------
435 {
436 if (m_Group)
437 {
438 m_bGroupActive = false;
439 m_Group.DeactivateAllMembers();
440 }
441 }
442 //------------------------------------------------------------------------------------------------
446 {
447 return m_bGroupActive;
448 }
449
450 //------------------------------------------------------------------------------------------------
452 {
453 if (!m_Group || m_Group.GetAgentsCount() > 0)
454 return;
455
456 ChimeraWorld world = GetOwner().GetWorld();
457 if (m_RespawnTimestamp.GreaterEqual(world.GetServerTimestamp()))
458 return;
459
460 // Set up respawn timestamp, convert s to ms, reset original group size
461 m_RespawnTimestamp = world.GetServerTimestamp().PlusSeconds(m_iRespawnPeriod);
462 m_iMembersAlive = -1;
463 m_bSpawned = false;
464 }
465
466 //------------------------------------------------------------------------------------------------
467 override void OnPostInit(IEntity owner)
468 {
470 if (!factionComponent)
471 {
472 Print("SCR_AmbientPatrolSpawnPointComponent: SCR_FactionAffiliationComponent not found on owner entity. Patrol spawning will not be available.", LogLevel.WARNING);
473 return;
474 }
475
477 if (manager)
478 manager.RegisterPatrol(this);
479 }
480
481 //------------------------------------------------------------------------------------------------
482 override void OnDelete(IEntity owner)
483 {
485 if (manager)
486 manager.UnregisterPatrol(this);
487 }
488}
EEntityCatalogType
IEntity SpawnEntityPrefabEx(ResourceName prefab, bool randomizeEditableVariant, BaseWorld world=null, EntitySpawnParams params=null)
Definition game.c:90
ArmaReforgerScripted GetGame()
Definition game.c:1398
SCR_AIGroupSettingsComponentClass m_Group
void OnAgentRemoved(AIGroup group, AIAgent agent)
WorldTimestamp m_RespawnTimestamp
SCR_AIGroup GetSpawnedGroup()
WorldTimestamp GetDespawnTimestamp()
void SetWaypoint(AIWaypoint wp)
void SetDespawnTimestamp(WorldTimestamp time)
void SetIsSpawned(bool spawned)
void SetRespawnTimestamp(WorldTimestamp timestamp)
void SetIsPaused(bool paused)
void SetMembersAlive(int count)
AIWaypointCycle SpawnCycleWaypoint(notnull EntitySpawnParams params)
ResourceName GetRandomPrefabByProbability(notnull SCR_EntityCatalog entityCatalog, notnull array< SCR_EntityCatalogEntry > data)
void SetspawnedGroup(SCR_AIGroup group)
WorldTimestamp GetRespawnTimestamp()
WorldTimestamp m_DespawnTimestamp
SCR_CharacterSoundComponentClass GetComponentData()
Get all prefabs that have the spawner data
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
proto external GenericComponent FindComponent(typename typeName)
proto external Managed FindComponent(typename typeName)
proto external vector GetOrigin()
proto external IEntity GetChildren()
proto external BaseWorld GetWorld()
proto external IEntity GetSibling()
Definition Math.c:13
void RegisterPatrol(notnull SCR_AmbientPatrolSpawnPointComponent patrol)
static SCR_AmbientPatrolSystem GetInstance()
void UnregisterPatrol(notnull SCR_AmbientPatrolSpawnPointComponent patrol)
Get prefab entity Data of type Ignores disabled Data s param dataType class of Data type you with to obtain return Entity Data of given type Null if not found *SCR_BaseEntityCatalogData GetEntityDataOfType(typename dataType)
Get Prefab data return Prefab data *ResourceName GetPrefab()
Get Catalog Entry of index Can ignores disabled entries param Index of entry within list return Catalog Entry Null if entry is disabled *SCR_EntityCatalogEntry GetCatalogEntry(int index)
SCR_EntityCatalog GetFactionEntityCatalogOfType(EEntityCatalogType catalogType, bool printNotFound=true)
proto external GenericEntity GetOwner()
Get owner entity.
void EntitySpawnParams()
Definition gameLib.c:130
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute