NPCs do not consistently use their ranged weapons
Describe the bug
NPCs at least 8 months ago reliably used their crossbow or other ranged weapon they started equipped with, only beginning melee attacking where it was reasonably practical to do so.
Currently they don't seem to bother getting a shot off before in melee range, they sometimes do but I'm unable to replicate behavior similar to older behavior in this regard generally, I'm unsure what has actually changed. Aim time doesn't seem to be the problem, as telling them to not spend time aiming does not fix this.
This makes any NPC that isn't melee based functionally useless in combat.
Steps To Reproduce
Recruit random NPC in shelter who has ranged weapon Get into combat Chances are they won't bother using it to their advantage even when shorter aim time is specified
Expected behavior
An NPC with a crossbow will get a shot off before engaging in melee combat with a zombie, significantly weakening their foe before risking melee damage.
Versions and configuration
OS: Windows OS Version: 8.1 Game Version: 0.D-11962-ge228853 [64-bit] Graphics Version: Tiles Mods loaded: [ Dark Days Ahead [dda], Disable NPC Needs [no_npc_food], Magiclysm [magiclysm], No Rail Stations [No_Rail_Stations], sees-player icon, retrodays [sees_player_retro], Bionic Slots [cbm_slots], SpeedyDex [speedydex] ]
Have you seen this with ranged weapons generally, or specifically crossbow/archery weapons? There was a big rework of archery recently.
most definitely is not anything to do with the archery update, if anything they'd have more reason to use bows.
Happened in builds long before it, I just didn't tolerated it more and should've reported it sooner.
If someone wants to tackle this, look in npcmove.cpp method_of_attack()
be sure to take a few swigs of calming whisky beforehand, you'll need it.
This issue has been mentioned on Cataclysm: Dark Days Ahead. There might be relevant details there:
https://discourse.cataclysmdda.org/t/no-hope-mod-v2-6/24971/25
At what ranges are we talking, relative to how certain they're supposed to be before they fire?
There are recent reports that NPCs occasionally throw their ranged weapon entirely, rather than using it. That seems like a symptom of this issue, so I'm combining them here.
What makes this issue challenging is that this behavior appears sporadic/erratic.
this is an issue from 2020 and nobody has tried to fix it?
RIP
Yeah, you shame those devs for not getting to every issue that pops up on these boards right when it pops up! That'll prompt them to respond more quickly!
It's like all the monetary rewards, notoriety, & endless thanks from grateful players mean nothing to them. Even when contributors comment on old threads, it's not like they're organizing in preparation for action; they're simply dredging up the past to rub it in our faces! It's great to see some1 like you, coyo7e, taking action where all the rest of us have faltered.
@coyo7e: AFAIR there were some attempts but they went nowhere - mostly because the issue is so inconsistent.
@LeahLuong: Please tone down the sarcasm?
@Zireael07 "Toned-down" sarcasm online is too often misinterpreted as sincerity. Please clarify your position. Is your intent to control my speech here?? If you've an aversion to sarcasm of any kind, you may want to reconsider the frequency of your internet usage.
Not sure why you marked my comment w/ the "confusion" emoji when you obviously detected its tone. I'm confused by it but will refrain from emoji use in this case.
Experienced a bandit throw their pistol at me, very odd!

