-
Notifications
You must be signed in to change notification settings - Fork 0
/
danj_statviz.xml
469 lines (433 loc) · 16.2 KB
/
danj_statviz.xml
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
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Plugin "Danj_StatViz" written by Danj 2023-02-25 -->
<muclient>
<plugin name="Danj_StatViz" author="Danj" id="89201300979e4ceba2129e51" language="Lua" purpose="Displays the 6 basic stats on a spider graph" save_state="y" date_written="2023-02-25" requires="4.90" version="1.02">
</plugin>
<!-- Script -->
<script>
<![CDATA[
require "themed_miniwindows"
require "aardwolf_colors"
require "wrapped_captures"
-- Constants
-- Stats.
Stat_Names = {
"STR",
"INT",
"WIS",
"DEX",
"CON",
"LUK"
}
Stat_FullNames = {
"Strength",
"Intelligence",
"Wisdom",
"Dexterity",
"Constitution",
"Luck"
}
-- Angle values for each stat in degrees.
Stat_Axes = {
STR = 270, -- top
INT = 330,
WIS = 30,
DEX = 90, -- bottom
CON = 150,
LUK = 210
}
-- This function converts polar coordinates (r, phi) to Cartesian coordinates (x,y).
-- Note that phi is in degrees and runs clockwise from the horizontal axis.
-- https://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates
local function polar_to_cartesian(
r, -- the radius or distance from the pole
phi -- the angle or direction in degrees
)
local x, y
phi = math.rad(phi) -- Convert angle from degrees to radians
x = r * math.cos(phi) -- x = r cos phi
y = r * math.sin(phi) -- y = r sin phi
-- Rounding
if x == math.abs(x) then
x = math.floor(x + 0.5)
else
x = math.ceil(x - 0.5)
end
if y == math.abs(y) then
y = math.floor(y + 0.5)
else
y = math.ceil(y - 0.5)
end
return x, y
end
-- This function works out where the pole of our spider graph should be.
local function find_pole()
local pole_x, pole_y, fontheight
fontheight = WindowFontInfo( -- get the font height
My_Window.id,
"f", -- id of the font loaded earlier
1 -- this specifies we want to know height
)
pole_x = (My_Window.bodyright - My_Window.bodyleft) / 2 -- centre of the window's body
pole_y = ((My_Window.bodybottom - My_Window.bodytop) / 2) + fontheight -- don't forget to offset for the top label
pole_x = math.floor(pole_x + 0.5) -- don't forget to round, now we're rounding everything else!
pole_y = math.floor(pole_y + 0.5)
return pole_x, pole_y
end
-- This function draws the plot area of our spider graph.
local function draw_plot_area()
local pole_x, pole_y
local plotarea = {
top = 0,
bottom = 0,
left = 0,
right = 0
}
local fontheight, fontwidth, fontdescent
local axis_x, axis_y
local r, phi1, phi2, stat1, stat2
local gridfrom_x, gridfrom_y, gridto_x, gridto_y
local label_x, label_y
-- Axis calculations
pole_x, pole_y = find_pole() -- find the pole
fontheight = WindowFontInfo( -- get the font height
My_Window.id,
"f", -- id of the font loaded earlier
1 -- this specifies we want to know height
)
fontdescent = WindowFontInfo( -- get the font descent (pixels below baseline)
My_Window.id, -- my window's id
"f", -- id of the font loaded earlier
3 -- this specifies we want to know descent
)
plotarea.top = My_Window.bodytop + 20 + fontheight -- allow space for top axis label
plotarea.bottom = My_Window.bodybottom - 20 - fontheight -- allow space for bottom axis label
plotarea.left = My_Window.bodyleft + 20
plotarea.right = My_Window.bodyright - 20
if (plotarea.bottom - plotarea.top) < (plotarea.right - plotarea.left) then
Axis_Length = (plotarea.bottom - plotarea.top) / 2
else
Axis_Length = (plotarea.right - plotarea.left) / 2
end
-- Draw axes
for k, v in pairs(Stat_Axes) do
axis_x, axis_y = polar_to_cartesian(Axis_Length, v) -- get the x,y coordinates for the end of the axis
axis_x = axis_x + pole_x -- don't forget to offset from the pole
axis_y = axis_y + pole_y -- don't forget to offset from the pole
WindowLine(
My_Window.id, -- my window's id
pole_x, pole_y, -- draw from the pole
axis_x, axis_y, -- draw to the end of the axis
ColourNameToRGB("white"), -- white line
miniwin.pen_solid, -- solid
2 -- line width
)
end
-- Draw gridlines
for percent = 0.2,1.0,0.2 do
r = Axis_Length * percent -- gridlines at 20%, 40%, 60%, 80% and 100% of maximum
for i = 1,6 do
stat1 = Stat_Names[i]
if i == 6 then
stat2 = Stat_Names[1]
else
stat2 = Stat_Names[i+1]
end
phi1 = Stat_Axes[stat1]
phi2 = Stat_Axes[stat2]
gridfrom_x, gridfrom_y = polar_to_cartesian(r, phi1) -- start of gridline
gridto_x, gridto_y = polar_to_cartesian(r, phi2) -- end of gridline
gridfrom_x = gridfrom_x + pole_x -- don't forget to offset from the pole
gridto_x = gridto_x + pole_x
gridfrom_y = gridfrom_y + pole_y
gridto_y = gridto_y + pole_y
WindowLine(
My_Window.id,
gridfrom_x, gridfrom_y,
gridto_x, gridto_y,
ColourNameToRGB("grey"),
miniwin.pen_dot,
1
)
end
end
-- Label axes
for i = 1,6 do
label_x, label_y = polar_to_cartesian(Axis_Length, Stat_Axes[Stat_Names[i]]) -- this is where the end of the axis is, we want this to be bottom centre of our text rectangle
label_x = label_x + pole_x -- don't forget to offset from the pole
label_y = label_y + pole_y -- don't forget to offset from the pole
fontwidth = WindowTextWidth( -- width of label string
My_Window.id,
"f",
Stat_Names[i]
)
if Stat_Axes[Stat_Names[i]] == 90 or Stat_Axes[Stat_Names[i]] == 270 then
label_x = label_x - (fontwidth / 2)
if Stat_Axes[Stat_Names[i]] == 270 then
label_y = label_y - fontheight
end
elseif Stat_Axes[Stat_Names[i]] > 90 and Stat_Axes[Stat_Names[i]] < 270 then
label_x = label_x - fontwidth - fontdescent
label_y = label_y - (fontheight / 2)
elseif Stat_Axes[Stat_Names[i]] > 270 or Stat_Axes[Stat_Names[i]] < 90 then
label_x = label_x + fontdescent
label_y = label_y - (fontheight / 2)
end
WindowText(
My_Window.id, -- my window's id
"f", -- font loaded earlier
Stat_Names[i], -- label
label_x, label_y, -- top left of label
(label_x + fontwidth), (label_y + fontheight), -- bottom right of label
ColourNameToRGB("white"), -- colour of label
false -- not Unicode
)
end
end
-- This function plots our spider graph.
local function plot_stats()
local phi, r, x, y
local pole_x, pole_y
local polygon_points = ""
local suggest_sv_update = false
-- Check all stat values are sensible, reset them and print errors if they're not
for i = 1,6 do
if Stat_Values[Stat_Names[i]].current == nil then
Stat_Values[Stat_Names[i]].current = 0
ColourNote("orange", "", "statviz: current " .. Stat_Names[i] .. " had invalid value")
suggest_sv_update = true
end
if Stat_Values[Stat_Names[i]].maximum == nil then
Stat_Values[Stat_Names[i]].maximum = 400
ColourNote("orange", "", "statviz: maximum " .. Stat_Names[i] .. " had invalid value")
suggest_sv_update = true
end
end
if suggest_sv_update then
ColourNote("orange", "", "statviz: Try doing an sv update to re-capture your current and maximum stats.")
return
end
pole_x, pole_y = find_pole()
-- Calculations
for i = 1,6 do
phi = Stat_Axes[Stat_Names[i]]
r = Axis_Length * (Stat_Values[Stat_Names[i]].current / Stat_Values[Stat_Names[i]].maximum)
x, y = polar_to_cartesian(r, phi)
x = x + pole_x
y = y + pole_y
if polygon_points == "" then
polygon_points = x .. "," .. y
else
polygon_points = polygon_points .. "," .. x .. "," .. y
end
end
-- Draw our polygon
WindowPolygon(
My_Window.id,
polygon_points,
ColourNameToRGB("lime"), miniwin.pen_solid, 3, -- outline
ColourNameToRGB("darkgreen"), miniwin.brush_hatch_cross_diagonal, -- fill inside
true,
false
)
end
-- This function draws our window content.
local function my_draw()
My_Window:blank() -- clear the window
draw_plot_area() -- draw the plot area
plot_stats() -- plot the graph
end
-- This function creates the miniwindow.
local function build_window()
My_Window = ThemedBasicWindow(
"Danj_StatViz", -- window id
100, -- top
100, -- left
300, -- width
300, -- height
"Stats Visualization", -- title
"center", -- title alignment
true, -- it's a temporary window
1, -- resizer type 1 (2 is only for use with scrollbars and this window will never have a scrollbar)
my_draw, -- draw our window content while resizing
my_draw -- draw our window content after resizing
)
WindowFont( -- load a font, because we can't get text metrics without doing this
My_Window.id, -- my window's id
"f", -- font id
"Trebuchet MS", -- font name
8, -- font size (points)
false, -- no bold
false, -- no italics
false, -- no underline
false -- no strikethrough
)
my_draw() -- draw our window content
My_Window:dress_window() -- add the window dressing
end
-- This is the callback function for capturing train output.
local function train_capture_callback(
captured_styles,
start_line,
end_line
)
local train_output
local current, maximum, existingWindows
local windowExists = false
if captured_styles ~= nil then
train_output = utils.split(strip_colours_from_styles(captured_styles), "\n") -- strip colours and split the output into a table
else
ColourNote("orange", "", "statviz: Could not capture train output. Please ensure you are connected and logged in to Aardwolf MUD.")
return
end
for k, v in pairs(train_output) do
if string.find(v, ":") then -- we're only interested in lines with a colon
for i = 1, 6 do
if string.find(v, Stat_FullNames[i]) then
_, _, current, maximum = string.find(v, "(%d+)%s+(%d+)%*?$")
Stat_Values[Stat_Names[i]].current = current
Stat_Values[Stat_Names[i]].maximum = maximum
end
end
end
end
-- the existence of My_Window and My_Window.id are not sufficient to confirm the window actually exists
-- list all windows
existingWindows = WindowList()
if existingWindows ~= nil then
for _, v in ipairs(existingWindows) do
if string.find(v,"Danj_StatViz") then
windowExists = true
end
end
end -- if any
if windowExists then
my_draw() -- draw our window content
My_Window:dress_window() -- add the window dressing
end
end
-- This is the function to capture train output.
function Sv_update()
Capture.untagged_output(
"train",
true,
true,
true,
train_capture_callback,
nil,
train_capture_callback
)
end
-- This is the function to display the window and update stats.
function Sv_on()
build_window()
Sv_update()
end
-- This is the function to remove the window.
function Sv_off()
if My_Window and My_Window.id then
WindowDelete(My_Window.id)
end
end
-- This is the function to display help information.
function Sv_help()
ColourNote("orange", "", "Stats Visualization help")
ColourNote("orange", "", "========================")
ColourNote("orange", "", "sv on|off -- show or hide the stats visualization window")
ColourNote("orange", "", "sv update -- update the stats visualisation window with the latest data from \"train\" output")
ColourNote("orange", "", "sv help -- display help information")
end
-- On plugin install, display help information and set up aliases and variables.
function OnPluginInstall()
Sv_help()
AddAlias(
"sv_on",
"sv on",
"Sv_on()",
alias_flag.Enabled,
""
)
SetAliasOption(
"sv_on",
"send_to",
sendto.scriptafteromit
)
AddAlias(
"sv_off",
"sv off",
"Sv_off()",
alias_flag.Enabled,
""
)
SetAliasOption(
"sv_off",
"send_to",
sendto.scriptafteromit
)
AddAlias(
"sv_update",
"sv update",
"Sv_update()",
alias_flag.Enabled,
""
)
SetAliasOption(
"sv_update",
"send_to",
sendto.scriptafteromit
)
AddAlias(
"sv_help",
"sv help",
"Sv_help()",
alias_flag.Enabled,
""
)
SetAliasOption(
"sv_help",
"send_to",
sendto.scriptafteromit
)
-- Initial stat values
Stat_Values = {
STR = {
current = 0,
maximum = 400
},
INT = {
current = 0,
maximum = 400
},
WIS = {
current = 0,
maximum = 400
},
DEX = {
current = 0,
maximum = 400
},
CON = {
current = 0,
maximum = 400
},
LUK = {
current = 0,
maximum = 400
}
}
end
-- On plugin disable, remove the window and aliases
function OnPluginDisable()
Sv_off()
DeleteAlias("sv_on")
DeleteAlias("sv_off")
DeleteAlias("sv_update")
DeleteAlias("sv_help")
end
function OnPluginClose()
OnPluginDisable()
end
]]>
</script>
</muclient>