@@ -7,15 +7,30 @@ local utils = require('utils')
77local GLOBAL_KEY = " eggwatch"
88local EVENT_FREQ = 7
99local print_prefix = " eggwatch: "
10+ local default_table = {10 , false , false }
11+ local stringtoboolean = { [" true" ]= true , [" false" ]= false , [" 1" ] = true , [" 0" ] = false , [" Y" ] = true , [" N" ] = false }
1012
11- local default_table = {}
12- default_table .DEFAULT = {10 , false , false }
13+ function dump (o )
14+ if type (o ) == " table" then
15+ local s = " { "
16+ for k , v in pairs (o ) do
17+ if type (k ) ~= " number" then
18+ k = ' "' .. k .. ' "'
19+ end
20+ s = s .. " [" .. k .. " ] = " .. dump (v ) .. " ,"
21+ end
22+ return s .. " } "
23+ else
24+ return tostring (o )
25+ end
26+ end
1327
1428local function get_default_state ()
1529 return {
1630 enabled = false ,
1731 verbose = false ,
18- target_eggs_count_per_race = default_table
32+ default = default_table ,
33+ target_eggs_count_per_race = {}
1934 }
2035end
2136
3247local function handle_error (text )
3348 qerror (text )
3449end
35-
50+ local function format_target_count_row (header , row )
51+ return header .. ' : ' .. ' target count: ' .. row [1 ] .. ' ; count children: ' .. tostring (row [2 ]) .. ' ; count adults: ' .. tostring (row [3 ])
52+ end
3653local function print_status ()
3754 print_local ((" eggwatch is currently %s." ):format (state .enabled and " enabled" or " disabled" ))
55+ print_local (format_target_count_row (' Default' , state .default ))
56+ if state .target_eggs_count_per_race ~= nil then
57+ for k , v in pairs (state .target_eggs_count_per_race ) do
58+ print_local (format_target_count_row (df .global .world .raws .creatures .all [k ].creature_id , v ))
59+ end
60+ end
3861 if state .verbose then
3962 print_local (" eggwatch is in verbose mode" )
4063 end
@@ -46,15 +69,39 @@ local function print_detalis(details)
4669 end
4770end
4871
72+
4973local function persist_state ()
50- dfhack .persistent .saveSiteData (GLOBAL_KEY , state )
74+ local state_to_persist = {}
75+ state_to_persist .enabled = state .enabled
76+ state_to_persist .verbose = state .verbose
77+ state_to_persist .default = state .default
78+ state_to_persist .target_eggs_count_per_race = {}
79+ if state .target_eggs_count_per_race ~= nil then
80+ for k , v in pairs (state .target_eggs_count_per_race ) do
81+ state_to_persist .target_eggs_count_per_race [tostring (k )]= v
82+ end
83+ end
84+ dfhack .persistent .saveSiteData (GLOBAL_KEY , state_to_persist )
5185end
5286
5387--- Load the saved state of the script
5488local function load_state ()
5589 -- load persistent data
56- state = get_default_state ()
57- utils .assign (state , dfhack .persistent .getSiteData (GLOBAL_KEY , state ))
90+ local persisted_data = dfhack .persistent .getSiteData (GLOBAL_KEY , persisted_data )
91+ state = {}
92+ if persisted_data ~= nil then
93+ state .enabled = persisted_data .enabled
94+ state .verbose = persisted_data .verbose
95+ state .default = persisted_data .default
96+ state .target_eggs_count_per_race = {}
97+ if persisted_data .target_eggs_count_per_race ~= nil then
98+ for k , v in pairs (persisted_data .target_eggs_count_per_race ) do
99+ state .target_eggs_count_per_race [tonumber (k )]= v
100+ end
101+ end
102+ else
103+ state = get_default_state ()
104+ end
58105end
59106
60107local function update_event_listener ()
143190-- dfhack.items.moveToContainer(created_egg_stack, find_current_nestbox(original_eggs))
144191-- end
145192
146- local function count_forbidden_eggs_for_race_in_claimed_nestobxes (race_creature_id )
193+ local function count_forbidden_eggs_for_race_in_claimed_nestobxes (race )
147194 print_detalis ((" start count_forbidden_eggs_for_race_in_claimed_nestobxes" ))
148195 local eggs_count = 0
149196 for _ , nestbox in ipairs (df .global .world .buildings .other .NEST_BOX ) do
@@ -156,8 +203,8 @@ local function count_forbidden_eggs_for_race_in_claimed_nestobxes(race_creature_
156203 print_detalis ((" Found claimed nextbox containing items that are eggs" ))
157204 if nestbox_contained_item .item .egg_flags .fertile and nestbox_contained_item .item .flags .forbid then
158205 print_detalis ((" Eggs are fertile and forbidden" ))
159- if df . creature_raw . find ( nestbox_contained_item .item .race ). creature_id == race_creature_id then
160- print_detalis ((" Eggs belong to %s" ):format (race_creature_id ))
206+ if nestbox_contained_item .item .race == race then
207+ print_detalis ((" Eggs belong to %s" ):format (race ))
161208 print_detalis (
162209 (" eggs_count %s + new %s" ):format (
163210 eggs_count ,
@@ -177,16 +224,16 @@ local function count_forbidden_eggs_for_race_in_claimed_nestobxes(race_creature_
177224 return eggs_count
178225end
179226
180- local function get_config_for_race (race_creature_id )
181- print_detalis ((" getting config for race %s " ):format (race_creature_id ))
227+ local function get_config_for_race (race )
228+ print_detalis ((" getting config for race %s " ):format (race ))
182229 for k , v in pairs (state .target_eggs_count_per_race ) do
183- if k == race_creature_id then
230+ if k == race then
184231 return v
185232 end
186233 end
187- state .target_eggs_count_per_race [race_creature_id ] = state .target_eggs_count_per_race . DEFAULT
234+ state .target_eggs_count_per_race [race ] = state .default
188235 persist_state ()
189- return state .target_eggs_count_per_race [race_creature_id ]
236+ return state .target_eggs_count_per_race [race ]
190237end
191238
192239local function is_valid_animal (unit )
@@ -198,17 +245,17 @@ local function is_valid_animal(unit)
198245 not dfhack .units .isDead (unit )
199246end
200247
201- local function count_live_animals (race_creature_id , count_children , count_adults )
202- if count_adults then print_detalis ((' we are counting adults for %s' ):format (race_creature_id )) end
203- if count_children then print_detalis ((' we are counting children and babies for %s' ):format (race_creature_id )) end
248+ local function count_live_animals (race , count_children , count_adults )
249+ if count_adults then print_detalis ((' we are counting adults for %s' ):format (race )) end
250+ if count_children then print_detalis ((' we are counting children and babies for %s' ):format (race )) end
204251
205252 local count = 0
206253 if not count_adults and not count_children then
207254 return count
208255 end
209256
210257 for _ ,unit in ipairs (df .global .world .units .active ) do
211- if race_creature_id == df . creature_raw . find ( unit .race ). creature_id
258+ if race == unit .race
212259 and is_valid_animal (unit )
213260 and ( (count_adults and dfhack .units .isAdult (unit ))
214261 or (count_children and ( dfhack .units .isChild (unit ) or dfhack .units .isBaby (unit )))
@@ -227,8 +274,8 @@ local function handle_eggs(eggs)
227274 return
228275 end
229276
230- local race_creature_id = df . creature_raw . find ( eggs .race ). creature_id
231- local race_config = get_config_for_race (race_creature_id )
277+ local race = eggs .race
278+ local race_config = get_config_for_race (race )
232279 local max_eggs = race_config [1 ]
233280 local count_children = race_config [2 ]
234281 local count_adults = race_config [3 ]
@@ -240,17 +287,17 @@ local function handle_eggs(eggs)
240287 local current_eggs = eggs .stack_size
241288
242289 local total_count = current_eggs
243- total_count = total_count + count_forbidden_eggs_for_race_in_claimed_nestobxes (race_creature_id )
290+ total_count = total_count + count_forbidden_eggs_for_race_in_claimed_nestobxes (race )
244291
245292 if total_count - current_eggs < max_eggs then
246- print_detalis ((" Total count for %s only existing eggs is %s, about to count life animals if enabled" ):format (race_creature_id , total_count - current_eggs ))
247- total_count = total_count + count_live_animals (race_creature_id , count_children , count_adults )
293+ print_detalis ((" Total count for %s only existing eggs is %s, about to count life animals if enabled" ):format (race , total_count - current_eggs ))
294+ total_count = total_count + count_live_animals (race , count_children , count_adults )
248295 else
249- print_detalis ((" Total count for %s eggs only is %s greater than maximum %s, no need to count life animals" ):format (race_creature_id , total_count , max_eggs ))
296+ print_detalis ((" Total count for %s eggs only is %s greater than maximum %s, no need to count life animals" ):format (race , total_count , max_eggs ))
250297 return
251298 end
252299
253- print_detalis ((" Total count for %s eggs is %s" ):format (race_creature_id , total_count ))
300+ print_detalis ((" Total count for %s eggs is %s" ):format (race , total_count ))
254301
255302 if total_count - current_eggs < max_eggs then
256303 -- ###if possible split egg stack to forbid only part below max change previous condition to total_count < max_eggs
@@ -261,11 +308,11 @@ local function handle_eggs(eggs)
261308 -- create_new_egg_stack(eggs, remaining_eggs, df.creature_raw.find(eggs.race), race_creature.caste[eggs.caste])
262309 -- eggs.stack_size = forbid_eggs
263310 -- eggs.flags.forbid = true
264- -- print(('Total count for %s eggs is %s over maximum %s , forbidden %s eggs out of clutch of %s.'):format(race_creature_id , total_count, max_eggs, forbid_eggs, current_eggs))
311+ -- print(('Total count for %s eggs is %s over maximum %s , forbidden %s eggs out of clutch of %s.'):format(race , total_count, max_eggs, forbid_eggs, current_eggs))
265312 eggs .flags .forbid = true
266313 print_local (
267314 (" Previously existing %s eggs is %s lower than maximum %s , forbidden %s new eggs." ):format (
268- race_creature_id ,
315+ race ,
269316 total_count - current_eggs ,
270317 max_eggs ,
271318 current_eggs
@@ -274,7 +321,7 @@ local function handle_eggs(eggs)
274321 else
275322 print_local (
276323 (" Total count for %s eggs is %s over maximum %s, newly laid eggs %s , no action taken." ):format (
277- race_creature_id ,
324+ race ,
278325 total_count ,
279326 max_eggs ,
280327 current_eggs
@@ -297,45 +344,34 @@ end
297344local function validate_creature_id (creature_id )
298345 for i , c in ipairs (df .global .world .raws .creatures .all ) do
299346 if c .creature_id == creature_id then
300- return true
347+ return i
301348 end
302349 end
303- return false
350+ return - 1
304351end
305352
306353local function set_target (target_race , target_count , count_children , count_adult )
307- local stringtoboolean = { [" true" ]= true , [" false" ]= false , [" 1" ] = true , [" 0" ] = false , [" Y" ] = true , [" N" ] = false }
308354
309355 if target_race == nil or target_race == " " then
310356 handle_error (' must specify "DEFAULT" or valid creature_id' )
311357 end
358+
312359 local target_race_upper = string.upper (target_race )
360+
313361 if tonumber (target_count ) == nil or tonumber (target_count ) < 0 then
314362 handle_error (" No valid target count specified" )
315363 end
316- if target_race_upper == " DEFAULT" or validate_creature_id (target_race_upper ) then
317- state .target_eggs_count_per_race [target_race_upper ] = {tonumber (target_count ), stringtoboolean [count_children ] or false , stringtoboolean [count_adult ] or false }
364+ local race = validate_creature_id (target_race_upper )
365+ if target_race_upper == " DEFAULT" then
366+ state .default = {tonumber (target_count ), stringtoboolean [count_children ] or false , stringtoboolean [count_adult ] or false }
367+ elseif race >= 0 then
368+ print (race )
369+ state .target_eggs_count_per_race [race ] = {tonumber (target_count ), stringtoboolean [count_children ] or false , stringtoboolean [count_adult ] or false }
318370 else
319371 handle_error (' must specify "DEFAULT" or valid creature_id' )
320372 end
321-
322- print_local (dump (state .target_eggs_count_per_race ))
323373end
324374
325- function dump (o )
326- if type (o ) == " table" then
327- local s = " { "
328- for k , v in pairs (o ) do
329- if type (k ) ~= " number" then
330- k = ' "' .. k .. ' "'
331- end
332- s = s .. " [" .. k .. " ] = " .. dump (v ) .. " ,"
333- end
334- return s .. " } "
335- else
336- return tostring (o )
337- end
338- end
339375
340376if df .global .gamemode ~= df .game_mode .DWARF or not dfhack .isMapLoaded () then
341377 dfhack .printerr (" eggwatch needs a loaded fortress to work" )
@@ -379,16 +415,10 @@ elseif command == "verbose" then
379415 state .verbose = not state .verbose
380416 print_status ()
381417elseif command == ' clear' then
382- state .target_eggs_count_per_race = default_table
383- elseif command == ' hardreset' then
384418 state = get_default_state ()
385419 update_event_listener ()
386420elseif not command or command == " status" then
387421 print_status ()
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 ))
392422elseif (command ~= ' enable' or command ~= ' disable' ) and not dfhack_flags .enable then
393423 handle_error ((' Command "%s" is not recognized' ):format (command ))
394424end
0 commit comments