forked from NathanSnail/wand_eval_tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.lua
More file actions
463 lines (437 loc) · 13 KB
/
Copy pathrenderer.lua
File metadata and controls
463 lines (437 loc) · 13 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
---@class renderer
local M = {}
---@param node node
---@param engine_data fake_engine
---@return boolean
local function make_text(node, engine_data)
if engine_data.nodes_to_shot_ref[node] then
return false
end
local build = (node.count or 1) .. " " .. node.name .. " ["
for _, v in ipairs(node.children) do
local res = make_text(v, engine_data)
if res == false then
return false
end
build = build .. res
end
return build .. "]"
end
---@param node node
---@param engine_data fake_engine
local function fold(node, engine_data)
local raw = require("data")
local equal = true
if engine_data.nodes_to_shot_ref[node] then
for k, v in pairs(engine_data.nodes_to_shot_ref[node].state) do
if raw[k] ~= v and not ({ action_draw_many_count = true, reload_time = true })[k] then
equal = false
end
end
if equal then
local num = engine_data.shot_refs_to_nums[engine_data.nodes_to_shot_ref[node]]
for k, v in pairs(engine_data.shot_refs_to_nums) do
if num.real < v.real then
engine_data.shot_refs_to_nums[k].disp = engine_data.shot_refs_to_nums[k].disp - 1
end
end
engine_data.nodes_to_shot_ref[node] = nil
end
end
node.count = 1
local i = 1
---@type string | false
local last = ""
local cur_c = 1
local index_set = {}
while i <= #node.children do
local v = node.children[i]
fold(v, engine_data)
local cur = make_text(v, engine_data)
if last == cur and cur ~= false then
index_set[node.children[i].index] = true
cur_c = cur_c + 1
table.remove(node.children, i)
else
last = cur
if i ~= 1 then
if node.children[i - 1].index ~= nil then
index_set[node.children[i - 1].index] = true
end
local indexes = {}
for k, _ in pairs(index_set) do
table.insert(indexes, k)
end
index_set = {}
node.children[i - 1].count = cur_c
node.children[i - 1].index = indexes
cur_c = 1
end
i = i + 1
end
end
if i ~= 1 then
if node.children[i - 1].index then
index_set[node.children[i - 1].index] = true
end
local indexes = {}
for k, _ in pairs(index_set) do
table.insert(indexes, k)
end
index_set = {}
node.children[i - 1].count = cur_c
node.children[i - 1].index = indexes
cur_c = 1
end
end
---@param node node
---@param val integer
local function pre_multiply(node, val)
node.count = (node.count or 1) * val
for _, v in ipairs(node.children) do
pre_multiply(v, node.count)
end
end
---@param str string
---@return integer
local function len(str)
return str:gsub("[\128-\191]", ""):len()
end
---@class (exact) node
---@field name string
---@field children node[]
---@field count integer?
---@field extra string?
---@field index (integer|integer[])?
---@class (exact) bar
---@field start integer
---@field finish integer
---@field right_shift integer
---@field value integer
---@class (exact) incomplete_render
---@field tree_semi_rendered string
---@field bars bar[]
---@param incomplete_render incomplete_render
---@param engine_data fake_engine
---@param text_formatter text_formatter
---@return incomplete_render
local function post_multiply(incomplete_render, engine_data, text_formatter)
local bars = incomplete_render.bars
local bar_idx = 1
local out_sp = {}
for str in incomplete_render.tree_semi_rendered:gmatch("([^\n]+)") do
table.insert(out_sp, str)
end
for k, str in ipairs(out_sp) do
local colourless = str:gsub(string.char(27) .. ".-m", "")
if bars[bar_idx].finish < k then
bar_idx = bar_idx + 1
end
bars[bar_idx].right_shift = math.max(bars[bar_idx].right_shift, len(colourless))
end
bar_idx = 1
for line_num, line_text in ipairs(out_sp) do
local colourless = line_text:gsub(string.char(27) .. ".-m", "")
if bars[bar_idx].finish < line_num then
bar_idx = bar_idx + 1
end
local cur_bar = bars[bar_idx]
local extra = (" "):rep(cur_bar.right_shift - len(colourless) + 1)
if cur_bar.start == cur_bar.finish then
extra = extra .. "]"
elseif cur_bar.start == line_num then
extra = extra .. "┐"
elseif cur_bar.finish == line_num then
extra = extra .. "┘"
else
extra = extra .. "│"
end
if math.floor((cur_bar.start + cur_bar.finish) / 2) == line_num then
extra = extra
.. " "
.. text_formatter.colour_codes.RESET
.. cur_bar.value
.. text_formatter.colour_codes.GREY
end
if cur_bar.value ~= 1 then
out_sp[line_num] = out_sp[line_num] .. extra
end
if engine_data.lines_to_shot_nums[line_num] then
out_sp[line_num] = out_sp[line_num]
.. " @ "
.. text_formatter.colour_codes.RESET
.. engine_data.lines_to_shot_nums[line_num]
.. text_formatter.colour_codes.GREY
end
end
local out = table.concat(out_sp, "\n")
return { tree_semi_rendered = out, bars = bars }
end
---@param node node
---@param prefix string
---@param no_extra boolean
---@param indent_level integer
---@param engine_data fake_engine
---@param text_formatter text_formatter
---@param incomplete_render incomplete_render
---@param options options
local function handle(node, prefix, no_extra, indent_level, engine_data, text_formatter, incomplete_render, options)
indent_level = indent_level or 0
local t_prefix = ""
for k = 1, prefix:len() do
local v = prefix:sub(k, k)
if v == "#" then
t_prefix = t_prefix .. (k == prefix:len() and (no_extra and "└" or "├") or "│")
else
t_prefix = t_prefix .. " "
end
end
incomplete_render.tree_semi_rendered = incomplete_render.tree_semi_rendered
.. t_prefix
.. text_formatter.id_text(node.name, engine_data.translations)
.. (node.extra and (" " .. text_formatter.colour_codes.RESET .. node.extra) or "")
.. text_formatter.colour_codes.GREY
.. "\n"
if engine_data.nodes_to_shot_ref[node] then
local _, c = incomplete_render.tree_semi_rendered:gsub("\n", "\n")
local cur_line = engine_data.shot_refs_to_nums[engine_data.nodes_to_shot_ref[node]].disp
engine_data.lines_to_shot_nums[c] = cur_line
end
local last_bar = incomplete_render.bars[#incomplete_render.bars]
if last_bar.right_shift <= indent_level and last_bar.value == node.count then
last_bar.finish = last_bar.finish + 1
else
local new_bar = {
start = last_bar.finish + 1,
finish = last_bar.finish + 1,
right_shift = indent_level,
value = node.count,
}
table.insert(incomplete_render.bars, new_bar)
end
for k, v in ipairs(node.children) do
local dont = k == #node.children
if no_extra then
prefix = prefix:sub(1, prefix:len() - 1) .. " "
end
handle(v, prefix .. "#", dont, indent_level + 1, engine_data, text_formatter, incomplete_render, options)
end
end
---@param src node
---@param indent string?
---@return string
local function render_json(src, indent)
indent = indent or ""
indent = indent .. "\t"
---@cast src node
local s = "{\n"
s = s .. indent .. '"name": "' .. src.name .. '",\n'
src.count = src.count or 1
s = s .. indent .. '"count": ' .. src.count .. ",\n"
src.extra = src.extra or ""
s = s .. indent .. '"extra": "' .. src.extra .. '",\n'
src.index = src.index or {}
local idx = src.index
if type(idx) == "number" then
src.index = { idx }
end
---@diagnostic disable-next-line: param-type-mismatch
local idx_str = table.concat(src.index, ", ")
s = s .. indent .. '"index": [' .. idx_str .. "],\n"
s = s .. indent .. '"children": [' .. (#src.children ~= 0 and "\n" or "")
for k, v in ipairs(src.children) do
s = s .. indent .. "\t" .. render_json(v, indent .. "\t")
if k ~= #src.children then
s = s .. ","
end
s = s .. "\n"
end
s = s .. (#src.children ~= 0 and indent or "") .. "]\n"
s = s .. indent:sub(2) .. "}"
return s
end
---@param calls node
---@param engine_data fake_engine
---@param text_formatter text_formatter
---@param options options
---@return string
function M.render(calls, engine_data, text_formatter, options)
if options.fold then
fold(calls, engine_data)
end
if options.json then
return render_json(calls)
end
pre_multiply(calls, 1)
local render = { tree_semi_rendered = "", bars = { { start = 1, finish = 0, right_shift = 0, value = 1 } } }
if options.tree then
handle(calls, "", false, 0, engine_data, text_formatter, render, options)
render = post_multiply(render, engine_data, text_formatter)
end
render.tree_semi_rendered = render.tree_semi_rendered
.. "\n"
.. (options.counts and M.render_counts(engine_data, text_formatter, options.ansi and not options.tree) or "")
.. (options.states and M.render_shot_states(engine_data, text_formatter) or "")
render.tree_semi_rendered = (options.ansi and "```ansi\n" or "")
.. render.tree_semi_rendered
.. (options.ansi and "\n```" or "")
return render.tree_semi_rendered
end
---@param engine_data fake_engine
---@param text_formatter text_formatter
---@param trailing_grey boolean
---@return string
function M.render_counts(engine_data, text_formatter, trailing_grey)
local count_pairs = {}
local big_length = 0
local big_length2 = 0
for k, v in pairs(engine_data.counts) do
table.insert(count_pairs, { k, tostring(v), v })
big_length = math.max(big_length, len(engine_data.translations[k] or k))
big_length2 = math.max(big_length2, tostring(v):len())
end
table.sort(count_pairs, function(a, b)
if a[3] ~= b[3] then
return a[3] > b[3]
end
local res = text_formatter.colour_compare(a[1], b[1])
if res ~= nil then
return res
end
return a[1] > a[1]
end)
local count_message = (trailing_grey and text_formatter.colour_codes.GREY or "") .. "┌" .. ("─"):rep(big_length + 2) .. "┬" .. ("─"):rep(big_length2 + 2) .. "┐\n"
for _, v in ipairs(count_pairs) do
count_message = count_message
.. "│ "
.. text_formatter.id_text(v[1], engine_data.translations)
.. (" "):rep(big_length - len(engine_data.translations[v[1]] or v[1]) + 1)
.. text_formatter.colour_codes.GREY
.. "│ "
.. text_formatter.colour_codes.RESET
.. v[2]
.. text_formatter.colour_codes.GREY
.. (" "):rep(big_length2 - v[2]:len() + 1)
.. "│\n"
end
count_message = count_message
.. "└"
.. ("─"):rep(big_length + 2)
.. "┴"
.. ("─"):rep(big_length2 + 2)
.. "┘\n"
return count_message
end
---@param state {string: integer}
---@param first boolean
---@return {string: string}
local function gather_state_modifications(state, first)
local default = require("data")
local diff = {}
for k, v in pairs(state) do
if default[k] ~= v then
diff[k] = tostring(v)
end
end
diff.action_name = nil
diff.action_id = nil
diff.action_mana_drain = nil
diff.action_draw_many_count = nil
diff.action_type = nil
diff.action_recursive = nil
diff.reload_time = nil
if not first then
diff.fire_rate_wait = nil
end
---@param csv string?
---@return string[]
local function handle_xml_csv(csv)
if not csv then return {} end
---@type string[]
local mods = {}
for mod in csv:gmatch("([^,]+)") do
table.insert(mods, mod)
end
for k, mod in ipairs(mods) do
local suffix = mod:gmatch("/[^/]+%.xml")()
mods[k] = suffix:sub(2, suffix:len() - 4)
end
local counted = {}
for _, v in ipairs(mods) do
counted[v] = (counted[v] or 0) + 1
end
local numeric = {}
for k, v in pairs(counted) do
table.insert(numeric, k .. (v == 1 and "" or (" ×" .. tostring(v))))
end
return numeric
end
diff.extra_entities = table.concat(handle_xml_csv(diff.extra_entities), ", ")
if diff.extra_entities == "" then
diff.extra_entities = nil
end
diff.game_effect_entities = table.concat(handle_xml_csv(diff.game_effect_entities), ", ")
if diff.game_effect_entities == "" then
diff.game_effect_entities = nil
end
local t = {}
for k, v in pairs(diff) do
table.insert(t, { k, v })
end
table.sort(t, function(a, b)
return a[1] < b[1]
end)
return t
end
---@param engine_data fake_engine
---@param text_formatter text_formatter
---@return string
function M.render_shot_states(engine_data, text_formatter)
local shot_nums_to_refs = {}
for shot, num in pairs(engine_data.shot_refs_to_nums) do
shot_nums_to_refs[num.disp] = shot
end
local out = ""
for num, shot in ipairs(shot_nums_to_refs) do
local shot_table = text_formatter.colour_codes.RESET .. "Shot state " .. num .. ":\n"
local diff = gather_state_modifications(shot.state, num == 1)
local name_width = 0
local value_width = 0
for _, v in ipairs(diff) do
local key = v[1]
local value = v[2]
name_width = math.max(name_width, key:len())
value_width = math.max(value_width, tostring(value):len())
end
name_width = name_width + 2
value_width = value_width + 2
shot_table = shot_table
.. text_formatter.colour_codes.GREY
.. "┌"
.. ("─"):rep(name_width)
.. "┬"
.. ("─"):rep(value_width)
.. "┐\n"
for _, v in ipairs(diff) do
local key = v[1]
local value = v[2]
local v_str = tostring(value)
shot_table = shot_table
.. "│ "
.. text_formatter.colour_codes.RESET
.. key
.. text_formatter.colour_codes.GREY
.. (" "):rep(name_width - key:len() - 1)
.. "│ "
.. text_formatter.colour_codes.RESET
.. v_str
.. text_formatter.colour_codes.GREY
.. (" "):rep(value_width - len(v_str) - 1)
.. "│\n"
end
shot_table = shot_table .. "└" .. ("─"):rep(name_width) .. "┴" .. ("─"):rep(value_width) .. "┘\n"
out = out .. shot_table .. "\n"
end
return out
end
return M