Windfisch / factorio-bot

Work-in-progress factorio bot
GNU General Public License v3.0
33 stars 4 forks source link

Asynchronous pre-planning of routes in the background #20

Open Windfisch opened 6 years ago

Windfisch commented 6 years ago

WalkTo actions (or anything involving route planning, actually) currently plan their routes on start(), introducing a long delay if the distance is large.

Instead, these actions should be able to plan ahead, changing things as follows:

  1. WalkTo actions store their route instead of generating it on-the-fly.
  2. Their route is initially pre-populated with a naive straight line. It is intended to replaced very soon by the first update_route(Pos start) call.
  3. They offer an update_route() method that's intended to be executed from a different thread.
  4. update_route() will atomically / mutex-locked update the previous route, so at any point in time, there's a route that's more-or-less-valid.
  5. If at the begin of update_route()'s execution it is discovered that the action is already running, then route calculation takes the current player's position as its starting point.
  6. If at the end of update_route()'s execution it is discovered that the action is already running, the player is "pulled onto the route" with another short pathfinding call.

The pre-planning thread will iterate over all WalkTo's in tasks and over the implicit WalkTo's in between the tasks, and pre-plan them up to a limited time/waylength in the future. Also, upon start() of a WalkTo action, it is replanned again, to account for last-minute changes.