From 55f7062c8402010b3b1feb03f9815cb0a2277a11 Mon Sep 17 00:00:00 2001 From: Tom Kerr Date: Fri, 3 Nov 2023 16:44:33 +0000 Subject: [PATCH 1/2] feat: more extensive find alternative building placement --- src/ares/build_runner/build_order_runner.py | 4 +- src/ares/managers/placement_manager.py | 42 ++++++++++++--------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/ares/build_runner/build_order_runner.py b/src/ares/build_runner/build_order_runner.py index afe5d99..e162955 100644 --- a/src/ares/build_runner/build_order_runner.py +++ b/src/ares/build_runner/build_order_runner.py @@ -121,10 +121,12 @@ async def run_build(self) -> None: if len(self.build_order) > 0: await self.do_step(self.build_order[self.build_step]) - if not self.build_completed and self.build_step >= len(self.build_order): + if self.build_completed or len(self.ai.townhalls) > 1 or self.ai.time > 120.0: self.mediator.switch_roles( from_role=UnitRole.PERSISTENT_BUILDER, to_role=UnitRole.GATHERING ) + + if not self.build_completed and self.build_step >= len(self.build_order): self._opening_build_completed = True return diff --git a/src/ares/managers/placement_manager.py b/src/ares/managers/placement_manager.py index 1dc130f..15e21aa 100644 --- a/src/ares/managers/placement_manager.py +++ b/src/ares/managers/placement_manager.py @@ -301,29 +301,35 @@ def request_building_placement( ) return base_locations.remove(location) - location = min( + locations = sorted( base_locations, key=lambda k: cy_distance_to(k, base_location), ) - logger.warning( - f"No available {building_size} found near location: " - f"{base_location}, trying near {location}" - ) - potential_placements: dict[Point2:dict] = self.placements_dict[ - location - ][building_size] - available: list[Point2] = [ - placement - for placement in potential_placements - if potential_placements[placement]["available"] - and not potential_placements[placement]["worker_on_route"] - and self.can_place_structure(placement, structure_type) - ] - if len(available) == 0: + for location in locations: logger.warning( - f"No available {building_size} found near location: {location}" + f"No available {building_size} found near location: " + f"{base_location}, trying near {location}" ) - return + potential_placements: dict[Point2:dict] = self.placements_dict[ + location + ][building_size] + available: list[Point2] = [ + placement + for placement in potential_placements + if potential_placements[placement]["available"] + and not potential_placements[placement]["worker_on_route"] + and self.can_place_structure(placement, structure_type) + ] + if len(available) == 0: + logger.warning( + f"No {building_size} found near location: {location}" + ) + # FOUND SOMETHING! Break out and continue + else: + break + + if len(available) == 0: + logger.warning(f"No available {building_size} found, giving up") # get closest available by default final_placement: Point2 = min( From 17bfef12a7edd1f23cd80171427f16a58c60d8e3 Mon Sep 17 00:00:00 2001 From: Tom Kerr Date: Fri, 3 Nov 2023 16:45:02 +0000 Subject: [PATCH 2/2] fix: ensure requested placements are removed properly --- src/ares/managers/placement_manager.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ares/managers/placement_manager.py b/src/ares/managers/placement_manager.py index 15e21aa..fb4cc47 100644 --- a/src/ares/managers/placement_manager.py +++ b/src/ares/managers/placement_manager.py @@ -915,7 +915,9 @@ def _track_requested_placements(self) -> None: > base_placements[two_by_two][building_location]["time_requested"] + self.WORKER_ON_ROUTE_TIMEOUT ): - base_placements[two_by_two][building_location]["available"] = True + self._make_placement_available( + two_by_two, base_location, building_location + ) loc_to_remove.append(building_location) elif building_location in base_placements[three_by_three]: @@ -926,9 +928,9 @@ def _track_requested_placements(self) -> None: ] + self.WORKER_ON_ROUTE_TIMEOUT ): - base_placements[three_by_three][building_location][ - "available" - ] = True + self._make_placement_available( + three_by_three, base_location, building_location + ) loc_to_remove.append(building_location) for loc in loc_to_remove: