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

Fix pocket obtain shenanigans #40322

Merged
merged 4 commits into from
May 9, 2020

Conversation

nphyx
Copy link
Contributor

@nphyx nphyx commented May 8, 2020

Summary

SUMMARY: Bugfixes "fixes occasional debugmsg and segfaults caused by using items in inventory"

Purpose of change

Using items via the item action menu would occasionally cause a debugmsg, sometimes followed by a segfault, and in both cases the used item vanished and the activity failed.

I tracked this problem down to avatar_action::use_item where the debugmsg is thrown, and further to some shenanigans in item_location::obtain() for item_in_container variants.

The theory is that during obtain(), the item gets moved from its original location and placed in a player's inventory; when it's being obtained from a container, that container is sometimes a pocket on the player, and sometimes item gets placed back in the same pocket. When remove_item() is subsequently called the item then gets deleted, and the returned item_location has a null target item.

Describe the solution

Check if the item's container is already held by the player, and if so charge the move cost to obtain the item but do not move it around, since it's already in the player's inventory.

Describe alternatives you've considered

I can't think of a more elegant way to fix this. One option was further hacks and special case checks in use_item around the debug message. That worked but was ugly (also, this problem may crop up elsewhere so not a total solution).

Testing

  1. created a situation where I could readily reproduce the bug
  2. caused it repeatedly and verified that it was always the same circumstance (nullptr on an item_location's target item)
  3. applied this fix and verified that the bug no longer happened

Additional context

This one is really difficult to reproduce consistently. Even sharing my save file with KorG where I'd set up the circumstance, he could not reproduce it.

So I'm not sure how anybody else can verify this works unless they can also get the bug to happen for them. I'm also not 100% sure my diagnosis describes what's going on adequately.

I do believe this is a valuable safety/sanity check regardless of whether it solves a problem immediately for anyone other than myself.

Side effect

This fixes the problem brought up in the now-deleted TODO in the diff: the player will be charged an appropriate amount of moves for obtaining the item regardless of where it comes from, so the refund is no longer sensible or necessary. If the costs need to be adjusted henceforth it should be done in the appropriate obtain_cost() function rather than with a hack in use_item.

src/item_location.cpp Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants