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

NPC AI logic and test issues #27438

Merged

Conversation

jbytheway
Copy link
Contributor

@jbytheway jbytheway commented Jan 4, 2019

Summary

SUMMARY: Bugfixes "Fix NPC weapon wielding turn cost"

Purpose of change

I started looking into this because of the occasional test failure involving npc-movement, specifically the NPC in vehicle should not escape from dangerous terrain was failing.

After some investigation it turned out that this test was only ever passing most of the time due to a bug, and when that bug was avoided it would fail.

This PR fixes the bug and the test.

Describe the solution

The point of this test is that NPCs should normally try to escape hazardous terrain, but not if they are in a vehicle. So it checks that the non-vehicle NPC moves, and the in-vehicle one does not.

The trouble is that there are many other possible reasons for an NPC to move. When there's nothing else in particular to do they will go looking for some distant item, and move; that's what caused the test failure.

Usually this didn't happen, because of a bug in the weapon-wielding logic. When an NPC gets new items they check to see whether any is a better weapon. These newly-created NPCs all had new items and all did this check.

It was supposed to be that when they had a better weapon they would wield it (spending their turn) instead of moving. What actually happened is that they would spend their turn when they didn't wield a new weapon (and not spend it when they did).

So, the test failure would happen for NPCs who either had no new items or happened to be holding a better weapon (a somewhat rare event). It didn't always fail even then; it also had to be that they found a useful path towards whichever item they decided to seek.

So, there are two changes here:

  • Fix the bug so NPCs spend their turn when they do wield a weapon, rather than when they do not.
  • Assign all NPCs in this unit test the NPC_MISSION_SHOPKEEPER mission. This pre-empts the more general 'go find random stuff' logic and means that they will stand still unless in peril.

Describe alternatives you've considered

I thought about removing the non-follower NPC in this situation (because I think the follower NPC wouldn't have had this issue, and would still test the feature adequately).

Additional context

@mlangsdorf Review would be appreciated.

One of the NPC movement tests checks that an NPC in a vehicle on
hazardous terrain does not move.  The intention is that the NPC should
not be fleeing the hazardous terrain.  Unfortunately there are other
reasons why NPCs may move (such as long-term goals to acquire ammo
etc.).

Resolve this by giving the NPCs in the test a mission which causes them
to stay still unless in peril.
To see which NPC is causing a test failure.
npc::scan_new_items() was supposed to return true if some action was
taken, but the logic was reversed, because npc::wield_better_weapon()
returns true if a weapon was wielded.

The upshot was that NPCs were being charged for the time to equip a
weapon when they got new items but *didn't* equip a weapon.

Rearrange the logic to make more sense.
Copy link
Contributor

@mlangsdorf mlangsdorf left a comment

Choose a reason for hiding this comment

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

I am very worried that have become the NPC AI expert, because I know almost nothing about implementing AI.

@mlangsdorf mlangsdorf added NPC / Factions NPCs, AI, Speech, Factions, Ownership Code: Tests Measurement, self-control, statistics, balancing. [C++] Changes (can be) made in C++. Previously named `Code` <Bugfix> This is a fix for a bug (or closes open issue) labels Jan 5, 2019
@kevingranade kevingranade merged commit c5361b5 into CleverRaven:master Jan 5, 2019
@jbytheway jbytheway deleted the npc_ai_logic_and_test_issues branch January 5, 2019 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
<Bugfix> This is a fix for a bug (or closes open issue) [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.

3 participants