-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreclaim_field_highlight.lua
1022 lines (828 loc) · 27.7 KB
/
reclaim_field_highlight.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
function widget:GetInfo()
return {
name = "Reclaim Field Highlight",
desc = "Highlights clusters of reclaimable material",
author = "ivand, refactored by esainane, edited for BAR by Lexon",
date = "2022",
license = "public",
layer = 1000,
enabled = true -- loaded by default?
}
end
VFS.Include("LuaRules/Configs/customcmds.h.lua")
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Options
--[[----------------------------------------------------------------------------
When to show reclaim highlight?
In addition to options bellow you can bind "reclaim_highlight" action to a key
and show reclaim when that key is pressed
------------------------------------------------------------------------------]]
--Always show reclaim highlight
local showHighlight_Always = false
--Show reclaim when f4 resource view is active
local showHighlight_Ecoview = true
--Show reclaim when any unit that can reclaim is selected
local showHighlight_AnyReclaimerSelected = true
--Show reclaim when a ressurection bot is selected
local showHighlight_ResBotSelected = true
--Show reclaim when reclaim order is selected
local showHighlight_ReclaimOrder = true
--------------------------------------------------------------------------------
--If true energy reclaim will be converted into metal (1 / 70) and added to the reclaim field value
local includeEnergy = false
--Metal value font
local numberColor = {1, 1, 1, 1}
local fontSizeMin = 30
local fontSizeMax = 250
local fontScaling = 0.6
--Field color
local reclaimColor = {0, 0, 0, 0.2}
local reclaimEdgeColor = {1, 1, 1, 0.22}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Priority Queue
local min = math.min
local max = math.max
local mathHuge = math.huge
local sqrt = math.sqrt
local insert = table.insert
local remove = table.remove
local PriorityQueue = {}
function PriorityQueue.new(cmp, initial)
local pq = setmetatable({}, {
__index = {
cmp = cmp or function(a,b) return a < b end,
push = function(self, v)
insert(self, v)
local next = #self
local prev = (next-next%2)/2
while next > 1 and cmp(self[next], self[prev]) do
self[next], self[prev] = self[prev], self[next]
next = prev
prev = (next-next%2)/2
end
end,
pop = function(self)
if #self < 2 then
return remove(self)
end
local root = 1
local r = self[root]
self[root] = remove(self)
local size = #self
if size > 1 then
local child = 2*root
while child <= size do
local aBool = cmp(self[root],self[child]);
local bBool = true;
local cBool = true;
if child+1 <= size then
bBool = cmp( self[root],self[child+1]);
cBool = cmp(self[child], self[child+1]);
end
if aBool and bBool then
break;
elseif cBool then
self[root], self[child] = self[child], self[root]
root = child
else
self[root], self[child+1] = self[child+1], self[root]
root = child+1
end
child = 2*root
end
end
return r
end,
peek = function(self)
return self[1]
end,
}
})
for _,el in ipairs(initial or {}) do
pq:push(el)
end
return pq
end
-------
-------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Optics
local function DistSq(p1, p2)
return (p1.x - p2.x)^2 + (p1.z - p2.z)^2
end
local TableEcho = Spring.Utilities.TableEcho
local Optics = {}
function Optics.new(incPoints, incNeighborMatrix, incMinPoints)
local object = setmetatable({}, {
__index = {
points = incPoints or {},
neighborMatrix = incNeighborMatrix or {},
minPoints = incMinPoints,
pointByfID = {},
unprocessed = {},
ordered = {},
-- get ready for a clustering run
_Setup = function(self)
local points, unprocessed = self.points, self.unprocessed
--Spring.Echo("epsSq", self.epsSq, "minPoints", self.minPoints)
for pIdx, point in pairs(points) do
point.rd = nil
point.cd = nil
point.processed = nil
unprocessed[point] = true
self.pointByfID[point.fID] = point
end
end,
-- distance from a point to its nth neighbor (n = minPoints - 1)
_CoreDistance = function(self, point, neighbors)
if point.cd then
return point.cd
end
if #neighbors >= self.minPoints - 1 then --(minPoints - 1) because point is also part of minPoints
local distTable = {}
for i = 1, #neighbors do
local neighbor = neighbors[i]
distTable[#distTable + 1] = DistSq(point, neighbor)
end
table.sort(distTable)
--TableEcho({point=point, neighbors=neighbors, distTable=distTable}, "_CoreDistance (#neighbors >= self.minPoints - 1)")
point.cd = distTable[self.minPoints - 1] --return (minPoints - 1) farthest distance as CoreDistance
return point.cd
end
return nil
end,
-- neighbors for a point within eps
_Neighbors = function(self, pIdx)
local neighbors = {}
for pIdx2, _ in pairs(self.neighborMatrix[pIdx]) do
neighbors[#neighbors + 1] = self.pointByfID[pIdx2]
end
--Spring.Echo("#neighbors", #neighbors)
return neighbors
end,
-- mark a point as processed
_Processed = function(self, point)
--Spring.Echo("_Processed")
point.processed = true
self.unprocessed[point] = nil
local ordered = self.ordered
ordered[#ordered + 1] = point
end,
-- update seeds if a smaller reachability distance is found
_Update = function(self, neighbors, point, seedsPQ)
for ni = 1, #neighbors do
local n = neighbors[ni]
if not n.processed then
--Spring.Echo("newRD")
local newRd = max(point.cd, DistSq(point, n))
if n.rd == nil then
n.rd = newRd
--this is a bug!!!!
seedsPQ:push({newRd, n})
elseif newRd < n.rd then
--this is a bug!!!!
n.rd = newRd
end
end
end
--return seedsPQ
end,
-- run the OPTICS algorithm
Run = function(self)
self:_Setup()
local unprocessed = self.unprocessed
--TableEcho(unprocessed, "unprocessed")
while next(unprocessed) do
--TableEcho({item=next(unprocessed)}, "next(unprocessed)")
local point = next(unprocessed)
-- mark p as processed
self:_Processed(point)
-- find p's neighbors
local neighbors = self:_Neighbors(point.fID)
--TableEcho({point=point, neighbors=neighbors}, "self:_Neighbors(point)")
--TableEcho({point=point, neighborsNum=#neighbors}, "self:_Neighbors(point)")
-- if p has a core_distance, i.e has min_cluster_size - 1 neighbors
if self:_CoreDistance(point, neighbors) then
--Spring.Echo("if self:_CoreDistance(point, neighbors) then")
local seedsPQ = PriorityQueue.new( function(a,b) return a[1] < b[1] end )
--seedsPQ = self:_Update(neighbors, point, seedsPQ)
self:_Update(neighbors, point, seedsPQ)
while seedsPQ:peek() do
-- seeds.sort(key=lambda n: n.rd)
local n = seedsPQ:pop()[2] --because we don't need priority
--TableEcho({n=n}, "seedsPQ:pop()")
-- mark n as processed
self:_Processed(n)
-- find n's neighbors
local nNeighbors = self:_Neighbors(n.fID)
--TableEcho({n=n, nNeighbors=nNeighbors}, "seedsPQ:peek()")
-- if p has a core_distance...
if self:_CoreDistance(n, nNeighbors) then
--seedsPQ = self:_Update(nNeighbors, n, seedsPQ)
self:_Update(nNeighbors, n, seedsPQ)
end
end
end
end
-- when all points have been processed
-- return the ordered list
--Spring.Echo("#ordered", #self.ordered)
--TableEcho({ordered = self.ordered}, "ordered")
return self.ordered
end,
-- ???
Clusterize = function(self, clusterThreshold)
local clusters = {}
local separators = {}
local clusterThresholdSq = clusterThreshold^2
local ordered = self.ordered
for i = 1, #ordered do
local thisP = ordered[i]
local thisRD = thisP.rd or mathHuge
-- use an upper limit to separate the clusters
if thisRD > clusterThresholdSq then
separators[#separators + 1] = i
end
end
separators[#separators + 1] = #ordered + 1
--TableEcho(separators,"separators")
for j = 1, #separators - 1 do
local sepStart = separators[j]
local sepEnd = separators[j + 1]
print(sepEnd, sepStart, sepEnd - sepStart, self.minPoints)
if sepEnd - sepStart >= self.minPoints then
--Spring.Echo("sepEnd - sepStart >= self.minPoints")
--self.ordered[start:end]
local clPoints = {}
for si = sepStart, sepEnd - 1 do
clPoints[#clPoints + 1] = ordered[si].fID
end
-- TableEcho({clPoints=clPoints}, "clPoints")
clusters[#clusters + 1] = {}
clusters[#clusters].members = clPoints
--Spring.Echo("#clPoints", #clPoints)
end
end
--TableEcho({ordered=ordered}, "clusters")
return clusters
end,
}
})
return object
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Convex Hull
local function cross(p, q, r)
return (q.z - p.z) * (r.x - q.x)
- (q.x - p.x) * (r.z - q.z)
end
--- JARVIS MARCH
--- MONOTONE CHAIN
-- https://gist.githubusercontent.com/sixFingers/ee5c1dce72206edc5a42b3246a52ce2e/raw/b2d51e5236668e5408d24b982eec9c339dc94065/Lua%2520Convex%2520Hull
-- Andrew's monotone chain convex hull algorithm
-- https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain
-- Direct port from Javascript version
local function MonotoneChain(points)
local numPoints = #points
if numPoints < 3 then return end
table.sort(points,
function(a, b)
return a.x == b.x and a.z > b.z or a.x > b.x
end
)
local lower = {}
for i = 1, numPoints do
while (#lower >= 2 and cross(lower[#lower - 1], lower[#lower], points[i]) <= 0) do
table.remove(lower, #lower)
end
table.insert(lower, points[i])
end
local upper = {}
for i = numPoints, 1, -1 do
while (#upper >= 2 and cross(upper[#upper - 1], upper[#upper], points[i]) <= 0) do
table.remove(upper, #upper)
end
table.insert(upper, points[i])
end
table.remove(upper, #upper)
table.remove(lower, #lower)
for _, point in ipairs(lower) do
table.insert(upper, point)
end
return upper
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Speedups
local glBeginEnd = gl.BeginEnd
local glBlending = gl.Blending
local glCallList = gl.CallList
local glColor = gl.Color
local glCreateList = gl.CreateList
local glDeleteList = gl.DeleteList
local glDepthTest = gl.DepthTest
local glLineWidth = gl.LineWidth
local glPolygonMode = gl.PolygonMode
local glPopMatrix = gl.PopMatrix
local glPushMatrix = gl.PushMatrix
local glRotate = gl.Rotate
local glText = gl.Text
local glTranslate = gl.Translate
local glVertex = gl.Vertex
local spGetAllFeatures = Spring.GetAllFeatures
local spGetCameraPosition = Spring.GetCameraPosition
local spGetFeatureHeight = Spring.GetFeatureHeight
local spGetFeaturePosition = Spring.GetFeaturePosition
local spGetFeatureResources = Spring.GetFeatureResources
local spGetFeatureTeam = Spring.GetFeatureTeam
local spGetGaiaTeamID = Spring.GetGaiaTeamID
local spGetGameFrame = Spring.GetGameFrame
local spGetGroundHeight = Spring.GetGroundHeight
local spIsGUIHidden = Spring.IsGUIHidden
local spTraceScreenRay = Spring.TraceScreenRay
local spValidFeatureID = Spring.ValidFeatureID
local spGetActiveCommand = Spring.GetActiveCommand
local spGetActiveCmdDesc = Spring.GetActiveCmdDesc
local spGetMapDrawMode = Spring.GetMapDrawMode
local spGetUnitDefID = Spring.GetUnitDefID
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Data
local screenx, screeny
local gaiaTeamId = spGetGaiaTeamID()
local scanInterval = 1 * Game.gameSpeed
local scanForRemovalInterval = 2 * Game.gameSpeed --2 sec
local minDistance = 300
local minSqDistance = minDistance^2
local minPoints = 2
local minFeatureMetal = 9 --flea
local drawEnabled = true
local actionActive = false
local textParametersChanged = false
local knownFeatures = {}
local reclaimerSelected = false
local resBotSelected = false
local canReclaim = {}
local canResurrect = {}
for unitDefID, unitDef in pairs(UnitDefs) do
if unitDef.canResurrect then
canResurrect[unitDefID] = true
end
if unitDef.isBuilder and not unitDef.isBuilding then
canReclaim[unitDefID] = true
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- State update
local function enableHighlight()
actionActive = true
end
local function disableHighlight()
actionActive = false
end
local function UpdateDrawEnabled()
if actionActive then return true end
local ecoview = (spGetMapDrawMode() == 'metal')
if showHighlight_Always or
(showHighlight_Ecoview and ecoview) or
(showHighlight_AnyReclaimerSelected and reclaimerSelected) or
(showHighlight_ResBotSelected and resBotSelected) then
return true
end
if showHighlight_ReclaimOrder then
local currentCmd = spGetActiveCommand()
if currentCmd then
local activeCmdDesc = spGetActiveCmdDesc(currentCmd)
return (activeCmdDesc and (activeCmdDesc.name == "Reclaim"))
end
end
return false
end
function widget:SelectionChanged(units)
local udefID
reclaimerSelected = false
resBotSelected = false
for _, v in pairs(units) do
udefID = spGetUnitDefID(v)
if canResurrect[udefID] then
resBotSelected = true
reclaimerSelected = true
return
elseif canReclaim[udefID] then
reclaimerSelected = true
return
end
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Feature Tracking
local featureNeighborsMatrix = {}
local function UpdateFeatureNeighborsMatrix(fID, added, posChanged, removed)
local fInfo = knownFeatures[fID]
if added then
featureNeighborsMatrix[fID] = {}
for fID2, fInfo2 in pairs(knownFeatures) do
if fID2 ~= fID then --don't include self into featureNeighborsMatrix[][]
local sqDist = (fInfo.x - fInfo2.x)^2 + (fInfo.z - fInfo2.z)^2
if sqDist <= minSqDistance then
featureNeighborsMatrix[fID][fID2] = true
featureNeighborsMatrix[fID2][fID] = true
end
end
end
end
if removed then
for fID2, _ in pairs(featureNeighborsMatrix[fID]) do
featureNeighborsMatrix[fID2][fID] = nil
featureNeighborsMatrix[fID][fID2] = nil
end
end
if posChanged then
UpdateFeatureNeighborsMatrix(fID, false, false, true) --remove
UpdateFeatureNeighborsMatrix(fID, true, false, false) --add again
end
end
local featureClusters = {}
local E2M = 1 / 70 --solar ratio
local featuresUpdated = false
local clusterMetalUpdated = false
local function UpdateFeatures(gf)
featuresUpdated = false
clusterMetalUpdated = false
for _, fID in ipairs(spGetAllFeatures()) do
local metal, _, energy = spGetFeatureResources(fID)
if includeEnergy then metal = metal + energy * E2M end
if (not knownFeatures[fID]) and (metal >= minFeatureMetal) then --first time seen
local f = {}
f.lastScanned = gf
local fx, _, fz = spGetFeaturePosition(fID)
local fy = spGetGroundHeight(fx, fz)
f.x = fx
f.y = fy
f.z = fz
f.isGaia = (spGetFeatureTeam(fID) == gaiaTeamId)
f.height = spGetFeatureHeight(fID)
f.drawAlt = ((fy > 0 and fy) or 0) --+ f.height
f.metal = metal
knownFeatures[fID] = f
UpdateFeatureNeighborsMatrix(fID, true, false, false)
featuresUpdated = true
end
if knownFeatures[fID] and gf - knownFeatures[fID].lastScanned >= scanInterval then
knownFeatures[fID].lastScanned = gf
local fx, _, fz = spGetFeaturePosition(fID)
local fy = spGetGroundHeight(fx, fz)
if knownFeatures[fID].x ~= fx or knownFeatures[fID].y ~= fy or knownFeatures[fID].z ~= fz then
knownFeatures[fID].x = fx
knownFeatures[fID].y = fy
knownFeatures[fID].z = fz
knownFeatures[fID].drawAlt = ((fy > 0 and fy) or 0) --+ knownFeatures[fID].height
UpdateFeatureNeighborsMatrix(fID, false, true, false)
featuresUpdated = true
end
if knownFeatures[fID].metal ~= metal then
--spEcho("knownFeatures[fID].metal ~= metal", metal)
if knownFeatures[fID].clID then
--spEcho("knownFeatures[fID].clID")
local thisCluster = featureClusters[ knownFeatures[fID].clID ]
thisCluster.metal = thisCluster.metal - knownFeatures[fID].metal
if metal >= minFeatureMetal then
thisCluster.metal = thisCluster.metal + metal
knownFeatures[fID].metal = metal
--spEcho("clusterMetalUpdated = true", thisCluster.metal)
clusterMetalUpdated = true
else
UpdateFeatureNeighborsMatrix(fID, false, false, true)
knownFeatures[fID] = nil
featuresUpdated = true
end
end
end
end
end
for fID, fInfo in pairs(knownFeatures) do
if fInfo.isGaia and spValidFeatureID(fID) == false then
--spEcho("fInfo.isGaia and spValidFeatureID(fID) == false")
UpdateFeatureNeighborsMatrix(fID, false, false, true)
fInfo = nil
knownFeatures[fID] = nil
featuresUpdated = true
end
if fInfo and gf - fInfo.lastScanned >= scanForRemovalInterval then --long time unseen features, maybe they were relcaimed or destroyed?
UpdateFeatureNeighborsMatrix(fID, false, false, true)
fInfo = nil
knownFeatures[fID] = nil
featuresUpdated = true
end
if fInfo and featuresUpdated then
knownFeatures[fID].clID = nil
end
end
end
local function ClusterizeFeatures()
local pointsTable = {}
local unclusteredPoints = {}
--spEcho("#knownFeatures", #knownFeatures)
for fID, fInfo in pairs(knownFeatures) do
pointsTable[#pointsTable + 1] = {
x = fInfo.x,
z = fInfo.z,
fID = fID,
}
unclusteredPoints[fID] = true
end
--TableEcho(featureNeighborsMatrix, "featureNeighborsMatrix")
local opticsObject = Optics.new(pointsTable, featureNeighborsMatrix, minPoints)
opticsObject:Run()
featureClusters = opticsObject:Clusterize(minDistance)
for i = 1, #featureClusters do
local thisCluster = featureClusters[i]
thisCluster.xmin = mathHuge
thisCluster.xmax = -mathHuge
thisCluster.zmin = mathHuge
thisCluster.zmax = -mathHuge
local metal = 0
for j = 1, #thisCluster.members do
local fID = thisCluster.members[j]
local fInfo = knownFeatures[fID]
thisCluster.xmin = min(thisCluster.xmin, fInfo.x)
thisCluster.xmax = max(thisCluster.xmax, fInfo.x)
thisCluster.zmin = min(thisCluster.zmin, fInfo.z)
thisCluster.zmax = max(thisCluster.zmax, fInfo.z)
metal = metal + fInfo.metal
knownFeatures[fID].clID = i
unclusteredPoints[fID] = nil
end
thisCluster.metal = metal
end
for fID, _ in pairs(unclusteredPoints) do --add Singlepoint featureClusters
local fInfo = knownFeatures[fID]
local thisCluster = {}
thisCluster.members = {fID}
thisCluster.metal = fInfo.metal
thisCluster.xmin = fInfo.x
thisCluster.xmax = fInfo.x
thisCluster.zmin = fInfo.z
thisCluster.zmax = fInfo.z
featureClusters[#featureClusters + 1] = thisCluster
knownFeatures[fID].clID = #featureClusters
end
end
local function lineCheck(points)
if points[1] == nil then return true end
local totalArea = 0
local pt1 = points[1]
for i = 2, #points - 1 do
local pt2 = points[i]
local pt3 = points[i + 1]
--Heron formula to get triangle area
local a = sqrt((pt2.x - pt1.x)^2 + (pt2.z - pt1.z)^2)
local b = sqrt((pt3.x - pt2.x)^2 + (pt3.z - pt2.z)^2)
local c = sqrt((pt3.x - pt1.x)^2 + (pt3.z - pt1.z)^2)
local p = (a + b + c)/2 --half perimeter
local triangleArea = sqrt(p * (p - a) * (p - b) * (p - c))
totalArea = totalArea + triangleArea
end
if totalArea < 3000 then return false end
return true
end
local minDim = 100
local featureConvexHulls = {}
local function ClustersToConvexHull()
featureConvexHulls = {}
--spEcho("#featureClusters", #featureClusters)
for fc = 1, #featureClusters do
local clusterPoints = {}
for fcm = 1, #featureClusters[fc].members do
local fID = featureClusters[fc].members[fcm]
clusterPoints[#clusterPoints + 1] = {
x = knownFeatures[fID].x,
y = knownFeatures[fID].drawAlt,
z = knownFeatures[fID].z
}
--Spring.MarkerAddPoint(knownFeatures[fID].x, 0, knownFeatures[fID].z, string.format("%i(%i)", fc, fcm))
end
--- TODO perform pruning as described in the article below, if convex hull algo will start to choke out
-- http://mindthenerd.blogspot.ru/2012/05/fastest-convex-hull-algorithm-ever.html
local convexHull
if #clusterPoints >= 3 and lineCheck(clusterPoints) then
--spEcho("#clusterPoints >= 3")
--convexHull = ConvexHull.JarvisMarch(clusterPoints)
convexHull = MonotoneChain(clusterPoints) --twice faster
else
--spEcho("not #clusterPoints >= 3")
local thisCluster = featureClusters[fc]
local xmin, xmax, zmin, zmax = thisCluster.xmin, thisCluster.xmax, thisCluster.zmin, thisCluster.zmax
local dx, dz = xmax - xmin, zmax - zmin
if dx < minDim then
xmin = xmin - (minDim - dx) / 2
xmax = xmax + (minDim - dx) / 2
end
if dz < minDim then
zmin = zmin - (minDim - dz) / 2
zmax = zmax + (minDim - dz) / 2
end
local height = clusterPoints[1].y
if #clusterPoints == 2 then
height = max(height, clusterPoints[2].y)
end
convexHull = {
{x = xmin, y = height, z = zmin},
{x = xmax, y = height, z = zmin},
{x = xmax, y = height, z = zmax},
{x = xmin, y = height, z = zmax},
}
end
local cx, cz, cy = 0, 0, 0
for i = 1, #convexHull do
local convexHullPoint = convexHull[i]
cx = cx + convexHullPoint.x
cz = cz + convexHullPoint.z
cy = max(cy, convexHullPoint.y)
end
local totalArea = 0
local pt1 = convexHull[1]
for i = 2, #convexHull - 1 do
local pt2 = convexHull[i]
local pt3 = convexHull[i + 1]
--Heron formula to get triangle area
local a = sqrt((pt2.x - pt1.x)^2 + (pt2.z - pt1.z)^2)
local b = sqrt((pt3.x - pt2.x)^2 + (pt3.z - pt2.z)^2)
local c = sqrt((pt3.x - pt1.x)^2 + (pt3.z - pt1.z)^2)
local p = (a + b + c)/2 --half perimeter
local triangleArea = sqrt(p * (p - a) * (p - b) * (p - c))
totalArea = totalArea + triangleArea
end
convexHull.area = totalArea
convexHull.center = {x = cx/#convexHull, z = cz/#convexHull, y = cy + 1}
featureConvexHulls[fc] = convexHull
--[[
for i = 1, #convexHull do
Sppring.MarkerAddPoint(convexHull[i].x, convexHull[i].y, convexHull[i].z, string.format("C%i(%i)", fc, i))
end
]]--
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function widget:Initialize()
screenx, screeny = widgetHandler:GetViewSizes()
widgetHandler:AddAction("reclaim_highlight", enableHighlight, nil, "p")
widgetHandler:AddAction("reclaim_highlight", disableHighlight, nil, "r")
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Drawing
local color
local cameraScale
local function DrawHullVertices(hull)
for j = 1, #hull do
glVertex(hull[j].x, hull[j].y, hull[j].z)
end
end
local drawFeatureConvexHullSolidList
local function DrawFeatureConvexHullSolid()
glPolygonMode(GL.FRONT_AND_BACK, GL.FILL)
for i = 1, #featureConvexHulls do
glBeginEnd(GL.TRIANGLE_FAN, DrawHullVertices, featureConvexHulls[i])
end
end
local drawFeatureConvexHullEdgeList
local function DrawFeatureConvexHullEdge()
glPolygonMode(GL.FRONT_AND_BACK, GL.LINE)
for i = 1, #featureConvexHulls do
glBeginEnd(GL.LINE_LOOP, DrawHullVertices, featureConvexHulls[i])
end
glPolygonMode(GL.FRONT_AND_BACK, GL.FILL)
end
local drawFeatureClusterTextList
local function DrawFeatureClusterText()
for i = 1, #featureConvexHulls do
glPushMatrix()
local center = featureConvexHulls[i].center
glTranslate(center.x, center.y, center.z)
glRotate(-90, 1, 0, 0)
local fontSize = fontSizeMin * fontScaling
local area = featureConvexHulls[i].area
fontSize = sqrt(area) * fontSize / minDim
fontSize = max(fontSize, fontSizeMin)
fontSize = min(fontSize, fontSizeMax)
local metal = featureClusters[i].metal
--spEcho(metal)
local metalText
if metal < 1000 then
metalText = string.format("%.0f", metal) --exact number
elseif metal < 10000 then
metalText = string.format("%.1fK", math.floor(metal / 100) / 10) --4.5K
else
metalText = string.format("%.0fK", math.floor(metal / 1000)) --40K
end
glColor(numberColor)
--glRect(-200, -200, 200, 200)
glText(metalText, 0, 0, fontSize, "cv")--cvo for outline
glPopMatrix()
end
end
local checkFrequency = 30
local cumDt = 0
function widget:Update(dt)
cumDt = cumDt + dt
local cx, cy, cz = spGetCameraPosition()
local desc, w = spTraceScreenRay(screenx / 2, screeny / 2, true)
if desc then
local cameraDist = min( 8000, sqrt( (cx-w[1])^2 + (cy-w[2])^2 + (cz-w[3])^2 ) )
cameraScale = sqrt((cameraDist / 600)) --number is an "optimal" view distance
else
cameraScale = 1.0
end
drawEnabled = UpdateDrawEnabled()
local frame = spGetGameFrame()
end
function widget:GameFrame(frame)
local frameMod = frame % checkFrequency
if frameMod ~= 0 then
return
end
UpdateFeatures(frame)
--spEcho("featuresUpdated", featuresUpdated)
if featuresUpdated and drawEnabled then
ClusterizeFeatures()
ClustersToConvexHull()
--spEcho("LuaUI memsize before = ", collectgarbage("count"))
--collectgarbage("collect")
--spEcho("LuaUI memsize after = ", collectgarbage("count"))
end
if featuresUpdated or drawFeatureConvexHullSolidList == nil then
--spEcho("featuresUpdated")
if drawFeatureConvexHullSolidList then
glDeleteList(drawFeatureConvexHullSolidList)
drawFeatureConvexHullSolidList = nil
end
if drawFeatureConvexHullEdgeList then
glDeleteList(drawFeatureConvexHullEdgeList)
drawFeatureConvexHullEdgeList = nil
end
drawFeatureConvexHullSolidList = glCreateList(DrawFeatureConvexHullSolid)
drawFeatureConvexHullEdgeList = glCreateList(DrawFeatureConvexHullEdge)
end
if textParametersChanged or featuresUpdated or clusterMetalUpdated or drawFeatureClusterTextList == nil then
--spEcho("clusterMetalUpdated")
if drawFeatureClusterTextList then
glDeleteList(drawFeatureClusterTextList)
drawFeatureClusterTextList = nil
end
drawFeatureClusterTextList = glCreateList(DrawFeatureClusterText)
textParametersChanged = false
end
end
function widget:ViewResize(viewSizeX, viewSizeY)
screenx, screeny = widgetHandler:GetViewSizes()
end
function widget:DrawWorld()
if spIsGUIHidden() or not drawEnabled then
return
end
glDepthTest(false)
glBlending(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA)
if drawFeatureClusterTextList then
glCallList(drawFeatureClusterTextList)
--DrawFeatureClusterText()
end
glDepthTest(true)
end
function widget:DrawWorldPreUnit()
if spIsGUIHidden() or not drawEnabled then
return
end
glDepthTest(false)
glBlending(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA)
if drawFeatureConvexHullSolidList then
glColor(reclaimColor)
glCallList(drawFeatureConvexHullSolidList)
--DrawFeatureConvexHullSolid()
end