Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_AIFindMedic.c
Go to the documentation of this file.
2{
3 static const string PORT_REQUIREMENTS_IN = "RequirementsIn";
4 static const string PORT_ENTITY_IN = "EntityIn";
5 static const string PORT_GROUP_MEMBER_OUT = "GroupMemberOut";
6 static const string PORT_AGENTS_EXCLUDE_ARRAY = "AgentsExcludeArray";
7
8 //------------------------------------------------------------------------------------------------
9 static override bool VisibleInPalette() {return true;}
10
11
12 //------------------------------------------------------------------------------------------------
13 override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
14 {
15 SCR_AIGroupUtilityComponent groupUtilityComponent;
16 IEntity entityToHeal;
17 array<AIAgent> groupAgents = {};
18 array<AIAgent> agentsToExclude = {};
19 array<AIAgent> agentsOfHandler = {};
20 AIAgent medic;
21
22 AIGroup group = AIGroup.Cast(owner);
23 if (group)
24 {
25 groupUtilityComponent = SCR_AIGroupUtilityComponent.Cast(group.FindComponent(SCR_AIGroupUtilityComponent));
26 group.GetAgents(groupAgents);
27 }
28
29 if (!groupUtilityComponent || !groupUtilityComponent.m_aInfoComponents)
30 return ENodeResult.FAIL;
31
32 GetVariableIn(PORT_ENTITY_IN, entityToHeal);
33 if (!entityToHeal)
34 return ENodeResult.FAIL;
35 if (!GetVariableIn(PORT_AGENTS_EXCLUDE_ARRAY, agentsToExclude))
36 agentsToExclude = {};
37
38 ChimeraCharacter charToHeal = ChimeraCharacter.Cast(entityToHeal);
39 if (!charToHeal)
40 return ENodeResult.FAIL;
41 AIControlComponent contr = AIControlComponent.Cast(charToHeal.FindComponent(AIControlComponent));
42 if (!contr)
43 return ENodeResult.FAIL;
44 AIAgent agentToHeal = contr.GetControlAIAgent();
45 if (!agentToHeal)
46 return ENodeResult.FAIL;
47
48 // if entityToHeal is in vehicle, let's find a medic inside vehicle first
49 if (charToHeal.IsInVehicle())
50 {
51 // get entity to heal vehicle
52 int agentToHealHandler = groupUtilityComponent.m_GroupMovementComponent.GetAgentMoveHandlerId(agentToHeal);
53 if (agentToHealHandler < AIGroupMovementComponent.DEFAULT_HANDLER_ID)
54 Print("Healing agent not member of our group!", LogLevel.WARNING);
55 agentToHealHandler = Math.Max(AIGroupMovementComponent.DEFAULT_HANDLER_ID, agentToHealHandler);
56 groupUtilityComponent.m_GroupMovementComponent.GetAgentsInHandler(agentsOfHandler, agentToHealHandler);
57
58 // try to find a medic from same vehicle subgroup, that is not a driver
59 medic = FindAgentMedic(entityToHeal, agentsOfHandler, agentsToExclude, true);
60 if (medic)
61 {
62 SetVariableOut(PORT_GROUP_MEMBER_OUT,medic);
63 return ENodeResult.SUCCESS;
64 }
65 // try to find a medic from same vehicle subgroup, even driver
66 medic = FindAgentMedic(entityToHeal, agentsOfHandler, agentsToExclude, false);
67 if (medic)
68 {
69 SetVariableOut(PORT_GROUP_MEMBER_OUT,medic);
70 return ENodeResult.SUCCESS;
71 }
72 // look for any available medic within group
73 medic = FindAgentMedic(entityToHeal, groupAgents, agentsToExclude);
74 if (medic)
75 {
76 SetVariableOut(PORT_GROUP_MEMBER_OUT,medic);
77 return ENodeResult.SUCCESS;
78 }
79 else
80 return ENodeResult.FAIL;
81 }
82 // if entityToHeal is not in a vehicle
83 else
84 {
85 // look for a medic on ground first, in case some agents of group are in a vehicle
86 groupUtilityComponent.m_GroupMovementComponent.GetAgentsInHandler(agentsOfHandler, AIGroupMovementComponent.DEFAULT_HANDLER_ID);
87 medic = FindAgentMedic(entityToHeal, agentsOfHandler, agentsToExclude);
88 if (medic)
89 {
90 SetVariableOut(PORT_GROUP_MEMBER_OUT,medic);
91 return ENodeResult.SUCCESS;
92 }
93 // look for any available agent
94 else
95 {
96 medic = FindAgentMedic(entityToHeal, groupAgents, agentsToExclude);
97 if (medic)
98 {
99 SetVariableOut(PORT_GROUP_MEMBER_OUT,medic);
100 return ENodeResult.SUCCESS;
101 }
102 else
103 return ENodeResult.FAIL;
104 }
105 }
106
107 ClearVariable(PORT_GROUP_MEMBER_OUT);
108 return ENodeResult.FAIL;
109 }
110
111 //----------------------------------------------------------------------------------------------------------------------------------------------
112 protected AIAgent FindAgentMedic(IEntity entToHeal, notnull array<AIAgent> agents, notnull array<AIAgent> agentsToExclude, bool excludeDrivers = false)
113 {
114 AIAgent closestMedic = null;
115 float closestMedicDistance = float.MAX;
116
117 foreach (AIAgent agent : agents)
118 {
119 SCR_ChimeraAIAgent chimera = SCR_ChimeraAIAgent.Cast(agent);
120 SCR_AIInfoComponent info = chimera.m_InfoComponent;
121 if (!info)
122 continue;
123 if(!agent.GetControlledEntity())
124 continue;
125 if(agent.GetControlledEntity() == entToHeal)
126 continue;
127 if(agentsToExclude.Contains(agent))
128 continue;
129 if (info.HasUnitState(EUnitState.UNCONSCIOUS))
130 continue;
131 if (excludeDrivers)
132 {
133 if (info.HasUnitState(EUnitState.PILOT))
134 continue;
135 }
136 if (info.HasRole(EUnitRole.MEDIC) && info.GetAIState() == EUnitAIState.AVAILABLE)
137 {
138 float dist = vector.DistanceSq(agent.GetControlledEntity().GetOrigin(), entToHeal.GetOrigin());
139
140 if (dist < closestMedicDistance)
141 {
142 closestMedicDistance = dist;
143 closestMedic = agent;
144 }
145 }
146 }
147
148 return closestMedic;
149 }
150
151 //------------------------------------------------------------------------------------------------
152 protected static ref TStringArray s_aVarsOut = {
153 PORT_GROUP_MEMBER_OUT
154 };
156 {
157 return s_aVarsOut;
158 }
159
160 //------------------------------------------------------------------------------------------------
161 protected static ref TStringArray s_aVarsIn = {
162 PORT_REQUIREMENTS_IN,
163 PORT_ENTITY_IN,
164 PORT_AGENTS_EXCLUDE_ARRAY
165 };
167 {
168 return s_aVarsIn;
169 }
170
171 //------------------------------------------------------------------------------------------------
172 protected static override string GetOnHoverDescription()
173 {
174 return "Finds group member available for medic role";
175 }
176}
proto external vector GetOrigin()
Definition Math.c:13
proto void SetVariableOut(string name, void val)
proto bool GetVariableIn(string name, out void val)
bool VisibleInPalette()
proto void ClearVariable(string name)
override TStringArray GetVariablesIn()
static ref TStringArray s_aVarsIn
AIAgent FindAgentMedic(IEntity entToHeal, notnull array< AIAgent > agents, notnull array< AIAgent > agentsToExclude, bool excludeDrivers=false)
static override string GetOnHoverDescription()
static ref TStringArray s_aVarsOut
override TStringArray GetVariablesOut()
bool HasUnitState(EUnitState state)
bool HasRole(EUnitRole role)
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