@@ -6,7 +6,7 @@ local utils = require("utils")
66
77local GLOBAL_KEY = " eggwatch"
88local default_table = {10 , false , false }
9- local stringtoboolean = { [" true" ]= true , [" false" ]= false , [" 1" ] = true , [" 0" ] = false , [" Y" ] = true , [" N" ] = false }
9+ local stringtoboolean = { [" true" ] = true , [" false" ] = false , [" 1" ] = true , [" 0" ] = false , [" Y" ] = true , [" N" ] = false }
1010
1111function dump (o )
1212 if type (o ) == " table" then
@@ -41,7 +41,7 @@ function isEnabled()
4141end
4242
4343local function print_local (text )
44- print (GLOBAL_KEY .. " : " .. text )
44+ print (GLOBAL_KEY .. " : " .. text )
4545end
4646
4747local function handle_error (text )
@@ -53,33 +53,33 @@ local function print_details(details)
5353 print_local (details )
5454 end
5555end
56- local function format_target_count_row (header , row )
57- return header .. " : " .. " target count: " .. row [1 ] .. " ; count children: " .. tostring (row [2 ]) .. " ; count adults: " .. tostring (row [3 ])
56+ local function format_target_count_row (header , row )
57+ return header ..
58+ " : " ..
59+ " target count: " ..
60+ row [1 ] .. " ; count children: " .. tostring (row [2 ]) .. " ; count adults: " .. tostring (row [3 ])
5861end
5962local function print_status ()
6063 print_local ((" eggwatch is currently %s." ):format (state .enabled and " enabled" or " disabled" ))
61- print_local ((" egg stack splitting is %s" ):format (state .split_stacks and " enabled" or " disabled" ))
64+ print_local ((" egg stack splitting is %s" ):format (state .split_stacks and " enabled" or " disabled" ))
6265 print_local (format_target_count_row (" Default" , state .default ))
6366 if state .target_eggs_count_per_race ~= nil then
64- for k , v in pairs (state .target_eggs_count_per_race ) do
65- print_local (format_target_count_row (df .global .world .raws .creatures .all [k ].creature_id , v ))
66- end
67+ for k , v in pairs (state .target_eggs_count_per_race ) do
68+ print_local (format_target_count_row (df .global .world .raws .creatures .all [k ].creature_id , v ))
69+ end
6770 end
68- print_details (" eggwatch is in verbose mode" )
69- print_details (dump (state ))
70-
71+ print_details (" eggwatch is in verbose mode" )
72+ print_details (dump (state ))
7173end
7274
73-
74-
7575local function persist_state ()
7676 print_details ((" start load_state" ))
7777 local state_to_persist = {}
7878 state_to_persist = utils .clone (state )
7979 state_to_persist .target_eggs_count_per_race = {}
8080 if state .target_eggs_count_per_race ~= nil then
8181 for k , v in pairs (state .target_eggs_count_per_race ) do
82- state_to_persist .target_eggs_count_per_race [tostring (k )]= v
82+ state_to_persist .target_eggs_count_per_race [tostring (k )] = v
8383 end
8484 end
8585 dfhack .persistent .saveSiteData (GLOBAL_KEY , state_to_persist )
@@ -91,13 +91,13 @@ local function load_state()
9191 print_details ((" start load_state" ))
9292 -- load persistent data
9393 local persisted_data = dfhack .persistent .getSiteData (GLOBAL_KEY , {})
94- local processed_persisted_data = {}
95- if persisted_data ~= nil then
94+ local processed_persisted_data = {}
95+ if persisted_data ~= nil then
9696 processed_persisted_data = utils .clone (persisted_data )
9797 processed_persisted_data .target_eggs_count_per_race = {}
9898 if persisted_data .target_eggs_count_per_race ~= nil then
9999 for k , v in pairs (persisted_data .target_eggs_count_per_race ) do
100- processed_persisted_data .target_eggs_count_per_race [tonumber (k )]= v
100+ processed_persisted_data .target_eggs_count_per_race [tonumber (k )] = v
101101 end
102102 end
103103 end
@@ -165,9 +165,9 @@ local function copy_egg_fields(source_egg, target_egg)
165165 target_egg .hatchling_flags4 = utils .clone (source_egg .hatchling_flags4 , true )
166166 print_details (" flags done" )
167167 target_egg .hatchling_training_level = utils .clone (source_egg .hatchling_training_level , true )
168- utils .assign (target_egg .hatchling_animal_population ,source_egg .hatchling_animal_population )
168+ utils .assign (target_egg .hatchling_animal_population , source_egg .hatchling_animal_population )
169169 print_details (" hatchling_animal_population done" )
170- target_egg .hatchling_mother_id = source_egg .hatchling_mother_id
170+ target_egg .hatchling_mother_id = source_egg .hatchling_mother_id
171171 print_details (" hatchling_mother_id done" )
172172 target_egg .mother_hf = source_egg .mother_hf
173173 target_egg .father_hf = source_egg .mother_hf
@@ -181,25 +181,32 @@ local function copy_egg_fields(source_egg, target_egg)
181181 target_egg .hatchling_civ_id = source_egg .hatchling_civ_id
182182 print_details (" hatchling_civ_id done" )
183183 print_details (" end copy_egg_fields" )
184- end
184+ end
185185
186- local function resize_egg_stack (egg_stack , new_stack_size )
186+ local function resize_egg_stack (egg_stack , new_stack_size )
187187 print_details (" start resize_egg_stack" )
188- egg_stack .stack_size = new_stack_size
188+ egg_stack .stack_size = new_stack_size
189189 -- TODO check if weight or size need adjustment
190190 print_details (" end resize_egg_stack" )
191- end
191+ end
192192
193- local function create_new_egg_stack (original_eggs , remaining_eggs )
193+ local function create_new_egg_stack (original_eggs , remaining_eggs )
194194 print_details (" start create_new_egg_stack" )
195195
196196 print_details (" about to split create new egg stack" )
197197 print_details ((" type= %s" ):format (original_eggs :getType ()))
198- print_details ((" creature= %s" ):format ( original_eggs .race ))
198+ print_details ((" creature= %s" ):format (original_eggs .race ))
199199 print_details ((" caste= %s " ):format (original_eggs .caste ))
200- print_details (remaining_eggs )
201-
202- local created_items = dfhack .items .createItem (df .unit .find (original_eggs .hatchling_mother_id ), original_eggs :getType (), - 1 , original_eggs .race , original_eggs .caste )
200+ print_details (remaining_eggs )
201+
202+ local created_items =
203+ dfhack .items .createItem (
204+ df .unit .find (original_eggs .hatchling_mother_id ),
205+ original_eggs :getType (),
206+ - 1 ,
207+ original_eggs .race ,
208+ original_eggs .caste
209+ )
203210 print_details (" created new egg stack" )
204211 local created_egg_stack = created_items [0 ] or created_items [1 ]
205212 print_details (df .creature_raw .find (created_egg_stack .race ).creature_id )
@@ -218,14 +225,14 @@ local function create_new_egg_stack (original_eggs, remaining_eggs)
218225 print_details (" end create_new_egg_stack" )
219226end
220227
221- local function split_egg_stack (source_egg_stack , to_be_left_in_source_stack )
228+ local function split_egg_stack (source_egg_stack , to_be_left_in_source_stack )
222229 print_details (" start split_egg_stack" )
223230 local egg_count_in_new_stack_size = source_egg_stack .stack_size - to_be_left_in_source_stack
224231 if egg_count_in_new_stack_size > 0 then
225- create_new_egg_stack (source_egg_stack , egg_count_in_new_stack_size )
226- resize_egg_stack (source_egg_stack , to_be_left_in_source_stack )
232+ create_new_egg_stack (source_egg_stack , egg_count_in_new_stack_size )
233+ resize_egg_stack (source_egg_stack , to_be_left_in_source_stack )
227234 else
228- print_details (" nothing to do, wrong egg_count_in_new_stack_size" )
235+ print_details (" nothing to do, wrong egg_count_in_new_stack_size" )
229236 end
230237 print_details (" end split_egg_stack" )
231238end
@@ -280,31 +287,32 @@ local function get_config_for_race(race)
280287end
281288
282289local function is_valid_animal (unit )
283- return unit and
284- dfhack .units .isActive (unit ) and
285- dfhack .units .isAnimal (unit ) and
286- dfhack .units .isFortControlled (unit ) and
290+ return unit and dfhack .units .isActive (unit ) and dfhack .units .isAnimal (unit ) and dfhack .units .isFortControlled (unit ) and
287291 dfhack .units .isTame (unit ) and
288292 not dfhack .units .isDead (unit )
289293end
290294
291295local function count_live_animals (race , count_children , count_adults )
292296 print_details ((" start count_live_animals" ))
293- if count_adults then print_details ((" we are counting adults for %s" ):format (race )) end
294- if count_children then print_details ((" we are counting children and babies for %s" ):format (race )) end
297+ if count_adults then
298+ print_details ((" we are counting adults for %s" ):format (race ))
299+ end
300+ if count_children then
301+ print_details ((" we are counting children and babies for %s" ):format (race ))
302+ end
295303
296304 local count = 0
297305 if not count_adults and not count_children then
298306 print_details ((" end 1 count_live_animals" ))
299307 return count
300308 end
301309
302- for _ ,unit in ipairs (df .global .world .units .active ) do
303- if race == unit . race
304- and is_valid_animal (unit )
305- and ( ( count_adults and dfhack .units .isAdult (unit ))
306- or (count_children and ( dfhack .units .isChild (unit ) or dfhack .units .isBaby (unit )))
307- ) then
310+ for _ , unit in ipairs (df .global .world .units .active ) do
311+ if
312+ race == unit . race and is_valid_animal (unit ) and
313+ (( count_adults and dfhack .units .isAdult (unit )) or
314+ (count_children and (dfhack .units .isChild (unit ) or dfhack .units .isBaby (unit ) )))
315+ then
308316 count = count + 1
309317 end
310318 end
@@ -336,10 +344,21 @@ local function handle_eggs(eggs)
336344 total_count = total_count + count_forbidden_eggs_for_race_in_claimed_nestobxes (race )
337345
338346 if total_count - current_eggs < max_eggs then
339- print_details ((" Total count for %s only existing eggs is %s, about to count life animals if enabled" ):format (race , total_count - current_eggs ))
347+ print_details (
348+ (" Total count for %s only existing eggs is %s, about to count life animals if enabled" ):format (
349+ race ,
350+ total_count - current_eggs
351+ )
352+ )
340353 total_count = total_count + count_live_animals (race , count_children , count_adults )
341354 else
342- print_details ((" Total count for %s eggs only is %s greater than maximum %s, no need to count life animals" ):format (race , total_count , max_eggs ))
355+ print_details (
356+ (" Total count for %s eggs only is %s greater than maximum %s, no need to count life animals" ):format (
357+ race ,
358+ total_count ,
359+ max_eggs
360+ )
361+ )
343362 print_details ((" end 1 handle_eggs" ))
344363 return
345364 end
@@ -348,8 +367,8 @@ local function handle_eggs(eggs)
348367
349368 if total_count - current_eggs < max_eggs then
350369 if state .split_stacks and total_count > max_eggs then
351- local egg_count_to_leave_in_source_stack = max_eggs - total_count + current_eggs
352- split_egg_stack (eggs , egg_count_to_leave_in_source_stack )
370+ local egg_count_to_leave_in_source_stack = max_eggs - total_count + current_eggs
371+ split_egg_stack (eggs , egg_count_to_leave_in_source_stack )
353372 end
354373
355374 eggs .flags .forbid = true
@@ -378,7 +397,7 @@ function check_item_created(item_id)
378397 -- print_details(("start check_item_created"))
379398 local item = df .item .find (item_id )
380399 if not item or not is_egg (item ) then
381- -- print_details(("end 1 check_item_created"))
400+ -- print_details(("end 1 check_item_created"))
382401 return
383402 end
384403 -- print_local(("item_id for original eggs: %s"):format (item_id))
@@ -412,17 +431,24 @@ local function set_target(target_race, target_count, count_children, count_adult
412431 end
413432 local race = validate_creature_id (target_race_upper )
414433 if target_race_upper == " DEFAULT" then
415- state .default = {tonumber (target_count ), stringtoboolean [count_children ] or false , stringtoboolean [count_adult ] or false }
434+ state .default = {
435+ tonumber (target_count ),
436+ stringtoboolean [count_children ] or false ,
437+ stringtoboolean [count_adult ] or false
438+ }
416439 elseif race >= 0 then
417- print (race )
418- state .target_eggs_count_per_race [race ] = {tonumber (target_count ), stringtoboolean [count_children ] or false , stringtoboolean [count_adult ] or false }
440+ print (race )
441+ state .target_eggs_count_per_race [race ] = {
442+ tonumber (target_count ),
443+ stringtoboolean [count_children ] or false ,
444+ stringtoboolean [count_adult ] or false
445+ }
419446 else
420447 handle_error (" must specify DEFAULT or valid creature_id" )
421448 end
422449 print_details ((" end set_target" ))
423450end
424451
425-
426452if df .global .gamemode ~= df .game_mode .DWARF or not dfhack .isMapLoaded () then
427453 dfhack .printerr (" eggwatch needs a loaded fortress to work" )
428454 return
@@ -438,17 +464,21 @@ local positionals =
438464 argparse .processArgsGetopt (
439465 args ,
440466 {
441- {" h" , " help" , handler = function ()
467+ {
468+ " h" ,
469+ " help" ,
470+ handler = function ()
442471 opts .help = true
443- end }
472+ end
473+ }
444474 }
445475)
446476
447477if dfhack_flags .enable then
448478 if dfhack_flags .enable_state then
449479 do_enable ()
450480 print_status ()
451- else
481+ else
452482 do_disable ()
453483 print_status ()
454484 end
@@ -459,7 +489,7 @@ local command = positionals[1]
459489if command == " help" or opts .help then
460490 print (dfhack .script_help ())
461491elseif command == " target" then
462- set_target (positionals [2 ], positionals [3 ], positionals [4 ], positionals [5 ])
492+ set_target (positionals [2 ], positionals [3 ], positionals [4 ], positionals [5 ])
463493 print_status ()
464494elseif command == " verbose" then
465495 state .verbose = not state .verbose
@@ -468,11 +498,11 @@ elseif command == "clear" then
468498 state = get_default_state ()
469499 update_event_listener ()
470500elseif command == " split_stacks" then
471- state .split_stacks = stringtoboolean [positionals [2 ]]
501+ state .split_stacks = stringtoboolean [positionals [2 ]]
472502 print_status ()
473503elseif not command or command == " status" then
474504 print_status ()
475505elseif (command ~= " enable" or command ~= " disable" ) and not dfhack_flags .enable then
476- handle_error ((" Command " % s " is not recognized" ):format (command ))
506+ handle_error ((" Command " % s " is not recognized" ):format (command ))
477507end
478508persist_state ()
0 commit comments