def test_noPath(self): grid = SearchGrid(3, 1) grid.set(1, 0, INPENETRABLE) pathFinder = PathFinder(grid) foundPath = pathFinder.find((0, 0), (2, 0)) self.assertEqual(None, foundPath)
def test_directPathBlockedByImpenetrable(self): grid = SearchGrid(5, 5) grid.set(1, 0, INPENETRABLE) pathFinder = PathFinder(grid) foundPath = pathFinder.find((0, 0), (2, 0)) self.assertEqual([(0, 0), (0, 1), (1, 1), (2, 1), (2, 0)], foundPath)
def test_shortestPathGoesOverExtraCostCell(self): grid = SearchGrid(5, 5) grid.set(1, 0, EXTRACOST) grid.set(1, 1, INPENETRABLE) pathFinder = PathFinder(grid) foundPath = pathFinder.find((0, 0), (2, 0)) self.assertEqual([(0, 0), (1, 0), (2, 0)], foundPath)
def run(self) -> None: location = self.entity.get_map_location() direction_to_deposit = location.direction_to(self._deposit.location) if location == self._deposit.location or location.is_adjacent_to(self._deposit.location): self._deposit.observed_karbonite = GC.get().karbonite_at(self._deposit.location) if self._deposit.observed_karbonite > 0: self._start_harvesting(direction_to_deposit) return if self._deposit.observed_karbonite <= 0: self._update_deposit() if self._deposit is None: return if self._deposit.observed_karbonite <= 0: from states.units.robots.worker.idle import WorkerIdleState self.entity.get_fsm().change_state(WorkerIdleState) if self._path == [] or not GC.get().can_move(self.unit.id, self._path[0]): self._path = PathFinder.get_shortest_path(location, self._deposit.location, False) if self.unit.get_unit().movement_heat() < 10 and self._path != []: next_dir = self._path.pop(0) GC.get().move_robot(self.unit.id, next_dir)
def _choose_best_builder(self, build_structure_type: bc.UnitType) -> (Worker, bc.Direction): # choose worker that is the furthest from the deposit for building builder = None build_direction = None max_dist = 0 for worker in self.team.workers: worker_loc = worker.get_map_location() nearest_dep = GC.get_nearest_karbonite_deposit(worker_loc) path = PathFinder.get_shortest_path(worker_loc, nearest_dep.location, False) for direction in bc.Direction: if (path is None or len(path) > max_dist) and GC.get().can_blueprint(worker.id, build_structure_type, direction): builder = worker build_direction = direction max_dist = len(path) return builder, build_direction
def test_freePathMultipleSteps(self): pathFinder = PathFinder(SearchGrid(5, 5)) foundPath = pathFinder.find((0, 0), (3, 0)) self.assertEqual([(0, 0), (1, 0), (2, 0), (3, 0)], foundPath)
def test_startLocationIsSameAsEndLocation(self): pathFinder = PathFinder(SearchGrid(5, 5)) foundPath = pathFinder.find((0, 0), (0, 0)) self.assertEqual([(0, 0)], foundPath)
def processTask(task): path = PathFinder(task.searchGrid).find(task.startLocation, task.endLocation) task.setCompleted(path)
def _recompute_route(self, ignore_robots): self._route = PathFinder.get_shortest_path( self.entity.get_map_location(), self._moving_to_location, ignore_robots)
def createPath(self, startLocation, endLocation): searchGrid = SearchGridGenerator.generateSearchGridFromPlayfield() pathFinder = PathFinder(searchGrid) path = pathFinder.find(startLocation, endLocation) return PlannedPath(path)
import traceback import sys import battlecode as bc import random from ai import AI from game.game_controller import GC from pathfinding.pathfinder import PathFinder random.seed(6137) GC(bc.GameController()) PathFinder() ai = AI() while True: try: ai.play_round() except Exception as e: print(traceback.format_exc()) GC.get().next_turn() # these lines are not strictly necessary, but it helps make the logs make more sense # it forces everything we've written this turn to be written to the manager sys.stdout.flush() sys.stderr.flush()