forked from ZeroK-RTS/Zero-K
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlups.lua
1134 lines (967 loc) · 31.6 KB
/
lups.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
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
-- $Id: lups.lua 4099 2009-03-16 05:18:45Z jk $
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
--
-- file: api_gfx_lups.lua
-- brief: Lua Particle System
-- authors: jK
-- last updated: Jan. 2008
--
-- Copyright (C) 2007,2008.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
local function GetInfo()
return {
name = "Lups",
desc = "Lua Particle System",
author = "jK",
date = "2008-2014",
license = "GNU GPL, v2 or later",
layer = 1000,
api = true,
enabled = true
}
end
--// FIXME
-- 1. add los handling (inRadar,alwaysVisible, etc.)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--// Error Log Handling
PRIO_MAJOR = 0
PRIO_ERROR = 1
PRIO_LESS = 2
local errorLog = {}
local printErrorsAbove = PRIO_MAJOR
function print(priority,...)
local errorMsg = ""
for i=1,select('#',...) do
errorMsg = errorMsg .. select(i,...)
end
errorLog[#errorLog+1] = {priority=priority,message=errorMsg}
if (priority<=printErrorsAbove) then
Spring.Echo(...)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--// locals
local push = table.insert
local pop = table.remove
local StrToLower = string.lower
local pairs = pairs
local ipairs = ipairs
local next = next
local spGetUnitRadius = Spring.GetUnitRadius
local spIsUnitVisible = Spring.IsUnitVisible
local spIsSphereInView = Spring.IsSphereInView
local spGetUnitLosState = Spring.GetUnitLosState
local spGetUnitViewPosition = Spring.GetUnitViewPosition
local spGetUnitDirection = Spring.GetUnitDirection
local spGetHeadingFromVector = Spring.GetHeadingFromVector
local spGetUnitIsActive = Spring.GetUnitIsActive
local spGetUnitRulesParam = Spring.GetUnitRulesParam
local spGetGameFrame = Spring.GetGameFrame
local spGetFrameTimeOffset = Spring.GetFrameTimeOffset
local spGetUnitPieceList = Spring.GetUnitPieceList
local spGetSpectatingState = Spring.GetSpectatingState
local spGetLocalAllyTeamID = Spring.GetLocalAllyTeamID
local scGetReadAllyTeam = Script.GetReadAllyTeam
local spGetUnitPieceMap = Spring.GetUnitPieceMap
local spValidUnitID = Spring.ValidUnitID
local spGetUnitIsStunned = Spring.GetUnitIsStunned
local spGetProjectilePosition = Spring.GetProjectilePosition
local GetMovetypeUnitDefID = Spring.Utilities.GetMovetypeUnitDefID
local glUnitPieceMatrix = gl.UnitPieceMatrix
local glPushMatrix = gl.PushMatrix
local glPopMatrix = gl.PopMatrix
local glTranslate = gl.Translate
local glRotate = gl.Rotate
local glScale = gl.Scale
local glBlending = gl.Blending
local glAlphaTest = gl.AlphaTest
local glDepthTest = gl.DepthTest
local glDepthMask = gl.DepthMask
local glUnitMultMatrix = gl.UnitMultMatrix
local glUnitPieceMultMatrix = gl.UnitPieceMultMatrix
local GL_GREATER = GL.GREATER
local GL_ONE = GL.ONE
local GL_SRC_ALPHA = GL.SRC_ALPHA
local GL_ONE_MINUS_SRC_ALPHA = GL.ONE_MINUS_SRC_ALPHA
local isWidget = (widgetHandler and true) or false
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--// hardware capabilities
local GL_VENDOR = 0x1F00
local GL_RENDERER = 0x1F01
local GL_VERSION = 0x1F02
local glVendor = gl.GetString(GL_VENDOR)
local glRenderer = (gl.GetString(GL_RENDERER)):lower()
isNvidia = (glVendor:find("NVIDIA"))
isATI = (glVendor:find("ATI "))
isMS = (glVendor:find("Microsoft"))
isIntel = (glVendor:find("Intel"))
canCTT = (gl.CopyToTexture ~= nil)
canFBO = (gl.DeleteTextureFBO ~= nil)
canRTT = (gl.RenderToTexture ~= nil)
canShader = (gl.CreateShader ~= nil)
canDistortions = false --// check Initialize()
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--// widget/gadget handling
local handler = (widget and widgetHandler)or(gadgetHandler)
local GG = (widget and WG)or(GG)
local VFSMODE = (widget and VFS.RAW_FIRST)or(VFS.ZIP_ONLY)
--// locations
local LUPS_LOCATION = 'lups/'
local PCLASSES_DIRNAME = LUPS_LOCATION .. 'ParticleClasses/'
local HEADERS_DIRNAME = LUPS_LOCATION .. 'headers/'
--// helpers
VFS.Include(LUPS_LOCATION .. 'loadconfig.lua',nil,VFSMODE)
--// load some headers
VFS.Include(HEADERS_DIRNAME .. 'general.lua',nil,VFSMODE)
VFS.Include(HEADERS_DIRNAME .. 'mathenv.lua',nil,VFSMODE)
VFS.Include(HEADERS_DIRNAME .. 'figures.lua',nil,VFSMODE)
VFS.Include(HEADERS_DIRNAME .. 'vectors.lua',nil,VFSMODE)
VFS.Include(HEADERS_DIRNAME .. 'hsl.lua',nil,VFSMODE)
VFS.Include(HEADERS_DIRNAME .. 'nanoupdate.lua',nil,VFSMODE)
--// load binary insert library
VFS.Include(HEADERS_DIRNAME .. 'tablebin.lua')
local flayer_comp = function( partA,partB )
return ( partA==partB )or
( (partA.layer==partB.layer)and((partA.unit or -1)<(partB.unit or -1)) )or
( partA.layer<partB.layer )
end
--// workaround for broken UnitDraw() callin
local nilDispList
--// global function (fx classes can use it) for easier access to Lups.cfg
function GetLupsSetting(key, default)
local value = LupsConfig[key]
if (value~=nil) then
return value
else
return default
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--// some global vars (so the effects can use them)
vsx, vsy, vpx, vpy = Spring.Orig.GetViewGeometry() --// screen pos & view pos (view pos only unequal zero if dualscreen+minimapOnTheLeft)
LocalAllyTeamID = 0
thisGameFrame = 0
frameOffset = 0
LupsConfig = {}
local noDrawUnits = {}
function SetUnitLuaDraw(unitID,nodraw)
if (nodraw) then
noDrawUnits[unitID] = (noDrawUnits[unitID] or 0) + 1
if (noDrawUnits[unitID]==1) then
--if (Spring.Utilities.GetEngineVersion()=="0.76b1") then
Spring.UnitRendering.ActivateMaterial(unitID,1)
--Spring.UnitRendering.SetLODLength(unitID,1,-1000)
for pieceID in ipairs(Spring.GetUnitPieceList(unitID) or {}) do
Spring.UnitRendering.SetPieceList(unitID,1,pieceID,nilDispList)
end
--else
-- Spring.UnitRendering.SetUnitLuaDraw(unitID,true)
--end
end
else
noDrawUnits[unitID] = (noDrawUnits[unitID] or 0) - 1
if (noDrawUnits[unitID]==0) then
--if (Spring.Utilities.GetEngineVersion()=="0.76b1") then
Spring.UnitRendering.DeactivateMaterial(unitID,1)
--else
-- Spring.UnitRendering.SetUnitLuaDraw(unitID,false)
--end
noDrawUnits[unitID] = nil
end
end
end
local function DrawUnit(_,unitID,drawMode)
--[[
drawMode:
notDrawing = 0,
normalDraw = 1,
shadowDraw = 2,
reflectionDraw = 3,
refractionDraw = 4
--]]
if (drawMode==1)and(noDrawUnits[unitID]) then
return true
end
return false
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local oldVsx,oldVsy = vsx-1,vsy-1
--// load particle classes
local fxClasses = {}
local DistortionClass
local files = VFS.DirList(PCLASSES_DIRNAME, "*.lua",VFSMODE)
for _,filename in ipairs(files) do
local Class = VFS.Include(filename,nil,VFSMODE)
if (Class) then
if (Class.GetInfo) then
Class.pi = Class.GetInfo()
local sClassName = string.lower(Class.pi.name)
if (fxClasses[sClassName]) then
print(PRIO_LESS,'LUPS: duplicated particle class name "' .. sClassName .. '"')
else
fxClasses[sClassName] = Class
end
else
print(PRIO_ERROR,'LUPS: "' .. Class .. '" is missing GetInfo() ')
end
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--// saves all particles
-- An IterableMap version has already had some testing and found to have little
-- effect. Run a benchmark on that old version before reapplying it.
local particles = {}
local particlesCount = 0
local RenderSequence = {} --// mult-dim table with: [layer][partClass][unitID][fx]
local effectsInDelay = {} --// fxs which use the delay tag, and waiting for their spawn
local partIDCount = 0 --// increasing ID used to identify the particles
--[[
local function DebugPieces(unit,piecenum,level)
local piece = Spring.GetUnitPieceInfo(unit,piecenum)
Spring.Echo( string.rep(" ", level) .. "->" .. piece.name .. " (" .. piecenum .. ")")
for _,pieceChildName in ipairs(piece.children) do
local pieceNum = spGetUnitPieceMap(unit)[pieceChildName]
DebugPieces(unit,pieceNum,level+1)
end
end
--]]
--// the id param is internal don't use it!
function AddParticles(Class,Options ,__id)
if (not Options) then
print(PRIO_LESS,'LUPS->AddFX: no options given');
return -1;
end
if Options.quality and Options.quality > GetLupsSetting("quality", 3) then
return -1;
end
if (Options.delay and Options.delay~=0) then
partIDCount = partIDCount+1
newOptions = {}; CopyTable(newOptions,Options); newOptions.delay=nil
effectsInDelay[#effectsInDelay+1] = {frame=thisGameFrame+Options.delay, class=Class, options=newOptions, id=partIDCount};
return partIDCount
end
Class = StrToLower(Class)
local particleClass = fxClasses[Class]
if (not particleClass) then
print(PRIO_LESS,'LUPS->AddFX: couldn\'t find a particle class named "' .. Class .. '"');
return -1;
end
if (Options.unit)and(not spValidUnitID(Options.unit)) then
print(PRIO_LESS,'LUPS->AddFX: unit is already dead/invalid "' .. Class .. '"');
return -1;
end
--// piecename to piecenum conversion (spring >=76b1 only!)
if (Options.unit and Options.piece) then
local pieceMap = spGetUnitPieceMap(Options.unit)
Options.piecenum = pieceMap and pieceMap[Options.piece] --added check. switching spectator view can cause "attempt to index a nil value"
if (not Options.piecenum) then
local udid = Spring.GetUnitDefID(Options.unit)
if (not udid) then
print(PRIO_LESS,"LUPS->AddFX:wrong unitID")
else
print(PRIO_ERROR,"LUPS->AddFX:wrong unitpiece " .. Options.piece .. "(" .. UnitDefs[udid].name .. ")")
end
return -1;
end
end
--Spring.Echo("-------------")
--DebugPieces(Options.unit,1,0)
local newParticles,reusedFxID = particleClass.Create(Options)
if (newParticles) then
particlesCount = particlesCount + 1
if (__id) then
newParticles.id = __id
else
partIDCount = partIDCount+1
newParticles.id = partIDCount
end
particles[ newParticles.id ] = newParticles
local space = ((not newParticles.worldspace) and newParticles.unit) or (-1)
local fxTable = CreateSubTables(RenderSequence,{newParticles.layer,particleClass,space})
newParticles.fxTable = fxTable
fxTable[#fxTable+1] = newParticles
return newParticles.id;
else
if (reusedFxID) then
return reusedFxID;
else
if (newParticles~=false) then
print(PRIO_LESS,"LUPS->AddFX:FX creation failed");
end
return -1;
end
end
end
function AddParticlesArray(array)
local class = ""
for i=1,#array do
local fxSettings = array[i]
class = fxSettings.class
fxSettings.class = nil
AddParticles(class,fxSettings)
end
end
function GetParticles(particlesID)
return particles[particlesID]
end
function RemoveParticles(particlesID)
local fx = particles[particlesID]
if (fx) then
if (type(fx.fxTable)=="table") then
for j,w in pairs(fx.fxTable) do
if (w.id==particlesID) then
pop(fx.fxTable,j)
end
end
end
fx:Destroy()
particles[particlesID] = nil
particlesCount = particlesCount-1;
return
else
local status,err = pcall(function()
--//FIXME
for i=1,#effectsInDelay do
if (effectsInDelay[i].id==particlesID) then
table.remove(effectsInDelay,i)
return
end
end
--//
end)
if (not status) then
Spring.Echo("Error (Lups) - "..(#effectsInDelay).." :"..err)
for i=1,#effectsInDelay do
Spring.Echo("->",effectsInDelay[i],type(effectsInDelay[i]))
end
effectsInDelay = {}
end
end
end
function GetStats()
local count = particlesCount
local effects = {}
local layers = 0
for i=-50,50 do
if (RenderSequence[i]) then
local layer = RenderSequence[i];
if (next(layer or {})) then layers=layers+1 end
for partClass,Units in pairs(layer) do
if (not effects[partClass.pi.name]) then
effects[partClass.pi.name] = {0,0} --//[1]:=fx count [2]:=part count
end
for unitID,UnitEffects in pairs(Units) do
for _,fx in pairs(UnitEffects) do
effects[partClass.pi.name][1] = effects[partClass.pi.name][1] + 1
effects[partClass.pi.name][2] = effects[partClass.pi.name][2] + (fx.count or 0)
--count = count+1
end
end
end
end
end
return count,layers,effects
end
function HasParticleClass(ClassName)
local Class = StrToLower(ClassName)
return (fxClasses[Class] and true)or(false)
end
function GetErrorLog(minPriority)
if (minPriority) then
local log = ""
for i=1,#errorLog do
if (errorLog[i].priority<=minPriority) then
log = log .. errorLog[i].message .. "\n"
end
end
if (log~="") then
local sysinfo = "Vendor:" .. glVendor ..
"\nRenderer:" .. glRenderer ..
(((isATI)and("\nisATI: true"))or("")) ..
(((isMS)and("\nisMS: true"))or("")) ..
(((isIntel)and("\nisIntel: true"))or("")) ..
"\ncanFBO:" .. tostring(canFBO) ..
"\ncanRTT:" .. tostring(canRTT) ..
"\ncanCTT:" .. tostring(canCTT) ..
"\ncanShader:" .. tostring(canShader) .. "\n"
log = sysinfo..log
end
return log
else
if (errorlog~="") then
local sysinfo = "Vendor:" .. glVendor ..
"\nRenderer:" .. glRenderer ..
"\nisATI:" .. tostring(isATI) ..
"\nisMS:" .. tostring(isMS) ..
"\nisIntel:" .. tostring(isIntel) ..
"\ncanFBO:" .. tostring(canFBO) ..
"\ncanRTT:" .. tostring(canRTT) ..
"\ncanCTT:" .. tostring(canCTT) ..
"\ncanShader:" .. tostring(canShader) .. "\n"
return sysinfo..errorLog
else
return errorLog
end
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local anyFXVisible = false
local anyDistortionsVisible = false
local function IsUnitPositionKnown(unitID)
if LocalAllyTeamID < 0 then
return true
end
local targetVisiblityState = Spring.GetUnitLosState(unitID, LocalAllyTeamID, true)
if not targetVisiblityState then
return false
end
local inLos = (targetVisiblityState == 15)
if inLos then
return true
end
local identified = (targetVisiblityState > 2)
if not identified then
return false
end
local unitDefID = Spring.GetUnitDefID(unitID)
if not (unitDefID) then
return false
end
return not GetMovetypeUnitDefID(unitDefID)
end
local function RadarDotCheck(unitID)
return true
end
local function Draw(extension,layer,water)
local FxLayer = RenderSequence[layer];
if (not FxLayer) then return end
local BeginDrawPass = "BeginDraw"..extension
local DrawPass = "Draw"..extension
local EndDrawPass = "EndDraw"..extension
for partClass,Units in pairs(FxLayer) do
local beginDraw = partClass[BeginDrawPass]
if (beginDraw) then
beginDraw()
local drawfunc = partClass[DrawPass]
if (not next(Units)) then
FxLayer[partClass]=nil
else
for unitID,UnitEffects in pairs(Units) do
if (not UnitEffects[1]) then
Units[unitID]=nil
else
if (unitID>-1) then
------------------------------------------------------------------------------------
-- render in unit/piece space ------------------------------------------------------
------------------------------------------------------------------------------------
glPushMatrix()
if gadget and not IsUnitPositionKnown(unitID) then
local x, y, z = Spring.GetUnitPosition(unitID)
local a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, a41, a42, a43, a44 = Spring.GetUnitTransformMatrix(unitID)
if a11 then
gl.MultMatrix(a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, x, y, z , a44)
else
glUnitMultMatrix(unitID)
end
else
glUnitMultMatrix(unitID)
end
--// render effects
for i=1,#UnitEffects do
local fx = UnitEffects[i]
if (fx.alwaysVisible or fx.visible) and (not water or not fx.nowater) then
if (fx.piecenum) then
--// enter piece space
glPushMatrix()
glUnitPieceMultMatrix(unitID,fx.piecenum)
glScale(1,1,-1)
drawfunc(fx)
glPopMatrix()
--// leave piece space
else
fx[DrawPass](fx)
end
end
end
--// leave unit space
glPopMatrix()
else
------------------------------------------------------------------------------------
-- render in world space -----------------------------------------------------------
------------------------------------------------------------------------------------
for i=1,#UnitEffects do
local fx = UnitEffects[i]
if (fx.alwaysVisible or fx.visible) and (not water or not fx.nowater) then
glPushMatrix()
if fx.projectile and not fx.worldspace then
local x,y,z = spGetProjectilePosition(fx.projectile)
glTranslate(x,y,z)
end
drawfunc(fx)
glPopMatrix()
end
end -- for
end -- if
end --if
end --for
end
partClass[EndDrawPass]()
end
end
end
local function DrawDistortionLayers()
glBlending(GL_ONE,GL_ONE)
for i=-50,50 do
Draw("Distortion",i)
end
glBlending(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
end
local function DrawParticlesOpaque()
if ( not anyFXVisible ) then return end
vsx, vsy, vpx, vpy = Spring.Orig.GetViewGeometry()
if (vsx~=oldVsx)or(vsy~=oldVsy) then
for _,partClass in pairs(fxClasses) do
if partClass.ViewResize then partClass.ViewResize(vsx, vsy) end
end
oldVsx, oldVsy = vsx, vsy
end
glDepthTest(true)
glDepthMask(true)
for i=-50,50 do
Draw("Opaque",i)
end
glDepthMask(false)
glDepthTest(false)
end
local function DrawParticles()
if ( not anyFXVisible ) then return end
glDepthTest(true)
--// Draw() (layers: -50 upto 0)
glAlphaTest(GL_GREATER, 0)
for i=-50,0 do
Draw("",i)
end
glAlphaTest(false)
--// DrawDistortion()
if (anyDistortionsVisible)and(DistortionClass) then
DistortionClass.BeginDraw()
gl.ActiveFBO(DistortionClass.fbo,DrawDistortionLayers)
DistortionClass.EndDraw()
end
--// Draw() (layers: 1 upto 50)
glAlphaTest(GL_GREATER, 0)
for i=1,50 do
Draw("",i)
end
glAlphaTest(false)
glDepthTest(false)
end
local function DrawParticlesWater()
if ( not anyFXVisible ) then return end
glDepthTest(true)
--// DrawOpaque()
glDepthMask(true)
for i=-50,50 do
Draw("Opaque",i)
end
glDepthMask(false)
--// Draw() (layers: -50 upto 50)
glAlphaTest(GL_GREATER, 0)
for i=-50,50 do
Draw("",i,true)
end
glAlphaTest(false)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Unit activity
local activeUnit = {}
local activeUnitCheckTime = {}
local ACTIVE_CHECK_PERIOD = 10
local function GetUnitIsActive(unitID)
if activeUnitCheckTime[unitID] and activeUnitCheckTime[unitID] > thisGameFrame then
return activeUnit[unitID]
end
activeUnitCheckTime[unitID] = thisGameFrame + ACTIVE_CHECK_PERIOD
activeUnit[unitID] = (spGetUnitIsActive(unitID) or spGetUnitRulesParam(unitID, "unitActiveOverride") == 1)
and (spGetUnitRulesParam(unitID, "disarmed") ~= 1)
and (spGetUnitRulesParam(unitID, "att_shieldDisabled") ~= 1)
and (spGetUnitRulesParam(unitID, "morphDisable") ~= 1)
and not spGetUnitIsStunned(unitID)
return activeUnit[unitID]
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local DrawWorldPreUnitVisibleFx
local DrawWorldVisibleFx
local DrawWorldReflectionVisibleFx
local DrawWorldRefractionVisibleFx
local DrawWorldShadowVisibleFx
local DrawScreenEffectsVisibleFx
local DrawInMiniMapVisibleFx
local function UpdateAllyTeamStatus()
local spec, specFullView = spGetSpectatingState()
if (specFullView) then
LocalAllyTeamID = scGetReadAllyTeam() or 0
else
LocalAllyTeamID = spGetLocalAllyTeamID() or 0
end
end
function IsPosInLos(x,y,z)
if LocalAllyTeamID == 0 then
UpdateAllyTeamStatus()
end
return LocalAllyTeamID == Script.ALL_ACCESS_TEAM or (LocalAllyTeamID ~= Script.NO_ACCESS_TEAM and Spring.IsPosInLos(x,y,z, LocalAllyTeamID))
end
function IsPosInRadar(x,y,z)
if LocalAllyTeamID == 0 then
UpdateAllyTeamStatus()
end
return LocalAllyTeamID == Script.ALL_ACCESS_TEAM or (LocalAllyTeamID ~= Script.NO_ACCESS_TEAM and Spring.IsPosInRadar(x,y,z, LocalAllyTeamID))
end
function IsPosInAirLos(x,y,z)
if LocalAllyTeamID == 0 then
UpdateAllyTeamStatus()
end
return LocalAllyTeamID == Script.ALL_ACCESS_TEAM or (LocalAllyTeamID ~= Script.NO_ACCESS_TEAM and Spring.IsPosInAirLos(x,y,z, LocalAllyTeamID))
end
function GetUnitLosState(unitID)
if LocalAllyTeamID == 0 then
UpdateAllyTeamStatus()
end
return LocalAllyTeamID == Script.ALL_ACCESS_TEAM or (LocalAllyTeamID ~= Script.NO_ACCESS_TEAM and (Spring.GetUnitLosState(unitID, LocalAllyTeamID) or {}).los) or false
end
local function IsUnitFXVisible(fx)
local unitActive = true
local unitID = fx.unit
if fx.onActive then
unitActive = GetUnitIsActive(unitID)
elseif fx.onUnitRulesParam then
unitActive = (spGetUnitRulesParam(unitID, fx.onUnitRulesParam) == 1)
end
--Spring.Utilities.UnitEcho(unitID, "w")
if (unitActive) then
if (isWidget and not fx.noIconDraw) or fx.alwaysVisible then
return true
elseif (fx.Visible) then
return fx:Visible()
else
local unitRadius = (spGetUnitRadius(unitID) or 0) + 40
local r = fx.radius or 0
return Spring.IsUnitVisible(unitID, unitRadius + r, fx.noIconDraw)
end
else
return fx.alwaysVisible
end
end
local function IsProjectileFXVisible(fx)
if fx.alwaysVisible then
return true
elseif fx.Visible then
return fx:Visible()
else
local proID = fx.projectile
local x,y,z = Spring.GetProjectilePosition(proID)
if (IsPosInLos(x,y,z)and (spIsSphereInView(x,y,z,(fx.radius or 200)+100)) ) then
return true
end
end
end
local function IsWorldFXVisible(fx)
if fx.alwaysVisible then
return true
elseif (fx.Visible) then
return fx:Visible()
elseif (fx.pos) then
local pos = fx.pos
if (IsPosInLos(pos[1],pos[2],pos[3]))and
(spIsSphereInView(pos[1],pos[2],pos[3],(fx.radius or 200)+100))
then
return true
end
end
end
local function CreateVisibleFxList()
local removeFX = {}
local removeCnt = 1
for _,fx in pairs(particles) do
if ((fx.unit or -1) > -1) then
fx.visible = IsUnitFXVisible(fx)
if (fx.visible) then
if (not anyFXVisible) then anyFXVisible = true end
if (not anyDistortionsVisible) then anyDistortionsVisible = fx.pi.distortion end
end
elseif ((fx.projectile or -1) > -1) then
fx.visible = IsProjectileFXVisible(fx)
if (fx.visible) then
if (not anyFXVisible) then anyFXVisible = true end
if (not anyDistortionsVisible) then anyDistortionsVisible = fx.pi.distortion end
end
else
fx.visible = IsWorldFXVisible(fx)
if (fx.visible) then
if (not anyFXVisible) then anyFXVisible = true end
if (not anyDistortionsVisible) then anyDistortionsVisible = fx.pi.distortion end
elseif (fx.Valid and (not fx:Valid())) then
removeFX[removeCnt] = fx.id
removeCnt = removeCnt + 1
end
end
end
--Spring.Echo("Lups fx cnt", particles.GetIndexMax())
for i=1,removeCnt-1 do
RemoveParticles(removeFX[i])
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function CleanInvalidUnitFX()
local removeFX = {}
local removeCnt = 1
for layerID,layer in pairs(RenderSequence) do
for partClass,Units in pairs(layer) do
for unitID,UnitEffects in pairs(Units) do
if (not UnitEffects[1]) then
Units[unitID] = nil
else
if (unitID>-1) then
if (not spValidUnitID(unitID)) then --// UnitID isn't valid anymore, remove all its effects
for i=1,#UnitEffects do
local fx = UnitEffects[i]
removeFX[removeCnt] = fx.id
removeCnt = removeCnt + 1
end
Units[unitID]=nil
end
end
end
end
end
end
for i=1,removeCnt-1 do
RemoveParticles(removeFX[i])
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--// needed to allow to use RemoveParticles in :Update of the particleclasses
local fxRemoveList = {}
function BufferRemoveParticles(id)
fxRemoveList[#fxRemoveList+1] = id
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local lastGameFrame = 0
local function GameFrame(_,n)
thisGameFrame = n
if ((not next(particles)) and (not effectsInDelay[1])) then return end
--// create delayed FXs
if (effectsInDelay[1]) then
local remaingFXs,cnt={},1
for i=1,#effectsInDelay do
local fx = effectsInDelay[i]
if (fx.frame>thisGameFrame) then
remaingFXs[cnt]=fx
cnt=cnt+1
else
AddParticles(fx.class,fx.options, fx.id)
if (fx.frame-thisGameFrame>0) then
particles[fx.id]:Update(fx.frame-thisGameFrame)
end
end
end
effectsInDelay = remaingFXs
end
--// cleanup FX from dead/invalid units
CleanInvalidUnitFX()
--// update FXs
framesToUpdate = thisGameFrame - lastGameFrame
for _,partFx in pairs(particles) do
if (n>=partFx.dieGameFrame) then
--// lifetime ended
if (partFx.repeatEffect) then
if (type(partFx.repeatEffect)=="number") then
partFx.repeatEffect = partFx.repeatEffect - 1
if (partFx.repeatEffect==1) then partFx.repeatEffect = nil end
end
if (partFx.ReInitialize) then
partFx:ReInitialize()
else
partFx.dieGameFrame = partFx.dieGameFrame + partFx.life
end
else
--// we can't remove items from a table we are iterating atm, so just buffer them and remove them later
BufferRemoveParticles(partFx.id)
end
else
--// update particles
if (partFx.Update) then
partFx:Update(framesToUpdate)
end
end
end
--// now we can remove particles
if (#fxRemoveList>0) then
for i=1,#fxRemoveList do
RemoveParticles(fxRemoveList[i])
end
fxRemoveList = {}
end
end
local function Update(_,dt)
--// update frameoffset and self allyteam
frameOffset = spGetFrameTimeOffset()
UpdateAllyTeamStatus()
--// Game Frame Update
local x = spGetGameFrame()
if ((x-lastGameFrame)>=1) then
GameFrame(nil,x)
lastGameFrame = x
end
--// check which fxs are visible
anyFXVisible = false
anyDistortionsVisible = false
if (next(particles)) then
CreateVisibleFxList()
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function CheckParticleClassReq(pi)
return
(canShader or (not pi.shader))and
(canFBO or (not pi.fbo))and
(canRTT or (not pi.rtt))and
(canCTT or (not pi.ctt))and
(canDistortions or (not pi.distortion))and
((not isIntel) or (pi.intel~=0))and
((not isMS) or (pi.ms~=0))
end
local function Initialize()
LupsConfig = LoadConfig("./lups.cfg")