Skip to content

Commit f8a4753

Browse files
committed
default to minimum adult age, not a static 20
1 parent f1146d4 commit f8a4753

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

docs/rejuvenate.rst

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ rejuvenate
66
:tags: fort armok units
77

88
If your most valuable citizens are getting old, this tool can save them. It
9-
decreases the age of the selected dwarf to 20 years, or to the age specified.
10-
Age can only be increased (e.g. when this tool is run on babies or children)
11-
if the ``--force`` option is specified.
9+
decreases the age of the selected dwarf to the minimum adult age, or to the age
10+
specified. Age can only be increased (e.g. when this tool is run on babies or
11+
children) if the ``--force`` option is specified.
1212

1313
Usage
1414
-----
@@ -21,21 +21,25 @@ Examples
2121
--------
2222

2323
``rejuvenate``
24-
Set the age of the selected dwarf to 20 (if they're older).
24+
Set the age of the selected dwarf to 18 (if they're older than 18). The
25+
target age may be different if you have modded dwarves to become an adult
26+
at a different age, or if you have selected a unit that is not a dwarf.
2527
``rejuvenate --all``
26-
Set the age of all dwarves over 20 to 20.
28+
Set the ages of all adult citizens and residents to their minimum adult
29+
ages.
2730
``rejuvenate --all --force``
28-
Set the age of all dwarves (including children and babies) to 20.
31+
Set the ages of all citizens and residents (including children and babies)
32+
to their minimum adult ages.
2933
``rejuvenate --age 149 --force``
3034
Set the age of the selected dwarf to 149, even if they are younger.
3135

3236
Options
3337
-------
3438

3539
``--all``
36-
Rejuvenate all citizens, not just the selected one.
40+
Rejuvenate all citizens and residents instead of a selected unit.
3741
``--age <num>``
38-
Sets the target to the age specified. If this is not set, the target age defaults to ``20``.
42+
Sets the target to the age specified. If this is not set, the target age defaults to the minimum adult age for the unit.
3943
``--force``
4044
Set age for units under the specified age to the specified age. Useful if
4145
there are too many babies around...

rejuvenate.lua

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,40 @@
22

33
local utils = require('utils')
44

5+
local DEFAULT_CHILD_AGE = 18
6+
local DEFAULT_OLD_AGE = 160
57
local ANY_BABY = df.global.world.units.other.ANY_BABY
68

9+
local function get_caste_misc(unit)
10+
local cre = df.creature_raw.find(unit.race)
11+
if not cre then return end
12+
if unit.caste < 0 or unit.caste >= #cre.caste then
13+
return
14+
end
15+
return cre.caste[unit.caste].misc
16+
end
17+
18+
local function get_adult_age(misc)
19+
return misc and misc.child_age or DEFAULT_CHILD_AGE
20+
end
21+
22+
local function get_rand_old_age(misc)
23+
return misc and math.random(misc.maxage_min, misc.maxage_max) or DEFAULT_OLD_AGE
24+
end
25+
726
-- called by armoks-blessing
827
function rejuvenate(unit, quiet, force, dry_run, age)
9-
age = age or 20
28+
local name = dfhack.df2console(dfhack.units.getReadableName(unit))
29+
local misc = get_caste_misc(unit)
30+
local adult_age = get_adult_age(misc)
31+
age = age or adult_age
32+
if age < adult_age then
33+
dfhack.printerr('cannot set age to child or baby range')
34+
return
35+
end
1036
local current_year = df.global.cur_year
1137
local new_birth_year = current_year - age
12-
local new_old_year = unit.old_year < 0 and -1 or math.max(unit.old_year, new_birth_year + 160)
13-
local name = dfhack.df2console(dfhack.units.getReadableName(unit))
38+
local new_old_year = unit.old_year < 0 and -1 or math.max(unit.old_year, new_birth_year + get_rand_old_age(misc))
1439
if unit.birth_year > new_birth_year and not force then
1540
if not quiet then
1641
dfhack.printerr(name .. ' is under ' .. age .. ' years old. Use --force to force.')
@@ -50,7 +75,7 @@ function rejuvenate(unit, quiet, force, dry_run, age)
5075
if hf then hf.profession = df.profession.STANDARD end
5176
end
5277
if not quiet then
53-
print(name .. ' is now ' .. age .. ' years old and will live to at least 160')
78+
print(name .. ' is now ' .. age .. ' years old and will live a normal lifespan henceforth')
5479
end
5580
end
5681

0 commit comments

Comments
 (0)