Vehicles stuck in endless loop when they cannot reach destination

Vehicles & Roads
mzhaase [github] · 3 years ago

Issue description

When a vehicle fails to reach a destination, it will sit idle for a couple of seconds, then try to reach the same destination again. This can lead to an infinite loop.

Example:

  1. Dump truck full
  2. Dump truck gets destination with highest priority
  3. Pathfinding cannot find a path, because conveyors are in the way
  4. Dump truck waits x seconds and tries again
  5. If the priorities are unchanged, goto 3

Expected behavior

When a destination is unreachable for a vehicle, that vehicle should put that destination on a per-vehicle blacklist for a certain time. When the vehicle gets a new job, it should evict items on the blacklist that are past ttl and otherwise only consider destinations that are not on the blacklist.

Example code:

ORDERED_DESTINATIONS = {
    "ore": ["id1", "id2"]
}
class Vehicle:
    def __init__(self):
        # might be faster to use two arrays here instead of a hashmap
        self.blacklist_destinations = []
        self.blacklist_timestamps = []
        self.load = "ore"
        self.ttl = 60

    def pathfind_to_destination(self, destination):
        path = a_star(destination)
        if not path:
            self.blacklist_destinations.append(destination)
            self.blacklist_timestamps.append(time.time())
            return None
        return path
    
    def get_job(self):
        if self.blacklist_destinations:
            for i in range(len(self.blacklist_destinations)):
                if time.time() - self.blacklist_timestamps[i] > self.ttl:
                    self.blacklist_destinations.pop(i)
                    self.blacklist_timestamps.pop(i)
                    break
        for destination in ORDERED_DESTINATIONS[self.load]:
            if destination not in self.blacklist_destinations:
                path = self.pathfind_to_destination(destination)
                if not path: continue
                self.do_job(path)
                return
        return None

Reproduction steps

  1. Have dump truck full of product
  2. Have high-priority destination for product behind raised conveyors
  3. Have low-priority destination somewhere else

Game version

v0.4.14B

21 Showing 1–2 of 2