|
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) |
| 5 | +local ANY_BABY = df.global.world.units.other.ANY_BABY |
| 6 | + |
| 7 | +-- called by armoks-blessing |
| 8 | +function rejuvenate(unit, quiet, force, dry_run, age) |
| 9 | + age = age or 20 |
8 | 10 | local current_year = df.global.cur_year |
9 | | - if not age then |
10 | | - age = 20 |
11 | | - end |
12 | 11 | local new_birth_year = current_year - age |
13 | | - local name = dfhack.df2console(dfhack.TranslateName(dfhack.units.getVisibleName(unit))) |
| 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)) |
14 | 14 | if unit.birth_year > new_birth_year and not force then |
15 | | - print(name .. ' is under ' .. age .. ' years old. Use --force to force.') |
| 15 | + if not quiet then |
| 16 | + dfhack.printerr(name .. ' is under ' .. age .. ' years old. Use --force to force.') |
| 17 | + end |
16 | 18 | return |
17 | 19 | end |
18 | 20 | if dry_run then |
19 | 21 | print('would change: ' .. name) |
20 | 22 | return |
21 | 23 | end |
| 24 | + |
| 25 | + local hf = df.historical_figure.find(unit.hist_figure_id) |
22 | 26 | 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 |
| 27 | + if hf then hf.born_year = new_birth_year end |
| 28 | + unit.old_year = new_old_year |
| 29 | + if hf then hf.old_year = new_old_year end |
| 30 | + |
26 | 31 | if unit.profession == df.profession.BABY or unit.profession == df.profession.CHILD then |
27 | 32 | if unit.profession == df.profession.BABY then |
28 | | - local leftoverUnits = {} |
29 | | - local shiftedLeftoverUnits = {} |
30 | | - -- create a copy |
31 | | - local babyUnits = df.global.world.units.other.ANY_BABY |
32 | | - -- create a new table with the units that aren't being removed in this iteration |
33 | | - for _, v in ipairs(babyUnits) do |
34 | | - if not v.id == unit.id then |
35 | | - table.insert(leftoverUnits, v) |
36 | | - end |
| 33 | + local idx = utils.linear_index(ANY_BABY, unit.id, 'id') |
| 34 | + if idx then |
| 35 | + ANY_BABY:erase(idx) |
37 | 36 | end |
38 | | - -- create a shifted table of the leftover units to make up for lua tables starting with index 1 and the game starting with index 0 |
39 | | - for i = 0, #leftoverUnits - 1, 1 do |
40 | | - local x = i+1 |
41 | | - shiftedLeftoverUnits[i] = leftoverUnits[x] |
42 | | - end |
43 | | - -- copy the leftover units back to the game table |
44 | | - df.global.world.units.other.ANY_BABY = shiftedLeftoverUnits |
45 | | - -- set extra flags to defaults |
46 | 37 | unit.flags1.rider = false |
47 | 38 | unit.relationship_ids.RiderMount = -1 |
48 | | - unit.mount_type = 0 |
| 39 | + unit.mount_type = df.rider_positions_type.STANDARD |
49 | 40 | unit.profession2 = df.profession.STANDARD |
50 | | - unit.idle_area_type = 26 |
| 41 | + unit.idle_area_type = df.unit_station_type.MillBuilding |
51 | 42 | unit.mood = -1 |
52 | 43 |
|
53 | 44 | -- let the mom know she isn't carrying anyone anymore |
54 | | - local motherUnitId = unit.relationship_ids.Mother |
55 | | - df.unit.find(motherUnitId).flags1.ridden = false |
| 45 | + local mother = df.unit.find(unit.relationship_ids.Mother) |
| 46 | + if mother then mother.flags1.ridden = false end |
56 | 47 | end |
57 | 48 | unit.profession = df.profession.STANDARD |
| 49 | + unit.profession2 = df.profession.STANDARD |
| 50 | + if hf then hf.profession = df.profession.STANDARD end |
| 51 | + end |
| 52 | + if not quiet then |
| 53 | + print(name .. ' is now ' .. age .. ' years old and will live to at least 160') |
58 | 54 | end |
59 | | - print(name .. ' is now ' .. age .. ' years old and will live to at least 160') |
60 | 55 | end |
61 | 56 |
|
62 | | -function main(args) |
| 57 | +local function main(args) |
63 | 58 | local units = {} --as:df.unit[] |
64 | 59 | if args.all then |
65 | 60 | units = dfhack.units.getCitizens() |
66 | 61 | else |
67 | 62 | table.insert(units, dfhack.gui.getSelectedUnit(true) or qerror("Please select a unit in the UI.")) |
68 | 63 | end |
69 | 64 | for _, u in ipairs(units) do |
70 | | - rejuvenate(u, args.force, args['dry-run'], args.age) |
| 65 | + rejuvenate(u, false, args.force, args['dry-run'], args.age) |
71 | 66 | end |
72 | 67 | end |
73 | 68 |
|
|
0 commit comments