@@ -14,32 +14,24 @@ local function spawnLiquid(position, liquid_level, liquid_type, update_liquids)
1414 map_block .flags .update_liquid_twice = update_liquids
1515end
1616
17- local function checkUnit (unit )
18- return (unit .body .blood_count ~= 0 or unit .body .blood_max == 0 ) and
19- not unit .flags1 .inactive and
20- not unit .flags1 .caged and
21- not unit .flags1 .chained
22- end
23-
24- local function isUnitFriendly (unit )
25- if dfhack .units .isDanger (unit ) then
17+ local function checkUnit (opts , unit )
18+ if not dfhack .units .isActive (unit ) or
19+ (unit .body .blood_max ~= 0 and unit .body .blood_count == 0 ) or
20+ unit .flags1 .caged or
21+ unit .flags1 .chained
22+ then
2623 return false
2724 end
28- local adv = dfhack .world .getAdventurer ()
29- if adv then
30- if adv == unit or
31- unit .relationship_ids .GroupLeader == adv .id or
32- unit .relationship_ids .PetOwner == adv .id
33- then
34- return true
35- end
25+ if opts .only_visible and not dfhack .units .isVisible (unit ) then
26+ return false
3627 end
37-
38- return dfhack .units .isOwnCiv (unit ) or
39- dfhack .units .isOwnGroup (unit ) or
40- dfhack .units .isVisiting (unit ) or
41- dfhack .units .isTame (unit ) or
42- dfhack .units .isDomesticated (unit )
28+ if not opts .include_friendly and not dfhack .units .isDanger (unit ) and not dfhack .units .isWildlife (unit ) then
29+ return false
30+ end
31+ if opts .selected_caste and opts .selected_caste ~= df .creature_raw .find (unit .race ).caste [unit .caste ].caste_id then
32+ return false
33+ end
34+ return true
4335end
4436
4537killMethod = {
@@ -138,26 +130,17 @@ local function getRaceCastes(race_id)
138130 return unit_castes
139131end
140132
141- local function getMapRaces (only_visible , include_friendly )
133+ local function getMapRaces (opts )
142134 local map_races = {}
143135 for _ , unit in pairs (df .global .world .units .active ) do
144- if only_visible and not dfhack .units .isVisible (unit ) then
145- goto skipunit
146- end
147- if not include_friendly and isUnitFriendly (unit ) then
148- goto skipunit
149- end
150- if dfhack .units .isActive (unit ) and checkUnit (unit ) then
151- local unit_race_name = dfhack .units .isUndead (unit ) and " UNDEAD" or df .creature_raw .find (unit .race ).creature_id
152-
153- local race = ensure_key (map_races , unit_race_name )
154- race .id = unit .race
155- race .name = unit_race_name
156- race .count = (race .count or 0 ) + 1
157- end
158- :: skipunit ::
136+ if not checkUnit (opts , unit ) then goto continue end
137+ local unit_race_name = dfhack .units .isUndead (unit ) and " UNDEAD" or df .creature_raw .find (unit .race ).creature_id
138+ local race = ensure_key (map_races , unit_race_name )
139+ race .id = unit .race
140+ race .name = unit_race_name
141+ race .count = (race .count or 0 ) + 1
142+ :: continue::
159143 end
160-
161144 return map_races
162145end
163146
@@ -200,9 +183,9 @@ if positionals[1] == "this" then
200183 return
201184end
202185
203- local map_races = getMapRaces (options . only_visible , options . include_friendly )
186+ local map_races = getMapRaces (options )
204187
205- if not positionals [1 ] then
188+ if not positionals [1 ] or positionals [ 1 ] == ' list ' then
206189 local sorted_races = {}
207190 for race , value in pairs (map_races ) do
208191 table.insert (sorted_races , { name = race , count = value .count })
@@ -224,28 +207,19 @@ if race_name:lower() == 'undead' then
224207 qerror (" No undead found on the map." )
225208 end
226209 for _ , unit in pairs (df .global .world .units .active ) do
227- if dfhack .units .isUndead (unit ) and checkUnit (unit ) then
210+ if dfhack .units .isUndead (unit ) and checkUnit (options , unit ) then
228211 killUnit (unit , options .method )
229212 count = count + 1
230213 end
231214 end
232215elseif positionals [1 ]:split (' :' )[1 ] == " all" then
233- local selected_caste = positionals [1 ]:split (' :' )[2 ]
216+ options . selected_caste = positionals [1 ]:split (' :' )[2 ]
234217
235218 for _ , unit in ipairs (df .global .world .units .active ) do
236219 if options .limit > 0 and count >= options .limit then
237220 break
238221 end
239- if not checkUnit (unit ) then
240- goto skipunit
241- end
242- if options .only_visible and not dfhack .units .isVisible (unit ) then
243- goto skipunit
244- end
245- if not options .include_friendly and isUnitFriendly (unit ) then
246- goto skipunit
247- end
248- if selected_caste and selected_caste ~= df .creature_raw .find (unit .race ).caste [unit .caste ].caste_id then
222+ if not checkUnit (options , unit ) then
249223 goto skipunit
250224 end
251225
@@ -285,21 +259,13 @@ else
285259 end
286260
287261 target = selected_race
262+ options .selected_caste = selected_caste
288263
289264 for _ , unit in pairs (df .global .world .units .active ) do
290265 if options .limit > 0 and count >= options .limit then
291266 break
292267 end
293- if not checkUnit (unit ) then
294- goto skipunit
295- end
296- if options .only_visible and not dfhack .units .isVisible (unit ) then
297- goto skipunit
298- end
299- if not options .include_friendly and isUnitFriendly (unit ) then
300- goto skipunit
301- end
302- if selected_caste and selected_caste ~= df .creature_raw .find (unit .race ).caste [unit .caste ].caste_id then
268+ if not checkUnit (options , unit ) then
303269 goto skipunit
304270 end
305271
0 commit comments