forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hermit.lua
102 lines (83 loc) · 2.44 KB
/
hermit.lua
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
-- Blocks most types of visitors (caravans, migrants, etc.)
--@ enable=true
--@ module=true
local argparse = require('argparse')
local repeatutil = require('repeat-util')
local GLOBAL_KEY = 'hermit'
local timed_events = df.global.timed_events
local allowlist = {
[df.timed_event_type.WildlifeCurious] = true,
[df.timed_event_type.WildlifeMischievous] = true,
[df.timed_event_type.WildlifeFlier] = true,
}
enabled = enabled or false
function isEnabled()
return enabled
end
local function persist_state()
dfhack.persistent.saveSiteData(GLOBAL_KEY, {enabled=enabled})
end
local function load_state()
local persisted_data = dfhack.persistent.getSiteData(GLOBAL_KEY, {enabled=false})
enabled = persisted_data.enabled
end
function event_loop()
if not enabled then return end
local tmp_events = {} --as:df.timed_event[]
for _, event in pairs(timed_events) do
table.insert(tmp_events, event)
end
timed_events:resize(0)
for _, event in pairs(tmp_events) do
if allowlist[event.type] then
timed_events:insert('#', event)
else
event:delete()
end
end
repeatutil.scheduleUnlessAlreadyScheduled(GLOBAL_KEY, 1, 'days', event_loop)
end
local function print_status()
print(('hermit is currently %s.'):format(enabled and 'enabled' or 'disabled'))
end
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc == SC_MAP_UNLOADED then
enabled = false
return
end
if sc ~= SC_MAP_LOADED or df.global.gamemode ~= df.game_mode.DWARF then
return
end
load_state()
event_loop()
end
if dfhack_flags.module then
return
end
if df.global.gamemode ~= df.game_mode.DWARF or not dfhack.isMapLoaded() then
dfhack.printerr('hermit needs a loaded fortress to work')
return
end
local args, opts = {...}, {}
if dfhack_flags and dfhack_flags.enable then
args = {dfhack_flags.enable_state and 'enable' or 'disable'}
end
local positionals = argparse.processArgsGetopt(args, {
{'h', 'help', handler=function() opts.help = true end},
})
local command = positionals[1]
if command == 'help' or opts.help then
print(dfhack.script_help())
elseif command == 'enable' then
enabled = true
persist_state()
event_loop()
elseif command == 'disable' then
enabled = false
persist_state()
repeatutil.cancel(GLOBAL_KEY)
elseif not command or command == 'status' then
print_status()
else
print(dfhack.script_help())
end