-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLockdown.lua
More file actions
1149 lines (1025 loc) · 31.4 KB
/
Lockdown.lua
File metadata and controls
1149 lines (1025 loc) · 31.4 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
-- Lockdown contains a significant amount of code providing a expanded targeting system around a user-positioned reticle, since we cannot reposition the mouseover location.
-- It must account for a few complications with the API to do so:
-- Units are not well classified as units the player should care about or should be able to target, by default
-- All units may be targeted with GameLib.SetTargetUnit(unit), but invalid targets are immediately un-targeted. This is the process in that case:
-- GameLib.SetTargetUnit(unit) called (It returns true or false based on if the player can target anything at the current time, not if the player can target [unit])
-- "UnitCreated" and "TargetUnitChanged" are called during this call, and finish before it returns
-- "TargetUnitChanged" is then called after it returns, inside the current frame.
local tUnitWhitelist = {
["Charge Target"] = true -- Challenge swing things
}
local tUnitBlacklist = {
}
----------------------------------------------------------
-- Localization
local tLocalization = {
en_us = {
button_configure = "Lockdown",
button_label_bind = "Click to change",
button_label_bind_wait = "Press new key",
button_label_mod = "No modifier",
Title_tweaks = "Tweaks",
Text_mouselockrebind = "Lockdown has been slimmed down quite a bit since Reloaded. Some options are missing or incomplete.",
togglelock = "Toggle Lockdown",
locktarget = "Lock/Unlock current target",
manualtarget = "Manual target",
auto_target = "Reticle targeting",
reticle_show = "Show reticle",
-- auto_target_delay = "Reticle target delay",
reticle_range_limit = "Maximum targeting range",
reticle_clear_distant_target = "Clear targets that move too far from the reticle",
reticle_clear_distant_range = "Clear target distance",
-- update_frame_rate = "Update every 4*n frames",
reticle_opacity = "Reticle opacity",
reticle_size = "Reticle size",
reticle_offset_y = "Vertical offset",
reticle_offset_x = "Horizontal offset",
reticle_hue_red = "Reticle hue (Red)",
reticle_hue_green = "Reticle hue (Green)",
reticle_hue_blue = "Reticle hue (Blue)",
message_target_locked = "Target locked: %s",
message_target_unlocked = "Target unlocked",
}
}
local L = setmetatable({}, {__index = tLocalization.en_us})
-- System key map
-- I don't even know who this is from
-- Three different mods have three different versions
local SystemKeyMap
----------------------------------------------------------
-- Local references
local pairs, ipairs, table, string, math = pairs, ipairs, table, string, math
local GameLib, Apollo = GameLib, Apollo
----------------------------------------------------------
-- Settings
-- Defaults
Lockdown = {
defaults = {
locktarget_key = 20, -- Caps Lock
locktarget_mod = false,
reticle_show = true,
reticle_range_limit = 50,
reticle_clear_distant_target = true,
reticle_clear_distant_range = 150,
auto_target = true,
auto_target_neutral = false,
auto_target_hostile = true,
auto_target_friendly = false,
auto_target_settler = false,
auto_target_harvest = true,
reticle_opacity = 0.1,
reticle_size = 128,
reticle_sprite = "giznat",
reticle_offset_x = 0,
reticle_offset_y = -70,
reticle_hue_red = 1,
reticle_hue_blue = 1,
reticle_hue_green = 1,
-- Deprecated
manualtarget_key = 72, -- H
manualtarget_mod = "control",
free_with_shift = false,
free_with_ctrl = false,
free_with_alt = true,
auto_target_delay = 0,
auto_target_interval = 100,
update_frame_rate = 4,
},
settings = {},
reticles = {}
}
local Lockdown = Lockdown
-- Since OnRestore can trigger late (or not trigger), preset these
Lockdown.tTargetDispositions = {
[Unit.CodeEnumDisposition.Friendly] = Lockdown.defaults.auto_target_friendly,
[Unit.CodeEnumDisposition.Neutral] = Lockdown.defaults.auto_target_neutral,
[Unit.CodeEnumDisposition.Hostile] = Lockdown.defaults.auto_target_hostile
}
local opt = Lockdown.settings
setmetatable(Lockdown.settings, {__index = Lockdown.defaults})
----------------------------------------------------------
-- Wait until a condition is met (Or simply wait for a delay) to call a given function
local Alfred = { timers = {}, i = 0 }
function Alfred:Wait(fCondition, fAction, nfDelay)
local i = self.i
local timer = ApolloTimer.Create(nfDelay or 0.1, true, "Timer_"..i, self)
self.timers[i] = timer
-- Apparently ApolloTimer doesn't like numerical function keys
self["Timer_"..i] = function()
if not fCondition or fCondition() then
timer:Stop()
fAction()
-- Release
self.timers[i] = nil
self["Timer_"..i] = nil
end
end
self.i = i + 1
return i
end
----------------------------------------------------------
-- I want my print(), and I want my print() whenitwillactuallywork!
local print_buffer = {}
local function print(...)
table.insert(print_buffer, {...})
end
Alfred:Wait(nil, function()
function print(...)
local out = {}
for i=1,select('#', ...) do
local v = select(i, ...)
table.insert(out, v == nil and "[nil]" or tostring(v))
end
Print(table.concat(out, " "))
end
-- Process and clear buffer
for i,v in ipairs(print_buffer) do
print(unpack(v))
end
print_buffer = nil
end, 3)
local function system_print(...)
if ChatSystemLib then
ChatSystemLib.PostOnChannel(2, table.concat({...}, ", "))
else
print(...)
end
end
----------------------------------------------------------
-- Helpers
-- Wipe a table for reuse
local function wipe(t)
for k,v in pairs(t) do
t[k] = nil
end
end
-- Load all form elements into a table by name
local function children_by_name(wnd, t)
local t = t or {}
for _,child in ipairs(wnd:GetChildren()) do
t[child:GetName()] = child
children_by_name(child, t)
end
return t
end
----------------------------------------------------------
-- Module stuff
function Lockdown:RegisterEventHandler(event, handler)
handler = handler or "EventHandler_"..event
assert(self[handler], "Requested event handler does not exist")
Apollo.RegisterEventHandler(event, handler, self)
end
function Lockdown:Init()
Apollo.RegisterAddon(self, true, L.button_configure)
-- We have to register for this right away, since we get units before
-- the addon finishes loading, much less the player unit existing.
self:RegisterEventHandler("UnitCreated", "Handler_ProcessUnit")
end
----------------------------------------------------------
-- Saved data
function Lockdown:OnSave(eLevel)
if eLevel == GameLib.CodeEnumAddonSaveLevel.Account then
local s = self.settings
-- Don't save defaults
for k,v in pairs(s) do
if v == self.defaults[k] then
s[k] = nil
end
end
return s
end
end
function Lockdown:OnRestore(eLevel, tData)
if eLevel == GameLib.CodeEnumAddonSaveLevel.Account and tData then
local s = self.settings
-- Restore settings
for k,v in pairs(self.defaults) do
if tData[k] ~= nil then
s[k] = tData[k]
end
end
-- Build settings dependent data
self.tTargetDispositions = {
[Unit.CodeEnumDisposition.Friendly] = s.auto_target_friendly,
[Unit.CodeEnumDisposition.Neutral] = s.auto_target_neutral,
[Unit.CodeEnumDisposition.Hostile] = s.auto_target_hostile
}
-- Update settings dependant events
self:KeyOrModifierUpdated()
end
end
local preload_units, g_isScientist, g_isSettler = {}
local LOCK_STATE = false
local PLAYER = nil
local FRAME = 0
local PAUSED = false
function Lockdown:OnLoad()
----------------------------------------------------------
-- Load reticle
self.wndReticle = Apollo.LoadForm("Lockdown.xml", "Lockdown_ReticleForm", "InWorldHudStratum", nil, self)
self.wndReticleSpriteTarget = self.wndReticle:FindChild("Lockdown_ReticleSpriteTarget")
-- Add reticles
self:AddReticle("tiny", [[Lockdown\reticles\tiny.png]], 128)
self:AddReticle("giznat", [[Lockdown\reticles\giznat.png]], 256)
self:Reticle_Update()
self:RegisterEventHandler("ResolutionChanged", "Reticle_Update")
----------------------------------------------------------------------
-- TICK TOCK TICK TOCK
self:RegisterEventHandler("FrameCount")
self:RegisterEventHandler("NextFrame")
----------------------------------------------------------
-- Options
Apollo.RegisterSlashCommand("lockdown", "OnConfigure", self)
Apollo.RegisterSlashCommand("ldt", "LockdownTest", self)
----------------------------------------------------------
-- Targeting
self.xmlMarker = XmlDoc.CreateFromFile("Lockdown_Marker.xml")
self:RegisterEventHandler("UnitActivationTypeChanged", "Handler_ProcessUnit")
self:RegisterEventHandler("UnitMiniMapMarkerChanged", "Handler_ProcessUnit")
self:RegisterEventHandler("UnitGibbed", "Handler_ProcessUnit")
self:RegisterEventHandler("UnitDestroyed")
self:RegisterEventHandler("ChangeWorld")
self:RegisterEventHandler("TargetUnitChanged")
self.timerEscResume = ApolloTimer.Create(1, false, "TimerHandler_EscResume", self)
self.timerEscResume:Stop()
----------------------------------------------------------
-- Keybinds
self:RegisterEventHandler("SystemKeyDown")
-- self.timerFreeKeys = ApolloTimer.Create(0.1, true, "TimerHandler_FreeKeys", self)
self:KeyOrModifierUpdated()
end
----------------------------------------------------------
-- Units and Advanced Targeting
-- true if desired, false if not, nil if needs removed
-- TODO: Better filtering of units we really don't care about to cut down on OnScreen calls
local function IsDesiredUnit(unit)
if not unit:IsValid() or unit:IsThePlayer() then return nil end
local unitType = unit:GetType()
local player = GameLib.GetPlayerUnit()
-- PC Units are not removed on death and respawn without a unit creation event
if unitType == "Player" then
if unit:GetDispositionTo(player) == Unit.CodeEnumDisposition.Friendly then
-- TODO: Friendly player filtering
elseif player:IsPvpFlagged() and unit:IsPvpFlagged() then
return true
end
return false
end
-- Lists
local name = unit:GetName()
if tUnitBlacklist[name] then
return nil
elseif tUnitWhitelist[name] then
return true
-- Harvest nodes
elseif unitType == "Harvest"
and unit:GetHarvestRequiredTradeskillName() ~= "Farmer"
and unit:CanBeHarvestedBy(player) then
return true
-- Visible plates
elseif unit:ShouldShowNamePlate() and unit:GetDispositionTo(player) ~= Unit.CodeEnumDisposition.Friendly then
if unit:IsDead() then
return false
end
return true
end
-- Activation states
local tAct = unit:GetActivationState()
local bActState = tAct and next(tAct) or false
if bActState then
-- Hide already activated quest objects
if tAct.QuestTarget and not (tAct.Interact and tAct.Interact.bCanInteract) then
if unitType == "Simple" then return else return false end
-- return false
-- Hide settler collection or improvements
-- elseif g_isSettler and not opt.auto_target_settler and (tAct.SettlerMinfrastructure or (tAct.Collect and tAct.Collect.bUsePlayerPath)) then
-- onscreen[unit:GetId()] = nil
-- return
-- Generic activateable objects
elseif tAct.Interact and (tAct.Interact.bCanInteract or tAct.Interact.bIsHighlightable or tAct.Interact.bShowCallout) then
return true
-- Quest turn-ins
elseif tAct.QuestReward and tAct.QuestReward.bIsActive and tAct.QuestReward.bCanInteract then
return true
-- Quest starting objects
elseif tAct.QuestNew and tAct.QuestNew.bCanInteract then
return true
-- Scientist scans
elseif g_isScientist then
-- Datacubes
if (tAct.Datacube and tAct.Datacube.bIsActive)
-- Raw scannable items (information constructs)
or (tAct.ScientistRawScannable and tAct.ScientistRawScannable.bIsActive) then
return true
end
end
end
-- Units we want based on quest or path status
local tRewards = unit:GetRewardInfo()
if tRewards then
for i=1,#tRewards do
local t = tRewards[i] --t.eType == eRewardQuest and
-- Quest items we need and haven't interacted with
if (t.peoObjective and t.peoObjective:GetStatus() == 1)
or (t.nCompleted
and t.nCompleted < t.nNeeded
and (not bActState
or not tAct.Interact
or tAct.Interact.bCanInteract))
-- or scientist scans
or (g_isScientist and t.eType == Unit.CodeEnumRewardInfoType.Scientist) then
return true
end
end
end
-- Discard simple units until their activation state changes
if unitType == "Simple" then return else return false end
end
local markers = {} -- Frames anchored to units for onscreen event
local onscreen = {} -- Currently onscreen units
local inrange = {} -- Currently in range units
local buffered = {} -- Units that were created while player was invalid
local suspended = {} -- Units that are not currently, but may be targetable
Lockdown.tTrackingData = {
markers = markers,
onscreen = onscreen,
inrange = inrange,
buffered = buffered,
suspended = suspended
}
local function DestroyMarker(uid)
if markers[uid] then
markers[uid]:Destroy()
markers[uid] = nil
onscreen[uid] = nil
inrange[uid] = nil
suspended[uid] = nil
end
end
function Lockdown:Handler_ProcessUnit(unit, bOnScreen)
if not unit then return end
local uid = unit:GetId()
if not uid or not unit:IsValid() or unit:IsThePlayer() then return end
-- Player is invalid/invalidated at certain times, buffer those units
-- since we need to get their standing to the player
if not PLAYER then
table.insert(buffered, unit)
return
end
-- Evaluate
local desired = IsDesiredUnit(unit)
if desired ~= nil then
local marker = markers[uid]
-- New marker
if not markers[uid] then
markers[uid] = Apollo.LoadForm(self.xmlMarker, "Lockdown_Marker", "InWorldHudStratum", self)
markers[uid]:SetData(unit)
markers[uid]:SetUnit(unit)
end
-- Start tracking
if bOnScreen == nil then
bOnScreen = GameLib.GetUnitScreenPosition(unit).bOnScreen
end
if desired and bOnScreen then
if not onscreen[uid] then
onscreen[uid] = unit
end
-- Stop tracking
elseif onscreen[uid] then
onscreen[uid] = nil
inrange[uid] = nil
end
-- Destroy marker
else
DestroyMarker(uid)
end
end
local uCurrentTarget, uLastAutoTarget
local plsNoOverflow = false
function Lockdown:EventHandler_UnitDestroyed(unit)
if plsNoOverflow then return nil end
plsNoOverflow = true
local uid = unit:GetId()
if markers[uid] then
-- Clear target if targeted by Lockdown
if unit == uLastAutoTarget and GameLib.IsMouseLockOn() and unit == GameLib.GetTargetUnit() then
uCurrentTarget = nil
GameLib.SetTargetUnit()
end
end
DestroyMarker(uid)
plsNoOverflow = false
end
-- Clear all units on world change
function Lockdown:EventHandler_ChangeWorld()
PLAYER = nil
for uid in pairs(markers) do
DestroyMarker(uid)
end
end
function Lockdown:EventHandler_WorldLocationOnScreen(wnd, ctrl, visible)
self:Handler_ProcessUnit(ctrl:GetUnit(), visible)
end
local function count(t)
local n = 0
for k,v in pairs(t) do
n = n + 1
end
return n
end
function Lockdown:LockdownTest()
local target = GameLib.GetTargetUnit()
local tid = target and target:GetId() or nil
local t = {
num_onscreen = count(onscreen), -- Number of onscreen markers
num_markers = count(markers), -- Total number of markers
list_onscreen = {}, -- Onscreen markers
list_offscreen = {}, -- Offscreen markers
}
for uid,unit in pairs(onscreen) do
table.insert(t.list_onscreen, unit:GetName())
end
for uid,marker in pairs(markers) do
if not onscreen[uid] then
table.insert(t.list_offscreen, marker:GetUnit():GetName())
end
end
if tid then
t.osdata = osdata[tid]
t.target = target
t.screen = GameLib.GetUnitScreenPosition(target)
t.marker = markers[tid]
t.onscreen = onscreen[tid]
t.is_marker_unit = t.marker:GetUnit() == target
end
SendVarToRover("Lockdown Debug Table", t)
end
--[[local fn = Lockdown.EventHandler_WorldLocationOnScreen
function Lockdown:EventHandler_WorldLocationOnScreen(wnd, ctrl, visible, unit)
if not unit then unit = ctrl:GetData() end
local id = unit:GetId()
local initial = onscreen[id]
fn(self, wnd, ctrl, visible, unit)
if initial ~= onscreen[id] then
print("WorldLocationOnScreen", onscreen[id] and "||||" or "----", unit:GetName())
end
end]]
local pReticle, nReticleRadius
-- Returns bIntersects, nDistIntersection, nDistPoints
local function UnitInReticle(unit)
local tPos = GameLib.GetUnitScreenPosition(unit)
if not tPos or not tPos.bOnScreen then
return false, 0, 0
end
-- Try to place unit point in middle of units
local nUnitRadius, pOverhead = 0
if unit:GetType() ~= "Simple" then
pOverhead = unit:GetOverheadAnchor()
-- Sanity check radius
if pOverhead then
nUnitRadius = math.max(15, (tPos.nY - pOverhead.y) / 2)
end
end
local nDistPoints = pReticle.Length(Vector2.New(tPos.nX, tPos.nY - nUnitRadius) - pReticle)
return nDistPoints < (nReticleRadius + nUnitRadius), nDistPoints - nReticleRadius - nUnitRadius, nDistPoints
end
-- Find in range unit closest to center of reticle
local uDelayedTarget, uLockedTarget
local nLastTargetFrame
function Lockdown:UpdateReticleTarget()
if not PLAYER or uLockedTarget then return end
uCurrentTarget = GameLib.GetTargetUnit()
-- Clear targets too far away from reticle
if uCurrentTarget and not uLockedTarget and self.settings.reticle_clear_distant_target and uCurrentTarget == uLastAutoTarget then
local bInReticle, nDistIntersection = UnitInReticle(uCurrentTarget)
if not bInReticle and nDistIntersection > self.settings.reticle_clear_distant_range then
GameLib.SetTargetUnit()
end
end
local nBest, uBest = 999
for id, unit in pairs(inrange) do
if not unit:IsOccluded() then
-- Check reticle intersection
local bInReticle, _, nDistPoints = UnitInReticle(unit)
if bInReticle and nDistPoints < nBest then
-- Verify possibly stale units
if unit == uLastAutoTarget and not uCurrentTarget then
-- SendVarToRover("Last lockdown target", unit)
self:Handler_ProcessUnit(unit, true)
if onscreen[id] then
nBest, uBest = nDistPoints, unit
end
else
nBest, uBest = nDistPoints, unit
end
end
end
end
-- Target best unit
-- TODO: Re-add delayed targeting
if uBest and uCurrentTarget ~= uBest then
uCurrentTarget, uLastAutoTarget = uBest, uBest
-- Save tick to detect failed target attempts
nLastTargetFrame = FRAME
if self.settings.auto_target then
GameLib.SetTargetUnit(uBest)
end
end
end
----------------------------------------------------------
-- Configuration
-- Specific setting change handlers
-- CAN'T TAKE THE CHANGE, MAN
local ChangeHandlers = {}
function Lockdown:ChangeSetting(setting, value)
self.settings[setting] = value
if ChangeHandlers[setting] then
ChangeHandlers[setting](value)
end
end
local function ReticleChanged()
Lockdown.wndReticle:Show(true)
Lockdown:Reticle_Update()
end
ChangeHandlers.reticle_opacity = ReticleChanged
ChangeHandlers.reticle_size = ReticleChanged
ChangeHandlers.reticle_offset_x = ReticleChanged
ChangeHandlers.reticle_offset_y = ReticleChanged
ChangeHandlers.reticle_hue_red = ReticleChanged
ChangeHandlers.reticle_hue_green = ReticleChanged
ChangeHandlers.reticle_hue_blue = ReticleChanged
ChangeHandlers.reticle_sprite = ReticleChanged
local function AutoTargetDisposition(sDisposition)
return function(value)
Lockdown.tTargetDispositions[Unit.CodeEnumDisposition[sDisposition]] = value
end
end
ChangeHandlers.auto_target_friendly = AutoTargetDisposition("Friendly")
ChangeHandlers.auto_target_neutral = AutoTargetDisposition("Neutral")
ChangeHandlers.auto_target_hostile = AutoTargetDisposition("Hostile")
-- Per category widget handlers
-- Binding keys
local tBindKeyMap = {}
local sWhichBind
local function BindKeySetText(btn, setting)
local s = Lockdown.settings
if s[setting] and s[setting] ~= "" then
btn:SetText(SystemKeyMap[s[setting]] or "????")
else
btn:SetText()
end
end
function Lockdown:OnBindKey(btn)
local setting = tBindKeyMap[btn:GetName()]
if not self.bind_mode_active then
self.bind_mode_active = true
btn:SetText(L.button_label_bind_wait)
btn:SetCheck(true)
sWhichBind = setting
self.w.Btn_Unbind:Show(true)
elseif sWhichBind == setting then
self.bind_mode_active = false
BindKeySetText(btn, setting)
btn:SetCheck(false)
self.w.Btn_Unbind:Show(false)
end
end
-- Binding modifiers
local tBindModMap = {}
function Lockdown:OnBindMod(btn)
local setting = tBindModMap[btn:GetName()]
local mod = self.settings[setting]
if not mod then
mod = "shift"
elseif mod == "shift" then
mod = "control"
else
mod = false
end
self:ChangeSetting(setting, mod)
self:KeyOrModifierUpdated()
btn:SetText(mod and mod or L.button_label_mod)
end
-- Checkboxes
local tCheckboxMap = {}
function Lockdown:OnButtonCheck(btn)
self:ChangeSetting(tCheckboxMap[btn:GetName()], btn:IsChecked())
end
-- Sliders
local tSliderMap = {}
function Lockdown:OnSlider(slider)
local setting, value = tSliderMap[slider:GetName()], slider:GetValue()
self:ChangeSetting(setting, value)
self:UpdateWidget_Slider(slider, setting)
end
function Lockdown:UpdateWidget_Slider(slider, setting)
local text = self.w["Text_"..setting]
if text then
text:SetText(math.floor(self.settings[setting]*100)/100)
end
end
-- General handlers
function Lockdown:OnConfigure()
self:UpdateConfigUI()
self.wndOptions:Invoke()
end
function Lockdown:OnConfigureClose()
self.wndOptions:Close()
end
function Lockdown:OnBtn_Unbind()
if self.bind_mode_active then
self.bind_mode_active = false
self:ChangeSetting(sWhichBind, "")
self:KeyOrModifierUpdated()
self:UpdateConfigUI()
end
end
function Lockdown:OnTab_General()
self.w.Content_General:Show(true)
self.w.Content_Reticle:Show(false)
self.w.Tab_General:SetCheck(true)
self.w.Tab_Reticle:SetCheck(false)
end
function Lockdown:OnTab_Reticle()
self.w.Content_General:Show(false)
self.w.Content_Reticle:Show(true)
self.w.Tab_General:SetCheck(false)
self.w.Tab_Reticle:SetCheck(true)
end
-- Update all text in config window
local bSettingsInit = false
function Lockdown:UpdateConfigUI()
local s, w = self.settings, self.w
if not bSettingsInit then
bSettingsInit = true
-- Load settings window
self.wndOptions = Apollo.LoadForm("Lockdown.xml", "Lockdown_OptionsForm", nil, self)
-- Reference all children by name
self.w = children_by_name(self.wndOptions)
w = self.w
-- Options children cache
--[=[ w = setmetatable({}, {__index = function(t, k)
local child = self.wndOptions:FindChild(k)
if child then
rawset(t, k, child)
end
return child
end})
self.xml = nil--]=]
-- Map elements matching Prefix_{setting} to settings
-- in given element_map table
-- Add desired events to those elements
local match, format = string.match, string.format
local function SetUpElements(element_prefix, element_map, event_map, setting_suffix)
-- Search for elements matching prefix
local pattern = "^"..element_prefix.."_(.+)$"
for name, elem in pairs(w) do
local found = match(name, pattern)
if found then
-- Confirm existing setting
local setting = setting_suffix and format("%s_%s", found, setting_suffix) or found
if self.defaults[setting] ~= nil then
-- Map
element_map[name] = setting
-- Add event handlers
for event, handler in pairs(event_map) do
elem:AddEventHandler(event, handler, self)
end
-- Localization
if L[setting] then
if element_prefix == "Check" then
elem:SetText(L[setting])
elseif element_prefix == "Slider" then
w["Widget_"..setting]:SetText(L[setting])
end
elseif element_prefix == "Key" and L[found] then
w["Widget_"..found]:SetText(L[found])
end
else
print("Element not bound", name, setting)
end
end
end
end
-- Checkboxes
SetUpElements("Check", tCheckboxMap, { ButtonCheck = "OnButtonCheck", ButtonUncheck = "OnButtonCheck" })
-- Binding keys
SetUpElements("Key", tBindKeyMap, { ButtonSignal = "OnBindKey" }, "key")
-- Binding modifiers
SetUpElements("Mod", tBindModMap, { ButtonSignal = "OnBindMod" }, "mod")
-- Sliders
SetUpElements("Slider", tSliderMap, { SliderBarChanged = "OnSlider" })
-- Blunt localization
for k,v in pairs(w) do
if L[k] then
v:SetText(L[k])
end
end
-- Default tab
self:OnTab_General()
end
-- Update checkboxes
for name, setting in pairs(tCheckboxMap) do
w[name]:SetCheck(s[setting])
end
-- Update key bind buttons
self.bind_mode_active = false
for name, setting in pairs(tBindKeyMap) do
BindKeySetText(w[name], setting)
w[name]:SetCheck(false)
end
-- Update key modifier buttons
for name, setting in pairs(tBindModMap) do
w[name]:SetText(s[setting] or L.button_label_mod)
end
-- Update sliders
for name, setting in pairs(tSliderMap) do
w[name]:SetValue(s[setting])
self:UpdateWidget_Slider(w[name], setting)
end
self.w.Btn_Unbind:Show(false)
end
----------------------------------------------------------
-- Keybind handling
-- Store key and modifier check function
local locktarget_key, locktarget_mod, manualtarget_key, manualtarget_mod
local function Upvalues(whichkey, whichmod)
local mod = opt[whichmod]
if mod == "shift" then
mod = Apollo.IsShiftKeyDown
elseif mod == "control" then
mod = Apollo.IsControlKeyDown
else
mod = false
end
return opt[whichkey], mod
end
function Lockdown:KeyOrModifierUpdated()
locktarget_key, locktarget_mod = Upvalues("locktarget_key", "locktarget_mod")
manualtarget_key, manualtarget_mod = Upvalues("manualtarget_key", "manualtarget_mod")
end
-- Keys
function Lockdown:EventHandler_SystemKeyDown(iKey, ...)
-- Listen for key to bind
if self.bind_mode_active and iKey ~= 0xF1 and iKey ~= 0xF2 then
self.bind_mode_active = false
self:ChangeSetting(sWhichBind, iKey)
self:KeyOrModifierUpdated()
self:UpdateConfigUI()
return
end
-- Manual target
if iKey == manualtarget_key and (not manualtarget_mod or manualtarget_mod()) then
if uCurrentTarget then
GameLib.SetTargetUnit(uCurrentTarget)
end
-- Open options on Escape
-- TODO: Pause locking for a short time after pressing escape
elseif iKey == 27 and LOCK_STATE then
PAUSED = true
self.timerEscResume:Start()
-- Lock target
elseif iKey == locktarget_key and (not locktarget_mod or locktarget_mod()) then
if uLockedTarget then
uLockedTarget = nil
system_print(L.message_target_unlocked)
-- TODO: Locked target indicator instead of clearing target
else
uLockedTarget = GameLib.GetTargetUnit()
if uLockedTarget and uLockedTarget:IsValid() then
system_print((L.message_target_locked):format(uLockedTarget:GetName()))
end
end
end
end
function Lockdown:TimerHandler_EscResume()
PAUSED = false
end
function Lockdown:TimerHandler_DelayedTarget()
end
function Lockdown:EventHandler_TargetUnitChanged()
if not GameLib.GetTargetUnit() then
-- Simple method to prevent target spamming on untargetable mobs
if uLastAutoTarget and nLastTargetFrame == FRAME then
local uid = uLastAutoTarget:GetId()
if uid then
onscreen[uid] = nil
inrange[uid] = nil
suspended[uid] = os.time() + 5
end
end
uCurrentTarget = nil
end
if uLockedTarget then
uLockedTarget = nil
system_print(L.message_target_unlocked)
end
end
function Lockdown:EventHandler_NextFrame()
if FRAME >= 254 then
FRAME = FRAME + 1
else
FRAME = 0
end
end
local iSlow = 0
-- Apparently fired every four frames
function Lockdown:EventHandler_FrameCount(_, nFrame)
-- Track state and manage reticle
local bState = GameLib.IsMouseLockOn()
if bState ~= LOCK_STATE then
LOCK_STATE = bState
self.wndReticle:Show(bState and self.settings.reticle_show)
end
-- Evaluate inrange units for a new target
if bState and not PAUSED then
self:UpdateReticleTarget()
end
-- Low rate updates
iSlow = iSlow + 1
-- if iSlow >= nSlowUpdateRate then
if iSlow >= 4 then
iSlow = 0
if PLAYER then
-- Update marker distances
if LOCK_STATE then
local vehicle = GameLib.GetPlayerVehicleUnit()
local origin = (vehicle and vehicle or PLAYER):GetPosition()
local limit = self.settings.reticle_range_limit
for uid, unit in pairs(onscreen) do
local p = onscreen[uid]:GetPosition()
if p then
if Vector3.New(p.x - origin.x, p.y - origin.y, p.z - origin.z):Length() < limit then
if not inrange[uid] then
inrange[uid] = unit
end
elseif inrange[uid] then
inrange[uid] = nil
end
end
end
end
-- Update suspension list
if next(suspended) then
local now = os.time()
for uid, expires in pairs(suspended) do
if expires < now then
suspended[uid] = nil
self:Handler_ProcessUnit(GameLib.GetUnitById(uid))
end
end
end
else
-- Player is invalid on initial addon load
-- A UnitCreated event is not fired on initial load
-- It is also invalidated on changing worlds, but does get
-- a UnitCreated there. So we'll just timer it instead.
local player = GameLib.GetPlayerUnit()
if player and player:IsValid() then
PLAYER = player
-- Process buffer
for i=#buffered,1,-1 do
self:Handler_ProcessUnit(buffered[i])
buffered[i] = nil