Fix multi-counting byproducts in batch food crafting #38127
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
SUMMARY: Bugfixes "Fix multi-counting byproducts in batch food crafting"
Purpose of change
Fixes #38071.
That bug was caused by byproducts of food crafting recipes getting progressively multi-counted when batch-crafting food that had variable calories and byproducts. This could lead to situations where you were crafting canned juice with negative calories.
As far as I can tell, this bug is currently confined to canned juice recipes but is theoretically possible in any food recipe with variable calories and byproducts, where the output is charge-based (e.g. a liquid in a can).
Describe the solution
There was a loop in complete_craft that was adding byproducts to the "used" list for every outputted item. So item 1 would have correct calorie counts, but every subsequent item had calories lower than it should by the previous total of the byproduct calories.
I fixed this by cloning the "used" list every time byproducts are calculated, so the byproducts did not accumulate.
Describe alternatives you've considered
There may be some more efficient way to achieve what I did without having to copy the used list every time.
Testing
Tested before and after crafting fruit juice, and calculated the caloric content expected. The result matched expected. Here's a screenshot from crafting 4 pear juice:
Additional context
As with previous C++ commits from me, I'm extremely rusty in C++, so if someone has a better way to do this please let me know.
I was a little fearful at first that doing this may have unexpected consequences, like somewhere in the code that somehow expected the byproduct list to grow as items were produced, but after reviewing I don't believe that is the case. However it's possible I've missed some obscure annals of the code base.