Skip to content

Commit cfdbd70

Browse files
committed
add local state variable and move persisted variables inside, add dfhack.onStateChange hook
1 parent 6803271 commit cfdbd70

File tree

1 file changed

+94
-57
lines changed

1 file changed

+94
-57
lines changed

eggwatch.lua

Lines changed: 94 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
1+
--@enable = true
2+
--@module = true
13
local argparse = require("argparse")
24
local eventful = require("plugins.eventful")
5+
local utils = require('utils')
36

47
local GLOBAL_KEY = "eggwatch"
58
local EVENT_FREQ = 7
69
local print_prefix = "eggwatch: "
710

8-
enabled = enabled or false
9-
default_table = {}
11+
local default_table = {}
1012
default_table.DEFAULT = {10, false, false}
11-
function isEnabled()
12-
return enabled
13-
end
1413

15-
local function persist_state()
16-
dfhack.persistent.saveSiteData(
17-
GLOBAL_KEY,
18-
{
19-
enabled = enabled,
20-
verbose = verbose,
21-
target_eggs_count_per_race = target_eggs_count_per_race
22-
}
23-
)
14+
local function get_default_state()
15+
return {
16+
enabled = false,
17+
verbose = false,
18+
target_eggs_count_per_race = default_table
19+
}
2420
end
2521

26-
--- Load the saved state of the script
27-
local function load_state()
28-
-- load persistent data
29-
local persisted_data = dfhack.persistent.getSiteData(GLOBAL_KEY, {})
30-
enabled = persisted_data.enabled or false
31-
verbose = persisted_data.verbose or false
32-
target_eggs_count_per_race = persisted_data.target_eggs_count_per_race or default_table
33-
end
22+
local state = state or get_default_state()
3423

35-
if dfhack_flags.module then
36-
return
24+
function isEnabled()
25+
return state.enabled
3726
end
3827

3928
local function print_local(text)
@@ -45,18 +34,67 @@ local function handle_error(text)
4534
end
4635

4736
local function print_status()
48-
print_local(("eggwatch is currently %s."):format(enabled and "enabled" or "disabled"))
49-
if verbose then
37+
print_local(("eggwatch is currently %s."):format(state.enabled and "enabled" or "disabled"))
38+
if state.verbose then
5039
print_local("eggwatch is in verbose mode")
5140
end
5241
end
5342

5443
local function print_detalis(details)
55-
if verbose then
44+
if state.verbose then
5645
print_local(details)
5746
end
5847
end
5948

49+
local function persist_state()
50+
dfhack.persistent.saveSiteData(GLOBAL_KEY, state)
51+
end
52+
53+
--- Load the saved state of the script
54+
local function load_state()
55+
-- load persistent data
56+
state = get_default_state()
57+
utils.assign(state, dfhack.persistent.getSiteData(GLOBAL_KEY, state))
58+
end
59+
60+
local function update_event_listener()
61+
if isEnabled() then
62+
eventful.enableEvent(eventful.eventType.ITEM_CREATED, EVENT_FREQ)
63+
eventful.onItemCreated[GLOBAL_KEY] = check_item_created
64+
print_local(("Subscribing in eventful for %s with frequency %s"):format("ITEM_CREATED", EVENT_FREQ))
65+
else
66+
eventful.onItemCreated[GLOBAL_KEY] = nil
67+
print_local(("Unregistering from eventful for %s"):format("ITEM_CREATED"))
68+
end
69+
end
70+
71+
local function do_enable()
72+
state.enabled = true
73+
update_event_listener()
74+
end
75+
76+
local function do_disable()
77+
state.enabled = false
78+
update_event_listener()
79+
end
80+
81+
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
82+
if sc == SC_MAP_UNLOADED then
83+
do_disable()
84+
return
85+
end
86+
if sc ~= SC_MAP_LOADED or df.global.gamemode ~= df.game_mode.DWARF then
87+
return
88+
end
89+
load_state()
90+
print_status()
91+
update_event_listener()
92+
end
93+
94+
if dfhack_flags.module then
95+
return
96+
end
97+
6098
local function is_egg(item)
6199
return df.item_type.EGG == item:getType()
62100
end
@@ -141,14 +179,14 @@ end
141179

