From f8a4753fe1251dce6177f84af5ff4c75afaf642a Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sat, 7 Sep 2024 10:54:45 -0700 Subject: [PATCH] default to minimum adult age, not a static 20 --- docs/rejuvenate.rst | 20 ++++++++++++-------- rejuvenate.lua | 33 +++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/docs/rejuvenate.rst b/docs/rejuvenate.rst index 73d6eebf8..aaa2216ca 100644 --- a/docs/rejuvenate.rst +++ b/docs/rejuvenate.rst @@ -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 ----- @@ -21,11 +21,15 @@ 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. @@ -33,9 +37,9 @@ Options ------- ``--all`` - Rejuvenate all citizens, not just the selected one. + Rejuvenate all citizens and residents instead of a selected unit. ``--age `` - 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... diff --git a/rejuvenate.lua b/rejuvenate.lua index 2ecdc7183..9952be746 100644 --- a/rejuvenate.lua +++ b/rejuvenate.lua @@ -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.') @@ -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