Skip to content

Commit

Permalink
default to minimum adult age, not a static 20
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Sep 7, 2024
1 parent f1146d4 commit f8a4753
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
20 changes: 12 additions & 8 deletions docs/rejuvenate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ rejuvenate
:tags: fort armok units

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

Usage
-----
Expand All @@ -21,21 +21,25 @@ Examples
--------

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

Options
-------

``--all``
Rejuvenate all citizens, not just the selected one.
Rejuvenate all citizens and residents instead of a selected unit.
``--age <num>``
Sets the target to the age specified. If this is not set, the target age defaults to ``20``.
Sets the target to the age specified. If this is not set, the target age defaults to the minimum adult age for the unit.
``--force``
Set age for units under the specified age to the specified age. Useful if
there are too many babies around...
Expand Down
33 changes: 29 additions & 4 deletions rejuvenate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,40 @@

local utils = require('utils')

local DEFAULT_CHILD_AGE = 18
local DEFAULT_OLD_AGE = 160
local ANY_BABY = df.global.world.units.other.ANY_BABY

local function get_caste_misc(unit)
local cre = df.creature_raw.find(unit.race)
if not cre then return end
if unit.caste < 0 or unit.caste >= #cre.caste then
return
end
return cre.caste[unit.caste].misc
end

local function get_adult_age(misc)
return misc and misc.child_age or DEFAULT_CHILD_AGE
end

local function get_rand_old_age(misc)
return misc and math.random(misc.maxage_min, misc.maxage_max) or DEFAULT_OLD_AGE
end

-- called by armoks-blessing
function rejuvenate(unit, quiet, force, dry_run, age)
age = age or 20
local name = dfhack.df2console(dfhack.units.getReadableName(unit))
local misc = get_caste_misc(unit)
local adult_age = get_adult_age(misc)
age = age or adult_age
if age < adult_age then
dfhack.printerr('cannot set age to child or baby range')
return
end
local current_year = df.global.cur_year
local new_birth_year = current_year - age
local new_old_year = unit.old_year < 0 and -1 or math.max(unit.old_year, new_birth_year + 160)
local name = dfhack.df2console(dfhack.units.getReadableName(unit))
local new_old_year = unit.old_year < 0 and -1 or math.max(unit.old_year, new_birth_year + get_rand_old_age(misc))
if unit.birth_year > new_birth_year and not force then
if not quiet then
dfhack.printerr(name .. ' is under ' .. age .. ' years old. Use --force to force.')
Expand Down Expand Up @@ -50,7 +75,7 @@ function rejuvenate(unit, quiet, force, dry_run, age)
if hf then hf.profession = df.profession.STANDARD end
end
if not quiet then
print(name .. ' is now ' .. age .. ' years old and will live to at least 160')
print(name .. ' is now ' .. age .. ' years old and will live a normal lifespan henceforth')
end
end

Expand Down

0 comments on commit f8a4753

Please sign in to comment.