Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ScenarioFrameworkSlotTaskAI.c
Go to the documentation of this file.
1[EntityEditorProps(category: "GameScripted/ScenarioFramework/Slot", description: "")]
5
6class SCR_ScenarioFrameworkSlotTaskAI : SCR_ScenarioFrameworkSlotTask
7{
8 [Attribute(desc: "Waypoint Groups if applicable", category: "Waypoints")]
9 ref SCR_ScenarioFrameworkWaypointSet m_WaypointSet;
10
11 [Attribute(desc: "Spawn AI on the first WP Slot", defvalue: "1", category: "Waypoints")]
12 bool m_bSpawnAIOnWPPos;
13
14 [Attribute(desc: "Default waypoint if any WP group is defined", "{93291E72AC23930F}Prefabs/AI/Waypoints/AIWaypoint_Defend.et", category: "Waypoints")]
15 ResourceName m_sWPToSpawn;
16
17 [Attribute(defvalue: "0", desc: "Balancing group sizes based on number of players", category: "Balance")]
19
20 [Attribute(defvalue: "1", desc: "Least amount of AIs in the group after balancing occurs. Will not exceed maximum number of units defined in the group prefab.", category: "Balance")]
22
23 [Attribute(defvalue: "1", desc: "When enabled and AI enters a vehicle, whole AI group won't be despawned by the Dynamic Despawn feature until the whole group is outside the vehicle", category: "Activation")]
25
26 [Attribute(defvalue: SCR_EAIGroupFormation.Wedge.ToString(), UIWidgets.ComboBox, "AI group formation", "", ParamEnumArray.FromEnum(SCR_EAIGroupFormation), category: "Common")]
27 SCR_EAIGroupFormation m_eAIGroupFormation;
28
29 [Attribute(defvalue: EAISkill.REGULAR.ToString(), UIWidgets.ComboBox, "AI skill in combat", "", ParamEnumArray.FromEnum(EAISkill), category: "Common")]
30 EAISkill m_eAISkill;
31
32 [Attribute(defvalue: "1", uiwidget: UIWidgets.EditBox, desc: "Sets perception ability. Affects speed at which perception detects targets. Bigger value means proportionally faster detection.", params: "0 100 0.001", category: "Common")]
34
35 [Attribute(defvalue: "{000CD338713F2B5A}Prefabs/AI/Groups/Group_Base.et", category: "Common")]
37
38 ref array<AIWaypoint> m_aWaypoints = {};
40 ref array<ResourceName> m_aAIPrefabsForRemoval = {};
41
42 ref array<SCR_ScenarioFrameworkSlotWaypoint> m_aSlotWaypoints = {};
44
45 // Temporary solution as we don't know if invoker or a different method will initialize waypoints
48
49 //------------------------------------------------------------------------------------------------
51 void SetAIPrefabsForRemoval(array<ResourceName> arrayForRemoval)
52 {
53 m_aAIPrefabsForRemoval = arrayForRemoval;
54 }
55
56 //------------------------------------------------------------------------------------------------
58 array<ResourceName> GetAIPrefabsForRemoval()
59 {
61 }
62
63 //------------------------------------------------------------------------------------------------
68 override void RestoreToDefault(bool includeChildren = false, bool reinitAfterRestoration = false, bool affectRandomization = true, bool deleteSpawnedEntities = true)
69 {
70 m_aWaypoints.Clear();
71 m_AIGroup = null;
73 m_aSlotWaypoints.Clear();
76
77 super.RestoreToDefault(includeChildren, reinitAfterRestoration, affectRandomization, deleteSpawnedEntities);
78 }
79
80 //------------------------------------------------------------------------------------------------
84 {
86 if (!m_Entity && !SCR_StringHelper.IsEmptyOrWhiteSpace(m_sObjectToSpawn))
87 {
89 return;
90 }
91
94
96 return;
97
98 m_bInitiated = false;
100 if (m_Entity)
101 m_vPosition = m_Entity.GetOrigin();
102
103 if (m_AIGroup)
104 m_AIGroup.GetOnAgentRemoved().Remove(DecreaseAIGroupMemberCount);
105
106 m_AIGroup = null;
107
108 m_aSpawnedEntities.RemoveItem(null);
109 foreach (IEntity entity : m_aSpawnedEntities)
110 {
111 m_vPosition = entity.GetOrigin();
112 SCR_EntityHelper.DeleteEntityAndChildren(entity);
113 }
114
115 m_aSpawnedEntities.Clear();
116 m_aWaypoints.Clear();
117 }
118
119 //------------------------------------------------------------------------------------------------
120 protected void OnAgentCompartmentEntered(AIAgent agent, IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
121 {
123 return;
124
125 if (!agent || !Vehicle.Cast(targetEntity))
126 return;
127
129 }
130
131 //------------------------------------------------------------------------------------------------
132 protected void OnAgentCompartmentLeft(AIAgent agent, IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
133 {
135 return;
136
137 if (!agent || !Vehicle.Cast(targetEntity))
138 return;
139
140 array<AIAgent> agents = {};
141 m_AIGroup.GetAgents(agents);
142
143 bool isInVehicle;
144 foreach (AIAgent groupAgent : agents)
145 {
146 ChimeraCharacter character = ChimeraCharacter.Cast(groupAgent.GetControlledEntity());
147 if (character && character.IsInVehicle())
148 {
149 isInVehicle = true;
150 break;
151 }
152 }
153
154 if (!isInVehicle)
156 }
157
158 //------------------------------------------------------------------------------------------------
161 override bool InitOtherThings()
162 {
163 m_aWaypoints.Clear();
164 m_AIGroup = null;
168
169 if (m_eActivationType == SCR_ScenarioFrameworkEActivationType.SAME_AS_PARENT && m_WaypointSet && !m_WaypointSet.m_aLayerName.IsEmpty())
171
172 return super.InitOtherThings();
173 }
174
175 //------------------------------------------------------------------------------------------------
189
190 //------------------------------------------------------------------------------------------------
193 {
194 if (!m_bInitiated)
195 return;
196
197 foreach (SCR_ScenarioFrameworkPlugin plugin : m_aPlugins)
198 {
199 plugin.Init(this);
200 }
201
203
204 if (m_ParentLayer)
205 m_ParentLayer.CheckAllChildrenSpawned(this);
206
208 SetWaypointToAI(this);
209
211 }
212
213 //------------------------------------------------------------------------------------------------
216 {
217 m_bGroupWasNull = false;
219 if (!m_AIGroup)
220 m_bGroupWasNull = true;
221
222 if (!m_AIGroup && !CreateAIGroup())
223 {
225 return;
226 }
227
228 if (!m_aAIPrefabsForRemoval.IsEmpty())
229 {
230 foreach (ResourceName prefabToRemove : m_aAIPrefabsForRemoval)
231 {
232 for (int i = m_AIGroup.m_aUnitPrefabSlots.Count() - 1; i >= 0; i--)
233 {
234 if (m_AIGroup.m_aUnitPrefabSlots[i] != prefabToRemove)
235 continue;
236
237 m_AIGroup.m_aUnitPrefabSlots.Remove(i);
238 break;
239 }
240 }
241 }
243 {
244 int iMaxUnitsInGroup = m_AIGroup.m_aUnitPrefabSlots.Count();
245 float iUnitsToSpawn = Math.Map(GetPlayersCount(), 1, GetMaxPlayersForGameMode(), Math.RandomInt(1, 3), iMaxUnitsInGroup);
246
247 if (iUnitsToSpawn < m_iMinUnitsInGroup)
248 iUnitsToSpawn = m_iMinUnitsInGroup;
249
250 m_AIGroup.SetMaxUnitsToSpawn(iUnitsToSpawn);
251 }
252
253 m_AIGroup.GetOnAgentRemoved().Insert(DecreaseAIGroupMemberCount);
254
255 if (m_bGroupWasNull)
256 {
257 m_AIGroup.SetNumberOfMembersToSpawn(1);
258 OnAgentAdded(null);
259 return;
260 }
261
262 m_AIGroup.SetMemberSpawnDelay(200);
263 m_AIGroup.GetOnAgentAdded().Insert(OnAgentAdded);
264 m_AIGroup.SpawnUnits();
265 }
266
267 //------------------------------------------------------------------------------------------------
269 void OnAgentAdded(AIAgent child)
270 {
271 if (!m_AIGroup)
272 {
274 return;
275 }
276
277 if (m_AIGroup.GetNumberOfMembersToSpawn() != m_AIGroup.GetAgentsCount())
278 return;
279
280 m_AIGroup.GetOnAgentAdded().Remove(OnAgentAdded);
281
283
284 if (m_aSlotWaypoints.IsEmpty())
285 SetWaypointToAI(this);
286
288 }
289
290 //------------------------------------------------------------------------------------------------
292 protected void InitGroupComponents()
293 {
294 array<AIAgent> agents = {};
295 m_AIGroup.GetAgents(agents);
296
298 if (formComp)
299 {
300 formComp.SetFormation(SCR_Enum.GetEnumName(SCR_EAIGroupFormation, m_eAIGroupFormation));
301
302 AIFormationDefinition formDef = formComp.GetFormation();
303 if (formDef)
304 {
305 foreach (int i, AIAgent agent : agents)
306 {
307 IEntity agentEntity = agent.GetControlledEntity();
308 if (!agentEntity)
309 continue;
310
311 vector basePosition = m_vPosition;
312 if (basePosition == vector.Zero)
313 {
314 basePosition = m_AIGroup.GetOrigin();
315 // Set the Y-Level to the Slot height to allow mission makers to tune the spawn height.
316 basePosition[1] = GetOwner().GetOrigin()[1];
317 }
318 vector formationPosition = basePosition + formDef.GetOffsetPosition(i);
319
320 World world = agentEntity.GetWorld();
321 if (world)
322 {
323 float surfaceY = world.GetSurfaceY(formationPosition[0], formationPosition[2]);
324 float oceanY = world.GetOceanHeight(formationPosition[0], formationPosition[2]);
325 formationPosition[1] = Math.Max(Math.Max(oceanY, surfaceY), formationPosition[1]);
326 vector floorPosition;
327 if (SCR_EmptyPositionHelper.TryFindNearbyFloorPositionForEntity(world, agentEntity, formationPosition, floorPosition))
328 formationPosition = floorPosition;
329 }
330 agentEntity.SetOrigin(formationPosition);
331 agentEntity.Update();
332
333 SCR_AICombatComponent combatComponent = SCR_AICombatComponent.Cast(agentEntity.FindComponent(SCR_AICombatComponent));
334 if (combatComponent)
335 {
336 combatComponent.SetAISkill(m_eAISkill);
337 combatComponent.SetPerceptionFactor(m_fPerceptionFactor);
338 }
339
341 continue;
342
343 SCR_ChimeraAIAgent chimeraAgent = SCR_ChimeraAIAgent.Cast(agent);
344 if (!chimeraAgent)
345 continue;
346
347 SCR_AIInfoComponent info = chimeraAgent.m_InfoComponent;
348 if (!info)
349 continue;
350
353 }
354 }
355 }
356 else
357 {
358 foreach (AIAgent agent : agents)
359 {
360 IEntity agentEntity = agent.GetControlledEntity();
361 if (!agentEntity)
362 continue;
363
364 SCR_AICombatComponent combatComponent = SCR_AICombatComponent.Cast(agentEntity.FindComponent(SCR_AICombatComponent));
365 if (combatComponent)
366 {
367 combatComponent.SetAISkill(m_eAISkill);
368 combatComponent.SetPerceptionFactor(m_fPerceptionFactor);
369 }
370
372 continue;
373
374 SCR_ChimeraAIAgent chimeraAgent = SCR_ChimeraAIAgent.Cast(agent);
375 if (!chimeraAgent)
376 continue;
377
378 SCR_AIInfoComponent info = chimeraAgent.m_InfoComponent;
379 if (!info)
380 continue;
381
384 }
385 }
386 }
387
388 //------------------------------------------------------------------------------------------------
390 protected void InitWaypoints()
391 {
392 array<string> WaypointSetLayers = {};
393 WaypointSetLayers = m_WaypointSet.m_aLayerName;
394 array<SCR_ScenarioFrameworkLayerBase> layerBases = {};
395 array<SCR_ScenarioFrameworkLayerBase> layerChildren = {};
398
399 BaseWorld world = GetGame().GetWorld();
400 if (!world)
401 return;
402
403 foreach (string waypointSetLayer : WaypointSetLayers)
404 {
405 IEntity layerEntity = world.FindEntityByName(waypointSetLayer);
406 if (!layerEntity)
407 continue;
408
410 if (layerBase && !SCR_ScenarioFrameworkSlotWaypoint.Cast(layerBase))
411 {
412 if (layerBases.Contains(layerBase))
413 continue;
414
415 layerChildren = layerBase.GetChildrenEntities();
416 foreach (SCR_ScenarioFrameworkLayerBase child : layerChildren)
417 {
418 slotWaypoint = SCR_ScenarioFrameworkSlotWaypoint.Cast(child);
419 if (slotWaypoint && !m_aSlotWaypoints.Contains(slotWaypoint))
420 m_aSlotWaypoints.Insert(slotWaypoint);
421 }
422 }
423 else
424 {
426 if (slotWaypoint && !m_aSlotWaypoints.Contains(slotWaypoint))
427 m_aSlotWaypoints.Insert(slotWaypoint);
428 }
429 }
430
431 int waypointCount = m_aSlotWaypoints.Count();
433 {
434 if (waypoint.GetIsInitiated())
436 else
437 waypoint.GetOnAllChildrenSpawned().Insert(CheckWaypointsAfterInit);
438
439 if (waypointCount == m_iCurrentlySpawnedWaypoints)
441 }
442 }
443
444 //------------------------------------------------------------------------------------------------
448 {
450 if (slotWaypoint)
451 {
452 SCR_ScenarioFrameworkWaypointCycle cycleWP = SCR_ScenarioFrameworkWaypointCycle.Cast(slotWaypoint.m_Waypoint);
453 if (cycleWP)
454 {
455 if (!cycleWP.m_bInitiated)
456 {
458 return;
459 }
460 else if (cycleWP.m_OnAllWaypointsSpawned)
461 {
463 }
464 }
465 }
466
469 ProcessWaypoints(this);
470
471 layer.GetOnAllChildrenSpawned().Remove(CheckWaypointsAfterInit);
472 }
473
474 //------------------------------------------------------------------------------------------------
484
485 //------------------------------------------------------------------------------------------------
488 {
491
492 if (!m_Entity)
493 {
494 Print(string.Format("ScenarioFramework: Trying to add waypoints to non existing entity! Did you select the object to spawn for %1?", GetOwner().GetName()), LogLevel.ERROR);
496 return;
497 }
498
499 if (m_aSlotWaypoints.IsEmpty())
500 {
502 }
503 else
504 {
505 AIWaypoint waypoint;
507 {
508 waypoint = AIWaypoint.Cast(waypointSlot.GetSpawnedEntity());
509 if (waypoint)
510 m_aWaypoints.Insert(waypoint);
511 }
512 }
513
514 if (!m_AIGroup)
516
517 if (!m_AIGroup)
518 {
519 if (!CreateAIGroup())
520 {
522 return;
523 }
524 }
525
526 m_aWaypoints.RemoveItemOrdered(null);
527
528 foreach (AIWaypoint waypoint : m_aWaypoints)
529 {
530 m_AIGroup.AddWaypoint(waypoint);
531 }
532
533 if (m_aWaypoints.IsEmpty())
534 {
536 return;
537 }
538
539 if (m_vPosition != vector.Zero || !m_bSpawnAIOnWPPos || m_aWaypoints.IsEmpty())
540 return;
541
542 AIWaypoint waypoint = m_aWaypoints[0];
543 AIWaypointCycle cycleWaypoint = AIWaypointCycle.Cast(waypoint);
544 if (cycleWaypoint)
545 {
546 array<AIWaypoint> cycleWaypoints = {};
547 cycleWaypoint.GetWaypoints(cycleWaypoints);
548 if (!cycleWaypoints.IsEmpty() && cycleWaypoints[0] != null)
549 {
550 waypoint = cycleWaypoints[0];
551 }
552 else
553 {
555 return;
556 }
557 }
558
560 if (!formComp)
561 return;
562
563 AIFormationDefinition formDef = formComp.GetFormation();
564 if (!formDef)
565 return;
566
567 array<AIAgent> agents = {};
568 m_AIGroup.GetAgents(agents);
569
570 foreach (int i, AIAgent agent : agents)
571 {
572 IEntity agentEntity = agent.GetControlledEntity();
573 if (!agentEntity)
574 continue;
575
576 agentEntity.SetOrigin(waypoint.GetOrigin() + formDef.GetOffsetPosition(i));
577 }
578 }
579
580 //------------------------------------------------------------------------------------------------
583 protected AIWaypoint CreateDefaultWaypoint()
584 {
585 if (!m_Entity)
586 return null;
587
588 EntitySpawnParams spawnParams = new EntitySpawnParams();
589 spawnParams.TransformMode = ETransformMode.WORLD;
590 spawnParams.Transform[3] = m_Entity.GetOrigin();
591
592 Resource resWP = Resource.Load(m_sWPToSpawn);
593 if (!resWP || !resWP.IsValid())
594 return null;
595
596 AIWaypoint waypoint = AIWaypoint.Cast(GetGame().SpawnEntityPrefab(resWP, GetGame().GetWorld(), spawnParams));
597 if (!waypoint)
598 return null;
599
600 m_aWaypoints.Insert(waypoint);
601 return waypoint;
602 }
603
604 //------------------------------------------------------------------------------------------------
608 void DecreaseAIGroupMemberCount(SCR_AIGroup group, AIAgent agent)
609 {
610 if (group.GetAgentsCount() == 0)
611 {
613 {
615 SetIsTerminated(true);
616 }
617 else
618 {
619 SetIsTerminated(true);
620 }
621 }
622
623 IEntity agentEntity = agent.GetControlledEntity();
624 if (!agentEntity)
625 {
626 Print("ScenarioFramework: Decreasing AI member count failed due to not getting AI entity!", LogLevel.ERROR);
627 return;
628 }
629
630 EntityPrefabData prefabData = agentEntity.GetPrefabData();
631 if (!prefabData)
632 {
633 Print("ScenarioFramework: Decreasing AI member count failed due to not getting entity prefab data", LogLevel.ERROR);
634 return;
635 }
636
637 ResourceName resource = prefabData.GetPrefabName();
638 if (resource)
639 m_aAIPrefabsForRemoval.Insert(resource);
640 }
641
642 //------------------------------------------------------------------------------------------------
645 protected bool CreateAIGroup()
646 {
647 EntitySpawnParams paramsPatrol = new EntitySpawnParams();
648 paramsPatrol.TransformMode = ETransformMode.WORLD;
649
650 paramsPatrol.Transform[3] = m_Entity.GetOrigin();
651 Resource groupResource = Resource.Load(m_sGroupPrefab);
652 if (!groupResource.IsValid())
653 return false;
654
655 m_AIGroup = SCR_AIGroup.Cast(GetGame().SpawnEntityPrefab(groupResource, GetGame().GetWorld(), paramsPatrol));
656 if (!m_AIGroup)
657 return false;
658
659 FactionAffiliationComponent facComp = FactionAffiliationComponent.Cast(m_Entity.FindComponent(FactionAffiliationComponent));
660 if (!facComp)
661 return false;
662
663 m_AIGroup.SetFaction(facComp.GetAffiliatedFaction());
664 m_AIGroup.AddAIEntityToGroup(m_Entity);
665
666 if (m_vPosition != vector.Zero)
667 m_Entity.SetOrigin(m_vPosition);
668
669 return true;
670 }
671}
ArmaReforgerScripted GetGame()
Definition game.c:1398
LayerPresets layer
vector m_vPosition
enum EAITargetInfoCategory m_Entity
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
void OnAgentAdded()
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
ref array< IEntity > m_aSpawnedEntities
SCR_ScenarioFrameworkEActivationType m_eActivationType
bool m_bEnableRepeatedSpawn
SCR_ScenarioFrameworkLayerBase m_ParentLayer
ScriptInvokerScenarioFrameworkLayer GetOnAllChildrenSpawned()
bool m_bExcludeFromDynamicDespawn
void SetIsTerminated(bool state)
void InitActivationActions()
void AfterAllChildrenSpawned(SCR_ScenarioFrameworkLayerBase layer)
void SCR_ScenarioFrameworkLayerBase(IEntityComponentSource src, IEntity ent, IEntity parent)
ref array< ref SCR_ScenarioFrameworkPlugin > m_aPlugins
int GetMaxPlayersForGameMode(FactionKey factionName="")
void DynamicDespawn(SCR_ScenarioFrameworkLayerBase layer)
bool m_bDynamicallyDespawned
bool InitOtherThings()
For situations where some other logic is to be appended in these checks and is to be performed before...
void RestoreToDefault()
override void SetWaypointToAI(SCR_ScenarioFrameworkLayerBase layer)
void OnAgentCompartmentLeft(AIAgent agent, IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
ref array< SCR_ScenarioFrameworkSlotWaypoint > m_aSlotWaypoints
void OnAgentCompartmentEntered(AIAgent agent, IEntity targetEntity, BaseCompartmentManagerComponent manager, int mgrID, int slotID, bool move)
void AfterAllAgentsSpawned()
Initializes plugins, actions, and checks for parent layer spawn completion, then sets waypoints for A...
SCR_AIGroup m_AIGroup
void ProcessWaypoints(SCR_ScenarioFrameworkLayerBase layer)
void InitWaypoints()
Initializes waypoints by finding them in specified layers, adding them to list, and processing them a...
ref array< AIWaypoint > m_aWaypoints
ResourceName m_sGroupPrefab
void InitGroupComponents()
Initializes AI group components, sets formation, and combat parameters for agents in the group.
void CheckWaypointsAfterInit(SCR_ScenarioFrameworkLayerBase layer)
bool m_bBalanceOnPlayersCount
ref array< ResourceName > m_aAIPrefabsForRemoval
void DecreaseAIGroupMemberCount(SCR_AIGroup group, AIAgent agent)
bool m_bExcludeFromDynamicDespawnOnVehicleEntered
SCR_EAIGroupFormation m_eAIGroupFormation
array< ResourceName > GetAIPrefabsForRemoval()
AIWaypoint CreateDefaultWaypoint()
int m_iCurrentlySpawnedWaypoints
void SetAIPrefabsForRemoval(array< ResourceName > arrayForRemoval)
void SCR_ScenarioFrameworkSlotWaypoint(IEntityComponentSource src, IEntity ent, IEntity parent)
proto external Managed FindComponent(typename typeName)
proto external void SetOrigin(vector orig)
proto external vector GetOrigin()
proto external int Update()
proto external BaseWorld GetWorld()
proto external EntityPrefabData GetPrefabData()
Definition Math.c:13
Object holding reference to resource. In destructor release the resource.
Definition Resource.c:25
static void IgnoreSpawning(bool ignore)
ref ScriptInvokerBase< SCR_AIOnCompartmentLeft > m_OnCompartmentLeft
ref ScriptInvokerBase< SCR_AIOnCompartmentEntered > m_OnCompartmentEntered
static bool TryFindNearbyFloorPositionForEntity(BaseWorld world, IEntity entity, vector position=vector.Zero, out vector floorPos=vector.Zero)
static void InvokeSlotAISpawned(SCR_ScenarioFrameworkLayerBase layer, IEntity entity)
ScriptInvokerScenarioFrameworkLayer GetOnAllWaypointsSpawned()
static bool IsEmptyOrWhiteSpace(string input)
Definition World.c:16
enum EPhysicsLayerPresets Vehicle
Definition gameLib.c:24
void EntitySpawnParams()
Definition gameLib.c:130
void OnAgentAdded(AIAgent agent)
IEntity GetOwner()
Owner entity of the fuel tank.
proto external void ActivateAI()
Activates AI group, removes unwanted prefabs, balances units count, sets on agent remove and add even...
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