-
Notifications
You must be signed in to change notification settings - Fork 1
/
ezmq.lua
2944 lines (2570 loc) · 84.8 KB
/
ezmq.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
-- collection of functions to simplify working with macroquest through Lua
-- @type mq
local mq = require("mq")
local log = require("knightlinc/Write")
local timer = require("lib/Timer")
-- Returns a 24-hour timestamp, in the format "HH:MM:SS".
---@return string
function time()
---@diagnostic disable-next-line: return-type-mismatch
return os.date("%H:%M:%S")
end
-- Send a text to all peers
function all_tellf(...)
--cmdf("/dgtell all [%s] %s", time(), string.format(...))
mq.cmdf("/bc [%s] %s", time(), string.format(...))
end
--- Send a text to all peers thru MQ2DanNet
function all_tell(msg)
mq.cmdf("/bc [%s] %s", time(), msg)
end
local follow = require("lib/following/Follow")
local spellGroups = require("lib/spells/SpellGroups")
local serverSettings = require("lib/settings/default/ServerSettings")
local botSettings = require("lib/settings/BotSettings")
-- returns true if `spawn` is within maxDistance
---@param spawn spawn
---@param maxDistance number
---@return boolean
function is_within_distance(spawn, maxDistance)
return spawn ~= nil and spawn() ~= nil and spawn.Distance() <= maxDistance
end
-- Am I the foreground instance?
---@return boolean
function is_orchestrator()
return mq.TLO.FrameLimiter.Status() == "Foreground"
end
-- returns true if location is within maxDistance from you
---@return boolean
function is_within_distance_to_loc(y, x, z, maxDistance)
return mq.TLO.Math.Distance(string.format("%f,%f,%f", y, x, z))() <= maxDistance
end
-- returns true if there is line of sight between you and `spawn`
---@param spawn spawn
---@return boolean
function line_of_sight_to(spawn)
local q = mq.TLO.Me.Y()..","..mq.TLO.Me.X()..","..mq.TLO.Me.Z()..":"..spawn.Y()..","..spawn.X()..","..spawn.Z()
return mq.TLO.LineOfSight(q)()
end
function move_to_peer(peer)
local spawn = spawn_from_peer_name(peer)
if spawn == nil then
return
end
move_to(spawn.ID())
end
-- Move to the location of `spawn` using MQ2Nav.
-- Returns true on success
---@param spawnID integer
---@return boolean
function move_to(spawnID)
local spawn = spawn_from_id(spawnID)
if spawn == nil or spawn() == nil then
all_tellf("move_to: lost target spawn %d", spawnID)
return false
end
log.Debug("move_to %d (at distance %d)", spawnID, spawn.Distance())
--if serverSettings.followMode:lower() == "mq2advpath" and not line_of_sight_to(spawn) then
-- all_tellf("move_to ERROR: cannot see %s", spawn.Name())
-- return
--end
local dist = 10
if spawn.Distance() < dist then
return true
end
if not is_standing() then
mq.cmd("/stand")
mq.delay(50)
end
-- abort existing follow commands
follow.Pause()
if serverSettings.followMode:lower() == "mq2nav" then
mq.cmdf("/nav id %d", spawnID)
elseif serverSettings.followMode:lower() == "mq2advpath" then
mq.cmdf("/afollow spawn %d", spawnID)
end
mq.delay("4m", function()
return spawn == nil or spawn() == nil or spawn.Distance() < dist or not mq.TLO.Navigation.Active()
end)
-- abort our given move-to commands
follow.Pause()
if spawn() == nil then
return false
end
return spawn.Distance() < dist
end
-- Returns true if low on endurance
---@return boolean
function low_endurance()
return mq.TLO.Me.PctEndurance() < 70
end
---@param y number
---@param x number
---@param z number|nil
function move_to_loc(y, x, z)
local dist = 10
if not is_standing() then
mq.cmd("/stand")
mq.delay(50)
end
-- abort existing follow commands
follow.Pause()
if serverSettings.followMode:lower() == "mq2nav" then
mq.cmdf("/nav loc %d %d %d", y, x, z)
elseif serverSettings.followMode:lower() == "mq2advpath" then
-- uses MQ2MoveUtils. TODO: can we move to a location using /afollow ?
mq.cmdf("/moveto loc %f %f %f", y, x, z)
end
mq.delay(30000, function() return is_within_distance_to_loc(y, x, z, 15) end)
-- abort our given move-to commands
follow.Pause()
end
-- Does `t` contain `v`?
---@param t table
---@param v any
---@return boolean
function in_array(t, v)
for i = 1, #t do
if v == t[i] then return true end
end
return false
end
-- returns true if we are running the Emu build of MacroQuest (rof2 client)
---@return boolean
function is_rof2()
if mq.TLO.MacroQuest.BuildName() == "Emu" then
return true
end
return false
end
-- returns true if `x` is a number (integer or float)
---@param peer string Peer name
---@return boolean
function is_number(x)
if tonumber(x) ~= nil then
return true
end
return false
end
-- returns true if `peer` is a connected client
---@param peer string Peer name
---@return boolean
function is_peer(peer)
-- NOTE: NetBots return string "NULL" if not exists, if it exists then it returns a number
return is_number(mq.TLO.NetBots(peer).ID())
end
-- returns true if peerName is in the same zone
---@param peer string
---@return boolean
function is_peer_in_zone(peer)
local spawn = spawn_from_peer_name(peer)
return spawn ~= nil and is_peer(spawn.Name())
end
-- returns true if spawnID is another peer
---@param spawnID integer
---@return boolean
function is_peer_id(spawnID)
local spawn = spawn_from_id(spawnID)
return spawn ~= nil and is_peer(spawn.Name())
end
-- returns true if spawnID is in LoS
---@return boolean
function is_spawn_los(spawnID)
local spawn = spawn_from_id(spawnID)
return spawn ~= nil and spawn.LineOfSight()
end
-- returns true if spawnID is a NPC
---@return boolean
function is_npc(spawnID)
local spawn = spawn_from_id(spawnID)
return spawn ~= nil and spawn() ~= nil and (spawn.Type() == "NPC" or spawn.Type() == "Pet")
end
---@param spawnID integer
---@return spawn|nil
function spawn_from_id(spawnID)
return spawn_from_query("id "..tostring(spawnID))
end
---@param name string
---@return spawn|nil
function spawn_from_peer_name(name)
return spawn_from_query("pc =".. name)
end
-- Get the number of spawns matching `query`
---@return integer
function spawn_count(query)
return mq.TLO.SpawnCount(query)()
end
---@param query string
---@return spawn|nil
function spawn_from_query(query)
local o = mq.TLO.Spawn(query)
if o() == nil then
return nil
end
return o
end
--- Returns the name of the group priest curer, with a preference for SHM or DRU
---@return string|nil
function get_group_curer()
local name = nil
for i=1,mq.TLO.Group.Members() do
local class = mq.TLO.Group.Member(i).Class.ShortName()
if (class == "CLR") then
name = mq.TLO.Group.Member(i).Name()
end
end
for i=1,mq.TLO.Group.Members() do
local class = mq.TLO.Group.Member(i).Class.ShortName()
if class == "SHM" or class == "DRU" then
name = mq.TLO.Group.Member(i).Name()
end
end
return name
end
--- Returns the name of a nearby priest curer, with a preference for SHM or DRU
---@return string|nil
function get_nearby_curer()
local spawnQuery = 'pc radius 100'
local peers = spawn_count(spawnQuery)
if peers == 0 then
return nil
end
for i = 1, peers do
local spawn = mq.TLO.NearestSpawn(i, spawnQuery)
if spawn ~= nil and spawn.ID() ~= nil then
if spawn.Class.ShortName() == "CLR" or spawn.Class.ShortName() == "DRU" or spawn.Class.ShortName() == "SHM" then
return spawn.Name()
end
end
end
return nil
end
--- Returns true if `id` is an item I have in inventory or in bank.
---@param id integer
---@return boolean
function have_item_id(id)
return have_item_inventory_id(id) or have_item_banked_id(id)
end
--- Returns true if `name` is an item name I have in inventory or in bank.
---@param name string
---@return boolean
function have_item(name)
return have_item_inventory(name) or have_item_banked(name)
end
-- Returns true if `name` is an item I have in inventory.
---@param name string
---@return boolean
function have_item_inventory(name)
return name == nil or mq.TLO.FindItemCount("=" .. name)() > 0
end
-- Returns true if `id` is an item I have in inventory.
---@param id integer
---@return boolean
function have_item_inventory_id(id)
return mq.TLO.FindItemCount(id)() > 0
end
-- Returns true if `name` is an item I have in bank.
---@param name string
---@return boolean
function have_item_banked(name)
return mq.TLO.FindItemBankCount("=" .. name)() > 0
end
-- Returns true if `id` is an item I have in bank.
---@param id integer
---@return boolean
function have_item_banked_id(id)
return mq.TLO.FindItemBankCount(id)() > 0
end
-- Returns number of items by EXACT NAME in bank.
---@param name string
---@return integer
function banked_item_count(name)
return mq.TLO.FindItemBankCount("=" .. name)()
end
-- Returns number of items by EXACT NAME in inventory.
---@param name string
---@return integer
function inventory_item_count(name)
return mq.TLO.FindItemCount("=" .. name)()
end
-- Returns true if I have item `name` equipped.
---@param name string
---@return boolean
function have_item_equipped(name)
-- equipment: 0-22 is worn gear, 23-32 is inventory top level
for i = 0, 22 do
if mq.TLO.Me.Inventory(i).ID() ~= nil and mq.TLO.Me.Inventory(i).Name() == name then
return true
end
end
return false
end
-- Returns true if I have a target.
---@return boolean
function have_target()
return mq.TLO.Target() ~= nil
end
-- Get the current target.
---@return spawn|nil
function get_target()
if not have_target() then
return nil
end
return mq.TLO.Target
end
-- Target NPC by name.
---@param name string
function target_npc_name(name)
mq.cmdf('/target npc "%s"', name)
end
-- Target spawn by id.
---@param id integer
function target_id(id)
if id ~= nil then
mq.cmdf("/target id %d", id)
mq.delay(10)
mq.delay(1000, function() return mq.TLO.Target.ID() == id end)
end
end
-- Partial search by name (inventory, bags). Will not search bank!
---@param name string
---@return item|nil
function find_item(name)
local item = mq.TLO.FindItem(name)
if item() ~= nil then
return item
end
return nil
end
-- Partial search by name (banked items)
---@param name string
---@return item|nil
function find_item_bank(name)
local item = mq.TLO.FindItemBank(name)
if item() ~= nil then
return item
end
return nil
end
-- Exact search by name, return item name (clickable item link if possible)
---@param name string
---@return string
function item_link(name)
local item = find_item("="..name)
if item == nil then
return name
end
return item.ItemLink("CLICKABLE")()
end
-- Is `item` clicky effect ready to use?
---@param name string
---@return boolean
function is_item_clicky_ready(name)
local item = find_item(name)
if item == nil then
return false
end
return item.Clicky() ~= nil and item.Timer.Ticks() == 0
end
-- Returns true if `name` is a spell currently memorized in a gem
---@param name string
---@return boolean
function is_memorized(name)
return mq.TLO.Me.Gem(mq.TLO.Spell(name).RankName())() ~= nil
end
-- Is spell in my spellbook?
---@param name string
---@return boolean
function have_spell(name)
if have_alt_ability(name) then
-- NOTE: some AA's overlap with spell names.
-- We work around this by pretending to lack those spells if we have the AA.
-- Examples:
-- CLR/06 Sanctuary / CLR Sanctuary AA
-- SHM/62 Ancestral Guard / SHM Ancestral Guard AA
-- SHD/60 Death Peace / SHD Death Peace AA
return false
end
local ranked = mq.TLO.Spell(name).RankName()
if mq.TLO.Me.Book(ranked)() ~= nil then
return true
end
return mq.TLO.Me.Book(name)() ~= nil
end
-- Returns true if we have enough mana to cast `spell`
---@return boolean
function have_mana_for_spell(name)
if not have_spell(name) then
return false
end
local spell = mq.TLO.Spell(name)
-- add 20 mana to spell cost to mitigate rof2 client being out of sync with server regen (oct 2023)
return mq.TLO.Me.CurrentMana() > (20 + spell.Mana())
end
-- Is this a name of a spell?
---@param name string
---@return boolean
function is_spell(name)
if have_alt_ability(name) then
-- NOTE: some AA's overlap with spell names. Examples:
-- CLR/06 Sanctuary / CLR Sanctuary AA
-- SHM/62 Ancestral Guard / SHM Ancestral Guard AA
return false
end
return mq.TLO.Spell(name)() ~= nil
end
---@param name string
---@return spell|nil
function get_spell(name)
local spell = mq.TLO.Spell(name)
if spell ~= nil then
return mq.TLO.Spell(spell.RankName())
end
return nil
end
---@param id number spell id
---@return spell|nil
function get_spell_from_id(id)
local spell = mq.TLO.Spell(id)
if spell() ~= nil then
return spell
end
return nil
end
-- Is spell `name` ready to cast?
---@param name string
---@return boolean
function is_spell_ready(name)
if mq.TLO.Me.SpellInCooldown() then
-- global cooldown
return false
end
local spell = get_spell(name)
if spell == nil then
return false
end
return mq.TLO.Me.SpellReady(spell.RankName())()
end
-- Returns true if `name` is an AA that exists.
---@param name string
---@return boolean
function is_alt_ability(name)
return mq.TLO.AltAbility(name)() ~= nil
end
-- Returns true if `name` is an AA that you have purchased.
---@param name string
---@return boolean
function have_alt_ability(name)
return mq.TLO.Me.AltAbility(name)() ~= nil
end
-- Returns rank of AA `name`, or 0.
---@param name string
---@return integer
function alt_ability_rank(name)
return toint(mq.TLO.Me.AltAbility(name).Rank())
end
-- Returns true if the AA `name` is ready to use.
---@param name string
---@return boolean
function is_alt_ability_ready(name)
return mq.TLO.Me.AltAbilityReady(name)()
end
-- Returns true if I have the ability `name`.
---@param name string
---@return boolean
function have_ability(name)
return mq.TLO.Me.Ability(name)()
end
-- Returns true if the ability `name` is ready to use.
---@param name string
---@return boolean
function is_ability_ready(name)
return mq.TLO.Me.AbilityReady(name)()
end
-- Do we have combat ability `name`?
---@return boolean
function have_combat_ability(name)
return mq.TLO.Me.CombatAbility(name)() ~= nil
end
-- Returns true if the combat ability `name` is ready to use.
---@param name string
---@return boolean
function is_combat_ability_ready(name)
return mq.TLO.Me.CombatAbilityReady(name)()
end
---@param name string
function use_combat_ability(name)
-- /disc argument MUST NOT use quotes
mq.cmdf("/disc %s", name)
end
---@param name string
function use_ability(name)
-- /doability argument MUST use quotes
mq.cmdf('/doability "%s"', name)
end
-- Performs alt ability `name` unconditionally, including retrying and waiting until cast is completed before returning
---@param name string
---@param spawnID integer|nil
function use_alt_ability(name, spawnID)
if not have_alt_ability(name) then
all_tellf("\arERROR\ax: I do not have AA \ay%s\ax", name)
return
end
if not is_alt_ability_ready(name) then
all_tellf("\arERROR\ax: %s is not ready, ready in \ay%s\ax", name, mq.TLO.Me.AltAbilityTimer(name).TimeHMS())
return
end
if not is_standing() then
cmd("/stand")
end
if is_brd() then
local exe = string.format('/medley queue "%s"', name)
if spawnID ~= nil then
exe = exe .. string.format(" -targetid|%d", spawnID)
end
cmd(exe)
else
wait_until_not_casting()
local args = '"'..name..'" alt -maxtries|3'
if spawnID ~= nil then
args = args .. ' -targetid|'.. tostring(spawnID)
end
mq.cmdf("/casting %s", args)
end
mq.delay(500)
mq.delay(20000, function() return not is_casting() end)
mq.delay(500)
end
-- returns true if I have the buff `name` on me
---@param name string
---@return boolean
function have_buff(name)
local spell = mq.TLO.Spell(name)
if spell() == nil then
--log.Debug("have_buff: spell didnt resolve for \ay%s", name)
if not have_ability(name) and not have_item(name) and name ~= "Wondrous Rapidity" and name ~= "Wonderous Rapidity" then
log.Debug("have_buff: asked about unrecognized buff \ay%s", name)
end
return false
end
if mq.TLO.Me.Buff(name)() == name then
return true
end
return mq.TLO.Me.Buff(spell.RankName())() == name
end
-- returns true if I have the song `name` on me
---@param name string
---@return boolean
function have_song(name)
local spell = mq.TLO.Spell(name)
if spell() == nil then
if not have_ability(name) and not have_item(name) then
all_tellf("have_song ERROR, asked about odd buff %s", name)
end
return false
end
return mq.TLO.Me.Song(name)() ~= nil or mq.TLO.Me.Song(spell.RankName())() ~= nil
end
---@param name string
---@return boolean
function have_buff_or_song(name)
return have_buff(name) or have_song(name)
end
-- returns true if my pet have the buff `name` on me
---@param name string
---@return boolean
function pet_have_buff(name)
if name == nil then
-- XXX backtrace and debug, should not happen!
-- FIXME: sometimes trigger on MAG, don't know why yet
all_tellf("pet_have_buff: ERROR input is nil, should not happen!")
return false
end
local spell = mq.TLO.Spell(name)
if spell() == nil then
all_tellf("ERROR pet_have_buff odd buff %s", name)
return false
end
if mq.TLO.Me.Pet.Buff(name)() ~= nil then
return true
end
return mq.TLO.Me.Pet.Buff(spell.RankName())() == name
end
-- Am I casting a spell/song?
---@return boolean
function is_casting()
return mq.TLO.Me.Casting() ~= nil
end
-- Pauses up to 60 seconds until casting current spell is finished
function wait_until_not_casting()
mq.delay("60s", function() return is_brd() or not is_casting() end)
end
-- Am I moving?
---@return boolean
function is_moving()
return mq.TLO.Me.Moving()
end
-- Am I standing?
---@return boolean
function is_standing()
return mq.TLO.Me.Standing()
end
-- Am I sitting?
---@return boolean
function is_sitting()
return mq.TLO.Me.Sitting()
end
-- Returns true if I am meditating (sitting and not max mana/end)
---@return boolean
function is_meditating()
if mq.TLO.Me.MaxMana() > 0 and mq.TLO.Me.PctMana() == 100 then
return false
end
if mq.TLO.Me.MaxMana() == 0 and mq.TLO.Me.PctEndurance() == 100 then
return false
end
return mq.TLO.Me.Sitting()
end
-- Am I invisible?
---@return boolean
function is_invisible()
return mq.TLO.Me.Invis()
end
-- Am I stunned?
---@return boolean
function is_stunned()
return mq.TLO.Me.Stunned()
end
-- Am I feigning?
---@return boolean
function is_feigning()
return mq.TLO.Me.Feigning()
end
-- Returns my class shortname, eg "WAR".
---@return string
function class_shortname()
return mq.TLO.Me.Class.ShortName()
end
-- Returns my race shortname, eg "OGR".
---@return string
function race_shortname()
return map_race_shortname(mq.TLO.Me.Race.Name())
end
-- Returns the race shortname, like HUM for Human.
---@param race string name
---@return string
function map_race_shortname(race)
local map = {
["Barbarian"] = "BAR",
["Dark Elf"] = "DEF",
["Drakkin"] = "DRK",
["Dwarf"] = "DWF",
["Erudite"] = "ERU",
["Froglok"] = "FRG",
["Gnome"] = "GNM",
["Half Elf"] = "HEF",
["Halfling"] = "HFL",
["High Elf"] = "HIE",
["Human"] = "HUM",
["Iksar"] = "IKS",
["Ogre"] = "OGR",
["Troll"] = "TRL",
["Vah Shir"] = "VAH",
["Wood Elf"] = "ELF",
}
if map[race] == nil then
log.Debug("map_race_shortname unmapped %s (not a player race)", race)
return "UNKNOWN-RACE"
end
return map[race]
end
-- Am I a Bard?
---@return boolean
function is_brd()
return mq.TLO.Me.Class.ShortName() == "BRD"
end
-- Am I a Warrior?
---@return boolean
function is_war()
return mq.TLO.Me.Class.ShortName() == "WAR"
end
-- Am I a Paladin?
---@return boolean
function is_pal()
return mq.TLO.Me.Class.ShortName() == "PAL"
end
-- Am I a Shadowknight?
---@return boolean
function is_shd()
return mq.TLO.Me.Class.ShortName() == "SHD"
end
-- Am I a Rogue?
---@return boolean
function is_rog()
return mq.TLO.Me.Class.ShortName() == "ROG"
end
-- Am I a Magician?
---@return boolean
function is_mag()
return mq.TLO.Me.Class.ShortName() == "MAG"
end
-- Am I a Necromancer?
---@return boolean
function is_nec()
return mq.TLO.Me.Class.ShortName() == "NEC"
end
-- Am I a Cleric?
---@return boolean
function is_clr()
return mq.TLO.Me.Class.ShortName() == "CLR"
end
-- Am I a Druid?
---@return boolean
function is_dru()
return mq.TLO.Me.Class.ShortName() == "DRU"
end
-- Am I a Shaman?
---@return boolean
function is_shm()
return mq.TLO.Me.Class.ShortName() == "SHM"
end
-- Am I a Ranger?
---@return boolean
function is_rng()
return mq.TLO.Me.Class.ShortName() == "RNG"
end
-- Am I a Wizard?
---@return boolean
function is_wiz()
return mq.TLO.Me.Class.ShortName() == "WIZ"
end
-- Am I a Enchanter?
---@return boolean
function is_enc()
return mq.TLO.Me.Class.ShortName() == "ENC"
end
-- Am I a Monk?
---@return boolean
function is_mnk()
return mq.TLO.Me.Class.ShortName() == "MNK"
end
-- Am I a Beastlord?
---@return boolean
function is_bst()
return mq.TLO.Me.Class.ShortName() == "BST"
end
-- Am I in a guild?
---@return boolean
function in_guild()
return mq.TLO.Me.Guild() ~= nil
end
-- Am I in a raid?
---@return boolean
function in_raid()
return mq.TLO.Raid.Members() > 0
end
-- Am I in a group?
---@return boolean
function in_group()
return mq.TLO.Group.Members() > 0
end
-- Am I group leader?
---@return boolean
function is_group_leader()
return mq.TLO.Group.Members() > 1 and mq.TLO.Group.Leader.Name() == mq.TLO.Me.Name()
end
-- Am I raid leader?
---@return boolean
function is_raid_leader()
return mq.TLO.Raid.Members() > 1 and mq.TLO.Raid.Leader.Name() == mq.TLO.Me.Name()
end
--- Am I grouped with PC `name`?
---@return boolean
function is_grouped_with(name)
if not in_group() then
return false
end
return name == mq.TLO.Me.Name() or mq.TLO.Group.Member(name).ID() ~= nil
end
--- Am I in raid with PC `name`?
---@return boolean
function is_raided_with(name)
if not in_raid() then
return false
end
return name == mq.TLO.Me.Name() or mq.TLO.Raid.Member(name).ID() ~= nil
end
-- Am I hovering? (just died, waiting for rez in the same zone)
---@return boolean
function is_hovering()
return window_open("RespawnWnd")
end
function is_gm()
return mq.TLO.Me.GM()
end
-- Am I in combat?
---@return boolean
function in_combat()
local xdist = mq.TLO.Me.XTarget(1).Distance()
return mq.TLO.Me.Combat()
or mq.TLO.Me.CombatState() == "COMBAT"
or mq.TLO.Me.PctAggro() > 0
or (xdist ~= nil and xdist < 200)
end
-- Is window `name` open?
---@param name string
---@return boolean
function window_open(name)
return mq.TLO.Window(name).Open() == true
end
-- Close window by clicking on a button
---@param name string
function close_window(name)
if not window_open(name) then
return
end
mq.cmdf("/invoke ${Window[%s].DoClose}", name)
end
-- Returns true if any obstructive window is open (loot, trade, bank, merchant, spell book)
---@return boolean
function obstructive_window_open()
return mq.TLO.Corpse.Open() or window_open("MerchantWnd") or window_open("GiveWnd") or window_open("BankWnd") or window_open("BigBankWnd")
or window_open("SpellBookWnd") or window_open("LootWnd") or window_open("tradewnd")
or window_open("ConfirmationDialogBox")
end
-- Opens merchant window with `spawn`.
---@param spawn spawn
function open_merchant_window(spawn)
if spawn == nil or spawn() == nil then
return
end
if window_open("MerchantWnd") then
all_tellf("WARNING: A merchant window was already open. Closing it")
close_window("MerchantWnd")
mq.delay(1000)
end
if spawn.Distance() > 20 then
log.Error("Too far away from merchant. Giving up")
return
end
mq.cmdf("/target id %d", spawn.ID())
local attempt = 1
while true do
if attempt >= 3 then
all_tellf("ERROR: Giving up opening merchant window after %d attempts", attempt)
break
end
if window_open("MerchantWnd") then