142180
local function get_config_for_race(race_creature_id)
143181
print_detalis(("getting config for race %s "):format(race_creature_id))
144-
for k, v in pairs(target_eggs_count_per_race) do
182+
for k, v in pairs(state.target_eggs_count_per_race) do
145183
if k == race_creature_id then
146184
return v
147185
end
148186
end
149-
target_eggs_count_per_race[race_creature_id] = target_eggs_count_per_race.DEFAULT
187+
state.target_eggs_count_per_race[race_creature_id] = state.target_eggs_count_per_race.DEFAULT
150188
persist_state()
151-
return target_eggs_count_per_race[race_creature_id]
189+
return state.target_eggs_count_per_race[race_creature_id]
152190
end
153191

154192
local function is_valid_animal(unit)
@@ -247,25 +285,15 @@ local function handle_eggs(eggs)
247285
print_detalis(("end handle_eggs"))
248286
end
249287

250-
local function check_item_created(item_id)
288+
function check_item_created(item_id)
289+
251290
local item = df.item.find(item_id)
252291
if not item or not is_egg(item) then
253292
return
254293
end
255294
handle_eggs(item)
256295
end
257296

258-
local function do_enable()
259-
enabled = true
260-
eventful.enableEvent(eventful.eventType.ITEM_CREATED, EVENT_FREQ)
261-
eventful.onItemCreated[GLOBAL_KEY] = check_item_created
262-
end
263-
264-
local function do_disable()
265-
enabled = false
266-
eventful.onItemCreated[GLOBAL_KEY] = nil
267-
end
268-
269297
local function validate_creature_id(creature_id)
270298
for i, c in ipairs(df.global.world.raws.creatures.all) do
271299
if c.creature_id == creature_id then
@@ -286,12 +314,12 @@ local stringtoboolean={ ["true"]=true, ["false"]=false, ["1"] = true , ["0"] =
286314
handle_error("No valid target count specified")
287315
end
288316
if target_race_upper == "DEFAULT" or validate_creature_id(target_race_upper) then
289-
target_eggs_count_per_race[target_race_upper] = {tonumber(target_count), stringtoboolean[count_children] or false, stringtoboolean[count_adult] or false}
317+
state.target_eggs_count_per_race[target_race_upper] = {tonumber(target_count), stringtoboolean[count_children] or false, stringtoboolean[count_adult] or false}
290318
else
291319
handle_error('must specify "DEFAULT" or valid creature_id')
292320
end
293321

294-
print_local(dump(target_eggs_count_per_race))
322+
print_local(dump(state.target_eggs_count_per_race))
295323
end
296324

297325
function dump(o)
@@ -314,6 +342,7 @@ if df.global.gamemode ~= df.game_mode.DWARF or not dfhack.isMapLoaded() then
314342
return
315343
end
316344

345+
load_state()
317346
local args, opts = {...}, {}
318347
if dfhack_flags and dfhack_flags.enable then
319348
args = {dfhack_flags.enable_state and "enable" or "disable"}
@@ -329,30 +358,38 @@ local positionals =
329358
}
330359
)
331360

332-
load_state()
361+
if dfhack_flags.enable then
362+
if dfhack_flags.enable_state then
363+
do_enable()
364+
print_status()
365+
else
366+
do_disable()
367+
print_status()
368+
end
369+
end
370+
333371
local command = positionals[1]
334372

335373
if command == "help" or opts.help then
336374
print(dfhack.script_help())
337-
elseif command == "enable" then
338-
do_enable()
339-
print_status()
340-
elseif command == "disable" then
341-
do_disable()
342-
print_status()
343375
elseif command == "target" then
344376
set_target(positionals[2], positionals[3], positionals[4], positionals[5])
345377
print_status()
346378
elseif command == "verbose" then
347-
verbose = not verbose
379+
state.verbose = not state.verbose
348380
print_status()
349381
elseif command == 'clear' then
350-
target_eggs_count_per_race = default_table
351-
382+
state.target_eggs_count_per_race = default_table
383+
elseif command == 'hardreset' then
384+
state = get_default_state()
385+
update_event_listener()
352386
elseif not command or command == "status" then
353387
print_status()
354-
print_local(dump(target_eggs_count_per_race))
355-
else
388+
-- print_local(dump(state.enabled))
389+
-- print_local(dump(state.verbose))
390+
-- print_local(dump(state))
391+
print_local(dump(state.target_eggs_count_per_race))
392+
elseif (command ~= 'enable' or command ~= 'disable') and not dfhack_flags.enable then
356393
handle_error(('Command "%s" is not recognized'):format(command))
357394
end
358-
persist_state()
395+
persist_state()

0 commit comments

Comments
 (0)