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

Rework active_item_cache to use safe references #31873

Merged
merged 3 commits into from
Jun 27, 2019

Conversation

ifreund
Copy link
Contributor

@ifreund ifreund commented Jun 26, 2019

Summary

SUMMARY: Bugfixes "Rework active_item_cache to use safe references."

Purpose of change

After #31796 the current item processing invalidates item_locations, which
causes issues with all activities using item_locations.

Fixes #31839
Fixes #31840
Fixes #31846
Fixes #31868

Describe the solution

This PR rewrites active_item_cache to store a safe_reference
instead of an item_stack::iterator, removing the now unnecessary
active_item_set and has() functions.

This also fixes a bug with active item reordering after processing introduced
by #31406 and a minor bug with the submaps_with_active_items cache.

Describe alternatives you've considered

I considered completely removing active_item_cache::remove() and relying on lazy removal of broken references in the getter functions. I think this would be better, but I didn't see a way to handle updating the submaps_with_active_items cache in that cases. I could do this for vehicles since their active item status isn't currently cached, but wasn't sure if it was desirable or not.

After CleverRaven#31796 item processing invalidates item_locations, which
causes issues with all activities using item_locations.

This commit rewrites active_item_cache to store a safe_reference<item>
instead of an item_stack::iterator, removing the now unnecessary
active_item_set and has() functions.

This also fixes a bug with active item reordering after processing introduced
by CleverRaven#31406 and a minor bug with the submaps_with_active_items cache.
@ifreund ifreund added [C++] Changes (can be) made in C++. Previously named `Code` Items / Item Actions / Item Qualities Items and how they work and interact <Bugfix> This is a fix for a bug (or closes open issue) Fields / Furniture / Terrain / Traps Objects that are part of the map or its features. labels Jun 26, 2019
@ifreund ifreund requested a review from kevingranade June 26, 2019 11:40
@ifreund
Copy link
Contributor Author

ifreund commented Jun 26, 2019

Could definitely use some more eyes on this. I'm pretty sure what I did works, but it would be easy to miss some subtle interaction here.

Copy link
Contributor

@jbytheway jbytheway left a comment

Choose a reason for hiding this comment

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

Looks promising (I've only read code, not tested). Thanks for taking care of the bugs I introduced :).

src/active_item_cache.h Outdated Show resolved Hide resolved
src/active_item_cache.h Outdated Show resolved Hide resolved
src/active_item_cache.cpp Show resolved Hide resolved
src/active_item_cache.cpp Outdated Show resolved Hide resolved
for( std::pair<const int, std::list<item_reference>> &kv : active_items ) {
for( std::list<item_reference>::iterator it = kv.second.begin(); it != kv.second.end(); ) {
if( it->safe_ref ) {
all_cached_items.emplace_back( *it );
Copy link
Contributor

@jbytheway jbytheway Jun 26, 2019

Choose a reason for hiding this comment

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

It looks like you're not incrementing the iterator in this branch?

(This might be the cause of the unit test failures)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Certainly seems like it, thanks for catching this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests pass locally for me after this change. Totally neglected to run them before pushing the initial commit since I thought they didn't test active item processing. Turns out they brush up against it a little.

Should pass travis now I hope

src/active_item_cache.h Outdated Show resolved Hide resolved
src/map.cpp Outdated
@@ -5086,43 +5074,44 @@ std::list<std::pair<tripoint, item *> > map::get_rc_items( int x, int y, int z )
return rc_pairs;
}

static bool trigger_radio_item( item_stack &items, item_stack::iterator &n,
static bool trigger_radio_item( item_stack &items, safe_reference<item> &safe_ref,
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems unlikely this needs a safe_reference. Can it just take an item* or item&?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It needs to be a

        using map_process_func = bool ( * )( item_stack &, safe_reference<item> &, const tripoint &,
                                             const std::string &, float, temperature_flag );

so unfortunately not

Copy link
Contributor

Choose a reason for hiding this comment

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

Understood.

In particular increment the iterator properly in active_item_cache::get()
Copy link
Contributor

@jbytheway jbytheway left a comment

Choose a reason for hiding this comment

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

LGTM

@IvanShestakov
Copy link
Contributor

I tried building from this merge request and opening an existing savegame: it solved the butchering and corpse hauling problem. On load it gave loong list of those errors:

src/savegame.cpp:858 [void overmap::unserialize(std::istream&)] Loaded bad ter! ter cabin_north
00:14:51.469 ERROR : src/savegame.cpp:858 [void overmap::unserialize(std::istream&)] Loaded bad ter! ter toxic_dump_north
00:14:51.481 ERROR : src/savegame.cpp:858 [void overmap::unserialize(std::istream&)] Loaded bad ter! ter lmoe_north
00:14:51.533 ERROR : src/savegame.cpp:858 [void overmap::unserialize(std::istream&)] Loaded bad ter! ter toxic_dump_north

@ifreund
Copy link
Contributor Author

ifreund commented Jun 26, 2019

I tried building from this merge request and opening an existing savegame: it solved the butchering and corpse hauling problem. On load it gave loong list of those errors:

Glad to hear it's working to fix those bugs for you! I'm not sure how those errors could be related to this PR in any way. At a guess I'd say that's an issue with your save.

@ProfoundDarkness
Copy link
Contributor

@IvanShestakov I don't think this PR is causing that error. I got something similar when I compiled a recent master having nothing to do with this PR. Out of curiosity/hunch I tried rolling back through PR #31831 (specifically to 615a4ed) and loaded the save, got the error. Went back one more commit (de083b3) and the save load error was gone.

@kevingranade kevingranade merged commit 0fe66aa into CleverRaven:master Jun 27, 2019
@AMurkin
Copy link
Contributor

AMurkin commented Jun 27, 2019

Quadruple kill!

@ghost ghost mentioned this pull request Jun 27, 2019
@ifreund ifreund deleted the active-item-safe-ref branch October 20, 2019 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
<Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code` Fields / Furniture / Terrain / Traps Objects that are part of the map or its features. Items / Item Actions / Item Qualities Items and how they work and interact
Projects
None yet
6 participants