-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Stomach code refactor #35143
Stomach code refactor #35143
Conversation
There were several issues with the previous implementation of the stomach_contents class. For instance, using the class required intricate knowledge of its implementation. Additionally, the most important and commonly performed task, digesting food every half-hour, required calling four specific methods in a specific order, when they could easily be combined into one method. Overall, the class suffered from over-complexity and I decided to fix this by making the following changes: Combined store_water, calculate_absorbed, store_absorbed, and bowel_movement into one method, "digest". Removed references to calories_absorbed and vitamins_absorbed, as these are no longer used after the above change. Combined pass_rates and absorb_rates: stomach-types never absorbed anything, and guts-types passed so slowly as to be inconsequential, so these concepts were combined into digest_rates. Whether a stomach_contents is a stomach or guts is now decided when the object is constructed; it was already that way in practice, I just made it official.
Now that vitamins aren't disappearing, this test can be tuned to make sure that vitamins are being absorbed into the body correctly.
- Replace auto with type specification. - Add const where applicable - Replaces 'player' type parameters with 'needs_rates' Co-Authored-By: Curtis Merrill <[email protected]>
set_thirst( std::max( | ||
-100, get_thirst() - units::to_milliliter<int>( digested_to_guts.water ) / 5 ) ); | ||
guts.ingest( digested_to_guts ); | ||
if( !is_npc() || !get_option<bool>( "NO_NPC_FOOD" ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if( !is_npc() || !get_option<bool>( "NO_NPC_FOOD" ) ) { | |
if( !( is_npc() && get_option<bool>( "NO_NPC_FOOD" ) ) ) { |
IMHO this is more readable. "If it's not an NPC and we're using no NPC food" as opposed to "if it's not an NPC or we're not using no NPC food".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"If it's not an NPC and we're using no NPC food"
No, this isn't a correct articulation of your version. That wording would represent
if( !is_npc() && get_option<bool>( "NO_NPC_FOOD" ) ) {
which isn't what we want. Your code itself is logically equivalent to mine, but I don't agree that yours is more readable.
I do agree that mine isn't particularly readable either though, I'll add a comment that explains it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the parenthesis don't come across in the word version I wrote earlier. My code is more like "Unless it's an NPC and we're using No NPC Food". Instead of a comment it could move to a should_process_food
method and then if( should_process_food() ) {
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still have to disagree, I think my version is more readable, and I don't want to add a function to a class we're trying to deprecate. I did add a comment though. I'll leave this conversation unresolved though so whoever goes to merge this can let me know if they think a change needs to be made.
Marking this as [WIP] until I manage to extirpate all references to the |
The previous ingest function in stomach.cpp used the 'player' object, which we are moving away from. Since the only call of that function was in the 'player' scope, it was trivial to move that code there and call the new, more generalized ingest function instead.
Stomach capacity was always based on the player's mutations, no matter who owned said stomach. Now the capacity function takes a Character reference as a parameter, and uses that Character's mutations instead.
Okay I'm happy with this now. |
* Remove unused methods from stomach.cpp * Combine several methods so stomach.cpp is simpler There were several issues with the previous implementation of the stomach_contents class. For instance, using the class required intricate knowledge of its implementation. Additionally, the most important and commonly performed task, digesting food every half-hour, required calling four specific methods in a specific order, when they could easily be combined into one method. Overall, the class suffered from over-complexity and I decided to fix this by making the following changes: Combined store_water, calculate_absorbed, store_absorbed, and bowel_movement into one method, "digest". Removed references to calories_absorbed and vitamins_absorbed, as these are no longer used after the above change. Combined pass_rates and absorb_rates: stomach-types never absorbed anything, and guts-types passed so slowly as to be inconsequential, so these concepts were combined into digest_rates. Whether a stomach_contents is a stomach or guts is now decided when the object is constructed; it was already that way in practice, I just made it official. * Make all_nutrition_starve_test far more strict Now that vitamins aren't disappearing, this test can be tuned to make sure that vitamins are being absorbed into the body correctly. * Implement suggestions from KorGgenT - Replace auto with type specification. - Add const where applicable - Replaces 'player' type parameters with 'needs_rates' Co-Authored-By: Curtis Merrill <[email protected]> * Remove unused #includes * Refactor ingest function The previous ingest function in stomach.cpp used the 'player' object, which we are moving away from. Since the only call of that function was in the 'player' scope, it was trivial to move that code there and call the new, more generalized ingest function instead. * Fix bug with stomach capacity calculation Stomach capacity was always based on the player's mutations, no matter who owned said stomach. Now the capacity function takes a Character reference as a parameter, and uses that Character's mutations instead.
This reverts commit 927e94d.
Summary
SUMMARY: Infrastructure "Refactor stomach code for readability and reduced complexity"
Purpose of change
Overall, the stomach_contents class suffered from over-complexity. Let me explain some of the issues it has:
Refactoring this code will make it much easier to maintain imho.
Describe the solution
Changes (in the order they were applied):
store_water
,calculate_absorbed
,store_absorbed
, andbowel_movement
into one method,digest
. This new method just returns the nutrients that were processed, and leaves it up to the method caller to decide what to do with them.Describe alternatives you've considered
I really wanted to additionally replace the
player&
argument that several methods have with a const member variable provided by the constructor, but I kept running into issues with that that I couldn't figure out.Testing
It passes all the unit tests related to eating and hunger, which are quite specific and would probably catch if something was terribly wrong. I have also played a character for a week of in-game time with the changes, and haven't encountered any bugs or unexpected differences from the previous functionality.