C7-Game / Prototype

An early-stage, open-source 4X strategy game
https://c7-game.github.io/
MIT License
34 stars 9 forks source link

191 refactor pathing algorithm #332

Closed Kright closed 2 years ago

Kright commented 2 years ago

Related to #191

Fractional movement points will be added in another pull request, because this is huge enough.

I rewrote dijkstra pathing algoritm for more abstract way. I decoupled it from game logic for movement possibility and costs.

DijkstrasAlgorithm.run(...) - pure algorithm, don't depend on game staff. EdgeWalker interface passed to Dijkstra's algorithm and called back do determine movement costs between tiles. WalkerOnLand - example of EdgeWalker imlementation for searching path on land.

BinaryMinHeap - custom data structure, needed for performance of Dijkstra's algorithm. "net472" doesn't have PriorityQueue, so I wrote it myself. Surprisingly the pathing algoritm turned out to be very simple.

I hope that existing BFSLandAlgorithm and DijkstrasLandAlgorithm could be replaced with new implementation and custom EdgeWalkers.

P.S. Algorithm already uses roads info when searching path, but units still untouched and they walk slow as without road.