Jump to content

[41.78.16] [All] Lose nutrition when cooking with the tail end of food items


nosebleed

Recommended Posts

• Version?
41.78.16


• Singleplayer/Multiplayer?
All


• Host or dedicated?
All


• Mods?
None


• Old or new save?
New


• Reproduction steps:
Start game in debug
Spawn the following items:
-Open canned corned beef
-Cooking Pot with Water
(for exaggerating the bug):
Edit the canned corned beef
Reduce the hunger value lower than the cost of putting it in the stew, for this example use -0.06 to simulate the tail-end of a food item
Set calories, carbs, protein and fat to 1000 each
hit save
right click the pot with water then create stew with the canned corned beef we edited
Note that the calories, carbs, protein and fat are less than 1000. The can is fully used.

 

Expected behaviour is that the created stew has either the same nutrition or higher (if cook level is higher than 0). With this bug, even with cooking level 10 the nutrition values are lower than the original cans values.

 

Indepth info, and why it affects normal in-game items:
This happens for all food that you use in recipes with partial leftovers. You get these leftovers generally with a higher cooking skill.
For an example to prove it affects normal in-game items:
Spawn in an opened canned vegetable soup and another cooking pot with water
set your cook level to 3
add the first use of the can to a stew in the pot
note the calories in the pot: 136.5
note the calories remaining in the can: 11.25
add the last use of the canned vegetable soup into the cooking pot with water
final calories: 147.47
This increases the stew by 10.97 calories, after adding a can worth 11.25. Expected increased calories should have been higher than 11.25 since our cook skill is 3. This affects all nutrition values, not just calories.

 

I have a hunch that this bug will affect the nutrition from rotten food when you cook with it at 7+ skill level, since they are probably in the partial food item territory.

In the grand scheme of things this is small, but it really messes up a mod I am trying to create for cooking. This has been doing my head in until I realized it's actually a bug!

Link to comment
Share on other sites

It’s just going to be a rounding between different number types and the division thereof or loss of precision when transitioning between Java and Lua.

 

 You can’t expect high precision with this sort of thing.

Link to comment
Share on other sites

The nutrition system hasn't shown high rounding errors so far while I've been doing the mod, only at the third decimal place sometimes from what I have tested. Certainly does not explain the wild variation in the above examples nor negative values.

 

In the first example where we edit in high nutrition numbers (which should help mitigate rounding errors), it loses ~9% of the nutrition at cook 0. At cook 10, it loses ~3%. How would a rounding error cause it to go negative? also why is it by such high amounts?

Link to comment
Share on other sites

Is it wild?

 

Unless I’m misreading this, we have a loss of ~1 calorie after taking some numbers that a computer can’t accurately store as a float (like 0.1 or 0.3), doing things with them while cutting off or adding trailing digits, then displaying a further approximation of the result. It’s expected behaviour; sacrificing some accuracy for speed and the amount of space taken up in memory. This is a tradeoff that’s acceptable in games but not at a bank.

 

 

You can read more about it here: https://en.m.wikipedia.org/wiki/Floating-point_arithmetic


https://docs.python.org/3/tutorial/floatingpoint.html

Link to comment
Share on other sites

I understand what you mean by rounding error, thanks for the additional info. It is not the case with this bug report.

I've done more digging and have finally found a formula that explains the bug so I can at least in the meantime apply a workaround in my mod until it's (hopefully) fixed.


TL;DR the final use when it removes an item, the cook bonus that reduces the amount used of an item applies, when we should actually be using the entire item up, hence the lost calories. It is not a small number. This is per item use in a recipe, so if we were to minmax loses with the bug, we can lose at least 710.1 calories in the example below in a single rabbit meat stew.


Calculating nutrition given a cook skill:
example with default rabbitmeat spawned in debug mode

$foodHunger = 30
$foodCalories = 969
$recipeHungerCost = 15
$cookPerkLevel = 10

