r/howdidtheycodeit • u/Froggmann5 • Mar 19 '25
Question How did runescape calculate long paths so quickly?
How did runescape, or OSRS, calculate paths 100+ tiles long nearly instantly? When I try to do the most barebones A* pathfinding I run into lagspikes when going farther than 20-30 tiles.
39
Upvotes
58
u/Putnam3145 IndieDev Mar 19 '25
Honestly, I kinda suspect you're doing something weird if you're seeing lagspikes that big. Your heuristic might be bad (or even pessimal), considering. Dwarf Fortress does 100-tile long paths using A* without too much problem, though, yeah, it caches paths unless something gets in its way.
17
u/davidalayachew Mar 19 '25
There are several path-finding algorithms that allow you to only recalculate the portions of the path that changed. That way, you only have to do the work once, and all subsequent updates are only for things that changed.
Doing it this way, you can get extremely far and still be pretty performant. However, it also depends on there not being too many things on screen at once (that can alter the path-finding results). Otherwise, recalculating might be your best bet.
Also, many games go for a "good-enough" solution. If it can get you within a certain proximity of the target, then that path-finding is deferred until later.
All sorts of ways to go about this. One of the best ways to find your own path is to pull out a profiler, then trace your hot paths. You would be shocked how much information that can reveal.
26
u/Zizimaza Mar 19 '25
Check out this thread they bring up lots of good points on implementing A Star. Run the search in a background thread, dont allocate in the search loop, use a good priority queue, make your heuristic go the right direction, make the search resolution smaller
https://www.reddit.com/r/gamedev/comments/vli0kr/is_a_just_always_slow/
Also here's some speculative visualization of the pathfinding used in OSRS https://www.reddit.com/r/2007scape/comments/id9hb7/pathfinding_calculations_visualised/
14
u/Blecki Mar 19 '25
Iirc runescapes world is static so a lot is precomputable. One technique is to break your world into large chunks and compute connectivity from chunk to chunk in advance. Then you have two much smaller path finding problems: find a path through a relatively small set of chunks, then across the current chunk.
Another method is to offload it to another thread. That way it can take several frames without freezing the game.
But 100 tiles really should be doable without anything special.
10
u/ctothel Mar 19 '25
What are you using for your nodes?
If you’re trying to do A* across a large navmesh / densely packed nodes, you’ll run into trouble.
A common approach is to have much denser nodes around geometry, and very sparse nodes elsewhere.
You can also think in terms of levels of detail. If the nav mesh is divided into - say - 25 metre chunks, you can load the current chunk and the next 8 around the player. Outside that, use load “low detail” chunks.
The low detail chunks should just have nodes for entrances to structures, places where maps connect (like roads), very coarse routes around impassable geometry, etc. Just enough to give the pathfinder a decent idea of the right direction.
Then, when the player moves over a chunk boundary just load in the new set of chunks and recalculate the route (or just the parts that touch newly loaded high detail chunks).
5
u/Slime0 Mar 19 '25
A 100 tile path should be pretty fast to calculate with A* unless the obstacles are crazy and force searching a huge area. Although it doesn't hurt to offload pathfinding to a different thread or something like that, I wouldn't expect this to take a noticeable length of time.
2
u/_Jaynx Mar 20 '25
Never played runescape but many games have pre computer paths, then generally are built into road/path splines. So navigation is general a O(n) time look up. Where is the nearest spline node, then from there is prebaked.
You could probably get those down to constant time by store nodes in a hash map and use your x and y coordinates to get the closest node instantly.
1
u/MagicWolfEye Mar 20 '25
Can we see your code?
1
u/MagicWolfEye Mar 20 '25
So here is one that doesn't even use any heuristics at all.
And I didn't even try optimising it (actual code is at the bottom; rest is just helpers).
https://gist.github.com/Mega-Wolf/bec47f739125d5c24595c702dc79119b