Skip to content

Commit 25ad971

Browse files
committed
apply suggestions from code review
and ensure histfig data is consistent
1 parent a4cc736 commit 25ad971

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Template for new versions:
4545
- `deep-embark`: fix error when embarking where there is no land to stand on (e.g. when embarking in the ocean with `gui/embark-anywhere`)
4646
- `deep-embark`: fix failure to transport units and items when embarking where there is no room to spawn the starting wagon
4747
- `gui/create-item`, `modtools/create-item`: items of type "VERMIN", "PET", "REMANS", "FISH", "RAW FISH", and "EGG" no longer spawn creature item "nothing" and will now stack correctly
48+
- `rejuvenate`: don't set a lifespan limit for creatures that are immortal (e.g. elves, goblins)
49+
- `rejuvenate`: properly disconnect babies from mothers when aging babies up to adults
4850

4951
## Misc Improvements
5052
- `gui/sitemap`: show whether a unit is friendly, hostile, or wild

docs/rejuvenate.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ rejuvenate
77

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

1213
Usage
1314
-----
@@ -24,7 +25,7 @@ Examples
2425
``rejuvenate --all``
2526
Set the age of all dwarves over 20 to 20.
2627
``rejuvenate --all --force``
27-
Set the age of all dwarves (including babies) to 20.
28+
Set the age of all dwarves (including children and babies) to 20.
2829
``rejuvenate --age 149 --force``
2930
Set the age of the selected dwarf to 149, even if they are younger.
3031

@@ -34,9 +35,9 @@ Options
3435
``--all``
3536
Rejuvenate all citizens, not just the selected one.
3637
``--age <num>``
37-
Sets the target to the age specified. If this is not set, the target age is 20.
38+
Sets the target to the age specified. If this is not set, the target age defaults to ``20``.
3839
``--force``
39-
Set age for units under the specified age to the specified age. Useful if there are too
40-
many babies around...
40+
Set age for units under the specified age to the specified age. Useful if
41+
there are too many babies around...
4142
``--dry-run``
4243
Only list units that would be changed; don't actually change ages.

rejuvenate.lua

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,68 @@
1-
-- set age of selected unit
2-
-- by vjek
31
--@ module = true
42

53
local utils = require('utils')
64

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
810
local current_year = df.global.cur_year
9-
if not age then
10-
age = 20
11-
end
1211
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))
1414
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
1618
return
1719
end
1820
if dry_run then
1921
print('would change: ' .. name)
2022
return
2123
end
24+
25+
local hf = df.historical_figure.find(unit.hist_figure_id)
2226
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+
2631
if unit.profession == df.profession.BABY or unit.profession == df.profession.CHILD then
2732
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)
3736
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
4637
unit.flags1.rider = false
4738
unit.relationship_ids.RiderMount = -1
48-
unit.mount_type = 0
39+
unit.mount_type = df.rider_positions_type.STANDARD
4940
unit.profession2 = df.profession.STANDARD
50-
unit.idle_area_type = 26
41+
unit.idle_area_type = df.unit_station_type.MillBuilding
5142
unit.mood = -1
5243

5344
-- 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
5647
end
5748
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')
5854
end
59-
print(name .. ' is now ' .. age .. ' years old and will live to at least 160')
6055
end
6156

62-
function main(args)
57+
local function main(args)
6358
local units = {} --as:df.unit[]
6459
if args.all then
6560
units = dfhack.units.getCitizens()
6661
else
6762
table.insert(units, dfhack.gui.getSelectedUnit(true) or qerror("Please select a unit in the UI."))
6863
end
6964
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)
7166
end
7267
end
7368

0 commit comments

Comments
 (0)