Repository navigation
Expand file tree
/
Copy pathbw_autoload.lua
More file actions
119 lines (101 loc) · 3.62 KB
/
Copy pathbw_autoload.lua
File metadata and controls
119 lines (101 loc) · 3.62 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
--@ module = true
-- bw_autoload.lua
-- Master autoloader for Broken World DFHack scripts.
-- Auto-loads via the DFHack overlay system - no manual init file edits needed.
-- The dummy overlay widget causes DFHack to scan and load this script
-- automatically when the mod is active, which registers the onStateChange
-- handler that enables/disables all mod scripts on map load/unload.
local overlay = require("plugins.overlay")
local CALLBACK_ID = "bw_autoload"
-- List of mod scripts to enable on map load.
local MOD_SCRIPTS = {
"advhunger",
"food-decay",
"roads",
"building",
"furniture",
"equip_monster",
"armaments",
"explode",
"ammo",
"shotgun",
"firecontrol",
"targeting", -- must run before no-friendly-fire so NFF sees redirected targets
"no-friendly-fire",
"flamethrower",
"gasmask",
"maneuver",
"cover",
"gunsound",
}
local last_enable_key = nil
local function get_enable_key()
if not dfhack.isMapLoaded() then return nil end
local wm = df.global.world.map
return ("%d,%d,%d,%d,%d"):format(
wm.region_x, wm.region_y, wm.x_count, wm.y_count, #wm.map_blocks)
end
local function enable_all(reason)
local key = get_enable_key()
if key and key == last_enable_key then
print(("[bw_autoload] Skipping duplicate enable (%s)."):format(reason or "state-change"))
return
end
print(("[bw_autoload] Map loaded (%s) - enabling Broken World scripts..."):format(
reason or "state-change"))
-- Load gun config in this module context and publish to _G so engine scripts
-- (which run in a sandboxed command context) can read it without reqscript.
local ok_cfg = pcall(dfhack.run_command, "config_gun")
if not ok_cfg or not _G.__GunConfig or not _G.__GunConfig._loaded then
dfhack.printerr("[bw_autoload] FATAL: config_gun failed to load")
return
end
for _, script_name in ipairs(MOD_SCRIPTS) do
local ok, err = pcall(dfhack.run_command, script_name, "enable")
if ok then
print(("[bw_autoload] %s: OK"):format(script_name))
else
dfhack.printerr(("[bw_autoload] %s: FAILED - %s"):format(script_name, tostring(err)))
end
end
last_enable_key = key
end
local function disable_all()
for _, script_name in ipairs(MOD_SCRIPTS) do
pcall(dfhack.run_command, script_name, "disable")
end
end
-- Register the state change handler.
-- SC_MAP_LOADED covers fortress mode embark.
-- SC_WORLD_LOADED covers adventure mode and arena mode, which do not
-- reliably fire SC_MAP_LOADED.
dfhack.onStateChange[CALLBACK_ID] = function(sc)
if sc == SC_MAP_LOADED or sc == SC_WORLD_LOADED then
enable_all(tostring(sc))
elseif sc == SC_MAP_UNLOADED or sc == SC_WORLD_UNLOADED then
last_enable_key = nil
disable_all()
end
end
-- If a map is already loaded when this script is first scanned, enable now.
if dfhack.isMapLoaded() then
enable_all("startup")
end
-- Dummy overlay widget - forces DFHack's overlay plugin to auto-load this
-- script when the mod is active. It renders nothing and is disabled by default.
BwBootstrap = defclass(BwBootstrap, overlay.OverlayWidget)
BwBootstrap.ATTRS{
desc = "Broken World autoloader (hidden bootstrap)",
default_pos = {x=-1, y=-1},
default_enabled = true,
viewscreens = "title",
}
function BwBootstrap:init() end
function BwBootstrap:render() end
OVERLAY_WIDGETS = {
bootstrap = BwBootstrap,
}
if not dfhack_flags.module then
-- Manual invocation: just print status
print("[bw_autoload] Registered - Broken World scripts will auto-enable on map load.")
end