-
Notifications
You must be signed in to change notification settings - Fork 4
/
rangescript.lua
597 lines (401 loc) · 18 KB
/
rangescript.lua
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
-- DCS - Simple Range Script
-- Version 1.2
-- By Ciribob - https://github.com/ciribob/DCS-SimpleRangeScript
--
-- Change log:
-- - Added more accuracte target distance measurement
-- - Added weapon name for bombing range to scoreboard
-- - Fixed script to work with new weapon names
--
-- Requires MIST 4.0.57 or newer!
-- Inspired by Original Script by SNAFU http://forums.eagle.ru/showthread.php?t=109174
range = {}
range.strafeTargets = {
{
-- GROUP NAME for the unit whos waypoints enclose the target
name = "left_zone",
minAlt = 500,
maxAlt = 1500,
goodPass = 20,
targets = {'Strafe pit Left 3','Strafe pit Left 2','Strafe pit Left 1'}, -- which target(s) are valid for this zone - Unit Names
},
{
name = "right_zone", -- GROUP NAME for the unit whos waypoints enclose the target
minAlt = 500,
maxAlt = 1500,
goodPass = 20,
targets = {'Strafe pit Right 3','Strafe pit Right 2','Strafe pit Right 1'}, -- which target(s) are valid for this zone - Unit Names
}
}
-- Zone Names
range.bombingMinAlt = 1800
range.bombingTargets = {
"target1",
"target2",
"target3",
"target4",
"target5",
"target6",
"target7",
"target8",
"target9",
"target10",
"target11",
"target12",
"target13",
"target14",
"target15",
}
function range.displayMyStrafePitResults(_unitName)
local _unit = Unit.getByName(_unitName)
if _unit and _unit:getPlayerName() then
local _message = "My Top 10 Strafe Pit Results: \n"
local _results = range.strafePlayerResults[_unit:getPlayerName()]
if _results == nil then
_message = _unit:getPlayerName()..": No Score yet"
else
local _sort = function( a,b ) return a.hits > b.hits end
table.sort(_results,_sort)
local _bestMsg = ""
local _count = 1
for _,_result in pairs(_results) do
_message = _message.."\n"..string.format("%s - Hits %i - %s",_result.zone.name,_result.hits,_result.text)
if _bestMsg == "" then
_bestMsg = string.format("%s - Hits %i - %s",_result.zone.name,_result.hits,_result.text)
end
-- 10 runs
if _count == 10 then
break
end
_count = _count+1
end
_message = _message .."\n\nBEST: ".._bestMsg
end
range.displayMessageToGroup(_unit, _message, 10,false)
end
end
function range.displayStrafePitResults(_unitName)
local _unit = Unit.getByName(_unitName)
local _playerResults = {}
if _unit and _unit:getPlayerName() then
local _message = "Strafe Pit Results - Top 10:\n"
for _playerName,_results in pairs(range.strafePlayerResults) do
local _best = nil
for _,_result in pairs(_results) do
if _best == nil or _result.hits > _best.hits then
_best = _result
end
end
if _best ~= nil then
table.insert(_playerResults,{msg = string.format("%s: %s - Hits %i - %s",_playerName,_best.zone.name,_best.hits,_best.text),hits = _best.hits})
end
end
--sort list!
local _sort = function( a,b ) return a.hits > b.hits end
table.sort(_playerResults,_sort)
for _i = 1, #_playerResults do
_message = _message.."\n[".._i.."]".._playerResults[_i].msg
--top 10
if _i > 10 then
break
end
end
range.displayMessageToGroup(_unit, _message, 10,false)
end
end
function range.resetRangeStats(_unitName)
local _unit = Unit.getByName(_unitName)
if _unit and _unit:getPlayerName() then
range.strafePlayerResults[_unit:getPlayerName()] = nil
range.bombingTargets[_unit:getPlayerName()] = nil
range.displayMessageToGroup(_unit, "Range Stats Cleared", 10,false)
end
end
function range.displayMyBombingResults(_unitName)
local _unit = Unit.getByName(_unitName)
if _unit and _unit:getPlayerName() then
local _message = "My Top 20 Bombing Results: \n"
local _results = range.bombPlayerResults[_unit:getPlayerName()]
if _results == nil then
_message = _unit:getPlayerName()..": No Score yet"
else
local _sort = function( a,b ) return a.distance < b.distance end
table.sort(_results,_sort)
local _bestMsg = ""
local _count = 1
for _,_result in pairs(_results) do
_message = _message.."\n"..string.format("%s - %s - %i m",_result.name,_result.weapon,_result.distance)
if _bestMsg == "" then
_bestMsg = string.format("%s - %s - %i m",_result.name,_result.weapon,_result.distance)
end
-- 20 runs
if _count == 20 then
break
end
_count = _count+1
end
_message = _message .."\n\nBEST: ".._bestMsg
end
range.displayMessageToGroup(_unit, _message, 10,false)
end
end
function range.displayBombingResults(_unitName)
local _unit = Unit.getByName(_unitName)
local _playerResults = {}
if _unit and _unit:getPlayerName() then
local _message = "Bombing Results - Top 15:\n"
for _playerName,_results in pairs(range.bombPlayerResults) do
local _best = nil
for _,_result in pairs(_results) do
if _best == nil or _result.distance < _best.distance then
_best = _result
end
end
if _best ~= nil then
table.insert(_playerResults,{msg = string.format("%s: %s - %s - %i m",_playerName,_best.name,_best.weapon,_best.distance),distance = _best.distance})
end
end
--sort list!
local _sort = function( a,b ) return a.distance < b.distance end
table.sort(_playerResults,_sort)
for _i = 1, #_playerResults do
_message = _message.."\n[".._i.."] ".._playerResults[_i].msg
--top 15
if _i > 15 then
break
end
end
range.displayMessageToGroup(_unit, _message, 10,false)
end
end
-- Handles all world events
range.eventHandler = {}
function range.eventHandler:onEvent(_eventDCS)
if _eventDCS == nil or _eventDCS.initiator == nil then
return true
end
local status, err = pcall(function(_event)
if _event.id == 15 then --player entered unit
-- env.info("Player entered unit")
if _event.initiator:getPlayerName() then
-- reset current status
range.strafeStatus[_event.initiator:getID()] = nil
range.addF10Commands(_event.initiator:getName())
if range.planes[_event.initiator:getID()] ~= true then
range.planes[_event.initiator:getID()] = true
range.checkInZone(_event.initiator:getName())
end
end
return true
elseif _event.id == world.event.S_EVENT_HIT and _event.target then
-- env.info("HIT! ".._event.target:getName().." with ".._event.weapon:getTypeName())
--_event.weapon is currently broken for clients
-- env.info(_event.initiator:getPlayerName().."HIT! ".._event.target:getName().." with ".._event.weapon:getTypeName())
-- trigger.action.outText("HIT! ".._event.target:getName().." with ".._event.weapon:getTypeName(),10,false)
local _currentTarget = range.strafeStatus[_event.initiator:getID()]
if _currentTarget then
for _, _targetName in pairs(_currentTarget.zone.targets) do
if _targetName == _event.target:getName() then
_currentTarget.hits = _currentTarget.hits + 1
return true
end
end
end
elseif _event.id == world.event.S_EVENT_SHOT then
local _weapon = _event.weapon:getTypeName()
local _weaponName = _weapon
if _event.initiator:getPosition().p.y > range.bombingMinAlt
then
local _ordnance = _event.weapon
env.info("Tracking ".._weapon.." - ".._ordnance:getName())
local _lastBombPos = {x=0,y=0,z=0}
local _unitName = _event.initiator:getName()
local trackBomb = function(_previousPos)
local _unit = Unit.getByName(_unitName)
-- env.info("Checking...")
if _unit ~= nil and _unit:getPlayerName() ~= nil then
-- when the pcall returns a failure the weapon has hit
local _status,_bombPos = pcall(function()
-- env.info("protected")
return _ordnance:getPoint()
end)
if _status then
--ok! still in the air
_lastBombPos = {x = _bombPos.x, y = _bombPos.y, z= _bombPos.z }
return timer.getTime() + 0.005 -- check again !
else
--hit
-- get closet target to last position
local _closetTarget = nil
local _distance = nil
for _,_targetZone in pairs(range.bombingTargets) do
local _temp = range.getDistance(_targetZone.point, _lastBombPos)
if _distance == nil or _temp < _distance then
_distance = _temp
_closetTarget = _targetZone
end
end
-- env.info(_distance.." from ".._closetTarget.name)
if _distance < 1000 then
if not range.bombPlayerResults[_unit:getPlayerName()] then
range.bombPlayerResults[_unit:getPlayerName()] = {}
end
local _results = range.bombPlayerResults[_unit:getPlayerName()]
table.insert(_results,{name=_closetTarget.name, distance =_distance, weapon = _weaponName })
local _message = string.format("%s - %i m from bullseye of %s",_unit:getPlayerName(), _distance,_closetTarget.name)
trigger.action.outText(_message,10,false)
end
end
end
return
end
timer.scheduleFunction(trackBomb, nil, timer.getTime() + 1)
end
end
return true
end, _eventDCS)
if (not status) then
env.error(string.format("Error while handling event %s", err),false)
end
end
function range.checkInZone(_unitName)
--check if we're in any zone
-- if we're in a zone, start looking for hits on target
-- if we're no longer in a zone but were previously, list the result and store the run
local _unit = Unit.getByName(_unitName)
if _unit and _unit:getPlayerName() then
timer.scheduleFunction(range.checkInZone, _unitName, timer.getTime() + 1)
local _unitPos = _unit:getPosition().p
-- currently strafing?
local _currentStrafeRun = range.strafeStatus[_unit:getID()]
if _currentStrafeRun ~= nil then
if _currentStrafeRun.zone.polygon~=nil and mist.pointInPolygon(_unitPos,_currentStrafeRun.zone.polygon,_currentStrafeRun.zone.maxAlt) and _unitPos.y >= _currentStrafeRun.zone.minAlt then
--still in zone, do nothing
_currentStrafeRun.time = _currentStrafeRun.time+1
elseif _currentStrafeRun.zone.polygon~=nil then
_currentStrafeRun.time = _currentStrafeRun.time+1
if _currentStrafeRun.time <= 3 then
range.strafeStatus[_unit:getID()] = nil
local _msg = _unit:getPlayerName()..": left ".._currentStrafeRun.zone.." too quickly. No Score. "
range.displayMessageToGroup(_unit, _msg, 10,true)
else
local _result = range.strafeStatus[_unit:getID()]
local _msg = _unit:getPlayerName().." "
if _result.hits >= _result.zone.goodPass then
_msg = _msg .."GOOD PASS with ".._result.hits.." on "
_result.text = "GOOD PASS"
else
_msg = _msg .."INEFFECTIVE PASS with ".._result.hits.." on "
_result.text = "INEFFECTIVE PASS"
end
_msg = _msg .._result.zone.name
trigger.action.outText(_msg,10,false)
range.strafeStatus[_unit:getID()] = nil
-- Save so the player can retrieve them
local _stats = range.strafePlayerResults[_unit:getPlayerName()] or {}
table.insert(_stats,_result)
range.strafePlayerResults[_unit:getPlayerName()] = _stats
end
end
else
-- check to see if we're in a zone
for _,_targetZone in pairs(range.strafeTargets) do
if _targetZone.polygon~=nil and mist.pointInPolygon(_unitPos,_targetZone.polygon,_targetZone.maxAlt) then
if range.strafeStatus[_unit:getID()] == nil and _unitPos.y >= _targetZone.minAlt then
range.strafeStatus[_unit:getID()] = {hits = 0, zone = _targetZone, time = 1 }
local _msg = _unit:getPlayerName().." rolling in on ".._targetZone.name
range.displayMessageToGroup(_unit, _msg, 10,true)
end
break
end
end
end
else
timer.scheduleFunction(range.checkInZone, _unitName, timer.getTime() + 5)
end
end
function range.getGroupId(_unit)
local _unitDB = mist.DBs.unitsById[tonumber(_unit:getID())]
if _unitDB ~= nil and _unitDB.groupId then
return _unitDB.groupId
end
return nil
end
function range.displayMessageToGroup(_unit, _text, _time,_clear)
local _groupId = range.getGroupId(_unit)
if _groupId then
if _clear == true then
trigger.action.outTextForGroup(_groupId, _text, _time,_clear)
else
trigger.action.outTextForGroup(_groupId, _text, _time)
end
end
end
--range.gunTypes ={"weapons.shells.GAU8_30_AP","weapons.shells.GAU8_30_HE","weapons.shells.GAU8_30_TP","weapons.shells.M61_20_HE","weapons.shells.M61_20_AP","weapons.shells.M2_12_7_t","weapons.shells.7_62x51","weapons.shells.M134_7_62_T","weapons.shells.M134_7_62x51",
-- "weapons.shells.M20_50_aero_APIT","weapons.shells.M20_50_aero_APIT","weapons.shells.GSH301_30_HE","weapons.shells.GSH301_30_AP","weapons.shells.GSH23_23_HE_T","weapons.shells.2A42_30_HE","weapons.shells.2A42_30_AP","weapons.shells.GSH23_23_HE_T",
-- "weapons.shells.YakB_12_7_T","weapons.shells.YakB_12_7","weapons.shells.PKT_7_62_T","weapons.shells.PKT_7_62","weapons.shells.VOG17"}
range.addedTo = {}
function range.addF10Commands(_unitName)
local _unit = Unit.getByName(_unitName)
if _unit then
local _group = mist.DBs.unitsById[tonumber(_unit:getID())]
if _group then
local _gid = _group.groupId
if not range.addedTo[_gid] then
range.addedTo[_gid] = true
local _rootPath = missionCommands.addSubMenuForGroup(_gid, "Range")
missionCommands.addCommandForGroup(_gid,"My Strafe results", _rootPath, range.displayMyStrafePitResults, _unitName)
missionCommands.addCommandForGroup(_gid,"All Strafe results", _rootPath, range.displayStrafePitResults, _unitName)
missionCommands.addCommandForGroup(_gid,"My Bombing results", _rootPath, range.displayMyBombingResults, _unitName)
missionCommands.addCommandForGroup(_gid,"All Bombing results", _rootPath, range.displayBombingResults, _unitName)
missionCommands.addCommandForGroup(_gid,"Reset Stats", _rootPath, range.resetRangeStats, _unitName)
end
end
end
end
--get distance in meters assuming a Flat world
function range.getDistance(_point1, _point2)
local xUnit = _point1.x
local yUnit = _point1.z
local xZone = _point2.x
local yZone = _point2.z
local xDiff = xUnit - xZone
local yDiff = yUnit - yZone
return math.sqrt(xDiff * xDiff + yDiff * yDiff)
end
--http://stackoverflow.com/questions/1426954/split-string-in-lua
function range.split(str, sep)
local result = {}
local regex = ("([^%s]+)"):format(sep)
for each in str:gmatch(regex) do
table.insert(result, each)
end
return result
end
--init
range.strafeStatus = {}
range.strafePlayerResults = {}
range.bombPlayerResults = {}
range.planes = {}
for _,_targetZone in pairs(range.strafeTargets) do
if Group.getByName(_targetZone.name) then
local _points = mist.getGroupPoints(_targetZone.name)
env.info("Done for: ".._targetZone.name)
_targetZone.polygon = _points
else
env.info("Couldn't find: ".._targetZone.name)
_targetZone.polygon = nil
end
end
local _tempTargets = range.bombingTargets
range.bombingTargets = {}
for _,_targetZone in pairs(_tempTargets) do
local _triggerZone = trigger.misc.getZone(_targetZone)
if _triggerZone then
table.insert(range.bombingTargets,{name=_targetZone,point=_triggerZone.point})
env.info("Done for: ".._targetZone)
else
env.info("Failed for: ".._targetZone)
end
end
world.addEventHandler(range.eventHandler)