Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
ModelAutotestEntity.c
Go to the documentation of this file.
1 // #define DEBUG_PRINT
2 [EntityEditorProps(category: "GameScripted/Utility", description: "Model autotest entity", color: "0 0 255 255")]
4 {
5 };
6 
7 //------------------------------------------------------------------------------------------------
10 {
11  [Attribute("UndefinedCamera", UIWidgets.EditBox, "The name of this camera.", "")]
12  private string m_sCameraLabel;
13  [Attribute("-30 25 -30", UIWidgets.EditBox, "Camera position in world coordinates (metres)", "")]
14  private vector m_vCameraPosition;
15  [Attribute("0 45 0", UIWidgets.EditBox, "Camera rotation in world coordinates (degrees)", "")]
16  private vector m_vCameraRotation;
17  [Attribute("5", UIWidgets.Slider, "The interval before this camera is done.", "0 100 1")]
18  private float m_fCameraDuration;
19  [Attribute("false", UIWidgets.CheckBox, "Whether camera is set from the center of spawned objects, or in global coordinates.", "")]
20  private bool m_bRelativeToObjectsCenter;
21 
22  // array to hold frames per second captured by this camera
23  private ref array<float> m_aFramesPerSecond = null;
24 
25  //------------------------------------------------------------------------------------------------
27  bool GetIsRelativeToCenter()
28  {
29  return m_bRelativeToObjectsCenter;
30  }
31 
32  //------------------------------------------------------------------------------------------------
34  string GetLabel()
35  {
36  return m_sCameraLabel;
37  }
38 
39  //------------------------------------------------------------------------------------------------
41  vector GetPosition()
42  {
43  return m_vCameraPosition;
44  }
45 
46  //------------------------------------------------------------------------------------------------
48  vector GetRotation()
49  {
50  return m_vCameraRotation;
51  }
52 
53  //------------------------------------------------------------------------------------------------
55  float GetDuration()
56  {
57  return m_fCameraDuration;
58  }
59 
60  //------------------------------------------------------------------------------------------------
62  void LogFramesPerSecond(float timeSlice)
63  {
64  if (!m_aFramesPerSecond)
65  m_aFramesPerSecond = new array<float>();
66 
67  m_aFramesPerSecond.Insert((float)System.GetFPS());
68  }
69 
70  //------------------------------------------------------------------------------------------------
72  void ClearFramesPerSecond()
73  {
74  if (!m_aFramesPerSecond)
75  {
76  return;
77  }
78 
79  m_aFramesPerSecond.Clear();
80  }
81 
82 
83  //------------------------------------------------------------------------------------------------
86  bool GetFramesPerSecondsData(out float minFps, out float maxFps, out float averageFps)
87  {
88  // no data return false
89 
90  if (!m_aFramesPerSecond)
91  return false;
92  int fpsArrayLength = m_aFramesPerSecond.Count();
93  if (fpsArrayLength <= 0)
94  return false;
95 
96  float min = 99999999.0;
97  float max = -99999999.0;
98  float total = 0.0;
99  float avg = 0.0;
100 
101  // find max, min and add to total fps
102  for (int i = 0; i < fpsArrayLength; i++)
103  {
104  float currentFrame = m_aFramesPerSecond[i];
105 
106  if (currentFrame > max)
107  max = currentFrame;
108 
109  if (currentFrame < min)
110  min = currentFrame;
111 
112  total += currentFrame;
113  }
114  // calculate avg from total fps / fps captures count
115  avg = total / (float)fpsArrayLength;
116 
117  // output results
118  minFps = min;
119  maxFps = max;
120  averageFps = avg;
121 
122  return true;
123  }
124 
125 
126  //------------------------------------------------------------------------------------------------
128  void SetAsCamera(BaseWorld world, vector center)
129  {
130  // target cam matrix
131  vector camMat[4];
132 
133  // create a matrix from euler angles
134  vector camRot = m_vCameraRotation;
135  vector eulerAngles = Vector(camRot[1], camRot[0], camRot[2]);
136  vector rotMat[3];
137  Math3D.AnglesToMatrix(eulerAngles, rotMat);
138 
139  // fill matrix with rotation data
140  camMat[0] = rotMat[0];
141  camMat[1] = rotMat[1];
142  camMat[2] = rotMat[2];
143 
144  // decide camera world position, fill matrix
145  vector centerOffset = vector.Zero;
146  if (m_bRelativeToObjectsCenter)
147  {
148  centerOffset = center;
149  centerOffset[1] = 0;
150  }
151  camMat[3] = m_vCameraPosition + centerOffset;
152 
153  // set engine camera's transform
154  int cameraIndex = world.GetCurrentCameraId();
155  world.SetCameraEx(cameraIndex, camMat);
156  }
157 
158  //------------------------------------------------------------------------------------------------
160  {
161  if (m_aFramesPerSecond)
162  {
163  m_aFramesPerSecond.Clear();
164  m_aFramesPerSecond = null;
165  }
166  }
167 };
168 
169 
170 //------------------------------------------------------------------------------------------------
172 {
174  [Attribute("AR Models Autotest", UIWidgets.EditBox, "Target result page name in Confluence", "")]
175  private string m_sConfluenceParentPageName;
177  [Attribute("", UIWidgets.Object, "Camera definitions for each model spawned", "")]
178  private ref array<ref SCR_ModelAutotestCamera> m_aCameras;
179 
181  private float m_fWarmupDelay = 2.5;
183  private float m_fTimePassed = 0.0;
185 
186  private bool m_bWarmedUp = false; // true after warmup delay is passed
187  private bool m_bRespawningObjects = false; // set to true during load or despawn of an object
188  private bool m_bTestStarted = false; // set to true if proper object is spawned
189  private bool m_bCameraSet = false;
190 
192  private int m_iCurrentObjectIndex = 0;
194  private int m_iCurrentCameraIndex = 0;
196  private int m_iCamerasCount = 0;
198  private SCR_ModelAutotestCamera m_CurrentAutotestCamera = null;
199 
201  private vector m_vCameraPositionCenter = vector.Zero;
202 
204  private ref AutotestRegister m_AutotestRegister = null;
205  private int m_iAutotestIndex = 0;
206 
207 
208  //------------------------------------------------------------------------------------------------
210  private string GetPersistentDataPath()
211  {
212  string indexFileName = m_sConfluenceParentPageName;
213  indexFileName.Replace(" ", "");
214  return string.Format("%1.idx", indexFileName);
215  }
216 
217  //------------------------------------------------------------------------------------------------
219  private string GetFormattedTimestamp()
220  {
221  int year, month, day;
222  System.GetYearMonthDay(year, month, day);
223  string smonth, sday;
224  if(month < 10)
225  smonth = string.Format("0%1", month);
226  else
227  smonth = string.Format("%1", month);
228 
229  if(day < 10)
230  sday = string.Format("0%1", day);
231  else
232  sday = string.Format("%1", day);
233 
234  m_AutotestRegister.SavePersistentData(GetPersistentDataPath(), string.Format("%1", m_iAutotestIndex));
235 
236  return string.Format("%1%2%3-%4", year, smonth, sday, m_iAutotestIndex);
237  }
238 
239  //------------------------------------------------------------------------------------------------
242  private bool ValidateAutotestSettings()
243  {
244  // Not in game
245  if (!GetGame().GetWorldEntity())
246  return false;
247 
248 
249  // Array null? Error, return.
250  if (!m_aCameras)
251  {
252  Print("No autotest cameras defined in ModelAutotestEntity! Returning to edit mode!", LogLevel.ERROR);
253  GetGame().RequestClose();
254  return false;
255  }
256 
257  if (m_aCameras.Count() <= 0)
258  {
259  Print("No autotest cameras defined in ModelAutotestEntity! Returning to edit mode!", LogLevel.ERROR);
260  GetGame().RequestClose();
261  return false;
262  }
263 
264  // Camera null? Error, return.
265  // also init camera if not null
266  if (!m_CurrentAutotestCamera)
267  {
268  SetCamera(GetGame().GetWorldEntity().GetWorld(), 0, m_vCameraPositionCenter);
269  }
270  else
271  {
272  Print("Specified autotest camera was invalid! Returning to edit mode!", LogLevel.ERROR);
273  GetGame().RequestClose();
274  return false;
275  }
276 
277  // Invalid name page
278  if (!m_sConfluenceParentPageName || m_sConfluenceParentPageName == string.Empty)
279  {
280  Print("Specified target confluence page is invalid! Returning to edit mode!", LogLevel.ERROR);
281  GetGame().RequestClose();
282  return false;
283  }
284 
285  return true;
286  }
287 
288  //------------------------------------------------------------------------------------------------
289  private void SetCamera(BaseWorld world, int index, vector center)
290  {
291  m_CurrentAutotestCamera = m_aCameras[index];
292  m_CurrentAutotestCamera.SetAsCamera(world, center);
293  m_bCameraSet = true;
294  OnSetCamera(world, index);
295  }
296 
297  //------------------------------------------------------------------------------------------------
298  private void WriteMeasurementData()
299  {
300  string name;
301  ref MeasurementFile measurementFile = null;
302  if (GetObjectName(m_iCurrentObjectIndex, name))
303  {
304  int len = name.Length();
305  int lastBracket = name.IndexOf("}")+1;
306 
307  string formattedName = name.Substring(lastBracket, len-lastBracket);
308  string fileName = formattedName;
309  // Replace directories slashes with # for filename
310  fileName.Replace("/", "#");
311  fileName.Replace("\\", "#");
312  if (!measurementFile)
313  measurementFile = m_AutotestRegister.OpenMeasurementFile(fileName, formattedName, MeasurementType.GraphLine, false);
314 
315  string header = "date,";
316  for (int i = 0; i < m_aCameras.Count(); i++)
317  {
318  if (i == m_aCameras.Count()-1)
319  header += "@"+m_aCameras[i].GetLabel();
320  else
321  header += "@"+m_aCameras[i].GetLabel()+",";
322  }
323 
324 
325  string data = GetFormattedTimestamp()+",";
326  for (int i = 0; i < m_iCamerasCount; i++)
327  {
328  float min, max, avg;
329  m_aCameras[i].GetFramesPerSecondsData(min, max, avg);
330  m_aCameras[i].ClearFramesPerSecond();
331 
332  if (i == m_iCamerasCount - 1)
333  data += avg.ToString();
334  else
335  data += avg.ToString() + ",";
336 
337  }
338  measurementFile.SetGraphHeader(header);
339  measurementFile.AddData(data);
340  measurementFile = null;
341  }
342  }
343 
344  //------------------------------------------------------------------------------------------------
346  protected event void OnSetCamera(BaseWorld world, int cameraIndex)
347  {
348  Print(string.Format("| Model Autotest | : Camera set to \"%1\"!", m_aCameras[cameraIndex].GetLabel()));
349  // Override me!
350  }
351 
352  //------------------------------------------------------------------------------------------------
354  protected event void OnSetModel(int modelIndex)
355  {
356  string mdlName = string.Empty;
357  if (!GetObjectName(m_iCurrentObjectIndex, mdlName))
358  mdlName = m_iCurrentObjectIndex.ToString();
359  Print(string.Format("| Model Autotest | : Model set to \"%1\"!", mdlName));
360  // Override me!
361  }
362 
363  //------------------------------------------------------------------------------------------------
365  protected event void OnAutotestInitialized()
366  {
367  // Override me!
368  }
369 
370  //------------------------------------------------------------------------------------------------
372  protected event void OnAutotestFrame(float timeSlice)
373  {
374  // Override me!
375  }
376 
377  //------------------------------------------------------------------------------------------------
378  private override void EOnFrame(IEntity owner, float timeSlice)
379  {
380  // camera is out of bounds, get next object
381  if (m_iCurrentCameraIndex >= m_iCamerasCount)
382  {
383  // TODO:
384  // output data to file
385  WriteMeasurementData();
386  m_bTestStarted = false;
387  m_iCurrentObjectIndex++;
388  m_iCurrentCameraIndex = 0;
389  m_bTestStarted = false;
390  }
391 
392  if (!m_bCameraSet)
393  {
394  SetCamera(owner.GetWorld(), m_iCurrentCameraIndex, m_vCameraPositionCenter);
395  }
396 
397  // test not started, get next object and
398  if (!m_bTestStarted)
399  {
400  if (!Spawn(m_iCurrentObjectIndex, m_vCameraPositionCenter))
401  {
402  // TODO:
403  // objects oob, quit test
404  GetGame().RequestClose();
405  }
406 
407  OnSetModel(m_iCurrentObjectIndex);
408  m_bWarmedUp = false;
409  m_bTestStarted = true;
410  }
411 
412  OnAutotestFrame(timeSlice);
413 
414  // objects are spawned
415  if (m_bTestStarted)
416  {
417  // warmup first
418  if (!m_bWarmedUp)
419  {
420  m_fTimePassed += timeSlice;
421  if (m_fTimePassed <= m_fWarmupDelay)
422  {
423  return;
424  }
425  else
426  {
427  int testNr = m_iCamerasCount*m_iCurrentObjectIndex + m_iCurrentCameraIndex+1;
428  Print(string.Format("| Model Autotest | : Test number %1 started!", testNr));
429  m_bWarmedUp = true;
430  m_fTimePassed = 0.0;
431  }
432  }
433 
434  float currentCameraTime = m_CurrentAutotestCamera.GetDuration();
435 
436  if (m_fTimePassed < currentCameraTime)
437  {
438  m_CurrentAutotestCamera.LogFramesPerSecond(timeSlice);
439  m_fTimePassed+=timeSlice;
440  }
441  else
442  {
443  int testNr = m_iCamerasCount*m_iCurrentObjectIndex + m_iCurrentCameraIndex+1;
444  Print(string.Format("| Model Autotest | : Test number %1 finished!", testNr));
445 
446  float min, max, avg;
447  m_aCameras[m_iCurrentCameraIndex].GetFramesPerSecondsData(min, max, avg);
448  Print(string.Format("Test number %4 Results: Min FPS: %1 | Max FPS: %2 || Average FPS: %3", min, max, avg, testNr));
449  Print("-----------------------------------------------------------------");
450  m_fTimePassed = 0.0;
451  m_bWarmedUp = false;
452  m_bCameraSet = false;
453  m_iCurrentCameraIndex++;
454  }
455  }
456  }
457 
458  //------------------------------------------------------------------------------------------------
459  protected float GetCurrentTestDuration()
460  {
461  if (m_bTestStarted && m_bWarmedUp)
462  {
463  return m_fTimePassed;
464  }
465  else
466  return 0.0;
467  }
468 
469  //------------------------------------------------------------------------------------------------
470  protected float GetCurrentTestMaxDuration()
471  {
472  if (!m_CurrentAutotestCamera)
473  return 0.0;
474  else
475  return m_CurrentAutotestCamera.GetDuration();
476  }
477 
478  //------------------------------------------------------------------------------------------------
479  private override void EOnInit(IEntity owner)
480  {
481  super.EOnInit(owner);
482 
483  if (!ValidateAutotestSettings())
484  return;
485 
486  // Initialize camera count and array for fps
487  m_iCamerasCount = m_aCameras.Count();
488 
489  // Creates autotest register
490  m_AutotestRegister = new AutotestRegister();
491  m_AutotestRegister.Init(m_sConfluenceParentPageName);
492 
493  // Autotest run index within the current date.
494  string idxLine = m_AutotestRegister.LoadPersistentData(GetPersistentDataPath());
495  if(idxLine.Length() == 0)
496  m_iAutotestIndex = 0;
497  else
498  m_iAutotestIndex = idxLine.ToInt() + 1;
499 
500  // Initialize the autotest itself!
501  Print(string.Format("| Model Autotest | : Starting in %1s", m_fWarmupDelay));
502 
503  OnAutotestInitialized();
504  }
505 
506  //------------------------------------------------------------------------------------------------
508  {
509  for (int i = 0; i < m_aCameras.Count(); i++)
510  {
511  m_aCameras[i] = null;
512  }
513  }
514 };
SCR_GridSpawnerEntity
Definition: GridSpawnerEntity.c:19
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_GridSpawnerEntityClass
Definition: GridSpawnerEntity.c:4
Spawn
override IEntity Spawn(IEntity owner, Physics parentPhysics, SCR_HitInfo hitInfo, bool snapToTerrain=false)
Definition: SCR_DestructionBaseComponent.c:815
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_ModelAutotestEntityClass
Definition: ModelAutotestEntity.c:3
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
SCR_ModelAutotestCamera
Definition: ModelAutotestEntity.c:9
SCR_ModelAutotestEntity
Definition: ModelAutotestEntity.c:171
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305
GetLabel
EEditableEntityLabel GetLabel()
Definition: SCR_ServicePointComponent.c:65
BaseContainerProps
SCR_AIGoalReaction_Follow BaseContainerProps
Handles insects that are supposed to be spawned around selected prefabs defined in prefab names array...
Definition: SCR_AIGoalReaction.c:468
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180