24 SignalsManagerComponent m_SignalsManagerComponent;
25 VoNComponent m_VoNComponent;
26 FactionAffiliationComponent m_FactionComp;
28 protected ref array<ref SCR_AITalkRequest> m_aRequestQueue = {};
30 protected float m_fActiveTimer_ms;
31 bool m_bNeedUpdate =
false;
33 protected bool m_bMuted;
35 protected const float SAMPLE_LENGTH_MS = 2000.0;
36 protected const float NEARBY_SPEAKER_CHECK_RANGE = 10.0;
47 m_VoNComponent = VoNComponent.Cast(entity.FindComponent(VoNComponent));
49 m_FactionComp = FactionAffiliationComponent.Cast(entity.FindComponent(FactionAffiliationComponent));
56 AddDebugMessage(
string.Format(
"AddRequest: %1", request.GetDebugString()));
63 AddDebugMessage(
" Ignored because CommsHandler is muted");
84 int newPriority = request.m_iPriority;
87 for (
int i = m_aRequestQueue.Count()-1; i >= 0; i--)
89 int priority = m_aRequestQueue[idInsertAt].m_iPriority;
91 if (newPriority > priority)
103 if (idInsertAt == m_aRequestQueue.Count())
104 m_aRequestQueue.Insert(request);
106 m_aRequestQueue.InsertAt(request, idInsertAt);
109 AddDebugMessage(
string.Format(
" Added to queue at: %1, new queue size: %2", idInsertAt, m_aRequestQueue.Count()));
113 m_bNeedUpdate =
true;
123 if (
m_Agent.GetLOD() != 0 || !
m_Agent.GetControlComponent().IsAIActivated())
131 AIGroup myGroup =
m_Agent.GetParentGroup();
134 if ((!myGroup || myGroup.GetAgentsCount() <= 1) &&
135 !request.m_bTransmitIfNoReceivers)
137 if (!request.m_bTransmitIfPassenger)
139 ChimeraCharacter
char = ChimeraCharacter.Cast(
m_Agent.GetControlledEntity());
142 CompartmentAccessComponent compAcc =
char.GetCompartmentAccessComponent();
151 if (scrAiGroup && scrAiGroup.IsSlave() && scrAiGroup.GetLeaderAgent() ==
m_Agent)
158 void SetMuted(
bool mute)
161 AddDebugMessage(
string.Format(
"SetMuted: %1", mute));
169 ClearAndFailAllRequests();
171 m_bNeedUpdate =
false;
190 void SetSuspended(
bool suspended)
193 AddDebugMessage(
string.Format(
"SetSuspended: %1", suspended));
201 if (m_CurrentRequest)
203 m_aRequestQueue.InsertAt(m_CurrentRequest, 0);
204 m_CurrentRequest =
null;
226 void Update(
float timeSlice)
238 m_CurrentRequest = validRequest;
239 TryTransmitAndSwitchState();
248 float currentTime_ms =
GetGame().GetWorld().GetWorldTime();
250 if (currentTime_ms - m_CurrentRequest.m_fCreatedTimestamp_ms >= m_CurrentRequest.m_fTimeout_ms)
253 FailRequest(m_CurrentRequest);
254 m_CurrentRequest =
null;
260 if (CanTransmit(m_CurrentRequest))
262 TransmitRequest(m_CurrentRequest);
264 m_fActiveTimer_ms = 0;
273 m_fActiveTimer_ms += timeSlice;
275 if (m_fActiveTimer_ms > SAMPLE_LENGTH_MS)
278 CompleteRequest(m_CurrentRequest);
279 m_CurrentRequest =
null;
286 m_CurrentRequest = validRequest;
287 if (m_CurrentRequest.m_bTransmitIfChannelBusy)
291 TryTransmitAndSwitchState();
311 m_bNeedUpdate = !m_aRequestQueue.IsEmpty() || m_CurrentRequest;
319 AddDebugMessage(
"Reset");
321 ClearAndFailAllRequests();
333 float currentTime_ms =
GetGame().GetWorld().GetWorldTime();
335 for (
int i = 0; i < m_aRequestQueue.Count(); i++)
338 if (currentTime_ms - thisRequest.m_fCreatedTimestamp_ms < thisRequest.m_fTimeout_ms)
340 request = thisRequest;
341 m_aRequestQueue.RemoveOrdered(0);
346 FailRequest(thisRequest);
347 m_aRequestQueue.RemoveOrdered(0);
356 protected void TryTransmitAndSwitchState()
358 if (CanBypass(m_CurrentRequest))
361 CompleteRequest(m_CurrentRequest);
362 m_CurrentRequest =
null;
365 else if (CanTransmit(m_CurrentRequest))
368 TransmitRequest(m_CurrentRequest);
370 m_fActiveTimer_ms = 0;
388 if (request.m_bTransmitIfChannelBusy)
395 return IsChannelFree();
402 protected bool IsChannelFree()
408 array<IEntity> entities = {};
412 Print(
"SCR_AICommsHandler: TagManager is not present in the world, AI comms might behave incorrect", LogLevel.ERROR);
416 tm.GetTagsInRange(entities,
m_Entity.GetOrigin(), NEARBY_SPEAKER_CHECK_RANGE,
ETagCategory.NameTag);
420 myFaction = m_FactionComp.GetAffiliatedFaction();
421 foreach (IEntity ent : entities)
423 SCR_ChimeraCharacter characterEnt = SCR_ChimeraCharacter.Cast(ent);
428 if (characterEnt.m_pFactionComponent && (characterEnt.m_pFactionComponent.GetAffiliatedFaction() != myFaction))
431 CharacterControllerComponent characterController = characterEnt.GetCharacterController();
432 AIControlComponent aiControlComponent = characterController.GetAIControlComponent();
434 AIAgent agent = aiControlComponent.GetControlAIAgent();
439 SCR_AIUtilityComponent utility = chimeraAgent.m_UtilityComponent;
471 AddDebugMessage(
string.Format(
"TransmitRequest: %1", rq.GetDebugString()));
479 string str =
string.Format(
"SCR_AISoundHandling.SetSignalsAndTransmit failed for request: %1", rq.GetDebugString());
481 Print(
string.Format(
"SCR_AICommsHandler: %1", str), LogLevel.ERROR);
484 AddDebugMessage(str, LogLevel.ERROR);
493 AddDebugMessage(
string.Format(
"FailRequest: %1", rq.GetDebugString()));
502 AddDebugMessage(
string.Format(
"CompleteRequest: %1", rq.GetDebugString()));
509 protected void ClearAndFailAllRequests()
512 AddDebugMessage(
"ClearAndFailAllRequests");
515 if (m_CurrentRequest)
516 FailRequest(m_CurrentRequest);
517 m_CurrentRequest =
null;
520 FailRequest(request);
521 m_aRequestQueue.Clear();
527 void EOnDiag(
float timeSlice)
531 vector ownerPos =
m_Entity.GetOrigin();
532 vector textPos = ownerPos + Vector(0, 4.5, 0);
533 int color = Color.GRAY;
535 string str =
string.Format(
"Queue: %1, State: %2",
536 m_aRequestQueue.Count(),
542 color = Color.MAGENTA;
544 color = Color.ORANGE;
546 str +=
string.Format(
"\n%1",
typename.EnumToString(
ECommunicationType, m_CurrentRequest.m_eCommType));
549 DebugTextWorldSpace.Create(
GetGame().GetWorld(), str, DebugTextFlags.ONCE | DebugTextFlags.CENTER | DebugTextFlags.FACE_CAMERA,
550 textPos[0], textPos[1], textPos[2], color: color, bgColor: Color.BLACK,
558 ClearAndFailAllRequests();
563 protected void AddDebugMessage(
string str, LogLevel logLevel = LogLevel.NORMAL)
567 SCR_AIInfoBaseComponent infoComp = SCR_AIInfoBaseComponent.Cast(
m_Agent.FindComponent(SCR_AIInfoBaseComponent));