@@ -24,8 +24,7 @@ local function findClosest(pos, item_vector, is_good)
2424 local dclosest = - 1
2525 for _ ,item in ipairs (item_vector ) do
2626 if not item .flags .in_job and (not is_good or is_good (item )) then
27- local x , y , z = dfhack .items .getPosition (item )
28- local pitem = xyz2pos (x , y , z )
27+ local pitem = xyz2pos (dfhack .items .getPosition (item ))
2928 local ditem = distance (pos , pitem )
3029 if dfhack .maps .canWalkBetween (pos , pitem ) and (not closest or ditem < dclosest ) then
3130 closest = item
3837
3938--- find a drink
4039--- @param pos df.coord
41- --- @return df.item_drinkst | nil
40+ --- @return df.item_drinkst ?
4241local function get_closest_drink (pos )
4342 local is_good = function (drink )
4443 local container = dfhack .items .getContainer (drink )
45- return container and df . item_barrelst : is_instance ( container )
44+ return container and container : isFoodStorage ( )
4645 end
4746 return findClosest (pos , df .global .world .items .other .DRINK , is_good )
4847end
5251local function get_closest_meal (pos )
5352 --- @param meal df.item_foodst
5453 local function is_good (meal )
55- return meal .flags .rotten == false
54+ if meal .flags .rotten then
55+ return false
56+ else
57+ local container = dfhack .items .getContainer (meal )
58+ return not container or container :isFoodStorage ()
59+ end
5660 end
5761 return findClosest (pos , df .global .world .items .other .FOOD , is_good )
5862end
@@ -123,13 +127,13 @@ local function load_state()
123127 enabled = persisted_data .enabled or false
124128end
125129
126- DrinkAlcohol = df .need_type [ ' DrinkAlcohol' ]
127- EatGoodMeal = df .need_type [ ' EatGoodMeal' ]
130+ DrinkAlcohol = df .need_type . DrinkAlcohol
131+ EatGoodMeal = df .need_type . EatGoodMeal
128132
129133--- @type integer[]
130- watched = {}
134+ watched = watched or {}
131135
132- threshold = - 9000
136+ local threshold = - 9000
133137
134138--- unit loop: check for idle watched units and create eat/drink jobs for them
135139local function unit_loop ()
@@ -138,23 +142,25 @@ local function unit_loop()
138142 local kept = {}
139143 for _ , unit_id in ipairs (watched ) do
140144 local unit = df .unit .find (unit_id )
141- if unit and not (unit .flags1 .caged or unit .flags1 .chained ) then
142- if not idle .unitIsAvailable (unit ) then
143- table.insert (kept , unit .id )
144- else
145- --
146- for _ , need in ipairs (unit .status .current_soul .personality .needs ) do
147- if need .id == DrinkAlcohol and need .focus_level < threshold then
148- goDrink (unit )
149- goto next_unit
150- elseif need .id == EatGoodMeal and need .focus_level < threshold then
151- goEat (unit )
152- goto next_unit
153- end
145+ if
146+ not unit or not dfhack .units .isActive (unit ) or
147+ unit .flags1 .caged or unit .flags1 .chained
148+ then
149+ goto next_unit
150+ end
151+ if not idle .unitIsAvailable (unit ) then
152+ table.insert (kept , unit .id )
153+ else
154+ -- unit is available for jobs; satisfy one of its needs
155+ for _ , need in ipairs (unit .status .current_soul .personality .needs ) do
156+ if need .id == DrinkAlcohol and need .focus_level < threshold then
157+ goDrink (unit )
158+ break
159+ elseif need .id == EatGoodMeal and need .focus_level < threshold then
160+ goEat (unit )
161+ break
154162 end
155163 end
156- else
157- -- print('immortal-cravings: unit gone or caged')
158164 end
159165 :: next_unit::
160166 end
167173
168174--- main loop: look for citizens with personality needs for food/drink but w/o physiological need
169175local function main_loop ()
170- print (' immortal-cravings watching:' )
176+ -- print('immortal-cravings watching:')
171177 watched = {}
172178 for _ , unit in ipairs (dfhack .units .getCitizens ()) do
173179 if unit .curse .add_tags1 .NO_DRINK or unit .curse .add_tags1 .NO_EAT then
@@ -176,7 +182,7 @@ local function main_loop()
176182 need .id == EatGoodMeal and need .focus_level < threshold
177183 then
178184 table.insert (watched , unit .id )
179- print (' ' .. dfhack .df2console (dfhack .TranslateName (dfhack .units .getVisibleName (unit ))))
185+ -- print(' '..dfhack.df2console(dfhack.TranslateName(dfhack.units.getVisibleName(unit))))
180186 goto next_unit
181187 end
182188 end
208214dfhack .onStateChange [GLOBAL_KEY ] = function (sc )
209215 if sc == SC_MAP_UNLOADED then
210216 enabled = false
217+ -- repeat-util will cancel the loops on unload
211218 return
212219 end
213220
0 commit comments