Skip to content

Commit d330738

Browse files
committed
immortal-cravings: also take care of immortal units with alcohol dependence
1 parent af457f9 commit d330738

3 files changed

Lines changed: 37 additions & 15 deletions

File tree

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Template for new versions:
3232

3333
## Fixes
3434

35+
`immortal-cravings`: also take care of immortal units with alcohol dependence
36+
3537
## Misc Improvements
3638

3739
## Removed

docs/immortal-cravings.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ immortal-cravings
66
:tags: fort gameplay
77

88
When enabled, this script watches your fort for units that have no physiological
9-
need to eat or drink but still have personality needs that can only be satisfied
10-
by eating or drinking (e.g. necromancers or goblins). This enables those units
11-
to help themselves to a drink or a meal when they crave one and are not
12-
otherwise occupied.
9+
need to eat or drink but are still alcohol dependent or have personality needs
10+
that can only be satisfied by eating or drinking (e.g. necromancers or goblins).
11+
This enables those units to help themselves to a drink or a meal when they crave
12+
one and are not otherwise occupied.
1313

1414
Usage
1515
-----

immortal-cravings.lua

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33

44
local 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
1116
local 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))
148153
end
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

152170
local GLOBAL_KEY = 'immortal-cravings'
@@ -175,8 +193,6 @@ EatGoodMeal = df.need_type.EatGoodMeal
175193
---@type integer[]
176194
watched = watched or {}
177195

178-
local threshold = -9000
179-
180196
---unit loop: check for idle watched units and create eat/drink jobs for them
181197
local 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
215237
end
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
223242
local 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

Comments
 (0)