cataclysmbnteam / Cataclysm-BN

Cataclysm: Bright Nights, A fork/variant of Cataclysm:DDA by CleverRaven.
https://docs.cataclysmbn.org
Other
697 stars 272 forks source link

Extreme slowdown with NPC Farming assignments #1500

Closed AlwaysElise closed 2 years ago

AlwaysElise commented 2 years ago

Description

While player faction NPCs at a faction camp are attempting to do farming tasks, game ticks take extraordinarily long (>1 second). I have a 100% repro save (0.5GB unfortunately) where it is occurring during the player doing autodoc surgery, effectively softlocking the game on the issue. Break All on the application while this is occurring results in a call stack consistently within the NPC behavior code for farming; with an NPC repeatedly planning out a Planting action, which requires acquiring a tool for Tilling, and Fetching a shovel to do so. In range, both a shovel and halligan bar are detected; after letting it run for a while, this behavior continues indefinitely, suggesting the NPC never managed to successfully Fetch the tools. Commenting out code associated with locating a tool to Fetch resolved the issue on a copy of the save. See Additional Context for a bit more info on my testing of it.

Prior to this, during normal play, farming tasks seemed to do similar things for shorter periods of time, slowing down the speed for the player to make a movement into the seconds range.

Steps To Reproduce

  1. Create a faction camp with an NPC at a typical survivor base (lot of items increases the search cost and makes the issue more visible; in my case, it was about 600 items in my camp, and a few hundred farming tiles), and set up a variety of typical zones for it.
  2. Ensure the NPC doesn't have digging equipment, but that equipment is present nearby (in my case, was in a Tools loot location overlapping a Base Storage)
  3. Bring the NPC outside and close the window between them and the digging tool. Impassable furniture installation may also work?
  4. Sleep; it now takes significantly longer.

Expected behavior

NPC completes tool Fetch task without getting stuck repeatedly doing very expensive queries on what is a fairly standard survivor camp setup.

Versions and configuration

Reproduced with a save (alas, 0.5GB) in both a custom built version from April 24, 2022, and a downloaded release version 82f5658. Unlikely to be affected by changes outside of NPC activity code.

Additional context

Theories:

  1. (disproven; NPC had plenty of inventory space) NPC without enough carry capacity dropping item after fetching it?
  2. (seems most likely) NPC pathfinding failing to proceed to the tool due to path obstructed by non-passable furniture?
  3. (seems unlikely but plausible) NPC pathfinding switching between 2 candidate tools?

Further on theory 2 from more testing: Observed an NPC on the Play Now start option stuck outside a closed window while attempting to get shovel for tilling task stored inside the shelter. A nearby, open window was available for pathing, but NPC waited until window on shortest path was opened before proceeding to the tool. In my main save, at least one window is obstructed by non-passable furniture, and tools are stored near several other impassable furniture items. Combined with hundreds more objects to search through, and a large farming area, it seems plausible that the NPC was attempting a Fetch action, interrupted by an obstructed path, and reset back to querying how to do the Farming task again. This suggests to me the most likely cause: NPC Fetch pathfinding resulting in a faulty path the NPC is unable to take, looping back around to a (heavy perf-wise) query of all farm tiles and items in an area on the next action. I think perf cost of this per action attempt would scale on the basis of approximately Number of Farm Tiles * Number of Nearby Items.

Primary fix suggestion: debug the Fetch pathfinding and why it differs from the path the NPC is able to take. This should reduce the number of long-duration Fetch queries firing off continually. Secondary long term perf suggestion: figure out why these actions take so long to process and if there's anything to be done about that.

SaintCirno9 commented 2 years ago

Well, I've never played so much time to see a 0.5GB size save file. 🤣🤣🤣

But in my test, it's not a problem of NPC pathfinding.

Instead, NPC cannot fetch the corresponding items cause the items are in darkness, hmmmm.

By the way, it's okay if the items are in outside with closing door and windows. GIF 2022-5-1 17-01-20

I may try to fix this problem later, need some time to look into the code.

AlwaysElise commented 2 years ago

I added that change to my local build, and it fixed the issue on that initial save file. Looks like the solution; thanks!

Zireael07 commented 2 years ago

Fetch being possible in the dark is a nice stopgap, but if querying/pathfinding to tools takes so much time, it should be improved somehow (only look for it every couple of ticks? mark zones as 'dirty' when tools are dropped and don't look for tools that aren't in it at all? just some ideas)

SaintCirno9 commented 2 years ago

Fetch being possible in the dark is a nice stopgap, but if querying/pathfinding to tools takes so much time, it should be improved somehow (only look for it every couple of ticks? mark zones as 'dirty' when tools are dropped and don't look for tools that aren't in it at all? just some ideas)

Literally it is just a bug where the fetch activity is dark capable in former check, but forget to add that to the latter.

Nothing related to pathfinding. The NPC could reach the right location to pick up the tools as you can see in my second gif posted above. They just failed to pass the darkness check so they will keep picking, thus cause the game "freeze".