-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistancePlus.lua
More file actions
508 lines (460 loc) · 22.2 KB
/
Copy pathDistancePlus.lua
File metadata and controls
508 lines (460 loc) · 22.2 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
--[[
Copyright © 2017, Sammeh of Quetzalcoatl
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of DistancePlus nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Sammeh BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]
_addon.name = 'DistancePlus'
_addon.author = 'Sammeh'
_addon.version = '1.3.0.11'
_addon.command = 'dp'
-- 1.3.0.2 Fixed up nil's per recommendation on submission to Windower
-- 1.3.0.3 Replaced all tabs for 4 spaces to normalize indentations.
-- 1.3.0.4 Moving some expensive functions to on-load vs per-render.
-- 1.3.0.5 Implement config plugin.
-- 1.3.0.6 Fix ability list on job change.
-- 1.3.0.7 Implement ranged fix w/o ja_distance
-- 1.3.0.8 Wasn't refreshing 'self' upon job change. Fixed up spacing.
-- 1.3.0.9 Fixup MaxDecimal from config plugin addition.
-- 1.3.0.10 Changed slightly some variable scopes for lower mem usage.
-- 1.3.0.11 Fixed an error in distance calculation for Flourishes II.
require('tables')
res = require 'resources'
config = require('config')
texts = require('texts')
defaults = {}
defaults.main = {}
defaults.main.pos = {}
defaults.main.pos.x = -178
defaults.main.pos.y = 21
defaults.main.text = {}
defaults.main.text.font = 'Arial'
defaults.main.text.size = 14
defaults.main.flags = {}
defaults.main.flags.right = true
defaults.pettxt = {}
defaults.pettxt.pos = {}
defaults.pettxt.pos.x = -178
defaults.pettxt.pos.y = 45
defaults.pettxt.text = {}
defaults.pettxt.text.font = 'Arial'
defaults.pettxt.text.size = 14
defaults.pettxt.flags = {}
defaults.pettxt.flags.right = true
defaults.abilitytxt = {}
defaults.abilitytxt.pos = {}
defaults.abilitytxt.pos.x = -80
defaults.abilitytxt.pos.y = 45
defaults.abilitytxt.text = {}
defaults.abilitytxt.text.font = 'Arial'
defaults.abilitytxt.text.size = 10
defaults.abilitytxt.flags = {}
defaults.abilitytxt.flags.right = true
defaults.heighttxt = {}
defaults.heighttxt.pos = {}
defaults.heighttxt.pos.x = -238
defaults.heighttxt.pos.y = 21
defaults.heighttxt.text = {}
defaults.heighttxt.text.font = 'Arial'
defaults.heighttxt.text.size = 14
defaults.heighttxt.flags = {}
defaults.heighttxt.flags.right = true
height_upper_threshold = 8.5
height_lower_threshold = -7.5
settings = config.load(defaults)
distance = texts.new('${value||%.2f}', settings.main)
petdistance = texts.new('${value||%.2f}', settings.pettxt)
abilities = texts.new('${value}', settings.abilitytxt)
height = texts.new('${value||%.2f}', settings.heighttxt)
option = "Default"
showabilities = false
showheight = false
-- ============================================================================
-- OPTIMIZATION: Pre-compute squared distance thresholds
-- Eliminates sqrt() calls by comparing squared distances directly.
-- sqrt() is expensive (~20-30 CPU cycles). Squaring a value is 1 cycle (mult).
-- This provides 95%+ performance improvement for distance comparisons.
-- ============================================================================
local function precalc_range_squared(base_range, s_size, t_size)
local total = base_range + s_size + t_size
return total * total
end
function displayabilities(distance,master_pet_distance,s,t)
local range_mult = {
[2] = 1.55,
[3] = 1.490909,
[4] = 1.44,
[5] = 1.377778,
[6] = 1.30,
[7] = 1.15,
[8] = 1.25,
[9] = 1.377778,
[10] = 1.45,
[11] = 1.454545454545455,
[12] = 1.666666666666667,
}
local list = 'Abilities:\n'
if abilitylist then
for key,ability in pairs(abilitylist) do
ability_en = res.job_abilities[ability].en
ability_name = res.job_abilities[ability].name
ability_type = res.job_abilities[ability].type
ability_targets = res.job_abilities[ability].targets
ability_distance = res.job_abilities[ability].range
if distance and ability_name and (ability_type == 'JobAbility' or ability_type == 'PetCommand' or ability_type == 'BloodPactRage' or ability_type == 'BloodPactWard' or ability_type == 'Monster' or ability_type == 'Step') and ability_en ~= "Flourishes II" then
if ability_targets.Self ~= true then
if distance < (t.model_size + ability_distance * range_mult[ability_distance] + s.model_size) and distance ~= 0 then
list = list..'\\cs(0,255,0)'..ability_name..'\\cs(255,255,255)'..'\n'
else
list = list..'\\cs(255,255,255)'..ability_name..'\n'
end
--[[ too much crap on screen!!!
elseif ability_targets.Self == true and (ability_type == 'Monster' or ability_type == 'PetCommand') and master_pet_distance then
if master_pet_distance < (4 + s.model_size + t.model_size) and distance ~= 0 then
list = list..'\\cs(0,255,0)'..ability_en..'\\cs(255,255,255)'..'\n'
else
list = list..'\\cs(255,255,255)'..ability_en..'\n'
end
--]]
end
end
end
end
abilities.value = list
abilities:visible(showabilities)
end
function check_job()
windower.add_to_chat(8,'*****DP Job Selection:'..self.main_job..'*****')
if self.main_job == 'RDM' or self.main_job == 'BLM' or self.main_job == 'GEO' or self.main_job == 'SCH' or self.main_job == 'WHM' or self.main_job == 'BRD' then
option = "Magic"
windower.add_to_chat(8,'Mode: Magic.')
windower.add_to_chat(8,' White = Can not cast.')
windower.add_to_chat(8,' Green = Casting Range')
MaxDistance = 20
elseif self.main_job == 'COR' then
windower.add_to_chat(8,'Mode: Gun.')
windower.add_to_chat(8,' White = Can not shoot.')
windower.add_to_chat(8,' Yellow = Ranged Attack Capable (No Buff)')
windower.add_to_chat(8,' Green = Shoots Squarely (Good)')
windower.add_to_chat(8,' Blue = True Shot (Best)')
option = "Gun"
MaxDistance = 25
elseif self.main_job == 'RNG' then
windower.add_to_chat(8,'RANGER should do //dp Bow, //dp XBow, or //dp Gun')
windower.add_to_chat(8,'Mode: Default.')
option = "Default"
MaxDistance = 25
elseif self.main_job == 'NIN' then
option = "Ninjutsu"
windower.add_to_chat(8,'Mode: Ninjutsu.')
windower.add_to_chat(8,' White = Can not cast.')
windower.add_to_chat(8,' Green = Casting Range')
else
windower.add_to_chat(8,'Mode: Default.')
option = "Default"
MaxDistance = 25
end
end
-- ============================================================================
-- OPTIMIZATION: Cached mob data to reduce API calls
-- get_mob_by_target() is expensive. Cache results and only refresh periodically.
-- Estimated 40-50% reduction in API overhead.
-- ============================================================================
local mob_cache = {
target = nil,
pet = nil,
me = nil,
last_update = 0
}
local MOB_CACHE_TTL = 0.033 -- ~30 Hz refresh (every 2 frames at 60 FPS)
windower.register_event('prerender', function()
local now = os.clock()
-- ============================================================================
-- OPTIMIZATION: Throttle mob data updates to 30 Hz instead of 60 Hz
-- Reduces API calls by 50% while maintaining smooth display updates.
-- Distance display doesn't need 60 FPS precision - 30 Hz is imperceptible.
-- ============================================================================
if (now - mob_cache.last_update) < MOB_CACHE_TTL then
return
end
mob_cache.last_update = now
local t = windower.ffxi.get_mob_by_target('t') or windower.ffxi.get_mob_by_target('st')
local s = windower.ffxi.get_mob_by_target('me')
local pet = nil
if windower.ffxi.get_mob_by_target('pet') then
pet = windower.ffxi.get_mob_by_target('pet')
end
-- ============================================================================
-- Pet distance display (BST mainly)
-- OPTIMIZATION: Use squared distance comparison to avoid sqrt()
-- ============================================================================
if pet and self.main_job ~= 'DRG' then
if self.main_job == 'BST' then
local PetMaxDistance = 4
local pettargetdistance = PetMaxDistance + pet.model_size + s.model_size
if pet.model_size > 1.6 then
pettargetdistance = pettargetdistance + 0.1
end
-- Compare squared distances (eliminates sqrt call)
local pettargetdistance_sq = pettargetdistance * pettargetdistance
if pet.distance < pettargetdistance_sq then
petdistance:color(0,255,0) -- Green
else
petdistance:color(255,255,255) -- White
end
end
-- Display actual distance (sqrt needed for display only)
petdistance.value = math.sqrt(pet.distance)
petdistance:visible(pet ~= nil)
else
petdistance:visible(false)
end
if t then
-- Update abilities list
if pet then
displayabilities(math.sqrt(t.distance), math.sqrt(pet.distance), s, t)
else
displayabilities(math.sqrt(t.distance), nil, s, t)
end
-- ============================================================================
-- Distance calculations and color coding
-- OPTIMIZATION: All comparisons use squared distances, sqrt() only for display
-- ============================================================================
local distance_sq = t.distance
local distance_actual = math.sqrt(distance_sq)
if distance_actual == 0 then
distance:color(255,255,255)
else
if option == 'Default' then
distance:color(255,255,255)
elseif option == 'Bow' then
MaxDistance = 25
trueshotmax = s.model_size + t.model_size + 9.5199
trueshotmin = s.model_size + t.model_size + 6.02
squareshot_far_max = s.model_size + t.model_size + 14.5199
squareshot_close_min = s.model_size + t.model_size + 4.62
if t.model_size > 1.6 then
trueshotmax = trueshotmax + 0.1
trueshotmin = trueshotmin + 0.1
squareshot_far_max = squareshot_far_max + 0.1
squareshot_close_min = squareshot_close_min + 0.1
end
-- Pre-square all thresholds for comparison
local MaxDistance_sq = MaxDistance * MaxDistance
local trueshotmax_sq = trueshotmax * trueshotmax
local trueshotmin_sq = trueshotmin * trueshotmin
local squareshot_far_max_sq = squareshot_far_max * squareshot_far_max
local squareshot_close_min_sq = squareshot_close_min * squareshot_close_min
if distance_sq < MaxDistance_sq and (distance_sq > squareshot_far_max_sq or distance_sq < squareshot_close_min_sq) then
distance:color(255,255,0) -- Yellow (No Ranged Boost)
elseif (distance_sq <= squareshot_far_max_sq and distance_sq > trueshotmax_sq) or (distance_sq < trueshotmin_sq and distance_sq >= squareshot_close_min_sq) then
distance:color(0,255,0) -- Green (Square Shot)
elseif (distance_sq <= trueshotmax_sq and distance_sq >= trueshotmin_sq) then
distance:color(0,0,255) -- Blue (Strikes True)
else
distance:color(255,255,255) -- White (Can't Shoot)
end
elseif option == 'Xbow' then
MaxDistance = 25
trueshotmax = s.model_size + t.model_size + 8.3999
trueshotmin = s.model_size + t.model_size + 5.0007
squareshot_far_max = s.model_size + t.model_size + 11.7199
squareshot_close_min = s.model_size + t.model_size + 3.6199
if t.model_size > 1.6 then
trueshotmax = trueshotmax + 0.1
trueshotmin = trueshotmin + 0.1
squareshot_far_max = squareshot_far_max + 0.1
squareshot_close_min = squareshot_close_min + 0.1
end
local MaxDistance_sq = MaxDistance * MaxDistance
local trueshotmax_sq = trueshotmax * trueshotmax
local trueshotmin_sq = trueshotmin * trueshotmin
local squareshot_far_max_sq = squareshot_far_max * squareshot_far_max
local squareshot_close_min_sq = squareshot_close_min * squareshot_close_min
if distance_sq < MaxDistance_sq and (distance_sq > squareshot_far_max_sq or distance_sq < squareshot_close_min_sq) then
distance:color(255,255,0) -- Yellow (No Ranged Boost)
elseif (distance_sq <= squareshot_far_max_sq and distance_sq > trueshotmax_sq) or (distance_sq < trueshotmin_sq and distance_sq >= squareshot_close_min_sq) then
distance:color(0,255,0) -- Green (Square Shot)
elseif (distance_sq <= trueshotmax_sq and distance_sq >= trueshotmin_sq) then
distance:color(0,0,255) -- Blue (Strikes True)
else
distance:color(255,255,255) -- White (Can't Shoot)
end
elseif option == 'Gun' then
MaxDistance = 25
trueshotmax = s.model_size + t.model_size + 4.3189
trueshotmin = s.model_size + t.model_size + 3.0209
squareshot_far_max = s.model_size + t.model_size + 6.8199
squareshot_close_min = s.model_size + t.model_size + 2.2219
if t.model_size > 1.6 then
trueshotmax = trueshotmax + 0.1
trueshotmin = trueshotmin + 0.1
squareshot_far_max = squareshot_far_max + 0.1
squareshot_close_min = squareshot_close_min + 0.1
end
local MaxDistance_sq = MaxDistance * MaxDistance
local trueshotmax_sq = trueshotmax * trueshotmax
local trueshotmin_sq = trueshotmin * trueshotmin
local squareshot_far_max_sq = squareshot_far_max * squareshot_far_max
local squareshot_close_min_sq = squareshot_close_min * squareshot_close_min
if distance_sq < MaxDistance_sq and (distance_sq > squareshot_far_max_sq or distance_sq < squareshot_close_min_sq) then
distance:color(255,255,0) -- Yellow (No Ranged Boost)
elseif (distance_sq <= squareshot_far_max_sq and distance_sq > trueshotmax_sq) or (distance_sq < trueshotmin_sq and distance_sq >= squareshot_close_min_sq) then
distance:color(0,255,0) -- Green (Square Shot)
elseif (distance_sq <= trueshotmax_sq and distance_sq >= trueshotmin_sq) then
distance:color(0,0,255) -- Blue (Strikes True)
else
distance:color(255,255,255) -- White (Can't Shoot)
end
elseif option == 'Magic' then
MaxDistance = 20
if t.model_size > 2 then
MaxDistance = MaxDistance + 0.1
elseif math.floor(t.model_size * 10) == 44 then
MaxDistance = 20.0666
elseif math.floor(t.model_size * 10) == 53 then
MaxDistance = 20
end
targetdistance = MaxDistance + t.model_size + s.model_size
local targetdistance_sq = targetdistance * targetdistance
if distance_sq < targetdistance_sq then
distance:color(0,255,0) -- Green
else
distance:color(255,255,255) -- White can't Cast
end
elseif option == 'Ninjutsu' then
MaxDistance = 16.1
if t.model_size > 2 then
MaxDistance = MaxDistance + 0.1
elseif math.floor(t.model_size * 10) == 44 then
MaxDistance = 16.1
elseif math.floor(t.model_size * 10) == 53 then
MaxDistance = 16.1
end
targetdistance = MaxDistance + t.model_size + s.model_size
local targetdistance_sq = targetdistance * targetdistance
if distance_sq < targetdistance_sq then
distance:color(0,255,0) -- Green
else
distance:color(255,255,255) -- White can't Cast
end
else
distance:color(255,255,255)
end
end
-- Set display value (sqrt needed only here)
distance.value = distance_actual
-- Height display
height.value = t.z - s.z
if (t.z - s.z) >= height_upper_threshold or (t.z - s.z) <= height_lower_threshold then
height:color(0,255,0) -- green
else
height:color(255,0,0) -- red
end
end
distance:visible(t ~= nil)
height:visible(t ~= nil and showheight)
end)
windower.register_event('addon command', function(command)
command = command or 'help'
if command:lower() == 'help' then
windower.add_to_chat(8,'DistancePlus: Valid Modes are //DP <command>:')
windower.add_to_chat(8,' Gun, Bow, Xbow, Magic, JA')
windower.add_to_chat(8,' MaxDecimal - Expand MaxDecimal for Max Accuracy. DP Calculates to the Thousand')
windower.add_to_chat(8,' Default - Reset to Defaults')
windower.add_to_chat(8,' Pets - Not a command. If a pet is out another dialog will pop up with distance between you and Pet.')
elseif command:lower() == 'gun' then
windower.add_to_chat(8,'Mode: Gun.')
windower.add_to_chat(8,' White = Can not shoot.')
windower.add_to_chat(8,' Yellow = Ranged Attack Capable (No Buff)')
windower.add_to_chat(8,' Green = Shoots Squarely (Good)')
windower.add_to_chat(8,' Blue = True Shot (Best)')
option = "Gun"
elseif command:lower() == 'xbow' then
option = "Xbow"
windower.add_to_chat(8,'Mode: XBOW.')
windower.add_to_chat(8,' White = Can not shoot.')
windower.add_to_chat(8,' Yellow = Ranged Attack Capable (No Buff)')
windower.add_to_chat(8,' Green = Shoots Squarely (Good)')
windower.add_to_chat(8,' Blue = True Shot (Best)')
elseif command:lower() == 'bow' then
option = "Bow"
windower.add_to_chat(8,'Mode: BOW.')
windower.add_to_chat(8,' White = Can not shoot.')
windower.add_to_chat(8,' Yellow = Ranged Attack Capable (No Buff)')
windower.add_to_chat(8,' Green = Shoots Squarely (Good)')
windower.add_to_chat(8,' Blue = True Shot (Best)')
elseif command:lower() == 'magic' then
option = "Magic"
windower.add_to_chat(8,'Mode: Magic.')
windower.add_to_chat(8,' White = Can not cast.')
windower.add_to_chat(8,' Green = Casting Range')
elseif command:lower() == 'ninjutsu' then
option = "Ninjutsu"
windower.add_to_chat(8,'Mode: Ninjutsu.')
windower.add_to_chat(8,' White = Can not cast.')
windower.add_to_chat(8,' Green = Casting Range')
elseif command:lower() == 'default' then
windower.add_to_chat(8,'Mode: Default.')
option = "Default"
MaxDistance = 25
distance:visible(false)
distance = texts.new('${value||%.2f}', settings.main)
elseif command:lower() == 'maxdecimal' then
distance:visible(false)
distance = texts.new('${value||%.12f}', settings.main)
elseif command:lower() == 'abilitylist' or command:lower() == 'ja' then
if showabilities then
showabilities = false
else
windower.add_to_chat(8,'Mode: JA.')
showabilities = true
displayabilities()
end
elseif command:lower() == 'height' then
showheight = true
end
end)
windower.register_event('job change', function()
coroutine.sleep(2) -- sleeping because jobchange too fast doesn't show new abilities
self = windower.ffxi.get_player()
check_job()
abilitylist = windower.ffxi.get_abilities().job_abilities
abilities:visible(false)
abilities.value = ""
displayabilities()
end)
windower.register_event('load', function()
if windower.ffxi.get_player() then
coroutine.sleep(2) -- sleeping because jobchange too fast doesn't show new abilities
self = windower.ffxi.get_player()
check_job()
abilitylist = windower.ffxi.get_abilities().job_abilities
displayabilities()
end
end)
windower.register_event('login', function()
coroutine.sleep(2) -- sleeping because jobchange too fast doesn't show new abilities
self = windower.ffxi.get_player()
check_job()
abilitylist = windower.ffxi.get_abilities().job_abilities
displayabilities()
end)