Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
1167 lines (1084 loc) · 39.8 KB
/
Copy pathmain.lua
File metadata and controls
1167 lines (1084 loc) · 39.8 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
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
-- Player Toolkit: Lua API 4 overlay panel using BSX.Lua.UI/5 theming, tabs,
-- searchable categorized item catalog, stats, and currency.
local function clamp(value, low, high)
if type(value) ~= "number" or value ~= value then return low end
if value < low then return low end
if value > high then return high end
return value
end
local function trim_utf8(text, max_bytes, ellipsis)
local s = tostring(text or ""):gsub("[%z\1-\31]", " ")
if #s <= max_bytes then return s end
local suf = ellipsis or "..."
local cap = math.max(1, max_bytes - #suf)
while cap > 1 do
local b = string.byte(s, cap + 1)
if not b or b < 0x80 or b >= 0xC0 then break end
cap = cap - 1
end
return string.sub(s, 1, cap) .. suf
end
local function clamp_text(text)
return trim_utf8(text, 127, "...")
end
local function pack_combo_items(list, fallback)
local out = {}
local bytes = 0
for i = 1, #list do
local s = trim_utf8(list[i], 28, "...")
if #s > 0 and (bytes + #s + 1) <= 250 then
out[#out + 1] = s
bytes = bytes + #s + 1
end
if #out >= 8 then break end
end
if #out == 0 then
out[1] = fallback or "(empty)"
end
return out
end
local PAGE_SIZE = 200
local MAX_LISTBOX_ITEMS = 2000
local MAX_LISTBOX_BYTES = 115000
local function listbox_entry_bytes(entry)
local lbl = type(entry) == "table" and tostring(entry.label or "") or tostring(entry or "")
local cat = type(entry) == "table" and tostring(entry.category or "") or ""
if #cat > 0 then
return #cat + 1 + #lbl + 1
end
return #lbl + 1
end
local refresh_seconds = clamp(config.float("PlayerToolkit", "RefreshSeconds", 0.5), 0.25, 10.0)
local item_row = math.floor(clamp(config.int("PlayerToolkit", "DefaultItemRow", 2075), 0, 65534))
local item_qty = math.floor(clamp(config.int("PlayerToolkit", "DefaultQty", 1), 1, 100))
local STAT_IDS = { 0, 25, 26 }
local STAT_DEFAULTS = { "HP", "Stamina", "MP" }
local STAT_LABELS = { "HP [id:0]", "Stamina [id:25]", "MP [id:26]" }
local COPPER_ROW = 2075
local stat_idx = 1
local world_ready = false
local logged_panel = false
local logged_ready = false
local stat_names_loaded = false
local cat_name_cache = {}
local item_meta_cache = {}
local view_mode = "catalog"
local catalog_started = false
local catalog_indexing = false
local catalog_use_fast_search = true
local catalog_cursor = 0
local catalog_count = 0
local catalog_max_row = -1
local catalog_rows = {}
local catalog_keys = {}
local catalog_labels = {}
local catalog_cats = {}
local catalog_hay = {}
local catalog_seen_rows = {}
local catalog_row_to_idx = {}
local catalog_cat_counts = {}
local catalog_last_ui_count = 0
local bag_count = 0
local bag_rows = {}
local bag_keys = {}
local bag_labels = {}
local bag_cats = {}
local bag_hay = {}
local bag_buckets = {}
local bag_indices = {}
local bag_cat_counts = {}
local search_query = ""
local search_tokens = {}
local search_exact_num = nil
local selected_category = nil
local active_category_list = {}
local current_page = 1
local filtered_indices = nil
local filtered_count = 0
local other_cat_hits = 0
local picker_entries = {
{ row = COPPER_ROW, label = "[Currency] 2075: Money_Copper" },
}
local selected_bag_row = nil
local selected_bag_bucket = nil
local selected_bag_index = nil
local last_summary = {
world_ready = false,
stat_id = STAT_IDS[stat_idx],
item_row = item_row,
item_qty = item_qty,
copper = nil,
}
local TOOLKIT_THEME = {
accent = "#C84B52",
header = "#3E1B20",
child_bg = "#141318",
frame_bg = "#201E27",
border = "#5A333D",
text = "#F2EFF6",
}
local panel = bsx.ui.panel("Player Toolkit")
panel:set_theme(TOOLKIT_THEME)
local status_id
local stats_readout_id
local hp_bar_id
local stat_combo_id
local copper_id
local cat_status_id
local cat_listbox_id
local page_info_id
local catalog_listbox_id
local search_input_id
local row_input_id
local item_info_id
local qty_slider_id
local function format_stat(st)
if not st then return "--/--" end
return string.format("%d/%d", st.current or 0, st.max or 0)
end
local function format_currency(copper_str)
local n = tonumber(copper_str)
if not n then return tostring(copper_str or "--") end
local whole = math.floor(n)
local silver = math.floor(whole / 100)
local copper = whole % 100
return string.format("%ds %02dc (%d copper)", silver, copper, whole)
end
local function resolve_stat_labels()
if stat_names_loaded then return end
local updated = false
for i = 1, #STAT_IDS do
local name = game.stat_name(STAT_IDS[i])
if name and #name > 0 then
STAT_LABELS[i] = string.format("%s [id:%d]", name, STAT_IDS[i])
updated = true
else
STAT_LABELS[i] = string.format("%s [id:%d]", STAT_DEFAULTS[i], STAT_IDS[i])
end
end
if updated then
stat_names_loaded = true
panel:set_items(stat_combo_id, pack_combo_items(STAT_LABELS, "HP [id:0]"), stat_idx)
end
end
local function tidy_category_name(raw)
local s = tostring(raw or ""):gsub("[%z\1-\31]", " ")
s = s:gsub("^[Cc]ategory_", ""):gsub("_", " "):gsub("^%s+", ""):gsub("%s+$", "")
if s == "" then return nil end
return trim_utf8(s, 28, "..")
end
local function resolve_category_label(cat_row, item_name, item_type)
if cat_row ~= nil then
local cached = cat_name_cache[cat_row]
if cached ~= nil then return cached end
local resolved = nil
if game.categories and game.categories.info then
local info = game.categories.info(cat_row)
if info then
resolved = tidy_category_name(info.display_name) or tidy_category_name(info.name)
end
end
resolved = resolved or ("Cat " .. tostring(cat_row))
cat_name_cache[cat_row] = resolved
return resolved
end
if type(item_name) == "string" and item_name:sub(1, 6) == "Money_" then
return "Currency"
end
if item_type ~= nil then
return "Type " .. tostring(item_type)
end
return "Items"
end
local function ensure_item_meta(row)
local meta = item_meta_cache[row]
if meta then return meta end
if game.items and game.items.search then
local hits = game.items.search(tostring(row), 4, row)
if hits then
for i = 1, #hits do
local h = hits[i]
item_meta_cache[h.row] = h
if h.row == row then return h end
end
end
end
return nil
end
local function update_item_info()
local meta = item_meta_cache[item_row]
local info, why = game.item_info(item_row)
local cidx = catalog_row_to_idx[item_row]
local disp = (info and info.display_name) or (meta and meta.display_name) or game.item_display_name(item_row)
local name = (info and info.name) or (meta and meta.name) or game.item_name(item_row) or (info and tostring(info.key or "?"))
if disp or name then
local title = (disp and disp ~= "" and name and disp ~= name)
and string.format("%s (%s)", tostring(disp), tostring(name))
or tostring(disp or name or "?")
local cat_txt = meta and resolve_category_label(meta.category_row, meta.name, meta.item_type)
or (cidx and catalog_cats[cidx])
or nil
local stack_val = (info and info.max_stack) or (meta and meta.max_stack)
local dur_val = (meta and meta.max_durability ~= nil and meta.max_durability ~= 65535) and meta.max_durability or nil
local parts = { string.format("Row %d: %s", item_row, title) }
if cat_txt then parts[#parts + 1] = "Cat: " .. cat_txt end
if stack_val ~= nil then parts[#parts + 1] = "Stack: " .. tostring(stack_val) end
if dur_val ~= nil then parts[#parts + 1] = "Dur: " .. tostring(dur_val) end
panel:set_text(item_info_id, clamp_text(table.concat(parts, " | ")))
elseif cidx and catalog_labels[cidx] then
local cat_txt = catalog_cats[cidx]
local txt = cat_txt and (catalog_labels[cidx] .. " | Cat: " .. cat_txt) or catalog_labels[cidx]
panel:set_text(item_info_id, clamp_text("Row " .. txt))
else
panel:set_text(item_info_id, clamp_text(string.format("Row %d: %s", item_row, tostring(why or "unavailable"))))
end
end
local function parse_search_query(raw)
local q = tostring(raw or ""):gsub("^%s+", ""):gsub("%s+$", "")
search_query = q
search_tokens = {}
local lower = string.lower(q)
for tok in lower:gmatch("%S+") do
search_tokens[#search_tokens + 1] = tok
end
if q:match("^%d+$") then
search_exact_num = tonumber(q)
else
search_exact_num = nil
end
end
local function format_item_label(entry, extra_suffix)
local r = entry.row
local disp = entry.display_name
local name = entry.name
local base
if disp and disp ~= "" and name and disp ~= name then
base = string.format("%d: %s (%s)", r, disp, name)
else
base = string.format("%d: %s", r, tostring(disp or name or "?"))
end
if extra_suffix and #extra_suffix > 0 then
base = base .. extra_suffix
end
return trim_utf8(base, 92, "...")
end
local function build_search_hay(row, key, disp, name, cat)
return string.lower(table.concat({
tostring(row or ""),
tostring(key or ""),
tostring(disp or ""),
tostring(name or ""),
tostring(cat or ""),
}, " "))
end
local function matches_tokens(hay, tokens)
for i = 1, #tokens do
if string.find(hay, tokens[i], 1, true) == nil then
return false
end
end
return true
end
local function get_active_source()
if view_mode == "bag" then
return bag_count, bag_rows, bag_keys, bag_labels, bag_cats, bag_hay, bag_buckets, bag_indices, bag_cat_counts
end
return catalog_count, catalog_rows, catalog_keys, catalog_labels, catalog_cats, catalog_hay, nil, nil, catalog_cat_counts
end
local function rebuild_category_picker(source_count, cat_counts)
local list = {}
for cat, cnt in pairs(cat_counts) do
if cnt and cnt > 0 then
list[#list + 1] = cat
end
end
table.sort(list)
active_category_list = list
if selected_category ~= nil and not (cat_counts[selected_category] and cat_counts[selected_category] > 0) then
selected_category = nil
end
local payload = {}
local bytes = 0
local sel_idx = 1
local all_lbl = trim_utf8(string.format("All Categories (%d)", source_count), 120, "...")
payload[1] = all_lbl
bytes = bytes + #all_lbl + 1
for i = 1, #list do
local cat = list[i]
local entry_lbl = trim_utf8(string.format("%s (%d)", cat, cat_counts[cat] or 0), 120, "...")
local cost = #entry_lbl + 1
if #payload >= MAX_LISTBOX_ITEMS or (bytes + cost) > MAX_LISTBOX_BYTES then
break
end
bytes = bytes + cost
payload[#payload + 1] = entry_lbl
if selected_category == cat then
sel_idx = #payload
end
end
panel:set_items(cat_listbox_id, payload, sel_idx)
if selected_category == nil then
panel:set_text(cat_status_id, clamp_text(string.format("Category: All Categories (%d)", source_count)))
else
panel:set_text(cat_status_id, clamp_text(string.format("Category: %s (%d)", selected_category, cat_counts[selected_category] or 0)))
end
end
local function rebuild_filtered_indices()
local source_count, rows, keys, _, cats, hay = get_active_source()
other_cat_hits = 0
if #search_tokens == 0 and selected_category == nil then
filtered_indices = nil
filtered_count = source_count
return
end
local exact = {}
local rest = {}
local want_cat = selected_category
local has_query = (#search_tokens > 0)
for i = 1, source_count do
local cat_ok = (want_cat == nil or cats[i] == want_cat)
local text_ok = (not has_query) or matches_tokens(hay[i], search_tokens)
if cat_ok then
if text_ok then
if search_exact_num ~= nil and (rows[i] == search_exact_num or keys[i] == search_exact_num) then
exact[#exact + 1] = i
else
rest[#rest + 1] = i
end
end
elseif has_query and text_ok then
other_cat_hits = other_cat_hits + 1
end
end
for i = 1, #rest do
exact[#exact + 1] = rest[i]
end
filtered_indices = exact
filtered_count = #exact
end
local function render_current_page(preserve_row)
local _, rows, _, labels, cats, _, buckets, indices = get_active_source()
local total = filtered_count
local total_pages = math.max(1, math.ceil(total / PAGE_SIZE))
if current_page < 1 then current_page = 1 end
if current_page > total_pages then current_page = total_pages end
picker_entries = {}
local payload = {}
local bytes = 0
local selected_idx = 0
local start_i = (current_page - 1) * PAGE_SIZE + 1
local end_i = math.min(total, current_page * PAGE_SIZE)
if total > 0 then
for pos = start_i, end_i do
local src_idx = filtered_indices and filtered_indices[pos] or pos
local r = rows[src_idx]
local lbl = labels[src_idx]
local cat = cats[src_idx] or "Items"
local display_lbl
if selected_category == nil then
display_lbl = trim_utf8("[" .. cat .. "] " .. lbl, 120, "...")
else
display_lbl = trim_utf8(lbl, 120, "...")
end
local cost = #display_lbl + 1
if #payload >= MAX_LISTBOX_ITEMS or (bytes + cost) > MAX_LISTBOX_BYTES then
break
end
bytes = bytes + cost
payload[#payload + 1] = { label = display_lbl }
picker_entries[#picker_entries + 1] = {
row = r,
bucket = buckets and buckets[src_idx] or nil,
index = indices and indices[src_idx] or nil,
label = display_lbl,
category = cat,
}
if preserve_row ~= nil and r == preserve_row and selected_idx == 0 then
selected_idx = #picker_entries
end
end
end
if #payload == 0 then
local empty_lbl
if catalog_indexing and view_mode == "catalog" then
empty_lbl = "(indexing catalog...)"
elseif selected_category ~= nil and other_cat_hits > 0 then
empty_lbl = string.format("(0 in %s; %d in All Categories - click All Cats)", selected_category, other_cat_hits)
elseif search_query ~= "" then
empty_lbl = string.format("No items matched '%s'", search_query)
elseif view_mode == "bag" then
empty_lbl = "(bag empty)"
else
empty_lbl = "(catalog empty)"
end
panel:set_items(catalog_listbox_id, { { label = trim_utf8(empty_lbl, 120, "...") } }, 0)
if selected_category ~= nil and other_cat_hits > 0 then
panel:set_text(page_info_id, clamp_text(string.format("0 in %s (%d in All Categories)", selected_category, other_cat_hits)))
else
panel:set_text(page_info_id, clamp_text("Showing 0 of 0 (Page 1/1)"))
end
return 0
end
if selected_idx == 0 then
selected_idx = 1
end
panel:set_items(catalog_listbox_id, payload, selected_idx)
if preserve_row == nil and picker_entries[selected_idx] then
local picked = picker_entries[selected_idx]
item_row = picked.row
selected_bag_row = picked.row
selected_bag_bucket = picked.bucket
selected_bag_index = picked.index
panel:set_value(row_input_id, tostring(item_row))
update_item_info()
end
panel:set_text(page_info_id, clamp_text(string.format("Showing %d-%d of %d (Page %d/%d)",
start_i, start_i + #payload - 1, total, current_page, total_pages)))
return #picker_entries
end
local function refresh_active_view(preserve_row)
local source_count, _, _, _, _, _, _, _, cat_counts = get_active_source()
rebuild_category_picker(source_count, cat_counts)
rebuild_filtered_indices()
return render_current_page(preserve_row)
end
local function reset_catalog_store()
catalog_count = 0
catalog_max_row = -1
catalog_rows = {}
catalog_keys = {}
catalog_labels = {}
catalog_cats = {}
catalog_hay = {}
catalog_seen_rows = {}
catalog_row_to_idx = {}
catalog_cat_counts = {}
catalog_last_ui_count = 0
end
local function append_catalog_entry(row, key, disp, name, cat)
if row == nil or catalog_seen_rows[row] then return nil end
local idx = catalog_count + 1
local lbl = format_item_label({ row = row, display_name = disp, name = name })
local cat_lbl = cat or "Items"
catalog_count = idx
if row > catalog_max_row then catalog_max_row = row end
catalog_seen_rows[row] = true
catalog_row_to_idx[row] = idx
catalog_rows[idx] = row
catalog_keys[idx] = key or row
catalog_labels[idx] = lbl
catalog_cats[idx] = cat_lbl
catalog_hay[idx] = build_search_hay(row, key, disp, name, cat_lbl)
catalog_cat_counts[cat_lbl] = (catalog_cat_counts[cat_lbl] or 0) + 1
return idx
end
local function start_catalog_index(force)
if catalog_indexing and not force then return end
if catalog_started and not force and catalog_count > 1 then
if view_mode == "catalog" then
refresh_active_view(item_row)
end
return
end
catalog_started = true
catalog_indexing = true
catalog_use_fast_search = (game.items and game.items.search ~= nil)
catalog_cursor = 0
reset_catalog_store()
end
local function step_catalog_index()
if not catalog_indexing then return end
local batches = 0
local batch_limit = catalog_use_fast_search and 6 or 24
local page_dirty = false
while catalog_cursor ~= nil and batches < batch_limit do
local page, next_c
if catalog_use_fast_search then
page, next_c = game.items.search("", 100, catalog_cursor)
if not page and catalog_cursor == 0 and tostring(next_c):find("adapter unavailable", 1, true) then
catalog_use_fast_search = false
page, next_c = game.items.find("", 8, catalog_cursor, true)
end
else
page, next_c = game.items.find("", 8, catalog_cursor, true)
end
if not page then
local msg = tostring(next_c or "")
if msg:find("not loaded", 1, true) then
return
end
catalog_indexing = false
if catalog_count == 0 then
append_catalog_entry(COPPER_ROW, COPPER_ROW, "Copper", "Money_Copper", "Currency")
end
if view_mode == "catalog" then
refresh_active_view(item_row)
end
panel:set_text(status_id, clamp_text("Catalog index stopped: " .. msg))
return
end
for i = 1, #page do
local entry = page[i]
local cat_lbl = resolve_category_label(entry.category_row, entry.name, entry.item_type)
local idx = append_catalog_entry(entry.row, entry.key, entry.display_name, entry.name, cat_lbl)
if idx and view_mode == "catalog" and filtered_indices ~= nil then
local cat_ok = (selected_category == nil or cat_lbl == selected_category)
local text_ok = (#search_tokens == 0) or matches_tokens(catalog_hay[idx], search_tokens)
if cat_ok then
if text_ok then
filtered_indices[#filtered_indices + 1] = idx
filtered_count = #filtered_indices
if filtered_count <= current_page * PAGE_SIZE then
page_dirty = true
end
end
elseif #search_tokens > 0 and text_ok then
other_cat_hits = other_cat_hits + 1
page_dirty = true
end
end
end
if next_c ~= nil and next_c <= catalog_cursor then
next_c = nil
end
catalog_cursor = next_c
batches = batches + 1
end
local finished = (catalog_cursor == nil)
if finished then
catalog_indexing = false
if view_mode == "catalog" then
refresh_active_view(item_row)
if search_query ~= "" then
panel:set_text(status_id, clamp_text(string.format("Search '%s': %d of %d item(s)",
search_query, filtered_count, catalog_count)))
else
panel:set_text(status_id, clamp_text(string.format("Catalog ready: %d item(s) (%d categories)",
catalog_count, #active_category_list)))
end
end
bsx.log(string.format("player_toolkit: catalog ready items=%d categories=%d max_row=%d",
catalog_count, #active_category_list, catalog_max_row))
return
end
if view_mode == "catalog" then
if page_dirty or (catalog_count - catalog_last_ui_count >= 500) or (catalog_last_ui_count == 0 and catalog_count > 0) then
catalog_last_ui_count = catalog_count
rebuild_category_picker(catalog_count, catalog_cat_counts)
if filtered_indices == nil then
filtered_count = catalog_count
end
render_current_page(item_row)
panel:set_text(status_id, clamp_text(string.format("Indexing catalog... %d item(s) (row %d)",
catalog_count, catalog_cursor)))
end
end
end
local function refresh()
resolve_stat_labels()
local actor = game.player()
local hp = actor and game.stats.read(actor, 0) or nil
local stam = actor and game.stats.read(actor, 25) or nil
local mp = actor and game.stats.read(actor, 26) or nil
panel:set_text(stats_readout_id, clamp_text(string.format("HP: %s | Stam: %s | MP: %s",
format_stat(hp), format_stat(stam), format_stat(mp))))
if hp and hp.max and hp.max > 0 then
panel:set_value(hp_bar_id, clamp((hp.current or 0) / hp.max, 0.0, 1.0))
panel:set_text(hp_bar_id, clamp_text(string.format("HP: %d / %d", hp.current or 0, hp.max)))
else
panel:set_value(hp_bar_id, 0.0)
panel:set_text(hp_bar_id, actor and "HP: unavailable" or "HP: waiting for player")
end
local copper, why_c = game.inventory.count(COPPER_ROW)
if copper then
panel:set_text(copper_id, clamp_text("Money (row 2075): " .. format_currency(copper)))
else
panel:set_text(copper_id, clamp_text("Money (row 2075): " .. tostring(why_c or "--")))
end
update_item_info()
last_summary = {
world_ready = world_ready,
stat_id = STAT_IDS[stat_idx] or 0,
item_row = item_row,
item_qty = item_qty,
copper = copper,
}
if world_ready and not logged_ready then
logged_ready = true
local ready_msg = string.format("PLAYER_TOOLKIT_READY hp=%s stam=%s mp=%s copper=%s",
format_stat(hp), format_stat(stam), format_stat(mp), tostring(copper or "--"))
panel:set_text(status_id, clamp_text(string.format("Ready | %s | dev=%s",
copper and format_currency(copper) or "Money --", tostring(bsx.dev_mode))))
bsx.log(ready_msg)
end
end
local function apply_stat_delta(delta, label)
local stat_id = STAT_IDS[stat_idx] or 0
local ok, why = game.stats.add(stat_id, delta)
if not ok then
local msg = "Stat add blocked: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("player_toolkit: " .. msg)
end
local msg = string.format("%s stat %d by %+d", label, stat_id, delta)
panel:set_text(status_id, clamp_text(msg))
bsx.bus.emit("player_toolkit.applied", { kind = "stat_add", stat_id = stat_id, delta = delta })
bsx.log("player_toolkit: " .. msg)
refresh()
end
local function apply_inventory_add(row, qty, label)
local ok, why = game.inventory.add(row, qty)
if not ok then
local msg = "Item grant blocked: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("player_toolkit: " .. msg)
end
local msg = string.format("Granted %d x row %d (%s)", qty, row, label)
panel:set_text(status_id, clamp_text(msg))
bsx.bus.emit("player_toolkit.applied", { kind = "inventory_add", row = row, count = qty })
bsx.log("player_toolkit: " .. msg)
refresh()
end
local function set_quantity(qty)
item_qty = math.floor(clamp(qty, 1, 100))
panel:set_value(qty_slider_id, item_qty)
panel:set_text(status_id, clamp_text("Quantity set to " .. tostring(item_qty)))
end
local function find_bag_slot(target_row)
local inv, why = game.inventory.list(4096)
if not inv then return nil, why end
if selected_bag_row == target_row and selected_bag_bucket ~= nil and selected_bag_index ~= nil then
for i = 1, #inv do
local it = inv[i]
if it.bucket == selected_bag_bucket and it.index == selected_bag_index and
it.row == target_row and (tonumber(it.count) or 0) > 0 then
return it.bucket, it.index
end
end
end
for i = 1, #inv do
local it = inv[i]
if it.row == target_row and (tonumber(it.count) or 0) > 0 then
selected_bag_bucket = it.bucket
selected_bag_index = it.index
selected_bag_row = it.row
return it.bucket, it.index
end
end
return nil, string.format("row %d not found in bag", target_row)
end
status_id = panel:text("Waiting for world...")
panel:begin_tab_bar("pt_tabs")
panel:tab("Items & Currency")
copper_id = panel:text("Money (row 2075): --")
panel:button("+100 Silver (10k)", function() apply_inventory_add(COPPER_ROW, 10000, "100 Silver") end)
panel:same_line()
panel:button("+10k Silver (1M)", function() apply_inventory_add(COPPER_ROW, 1000000, "10000 Silver") end)
panel:button("Catalog", function()
view_mode = "catalog"
selected_category = nil
current_page = 1
if not catalog_started or catalog_count == 0 then
start_catalog_index(true)
step_catalog_index()
else
refresh_active_view(item_row)
panel:set_text(status_id, clamp_text(string.format("Catalog: %d of %d item(s)", filtered_count, catalog_count)))
end
end)
panel:same_line()
panel:button("My Bag", function()
local inv, why = game.inventory.list(4096)
if not inv then
local msg = "Bag read failed: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("player_toolkit: " .. msg)
end
view_mode = "bag"
selected_category = nil
current_page = 1
bag_count = 0
bag_rows = {}
bag_keys = {}
bag_labels = {}
bag_cats = {}
bag_hay = {}
bag_buckets = {}
bag_indices = {}
bag_cat_counts = {}
local totals = {}
local first_slot = {}
local order = {}
for i = 1, #inv do
local it = inv[i]
local cnt = tonumber(it.count) or 0
if it.row and cnt > 0 then
if not totals[it.row] then
totals[it.row] = 0
first_slot[it.row] = { bucket = it.bucket, index = it.index }
order[#order + 1] = it.row
end
totals[it.row] = totals[it.row] + cnt
end
end
for i = 1, #order do
local r = order[i]
local meta = ensure_item_meta(r)
local disp = (meta and meta.display_name) or game.item_display_name(r)
local name = (meta and meta.name) or game.item_name(r) or "item"
local cat_lbl = resolve_category_label(meta and meta.category_row or nil, name, meta and meta.item_type or nil)
local sl = first_slot[r]
local idx = bag_count + 1
bag_count = idx
bag_rows[idx] = r
bag_keys[idx] = (meta and meta.key) or r
bag_labels[idx] = format_item_label({ row = r, display_name = disp, name = name }, " x" .. tostring(totals[r]))
bag_cats[idx] = cat_lbl
bag_hay[idx] = build_search_hay(r, bag_keys[idx], disp, name, cat_lbl)
bag_buckets[idx] = sl and sl.bucket or nil
bag_indices[idx] = sl and sl.index or nil
bag_cat_counts[cat_lbl] = (bag_cat_counts[cat_lbl] or 0) + 1
end
refresh_active_view(nil)
panel:set_text(status_id, clamp_text(string.format("Loaded %d bag item(s)", bag_count)))
end)
panel:same_line()
panel:button("Refresh Catalog", function()
view_mode = "catalog"
selected_category = nil
current_page = 1
cat_name_cache = {}
item_meta_cache = {}
start_catalog_index(true)
step_catalog_index()
end)
panel:set_next_width(240)
search_input_id = panel:input_text("Search Item", "", function(text)
parse_search_query(text)
current_page = 1
if not catalog_started then
start_catalog_index(true)
step_catalog_index()
end
local source_count = get_active_source()
rebuild_filtered_indices()
render_current_page(nil)
if search_query == "" then
panel:set_text(status_id, clamp_text(string.format("Showing %d item(s)", filtered_count)))
elseif selected_category ~= nil and other_cat_hits > 0 then
panel:set_text(status_id, clamp_text(string.format("Search '%s': %d in %s (%d in All Categories)",
search_query, filtered_count, selected_category, other_cat_hits)))
else
panel:set_text(status_id, clamp_text(string.format("Search '%s': %d of %d item(s)",
search_query, filtered_count, source_count)))
end
end, { live = true })
panel:same_line()
panel:button("Clear", function()
parse_search_query("")
panel:set_value(search_input_id, "")
current_page = 1
local source_count = get_active_source()
rebuild_filtered_indices()
render_current_page(item_row)
panel:set_text(status_id, clamp_text(string.format("Showing %d of %d item(s)", filtered_count, source_count)))
end)
cat_status_id = panel:text("Category: All Categories")
local function select_category(cat_or_nil)
selected_category = cat_or_nil
current_page = 1
refresh_active_view(nil)
if selected_category == nil then
panel:set_text(status_id, clamp_text(string.format("Category: All Categories (%d match)", filtered_count)))
elseif other_cat_hits > 0 then
panel:set_text(status_id, clamp_text(string.format("Category %s: %d match (%d in All Categories)",
selected_category, filtered_count, other_cat_hits)))
else
panel:set_text(status_id, clamp_text(string.format("Category %s: %d item(s)", selected_category, filtered_count)))
end
end
panel:button("< Cat", function()
local total_cats = #active_category_list
if total_cats == 0 then
select_category(nil)
return
end
if selected_category == nil then
select_category(active_category_list[total_cats])
return
end
for i = 1, total_cats do
if active_category_list[i] == selected_category then
select_category(i > 1 and active_category_list[i - 1] or nil)
return
end
end
select_category(nil)
end)
panel:same_line()
panel:button("Cat >", function()
local total_cats = #active_category_list
if total_cats == 0 then
select_category(nil)
return
end
if selected_category == nil then
select_category(active_category_list[1])
return
end
for i = 1, total_cats do
if active_category_list[i] == selected_category then
select_category(i < total_cats and active_category_list[i + 1] or nil)
return
end
end
select_category(nil)
end)
panel:same_line()
panel:button("All Cats", function()
select_category(nil)
end)
panel:section("Browse Categories", false)
cat_listbox_id = panel:listbox("Categories", { "All Categories" }, {
height = 140,
searchable = true,
selected = 1,
}, function(idx)
if idx <= 1 then
select_category(nil)
else
local cat = active_category_list[idx - 1]
if cat then
select_category(cat)
end
end
end)
panel:end_section()
panel:button("< Prev", function()
if current_page > 1 then
current_page = current_page - 1
render_current_page(nil)
panel:set_text(status_id, clamp_text(string.format("Page %d/%d (%d items)",
current_page, math.max(1, math.ceil(filtered_count / PAGE_SIZE)), filtered_count)))
end
end)
panel:same_line()
panel:button("Next >", function()
local total_pages = math.max(1, math.ceil(filtered_count / PAGE_SIZE))
if current_page < total_pages then
current_page = current_page + 1
render_current_page(nil)
panel:set_text(status_id, clamp_text(string.format("Page %d/%d (%d items)",
current_page, total_pages, filtered_count)))
end
end)
panel:same_line()
page_info_id = panel:text("Showing 0 of 0 (Page 1/1)")
catalog_listbox_id = panel:listbox("Item Catalog", picker_entries, {
height = 240,
searchable = false,
selected = 1,
}, function(idx, label)
local picked = picker_entries[idx]
if picked and picked.row then
item_row = picked.row
selected_bag_row = picked.row
selected_bag_bucket = picked.bucket
selected_bag_index = picked.index
panel:set_value(row_input_id, tostring(item_row))
update_item_info()
panel:set_text(status_id, clamp_text("Selected " .. tostring(label or picked.label)))
end
end)
panel:set_next_width(120)
row_input_id = panel:input_text("Item Row", tostring(item_row), function(text)
local trimmed = tostring(text or ""):match("^%s*(%d+)%s*$")
local n = trimmed and tonumber(trimmed) or nil
if not n or n < 0 or n > 65534 then
local msg = "Item Row must be 0..65534"
panel:set_text(status_id, msg)
return bsx.log("player_toolkit: " .. msg)
end
item_row = math.floor(n)
if selected_bag_row ~= item_row then
selected_bag_bucket = nil
selected_bag_index = nil
end
ensure_item_meta(item_row)
update_item_info()
panel:set_text(status_id, clamp_text("Selected item row " .. tostring(item_row)))
end)
item_info_id = panel:text("Item: --")
panel:set_next_width(160)
qty_slider_id = panel:slider("Quantity", 1.0, 100.0, item_qty, function(val)
item_qty = math.floor(clamp(val + 0.5, 1, 100))
end)
panel:same_line()
panel:button("x1", function() set_quantity(1) end)
panel:same_line()
panel:button("x10", function() set_quantity(10) end)
panel:same_line()
panel:button("x50", function() set_quantity(50) end)
panel:push_color("button", "#B8333D")
panel:push_color("button_hovered", "#D14650")
panel:button("Grant Item", function()