Skip to content

NPC AI: Fleeing optimizations #81866

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

I-am-Erk
Copy link
Member

@I-am-Erk I-am-Erk commented Jul 17, 2025

Summary

Features "NPC AI fixes to improve fleeing in bad situations, reduce it in good ones"

Purpose of change

NPCs still run away wrong. I worked hard on it, but it needed playtests, it's had almost two years and now I have the reports back.

  • NPCs overrate swarms. A group of heavily armoured NPCs with guns will still run from a dozen basic zombies, because the zombies multiply threat as they increase in number.
  • NPCs underrate swarms. A poorly armed NPC tries to hold their ground against impossible odds.
  • NPCs overrate the threat of the player due to stay in formation bonuses. This is important, but if the NPC should be running for their goddamn life, having the player come to their rescue should not stop that.
  • Personality effects can be a bit too strong, "suicidally brave" kicks in too soon.
  • the NPC threat cap is causing problems. I do not want to remove it entirely, but I am thinking of adding some falloff effects so that the level can keep rising, but with diminishing returns.

Describe the solution

This will not solve all the problems but I wanna get some easy ones. Will update as I go through.

  1. NPCs previously added their aggression and bravery to their self evaluated threat, for a max of +/- 20 points. 20 points is a lot if you're an unarmed schmuck, and very little if you have bulletproof armour and an AR15. Instead of this, I have changed it to a multiplier, so the effects are more profound at high levels and not as big a deal for a weak unarmed loser. At absolute max aggression + bravery, the NPC will triple their self-evaluated threat, and at absolute minimum, they'll cut it by 1/3. At very low values this will bump them from like a 3 (run from a zombie) to a 9 (fight two zombies). At high values it kicks in more, which I think makes sense: "I have a pistol, I am rambo" seems more like what we're going for here.
  2. NPCs currently assess "swarm" values for monsters that are in close range, and "horde" values for monsters at medium range, but what they consider medium range is quite long, and swarm is quite close. This means that melee characters haven't been evaluating swarms as dangerous enough, and have been overvaluing hordes. I reduced the radius melee characters count monsters in, but increased what they consider a close in swarm. Then, I made them ignore monsters that are much weaker than their defensive self assessment, so your heavily armoured NPC won't get too bothered by a swarm of low grade regular zombies.
  3. I made the "bravery boost" of nearby allies increase slowly over a few turns, so if the player comes and stands beside NPC but they were about to run, they'll still almost certainly run, unless they'd only barely decided to do so before. However as time goes on they'll become more confident, creating the important "stay in formation" bonus from before to stop them running away if you're holding the line together. They also factor your trust in a little although I'd like it to be more important.
  4. I'm considering having the NPC threat cap go up linearly until 200, then fall off so that every 2 points of evaluated threat is at first +1, then every 3 points, etc. I will have to review the math. this allows NPCs to evaluate their threat to infinite levels so that amazing super uber gear does matter, but maybe stop them from getting too much god syndrome. Let's see. I havent done this yet

Describe alternatives you've considered

I've also been thinking of ways to apply this outside the player faction NPCs and that could help here, but I think I'll save that for later.

Many people want the ability to call a retreat. Including me, I really want that. However, I feel it's vitally important to avoid adding that until the NPCs can already do a pretty OK job of assessing it themselves... otherwise, I won't get this vital playtest data, because we'll all get used to micromanaging the dumb little dudes, and it'll never be fixed. Don't worry, once we're happy with their fleeing ability, there will absolutely be both "Run away!" and "Hold the line!" commands to help bolster their AI. I would also like these commands to be available to NPCs who have a party leader amongst themselves, so that the AI running the bandits can also call a retreat.

My threat assessment code is still mostly for melee npcs. I have some ideas for ranged NPCs but that's for another PR, I really want to get melee NPCs to a point that we're all happy with first.

Testing

Compiles, and the game runs, but a completely unrelated error about vehicle parts crashed it before i could get decent playtest data. Will try again tomorrow, taking it out of draft though since at least I can see it runs.

Additional context

My kid is helping me with this, it's amazing. If you ever feel like you can't code, remember, my 13 year old is helping me with this. You can do it! I believe in you.

Making this a multiplier keeps it from skewing results too far at low combat levels, but it should still be important
@github-actions github-actions bot added NPC / Factions NPCs, AI, Speech, Factions, Ownership [C++] Changes (can be) made in C++. Previously named `Code` <Enhancement / Feature> New features, or enhancements on existing json-styled JSON lint passed, label assigned by github actions labels Jul 17, 2025
I-am-Erk added 2 commits July 16, 2025 19:48
medium range is closer, but close range is farther.
@Nebnis
Copy link
Contributor

Nebnis commented Jul 17, 2025

would be nice if raising their opinion also made them "suicidaly braver" Because your best friend got your back and won't run away even against the worst odds unless they are alone ofc

@I-am-Erk
Copy link
Member Author

would be nice if raising their opinion also made them "suicidaly braver" Because your best friend got your back and won't run away even against the worst odds unless they are alone ofc

Yes, thought thr same thing last night. I'm going to make the "player is nearby" effects cue of the NPCs trust of the player a lot more.

@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Jul 18, 2025
@github-actions github-actions bot added the astyled astyled PR, label is assigned by github actions label Jul 18, 2025
@I-am-Erk I-am-Erk marked this pull request as ready for review July 18, 2025 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions [C++] Changes (can be) made in C++. Previously named `Code` <Enhancement / Feature> New features, or enhancements on existing json-styled JSON lint passed, label assigned by github actions NPC / Factions NPCs, AI, Speech, Factions, Ownership
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants