|
2 | 2 |
|
3 | 3 | local utils = require('utils') |
4 | 4 |
|
| 5 | +local DEFAULT_CHILD_AGE = 18 |
| 6 | +local DEFAULT_OLD_AGE = 160 |
5 | 7 | local ANY_BABY = df.global.world.units.other.ANY_BABY |
6 | 8 |
|
| 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 | + |
7 | 26 | -- called by armoks-blessing |
8 | 27 | 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 |
10 | 36 | local current_year = df.global.cur_year |
11 | 37 | 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)) |
14 | 39 | if unit.birth_year > new_birth_year and not force then |
15 | 40 | if not quiet then |
16 | 41 | dfhack.printerr(name .. ' is under ' .. age .. ' years old. Use --force to force.') |
@@ -50,7 +75,7 @@ function rejuvenate(unit, quiet, force, dry_run, age) |
50 | 75 | if hf then hf.profession = df.profession.STANDARD end |
51 | 76 | end |
52 | 77 | 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') |
54 | 79 | end |
55 | 80 | end |
56 | 81 |
|
|
0 commit comments