Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
AutotestGrid.c
Go to the documentation of this file.
1// Script File
2
3[EntityEditorProps(category: "GameLib/Scripted/Autotest", description:"AutotestGrid", dynamicBox: true)]
4class AutotestGridClass: GenericEntityClass
5{
6}
7
8class SimpleTimeSignal
9{
10 private float m_LastTime;
11 private float m_TimeInterval;
12
13 void SimpleTimeSignal(float timeInterval)
14 {
15 m_LastTime = 0;
16 m_TimeInterval = timeInterval;
17 }
18
19 bool Update(float currentTime)
20 {
21 if (currentTime - m_LastTime > m_TimeInterval)
22 {
23 m_LastTime = currentTime;
24
25 return true;
26 }
27
28 return false;
29 }
30
31 void Restart(float currentTime)
32 {
33 m_LastTime = currentTime;
34 }
35
36 void RestartAndSignal()
37 {
38 m_LastTime = -m_TimeInterval;
39 }
40}
41
42class AutotestGrid: GenericEntity
43{
44 [Attribute("Camera1", UIWidgets.EditBox, "Name of camera entity", "")]
45 private string m_cameraEntityName; //< name of camera entity which will be used to take screenshots
46
47 [Attribute("60", UIWidgets.Slider, "Which FPS is considered as minimal", "0 240 1")]
48 private float m_FPSLimit; //< this time is waited for the screenshot to be captured and saved on disk
49
50 [Attribute("1.8", UIWidgets.Slider, "Distance between surface and camera on y-axis", "0 20 0.1")]
51 private float m_HeightAboveSurface;
52
53 [Attribute("5", UIWidgets.Slider, "Time in seconds to make a 360 degree rotation, to load all of the resources", "1 100 1")]
54 private float m_FirstRotationTime;
55
56 [Attribute("5", UIWidgets.Slider, "Time in seconds to make a 360 degree rotation, to make the measurement", "1 100 1")]
57 private float m_SecondRotationTime;
58
59 [Attribute("1", UIWidgets.Slider, "Time in seconds to start the autotest, n seconds after the initialization of this object", "1 20 1")]
60 private float m_StartAfterInitTime;
61
62 [Attribute("10", UIWidgets.Slider, "Grid size on x axis", "1 100 1")]
63 private float m_GridX;
64
65 [Attribute("10", UIWidgets.Slider, "Grid size on z axis", "1 100 1")]
66 private float m_GridZ;
67
68 private int m_GridPosition;
69
70 private IEntity m_camera;
71 private string m_directory;
72
73 private TextWidget m_FPSWidget;
74
75 protected float m_timeFromStart;
76 protected float m_timeLastStatsSnapshot;
77
78 private ref SimpleTimeSignal m_StartTtimeSignal;
79 private ref SimpleTimeSignal m_FirstRotationSignal;
80 private ref SimpleTimeSignal m_SecondRotationSignal;
81
82 //-1 - init - wait for init duration to pass
83 //0 - ready for 1st rotation
84 //1 - doing 1st rotation
85 //2 - ready for 2nd rotation
86 //3 - doing 2nd rotation
87 private int m_CurrentRotation;
88
89 private ref Measurement m_MeasureFPS;
90 private ref Measurement m_MeasureMemory;
91
92 private ref array<ref Measurement> m_MeasurementMemory;
93
94 [Attribute("MotionZone", UIWidgets.EditBox, "Motion Zone Object Name", "")]
95 private string m_MotionZone;
96
97 private MotionZone m_MotionZoneInstance;
98
99 private float m_CurrentRotationT;
100 private float m_CurrentDuration;
101
102 void AutotestGrid(IEntitySource src, IEntity parent)
103 {
105 SetFlags(EntityFlags.ACTIVE, true);
106 m_MeasureFPS = new Measurement();
107 m_MeasureMemory = new Measurement();
108 m_MeasurementMemory = new array<ref Measurement>();
109 int mc = MemoryStatsSnapshot.GetStatsCount();
110 for (int i = 0; i < mc; i++)
111 {
112 m_MeasurementMemory.Insert(new Measurement());
113 }
114
115 m_GridPosition = 0;
116 m_timeFromStart = 0;
117 m_timeLastStatsSnapshot = 0;
118 m_CurrentRotation = -1;
119 m_CurrentRotationT = 0;
120
121 m_StartTtimeSignal = new SimpleTimeSignal(m_StartAfterInitTime);
122 m_FirstRotationSignal = new SimpleTimeSignal(m_FirstRotationTime);
123 m_SecondRotationSignal = new SimpleTimeSignal(m_SecondRotationTime);
124
125 System.GetCLIParam("autotest-output-dir", m_directory);
126
127 if (m_directory.Length() == 0)
128 {
129 m_directory = "$logs:" + GetName();
130 }
131
132 AutotestBase.Reset();
133 AutotestBase.SetLastProcessingTime_Treshold(100/1000);
134 }
135
136 void ~AutotestGrid()
137 {
138 }
139
140 vector GridPositionToWorldPosition(int gridPosition)
141 {
142 float dX = m_MotionZoneInstance.GetMax()[0] - m_MotionZoneInstance.GetMin()[0];
143 float dZ = m_MotionZoneInstance.GetMax()[2] - m_MotionZoneInstance.GetMin()[2];
144
145 float nX = dX / m_GridX;
146 float nZ = dZ / m_GridZ;
147
148 int t = Math.AbsInt(gridPosition / m_GridX);
149 int cX = t;
150 int cZ = gridPosition - t * m_GridX;
151
152 vector ret = Vector(m_MotionZoneInstance.GetMin()[0] + nX * cX + 0.5 * nX, 0, m_MotionZoneInstance.GetMin()[2] + nZ * cZ + 0.5 * nZ);
153 ret[1] = m_HeightAboveSurface + GetYDistance(ret[0], ret[2]);
154
155 return ret;
156 }
157
158 void UpdateMeasurements()
159 {
160 int f = System.GetFPS();
161
162 m_MeasureFPS.AddValue(f);
163 m_MeasureMemory.AddValue(System.MemoryAllocationKB());
164
165 if (m_timeFromStart - m_timeLastStatsSnapshot > 2)
166 {
167 m_timeLastStatsSnapshot = m_timeFromStart;
168
169 auto snapshot = new MemoryStatsSnapshot();
170 int count = snapshot.GetStatsCount();
171 for (int i = 0; i < count; i++)
172 {
173 int value = snapshot.GetStatValue(i);
174 m_MeasurementMemory[i].AddValue(value);
175 }
176 }
177 }
178
179 override void EOnInit(IEntity owner)
180 {
181 m_camera = g_Game.FindEntity(m_cameraEntityName);
182
183 if (!m_camera)
184 {
185 Print("Camera not found 2!", LogLevel.ERROR);
186 }
187 else
188 {
189 m_camera.SetOrigin(Vector(1269.97, 32.95, 1156.55));
190 }
191
192 m_MotionZoneInstance = MotionZone.Cast(g_Game.FindEntity(m_MotionZone));
193
194 if (m_MotionZoneInstance != null)
195 {
196 m_MotionZoneInstance.Initialize();
197 }
198
199 m_FPSWidget = TextWidget.Cast(g_Game.GetWorkspace().CreateWidgetInWorkspace(WidgetType.TextWidgetTypeID, 16, 16, 512, 128, WidgetFlags.VISIBLE, new Color(0.0, 0.0, 0.0, 1.0), 1024));
200 m_FPSWidget.SetExactFontSize(64);
201
202/*
203 for (int i = 0; i < 100; i++)
204 {
205 vector v = GridPositionToWorldPosition(i);
206 Print("" + i + " | " + v);
207 }
208*/
209 }
210
211 private void MakeSummeryFile(string filename)
212 {
213 FileHandle descrFile = FileIO.OpenFile(filename, FileMode.WRITE);
214
215 if(descrFile)
216 {
217 int sizeX = g_Game.GetWorkspace().GetWidth();
218 int sizeY = g_Game.GetWorkspace().GetHeight();
219 descrFile.WriteLine(string.Format("GRID AUTO TEST"));
220 descrFile.WriteLine(string.Format("Resolution: %1 x %2 px", sizeX, sizeY));
221#ifdef WORKBENCH
222 descrFile.WriteLine(string.Format("Entering playmode time: %1 s", g_Game.GetLoadTime() / 1000));
223#else
224 descrFile.WriteLine(string.Format("Load time: %1 s", g_Game.GetLoadTime() / 1000));
225#endif
226 descrFile.WriteLine(string.Format("Duration: %1 s", m_timeFromStart));
227
228 descrFile.WriteLine(string.Format("FPS average (s): %1", m_MeasureFPS.ComputeAverage()));
229 descrFile.WriteLine(string.Format("FPS min (s): %1", m_MeasureFPS.m_Min));
230 descrFile.WriteLine(string.Format("FPS max (s): %1", m_MeasureFPS.m_Max));
231
232 int count = MemoryStatsSnapshot.GetStatsCount();
233 for (int i = 0; i < count; i++)
234 {
235 string name = MemoryStatsSnapshot.GetStatName(i);
236 descrFile.WriteLine(string.Format("Measurement '%1' average: %2", name, (int)m_MeasurementMemory[i].ComputeAverage()));
237 descrFile.WriteLine(string.Format("Measurement '%1' min: %2", name, (int)m_MeasurementMemory[i].m_Min));
238 descrFile.WriteLine(string.Format("Measurement '%1' max: %2", name, (int)m_MeasurementMemory[i].m_Max));
239 }
240
241 descrFile.Close();
242 Print("Summary file successfully saved into " + filename);
243 }
244 }
245
246 override void EOnFrame(IEntity owner, float timeSlice)
247 {
248
249 //DO FPS
250 int fps = System.GetFPS();
251 m_FPSWidget.SetText("FPS " + fps);
252 if (fps < m_FPSLimit)
253 m_FPSWidget.SetColor(new Color(1.0, 0.0, 0.0, 1.0));
254 else
255 m_FPSWidget.SetColor(new Color(0.0, 1.0, 0.0, 1.0));
256
257 if (m_CurrentRotation >= 0 && !g_Game.IsPreloadFinished())
258 return;
259
260 vector destination;
261 vector direction;
262
263 vector output[2];
264
265 destination = output[0];
266 direction = output[1];
267
268 bool signal0 = m_StartTtimeSignal.Update(m_timeFromStart);
269 bool signal1 = m_FirstRotationSignal.Update(m_timeFromStart);
270 bool signal2 = m_SecondRotationSignal.Update(m_timeFromStart);
271
272 if (signal0 && m_CurrentRotation == -1)
273 {
274 m_CurrentRotation = 0;
275 m_FirstRotationSignal.RestartAndSignal();
276
277 vector p = GridPositionToWorldPosition(m_GridPosition);
278 m_camera.SetOrigin(p);
279 g_Game.BeginPreload(GetWorld(), p, 500);
280 Print("-- init time done");
281 }
282 else
283 if (signal1 && m_CurrentRotation == 0)
284 {
285 //start 1st rotation
286 m_CurrentRotation = 1;
287 m_CurrentRotationT = 0;
288 m_CurrentDuration = m_FirstRotationTime;
289 Print("-- start 1st rotation");
290 }
291 else
292 if (signal1 && m_CurrentRotation == 1)
293 {
294 //end 1st rotation
295 m_CurrentRotation = 2;
296 m_CurrentDuration = 1;
297 Print("-- end 1st rotation");
298 m_SecondRotationSignal.RestartAndSignal();
299 }
300 else
301 if (signal2 && m_CurrentRotation == 2)
302 {
303 //start 2nd rotation
304 m_CurrentRotation = 3;
305 m_CurrentRotationT = 0;
306 m_CurrentDuration = m_SecondRotationTime;
307 Print("-- start 2nd rotation");
308 }
309 else
310 if (signal2 && m_CurrentRotation == 3)
311 {
312 m_GridPosition++;
313 if (m_GridPosition >= m_GridX * m_GridZ)
314 {
315 Print("-- END of test");
316 string summeryFilename = string.Format("%1/%2", m_directory, "summary_grid.txt");
317 Print("Autotest Finished; result in " + summeryFilename);
318 FileIO.MakeDirectory(m_directory);
319 MakeSummeryFile(summeryFilename);
321 }
322 else
323 {
324 m_CurrentRotation = 0;
325 m_CurrentDuration = 1;
326
327 vector p = GridPositionToWorldPosition(m_GridPosition);
328 m_camera.SetOrigin(p);
329 g_Game.BeginPreload(GetWorld(), p, 500);
330
331 m_FirstRotationSignal.RestartAndSignal();
332
333 //end 2nd rotation a move to next grid point
334 Print("-- end 2nd rotation a move to next grid point");
335 }
336 }
337
338 if (m_CurrentRotation > 0)
339 {
340 RotateCamera(timeSlice, m_CurrentDuration);
341 }
342
343 if (m_CurrentRotation == 3)
344 {
345 UpdateMeasurements();
346 }
347
348 if (AutotestBase.IsLastProcessingTime_Signal())
349 {
350 //print into log
351 Print("!!! Warning Frame update too long");
352
353 AutotestBase.ResetLastProcessingTime_Signal();
354 }
355
356 m_timeFromStart += timeSlice;
357 }
358
359 private void RotateCamera(float timeSlice, float duration)
360 {
361 vector matF[4];
362 vector matB[4];
363
364 float d = duration / 4;
365
366 if (m_CurrentRotationT <= 1 * d)
367 {
368 Math3D.DirectionAndUpMatrix(vector.Forward, vector.Up, matF);
369 Math3D.DirectionAndUpMatrix(vector.Right, vector.Up, matB);
370 }
371 else
372 if (m_CurrentRotationT <= 2 * d)
373 {
374 Math3D.DirectionAndUpMatrix(vector.Right, vector.Up, matF);
375 Math3D.DirectionAndUpMatrix(-vector.Forward, vector.Up, matB);
376 }
377 else
378 if (m_CurrentRotationT <= 3 * d)
379 {
380 Math3D.DirectionAndUpMatrix(-vector.Forward, vector.Up, matF);
381 Math3D.DirectionAndUpMatrix(-vector.Right, vector.Up, matB);
382 }
383 else
384 {
385 Math3D.DirectionAndUpMatrix(-vector.Right, vector.Up, matF);
386 Math3D.DirectionAndUpMatrix(vector.Forward, vector.Up, matB);
387 }
388
389 float quatF[4];
390 Math3D.MatrixToQuat(matF, quatF);
391
392 float quatB[4];
393 Math3D.MatrixToQuat(matB, quatB);
394
395 float outQuat[4];
396
397 float t = m_CurrentRotationT;
398 if (m_CurrentRotationT >= 3.0 * d)
399 {
400 t = (t - 3.0 * d) / d;
401 }
402 else
403 if (m_CurrentRotationT >= 2.0 * d)
404 {
405 t = (t - 2.0 * d) / d;
406 }
407 else
408 if (m_CurrentRotationT >= 1.0 * d)
409 {
410 t = (t - 1.0 * d) / d;
411 }
412 else
413 {
414 t = t / d;
415 }
416
417 Math3D.QuatLerp(outQuat, quatF, quatB, t);
418
419 m_CurrentRotationT += timeSlice;
420
421 vector angles = Math3D.QuatToAngles(outQuat);
422
423 m_camera.SetYawPitchRoll(angles);
424 }
425
426 private string QuatToString(float q[4])
427 {
428 return "(" + q[0] + "," + q[1] + "," + q[2] + "," + q[3] + ")";
429 }
430
431 private float GetYDistance(float x, float z)
432 {
433 float wy = GetWorld().GetSurfaceY(x, z);
434 float oh = 0;
435 if (GetWorld().IsOcean())
436 {
437 oh = GetWorld().GetOceanBaseHeight();
438 oh = Math.Ceil(Math.AbsFloat(oh));
439 }
440
441 float hh = wy;
442 if (oh > wy)
443 {
444 hh = oh;
445 }
446
447 return hh;
448 }
449
450}
AutotestGridClass m_LastTime
float m_Max
void MotionZone(IEntitySource src, IEntity parent)
MotionAutoTestClass m_Min
float ComputeAverage()
ref array< string > angles
enum SCR_ECompassType EntityEditorProps(category:"GameScripted/Gadgets", description:"Compass", color:"0 0 255 255")
Prefab data class for compass component.
void Restart(MissionWorkshopItem scenario)
vector direction
proto external void RequestClose()
Setting request flag for engine to exit the game.
proto external bool IsPreloadFinished()
If preload (started with BeginPreload method) is finished, returns true.
proto external int GetLoadTime()
Returns load time in milliseconds for the lastly loaded world.
proto external bool BeginPreload(notnull BaseWorld world, vector pos, float radius, int maxTime_sec=60)
Begin preloading on given position with given radius. Call is non-blocking, to get status of preload ...
proto external IEntity FindEntity(string name)
sealed WorkspaceWidget GetWorkspace()
Definition Game.c:16
void IEntity(IEntitySource src, IEntity parent)
protected script Constructor
proto external EntityEvent SetEventMask(EntityEvent e)
proto external BaseWorld GetWorld()
proto external EntityFlags SetFlags(EntityFlags flags, bool recursively=false)
proto external string GetName()
Game g_Game
Game singleton instance.
Definition gameLib.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
SCR_FieldOfViewSettings Attribute
EntityEvent
Various entity events.
Definition EntityEvent.c:14
EntityFlags
Various entity flags.
Definition EntityFlags.c:14
FileMode
Mode for opening file. See FileSystem::Open.
Definition FileMode.c:14
proto native vector Vector(float x, float y, float z)
WidgetFlags
Widget flags. See enf::Widget::SetFlags().
Definition WidgetFlags.c:14
TypeID WidgetType
Definition EnWidgets.c:6