|
1 | | --- set age of selected unit |
2 | | --- by vjek |
3 | 1 | --@ module = true |
4 | 2 |
|
5 | 3 | local utils = require('utils') |
6 | 4 |
|
7 | | -function rejuvenate(unit, force, dry_run, age) |
8 | | - local current_year = df.global.cur_year |
9 | | - if not age then |
10 | | - age = 20 |
| 5 | +local DEFAULT_CHILD_AGE = 18 |
| 6 | +local DEFAULT_OLD_AGE = 160 |
| 7 | +local ANY_BABY = df.global.world.units.other.ANY_BABY |
| 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 |
11 | 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 | + |
| 26 | +-- called by armoks-blessing |
| 27 | +function rejuvenate(unit, quiet, force, dry_run, age) |
| 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 |
| 36 | + local current_year = df.global.cur_year |
12 | 37 | local new_birth_year = current_year - age |
13 | | - local name = dfhack.df2console(dfhack.TranslateName(dfhack.units.getVisibleName(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 | | - print(name .. ' is under ' .. age .. ' years old. Use --force to force.') |
| 40 | + if not quiet then |
| 41 | + dfhack.printerr(name .. ' is under ' .. age .. ' years old. Use --force to force.') |
| 42 | + end |
16 | 43 | return |
17 | 44 | end |
18 | 45 | if dry_run then |
19 | 46 | print('would change: ' .. name) |
20 | 47 | return |
21 | 48 | end |
| 49 | + |
| 50 | + local hf = df.historical_figure.find(unit.hist_figure_id) |
22 | 51 | unit.birth_year = new_birth_year |
23 | | - if unit.old_year < new_birth_year + 160 then |
24 | | - unit.old_year = new_birth_year + 160 |
25 | | - end |
| 52 | + if hf then hf.born_year = new_birth_year end |
| 53 | + unit.old_year = new_old_year |
| 54 | + if hf then hf.old_year = new_old_year end |
| 55 | + |
26 | 56 | if unit.profession == df.profession.BABY or unit.profession == df.profession.CHILD then |
| 57 | + if unit.profession == df.profession.BABY then |
| 58 | + local idx = utils.linear_index(ANY_BABY, unit.id, 'id') |
| 59 | + if idx then |
| 60 | + ANY_BABY:erase(idx) |
| 61 | + end |
| 62 | + unit.flags1.rider = false |
| 63 | + unit.relationship_ids.RiderMount = -1 |
| 64 | + unit.mount_type = df.rider_positions_type.STANDARD |
| 65 | + unit.profession2 = df.profession.STANDARD |
| 66 | + unit.idle_area_type = df.unit_station_type.MillBuilding |
| 67 | + unit.mood = -1 |
| 68 | + |
| 69 | + -- let the mom know she isn't carrying anyone anymore |
| 70 | + local mother = df.unit.find(unit.relationship_ids.Mother) |
| 71 | + if mother then mother.flags1.ridden = false end |
| 72 | + end |
27 | 73 | unit.profession = df.profession.STANDARD |
| 74 | + unit.profession2 = df.profession.STANDARD |
| 75 | + if hf then hf.profession = df.profession.STANDARD end |
| 76 | + end |
| 77 | + if not quiet then |
| 78 | + print(name .. ' is now ' .. age .. ' years old and will live a normal lifespan henceforth') |
28 | 79 | end |
29 | | - print(name .. ' is now ' .. age .. ' years old and will live to at least 160') |
30 | 80 | end |
31 | 81 |
|
32 | | -function main(args) |
| 82 | +local function main(args) |
33 | 83 | local units = {} --as:df.unit[] |
34 | 84 | if args.all then |
35 | 85 | units = dfhack.units.getCitizens() |
36 | 86 | else |
37 | 87 | table.insert(units, dfhack.gui.getSelectedUnit(true) or qerror("Please select a unit in the UI.")) |
38 | 88 | end |
39 | 89 | for _, u in ipairs(units) do |
40 | | - rejuvenate(u, args.force, args['dry-run'], args.age) |
| 90 | + rejuvenate(u, false, args.force, args['dry-run'], args.age) |
41 | 91 | end |
42 | 92 | end |
43 | 93 |
|
|
0 commit comments