-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdetect.sqf
632 lines (513 loc) · 24.2 KB
/
bdetect.sqf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// -------------------------------------------------------------------------------------------
// bDetect | bullet detection framework
// -------------------------------------------------------------------------------------------
// Version: 0.76 BETA
// Date: 17/11/2012
// Author: Fabrizio_T, Ollem (MP code)
// Additional code: TPW
// File Name: bdetect.sqf
// License: GNU/GPL
// -------------------------------------------------------------------------------------------
// BEGINNING OF FRAMEWORK CODE
// -------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------
// Constants
// -------------------------------------------------------------------------------------------
bdetect_name = "bDetect | Bullet Detection Framework";
bdetect_name_short = "bDetect";
bdetect_version = "0.76 BETA";
// -------------------------------------------------------------------------------------------
// Global variables
// -------------------------------------------------------------------------------------------
// You should set these variables elsewhere, don't edit them here since they're default.
// See bottom of this file for framework initialization example.
if(isNil "bdetect_enable") then { bdetect_enable = true; }; // (Boolean, Default true) Toggle to Enable / Disable bdetect altogether.
if(isNil "bdetect_startup_hint") then { bdetect_startup_hint = true; }; // (Boolean, Default true) Toggle to Enable / Disable bDetect startup Hint.
if(isNil "bdetect_debug_enable") then { bdetect_debug_enable = false; }; // (Boolean, Default false) Toggle to Enable / Disable debug messages.
if(isNil "bdetect_debug_chat") then { bdetect_debug_chat = false; }; // (Boolean, Default false) Show debug messages also in globalChat.
if(isNil "bdetect_debug_levels") then { bdetect_debug_levels = [0,1,2,3,4,5,6,7,8,9]; }; // (Array, Default [0,1,2,3,4,5,6,7,8,9]) Filter debug messages by included levels.
if(isNil "bdetect_callback") then { bdetect_callback = "bdetect_fnc_callback"; }; // (String, Default "bdetect_fnc_callback") Name for your own callback function
if(isNil "bdetect_callback_mode") then { bdetect_callback_mode = "spawn"; }; // (String, Default "spawn") Allowed values: "call" or "spawn"
if(isNil "bdetect_fps_adaptation") then { bdetect_fps_adaptation = true; }; // (Boolean, Default true) Whether bDetect should try to keep over "bdetect_fps_min" FPS while degrading quality of detection
if(isNil "bdetect_fps_min") then { bdetect_fps_min = 15; }; // (Number, Default 20) The minimum FPS you wish to keep
if(isNil "bdetect_fps_calc_each_x_frames") then { bdetect_fps_calc_each_x_frames = 16; }; // (Number, Default 16) FPS check is done each "bdetect_fps_min" frames. 1 means each frame.
if(isNil "bdetect_eh_assign_cycle_wait") then { bdetect_eh_assign_cycle_wait = 10; }; // (Seconds, Default 10). Wait duration foreach cyclic execution of bdetect_fnc_eh_loop()
if(isNil "bdetect_bullet_min_delay") then { bdetect_bullet_min_delay = 0.1; }; // (Seconds, Default 0.1) Minimum time between 2 consecutive shots fired by an unit for the last bullet to be tracked. Very low values may cause lag.
if(isNil "bdetect_bullet_max_delay") then { bdetect_bullet_max_delay = 2; }; // (Seconds, Default 2)
if(isNil "bdetect_bullet_initial_min_speed") then { bdetect_bullet_initial_min_speed = 360; }; // (Meters/Second, Default 360) Bullets slower than this are ignored.
if(isNil "bdetect_bullet_max_proximity") then { bdetect_bullet_max_proximity = 10; }; // (Meters, Default 10) Maximum proximity to unit for triggering detection
if(isNil "bdetect_bullet_min_distance") then { bdetect_bullet_min_distance = 20; }; // (Meters, Default 25) Bullets having travelled less than this distance are ignored
if(isNil "bdetect_bullet_max_distance") then { bdetect_bullet_max_distance = 500; }; // (Meters, Default 500) Bullets havin travelled more than distance are ignored
if(isNil "bdetect_bullet_max_lifespan") then { bdetect_bullet_max_lifespan = 0.7; }; // (Seconds, Default 0.7) Bullets living more than these seconds are ignored
if(isNil "bdetect_bullet_max_height") then { bdetect_bullet_max_height = 8; }; // (Meters, Default 8) Bullets going higher than this -and- diverging from ground are ignored
if(isNil "bdetect_bullet_skip_mags") then { bdetect_bullet_skip_mags = []; }; // (Array) Skip these bullet types altogether. Example: ["30rnd_9x19_MP5", "30rnd_9x19_MP5SD", "15Rnd_9x19_M9"]
if(isNil "bdetect_mp_enable") then { bdetect_mp_enable = true; }; // (Boolean, Default true) Toggle to Enable / Disable MP experimental support
if(isNil "bdetect_mp_per_frame_native_163beta") then { bdetect_mp_per_frame_native_163beta = false; }; // (Boolean, Default true) Pre-frame Detection using "onEachFrame" scripting command, avalilable since ArmA 2 OA 1.62.98866
if(isNil "bdetect_mp_per_frame_emulation") then { bdetect_mp_per_frame_emulation = false; }; // (Boolean, Default false) Toggle to Enable / Disable experimental server per-frame-execution emulation
if(isNil "bdetect_mp_per_frame_emulation_frame_d") then { bdetect_mp_per_frame_emulation_frame_d = 0.02; }; // (Seconds, Default 0.02) Experimental server per-frame-execution emulation timeout
if(isNil "bdetect_units_kindof") then { bdetect_units_kindof = ["Man"]; }; // CfgVehicles classes being subject to suppression effects, example: ["Man","StaticWeapon","Car","Tank","Air"]
// Please NEVER edit the variables below.
if(isNil "bdetect_fired_bullets") then { bdetect_fired_bullets = []; };
if(isNil "bdetect_fired_bullets_count") then { bdetect_fired_bullets_count = 0; };
if(isNil "bdetect_fired_bullets_count_west") then { bdetect_fired_bullets_count_west = 0; };
if(isNil "bdetect_fired_bullets_count_east") then { bdetect_fired_bullets_count_east = 0; };
if(isNil "bdetect_fired_bullets_count_tracked") then { bdetect_fired_bullets_count_tracked = 0; };
if(isNil "bdetect_fired_bullets_count_detected") then { bdetect_fired_bullets_count_detected = 0; };
if(isNil "bdetect_fired_bullets_count_blacklisted") then { bdetect_fired_bullets_count_blacklisted = 0; };
if(isNil "bdetect_units_count") then { bdetect_units_count = 0; };
if(isNil "bdetect_units_count_killed") then { bdetect_units_count_killed = 0; };
if(isNil "bdetect_fps") then { bdetect_fps = bdetect_fps_min; };
if(isNil "bdetect_bullet_delay") then { bdetect_bullet_delay = bdetect_bullet_min_delay; };
if(isNil "bdetect_frame_tstamp") then { bdetect_frame_tstamp = 0; };
if(isNil "bdetect_frame_min_duration") then { bdetect_frame_min_duration = 0.015; }; // 60fps
// -------------------------------------------------------------------------------------------
// Functions
// -------------------------------------------------------------------------------------------
bdetect_fnc_per_frame_emulation =
{
private ["_fnc", "_msg"];
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_per_frame_emulation() has started"];
[ _msg, 8 ] call bdetect_fnc_debug;
};
while { true } do {
call bdetect_fnc_detect;
sleep bdetect_mp_per_frame_emulation_frame_d;
};
};
bdetect_fnc_init =
{
private [ "_msg", "_nul" ];
if( bdetect_debug_enable ) then {
_msg = format["%1 v%2 is starting ...", bdetect_name_short, bdetect_version];
[ _msg, 0 ] call bdetect_fnc_debug;
};
// bullet speed converted to kmh
bdetect_bullet_initial_min_speed = bdetect_bullet_initial_min_speed * 3.6;
// Add per-frame execution of time-critical function
if( bdetect_mp_enable && bdetect_mp_per_frame_emulation ) then { // emulated, per-timeout (MP)
_nul = [] spawn bdetect_fnc_per_frame_emulation;
}
else
{
// native per-frame (SP)
if(bdetect_mp_per_frame_native_163beta) then
{
onEachFrame { call bdetect_fnc_detect; };
}
else
{
[bdetect_fnc_detect,0] call cba_fnc_addPerFrameHandler;
};
};
// Assign event handlers to any units (even spawned ones)
bdetect_spawned_loop_handler = [] spawn bdetect_fnc_eh_loop;
};
// Keep searching units for newly spawned ones and assign fired EH to them
bdetect_fnc_eh_loop =
{
private [ "_x", "_msg"];
while { true } do // iteratively add EH to all units spawned at runtime
{
{ [_x] call bdetect_fnc_eh_add; } foreach allUnits; // Loop onto all units
if( isNil "bdetect_init_done" ) then
{
bdetect_init_done = true;
if( bdetect_debug_enable ) then {
_msg = format["%1 v%2 has started", bdetect_name_short, bdetect_version];
[ _msg, 0 ] call bdetect_fnc_debug;
};
if( bdetect_startup_hint ) then {
_msg = format["%1 v%2 has started", bdetect_name_short, bdetect_version];
hint _msg;
};
};
sleep bdetect_eh_assign_cycle_wait;
};
};
bdetect_fnc_eh_add =
{
private ["_unit", "_vehicle", "_e", "_msg"];
_unit = _this select 0;
_vehicle = assignedVehicle _unit;
if( isNull _vehicle && _unit != vehicle _unit ) then { _vehicle = vehicle _unit };
if( ( isNil { _unit getVariable "bdetect_fired_eh" })) then
{
_e = _unit addEventHandler ["Fired", bdetect_fnc_fired];
_unit setVariable ["bdetect_fired_eh", _e];
_e = _unit addEventHandler ["Killed", bdetect_fnc_killed];
_unit setVariable ["bdetect_killed_eh", _e];
bdetect_units_count = bdetect_units_count + 1;
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_eh_add() - unit=%1, FIRED + KILLED EH assigned ", _unit];
[ _msg, 3 ] call bdetect_fnc_debug;
};
};
// handling vehicles
if(
!( isNull _vehicle )
&& isNil { _vehicle getVariable "bdetect_fired_eh" }
) then {
_e = _vehicle addEventHandler ["Fired", bdetect_fnc_fired];
_vehicle setVariable ["bdetect_fired_eh", _e];
_e = _vehicle addEventHandler ["Killed", bdetect_fnc_killed];
_vehicle setVariable ["bdetect_killed_eh", _e];
bdetect_units_count = bdetect_units_count + 1;
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_eh_add() - vehicle=%1, FIRED + KILLED EH assigned", _vehicle];
[ _msg, 3 ] call bdetect_fnc_debug;
};
};
};
// Killed EH payload
bdetect_fnc_killed =
{
private ["_unit", "_killer", "_e", "_msg"];
_unit = _this select 0;
_killer = _this select 1;
// Remove FIRED EH
if( !isNil{ _unit getVariable "bdetect_fired_eh" } ) then
{
_e = _unit getVariable "bdetect_fired_eh";
_unit removeEventHandler ["fired", _e];
_unit setVariable ["bdetect_fired_eh", nil];
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_killed() - unit=%1, FIRED EH unassigned", _unit];
[ _msg, 3 ] call bdetect_fnc_debug;
};
};
// Remove (local) KILLED EH
if( !isNil{ _unit getVariable "bdetect_killed_eh" } ) then
{
//_e = _unit getVariable "bdetect_killed_eh";
//_unit removeEventHandler ["Killed", _e];// bug: in ArmA2 1.61 beta removing this cause all others killed EH to be removed?
_unit setVariable ["bdetect_killed_eh", nil];
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_killed() - unit=%1, KILLED EH unassigned", _unit];
[ _msg, 3 ] call bdetect_fnc_debug;
};
};
bdetect_units_count_killed = bdetect_units_count_killed + 1;
};
// Fired EH payload
bdetect_fnc_fired =
{
private ["_unit", "_weapon", "_muzzle", "_magazine", "_bullet", "_speed", "_msg", "_time", "_dt"];
if( bdetect_enable ) then
{
_unit = _this select 0;
_weapon = _this select 1;
_muzzle = _this select 2;
_mode = _this select 3;
_ammo = _this select 4;
_magazine = _this select 5;
_bullet = _this select 6;
_speed = speed _bullet;
_time = time; //diag_tickTime
_dt = _time - ( _unit getVariable ["bdetect_fired_time", 0] );
bdetect_fired_bullets_count = bdetect_fired_bullets_count + 1;
if(side _unit == WEST) then {
bdetect_fired_bullets_count_west = bdetect_fired_bullets_count_west + 1;
} else {
bdetect_fired_bullets_count_east = bdetect_fired_bullets_count_east + 1;
};
if( _dt > bdetect_bullet_delay
&& !( _magazine in bdetect_bullet_skip_mags )
&& _speed > bdetect_bullet_initial_min_speed ) then {
_unit setVariable ["bdetect_fired_time", _time];
// Append info to bullets array
[ _bullet, _unit, _time ] call bdetect_fnc_bullet_add;
bdetect_fired_bullets_count_tracked = bdetect_fired_bullets_count_tracked + 1;
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_fired() - Tracking: bullet=%1, shooter=%2, speed=%3, type=%4, _dt=%5", _bullet, _unit, _speed, typeOf _bullet, _dt ];
[ _msg, 2 ] call bdetect_fnc_debug;
};
}
else
{
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_fired() - Skipping: bullet=%1, shooter=%2, speed=%3 [min:%8], type=%4, _dt=%5 [min:%6 max:%7]", _bullet, _unit, _speed, typeOf _bullet, _dt, bdetect_bullet_min_delay, bdetect_bullet_max_delay, bdetect_bullet_initial_min_speed];
[ _msg, 2 ] call bdetect_fnc_debug;
};
};
};
};
// Time-critical detection function, to be executed per-frame
bdetect_fnc_detect =
{
private ["_tot", "_msg"];
if( bdetect_enable ) then
{
private ["_t", "_dt"];
_t = time; //diag_tickTime
_dt = _t - bdetect_frame_tstamp;
if( _dt >= bdetect_frame_min_duration ) then
{
bdetect_frame_tstamp = _t;
if( bdetect_debug_enable && diag_frameno % 32 == 0) then {
_msg = format["bdetect_fnc_detect() - Frame: duration=%1 (min=%2), FPS=%3", _dt , bdetect_frame_min_duration, diag_fps ];
[ _msg, 1 ] call bdetect_fnc_debug;
};
if( bdetect_fps_adaptation && diag_frameno % bdetect_fps_calc_each_x_frames == 0) then {
call bdetect_fnc_diag_min_fps;
};
_tot = count bdetect_fired_bullets;
if ( _tot > 0 ) then
{
[ _tot, _t ] call bdetect_fnc_detect_sub;
};
};
bdetect_fired_bullets = bdetect_fired_bullets - [-1];
};
};
// Subroutine executed within bdetect_fnc_detect()
bdetect_fnc_detect_sub =
{
private ["_tot", "_t", "_n", "_bullet", "_data", "_shooter", "_pos", "_time", "_blacklist", "_update_blacklist", "_bpos", "_dist", "_units", "_x", "_data", "_nul"];
_tot = _this select 0;
_t = _this select 1;
for "_n" from 0 to _tot - 1 step 2 do
{
_bullet = bdetect_fired_bullets select _n;
_data = bdetect_fired_bullets select (_n + 1);
_shooter = _data select 0;
_pos = _data select 1;
_time = _data select 2;
_blacklist = _data select 3;
_update_blacklist = false;
if( !( isnull _bullet ) ) then
{
_bpos = getPosATL _bullet;
_dist = _bpos distance _pos;
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_detect_sub() - Flying: bullet=%1, lifespan=%2, distance=%3, speed=%4, position=%5", _bullet, _t - _time, round _dist, round (speed _bullet / 3.6), getPosASL _bullet];
[ _msg, 2 ] call bdetect_fnc_debug;
};
};
if( isNull _bullet
|| !(alive _bullet)
|| _t - _time > bdetect_bullet_max_lifespan
|| _dist > bdetect_bullet_max_distance
|| speed _bullet < bdetect_bullet_initial_min_speed // funny rebounds handling
|| ( ( _bpos select 2) > bdetect_bullet_max_height && ( ( vectordir _bullet ) select 2 ) > 0.2 )
) then {
[ _bullet ] call bdetect_fnc_bullet_tag_remove;
}
else
{
if( _dist > bdetect_bullet_min_distance ) then {
_units = _bpos nearEntities [ bdetect_units_kindof, bdetect_bullet_max_proximity];
{
if( _x != _shooter && local _x && lifestate _x == "ALIVE" ) then
{
if( _x in _blacklist ) then
{
bdetect_fired_bullets_count_blacklisted = bdetect_fired_bullets_count_blacklisted + 1;
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_detect_sub() - blacklisted: bullet=%1, unit=%2, shooter=%3", _bullet, _x, _shooter];
[ _msg, 5 ] call bdetect_fnc_debug;
};
}
else
{
_blacklist set [ count _blacklist, _x];
_update_blacklist = true;
bdetect_fired_bullets_count_detected = bdetect_fired_bullets_count_detected + 1;
// compile callback name into function
bdetect_callback_compiled = call compile format["%1", bdetect_callback];
if( bdetect_callback_mode == "spawn" ) then {
_nul = [ _x, _shooter, _bullet, _bpos, _pos, _time ] spawn bdetect_callback_compiled;
} else {
[ _x, _shooter, _bullet, _bpos, _pos, _time ] call bdetect_callback_compiled;
};
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_detect_sub() - *** CLOSE BULLET ***: unit=%1, bullet=%2, shooter=%3, proximity=%4, data=%5", _x, _bullet, _shooter, round (_x distance _bpos), _data];
[ _msg, 9 ] call bdetect_fnc_debug;
};
};
};
} foreach _units;
if(_update_blacklist) then {
bdetect_fired_bullets set[ _n + 1, [_shooter, _pos, _time, _blacklist] ]; // Update blacklist
};
};
};
};
};
// Adapt frequency of some bullet checking depending on minimum FPS
bdetect_fnc_diag_min_fps =
{
private ["_fps", "_msg"];
_fps = diag_fps;
if( _fps < bdetect_fps_min * 1.1) then
{
if( bdetect_bullet_delay < bdetect_bullet_max_delay ) then {
bdetect_bullet_delay = ( ( bdetect_bullet_delay + 0.2) min bdetect_bullet_max_delay );
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_diag_min_fps() - FPS = %1, bdetect_bullet_delay = %2", _fps, bdetect_bullet_delay];
[ _msg, 1 ] call bdetect_fnc_debug;
};
};
}
else
{
if( bdetect_bullet_delay > bdetect_bullet_min_delay ) then {
bdetect_bullet_delay = (bdetect_bullet_delay - 0.1) max bdetect_bullet_min_delay;
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_diag_min_fps() - FPS = %1, bdetect_bullet_delay = %2", _fps, bdetect_bullet_delay];
[ _msg, 1 ] call bdetect_fnc_debug;
};
};
};
bdetect_fps = _fps;
};
// Add a bullet to bdetect_fired_bullets
bdetect_fnc_bullet_add =
{
private ["_bullet", "_shooter", "_pos", "_time", "_msg", "_n"];
_bullet = _this select 0; // bullet object
_shooter = _this select 1; // shooter
_pos = getPosATL _bullet; // bullet start position
_time = _this select 2; // bullet shoot time
_n = count bdetect_fired_bullets;
bdetect_fired_bullets set [ _n, _bullet ];
bdetect_fired_bullets set [ _n + 1, [ _shooter, _pos, _time, [] ] ];
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_bullet_add() - Bullet=%1, shooter=%2, bullets(%3)= %4", _bullet, _shooter, _n / 2, bdetect_fired_bullets];
[ _msg, 2] call bdetect_fnc_debug;
};
};
// Tag a bullet to be removed from bdetect_fired_bullets
bdetect_fnc_bullet_tag_remove =
{
private ["_bullet", "_n", "_msg" ];
_bullet = _this select 0;
_n = bdetect_fired_bullets find _bullet;
if( _n != -1 ) then
{
bdetect_fired_bullets set[ _n, -1 ];
bdetect_fired_bullets set[ _n + 1, -1 ];
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_bullet_tag_remove() - tagging null/expired bullet to be removed"];
[ _msg, 2 ] call bdetect_fnc_debug;
};
};
};
// Prototype for callback function to be executed within bdetect_fnc_detect
bdetect_fnc_callback =
{
private [ "_unit", "_shooter", "_bullet", "_bpos", "_pos", "_time", "_proximity", "_msg" ];
_unit = _this select 0; // unit being under fire
_shooter = _this select 1; // shooter
_bullet = _this select 2; // bullet object
_bpos = _this select 3; // bullet position
_pos = _data select 4; // starting position of bullet
_time = _data select 5; // starting time of bullet
_proximity = _bpos distance _unit; // distance between _bullet and _unit
if( bdetect_debug_enable ) then {
_msg = format["bdetect_fnc_callback() - unit=%1, bullet=%2, shooter=%3, proximity=%4, data=%5", _unit, _bullet, _shooter, _proximity, _data];
[ _msg, 9 ] call bdetect_fnc_debug;
};
};
// function to display and log stuff (into .rpt file) level zero is intended only for builtin messages
bdetect_fnc_debug =
{
private [ "_msg", "_level"];
/*
DEBUG LEVELS:
From 0-9 are reserved.
0 = Startup / unclassified messages
1 = Frames / FPS related messages
2 = "bdetect_fired_bullets" related messages
3 = EH related messages
...
5 = Unit blacklist messages
...
8 = MP related messages
9 = Unit detection related messages
*/
_level = _this select 1;
if( bdetect_debug_enable && _level in bdetect_debug_levels) then
{
_msg = _this select 0;
diag_log format["%1 [%2 v%3] Frame:%4 L%5: %6", time, bdetect_name_short, bdetect_version, diag_frameno, _level, _msg ];
if( bdetect_debug_chat ) then {
player globalchat format["%1 - %2", time, _msg ];
};
};
};
bdetect_fnc_benchmark =
{
private ["_cnt"];
if(isNil "bdetect_stats_max_bullets") then { bdetect_stats_max_bullets = 0; };
if(isNil "bdetect_stats_min_fps") then { bdetect_stats_min_fps = 999; };
if(isNil "bdetect_fired_bullets") then { bdetect_fired_bullets = []; };
_nul = [] spawn
{
sleep 5;
while { true } do
{
_cnt = count ( bdetect_fired_bullets ) / 2;
if( _cnt > bdetect_stats_max_bullets ) then { bdetect_stats_max_bullets = _cnt; };
if( diag_fps < bdetect_stats_min_fps ) then { bdetect_stats_min_fps = diag_fps };
hintsilent format["TIME: %1\nFPS: %2 (min: %3)\nBULLETS: %4 (max: %5)\nS.DELAY: %6 (Min FPS: %7)\nFIRED: %8 (W%9 E%10)\nTRACKED: %11\nDETECTED: %12\nBLACKLISTED: %13\nUNITS: %14\nKILLED: %15",
time,
diag_fps,
bdetect_stats_min_fps,
_cnt,
bdetect_stats_max_bullets,
bdetect_bullet_delay,
bdetect_fps_min,
bdetect_fired_bullets_count,
bdetect_fired_bullets_count_west,
bdetect_fired_bullets_count_east,
bdetect_fired_bullets_count_tracked,
bdetect_fired_bullets_count_detected,
bdetect_fired_bullets_count_blacklisted,
bdetect_units_count,
bdetect_units_count_killed];
sleep .25;
};
};
};
// -------------------------------------------------------------------------------------------
// END OF FRAMEWORK CODE
// -------------------------------------------------------------------------------------------
// BELOW: Example for running the framework
// The below commented code is not part of the framework, just an example of how to run it
// -------------------------------------------------------------------------------------------
/*
// Cut & paste the following code into your own sqf. file.
// Place "bdetect.sqf" into your own mission folder.
// First load the framework
call compile preprocessFileLineNumbers "bdetect.sqf"; // CAUTION: comment this line if you wish to execute the framework from -within- bdetect.sqf file
// Set any optional configuration variables whose value should be other than Default (see all the defined variables in bdetect.sqf, function bdetect_fnc_init() ).
// Below some examples.
bdetect_debug_enable = true; // Example only - Enable debug / logging in .rpt file. Beware, full levels logging may be massive.
bdetect_debug_levels = [0,9]; // Example only - Log only some basic messages (levels 0 and 9). Read comment about levels meanings into "bdetect.sqf, function bdetect_fnc_debug()
bdetect_debug_chat = true; // Example only - log also into globalChat.
// Now name your own unit callback function (the one that will be triggered when a bullet is detected close to an unit)
bdetect_callback = "my_function";
// Define your own callback function and its contents, named as above. Here's a prototypical function:
my_function = {
private [ "_unit", "_shooter", "_bullet", "_bpos", "_pos", "_time", "_proximity", "_msg" ];
_unit = _this select 0; // unit being under fire
_shooter = _this select 1; // shooter
_bullet = _this select 2; // bullet object
_bpos = _this select 3; // bullet position
_pos = _this select 4; // starting position of bullet
_time = _this select 5; // starting time of bullet
_proximity = _bpos distance _unit; // distance between _bullet and _unit
_msg = format["my_function() - [%1] close to bullet %2 fired by %3, proximity=%4m, data=%5", _unit, _bullet, _shooter, _proximity, _data];
[ _msg, 9 ] call bdetect_fnc_debug;
};
// Now initialize framework
sleep 5; // wait some seconds if you want to load other scripts
call bdetect_fnc_init;
// Wait for framework to be fully loaded
waitUntil { !(isNil "bdetect_init_done") };
// You are done. Optional: care to activate display of some runtime stats ?
sleep 5; // wait some more seconds for CPU load to normalize
call bdetect_fnc_benchmark;
// All done, place your other fancy stuff below ...
*/