コード例 #1
0
    def closest_inventory(self, start, *item_names, deposit=True):
        """
        """
        #any distance should be shorter possible on the board
        shortest_distance = con.BOARD_SIZE.width * con.BOARD_SIZE.height
        closest_block = None
        for building in self.buildings.values():
            if not building.has_inventory():
                continue
            inventory = building.blocks[0][0].inventory
            if (deposit == False and all(
                [inventory.check_item_get(name) for name in item_names])):
                pass
            elif (deposit == True and all(
                [inventory.check_item_deposit(name) for name in item_names])):
                pass
            else:
                continue

            distance = util.manhattan_distance(start.center,
                                               building.rect.center)
            if distance < shortest_distance:
                shortest_distance = distance
                closest_block = building
        return closest_block
コード例 #2
0
 def append(self, point: List[List[int]]):
     """Add a value to the path"""
     self.__coordinates.append(point)
     # distance will be zero for the first addition
     point1 = (sum(point[0]) / 2, sum(point[1]) / 2)
     point2 = (sum(self.__coordinates[-1][0]) / 2,
               sum(self.__coordinates[-1][1]) / 2)
     self.__length += util.manhattan_distance(point1, point2)
コード例 #3
0
ファイル: pipes.py プロジェクト: bramvanwersch/Machine_mining
 def check_storage_connections(self, node):
     storage_connections = []
     for edge in node.connected_edges:
         for c_node in edge.connected_nodes:
             if hasattr(c_node, "inventory") and c_node != node:
                 storage_connections.append([c_node, util.manhattan_distance(node.rect.center, c_node.rect.center), edge])
     storage_connections.sort(key=lambda x: x[1])
     return storage_connections
コード例 #4
0
ファイル: tasks.py プロジェクト: bramvanwersch/Machine_mining
 def __best_task_sort_tuple(self, task_dictionary, worker_pos):
     task_tuples = []
     for tasks in task_dictionary.values():
         task = tasks[0]
         distance = util.manhattan_distance(task.block.rect.topleft,
                                            worker_pos)
         task_tuples.append((task.selected, -1 * task.priority, distance))
     return sorted(task_tuples)
コード例 #5
0
 def path_lenght(self):
     """Give the length of the full path from the location of the path that is available"""
     return self.__length + util.manhattan_distance(self.__coordinates[-1],
                                                    self.start_location)
コード例 #6
0
    def __pathfind(self, start: "pygame.Rect",
                   end: "pygame.Rect") -> Union[None, "Node"]:
        """
        Find a path from a starting rectangle to an end rectangle by traversing the rectangle network using the A*
        pathfinding algorithm aproach

        Inspired and derived from:
        https://gist.github.com/Nicholas-Swift/003e1932ef2804bebef2710527008f44#file-astar-py
        """

        # Create start and end node
        start_node = Node(None, start, None)
        start_node.distance_from_start = start_node.total_for_both = 0
        end_node = Node(None, end, None)
        end_node.distance_from_start = end_node.distance_to_end = end_node.total_for_both = 0
        start_node.distance_to_end = util.manhattan_distance(
            start_node.position, end_node.position)
        if start == end:
            return end_node
        # Initialize both open and closed list
        open_list = []
        closed_list = []

        # Add the start node
        open_list.append(start_node)

        # Loop until you find the end
        while len(open_list) > 0:

            # Get the current node with lowest f
            current_node = open_list[0]
            current_index = 0
            for index, item in enumerate(open_list):
                if item.total_for_both < current_node.total_for_both:
                    current_node = item
                    current_index = index

            # Pop current off open list, add to closed list
            open_list.pop(current_index)
            closed_list.append(current_node)

            # Found the goal on block infront of destination
            connection_direction = util.side_by_side(current_node.rect,
                                                     end_node.rect)
            if connection_direction is not None:
                end_node.parent = current_node
                end_node.direction_index = connection_direction
                return end_node

            # Generate children
            children = []
            for direction_index, direction in enumerate(
                    current_node.rect.connecting_rects):
                for rect in direction:
                    # Create new node
                    new_node = Node(current_node, rect, direction_index)

                    # Append
                    children.append(new_node)

            # Loop through children
            for child in children:

                # Child is on the closed list
                if len([
                        closed_child for closed_child in closed_list
                        if closed_child.rect == child.rect
                ]) > 0:
                    continue

                child.distance_from_start = current_node.distance_from_start +\
                                util.manhattan_distance(child.position, current_node.position) # noqa --> this shit is stupid
                child.distance_to_end = util.manhattan_distance(
                    child.position, end_node.position)
                child.total_for_both = child.distance_from_start + child.distance_to_end

                # Child is already in the open list
                if len([
                        open_node for open_node in open_list
                        if child.rect == open_node.rect
                ]) > 0:
                    continue

                # Add the child to the open list
                open_list.append(child)
        return None