Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AIDebugVisualization.c
Go to the documentation of this file.
1 class SCR_AIDebugVisualizationClass: GenericEntityClass
2 {
3 };
4 
10 {
11  static SCR_AIDebugVisualization s_Instance;
12  protected ref array<ref SCR_AIMessageVisualization> m_aElements = {};
13  protected ref array<ref SCR_AIAgentDebugPanel> m_aPanels = {};
14 
15  //------------------------------------------------------------------------------------------------
16  static void Init()
17  {
18  if (s_Instance)
19  SCR_EntityHelper.DeleteEntityAndChildren(s_Instance);
20 
22  }
23 
24  //------------------------------------------------------------------------------------------------
25  static SCR_AIDebugVisualization GetInstance()
26  {
27  if (!s_Instance)
29 
30  return s_Instance;
31  }
32 
33  //------------------------------------------------------------------------------------------------
34  static void VisualizeMessage(IEntity entity, string message, EAIDebugCategory category, float showTime, Color color = Color.White, float fontSize = 16, bool ignoreCategory = false)
35  {
36  int i = DiagMenu.GetValue(SCR_DebugMenuID.DEBUGUI_AI_DEBUG_CATEGORY);
37  if (category != DiagMenu.GetValue(SCR_DebugMenuID.DEBUGUI_AI_DEBUG_CATEGORY) && !ignoreCategory)
38  return;
39 
40  SCR_AIDebugVisualization inst = GetInstance();
41 
42  if (!inst || !entity || message == string.Empty)
43  return;
44 
45  if (!color)
46  color = Color.FromInt(Color.WHITE);
47 
48  inst.RemoveVisualization(entity);
49 
50  // Create element
51  SCR_AIMessageVisualization visualization = new SCR_AIMessageVisualization(entity, message, showTime, Color.FromInt(color.PackToInt()), fontSize);
52  inst.m_aElements.Insert(visualization);
53  }
54 
55  //------------------------------------------------------------------------------------------------
56  void ShowAiAgentDebugPanel(AIAgent agent, IEntity entity)
57  {
58  SCR_AIAgentDebugPanel existingPanel;
59  foreach (SCR_AIAgentDebugPanel p : m_aPanels)
60  {
61  if ((p.m_Agent && p.m_Agent == agent) || (p.m_Entity && p.m_Entity == entity))
62  existingPanel = p;
63  }
64 
65  // Do nothing if this agent already has a panel
66  if (existingPanel)
67  return;
68 
69  SCR_AIAgentDebugPanel newPanel = new SCR_AIAgentDebugPanel(agent, entity);
70  m_aPanels.Insert(newPanel);
71  }
72 
73  //------------------------------------------------------------------------------------------------
74  protected void RemoveVisualization(IEntity entity)
75  {
77  {
78  if (!vis)
79  continue;
80 
81  if (vis.m_TargetEntity == entity)
82  {
83  m_aElements.RemoveItem(vis);
84  vis = null;
85  return;
86  }
87  }
88  }
89 
90  //------------------------------------------------------------------------------------------------
91  override protected void EOnDiag(IEntity owner, float timeSlice)
92  {
93  // Update messages above AIs
94  bool enabled = DiagMenu.GetValue(SCR_DebugMenuID.DEBUGUI_AI_DEBUG_CATEGORY);
95  if (enabled)
96  {
97  int count = m_aElements.Count();
98  for (int i = count - 1; i >=0; i--)
99  {
100  bool finished = m_aElements[i].Draw(timeSlice);
101  if (finished)
102  m_aElements.Remove(i);
103  }
104  }
105 
106  // Update AI debug panels
107  bool openDebugPanel = DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_AI_OPEN_DEBUG_PANEL);
108  if (openDebugPanel)
109  {
110  AIAgent selectedAgent;
111  IEntity selectedEntity;
112 
113  if (GetSelectedAiAgentOrEntity(selectedAgent, selectedEntity))
114  ShowAiAgentDebugPanel(selectedAgent, selectedEntity);
115  else
116  Debug.Error("Nothing is selected! You must select something with Game Master first!");
117 
118  DiagMenu.SetValue(SCR_DebugMenuID.DEBUGUI_AI_OPEN_DEBUG_PANEL, false);
119  }
120 
121  int count = m_aPanels.Count();
122  for (int i = count - 1; i >= 0; i--)
123  {
124  bool requestClose = m_aPanels[i].Update(timeSlice);
125  if (requestClose)
126  m_aPanels.Remove(i);
127  }
128 
129  // Set BT breakpoint for selected AI agent
130  if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_AI_SET_BT_BREAKPOINT))
131  {
132  AIAgent selectedAgent;
133  IEntity selectedEntity;
134  GetSelectedAiAgentOrEntity(selectedAgent, selectedEntity);
135  if (selectedAgent)
136  {
137  AIBehaviorTreeComponent btComp = AIBehaviorTreeComponent.Cast(selectedAgent.FindComponent(AIBehaviorTreeComponent));
138  if (btComp)
139  {
140  btComp.SetBtBreakpoint(true);
141  }
142  }
143 
144  DiagMenu.SetValue(SCR_DebugMenuID.DEBUGUI_AI_SET_BT_BREAKPOINT, false);
145  }
146 
147  // Perception manager
148  if (DiagMenu.GetBool(SCR_DebugMenuID.DEBUGUI_AI_SHOW_PERCEPTION_PANEL))
149  {
150  ShowPerceptionPanel();
151  }
152  }
153 
154  //-------------------------------------------------------------------------------------------
155  void ShowPerceptionPanel()
156  {
157  DbgUI.Begin("Perception Panel");
158 
159  DbgUI.Text("Values for tuning perception properties");
160 
161  ChimeraWorld world = GetWorld();
162  BaseTimeAndWeatherManagerEntity twm = world.GetTimeAndWeatherManager();
163  if (!twm)
164  {
165  DbgUI.Text("BaseTimeAndWeatherManagerEntity not found!");
166  }
167  else
168  {
169  vector sunDir;
170  vector moonDir;
171  float moonPhase;
172  twm.GetCurrentSunMoonDirAndPhase(sunDir, moonDir, moonPhase);
173  float sunElevDeg = Math.RAD2DEG * Math.Asin(-sunDir[1]);
174  float moonElevDeg = Math.RAD2DEG * Math.Asin(-moonDir[1]);
175  DbgUI.Text(string.Format("Sun Elevation: %1 deg", sunElevDeg.ToString(5, 1)));
176  DbgUI.Text(string.Format("Moon ElevatioN: %1 deg", moonElevDeg.ToString(5, 1)));
177  DbgUI.Text(string.Format("Moon Phase: %1", moonPhase.ToString(4, 2)));
178  }
179 
180  PerceptionManager pm = GetGame().GetPerceptionManager();
181  if (!pm)
182  {
183  DbgUI.Text("PerceptionManager not found!");
184  }
185  else
186  {
187  float directLV;
188  float ambientLV;
189  float totalLV;
190  pm.GetAmbientLV(directLV, ambientLV, totalLV);
191  DbgUI.Text(string.Format("Direct LV: %1, Ambient LV: %2, Total Ambient LV: %3", directLV, ambientLV, totalLV));
192  }
193 
194  DbgUI.End();
195  }
196 
197  //-------------------------------------------------------------------------------------------
198  protected bool GetSelectedAiAgentOrEntity(out AIAgent outAgent, out IEntity outEntity)
199  {
200  // Get first AI entity selected in Game Master
202  set<SCR_EditableEntityComponent> selectedEntities = new set<SCR_EditableEntityComponent>();
203  filter.GetEntities(selectedEntities);
204 
205  if (selectedEntities.Count() == 0)
206  {
207  outAgent = null;
208  outEntity = null;
209  return false;
210  }
211 
212  // Try to find a group
213  foreach (SCR_EditableEntityComponent comp : selectedEntities)
214  {
215  SCR_EditableGroupComponent editGroupComp = SCR_EditableGroupComponent.Cast(comp);
216  if (editGroupComp)
217  {
218  outAgent = AIAgent.Cast(editGroupComp.GetOwner());
219  outEntity = null;
220  return true;
221  }
222  }
223 
224  // Group not found, select first unit
225  SCR_EditableCharacterComponent editCharacterComp = SCR_EditableCharacterComponent.Cast(selectedEntities[0]);
226  if (editCharacterComp)
227  {
228  outAgent = editCharacterComp.GetAgent();
229  outEntity = outAgent.GetControlledEntity();
230  return true;
231  }
232 
233  // It's not an AI or group but could be a basic entity
234  outAgent = null;
235  outEntity = selectedEntities[0].GetOwner();
236  return true;
237  }
238 
239  //------------------------------------------------------------------------------------------------
240  void SCR_AIDebugVisualization(IEntitySource src, IEntity parent)
241  {
242  s_Instance = this;
243 
244  SetEventMask(EntityEvent.FRAME);
245  ConnectToDiagSystem();
246  SetFlags(EntityFlags.ACTIVE, true);
247  }
248 
249  //------------------------------------------------------------------------------------------------
251  {
252  DisconnectFromDiagSystem();
253  if (m_aElements)
254  m_aElements.Clear();
255  m_aElements = null;
256  }
257 };
EEditableEntityState
EEditableEntityState
Definition: EEditableEntityState.c:37
ChimeraWorld
Definition: ChimeraWorld.c:12
SpawnEntity
protected IEntity SpawnEntity(ResourceName entityResourceName, notnull IEntity slotOwner)
Definition: SCR_CatalogEntitySpawnerComponent.c:1008
SCR_AIDebugVisualization
Definition: SCR_AIDebugVisualization.c:9
SCR_EntityHelper
Definition: SCR_EntityHelper.c:1
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_AIMessageVisualization
Definition: SCR_AIMessageVisualization.c:1
SCR_EditableGroupComponent
void SCR_EditableGroupComponent(IEntityComponentSource src, IEntity ent, IEntity parent)
Definition: SCR_EditableGroupComponent.c:703
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
m_aElements
protected ref array< ref SCR_TabViewContent > m_aElements
Definition: SCR_TabViewComponent.c:13
s_Instance
SCR_SpawnerSlotManagerClass s_Instance
Class used for managing changes and removals of slots present in world.
EAIDebugCategory
EAIDebugCategory
Definition: SCR_AIWorld.c:11
SCR_BaseEditableEntityFilter
Definition: SCR_BaseEditableEntityFilter.c:13
SCR_EditableEntityComponent
Definition: SCR_EditableEntityComponent.c:13
SCR_DebugMenuID
SCR_DebugMenuID
This enum contains all IDs for DiagMenu entries added in script.
Definition: DebugMenuID.c:3
SCR_AIDebugVisualizationClass
Definition: SCR_AIDebugVisualization.c:1
SCR_AIAgentDebugPanel
Definition: SCR_AIAgentDebugPanel.c:4
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180