Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed mothers attempting to seek their units that were babies #1284

Merged
merged 1 commit into from
Sep 7, 2024

Conversation

ahlove3
Copy link
Contributor

@ahlove3 ahlove3 commented Sep 1, 2024

Fixed mothers attempting to seek their units that were babies by removing the unit id from the ANY_BABY table. Also made those grown babies act like adults by updating some flags.

Fixes: DFHack/dfhack#4894

Fixed mothers attempting to seek their units that were babies by removing the unit id from the ANY_BABY table. Also made those grown babies act like adults by updating some flags.
table.insert(leftoverUnits, v)
end
end
-- 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
Copy link
Member

@ab9rf ab9rf Sep 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the erase method on the world.units.other.ANY_BABY vector for this. this method directly calls the appropriate C++ method on the underlying vector instead of rewriting the vector

you can simply do world.units.other.ANY_BABY:erase(i) where i is the (0-based) index of the item to erase. note that this will invalidate the ipairs iterator, but the unit should only be on the vector once so once you find it you don't have to keep searching to find it again

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also avoid finding the index entirely by using utils.erase_sorted(df.global.world.units.other.ANY_BABY, unit, 'id')

Copy link
Member

@ab9rf ab9rf Sep 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also avoid finding the index entirely by using utils.erase_sorted(df.global.world.units.other.ANY_BABY, unit, 'id')

do note that the unit classification subvectors (such as ANY_BABY) are not guaranteed to be sorted. the categorize methods simply insert the unit/item/building at the end of the classified subvector. using tools that expect these vectors to be sorted may result in unexpected behavior

unfortunately just uncategorizing and recategorizing the unit is unsafe, because categorizing a unit sets the unit's schedule_id to -1 (why, toady, why?), which means it's not safe to do this here forgot that these methods aren't virtual for unitst

TL;DR: do not use utils.erase_sorted on a unit subvector, this is not safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up. I had trouble with lua doing a 1 based index vs C++ (as well as what I'm used to from other languages) doing a 0 based index and then that translating back into the game vector. I'll update with these fixes later this week/this weekend. Appreciate the suggestions

-- set extra flags to defaults
unit.flags1.rider = false
unit.relationship_ids.RiderMount = -1
unit.mount_type = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use df.rider_positions_type.STANDARD instead of 0

unit.relationship_ids.RiderMount = -1
unit.mount_type = 0
unit.profession2 = df.profession.STANDARD
unit.idle_area_type = 26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use df.unit_station_type.MillBuilding instead of 26


-- let the mom know she isn't carrying anyone anymore
local motherUnitId = unit.relationship_ids.Mother
df.unit.find(motherUnitId).flags1.ridden = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a good idea to always protect from nil when using any find method. The mother could have just been obliterated and the unit could be gone by the time this is run.

-- let the mom know she isn't carrying anyone anymore
local motherUnitId = unit.relationship_ids.Mother
df.unit.find(motherUnitId).flags1.ridden = false
end
unit.profession = df.profession.STANDARD
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check and update unit.profession2 and the profession in the histfig (df.historical_figure.find(unit.hist_figure_id))

table.insert(leftoverUnits, v)
end
end
-- 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also avoid finding the index entirely by using utils.erase_sorted(df.global.world.units.other.ANY_BABY, unit, 'id')

@myk002
Copy link
Member

myk002 commented Sep 6, 2024

Do you think you'll have time to finish this up this weekend? If so, we can get it into the upcoming release

Copy link
Member

@myk002 myk002 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a line to changelog.txt with a description of the fix

@myk002
Copy link
Member

myk002 commented Sep 7, 2024

There are a few other necessary fixes to rejuvenate, so I can follow up on the PR comments. Thanks!

@myk002 myk002 merged commit a4cc736 into DFHack:master Sep 7, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

rejuvenate --force needs to break mother/infant rider relationship
3 participants