forked from Xuerian/Lockdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockdown.lua
More file actions
1371 lines (1216 loc) · 38.7 KB
/
Copy pathLockdown.lua
File metadata and controls
1371 lines (1216 loc) · 38.7 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
-- Addon authors or Carbine, to add a window to the list of pausing windows, call this for normal windows
-- Event_FireGenericEvent("CombatMode_RegisterPausingWindow", wndHandle[, bCheckOften])
-- Lockdown automatically detects immediately created escapable windows, but redundant registration is not harmful.
----------------------------------------------------------
-- Window lists
-- Add windows that don't close on escape here
-- Many Carbine windows must be caught via event handlers
-- since they get recreated and handles go stale.
local tAdditionalWindows = {
"RoverForm",
"GeminiConsoleWindow",
"KeybindForm",
"DuelRequest",
-- Necessary?
"RoleConfirm",
"JoinGame",
"VoteKick",
"VoteSurrender",
"AddonError",
"ResurrectDialog",
"ExitInstanceDialog",
"GuildDesignerForm",
"SupplySachelForm",
"ViragsSocialMainForm",
}
-- Add windows to ignore here
local tIgnoreWindows = {
-- Floating text panes
StoryPanelInformational = true,
StoryPanelBubble = true,
StoryPanelBubbleCenter = true,
-- ProcsHUD
ProcsIcon1 = true,
ProcsIcon2 = true,
ProcsIcon3 = true,
-- ?
LootNotificationForm = true,
-- Clairvoyance mod, whose windows are all escapable for no good reason
ClairvoyanceNotification = true,
DisorientWindow = true,
WeaponIndicator = true,
-- Killing blow mod
KillingBlowAlert = true,
-- ZInterrupt
InterruptProgress = true,
}
-- Add windows to be checked at a high rate here
local tHotList = {
SpaceStashInventoryForm = true,
InventoryBag = true,
ProgressLogForm = true,
QuestWindow = true,
ZoneMapFrame = true,
QuestWindow_Metal = true,
QuestWindow_Holo = true,
}
----------------------------------------------------------
-- 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 = "Orange settings require included ahk script, read the Lockdown Curse page for details. Orange and yellow settings require a UI reload to take effect.",
togglelock = "Toggle Lockdown",
locktarget = "Lock/Unlock current target",
manualtarget = "Manual target",
Widget_free_with = "Free cursor while holding..",
free_also_toggles = "Free cursor acts as toggle (Unfinished)",
lock_on_load = "Lock on startup",
ahk_cursor_center = "Center cursor on lock",
ahk_update_interval = "AHK update interval [ms]",
auto_target = "Reticle targeting",
reticle_show = "Show reticle",
auto_target_delay = "Reticle target delay",
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 bActiveIntent
----------------------------------------------------------
-- Local references
local pairs, ipairs, table, string, math = pairs, ipairs, table, string, math
local GameLib, Apollo = GameLib, Apollo
----------------------------------------------------------
-- Settings
-- Defaults
local Lockdown = {
defaults = {
lock_on_load = true,
togglelock_key = 192, -- `
togglelock_mod = false,
locktarget_key = 20, -- Caps Lock
locktarget_mod = false,
manualtarget_key = 84, -- T
manualtarget_mod = "control",
free_with_shift = false,
free_with_ctrl = false,
free_with_alt = true,
free_also_toggles = false,
reticle_show = true,
auto_target = true,
auto_target_neutral = false,
auto_target_hostile = true,
auto_target_friendly = false,
auto_target_settler = false,
auto_target_delay = 0,
auto_target_interval = 100,
reticle_opacity = 0.3,
reticle_size = 32,
reticle_sprite = "giznat",
reticle_offset_x = 0,
reticle_offset_y = 0,
reticle_hue_red = 1,
reticle_hue_blue = 1,
reticle_hue_green = 1,
ahk_lmb_key = 189, -- -
ahk_rmb_key = 187, -- =
ahk_mmb_key = "",
ahk_cursor_center = true,
ahk_update_interval = 100,
},
settings = {},
reticles = {}
}
-- 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})
----------------------------------------------------------
-- TinyAsync, because WildStar
local TinyAsync = { timers = {}, i = 0 }
function TinyAsync: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 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
TinyAsync:Wait(function() return ChatAddon and ChatAddon.tWindow end, function()
function print(...)
local out = {}
for i=1,select('#', ...) do
table.insert(out, tostring(select(i, ...)))
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, 1)
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
----------------------------------------------------------
-- Because Carbine does it
function Lockdown:Init()
Apollo.RegisterAddon(self, true, L.button_configure)
Apollo.RegisterEventHandler("UnitCreated", "PreloadHandler_UnitCreated", self)
end
----------------------------------------------------------
-- Easy event handlers
-- Register a window update for a given event
function Lockdown:AddWindowEventListener(sEvent, sName)
local sHandler = "AutoEventHandler_"..sEvent
Apollo.RegisterEventHandler(sEvent, sHandler, self)
self[sHandler] = function(self_)
local wnd = Apollo.FindWindowByName(sName)
if wnd then
self_:RegisterWindow(wnd)
end
end
end
-- Register a delayed window update for a given event
local tDelayedWindows = {}
function Lockdown:AddDelayedWindowEventListener(sEvent, sName)
local sHandler = "AutoEventHandler_"..sEvent
Apollo.RegisterEventHandler(sEvent, sHandler, self)
self[sHandler] = function(self_)
tDelayedWindows[sName] = true
Lockdown.timerDelayedFrameCatch:Start()
end
end
----------------------------------------------------------
-- Saved data
function Lockdown:OnSave(eLevel)
if eLevel == GameLib.CodeEnumAddonSaveLevel.General then
local s = self.settings
s.ahk_lmb = SystemKeyMap[s.ahk_lmb_key] or ""
s.ahk_rmb = SystemKeyMap[s.ahk_rmb_key] or ""
s.ahk_mmb = SystemKeyMap[s.ahk_mmb_key] or ""
-- 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.General 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()
self.timerDelayedTarget:Set(s.auto_target_delay / 1000, false)
-- Show settings window after reload
if tData._is_ahk_reload then
self:OnConfigure()
end
self:SetActionMode(s.lock_on_load)
-- SendVarToRover("Lockdown", self)
end
end
local preload_units, is_scientist, is_settler, player = {}
function Lockdown:OnLoad()
----------------------------------------------------------
-- Load reticle
self.xmlDoc = XmlDoc.CreateFromFile("Lockdown.xml")
self.wndReticle = Apollo.LoadForm(self.xmlDoc, "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]], 128)
self:Reticle_Update()
self.wndReticle:Show(GameLib.IsMouseLockOn())
Apollo.RegisterEventHandler("ResolutionChanged", "Reticle_Update", self)
----------------------------------------------------------
-- Options
Apollo.RegisterSlashCommand("lockdown", "OnConfigure", self)
----------------------------------------------------------
-- Targeting
Apollo.RegisterEventHandler("TargetUnitChanged", "EventHandler_TargetUnitChanged", self)
self.timerRelock = ApolloTimer.Create(0.01, false, "TimerHandler_Relock", self)
self.timerRelock:Stop()
self.timerDelayedTarget = ApolloTimer.Create(1, false, "TimerHandler_DelayedTarget", self)
self.timerDelayedTarget:Stop()
----------------------------------------------------------
-- Automatic [un]locking
-- Crawl for frames to hook
self.timerFrameCrawl = ApolloTimer.Create(5.0, false, "TimerHandler_FrameCrawl", self)
-- Wait for windows to be created or re-created
self.timerDelayedFrameCatch = ApolloTimer.Create(0.1, false, "TimerHandler_DelayedFrameCatch", self)
self.timerDelayedFrameCatch:Stop()
-- Poll frames for visibility.
-- I'd much rather use hooks or callbacks or events, but I can't hook, I can't find the right callbacks/they don't work, and the events aren't consistant or listed.
-- You made me do this, Carbine.
self.timerColdPulse = ApolloTimer.Create(0.5, true, "TimerHandler_ColdPulse", self)
self.timerHotPulse = ApolloTimer.Create(0.2, true, "TimerHandler_HotPulse", self)
----------------------------------------------------------
-- Windows and their relevant events
Apollo.RegisterEventHandler("CombatMode_RegisterPausingWindow", "RegisterWindow", self)
-- These windows are created or re-created and must be caught with event handlers
-- Abilities builder
self:AddWindowEventListener("AbilityWindowHasBeenToggled", "AbilitiesBuilderForm")
-- Social panel
self:AddWindowEventListener("GenericEvent_InitializeFriends", "SocialPanelForm")
-- Lore window
self:AddWindowEventListener("HudAlert_ToggleLoreWindow", "LoreWindowForm")
self:AddWindowEventListener("InterfaceMenu_ToggleLoreWindow", "LoreWindowForm")
-- Guild windows
self:AddDelayedWindowEventListener("GuildBankerOpen", "GuildBankForm")
self:AddDelayedWindowEventListener("Guild_ToggleInfo", "GuildInfoForm")
self:AddDelayedWindowEventListener("Guild_TogglePerks", "GuildPerksForm")
self:AddDelayedWindowEventListener("Guild_ToggleRoster", "GuildRosterForm")
-- Crafting grid
self:AddDelayedWindowEventListener("GenericEvent_StartCraftingGrid", "CraftingGridForm")
-- Runecrafting
self:AddDelayedWindowEventListener("GenericEvent_CraftingResume_OpenEngraving", "RunecraftingForm")
-- Tradeskill trainer
self:AddDelayedWindowEventListener("InvokeTradeskillTrainerWindow", "TradeskillTrainerForm")
-- Settler building
self:AddDelayedWindowEventListener("InvokeSettlerBuild", "BuildMapForm")
-- Commodity marketplace
self:AddDelayedWindowEventListener("ToggleMarketplaceWindow", "MarketplaceCommodityForm")
-- Auctionhouse
self:AddDelayedWindowEventListener("ToggleAuctionWindow", "MarketplaceAuctionForm")
-- CREDD
self:AddDelayedWindowEventListener("ToggleCREDDExchangeWindow", "MarketplaceCREDDForm")
-- Instance settings
self:AddDelayedWindowEventListener("ShowInstanceGameModeDialog", "InstanceSettingsForm")
self:AddDelayedWindowEventListener("ShowInstanceRestrictedDialog", "InstanceSettingsRestrictedForm")
-- Public events
self:AddDelayedWindowEventListener("PublicEventInitiateVote", "PublicEventVoteForm")
self:AddDelayedWindowEventListener("PublicEventStart", "PublicEventStatsForm")
self:AddDelayedWindowEventListener("GenericEvent_OpenEventStatsZombie", "PublicEventStatsForm")
-- Match Tracker / PVP Score
self:AddDelayedWindowEventListener("Datachron_LoadPvPContent", "MatchTracker")
-- Bank
self:AddDelayedWindowEventListener("ShowBank", "BankViewerForm")
self:AddDelayedWindowEventListener("ToggleBank", "BankViewerForm")
----------------------------------------------------------
-- Keybinds
Apollo.RegisterEventHandler("SystemKeyDown", "EventHandler_SystemKeyDown", self)
self.timerFreeKeys = ApolloTimer.Create(0.1, true, "TimerHandler_FreeKeys", self)
-- Rainbows, unicorns, and kittens
-- Oh my
self:KeyOrModifierUpdated()
----------------------------------------------------------
-- Defer advanced targeting startup
if self.settings.auto_target then
self.timerHAL = ApolloTimer.Create(self.settings.auto_target_interval/1000, true, "TimerHandler_HAL", self)
self.timerHAL:Stop()
TinyAsync:Wait(function()
player = GameLib.GetPlayerUnit()
return player and player:IsValid()
end,
function()
-- Get player path
local nPath = PlayerPathLib.GetPlayerPathType()
is_scientist = nPath == 2
is_settler = nPath == 1
-- Update event registration
Apollo.RemoveEventHandler("UnitCreated", self)
Apollo.RegisterEventHandler("UnitCreated", "EventHandler_UnitCreated", self)
Apollo.RegisterEventHandler("UnitDestroyed", "EventHandler_UnitDestroyed", self)
Apollo.RegisterEventHandler("UnitGibbed", "EventHandler_UnitDestroyed", self)
Apollo.RegisterEventHandler("UnitActivationTypeChanged", "RefreshUnit", self)
-- Process pre-load units
for i,v in ipairs(preload_units) do
self:EventHandler_UnitCreated(v)
end
preload_units = nil
self.HALReady = true
-- Initial locked timer
if GameLib.IsMouseLockOn() then
self:StartHAL()
end
end)
end
end
function Lockdown:StartHAL()
if self.HALReady then
self.timerHAL:Start()
end
end
function Lockdown:StopHAL()
if self.HALReady then
self.timerHAL:Stop()
end
end
----------------------------------------------------------
-- Units and Advanced Targeting
function Lockdown:PreloadHandler_UnitCreated(unit)
table.insert(preload_units, unit)
end
local markers = {}
local onscreen = {}
function Lockdown:RefreshUnit(unit)
-- Catch newly activateable units
if not markers[unit:GetId()] then
self:EventHandler_UnitCreated(unit)
end
-- Qualify unit
self:EventHandler_WorldLocationOnScreen(nil, nil, GameLib.GetUnitScreenPosition(unit).bOnScreen, unit)
end
-- Store category of marker
function Lockdown:EventHandler_UnitCreated(unit)
local id = unit:GetId()
-- Invalid or existing markers
if not id or markers[id] or not unit:IsValid() or unit:IsThePlayer() then return nil end
local utype = unit:GetType()
-- Filter units
-- Players (Except Player)
if utype == "Player" or utype == "NonPlayer" or unit:GetRewardInfo() or unit:GetActivationState()
-- NPCs that get plates
-- or ((utype == "NonPlayer" or utype == "Turret") and unit:ShouldShowNamePlate())
-- Harvestable nodes (Except farming)
or (utype == "Harvest" and unit:GetHarvestRequiredTradeskillName() ~= "Farmer" and unit:CanBeHarvestedBy(player))
then
-- Ok!
-- Quest objective units, scannables
-- These are filtered in WorldLocationOnScreen, since they can change.
-- elseif (utype == "Simple" or utype == "NonPlayer") and unit:GetRewardInfo() then
-- Ok!
else return end -- Not ok.
-- Activate marker
local marker = Apollo.LoadForm(self.xmlDoc, "Lockdown_Marker", "InWorldHudStratum", self)
marker:Show()
marker:SetData(unit)
marker:SetUnit(unit)
markers[id] = marker
self:RefreshUnit(unit)
end
function Lockdown:EventHandler_UnitDestroyed(unit)
local id = unit:GetId()
if markers[id] then
markers[id]:Destroy()
markers[id] = nil
if GameLib.IsMouseLockOn() and unit == GameLib.GetTargetUnit() then
uCurrentTarget = nil
GameLib.SetTargetUnit()
end
end
if onscreen[id] then
onscreen[id] = nil
end
end
-- Check a marker (Unit) that just entered or left the screen
-- This function could possibly structured better
-- However, this is the way that most made sense at the time
function Lockdown:EventHandler_WorldLocationOnScreen(wnd, ctrl, visible, unit)
unit = unit or ctrl:GetData()
-- Purge invalid or dead units
if not unit:IsValid() or unit:IsDead() then
self:EventHandler_UnitDestroyed(unit)
return
-- Visible units
elseif visible then
-- Basic relevance
if unit:ShouldShowNamePlate() and self.tTargetDispositions[unit:GetDispositionTo(player)] then
onscreen[unit:GetId()] = unit
return
end
-- Units we want based on activation state
local tAct = unit:GetActivationState()
-- Ignore settler "Minfrastructure"
-- TODO: Options to include/filter these things
if tAct then
-- Hide already activated quest objects
if tAct.QuestTarget and not (tAct.Interact and tAct.Interact.bCanInteract) then
onscreen[unit:GetId()] = nil
return
end
-- Hide settler collection or improvements
if is_settler and not opt.auto_target_settler and (tAct.SettlerMinfrastructure or (tAct.Collect and tAct.Collect.bUsePlayerPath)) then
onscreen[unit:GetId()] = nil
return
end
-- Generic activateable objects
local t = tAct.Interact
if t and (t.bCanInteract or t.bIsHighlightable or t.bShowCallout) then
onscreen[unit:GetId()] = unit
return
end
-- Quest turn-ins
if tAct.QuestReward and tAct.QuestReward.bIsActive and tAct.QuestReward.bCanInteract then
onscreen[unit:GetId()] = unit
return
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]
-- Quest items we need and haven't interacted with
if (t.eType == Unit.CodeEnumRewardInfoType.Quest and t.nCompleted and t.nCompleted < t.nNeeded and (not tAct or not tAct.Interact or tAct.Interact.bCanInteract)) -- or #tAct == 0
-- or scientist scans
or (t.eType == Unit.CodeEnumRewardInfoType.Scientist and is_scientist) then
onscreen[unit:GetId()] = unit
return
end
end
end
end
-- Invisible or otherwise undesirable units
onscreen[unit:GetId()] = nil
end
-- Scan lists of markers in order of priority based on current state
-- If reticle center within range of unit center (reticle size vs estimated object size)
-- If object meets criteria (Node range, ally health)
local pReticle, nReticleRadius
local nLastTargetTime, uDelayedTarget, uCurrentTarget, uLastAutoTarget, uLockedTarget = 0
function Lockdown:TimerHandler_HAL()
if not player or uLockedTarget then return end
-- Prevent excessive flickering
if (os.clock() - nLastTargetTime) < 0.2 then return end
-- Grab local references to things we're going to use each iteration
local NewPoint, PointLength = Vector2.New, pReticle.Length
local GetUnitScreenPosition = GameLib.GetUnitScreenPosition
local GetOverheadAnchor, GetType = player.GetOverheadAnchor, player.GetType
local IsDead, IsOccluded = player.IsDead, player.IsOccluded
uCurrentTarget = GameLib.GetTargetUnit()
local nBest, uBest = 999
-- Iterate over onscreen units
for id, unit in pairs(onscreen) do
local tPos = GetUnitScreenPosition(unit)
-- Destroy player markers we shouldn't be getting
if unit == player or IsDead(unit) then
self:EventHandler_UnitDestroyed(unit)
-- Verify that unit is still on screen
elseif tPos and tPos.bOnScreen and not IsOccluded(unit) then
-- Try to place unit point in middle of unit
local pOverhead, nUnitRadius = GetOverheadAnchor(unit), 0
if pOverhead then
nUnitRadius = (tPos.nY - pOverhead.y)/2
-- Sanity check radius
if nUnitRadius < 0 then
nUnitRadius = 15
end
end
-- Check reticle intersection
local nDist = PointLength(NewPoint(tPos.nX, tPos.nY - nUnitRadius) - pReticle)
if nDist < nBest and nDist < (nUnitRadius + nReticleRadius) then
-- Verify possibly stale units
if unit == uLastAutoTarget and not uCurrentTarget then
-- SendVarToRover("Last lockdown target", unit)
self:EventHandler_WorldLocationOnScreen(nil, nil, true, unit)
if onscreen[unit:GetId()] then
nBest, uBest = nDist, unit
end
else
nBest, uBest = nDist, unit
end
end
end
end
-- Target best unit
-- TODO: Re-add delayed targeting
if uBest and uCurrentTarget ~= uBest then
uCurrentTarget, uLastAutoTarget = uBest, uBest
nLastTargetTime = os.clock()
if self.settings.auto_target then
GameLib.SetTargetUnit(uBest)
end
end
end
----------------------------------------------------------
-- Frame discovery, handling, and polling
-- Disover frames we should pause for
local tColdWindows, tHotWindows, tWindows = {}, {}, {}
function Lockdown:RegisterWindow(wnd, hot)
if wnd then
local sName = wnd:GetName()
if not tIgnoreWindows[sName] then
-- Add to window index
if not tWindows[sName] then
tWindows[sName] = (tHotList[sName] or hot) and "hot" or "cold"
end
-- Add or update handle
if tWindows[sName] == "hot" then
tHotWindows[sName] = wnd
else
tColdWindows[sName] = wnd
end
return true
end
end
return false
end
-- Discover simple unlocking frames
function Lockdown:TimerHandler_FrameCrawl()
local tStrata = Apollo.GetStrata()
for i=1,#tStrata do
local tWindows = Apollo.GetWindowsInStratum(tStrata[i])
for i=1,#tWindows do
if tWindows[i]:IsStyleOn("Escapable") and not tWindows[i]:IsStyleOn("CloseOnExternalClick") then
Lockdown:RegisterWindow(tWindows[i])
end
end
end
-- Existing frames that aren't found above
for i=1,#tAdditionalWindows do
local wnd = Apollo.FindWindowByName(tAdditionalWindows[i])
if wnd then
Lockdown:RegisterWindow(wnd)
end
end
end
-- Wait for certain frames to be created or recreated after a event
function Lockdown:TimerHandler_DelayedFrameCatch()
for sName in pairs(tDelayedWindows) do
local wnd = Apollo.FindWindowByName(sName)
if wnd then
self:RegisterWindow(wnd)
tDelayedWindows[sName] = nil
end
end
end
-- Poll unlocking frames
local free_key_held = false -- User toggling mouse with a modifier key
local tSkipWindows = {}
local bColdSuspend, bHotSuspend = false, false
function Lockdown:PulseCore(t, other_suspend, csi)
local bWindowUnlock = false
if not free_key_held then
local tSkipWindows = tSkipWindows
-- Poll windows
for _, wnd in pairs(t) do
-- Unlock if visible and not currently skipped
if wnd:IsShown() and wnd:IsValid() then
if not tSkipWindows[wnd] then
bWindowUnlock = true
end
-- Expire hidden from skiplist
elseif tSkipWindows[wnd] then
tSkipWindows[wnd] = nil
end
end
-- CSI(?) dialogs
-- TODO: Skip inconsequential CSI dialogs (QTEs)
if csi then
if CSIsLib.GetActiveCSI() then
if not tSkipWindows.CSI then
bWindowUnlock = true
end
else
tSkipWindows.CSI = false
end
end
-- Update lock
local lock = GameLib.IsMouseLockOn()
if not (bWindowUnlock or other_suspend or lock) and bActiveIntent then
self:SetActionMode(true)
elseif (bWindowUnlock or other_suspend) and lock then
self:SuspendActionMode()
end
end
return bWindowUnlock
end
function Lockdown:TimerHandler_ColdPulse()
bColdSuspend = self:PulseCore(tColdWindows, bHotSuspend, true)
end
function Lockdown:TimerHandler_HotPulse()
bHotSuspend = self:PulseCore(tHotWindows, bColdSuspend)
end
function Lockdown:PollAllWindows()
self:TimerHandler_HotPulse()
self:TimerHandler_ColdPulse()
return bColdSuspend or bHotSuspend
end
function Lockdown:TimerHandler_Relock()
self:SetActionMode(true)
end
----------------------------------------------------------
-- Configuration
-- Specific setting change handlers
-- CAN'T TAKE THE CHANGE, MAN
local ChangeHandlers = {}
function ChangeHandlers.auto_target_delay(value)
Lockdown.timerDelayedTarget:Set(value, false)
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.settings[setting] = mod
self:KeyOrModifierUpdated()
btn:SetText(mod and mod or L.button_label_mod)
end
-- Checkboxes
local tCheckboxMap = {}
function Lockdown:OnButtonCheck(btn)
self.settings[tCheckboxMap[btn:GetName()]] = btn:IsChecked()
end
-- Sliders
local tSliderMap = {}
function Lockdown:OnSlider(slider)
local setting, value = tSliderMap[slider:GetName()], slider:GetValue()
self.settings[setting] = value
if ChangeHandlers[setting] then
ChangeHandlers[setting](value)
end
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:Show(true, true)
end
function Lockdown:OnConfigureClose()
self.wndOptions:Show(false, true)
end
function Lockdown:OnBtn_Unbind()
if self.bind_mode_active then
self.bind_mode_active = false
self.settings[sWhichBind] = ""
self:KeyOrModifierUpdated()
self:UpdateConfigUI()
end
end
function Lockdown:OnBtn_ReloadUI()
self.settings._is_ahk_reload = true
RequestReloadUI()
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)
self:RegisterWindow(self.wndOptions)
-- 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.settings[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