$cookReductionBonus = 1 - (0.03 * $cookPerkLevel)
$cookNutritionIncrease = 1 + ($cookPerkLevel / 15)
$reducedRecipeCost = $recipeHungerCost * $cookReductionBonus
$percentageOfFoodToUse = MIN($reducedRecipeCost / $foodHunger, 1)

$CaloriesInStew = $cookNutritionIncrease * $foodCalories * $percentageOfFoodToUse = 565.25
$foodHunger = $foodHunger * (1 - $percentageOfFoodToUse) = 19.5
$foodCalories = $foodCalories * $percentageOfFoodToUse = 629.85


These numbers checkout in-game nutrition values.

Second use of the item:

$CaloriesInStew += $cookNutritionIncrease * $foodCalories * $percentageOfFoodToUse = 1130.5
$foodHunger = $foodHunger * (1 - $percentageOfFoodToUse) = 9
$foodCalories = $foodCalories * $percentageOfFoodToUse = 290.7


second use also exactly checks out with in-game nutrition values.

 

Now this is where things get funky on the final, third use now that $foodHunger is less than $recipeHungerCost.
The $percentageOfFoodToUse never gets set to 1 in zomboid if the cook skill is higher than 0. For some reason, after wracking my brains somewhat while playing with this formula, $recipeHungerCost gets set to the same value as $foodHunger when it's the final use of a food item, but before the reduced recipe cost calculation, instead of setting $reducedRecipeCost to the value of $foodHunger. This means the reduction-bonus applies, and we lose calories since the item is tossed after this final use. To demonstrate in a formula:

$recipeHungerCost = $foodHunger = 9
$reducedRecipeCost = $recipeHungerCost * $cookReductionBonus = 6.3
$percentageOfFoodToUse = MIN($reducedRecipeCost / $foodHunger, 1) = 0.7

In this example, we only get to use 70% of the food item instead of 100%, then throw the remaining rabbitmeat away. This equation above accurately calculates what we see ingame (with a final rounding error of 0.01 for proteins,carbs and lipids vs what is displayed ingame, but calories is high enough not to have rounding errors)

 

Final values:
Stew with 3x rabbitmeat uses has calories 1469.65 as it stands in zomboid right now.

If this bug is fixed, it should be atleast 1615 calories @cook level 10. That's a 9% difference, or a 118.35 calorie difference for a single recipe ingredient in a stew which takes up to 6 ingredients. With rabbitmeat alone we could see losses up to 710.1 calories for a single stew in the right circumstances. This is a significant potential loss of nutrition during a long playthough and we're only talking about a single meal out of the hundreds if not thousands people cook.

 

Thank you if you managed to read this far. I hope I wrote clearly enough and that perhaps a dev in the future might have a look into this. I can upload a spreadsheet file that I used to figure all this out, it might be a little easier to follow than the wall of text above.

Link to comment
Share on other sites

Those 710.1cal loses do not take into account the food use reduction bonus at all, only the calorie increase bonus. If that bonus were to apply on the final use in a way it was intended instead of subtracting, the loses are actually even higher in this bug.

Link to comment
Share on other sites

edit: missed the post about rabbit stew. Makes a lot more sense as to

why this would be a problem. I’ll leave the following up however in case it’s at all helpful and double check.
 

? What I think you’ve explained previously is …


Level 7 cooking.

 

Can of fresh corned beef starts at 720.

 

 It rots.

 

 Great. Now it’s 720 * 0.0386 … Whole 27 cal left that I can salvage thanks to the cooking skill.

 

I split it into portions for some reason. Now it’s 9 cal.


I gain 30% cal because of my cooking skill.

 

Great. Now it’s 12.05 cal. Or 11.45. Or something close enough, because  internally these numbers could be something like 0.3860000073909759521484375 due to the nature of floats. 0.3, 0.24, and 0.77 within the calc introduce further errors, compounding for a swing (in either direction) of around 1 cal.

 

I assume that’s what you’re talking about as otherwise I don’t know how you could get to the point where you only have 11 cal left over. In this case, you’re not losing 710 cal. You never had it in this scenario because the food rotted.

 

 (Mind is without testing it in game. Will double check later.)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...