Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_AIAllocateActionsForDefendActivity.c
Go to the documentation of this file.
2{
3 static const string PORT_WAYPOINT = "WaypointIn";
4 static const string PORT_WAYPOINT_RELATED = "WaypointRelated";
5 static const string PORT_PRIORITY_LEVEL = "PriorityLevel";
6
8 protected bool m_bWaypointRelated;
9 protected float m_fPriorityLevel;
12 protected SCR_MailboxComponent m_Mailbox;
14
15 //------------------------------------------------------------------------------------------------
16 static override bool VisibleInPalette() {return true;}
17
18 //------------------------------------------------------------------------------------------------
19 override void OnInit(AIAgent owner)
20 {
21 m_groupOwner = SCR_AIGroup.Cast(owner);
22 if (!m_groupOwner)
23 {
24 m_groupOwner = SCR_AIGroup.Cast(owner.GetParentGroup());
25 if (!m_groupOwner)
26 {
27 SCR_AgentMustBeAIGroup(this, owner);
28 return;
29 }
30 }
32 m_Mailbox = m_Utility.m_Mailbox;
33 m_RelatedActivity = SCR_AIDefendActivity.Cast(m_Utility.GetCurrentAction());
34 }
35
36 //------------------------------------------------------------------------------------------------
37 override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
38 {
39 if (!m_groupOwner)
40 return ENodeResult.FAIL;
41 IEntity waypointEnt;
42
44 return ENodeResult.FAIL;
45
46 GetVariableIn(PORT_WAYPOINT,waypointEnt);
47 GetVariableIn(PORT_WAYPOINT_RELATED,m_bWaypointRelated);
48 GetVariableIn(PORT_PRIORITY_LEVEL,m_fPriorityLevel);
49
50 array<AIAgent> groupMembers = {};
51 m_groupOwner.GetAgents(groupMembers);
52 if (groupMembers.IsEmpty())
53 return ENodeResult.FAIL;
54
55 int numSAToOccupy;
56
57 // Preparation of turrets and actions array
58 m_RelatedWaypoint = SCR_DefendWaypoint.Cast(waypointEnt);
60 return NodeError(this, owner, "Defend Waypoint not provided to the node!");
61
62 SCR_DefendWaypointPreset defendPreset = m_RelatedWaypoint.GetCurrentDefendPreset();
63 if (!defendPreset)
64 return SCR_AIErrorMessages.NodeErrorDefendPreset(this, owner);
65
66 bool useTurrets = defendPreset.GetUseTurrets();
67 bool teleport = m_RelatedWaypoint.GetFastInit();
68
69 ref array<BaseCompartmentSlot> compartments = {};
70 ref array<AISmartActionComponent> smartActions = {};
71
72 if (useTurrets)
73 {
74 m_groupOwner.GetAllocatedCompartments(compartments);
75 for (int i = compartments.Count() - 1; i >= 0; i--)
76 {
77 if (!TurretCompartmentSlot.Cast(compartments[i]))
78 compartments.Remove(i);
79 }
80 }
81
82 float fraction = defendPreset.GetFractionOfSA();
83 if (fraction > 0)
84 {
85 m_groupOwner.GetAllocatedSmartActions(smartActions);
86 numSAToOccupy = Math.Round(fraction * smartActions.Count());
87 }
88
89 // Distribution of turrets, actions and sector defends (randomized)
90 for (int i = 0, max = groupMembers.Count(); i < max; i++)
91 {
92 int j = groupMembers.GetRandomIndex();
93 if (useTurrets && compartments.Count() > 0)
94 {
95 OccupyTurret(groupMembers[j], compartments[0], teleport);
96 compartments.Remove(0);
97 groupMembers.Remove(j);
98 continue;
99 }
100 if (numSAToOccupy > 0)
101 {
102 OccupySA(groupMembers[j], smartActions[0], teleport);
103 smartActions.Remove(0);
104 groupMembers.Remove(j);
105 numSAToOccupy--;
106 continue;
107 }
108 m_RelatedActivity.AddAgentToRadialCover(groupMembers[j]);
109 groupMembers.Remove(j);
110 }
111
112 // Remaining agents to defend sectors of Waypoint
113 if (m_RelatedActivity.GetRadialCoverAgentsCount() > 0)
114 {
115 m_RelatedActivity.AllocateAgentsToRadialCover();
116 m_RelatedActivity.ClearRadialCoverAgents();
117 };
118
119 return ENodeResult.SUCCESS;
120 }
121
122 //Sends order to agent, reserves compartment
123 protected bool OccupyTurret(AIAgent who, BaseCompartmentSlot slot, bool teleport)
124 {
125 IEntity vehicle = slot.GetVehicle();
126 if (!vehicle)
127 return false;
128
130 if (!vehicleUsageComp)
131 return false;
132
133 if (teleport)
134 {
135 IEntity entityToTeleport = who.GetControlledEntity();
136 if (!entityToTeleport)
137 return false;
138 CompartmentAccessComponent CAComponent = CompartmentAccessComponent.Cast(entityToTeleport.FindComponent(CompartmentAccessComponent));
139 if(!CAComponent.GetInVehicle(vehicle, slot, true, -1, ECloseDoorAfterActions.INVALID, false))
140 Print("Unable to teleport to Turret", LogLevel.WARNING); // might be still in the process of teleporting...
141 }
143 m_Mailbox.RequestBroadcast(getInMessage, who);
144 m_groupOwner.GetGroupUtilityComponent().AddUsableVehicle(vehicleUsageComp);
145
146 return true;
147 }
148
149 //Sends order to agent, reserves smart action
150 protected bool OccupySA(AIAgent who, AISmartActionComponent smartAction, bool teleport)
151 {
152 if (teleport)
153 {
154 vector originOfObject, positionToTeleport;
155 vector mat[4];
156
157 originOfObject = smartAction.GetOwner().GetOrigin();
158 positionToTeleport = originOfObject + smartAction.GetActionOffset();
159 BaseGameEntity entityToTeleport = BaseGameEntity.Cast(who.GetControlledEntity());
160 if (!entityToTeleport)
161 return false;
162 Math3D.MatrixIdentity4(mat);
163 mat[3] = positionToTeleport;
164
165 entityToTeleport.Teleport(mat); // teleporting entity
166 }
167
168 SCR_AISmartActionComponent smartActionComponent = SCR_AISmartActionComponent.Cast(smartAction);
169 if (!smartActionComponent)
170 return false;
171 smartActionComponent.ReserveAction(who);
173 m_Mailbox.RequestBroadcast(actionMessage, who);
174 return true;
175 }
176
177 //------------------------------------------------------------------------------------------------
178 protected static ref TStringArray s_aVarsIn = {
179 PORT_WAYPOINT,
180 PORT_WAYPOINT_RELATED,
181 PORT_PRIORITY_LEVEL
182 };
184 {
185 return s_aVarsIn;
186 }
187
188 //------------------------------------------------------------------------------------------------
189 static override string GetOnHoverDescription()
190 {
191 return "AllocateActionsForDefendActivity: Goes over all group members and alocates them either turret, smart action or sector defend.\n Works only inside defend activity under defend waypoint.";
192 }
193}
ENodeResult NodeError(Node node, AIAgent owner, string msg)
Error call to be used in scripted BT nodes.
Definition NodeError.c:3
void SCR_AgentMustBeAIGroup(Node node, AIAgent owner)
Definition NodeError.c:14
EAICompartmentType
proto external Managed FindComponent(typename typeName)
proto external vector GetOrigin()
Definition Math.c:13
proto bool GetVariableIn(string name, out void val)
bool OccupySA(AIAgent who, AISmartActionComponent smartAction, bool teleport)
bool OccupyTurret(AIAgent who, BaseCompartmentSlot slot, bool teleport)
override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
static SCR_AIVehicleUsageComponent FindOnNearestParent(notnull IEntity ent, out IEntity componentOwner)
proto external GenericEntity GetOwner()
Get owner entity.
ENodeResult
Definition ENodeResult.c:13
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
array< string > TStringArray
Definition Types.c:385
ECloseDoorAfterActions