Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
693 lines (645 loc) · 28.2 KB
/
Copy pathmain.lua
File metadata and controls
693 lines (645 loc) · 28.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
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
-- World Controller: Lua API 4 changes-tier overlay panel for time, weather, speed scale, waypoints, and map pin teleport.
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 is_finite(v)
return type(v) == "number" and v == v and v > -math.huge and v < math.huge
end
local function clamp_text(text)
local s = tostring(text or ""):gsub("%z", " ")
if #s <= 127 then return s end
return string.sub(s, 1, 124) .. "..."
end
local same_day = config.bool("Controller", "SameDay", true)
local selected_hour = math.floor(clamp(config.int("Controller", "DefaultHour", 12), 0, 23))
local TIME_PRESET_LABELS = { "Dawn (06:00)", "Noon (12:00)", "Dusk (18:00)", "Midnight (00:00)" }
local TIME_PRESET_HOURS = { 6, 12, 18, 0 }
local WEATHER_LABELS = { "Clear", "Partly Cloudy", "Overcast", "Dense Fog", "Light Rain", "Storm", "Snow", "Blizzard" }
local WEATHER_PRESETS = {
{ rain = 0.0, snow = 0.0, precipitation = 0.0, humidity = 0.20, cloudiness = 0.05, wind_direction = 90, wind_speed = 2, fog = 0.0, cloud_density = 0.04, cloud_alpha = 0.06, cloud_speed = 0.5, sun_brightness = 1.0, wetness = 0.0, dust = 0.0 },
{ rain = 0.0, snow = 0.0, precipitation = 0.0, humidity = 0.40, cloudiness = 0.45, wind_direction = 100, wind_speed = 5, fog = 0.06, cloud_density = 0.42, cloud_alpha = 0.68, cloud_speed = 0.8, sun_brightness = 0.90, wetness = 0.0, dust = 0.0 },
{ rain = 0.0, snow = 0.0, precipitation = 0.05, humidity = 0.72, cloudiness = 0.95, wind_direction = 120, wind_speed = 7, fog = 0.28, cloud_density = 0.80, cloud_alpha = 0.94, cloud_speed = 1.0, sun_brightness = 0.56, wetness = 0.10, dust = 0.0 },
{ rain = 0.0, snow = 0.0, precipitation = 0.0, humidity = 0.96, cloudiness = 0.65, wind_direction = 90, wind_speed = 3, fog = 0.92, cloud_density = 0.60, cloud_alpha = 0.80, cloud_speed = 0.4, sun_brightness = 0.58, wetness = 0.15, dust = 0.0 },
{ rain = 0.45, snow = 0.0, precipitation = 0.45, humidity = 0.82, cloudiness = 0.82, wind_direction = 135, wind_speed = 10, fog = 0.35, cloud_density = 0.72, cloud_alpha = 0.88, cloud_speed = 1.2, sun_brightness = 0.50, wetness = 0.60, dust = 0.0 },
{ rain = 1.0, snow = 0.0, precipitation = 1.0, humidity = 0.95, cloudiness = 1.0, wind_direction = 225, wind_speed = 24, fog = 0.52, cloud_density = 0.92, cloud_alpha = 1.0, cloud_speed = 2.2, sun_brightness = 0.36, wetness = 1.0, dust = 0.0 },
{ rain = 0.0, snow = 0.9, precipitation = 0.9, humidity = 0.85, cloudiness = 0.92, wind_direction = 0, wind_speed = 11, fog = 0.40, cloud_density = 0.80, cloud_alpha = 0.92, cloud_speed = 1.0, sun_brightness = 0.55, wetness = 0.0, dust = 0.0 },
{ rain = 0.0, snow = 1.0, precipitation = 1.0, humidity = 0.98, cloudiness = 1.0, wind_direction = 315, wind_speed = 28, fog = 0.78, cloud_density = 0.96, cloud_alpha = 1.0, cloud_speed = 2.6, sun_brightness = 0.40, wetness = 0.0, dust = 0.5 },
}
local SLOT_LABELS = { "Slot 1", "Slot 2", "Slot 3", "Slot 4" }
local wx_cur = {
rain = 0.0, snow = 0.0, precipitation = 0.0, humidity = 0.20, cloudiness = 0.05,
wind_direction = 90.0, wind_speed = 2.0, fog = 0.0, cloud_density = 0.04,
cloud_alpha = 0.06, cloud_speed = 0.5, sun_brightness = 1.0, wetness = 0.0, dust = 0.0,
}
local wx_dirty = false
local last_wx_push = 0.0
local last_wx_log = 0.0
local time_preset_idx = 2
local weather_preset_idx = 1
local active_slot = 1
local world_ready = false
local logged_panel = false
local logged_ready = false
local freeze_time = false
local freeze_cb_id
local freeze_countdown = 0
local world_speed = 1.0
local speed_slider_id
local allow_bus_teleports = config.bool("Controller", "AllowBusTeleports", true)
local pin_info_id
local waypoints = {
{ label = "Camp", set = false, x = 0, y = 0, z = 0, yaw_deg = nil },
{ label = "Point 2", set = false, x = 0, y = 0, z = 0, yaw_deg = nil },
{ label = "Point 3", set = false, x = 0, y = 0, z = 0, yaw_deg = nil },
{ label = "Point 4", set = false, x = 0, y = 0, z = 0, yaw_deg = nil },
}
local panel = bsx.ui.panel("World Controller")
local tools_panel
local status_id = panel:text("Waiting for world...")
panel:section("Time & Weather", true)
local time_combo_id
local hour_slider_id
local weather_combo_id
time_combo_id = panel:combo("Time preset", TIME_PRESET_LABELS, time_preset_idx, function(idx)
time_preset_idx = idx
selected_hour = TIME_PRESET_HOURS[idx] or 12
panel:set_value(hour_slider_id, selected_hour)
end)
hour_slider_id = panel:slider("Hour", 0.0, 23.0, selected_hour, function(value)
selected_hour = math.floor(clamp(value + 0.5, 0, 23))
end)
panel:button("Apply Time", function()
local hour = math.floor(clamp(selected_hour, 0, 23))
local ok, why = game.world.set_time(hour, 0, 0, same_day)
if not ok then
local msg = "Time blocked: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local msg = string.format("Applied time %02d:00 (same_day=%s)", hour, tostring(same_day))
panel:set_text(status_id, clamp_text(msg))
bsx.bus.emit("world_controller.applied", { kind = "time", hour = hour })
bsx.log("world_controller: " .. msg)
end)
panel:same_line()
freeze_cb_id = panel:checkbox("Freeze Time", false, function(checked)
freeze_time = checked
freeze_countdown = 0
local msg = checked and string.format("Freeze Time enabled (%02d:00)", math.floor(clamp(selected_hour, 0, 23)))
or "Freeze Time disabled"
panel:set_text(status_id, clamp_text(msg))
bsx.log("world_controller: " .. msg)
end)
local sld_cloud_id, sld_rain_id, sld_snow_id, sld_fog_id, sld_wind_id, sld_cspeed_id, sld_sun_id, sld_dust_id
local function push_weather_tbl(tbl, label_note, always_log)
local ok, why = game.world.set_weather(tbl)
if not ok then
local msg = "Weather blocked: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local msg = label_note or string.format("Weather clouds=%.0f%% fog=%.0f%% rain=%.0f%%",
(tbl.cloudiness or 0) * 100, (tbl.fog or 0) * 100, (tbl.rain or 0) * 100)
panel:set_text(status_id, clamp_text(msg))
local now = os.clock()
if always_log or (now - last_wx_log >= 0.6) then
last_wx_log = now
bsx.bus.emit("world_controller.applied", { kind = "weather", preset = label_note or "custom" })
bsx.log("world_controller: " .. msg)
end
end
local function sync_weather_sliders(p)
for k, v in pairs(p) do wx_cur[k] = v end
if sld_cloud_id then tools_panel:set_value(sld_cloud_id, wx_cur.cloudiness) end
if sld_rain_id then tools_panel:set_value(sld_rain_id, wx_cur.rain) end
if sld_snow_id then tools_panel:set_value(sld_snow_id, wx_cur.snow) end
if sld_fog_id then tools_panel:set_value(sld_fog_id, wx_cur.fog) end
if sld_wind_id then tools_panel:set_value(sld_wind_id, wx_cur.wind_speed) end
if sld_cspeed_id then tools_panel:set_value(sld_cspeed_id, wx_cur.cloud_speed) end
if sld_sun_id then tools_panel:set_value(sld_sun_id, wx_cur.sun_brightness) end
if sld_dust_id then tools_panel:set_value(sld_dust_id, wx_cur.dust) end
end
weather_combo_id = panel:combo("Weather preset", WEATHER_LABELS, weather_preset_idx, function(idx)
weather_preset_idx = idx
local preset = WEATHER_PRESETS[idx] or WEATHER_PRESETS[1]
sync_weather_sliders(preset)
end)
panel:button("Apply Weather", function()
local label = WEATHER_LABELS[weather_preset_idx] or "Clear"
local preset = WEATHER_PRESETS[weather_preset_idx] or WEATHER_PRESETS[1]
sync_weather_sliders(preset)
wx_dirty = false
push_weather_tbl(wx_cur, "Applied weather: " .. label, true)
end)
panel:same_line()
panel:button("Reset Weather", function()
wx_dirty = false
local ok, why = game.world.reset_weather()
if not ok then
local msg = "Weather reset blocked: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local msg = "Reset weather override"
panel:set_text(status_id, msg)
bsx.bus.emit("world_controller.applied", { kind = "weather_reset" })
bsx.log("world_controller: " .. msg)
end)
local last_speed_log = 0
local function apply_speed(val, always_log)
local target = clamp(val, 0.1, 2.0)
local ok, why = game.world.set_time_scale(target)
if not ok then
local msg = "Speed blocked: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
world_speed = target
panel:set_value(speed_slider_id, world_speed)
local msg = string.format("World speed %.2fx", world_speed)
panel:set_text(status_id, clamp_text(msg))
bsx.bus.emit("world_controller.applied", { kind = "time_scale", scale = world_speed })
local now = os.clock()
if always_log or (now - last_speed_log >= 0.5) then
last_speed_log = now
bsx.log("world_controller: " .. msg)
end
end
speed_slider_id = panel:slider("World Speed", 0.1, 2.0, world_speed, function(value)
apply_speed(value, false)
end)
panel:button("0.5x", function() apply_speed(0.5, true) end)
panel:same_line()
panel:button("1.0x", function() apply_speed(1.0, true) end)
panel:same_line()
panel:button("1.5x", function() apply_speed(1.5, true) end)
panel:end_section()
panel:section("Waypoints & Teleport", true)
local slot_combo_id
local label_input_id
local slot_info_id
local function sanitize_label(text, fallback)
local s = tostring(text or ""):gsub("[%z|\r\n]", " "):gsub("^%s+", ""):gsub("%s+$", "")
if s == "" then return fallback end
if #s > 32 then return string.sub(s, 1, 32) end
return s
end
local function update_slot_ui()
local wp = waypoints[active_slot]
panel:set_value(label_input_id, wp.label)
if wp.set then
local yaw = wp.yaw_deg and string.format(" yaw=%.0f", wp.yaw_deg) or ""
panel:set_text(slot_info_id, clamp_text(string.format("%s: (%.1f, %.1f, %.1f)%s",
wp.label, wp.x, wp.y, wp.z, yaw)))
else
panel:set_text(slot_info_id, clamp_text(wp.label .. ": empty"))
end
end
local function encode_waypoint(wp)
return string.format("%s|%.17g|%.17g|%.17g|%s",
sanitize_label(wp.label, "Waypoint"), wp.x, wp.y, wp.z,
wp.yaw_deg and string.format("%.17g", wp.yaw_deg) or "")
end
local function split_pipe_fields(raw)
local parts = {}
local pos = 1
while true do
local sep = string.find(raw, "|", pos, true)
if not sep then
parts[#parts + 1] = string.sub(raw, pos)
break
end
parts[#parts + 1] = string.sub(raw, pos, sep - 1)
pos = sep + 1
end
return parts
end
local function load_waypoints()
for i = 1, #waypoints do
local raw = bsx.save.get("wc.wp." .. tostring(i))
if type(raw) == "string" then
local parts = split_pipe_fields(raw)
local lbl, xs, ys, zs, ys_deg = parts[1], parts[2], parts[3], parts[4], parts[5]
local x, y, z = tonumber(xs), tonumber(ys), tonumber(zs)
if lbl and lbl ~= "" and x and y and z then
waypoints[i] = {
label = sanitize_label(lbl, "Slot " .. tostring(i)),
set = true,
x = x,
y = y,
z = z,
yaw_deg = tonumber(ys_deg),
}
end
end
end
update_slot_ui()
end
slot_combo_id = panel:combo("Waypoint slot", SLOT_LABELS, active_slot, function(idx)
active_slot = idx
update_slot_ui()
end)
label_input_id = panel:input_text("Label", waypoints[active_slot].label, function(text)
waypoints[active_slot].label = sanitize_label(text, "Slot " .. tostring(active_slot))
if waypoints[active_slot].set then
bsx.save.set("wc.wp." .. tostring(active_slot), encode_waypoint(waypoints[active_slot]))
end
update_slot_ui()
end)
slot_info_id = panel:text("Camp: empty")
update_slot_ui()
panel:button("Save Here", function()
local pos, why = game.player_position()
if not pos then
local msg = "Position unavailable: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local wp = waypoints[active_slot]
wp.set = true
wp.x, wp.y, wp.z, wp.yaw_deg = pos.x, pos.y, pos.z, pos.yaw_deg
bsx.save.set("wc.wp." .. tostring(active_slot), encode_waypoint(wp))
update_slot_ui()
local msg = string.format("Saved %s at (%.1f, %.1f, %.1f)", wp.label, wp.x, wp.y, wp.z)
panel:set_text(status_id, clamp_text(msg))
bsx.log("world_controller: " .. msg)
end)
panel:same_line()
panel:button("Teleport", function()
local wp = waypoints[active_slot]
if not wp.set then
local msg = wp.label .. " has no saved coordinates"
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local ok, why = game.world.teleport(wp.x, wp.y, wp.z, wp.yaw_deg)
if not ok then
local msg = "Teleport blocked: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local msg = string.format("Teleport queued to %s", wp.label)
panel:set_text(status_id, clamp_text(msg))
bsx.bus.emit("world_controller.applied", { kind = "teleport", label = wp.label })
bsx.log("world_controller: " .. msg)
end)
pin_info_id = panel:text("Map Pin: set a marker on the map")
panel:button("TP to Map Pin", function()
local pin, why = game.world.map_marker()
if not pin then
local msg = "Map Pin unavailable: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local ok, tp_why = game.world.teleport(pin.x, pin.y, pin.z, nil, true)
if not ok then
local msg = "Map Pin TP blocked: " .. tostring(tp_why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local msg = string.format("Map Pin TP -> (%.1f, %.1f, %.1f)", pin.x, pin.y + 2.5, pin.z)
panel:set_text(status_id, clamp_text(msg))
bsx.bus.emit("world_controller.applied", { kind = "map_pin_teleport" })
bsx.log("world_controller: " .. msg)
end)
panel:same_line()
panel:button("Up +10m", function()
local pos, why = game.player_position()
if not pos then
local msg = "Position unavailable: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local ok, tp_why = game.world.teleport(pos.x, pos.y + 10.0, pos.z, pos.yaw_deg)
if not ok then
local msg = "Up TP blocked: " .. tostring(tp_why)
panel:set_text(status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
local msg = string.format("Up +10m -> (%.1f, %.1f, %.1f)", pos.x, pos.y + 10.0, pos.z)
panel:set_text(status_id, clamp_text(msg))
bsx.bus.emit("world_controller.applied", { kind = "up_teleport" })
bsx.log("world_controller: " .. msg)
end)
panel:checkbox("Allow Bus Teleports", allow_bus_teleports, function(checked)
allow_bus_teleports = (checked == true)
config.set("Controller", "AllowBusTeleports", allow_bus_teleports)
config.save()
local msg = allow_bus_teleports and "Bus teleports enabled" or "Bus teleports disabled"
panel:set_text(status_id, clamp_text(msg))
bsx.log("world_controller: " .. msg)
end)
panel:end_section()
local QUICK_PANELS = {
"InventoryEquipmentPanel",
"QuestMenuPanel",
"QuestMenuPanel2",
"SkillTreePanel",
"KnowledgePanel2",
"WorldMapView",
"ChallengeMenuPanel2",
"FactionQuestMenuPanel",
"PlayGuideView",
"AlertHistoryView",
"PetView",
"LogoutView",
}
local quick_panel_idx = 1
local map_zoom_scale = 1.0
tools_panel = bsx.ui.panel("World Tools")
local tools_status_id = tools_panel:text("World Tools: waiting for world...")
local map_zoom_info_id, map_zoom_slider_id, observed_panels_id
tools_panel:section("Weather Fine-Tuning", false)
local function on_wx_slider()
wx_cur.cloud_density = wx_cur.cloudiness <= 0.08 and 0.04 or clamp(0.25 + wx_cur.cloudiness * 0.68, 0.0, 0.98)
wx_cur.cloud_alpha = wx_cur.cloudiness <= 0.08 and 0.06 or clamp(0.45 + wx_cur.cloudiness * 0.55, 0.0, 1.0)
wx_cur.precipitation = math.max(wx_cur.rain, wx_cur.snow)
wx_cur.humidity = math.max(wx_cur.fog, wx_cur.precipitation)
wx_cur.wetness = clamp(wx_cur.rain * 0.9, 0.0, 1.0)
wx_dirty = true
end
sld_cloud_id = tools_panel:slider("Cloud Cover", 0.0, 1.0, wx_cur.cloudiness, function(v)
wx_cur.cloudiness = clamp(v, 0.0, 1.0); on_wx_slider()
end)
sld_fog_id = tools_panel:slider("Fog / Haze", 0.0, 1.0, wx_cur.fog, function(v)
wx_cur.fog = clamp(v, 0.0, 1.0); on_wx_slider()
end)
sld_rain_id = tools_panel:slider("Rain", 0.0, 1.0, wx_cur.rain, function(v)
wx_cur.rain = clamp(v, 0.0, 1.0); on_wx_slider()
end)
sld_snow_id = tools_panel:slider("Snow", 0.0, 1.0, wx_cur.snow, function(v)
wx_cur.snow = clamp(v, 0.0, 1.0); on_wx_slider()
end)
sld_wind_id = tools_panel:slider("Wind Speed", 0.0, 35.0, wx_cur.wind_speed, function(v)
wx_cur.wind_speed = clamp(v, 0.0, 35.0); on_wx_slider()
end)
sld_cspeed_id = tools_panel:slider("Cloud Speed", 0.0, 4.0, wx_cur.cloud_speed, function(v)
wx_cur.cloud_speed = clamp(v, 0.0, 4.0); wx_dirty = true
end)
sld_sun_id = tools_panel:slider("Sun Brightness", 0.2, 1.6, wx_cur.sun_brightness, function(v)
wx_cur.sun_brightness = clamp(v, 0.2, 1.6); wx_dirty = true
end)
sld_dust_id = tools_panel:slider("Dust / Sand", 0.0, 10.0, wx_cur.dust, function(v)
wx_cur.dust = clamp(v, 0.0, 10.0); wx_dirty = true
end)
tools_panel:end_section()
tools_panel:section("Raycast & World Map", true)
tools_panel:button("TP to Crosshair", function()
if not world_ready then
tools_panel:set_text(tools_status_id, "Waiting for world_ready...")
return
end
local hit, why = game.world.raycast(400.0)
if not hit then
local msg = "Raycast blocked: " .. tostring(why)
tools_panel:set_text(tools_status_id, clamp_text(msg))
return bsx.log("world_controller: " .. msg)
end
if not hit.hit then
tools_panel:set_text(tools_status_id, "No terrain hit along crosshair (max 400m)")
return
end
local ok, tp_why = game.world.teleport(hit.x, hit.y, hit.z, nil, true)
if ok then
local msg = string.format("Crosshair TP (%.0fm) -> (%.1f, %.1f, %.1f)", hit.distance or 0, hit.x, hit.y, hit.z)
tools_panel:set_text(tools_status_id, clamp_text(msg))
if bsx.ui.toast then bsx.ui.toast(clamp_text(msg), { level = "info", duration_ms = 2400 }) end
bsx.log("world_controller: " .. msg)
else
local msg = "Crosshair TP failed: " .. tostring(tp_why)
tools_panel:set_text(tools_status_id, clamp_text(msg))
bsx.log("world_controller: " .. msg)
end
end)
map_zoom_info_id = tools_panel:text("Map Zoom: open map once")
map_zoom_slider_id = tools_panel:slider("Map Zoom Scale", 0.2, 4.0, map_zoom_scale, function(value)
map_zoom_scale = clamp(value, 0.2, 4.0)
local ok, why = game.world.set_map_zoom_scale(map_zoom_scale)
if ok then
tools_panel:set_text(tools_status_id, string.format("Map Zoom Scale: %.2fx", map_zoom_scale))
else
tools_panel:set_text(tools_status_id, clamp_text("Map Zoom blocked: " .. tostring(why)))
end
end)
tools_panel:same_line()
tools_panel:button("1.0x", function()
map_zoom_scale = 1.0
tools_panel:set_value(map_zoom_slider_id, 1.0)
local ok, why = game.world.set_map_zoom_scale(1.0)
tools_panel:set_text(tools_status_id, ok and "Map Zoom Scale: 1.00x" or clamp_text("Map Zoom blocked: " .. tostring(why)))
end)
tools_panel:end_section()
tools_panel:section("Quick Menu", true)
tools_panel:combo("Panel", QUICK_PANELS, quick_panel_idx, function(idx)
quick_panel_idx = idx
end)
tools_panel:button("Open Panel", function()
local target = QUICK_PANELS[quick_panel_idx] or "InventoryEquipmentPanel"
local ok, why = game.ui.open_panel(target)
if ok then
local msg = "Queued open panel: " .. target
tools_panel:set_text(tools_status_id, clamp_text(msg))
if bsx.ui.toast then bsx.ui.toast("Opened " .. target, { level = "info", duration_ms = 2000 }) end
bsx.log("world_controller: " .. msg)
else
local msg = "Open Panel blocked: " .. tostring(why)
tools_panel:set_text(tools_status_id, clamp_text(msg))
bsx.log("world_controller: " .. msg)
end
end)
observed_panels_id = tools_panel:text("Observed UI: 0 panel(s)")
tools_panel:end_section()
local function count_saved_waypoints()
local n = 0
for i = 1, #waypoints do
if waypoints[i].set then n = n + 1 end
end
return n
end
bsx.bus.provide("world_controller.state", function()
return {
world_ready = world_ready,
hour = selected_hour,
weather_preset = WEATHER_LABELS[weather_preset_idx] or "Clear",
active_slot = active_slot,
waypoint_count = count_saved_waypoints(),
freeze_time = freeze_time,
world_speed = world_speed,
allow_bus_teleports = allow_bus_teleports,
}
end)
bsx.bus.provide("world_controller.actor_stat", function(req)
if type(req) ~= "table" or type(req.key) ~= "number" or req.key < 1 or req.key ~= math.floor(req.key) then
return { ok = false, reason = "invalid actor_key" }
end
local delta = type(req.delta) == "number" and math.floor(req.delta) or 0
if delta == 0 or delta < -1000000 or delta > 1000000 then
return { ok = false, reason = "invalid delta" }
end
local stat_id = type(req.stat_id) == "number" and math.floor(req.stat_id) or 0
if not (game.stats and game.stats.add_actor) then
return { ok = false, reason = "approval or ServerCommands required" }
end
local ok, why = game.stats.add_actor(req.key, stat_id, delta)
local note = ok
and string.format("Bus actor stat #%u stat=%d delta=%+d", req.key, stat_id, delta)
or ("Actor stat blocked: " .. tostring(why))
panel:set_text(status_id, clamp_text(note))
bsx.log("world_controller: " .. note)
return { ok = ok == true, reason = why }
end)
bsx.bus.on("world_controller.teleport_request", function(msg)
local target_label = (type(msg) == "table" and tostring(msg.label or "bus request")) or "bus request"
if not allow_bus_teleports then
local note = "Bus teleport ignored (Allow Bus Teleports is off)"
panel:set_text(status_id, clamp_text(note))
if bsx.ui and bsx.ui.toast then
bsx.ui.toast("Bus teleport ignored: enable Allow Bus Teleports in World Controller", { level = "warn", duration_ms = 2600 })
end
bsx.bus.emit("world_controller.teleport_result", { ok = false, reason = note, label = target_label })
bsx.log("world_controller: " .. note)
return
end
if type(msg) ~= "table" or not is_finite(msg.x) or not is_finite(msg.y) or not is_finite(msg.z) then
local note = "Bus teleport ignored: invalid coordinates"
panel:set_text(status_id, clamp_text(note))
if bsx.ui and bsx.ui.toast then
bsx.ui.toast(clamp_text(note), { level = "warn", duration_ms = 2600 })
end
bsx.bus.emit("world_controller.teleport_result", { ok = false, reason = note, label = target_label })
bsx.log("world_controller: " .. note)
return
end
local yaw = is_finite(msg.yaw_deg) and msg.yaw_deg or nil
local ok, why = game.world.teleport(msg.x, msg.y, msg.z, yaw)
if not ok then
local note = "Bus teleport blocked: " .. tostring(why)
panel:set_text(status_id, clamp_text(note))
if bsx.ui and bsx.ui.toast then
bsx.ui.toast(clamp_text(note), { level = "warn", duration_ms = 2800 })
end
bsx.bus.emit("world_controller.teleport_result", { ok = false, reason = note, label = target_label })
bsx.log("world_controller: " .. note)
return
end
local note = string.format("Bus teleport queued -> %s", target_label)
panel:set_text(status_id, clamp_text(note))
if bsx.ui and bsx.ui.toast then
bsx.ui.toast(clamp_text("Teleporting to " .. target_label), { level = "info", duration_ms = 2200 })
end
bsx.bus.emit("world_controller.teleport_result", { ok = true, label = target_label })
bsx.bus.emit("world_controller.applied", { kind = "bus_teleport", label = target_label })
bsx.log("world_controller: " .. note)
end)
local function update_pin_status()
local pin = game.world.map_marker()
if pin then
panel:set_text(pin_info_id, clamp_text(string.format("Map Pin: (%.1f, %.1f, %.1f)", pin.x, pin.y, pin.z)))
else
panel:set_text(pin_info_id, "Map Pin: set a marker on the map")
end
local zoom = game.world.map_zoom and game.world.map_zoom()
if zoom then
tools_panel:set_text(map_zoom_info_id, clamp_text(string.format("Map Zoom: %.2f (scale=%.2fx)", zoom, map_zoom_scale)))
end
local obs = game.ui and game.ui.observed_panels and game.ui.observed_panels()
if obs then
tools_panel:set_text(observed_panels_id, clamp_text(string.format("Observed UI: %d panel(s)", #obs)))
end
end
local function try_mark_ready()
if not world_ready or logged_ready then return end
local pos = game.player_position()
if not pos then return end
logged_ready = true
update_pin_status()
local tm = game.time()
local time_str = tm and string.format("%02d:%02d", tm.hour or 0, tm.minute or 0) or "--:--"
local line = string.format("WORLD_CONTROLLER_READY pos=(%.1f,%.1f,%.1f) time=%s",
pos.x, pos.y, pos.z, time_str)
panel:set_text(status_id, clamp_text(string.format("Ready at (%.1f, %.1f, %.1f) | %s",
pos.x, pos.y, pos.z, time_str)))
tools_panel:set_text(tools_status_id, "World Tools: ready")
bsx.log(line)
end
local function on_world_entered()
if world_ready then return end
world_ready = true
load_waypoints()
try_mark_ready()
end
local function sync_world_ready()
if world_ready then return end
local ps = game.player_state()
if ps and ps.world_ready then
on_world_entered()
end
end
bsx.on("world_ready", on_world_entered)
bsx.on("save_load", function()
world_ready = false
logged_ready = false
freeze_countdown = 0
world_speed = 1.0
map_zoom_scale = 1.0
panel:set_value(speed_slider_id, 1.0)
tools_panel:set_value(map_zoom_slider_id, 1.0)
tools_panel:set_text(tools_status_id, "World Tools: waiting for world...")
end)
pcall(bsx.on, "title_screen", function()
world_ready = false
logged_ready = false
freeze_countdown = 0
world_speed = 1.0
map_zoom_scale = 1.0
panel:set_value(speed_slider_id, 1.0)
tools_panel:set_value(map_zoom_slider_id, 1.0)
tools_panel:set_text(tools_status_id, "World Tools: waiting for world...")
end)
local pin_poll_tick = 0
bsx.on("frame", function()
if not logged_panel then
logged_panel = true
bsx.log("world_controller: panel active dev=" .. tostring(bsx.dev_mode))
end
sync_world_ready()
if world_ready and not logged_ready then
try_mark_ready()
end
if world_ready then
if wx_dirty then
local now = os.clock()
if (now - last_wx_push) >= 0.06 then
wx_dirty = false
last_wx_push = now
push_weather_tbl(wx_cur, nil, false)
end
end
pin_poll_tick = pin_poll_tick + 1
if pin_poll_tick >= 60 then
pin_poll_tick = 0
update_pin_status()
end
end
if world_ready and freeze_time then
if freeze_countdown <= 0 then
local hour = math.floor(clamp(selected_hour, 0, 23))
local ok, why = game.world.set_time(hour, 0, 0, same_day)
if not ok then
freeze_time = false
freeze_countdown = 0
if freeze_cb_id then panel:set_value(freeze_cb_id, false) end
local msg = "Freeze Time disabled: " .. tostring(why)
panel:set_text(status_id, clamp_text(msg))
bsx.log("world_controller: " .. msg)
else
freeze_countdown = 60
end
else
freeze_countdown = freeze_countdown - 1
end
end
end)