-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrangebox.lua
More file actions
305 lines (265 loc) · 13.3 KB
/
Copy pathrangebox.lua
File metadata and controls
305 lines (265 loc) · 13.3 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
--[[
* rangebox.lua – RangeBox v1.1
*
* Ashita v4 addon for HorizonXI
*
* A floating, always-on-top, freely moveable and resizable window that shows
* the distance to your current target in large, easy-to-read colour-coded text.
*
* Target priority (automatic):
* Sub-Target active → shows Sub-Target distance (controller tagging)
* Sub-Target absent → shows Main Target distance
* No target at all → shows "No target"
*
* Colour coding (FFXI action ranges):
* Green ( ≤ 3.0 yalms) – melee range
* Cyan ( ≤ 15.0 yalms) – short spell / ability range
* Yellow ( ≤ 21.5 yalms) – standard magic / ranged range
* Red ( > 21.5 yalms) – out of typical action range
*
* The window persists until you explicitly hide it.
* Drag to move, grab the corner to resize (unless locked).
*
* Commands
* ─────────
* /rb toggle – show / hide the window
* /rb show – show
* /rb hide – hide
* /rb lock – toggle drag/resize lock
* /rb size <n> – font scale (0.5–8.0, default 3.0)
* /rb alpha <n> – background alpha (0.0–1.0, default 0.75)
* /rb reset – reset position, size, and scale to defaults
* /rb help – print this list
--]]
addon.name = 'rangebox';
addon.author = 'Schmeee';
addon.version = '1.2';
addon.desc = 'Floating range display – sub-target takes priority over main target. XYZ distance math.';
addon.link = '';
require('common');
local imgui = require('imgui');
local settings = require('settings');
-- ─────────────────────────────────────────────────────────────────────────────
-- SETTINGS
-- ─────────────────────────────────────────────────────────────────────────────
local default_settings = T{
visible = true,
locked = false,
font_scale = 3.0,
bg_alpha = 0.75,
pos_x = 800,
pos_y = 400,
win_w = 220,
win_h = 120,
};
local cfg_container = T{ settings = default_settings };
local config_file = settings.load(cfg_container);
local cfg = config_file.settings;
local function save_settings()
settings.save();
end
-- ─────────────────────────────────────────────────────────────────────────────
-- DISTANCE HELPER (XYZ math — works for players AND mobs)
-- ─────────────────────────────────────────────────────────────────────────────
-- GetDistance() returns squared distance and returns 0 for player entities.
-- We always use XYZ coordinate math instead:
-- dist = sqrt( (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 )
local function get_distance_yalms(idx)
if (idx == nil or idx == 0 or idx >= 2048) then return nil; end
local ent = AshitaCore:GetMemoryManager():GetEntity();
if (ent == nil) then return nil; end
local name = ent:GetName(idx);
if (name == nil or name == '') then return nil; end
-- Get local player entity for our own position
local player = GetPlayerEntity();
if (player == nil) then return nil; end
local pidx = player.TargetIndex;
-- XYZ positions
local ax = ent:GetLocalPositionX(pidx);
local ay = ent:GetLocalPositionY(pidx);
local az = ent:GetLocalPositionZ(pidx);
local bx = ent:GetLocalPositionX(idx);
local by = ent:GetLocalPositionY(idx);
local bz = ent:GetLocalPositionZ(idx);
if (ax == nil or ay == nil or az == nil or
bx == nil or by == nil or bz == nil) then
return nil;
end
local dx = bx - ax;
local dy = by - ay;
local dz = bz - az;
return math.sqrt(dx*dx + dy*dy + dz*dz);
end
local function get_name(idx)
if (idx == nil or idx == 0 or idx >= 2048) then return nil; end
local ent = AshitaCore:GetMemoryManager():GetEntity();
if (ent == nil) then return nil; end
local n = ent:GetName(idx);
if (n == nil or n == '') then return nil; end
return n;
end
-- ─────────────────────────────────────────────────────────────────────────────
-- TARGET RESOLUTION (sub-target takes priority)
-- ─────────────────────────────────────────────────────────────────────────────
-- Returns { idx, label, is_sub } for whichever target we should display,
-- or nil if there is no valid target.
local function resolve_display_target()
local tmgr = AshitaCore:GetMemoryManager():GetTarget();
if (tmgr == nil) then return nil; end
local raw = tmgr:GetRawStructure();
if (raw == nil or raw.Targets == nil) then return nil; end
-- Ashita raw.Targets is 0-indexed:
-- Targets[0] = main target
-- Targets[1] = sub-target
-- Check sub-target first (priority for controller tagging)
local sub = raw.Targets[1];
if (sub ~= nil and sub.Index ~= nil and sub.Index ~= 0 and sub.Index ~= 0xFFFF) then
local name = get_name(sub.Index);
if (name ~= nil) then
return { idx = sub.Index, label = 'Sub-Target', is_sub = true };
end
end
-- Fall back to main target
local main = raw.Targets[0];
if (main ~= nil and main.Index ~= nil and main.Index ~= 0 and main.Index ~= 0xFFFF) then
local name = get_name(main.Index);
if (name ~= nil) then
return { idx = main.Index, label = 'Target', is_sub = false };
end
end
return nil;
end
-- ─────────────────────────────────────────────────────────────────────────────
-- COLOUR CODING
-- ─────────────────────────────────────────────────────────────────────────────
local function range_color(dist)
if (dist <= 3.0) then return { 0.2, 1.0, 0.3, 1.0 }; end -- green – melee
if (dist <= 15.0) then return { 0.2, 0.9, 1.0, 1.0 }; end -- cyan – short range
if (dist <= 21.5) then return { 1.0, 1.0, 0.2, 1.0 }; end -- yellow – standard magic
return { 1.0, 0.25, 0.25, 1.0 }; -- red – out of range
end
-- ─────────────────────────────────────────────────────────────────────────────
-- RENDERING
-- ─────────────────────────────────────────────────────────────────────────────
ashita.events.register('d3d_present', 'rangebox_frame', function()
if (not cfg.visible) then return; end
local flags = bit.bor(
ImGuiWindowFlags_NoTitleBar,
ImGuiWindowFlags_NoFocusOnAppearing,
ImGuiWindowFlags_NoBringToFrontOnFocus
);
if (cfg.locked) then
flags = bit.bor(flags, ImGuiWindowFlags_NoMove, ImGuiWindowFlags_NoResize);
end
imgui.SetNextWindowPos({ cfg.pos_x, cfg.pos_y }, ImGuiCond_FirstUseEver);
imgui.SetNextWindowSize({ cfg.win_w, cfg.win_h }, ImGuiCond_FirstUseEver);
imgui.SetNextWindowBgAlpha(cfg.bg_alpha);
if (imgui.Begin('RangeBox##rangebox', nil, flags)) then
-- Persist window position/size across frames
local wx, wy = imgui.GetWindowPos();
local ww, wh = imgui.GetWindowSize();
cfg.pos_x = wx; cfg.pos_y = wy;
cfg.win_w = ww; cfg.win_h = wh;
local target = resolve_display_target();
if (target == nil) then
-- No target at all
imgui.TextColored({ 0.5, 0.5, 0.5, 0.6 }, 'No target');
else
local dist = get_distance_yalms(target.idx);
local tname = get_name(target.idx) or '?';
local color = (dist ~= nil) and range_color(dist) or { 0.5, 0.5, 0.5, 0.8 };
-- Small label: "Sub-Target" or "Target"
imgui.TextColored({ 0.65, 0.65, 0.65, 0.85 }, target.label);
-- Large distance number
imgui.SetWindowFontScale(cfg.font_scale);
if (dist ~= nil) then
imgui.TextColored(color, ('%.1f'):format(dist));
else
imgui.TextColored({ 0.5, 0.5, 0.5, 0.7 }, '---');
end
imgui.SetWindowFontScale(1.0);
-- "yalms" unit inline with the number
imgui.SameLine();
local nudge = (cfg.font_scale - 1.0) * 8;
imgui.SetCursorPosY(imgui.GetCursorPosY() + nudge);
imgui.TextColored({ 0.7, 0.7, 0.7, 0.8 }, 'yalms');
-- Target name below
local name_color = target.is_sub
and { 0.4, 1.0, 0.4, 0.95 } -- green tint for sub-target
or { 0.9, 0.9, 0.6, 0.90 }; -- warm white for main target
imgui.TextColored(name_color, tname);
-- [SUB] badge if applicable
if (target.is_sub) then
imgui.SameLine();
imgui.TextColored({ 0.3, 1.0, 0.3, 0.9 }, '[SUB]');
end
end
end
imgui.End();
end);
-- ─────────────────────────────────────────────────────────────────────────────
-- COMMANDS
-- ─────────────────────────────────────────────────────────────────────────────
ashita.events.register('command', 'rangebox_cmd', function(e)
local args = e.command:args();
if (#args == 0) then return; end
local cmd = args[1]:lower();
if (cmd ~= '/rb' and cmd ~= '/rangebox') then return; end
e.blocked = true;
local sub = (args[2] and args[2]:lower()) or 'help';
if (sub == 'toggle') then
cfg.visible = not cfg.visible;
print(('[RangeBox] %s.'):format(cfg.visible and 'Shown' or 'Hidden'));
save_settings();
elseif (sub == 'show') then
cfg.visible = true; print('[RangeBox] Shown.'); save_settings();
elseif (sub == 'hide') then
cfg.visible = false; print('[RangeBox] Hidden.'); save_settings();
elseif (sub == 'lock') then
cfg.locked = not cfg.locked;
print(('[RangeBox] Window %s.'):format(cfg.locked and 'locked' or 'unlocked'));
save_settings();
elseif (sub == 'size') then
local v = tonumber(args[3]);
if (v and v >= 0.5 and v <= 8.0) then
cfg.font_scale = v;
print(('[RangeBox] Font scale → %.1f'):format(v));
save_settings();
else
print('[RangeBox] Usage: /rb size <0.5–8.0>');
end
elseif (sub == 'alpha') then
local v = tonumber(args[3]);
if (v and v >= 0 and v <= 1) then
cfg.bg_alpha = v;
print(('[RangeBox] Alpha → %.2f'):format(v));
save_settings();
else
print('[RangeBox] Usage: /rb alpha <0.0–1.0>');
end
elseif (sub == 'reset') then
cfg.pos_x = 800; cfg.pos_y = 400;
cfg.win_w = 220; cfg.win_h = 120;
cfg.font_scale = 3.0; cfg.bg_alpha = 0.75;
print('[RangeBox] Reset to defaults.'); save_settings();
else
print('[RangeBox] v1.2 — commands:');
print(' /rb toggle / show / hide');
print(' /rb lock – toggle drag/resize lock');
print(' /rb size <0.5–8.0> – font scale (default 3.0)');
print(' /rb alpha <0.0–1.0> – background transparency');
print(' /rb reset – reset position/size/scale');
end
end);
-- ─────────────────────────────────────────────────────────────────────────────
-- LOAD / UNLOAD
-- ─────────────────────────────────────────────────────────────────────────────
ashita.events.register('load', 'rangebox_load', function()
print('[RangeBox] v1.2 loaded. /rb help for commands.');
print('[RangeBox] Sub-target takes priority. XYZ distance math (works for players + mobs).');
print('[RangeBox] Red = >21.5 yalms (out of action range).');
end);
ashita.events.register('unload', 'rangebox_unload', function()
save_settings();
print('[RangeBox] Unloaded.');
end);