This is a pathfinding algorithm called A*. I will not explain how it works here but there are some good resources for understanding it. Here are a couple: https://en.wikipedia.org/wiki/A*_search_algorithm https://www.redblobgames.com/pathfinding/a-star/introduction.html This project took me a few days of on and off work. Feel free to use it anywhere, but if you are able to reuse my code then I would encourage you to try to implement it yourself.
IF YOU FIND BUGS, PLEASE TELL ME. This was partially inspired by the Red Blob Games article. This runs much much faster on Turbowarp of course. It is interesting to play around with the settings, namely the density of the impassable tiles and the size of the grid (and I invite you to do so).