Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ChimeraAIAgent.c
Go to the documentation of this file.
2 {
3 };
4 
5 class SCR_ChimeraAIAgent : ChimeraAIAgent
6 {
7  // Current waypoint of our group
8  AIWaypoint m_GroupWaypoint;
9  SCR_AIUtilityComponent m_UtilityComponent;
10  SCR_AIInfoComponent m_InfoComponent;
11 
12  protected SCR_CharacterControllerComponent m_CharacterController;
13  protected FactionAffiliationComponent m_FactionAffiliationComponent;
14  protected int m_iPendingPlayerId;
15 
16  //------------------------------------------------------------------------------------------------
17  override void EOnInit(IEntity owner)
18  {
19  IEntity controlledEntity = GetControlledEntity();
20  if (!controlledEntity)
21  return;
22 
23  ChimeraCharacter character = ChimeraCharacter.Cast(controlledEntity);
24  if (character)
25  {
26  m_CharacterController = SCR_CharacterControllerComponent.Cast(character.GetCharacterController());
28  m_CharacterController.m_OnLifeStateChanged.Insert(OnLifeStateChanged);
29  }
30 
31  GetGame().GetCallqueue().CallLater(EnsureAILimit, 1, false);
32 
33  m_FactionAffiliationComponent = FactionAffiliationComponent.Cast(controlledEntity.FindComponent(FactionAffiliationComponent));
34  m_InfoComponent = SCR_AIInfoComponent.Cast(FindComponent(SCR_AIInfoComponent));
35  m_UtilityComponent = SCR_AIUtilityComponent.Cast(FindComponent(SCR_AIUtilityComponent));
36  }
37 
38  //------------------------------------------------------------------------------------------------
39  void ~SCR_ChimeraAIAgent()
40  {
42  m_CharacterController.m_OnLifeStateChanged.Remove(OnLifeStateChanged);
43  }
44 
45  //------------------------------------------------------------------------------------------------
46  void EnsureAILimit()
47  {
48  auto aiWorld = GetGame().GetAIWorld();
49  if (!aiWorld)
50  return;
51 
52  if (aiWorld.CanLimitedAIBeAdded())
53  return;
54 
55  IEntity controlledEntity = GetControlledEntity();
56  if (!EntityUtils.IsPlayer(controlledEntity) && !IsPlayerPending_S()) // Ensure that pending players can spawn
57  {
58  SCR_EntityHelper.DeleteEntityAndChildren(controlledEntity);
59  }
60  }
61 
62  //------------------------------------------------------------------------------------------------
63  protected void SendWoundedMsg()
64  {
66  if (!damageMgr)
67  return;
68 
69  if (!damageMgr.IsDamagedOverTime(EDamageType.BLEEDING))
70  return;
71 
72  SCR_AIGroup msgReceiverGroup = null;
73 
74  SCR_AIGroup myGroup = SCR_AIGroup.Cast(GetParentGroup());
75  if (myGroup)
76  {
77  SCR_AIGroup slaveGroup = myGroup.GetSlave();
78  if (slaveGroup)
79  msgReceiverGroup = slaveGroup; // Send to our slave group - this is the one which has AIs and will heal us
80  else
81  msgReceiverGroup = myGroup; // Send to our group
82  }
83 
84  if (!msgReceiverGroup)
85  return;
86 
87  // Inject message to the group mailbox directly.
88  // This bypasses problems of our own mailbox being disabled (because character is possessed by GM, or because we are unconscious)
89  AICommunicationComponent comms = msgReceiverGroup.GetCommunicationComponent();
90  if (!comms)
91  return;
92 
94  msg.SetReceiver(msgReceiverGroup);
95  comms.RequestBroadcast(msg, msgReceiverGroup);
96  }
97 
98  //------------------------------------------------------------------------------------------------
99  void OnLifeStateChanged(ECharacterLifeState previousLifeState, ECharacterLifeState newLifeState)
100  {
101  if (newLifeState == ECharacterLifeState.INCAPACITATED)
102  {
103  // first send message and then deactivate otherwise message won't be sent
104  SendWoundedMsg();
105  GetControlComponent().DeactivateAI();
106 
107  SCR_AICommsHandler commsHandler = m_UtilityComponent.m_CommsHandler;
108  if (commsHandler)
109  commsHandler.SetSuspended(true);
110 
111  return;
112  }
113  else if (newLifeState == ECharacterLifeState.DEAD)
114  {
115  GetControlComponent().DeactivateAI();
116  SCR_AICommsHandler commsHandler = m_UtilityComponent.m_CommsHandler;
117  if (commsHandler)
118  commsHandler.Reset();
119 
120  return;
121  }
122  else if (newLifeState == ECharacterLifeState.ALIVE)
123  {
124  bool possesingMainEntity = false;
125  array<int> players = new array<int>;
126  GetGame().GetPlayerManager().GetPlayers(players);
127  foreach (int playerId: players)
128  {
129  if(SCR_PossessingManagerComponent.GetPlayerMainEntity(playerId) == GetControlledEntity())
130  {
131  possesingMainEntity = true;
132  break;
133  }
134  }
135 
136  if (!EntityUtils.IsPlayer(GetControlledEntity()) && !possesingMainEntity)
137  GetControlComponent().ActivateAI();
138 
139  AICommunicationComponent comms = GetCommunicationComponent();
140  if (comms)
141  {
142  // Clear orders, it doesn't matter if we have missed them
143  comms.ClearOrders();
144 
145  // Don't clear messages, we will process them to catch up with goals from group
146  // This is crucial to resume to group orders
147 
148  // Clear danger events, it doesn't matter if we have missed them
149  ClearDangerEvents(GetDangerEventsCount());
150  }
151 
152  SCR_AICommsHandler commsHandler = m_UtilityComponent.m_CommsHandler;
153  if (commsHandler)
154  commsHandler.SetSuspended(false);
155  }
156  }
157 
158  //------------------------------------------------------------------------------------------------
159  Faction GetFaction(IEntity entity)
160  {
161  // Common case first
162  SCR_ChimeraCharacter character = SCR_ChimeraCharacter.Cast(entity);
163  if (character && character.m_pFactionComponent)
164  {
165  return character.m_pFactionComponent.GetAffiliatedFaction();
166  }
167 
168  Vehicle vehicle = Vehicle.Cast(entity);
169  if (vehicle && vehicle.m_pFactionComponent)
170  {
171  return vehicle.m_pFactionComponent.GetAffiliatedFaction();
172  }
173 
174  // Worst case - some other entity, perform component search
175  FactionAffiliationComponent factionAffiliation = FactionAffiliationComponent.Cast(entity.FindComponent(FactionAffiliationComponent));
176  if (factionAffiliation)
177  return factionAffiliation.GetAffiliatedFaction();
178 
179  return null;
180  }
181 
182  //------------------------------------------------------------------------------------------------
183  bool IsEnemy(IEntity entity)
184  {
185  Faction otherFaction = GetFaction(entity);
186 
187  Faction myFaction;
189  myFaction = m_FactionAffiliationComponent.GetAffiliatedFaction();
190 
191  if (!otherFaction || !myFaction)
192  return false;
193 
194  return myFaction.IsFactionEnemy(otherFaction);
195  }
196 
197  //------------------------------------------------------------------------------------------------
198  void OnGroupWaypointChanged(AIWaypoint newWaypoint)
199  {
200  m_GroupWaypoint = newWaypoint;
201  }
202 
203  //------------------------------------------------------------------------------------------------
204  void SetPlayerPending_S(int playerId)
205  {
206  if (m_iPendingPlayerId != 0 && playerId != 0)
207  {
208  Print("Trying to set pending player on an already pending agent!", LogLevel.ERROR);
209  return;
210  }
211 
212  m_iPendingPlayerId = playerId;
213  }
214 
215  //------------------------------------------------------------------------------------------------
216  bool IsPlayerPending_S()
217  {
218  return m_iPendingPlayerId != 0;
219  }
220 };
SCR_EntityHelper
Definition: SCR_EntityHelper.c:1
ECharacterLifeState
ECharacterLifeState
Definition: ECharacterLifeState.c:12
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_ChimeraAIAgentClass
Definition: SCR_ChimeraAIAgent.c:1
m_CharacterController
SCR_CharacterPerceivableComponentClass m_CharacterController
func
func
Definition: SCR_AIThreatSystem.c:5
SCR_CharacterControllerComponent
Definition: SCR_CharacterControllerComponent.c:35
SCR_AIMessage_Wounded
Definition: SCR_AIMessage.c:202
ChimeraAIAgentClass
Definition: ChimeraAIAgent.c:12
SCR_ChimeraAIAgent
Definition: SCR_ChimeraAIAgent.c:5
SCR_CharacterDamageManagerComponent
Definition: SCR_CharacterDamageManagerComponent.c:18
GetControlledEntity
SCR_EditableEntityComponent GetControlledEntity()
Returns the controlled entity or null if none.
Definition: SCR_EditablePlayerDelegateComponent.c:86
Faction
Definition: Faction.c:12
m_FactionAffiliationComponent
protected FactionAffiliationComponent m_FactionAffiliationComponent
Definition: SCR_BaseSupportStationComponent.c:122
SCR_AIGroup
Definition: SCR_AIGroup.c:68
EDamageType
EDamageType
Definition: EDamageType.c:12
SCR_AICommsHandler
Definition: SCR_AICommsHandler.c:19
EntityUtils
Definition: EntityUtils.c:12