-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWirtsTools.lua
More file actions
2324 lines (2105 loc) · 74.8 KB
/
WirtsTools.lua
File metadata and controls
2324 lines (2105 loc) · 74.8 KB
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
----------------
--WirtsTools
--version 2.2.3
--Directions: load this script as Do Script File, then call setup functions in a do script action for the features
-- Required Notice: Copyright WirtsLegs 2024, (https://github.com/WirtsLegs/WirtsTools)
----------------
do
WT = {}
WT.utils={}
--add a startswith function to the lua string object
function string.starts(String, Start)
return string.sub(String, 1, string.len(Start)) == Start
end
--protected call (error handling)
local function p(...)
local status, retval = pcall(...)
env.warning(retval, false)
if not status then
return nil
end
return retval
end
local function TableConcat(t1, t2)
for i = 1, #t2 do
t1[#t1 + 1] = t2[i]
end
return t1
end
local function deepCopy(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for index, value in pairs(object) do
new_table[_copy(index)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
local function isInList(list, value)
for _, v in ipairs(list) do
if v == value then
return true
end
end
return false
end
--import zones from mission file
function WT.utils.getZones()
WT.zones = {}
--WT.zones.path =os.getenv('APPDATA').."\\..\\Local\\Temp\\DCS\\Mission\\mission"
--p(dofile,WT.zones.path)
local zones = nil
local zones_in = nil
if env.mission then
zones = {}
zones_in = env.mission.triggers.zones
for z = 1, #zones_in do
zones[zones_in[z]["name"]] = zones_in[z]
end
end
WT.zones = zones
end
function WT.utils.polygon(points)
local polygon = {}
for i, p in ipairs(points) do
if type(p) == "table" and p.x and p.y then
table.insert(polygon, p)
end
end
return polygon
end
function WT.utils.isInPolygon(p, polygon)
-- Part 1, checking wheter point is not inside the bounding box of the polygon. (optional)
local minX, minY, maxX, maxY = polygon[1].x, polygon[1].y, polygon[1].x, polygon[1].y
for i, q in ipairs(polygon) do
minX, maxX, minY, maxY = math.min(q.x, minX), math.max(q.x, maxX), math.min(q.y, minY), math.max(q.y, maxY)
end
if p.x < minX or p.x > maxX or p.y < minY or p.y > maxY then
return false
end
-- If the point is not inside the bounding box of the polygon. it can't be in the polygon.
-- You can delete this first part if you want, it's here just to improve performance.
-- Part 2, logic behind this is explained here https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html
-- it supports multiple components, concave components and holes in polygons as well
local inside = false
local j = #polygon
for i, q in ipairs(polygon) do
if (q.y > p.y) ~= (polygon[j].y > p.y) and p.x < (polygon[j].x - q.x) * (p.y - q.y) / (polygon[j].y - q.y) + q.x then
inside = not (inside)
end
j = i
end
return inside
end
--create event handler
local function newEventHandler(f)
local handler = {}
handler.f = f
function handler:onEvent(event)
self.f(event)
end
world.addEventHandler(handler)
return handler.id
end
function WT.utils.VecMag(vec)
if vec.z == nil then
return (vec.x ^ 2 + vec.y ^ 2) ^ 0.5
else
return (vec.x ^ 2 + vec.y ^ 2 + vec.z ^ 2) ^ 0.5
end
end
WT.tasks={}
WT.tasks.setInvisible = {
id = 'SetInvisible',
params = {
value = true
}
}
WT.tasks.setVisible = {
id = 'SetInvisible',
params = {
value = false
}
}
WT.tasks.groundMission = {
id = 'Mission',
params = {
airborne = false,
route = {
points = {},
}
}
}
WT.tasks.airMission = {
id = 'Mission',
params = {
airborne = true,
route = {
points = {},
}
}
}
function WT.utils.isInCircle(p, r, c)
return WT.utils.VecMag({ x = p.x - c.x, y = 0, z = p.z - c.z }) < r
end
function WT.utils.explodePoint(args)
trigger.action.explosion(args.point, args.power)
return nil
end
function WT.utils.detonateUnit(args)
local unit = args.unit
if not power then
power = 1000
end
if type(args.unit) == "string" then
args.unit = Unit.getByName(unit)
end
if args.unit then
local point = p(unit.getPoint, args.unit)
if point then
trigger.action.explosion(point, args.power)
end
end
end
--blows up all units in a group on slightly randomized delays (so not all perfectly in sync)
function WT.utils.detonateGroup(groupName,power)
if not power then
power = 1000
end
local group = Group.getByName(groupName)
local units = group:getUnits()
for i = 1, #units do
timer.scheduleFunction(WT.utils.detonateUnit, {unit=units[i],power=power}, timer.getTime() + 0.1*i* math.random(1, 10))
end
end
--will cleanup a sphere described by a Vec3 point (x,y,z) and a radius
function WT.utils.cleanupSphere(point, radius)
point.y = land.getHeight({ x = point.x, y = point.z })
local volS = {
id = world.VolumeType.SPHERE,
params = {
point = point,
radius = radius
}
}
world.removeJunk(volS)
end
--will cleanup a sphere described by a circular zone
function WT.utils.cleanupZone(zone)
local sphere = trigger.misc.getZone(zone)
WT.utils.cleanupSphere(sphere.point, sphere.radius)
end
function WT.inZone(point, zone)
if zone.type == 2 then
if WT.utils.isInPolygon(point, WT.utils.polygon(zone.verticies)) then --verticies
return true
end
else
if WT.utils.VecMag({ x = zone.x - point.x, y = zone.y - point.y }) < zone.radius then
return true
end
end
return false
end
WT.weapon = {}
WT.weapon.debug = false
WT.weapon.instanceTypes = {
IN_ZONE = 1,
NEAR = 2,
IMPACT_IN_ZONE = 3,
IMPACT_NEAR = 4,
HIT = 5,
SHOT=6
}
WT.weapon.weapons = {}
WT.weapon.instances = {}
function WT.weapon.newFilter()
local weaponFilter = {
Category = {},
GuidanceType = {},
MissileCategory = {},
WarheadType = {},
Coalition = {},
Name = {},
Func = {},
Category_neg = {},
GuidanceType_neg = {},
MissileCategory_neg = {},
WarheadType_neg = {},
Coalition_neg = {},
Name_neg = {},
Func_neg = {},
terms = 0,
addTerm = function(self, field, term, match)
if WT.weapon.debug and WT.weapon.debug == true then
trigger.action.outText("attempting to add term: " .. field .. "=" .. tostring(term), 5, false)
end
-- Validate the field name
if not self[field] and not self[field .. "_neg"] then
error(string.format(
"Unknown filter field '%s'. Must be one of: Category, GuidanceType, MissileCategory, WarheadType, Name, or Func.",
tostring(field)))
end
if match == false then
table.insert(self[field .. "_neg"], term)
self.terms = self.terms + 1
if WT.weapon.debug and WT.weapon.debug == true then
trigger.action.outText(field .. "_neg added: " .. tostring(term), 5, false)
end
else
table.insert(self[field], term)
self.terms = self.terms + 1
if WT.weapon.debug and WT.weapon.debug == true then
trigger.action.outText(field .. " added: " .. tostring(term), 5, false)
end
end
end,
checkFilter = function(self, weapon, debug)
if not debug then
local debug=false
end
if not weapon then
return false
end
if weapon:isExist() == false then
return false
end
local desc = weapon:getDesc()
if not desc then
-- If getDesc() returns nil for some reason, fail or pass as you see fit
return false
end
if self.terms == 0 then
if debug == true then
if weapon then
local name = weapon:getTypeName()
local side = weapon:getCoalition()
local cat = desc.category -- e.g. Weapon.Category.MISSILE
local guidance = desc.guidance -- e.g. Weapon.GuidanceType.IR
local missileCat = desc.missileCategory -- e.g. Weapon.MissileCategory.AAM
local warheadType = desc.warheadType
trigger.action.outText("Filter Check Weapon", 5, false)
trigger.action.outText("name: " .. name, 5, false)
trigger.action.outText("coalition: " .. side, 5, false)
trigger.action.outText("category: " .. tostring(cat), 5, false)
trigger.action.outText("guidance: " .. tostring(guidance), 5, false)
trigger.action.outText("missile cat: " .. tostring(missileCat), 5, false)
trigger.action.outText("warhead: " .. tostring(warheadType), 5, false)
else
trigger.action.outText("weapon nil")
end
end
return true;
end
if weapon == nil then
if debug == true then
trigger.action.outText("weapon nil")
end
return false;
end
-- Pull out the values we'll check
local name = weapon:getTypeName()
local side = weapon:getCoalition()
local cat = desc.category -- e.g. Weapon.Category.MISSILE
local guidance = desc.guidance -- e.g. Weapon.GuidanceType.IR
local missileCat = desc.missileCategory -- e.g. Weapon.MissileCategory.AAM
local warheadType = desc.warheadType -- e.g. Weapon.WarheadType.HE
-- (Depending on DCS version, you might need to check desc.warhead or something else.)
if debug == true then
trigger.action.outText("Filter Check Weapon", 5, false)
trigger.action.outText("name: " .. name, 5, false)
trigger.action.outText("coalition: " .. side, 5, false)
trigger.action.outText("category: " .. tostring(cat), 5, false)
trigger.action.outText("guidance: " .. tostring(guidance), 5, false)
trigger.action.outText("missile cat: " .. tostring(missileCat), 5, false)
trigger.action.outText("warhead: " .. tostring(warheadType), 5, false)
end
-- 1) Check Name (positive)
if self.Name and #self.Name > 0 then
-- If we have a positive Category filter, the weapon's Name must be in that list
if not isInList(self.Name, name) then
if debug == true then
trigger.action.outText("Name filter", 5, false)
end
return false
end
end
-- 1b) Check Name (negative)
if self.Name_neg and #self.Name_neg > 0 then
-- If the weapon's Name is in our negative list, fail
if isInList(self.Name_neg, name) then
if debug == true then
trigger.action.outText("Name neg filter", 5, false)
end
return false
end
end
if self.Coalition and #self.Coalition > 0 then
-- If we have a positive Category filter, the weapon's Name must be in that list
if not isInList(self.Coalition, side) then
if debug == true then
trigger.action.outText("Coalition filter", 5, false)
end
return false
end
end
-- 1b) Check Name (negative)
if self.Coalition_neg and #self.Coalition_neg > 0 then
-- If the weapon's Name is in our negative list, fail
if isInList(self.Coalition_neg, side) then
if debug == true then
trigger.action.outText("Coalition neg filter", 5, false)
end
return false
end
end
-- 1) Check Category (positive)
if self.Category and #self.Category > 0 then
-- If we have a positive Category filter, the weapon's category must be in that list
if not isInList(self.Category, cat) then
if debug == true then
trigger.action.outText("Category filter", 5, false)
end
return false
end
end
-- 1b) Check Category (negative)
if self.Category_neg and #self.Category_neg > 0 then
-- If the weapon's category is in our negative list, fail
if isInList(self.Category_neg, cat) then
if debug == true then
trigger.action.outText("Category neg filter", 5, false)
end
return false
end
end
-- 2) Check GuidanceType (positive)
if self.GuidanceType and #self.GuidanceType > 0 then
if not isInList(self.GuidanceType, guidance) then
if debug == true then
trigger.action.outText("Guidance filter", 5, false)
end
return false
end
end
-- 2b) Negative
if self.GuidanceType_neg and #self.GuidanceType_neg > 0 then
if isInList(self.GuidanceType_neg, guidance) then
if debug == true then
trigger.action.outText("Guidance neg filter", 5, false)
end
return false
end
end
-- 3) Check MissileCategory (positive)
if self.MissileCategory and #self.MissileCategory > 0 then
if not isInList(self.MissileCategory, missileCat) then
if debug == true then
trigger.action.outText("Missile Category filter", 5, false)
end
return false
end
end
-- 3b) Negative
if self.MissileCategory_neg and #self.MissileCategory_neg > 0 then
if isInList(self.MissileCategory_neg, missileCat) then
if debug == true then
trigger.action.outText("Missile Category neg filter", 5, false)
end
return false
end
end
-- 4) Check WarheadType (positive)
if self.WarheadType and #self.WarheadType > 0 then
if not isInList(self.WarheadType, warheadType) then
if debug == true then
trigger.action.outText("Warhead filter", 5, false)
end
return false
end
end
-- 4b) Negative
if self.WarheadType_neg and #self.WarheadType_neg > 0 then
if isInList(self.WarheadType_neg, warheadType) then
if debug == true then
trigger.action.outText("Warhead neg filter", 5, false)
end
return false
end
end
if self.Func and #self.Func > 0 then
for _, func in ipairs(self.Function) do
if func(weapon,debug)==false then
if debug == true then
trigger.action.outText("Function filter", 5, false)
end
return false
end
end
end
-- 4b) Negative
if self.Func_neg and #self.Func_neg > 0 then
for _, func in ipairs(self.Function_neg) do
if func(weapon,debug)==true then
if debug == true then
trigger.action.outText("Function filter", 5, false)
end
return false
end
end
end
-- If we didn't fail any checks, it passes
return true
end,
}
return weaponFilter
end
WT.weapon.filters = {
ALL = WT.weapon.newFilter(),
MISSILES = WT.weapon.newFilter(),
BOMBS = WT.weapon.newFilter(),
ROCKETS = WT.weapon.newFilter(),
SHELLS = WT.weapon.newFilter()
}
WT.weapon.filters.MISSILES:addTerm("Category", Weapon.Category.MISSILE)
WT.weapon.filters.BOMBS:addTerm("Category", Weapon.Category.BOMB)
WT.weapon.filters.ROCKETS:addTerm("Category", Weapon.Category.ROCKET)
WT.weapon.filters.SHELLS:addTerm("Category", Weapon.Category.SHELL)
function WT.weapon.updateWeapon(weapon, time)
if p(weapon.weapon.isExist, weapon.weapon) then
weapon.last_point = weapon.weapon:getPoint()
for i = 1, #weapon.instances do
weapon.instances[i]:weaponUpdate(weapon)
end
return time + 0.05
else
for i = 1, #weapon.instances do
if weapon.instances[i].active == true then
weapon.instances[i]:weaponGone(weapon)
end
end
return nil
end
end
function WT.weapon.handleEvents(event)
if event.id == world.event.S_EVENT_SHOT then --track fired weapons
local valid_instances = {}
for x = 1, #WT.weapon.instances do
if WT.weapon.instances[x]:checkEvent(event) == true then
valid_instances[#valid_instances + 1] = WT.weapon.instances[x]
end
end
if #valid_instances > 0 then
local weapon_category = event.weapon:getDesc().category
local weapon_name = event.weapon:getTypeName()
local p1 = event.weapon:getPoint()
local id = tostring(p1.x + p1.y + p1.z)
id = id .. event.weapon:getTypeName()
id = id .. tostring(timer.getTime())
id = id .. tostring(math.random())
local weapon = { weapon = event.weapon, name = weapon_name, category = weapon_category, target = event.weapon
:getTarget(), id = id, instances = valid_instances, last_point = p1 }
timer.scheduleFunction(WT.weapon.updateWeapon, weapon, timer.getTime() + 0.05)
end
elseif event.id == world.event.S_EVENT_HIT then --check hit events
if WT.weapon.debug == true then
trigger.action.outText("Hit detected", 5, false)
end
for x = 1, #WT.weapon.instances do
WT.weapon.instances[x]:checkEvent(event)
end
end
end
-----WEAPON INSTANCE TYPES HERE
function WT.weapon.newNearInstance(filter, target, range, flag)
local tgt_type = ""
local tgt = p(Unit.getByName, target)
if tgt == nil then
tgt = p(Group.getByName, target)
if tgt == nil then
return nil
end
tgt_type = "group"
else
tgt_type = "unit"
end
local inst = {
filter = filter,
target = target,
tgtType = tgt_type,
range = range,
flag = flag,
active = true,
present = {},
updateFunc = {},
changeFunc = {},
type = WT.weapon.instanceTypes.NEAR,
deactivate = function(self)
self.active = false
trigger.action.setUserFlag(self.flag, 0)
self.present = {}
end,
activate = function(self)
self.active = true
end,
addUpdateFunc = function(self, func)
self.updateFunc[#self.updateFunc + 1] = func
end,
addChangeFunc = function(self, func)
self.changeFunc[#self.changeFunc + 1] = func
end,
triggerChange = function(self,wep)
for i = 1, #self.changeFunc do
self.changeFunc[i]({instance=self,weapon = wep})
end
end,
triggerUpdate = function(self,wep)
for i = 1, #self.updateFunc do
self.updateFunc[i]({instance=self,weapon = wep})
end
end,
checkEvent = function(self, event)
if event.id == world.event.S_EVENT_SHOT then
if self.active == true then
if self.filter:checkFilter(event.weapon, WT.weapon.debug) == true then
return true
end
end
end
return false
end,
weaponGone = function(self, wep)
for r = 1, #self.present do
if self.present[r] == wep.id then
table.remove(self.present, r)
trigger.action.setUserFlag(self.flag, #self.present)
self:triggerChange(wep)
break
end
end
end,
weaponUpdate = function(self, wep)
local ref = {}
self:triggerUpdate(wep)
if self.tgtType == "group" then
local g = p(Group.getByName, self.target)
if g then
local u = p(Group.getUnit, g, 1)
if u then
ref = u:getPoint()
else
return
end
else
return
end
else
local u = p(Unit.getByName, self.target)
if u then
ref = u:getPoint()
else
return
end
end
local pos = wep.last_point
local dist = WT.utils.VecMag{ x = pos.x - ref.x, y = pos.y - ref.y, z = pos.z - ref.z }
if dist <= self.range then
if not isInList(self.present, wep.id) then
self.present[#self.present + 1] = wep.id
trigger.action.setUserFlag(self.flag, #self.present)
self:triggerChange(wep)
end
else
for r = 1, #self.present do
if self.present[r] == wep.id then
table.remove(self.present, r)
trigger.action.setUserFlag(self.flag, #self.present)
self:triggerChange(wep)
break
end
end
end
end
}
return inst
end
function WT.weapon.newImpactNearInstance(filter, target, range, flag)
local tgt_type = ""
local tgt = p(Unit.getByName, target)
if tgt == nil then
tgt = p(Group.getByName, target)
if tgt == nil then
return nil
end
tgt_type = "group"
else
tgt_type = "unit"
end
local inst = {
filter = filter,
target = target,
tgtType = tgt_type,
range = range,
flag = flag,
active = true,
impacts = 0,
changeFunc = {},
type = WT.weapon.instanceTypes.IMPACT_NEAR,
deactivate = function(self)
self.active = false
trigger.action.setUserFlag(self.flag, 0)
self.impacts = 0
end,
activate = function(self)
self.active = true
end,
weaponUpdate = function(self, wep)
return false
end,
addChangeFunc = function(self, func)
self.changeFunc[#self.changeFunc + 1] = func
end,
triggerChange = function(self,wep)
for i = 1, #self.changeFunc do
self.changeFunc[i]({instance=self,weapon = wep})
end
end,
checkEvent = function(self, event)
if event.id == world.event.S_EVENT_SHOT then
if self.active == true then
if self.filter:checkFilter(event.weapon, WT.weapon.debug) == true then
return true
end
end
end
return false
end,
weaponGone = function(self, wep)
local ref = {}
local pos = wep.last_point
local ground = land.getHeight({ x = pos.x, y = pos.z })
if pos.y - ground > 10 then
return
end
if self.tgtType == "group" then
local g = p(Group.getByName, self.target)
if g then
local u = p(Group.getUnits, g)
if u then
for j = 1, #u do
ref[#ref + 1] = u[j]:getPoint()
end
else
return
end
else
return
end
else
local u = p(Unit.getByName, self.target)
if u then
ref[#ref + 1] = u:getPoint()
else
return
end
end
for p = 1, #ref do
local dist = WT.utils.VecMag{ x = pos.x - ref[p].x, y = pos.y - ref[p].y, z = pos.z - ref[p].z }
if dist <= self.range then
self.impacts = self.impacts + 1
trigger.action.setUserFlag(self.flag, self.impacts)
self:triggerChange(wep)
return
end
end
end
}
return inst
end
function WT.weapon.newZoneInstance(filter, zone, flag)
local inst = {
filter = filter,
zone = zone,
flag = flag,
active = true,
present = {},
updateFunc = {},
changeFunc = {},
type = WT.weapon.instanceTypes.IN_ZONE,
deactivate = function(self)
self.active = false
trigger.action.setUserFlag(self.flag, 0)
self.present = {}
end,
activate = function(self)
self.active = true
end,
addUpdateFunc = function(self, func)
self.updateFunc[#self.updateFunc + 1] = func
end,
addChangeFunc = function(self, func)
self.changeFunc[#self.changeFunc + 1] = func
end,
triggerChange = function(self,wep)
for i = 1, #self.changeFunc do
self.changeFunc[i]({instance=self,weapon = wep})
end
end,
triggerUpdate = function(self,wep)
for i = 1, #self.updateFunc do
self.updateFunc[i]({instance=self,weapon = wep})
end
end,
weaponGone = function(self, wep)
for r = 1, #self.present do
if self.present[r] == wep.id then
table.remove(self.present, r)
trigger.action.setUserFlag(self.flag, #self.present)
self:triggerChange(wep)
break
end
end
end,
checkEvent = function(self, event)
if event.id == world.event.S_EVENT_SHOT then
if self.active == true then
if self.filter:checkFilter(event.weapon, WT.weapon.debug) == true then
return true
end
end
end
return false
end,
weaponUpdate = function(self, wep)
local pos = wep.last_point
self:triggerUpdate(wep)
if WT.inZone({ x = pos.x, y = pos.z }, WT.zones[self.zone]) == true then
if not isInList(self.present, wep.id) then
self.present[#self.present + 1] = wep.id
trigger.action.setUserFlag(self.flag, #self.present)
self:triggerChange(wep)
end
else
for r = 1, #self.present do
if self.present[r] == wep.id then
table.remove(self.present, r)
trigger.action.setUserFlag(self.flag, #self.present)
self:triggerChange(wep)
break
end
end
end
end
}
return inst
end
function WT.weapon.newImpactZoneInstance(filter, zone, flag)
local inst = {
filter = filter,
zone = zone,
flag = flag,
active = true,
impacts = 0,
changeFunc = {},
type = WT.weapon.instanceTypes.IN_ZONE,
deactivate = function(self)
self.active = false
trigger.action.setUserFlag(self.flag, 0)
self.impacts = 0
end,
activate = function(self)
self.active = true
end,
addChangeFunc = function(self, func)
self.changeFunc[#self.changeFunc + 1] = func
end,
triggerChange = function(self,wep)
for i = 1, #self.changeFunc do
self.changeFunc[i]({instance=self,weapon = wep})
end
end,
weaponUpdate = function(self, wep)
return false
end,
checkEvent = function(self, event)
if event.id == world.event.S_EVENT_SHOT then
if self.active == true then
if self.filter:checkFilter(event.weapon, WT.weapon.debug) == true then
return true
end
end
end
return false
end,
weaponGone = function(self, wep)
local pos = wep.last_point
local ground = land.getHeight({ x = pos.x, y = pos.z })
if pos.y - ground > 10 then
return
end
if WT.inZone({ x = pos.x, y = pos.z }, WT.zones[self.zone]) == true then
self.impacts = self.impacts + 1
trigger.action.setUserFlag(self.flag, self.impacts)
self:triggerChange(wep)
end
end
}
return inst
end
function WT.weapon.newShotInstance(filter, flag)
local inst = {
filter = filter,
flag = flag,
active = true,
shots = 0,
updateFunc = {},
changeFunc = {},
type = WT.weapon.instanceTypes.SHOT,
deactivate = function(self)
self.active = false
trigger.action.setUserFlag(self.flag, 0)
self.shots = 0
end,
activate = function(self)
self.active = true
return
end,
addUpdateFunc = function(self, func)
self.updateFunc[#self.updateFunc + 1] = func
end,
addChangeFunc = function(self, func)
self.changeFunc[#self.changeFunc + 1] = func
end,
triggerChange = function(self,wep)
for i = 1, #self.changeFunc do
self.changeFunc[i]({instance=self,weapon = wep})
end
end,
triggerUpdate = function(self,wep)
for i = 1, #self.updateFunc do
self.updateFunc[i]({instance=self,weapon = wep})
end
end,
checkEvent = function(self, event)
if event.id == world.event.S_EVENT_SHOT then
self:triggerUpdate(event)
if self.active == true then
if self.filter:checkFilter(event.weapon, WT.weapon.debug) == true then
self.shots=self.shots+1
trigger.action.setUserFlag(self.flag, self.shots)
self:triggerChange(event)
end
end