@LeahLuong they don't mean tone down your delivery, they mean stop being hostile. Yes the comment you were replying to was hostile and unnecessary, but your reply is equally hostile and unnecessary.
Don't try to manage other people's commentary here. Just focus on your own.
@kevingranade I was sticking up for devs like yourself. My intent was to respond to coyo7e in a humorous way in order to show him how hostile & unnecessary his comment was. If comments such as coyo7e's are like water off of a duck's back to you, I'll try to refrain from such responses in the future.
@LeahLuong: Your triaging and debugging is appreciated. Your attempts at humor... don't strike many people as humorous, so please refrain from those
Just tested at Bandit camp and all of them with guns appeared to fire at me. I'm not sure if they fired til they ran out but they continued to hit me with the guns after I closed to melee range. Other testers would be appreciated.
testing it with npc followers makes it definitely seem like this is still an issue. If you give them a powerful weapon like an m4, they will usually always opt to use it, however if you give them any low power ranged weapon like a bow, crossbow, or a .22 gun, they will usually opt to use it in melee, but will sometimes fire it under conditions I cannot reproduce.
Trying to circumvent this by setting their ai to "Don't move and engage targets at range" will make them nonstop try to move into you and spit out messages that they want you to move aside, while ignoring the zombie threat. This part might be a separate issue, but it is also an issue regardless.
testing it with npc followers makes it definitely seem like this is still an issue. If you give them a powerful weapon like an m4, they will usually always opt to use it, however if you give them any low power ranged weapon like a bow, crossbow, or a .22 gun, they will usually opt to use it in melee, but will sometimes fire it under conditions I cannot reproduce.
Trying to circumvent this by setting their ai to "Don't move and engage targets at range" will make them nonstop try to move into you and spit out messages that they want you to move aside, while ignoring the zombie threat. This part might be a separate issue, but it is also an issue regardless.
Because followers have more rules than hostile NPCs I think it would be better for them to have a specific issue around their weapon choices. But that’s my opinion I’m not sure it’s necessary.
testing it with npc followers makes it definitely seem like this is still an issue. If you give them a powerful weapon like an m4, they will usually always opt to use it, however if you give them any low power ranged weapon like a bow, crossbow, or a .22 gun, they will usually opt to use it in melee, but will sometimes fire it under conditions I cannot reproduce. Trying to circumvent this by setting their ai to "Don't move and engage targets at range" will make them nonstop try to move into you and spit out messages that they want you to move aside, while ignoring the zombie threat. This part might be a separate issue, but it is also an issue regardless.
Because followers have more rules than hostile NPCs I think it would be better for them to have a specific issue around their weapon choices. But that’s my opinion I’m not sure it’s necessary.
it seems like this issue was about npc followers in the first place, and specifically mentions the issue happening with a crossbow.
On another note, i tested giving bows/crossbows/ .22 guns to hostile npcs with the right strength, and they also tried to use them in melee most of the time.
It might actually be that the weapons would simply do more damage in melee, and the npcs aren't taking into account the time or risk factor of walking into melee. This might still be intended behavior, if the "don't move and shoot" option wasn't also seemingly broken.
That’s actually a good idea that NPCs should value melee dps at 60-75% of ranged dps.
Perhaps this bug is because the algorithm that chooses the 'best' weapon is judging the weapon effectiveness values without reading what is actually in them, choosing instead based on how many numbers are in the string. Ie, if a throwable soda can returned a value of 12, but a gun returned a value of 96 - the algorithm wouldn't be able to meaningfully tell the difference, possibly resulting in a npc using soda can over a gun. Perhaps contributing to the randomness of this bug sometimes working, sometimes not.
The parent function of npc::evaluate_best_weapon() in npcmove.cpp appears to be the beginning of how a npc chooses its weapon, it scans each item in its inventory for weapon type, via multiple evaluate()s in npc_attack.cpp finding a tripoint vector::npc_attack_rating for each. Then it runs npc_attack_rating() on the vector to calculate its effectiveness as an attack, returning that as an integer.
Here's the declaration for npc_attack_rating

So if it's returning integers (or anything else) per item to choose from for a weapon - the choosing compare() function should instead be something else to perform math to check the integers are bigger/smaller rather than counting how many characters the integer has? That appears to be how compare() works in C++
Heres the parent part where it appears to run compare()

That appears to be how compare() works in C++
Compare is a lambda function in the code you posted, and it uses npc_attack_rating::operator>(const npc_atack_rating &) to compare two attacks.
It is not using std::string::compare, which I think is what you are talking about.
you know what? fine.
I would suspect that a not horrifically complex way to resolve this would be to make the melee weapon valued less by the NPC for every tile, plus one or two (to give them time to actually switch from melee to ranged) after the range the weapon has, but maybe this is more complex than I suspect to achieve.
Inversely, for ranged weapons, they should have less value to the NPC in this calculation as enemies get to a closer range!
Yes, the way the game currently gets the 'best' weapon selected is a single catchall function that checks every item that has the ability to inflict damage, including socks, beer cans, toothbrushes, precious musical vinyl records etc which is a little silly, but it is what it is...
I think a simple and effective way to fix this would be to copy and modify the original check into two faster (smaller range of items to iterate through) custom checks before this that check first the distance of the enemy - when the enemy is far away, then run a check on ONLY ranged weapons (gun, bow, etc categories) damages, move on to next check; if none are found or distance of enemy is extremely close, proceed to check on ONLY melee range weapons, if none are found, proceed to the more intensive default check of every item in the inventory.
I remember this one. I recall NPC combat behavior was influenced by their personality settings, different for each NPC. Does this explain some of the intermittent behavior? I would always set my shooters to have an Aggression of 10 and then they would consistently shoot, usually at short range, but this had a drawback---too high and they can turn on you.
Personality factors are not shown in the NPC menu, but are shown in Debug. I would always need to edit the save to adjust the Aggression.