jpw1991 / chebs-necromancy

Cheb's Necromancy adds Necromancy to Valheim via craftable wands and structures. Minions will follow you, guard your base, and perform menial tasks.
The Unlicense
10 stars 4 forks source link

Efficiency improvement for worker minions #130

Closed jpw1991 closed 1 year ago

jpw1991 commented 1 year ago

Description

Worker minions pretty regularly scan for stuff to cut/mine. But really they needn't scan very often at all: Trees and rocks don't move.

Is the feature related to a problem?

Yes. Having a lot of miners/lumberjacks can cause framerate drops.

Solutions

Large Scan + Small Scan

I think there should be an infrequent large scan accompanied by more regular smaller scans.

Large scan:

  1. A large scan is performed that forms a list of trees/rocks in the whole sight radius
  2. Until that list is empty, the worker will not perform another large scan again

Small scan

Small scans are more frequently done and used to find stuff like logs or rock fragments that have formed as the result of harvesting, but these are always nearby and so the scan can be restricted to just a few meters and only performed after the current target is harvested (eg. tree breaks into logs, perform another small scan, target logs).

Workfllow

workerAI (1)

CW-Jesse commented 1 year ago

Well, the large list is always empty when it begins, and only ever filled once.

CW-Jesse commented 1 year ago
  1. Let's see if using the new FindClosest for other parts of the AI, such as LookForCuttableObjects, speeds it up?
  2. What about starting with small radii and increasing it up to the maximum search distance if nothing is found?
jpw1991 commented 1 year ago

Well, the large list is always empty when it begins, and only ever filled once.

Whoopsie. Yeah, I had imagined a line there. I promise that is unintentional...

CW-Jesse commented 1 year ago

I'm gonna profile the performance of the neckros and woodcutters and report back.

CW-Jesse commented 1 year ago

Okay, Neckros are 31% more efficient than the woodcutters due to the new UndeadMinion.FindClosest, which I find surprising because I don't think it's optimized. Second surprise: neither is inefficient in the first place. Low FPS can't be from their searching AI because they don't search every frame.

CW-Jesse commented 1 year ago

Oh, wait, the neckro might be faster just because of the mask used for OverlapSphere.

CW-Jesse commented 1 year ago

The only way to make it faster is to start with smaller radii and increase the size as you said. Also, it might help to try OverlapSphereNonAlloc:

https://docs.unity3d.com/ScriptReference/Physics.OverlapSphereNonAlloc.html

BUT this might be a case of premature optimization; is there ANY Humanoid you can have a hundred of that wouldn't slow the game down? So much time each frame is spent updating the walking animation on the main thread.

CW-Jesse commented 1 year ago

If I made a mod that put the animation updates on another thread, you could probably have hundreds of creatures while still being playable.

CW-Jesse commented 1 year ago

image

CW-Jesse commented 1 year ago

Oh, there's a different problem with the woodcutters that I didn't find until I did the profiling again: The woodcutters were checking for something to cut every frame: nextCheck is never set to the current time in Awake.

So if you've been running the game for a few hours, woodcutters are going to LookForCuttableObjects every frame for Time.time/UpdateDelay frames. After 3 hours of playing, that's 1800 frames, or 1.5 minutes at 20 FPS.

CW-Jesse commented 1 year ago

I don't know if it should close this issue, but this will definitely help.

CW-Jesse commented 1 year ago

Oh, it might. Because 1.5 minutes at 20 FPS is another 1800 frames, so it's not catching up. So it could be a permanent performance loss until... all the games are restarted? Maybe even the server? Yeah. This was a bigger issue than I thought.

CW-Jesse commented 1 year ago

With this fixed and the default time of 6 seconds between searches, the profiler looks like this (with 50 woodcutters and 50 neckros each with search radii of 1000): image That's not bad, considering that is way too high of a search radius.

jpw1991 commented 1 year ago

Great finds. Thank you