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

Generate NPCs more safely in tests and fix multi-submap shifting #36837

Merged
merged 8 commits into from
Jan 12, 2020

Conversation

jbytheway
Copy link
Contributor

@jbytheway jbytheway commented Jan 9, 2020

Summary

SUMMARY: None

Purpose of change

To stop the ongoing Travis failures for the Mingw test (see e.g. this one).

After some analysis (with the help of the new improved Windows backtraces, thanks @Qrox!), I tracked this to some oddities with the NPC position values in the check_npc_behavior_tree test.

Then that fix led to another obscure failure involving the active item cache, which I ultimately tracked down to being caused by shifting by more than one submap at a time.

Describe the solution

This turned out to be because the NPCs the test constructed in a rather ad hoc manner violated the invariants for the NPC class (in particular, its two position members were inconsistent).

To avoid that, create the NPC via the new, safer spawn_npc function from player_helpers.h. I created that by factoring out the spawn_npc lambda that was previously in npc_test.cpp.

The ranged balance tests had a similar problem. Fixed those by changing how standard_npc works. You now pass a position to its constructor, since its not safe to call set_pos on it.

To fix the submap shifting issue, add a checking in map::shift that the function parameters are within the allowed range, and then update game::update_map to use map::shift correctly in more cases.

Describe alternatives you've considered

Trying to make the way NPCs are spawned a little less confusing. Right now there are various spawn and place functions, and it's hard to understand how they all tie together.

Could perhaps have changed the check_npc_behavior_tree test to use standard_npc, but I didn't know it existed at the time.

Testing

Found a reliable way to reproduce a failure like that seen on Travis, and saw that it went away with this fix.

Add a regression test for the map:shift issue.

Ran the rest of the unit tests too.

Tested briefly in-game to ensure I can walk and teleport between submaps correctly.

@I-am-Erk I-am-Erk added [C++] Changes (can be) made in C++. Previously named `Code` Code: Tests Measurement, self-control, statistics, balancing. NPC / Factions NPCs, AI, Speech, Factions, Ownership labels Jan 9, 2020
@jbytheway
Copy link
Contributor Author

So now a different consistent Mingw error has replaced the one I fixed. Curious. I'm looking into it.

tests/player_helpers.cpp Outdated Show resolved Hide resolved
@jbytheway jbytheway changed the title Generate NPCs more safely in tests Generate NPCs more safely in tests and fix multi-submap shifting Jan 11, 2020
@jbytheway
Copy link
Contributor Author

OK, that fix led to two more issues, but I believe they're all fixed now.

The tests have a clear_player helper.  I wanted to use it with an NPC,
so I've split it into clear_avatar for the avatar, and clear_character
for a generic character.  Currently clear_character actually takes a
player& argument, because not all the things it clears are yet in
Character, but that's the direction we're headed.
Previously npc_test.cpp had its own spawn_npc helper.  Pull that out
into player_helpers, so that it can be used in other tests.
We were seeing intermittent failures on Mingw in
check_npc_behavior_tree.

This turned out to be because the NPCs the test constructed in a rather
ad hoc manner violated the invariants for the NPC class (in particular,
its two position members were inconsistent).

To avoid that, create the NPC via the new, safer spawn_npc function from
player_helpers.h.
map::shift is only supposed to shift a single submap at a time.

However, game::update_map could sometimes shift it more than one at a
time.

Add a check in map::shift for this error, and update game::update_map to
not cause it.
Pull out the interesting part of the just-resolved bug and make a new
unit test for it.
This allows tests to avoid calling set_pos on such, which is not safe,
since it assumes the npc is a 'real' one.
@kevingranade kevingranade merged commit f293533 into CleverRaven:master Jan 12, 2020
@jbytheway jbytheway deleted the test_npc_in_overmap branch January 12, 2020 13:48
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: Tests Measurement, self-control, statistics, balancing. NPC / Factions NPCs, AI, Speech, Factions, Ownership
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants