33
44local repeatutil = require (" repeat-util" )
55
6+ --- constants
7+
8+ local focus_threshold = - 9000
9+ local dependence_threshold = 33600 * 2
10+
611--- utility functions
712
8- local verbose = false
13+ verbose = true
914--- conditional printing of debug messages
1015--- @param message string
1116local function debug (message )
@@ -147,6 +152,19 @@ local function goEat(unit)
147152 print (dfhack .df2console (' immortal-cravings: %s is getting something to eat' ):format (name ))
148153end
149154
155+ --- @param unit df.unit
156+ --- @param flag_name string
157+ local function is_active_caste_flag (unit , flag_name )
158+ return not unit .uwss_remove_caste_flag [flag_name ] and
159+ (unit .uwss_add_caste_flag [flag_name ] or dfhack .units .casteFlagSet (unit .race , unit .caste , df .caste_raw_flags [flag_name ]))
160+ end
161+
162+ --- @param unit df.unit
163+ local function needs_alcohol (unit )
164+ local mt = dfhack .units .getMiscTrait (unit , df .misc_trait_type .WantsDrink , false )
165+ return mt and mt .value > dependence_threshold
166+ end
167+
150168--- script logic
151169
152170local GLOBAL_KEY = ' immortal-cravings'
@@ -175,8 +193,6 @@ EatGoodMeal = df.need_type.EatGoodMeal
175193--- @type integer[]
176194watched = watched or {}
177195
178- local threshold = - 9000
179-
180196--- unit loop: check for idle watched units and create eat/drink jobs for them
181197local function unit_loop ()
182198 debug ((' immortal-cravings: running unit loop (%d watched units)' ):format (# watched ))
@@ -194,12 +210,18 @@ local function unit_loop()
194210 debug (" immortal-cravings: skipping busy" .. dfhack .units .getReadableName (unit ))
195211 table.insert (kept , unit .id )
196212 else
197- -- unit is available for jobs; satisfy one of its needs
213+ -- unit is alcohol dependent and approaching withdrawal symptoms
214+ if needs_alcohol (unit ) then
215+ goDrink (unit )
216+ goto next_unit
217+ end
218+
219+ -- otherwise satisfy a personality need
198220 for _ , need in ipairs (unit .status .current_soul .personality .needs ) do
199- if need .id == DrinkAlcohol and need .focus_level < threshold then
221+ if need .id == DrinkAlcohol and need .focus_level < focus_threshold then
200222 goDrink (unit )
201223 break
202- elseif need .id == EatGoodMeal and need .focus_level < threshold then
224+ elseif need .id == EatGoodMeal and need .focus_level < focus_threshold then
203225 goEat (unit )
204226 break
205227 end
@@ -214,10 +236,7 @@ local function unit_loop()
214236 end
215237end
216238
217- local function is_active_caste_flag (unit , flag_name )
218- return not unit .uwss_remove_caste_flag [flag_name ] and
219- (unit .uwss_add_caste_flag [flag_name ] or dfhack .units .casteFlagSet (unit .race , unit .caste , df .caste_raw_flags [flag_name ]))
220- end
239+
221240
222241--- main loop: look for citizens with personality needs for food/drink but w/o physiological need
223242local function main_loop ()
@@ -227,7 +246,8 @@ local function main_loop()
227246 if
228247 (is_active_caste_flag (unit , ' NO_DRINK' ) or is_active_caste_flag (unit , ' NO_EAT' )) and
229248 unit .counters2 .stomach_content == 0 and
230- dfhack .units .getFocusPenalty (unit , DrinkAlcohol , EatGoodMeal ) < threshold
249+ (dfhack .units .getFocusPenalty (unit , DrinkAlcohol , EatGoodMeal ) < focus_threshold or
250+ needs_alcohol (unit ))
231251 then
232252 table.insert (watched , unit .id )
233253 debug (' ' .. dfhack .df2console (dfhack .units .getReadableName (unit )))
0 commit comments