@@ -7,7 +7,7 @@ local print_prefix = "eggwatch: "
77
88enabled = enabled or false
99default_table = {}
10- default_table .DEFAULT = 10
10+ default_table .DEFAULT = { 10 , false , false }
1111function isEnabled ()
1212 return enabled
1313end
3535if dfhack_flags .module then
3636 return
3737end
38+
3839local function print_local (text )
3940 print (print_prefix .. text )
4041end
42+
4143local function handle_error (text )
4244 qerror (text )
4345end
46+
4447local function print_status ()
4548 print_local ((" eggwatch is currently %s." ):format (enabled and " enabled" or " disabled" ))
4649 if verbose then
@@ -135,16 +138,71 @@ local function count_forbidden_eggs_for_race_in_claimed_nestobxes(race_creature_
135138 print_detalis ((" end count_forbidden_eggs_for_race_in_claimed_nestobxes" ))
136139 return eggs_count
137140end
141+
138142local function get_max_eggs_for_race (race_creature_id )
139143 for k , v in pairs (target_eggs_count_per_race ) do
140144 if k == race_creature_id then
141- return v
145+ return v [ 1 ]
142146 end
143147 end
144148 target_eggs_count_per_race [race_creature_id ] = target_eggs_count_per_race .DEFAULT
145149 persist_state ()
146- return target_eggs_count_per_race [race_creature_id ]
150+ return target_eggs_count_per_race [race_creature_id ][ 1 ]
147151end
152+
153+ local function count_children_for_race (race_creature_id )
154+ for k , v in pairs (target_eggs_count_per_race ) do
155+ if k == race_creature_id then
156+ return v [2 ]
157+ end
158+ end
159+ target_eggs_count_per_race [race_creature_id ] = target_eggs_count_per_race .DEFAULT
160+ return target_eggs_count_per_race [race_creature_id ][2 ]
161+ end
162+
163+ local function count_adults_for_race (race_creature_id )
164+ for k , v in pairs (target_eggs_count_per_race ) do
165+ if k == race_creature_id then
166+ return v [3 ]
167+ end
168+ end
169+ target_eggs_count_per_race [race_creature_id ] = target_eggs_count_per_race .DEFAULT
170+ return target_eggs_count_per_race [race_creature_id ][3 ]
171+ end
172+
173+ local function is_valid_animal (unit )
174+ return unit and
175+ dfhack .units .isActive (unit ) and
176+ dfhack .units .isAnimal (unit ) and
177+ dfhack .units .isFortControlled (unit ) and
178+ dfhack .units .isTame (unit ) and
179+ not dfhack .units .isDead (unit )
180+ end
181+ local function count_live_animals (race_creature_id )
182+ local count_adults = count_adults_for_race (race_creature_id )
183+ if count_adults then print_detalis ((' we are counting adults for %s' ):format (race_creature_id )) end
184+ local count_children = count_children_for_race (race_creature_id )
185+ if count_children then print_detalis ((' we are counting children and babies for %s' ):format (race_creature_id )) end
186+
187+ local count = 0
188+ if not count_adults and not count_children then
189+ return count
190+ end
191+ -- dfhack.units.isAdult(unit)
192+ for _ ,unit in ipairs (df .global .world .units .active ) do
193+ if race_creature_id == df .creature_raw .find (unit .race ).creature_id
194+ and is_valid_animal (unit )
195+ and ( (count_adults and dfhack .units .isAdult (unit ))
196+ or (count_children and ( dfhack .units .isChild (unit ) or dfhack .units .isBaby (unit )))
197+ ) then
198+ count = count + 1
199+ end
200+ end
201+ print_detalis ((' found %s life animals' ):format (count ))
202+ return count
203+ end
204+
205+
148206local function handle_eggs (eggs )
149207 print_detalis ((" start handle_eggs" ))
150208 if not eggs .egg_flags .fertile then
@@ -157,7 +215,7 @@ local function handle_eggs(eggs)
157215 local current_eggs = eggs .stack_size
158216
159217 local total_count = current_eggs
160- total_count = total_count + count_forbidden_eggs_for_race_in_claimed_nestobxes (race_creature_id )
218+ total_count = total_count + count_forbidden_eggs_for_race_in_claimed_nestobxes (race_creature_id ) + count_live_animals ( race_creature_id )
161219
162220 print_detalis ((" Total count for %s eggs is %s" ):format (race_creature_id , total_count ))
163221
@@ -201,6 +259,7 @@ local function check_item_created(item_id)
201259 end
202260 handle_eggs (item )
203261end
262+
204263local function do_enable ()
205264 enabled = true
206265 eventful .enableEvent (eventful .eventType .ITEM_CREATED , EVENT_FREQ )
@@ -221,7 +280,9 @@ local function validate_creature_id(creature_id)
221280 return false
222281end
223282
224- local function set_target (target_race , target_count )
283+ local function set_target (target_race , target_count , count_children , count_adult )
284+ local stringtoboolean = { [" true" ]= true , [" false" ]= false , [" 1" ] = true , [" 0" ] = false , [" Y" ] = true , [" N" ] = false }
285+
225286 if target_race == nil or target_race == " " then
226287 handle_error (' must specify "DEFAULT" or valid creature_id' )
227288 end
@@ -230,13 +291,14 @@ local function set_target(target_race, target_count)
230291 handle_error (" No valid target count specified" )
231292 end
232293 if target_race_upper == " DEFAULT" or validate_creature_id (target_race_upper ) then
233- target_eggs_count_per_race [target_race_upper ] = tonumber (target_count )
294+ target_eggs_count_per_race [target_race_upper ] = { tonumber (target_count ), stringtoboolean [ count_children ] or false , stringtoboolean [ count_adult ] or false }
234295 else
235296 handle_error (' must specify "DEFAULT" or valid creature_id' )
236297 end
237298
238299 print_local (dump (target_eggs_count_per_race ))
239300end
301+
240302function dump (o )
241303 if type (o ) == " table" then
242304 local s = " { "
@@ -284,7 +346,7 @@ elseif command == "disable" then
284346 do_disable ()
285347 print_status ()
286348elseif command == " target" then
287- set_target (positionals [2 ], positionals [3 ])
349+ set_target (positionals [2 ], positionals [3 ], positionals [ 4 ], positionals [ 5 ] )
288350 print_status ()
289351elseif command == " verbose" then
290352 verbose = not verbose
@@ -295,5 +357,7 @@ target_eggs_count_per_race = default_table
295357elseif not command or command == " status" then
296358 print_status ()
297359 print_local (dump (target_eggs_count_per_race ))
360+ else
361+ handle_error ((' Command "%s" is not recognized' ):format (command ))
298362end
299363persist_state ()
0 commit comments