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

Refactor eating morale modifiers and messages #37713

Merged
merged 10 commits into from
Feb 9, 2020

Conversation

wapcaplet
Copy link
Contributor

@wapcaplet wapcaplet commented Feb 4, 2020

Summary

SUMMARY: Infrastructure "Move food morale modifiers to modify_morale"

Purpose of change

Purely code refactoring/cleanup - separation of responsibility, and improved readability.

Describe the solution

It moves sections of player::eat concerned only with modifying morale to the more appropriate function, Character::modify_morale, and refactors a couple sections.

Describe alternatives you've considered

Considered leaving the morale effects in player::eat.

Testing

Used vim to ensure no undefined variables were left around.

Regression testing: Used debugging menu in game to mutate / spawn items and eat various morale-modifying foods, and ensure the correct messages and morale effects happened, including but not limited to:

  • Eat at a table, with/without table manners
  • Eat junk food with a sweet tooth (positive morale) or as a carnivore (negative morale)
  • Eat human flesh as a cannibal (+psycho, +spiritual)
  • Eat honey as an Ursine

Additional context

This was intended as part of a patch for #7533 but that's a separate problem (and harder for me to solve), so I thought it worthwhile on the basis of cleanup alone.

Morale changes are already (supposedly) handled in
`Character::modify_morale`, called by `Character::consume_effects`, but
large swaths of `player::eat` were taken up by code exclusively related
to morale modification. I'm moving this where I think it belongs.

Refactoring to support CleverRaven#7533

I read through all of the code carefully before moving, and used vim to
ensure no undefined variables were left around.

Tested in-game with various consumables, and ensured the morale bonuses
and messages were still correctly displayed.
@ZhilkinSerg ZhilkinSerg added [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Items: Food / Vitamins Comestibles and drinks labels Feb 5, 2020
Copy link
Contributor

@codemime codemime left a comment

Choose a reason for hiding this comment

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

This is a very good change. However, some further improvements can be made to the moved code.

src/consumption.cpp Outdated Show resolved Hide resolved
src/consumption.cpp Outdated Show resolved Hide resolved
src/consumption.cpp Outdated Show resolved Hide resolved
src/consumption.cpp Outdated Show resolved Hide resolved
src/consumption.cpp Show resolved Hide resolved
src/consumption.cpp Outdated Show resolved Hide resolved
@wapcaplet wapcaplet changed the title Refactor eating morale modifiers and messages WIP Refactor eating morale modifiers and messages Feb 5, 2020
@wapcaplet
Copy link
Contributor Author

This is a very good change. However, some further improvements can be made to the moved code.

@codemime Thanks for the suggestions! I will clean these up the best I can.

The part about looking for a nearby table is almost duplicated (along with the multiple invocations of veh_at) in two other source files as well, with the only difference being those are looking within a radius of 2 instead of 1:

bool has_table_nearby = false;
for( const tripoint &pt : g->m.points_in_radius( u.pos(), 2 ) ) {
if( g->m.has_flag_furn( flag_FLAT_SURF, pt ) || g->m.has_flag( flag_FLAT_SURF, pt ) ||
( ( g->m.veh_at( pt ) && ( g->m.veh_at( pt )->vehicle().has_part( flag_KITCHEN ) ||
g->m.veh_at( pt )->vehicle().has_part( flag_FLAT_SURF ) ) ) ) ) {
has_table_nearby = true;
}
}

bool has_table_nearby = false;
for( const tripoint &pt : g->m.points_in_radius( src_loc, 2 ) ) {
if( g->m.has_flag_furn( flag_FLAT_SURF, pt ) || g->m.has_flag( flag_FLAT_SURF, pt ) ||
( ( g->m.veh_at( pt ) && ( g->m.veh_at( pt )->vehicle().has_part( flag_KITCHEN ) ||
g->m.veh_at( pt )->vehicle().has_part( flag_FLAT_SURF ) ) ) ) ) {
has_table_nearby = true;
}
}

So it's clear we may need a standalone has_table_nearby function, with a distance argument - should that be a separate PR, or would it be appropriate to roll it into this one? I would prefer not to touch more files than necessary, so my plan for now is to tidy up the morale code that I moved, and consider opening a new PR to refactor those other uses of has_table_nearby.

@wapcaplet wapcaplet changed the title WIP Refactor eating morale modifiers and messages Refactor eating morale modifiers and messages Feb 6, 2020
@wapcaplet wapcaplet changed the title Refactor eating morale modifiers and messages WIP Refactor eating morale modifiers and messages Feb 7, 2020
@wapcaplet
Copy link
Contributor Author

New has_nearby_table function factored out and merged in PR #37790 - today I will finish cleaning up and testing modify_morale using the new function.

@wapcaplet wapcaplet changed the title WIP Refactor eating morale modifiers and messages Refactor eating morale modifiers and messages Feb 8, 2020
@wapcaplet
Copy link
Contributor Author

wapcaplet commented Feb 8, 2020

I've done another pass of regression testing and this looks ready to go.

There's a travis-ci failure but I don't think I can do anything about it:

Reading package lists...
Err:4 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial InRelease
  Could not connect to ppa.launchpad.net:80 (91.189.95.83), connection timed out
Err:5 http://apt.llvm.org/xenial llvm-toolchain-xenial-8 InRelease
  Could not connect to apt.llvm.org:80 (151.101.250.49), connection timed out
W: Failed to fetch http://apt.llvm.org/xenial/dists/llvm-toolchain-xenial-8/InRelease  Could not connect to apt.llvm.org:80 (151.101.250.49), connection timed out
W: Failed to fetch http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/dists/xenial/InRelease  Could not connect to ppa.launchpad.net:80 (91.189.95.83), connection timed out
W: Some index files failed to download. They have been ignored, or old ones used instead.

src/consumption.cpp Outdated Show resolved Hide resolved
For symmetry with `map::has_nearby_chair`, possible future re-use, and
to simplify the `Character::modify_morale` function slightly.
@codemime codemime self-assigned this Feb 8, 2020
src/map.cpp Outdated Show resolved Hide resolved
Co-Authored-By: codemime <codemime@gmail.com>
@wapcaplet wapcaplet deleted the refactor-eating-morale branch March 21, 2020 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Items: Food / Vitamins Comestibles and drinks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants