def solveWithBlobs(st, blobs): bot = st.bots[0] collectBoosters(st, bot) def findBlob(pos): for blob in blobs: if pos in blob: return blob return None curPos = st.botPos() while True: blob = findBlob(curPos) if blob is None: break blobRanks = {} def add(l, x, y): blobRanks[(x, y)] = l pathfinder.bfsFind(st, curPos, lambda l, x, y: False, register=add) while True: nextPos = closestRotInBlob(st, blob, {}) if nextPos is None: break curPos = closestRotInBlob(st) if curPos is None: break return st.actions()
def closestRotSolver(st, bot_num=0): bot = st.bots[bot_num] collectBoosters(st, bot_num) while True: path = pathfinder.bfsFind(st, bot.pos, wrapP(st), availP=drillableP(st)) if st.boosters[Booster.DRILL] > 0 and bot.drill_duration <= 0: path2 = pathfinder.bfsFind(st, bot.pos, wrapP(st), availP=withDrillP(st)) if path2 is not None and len(path2) < len(path): # print("Attach DRILL at " + str(len(bot.actions))) st.nextAction(AttachDrill()) path = path2 if path is None: break pathToCommands(path, st) if st.boosters[Booster.WHEEL] > 0 and bot.wheel_duration <= 0: if random.random() > WHEELS_PROC: # print("Attach WHEELS at " + str(len(bot.actions))) st.nextAction(AttachWheels()) if st.boosters[Booster.DRILL] > 0 and bot.drill_duration <= 0: if random.random() > DRILL_PROC: # print("Attach DRILL at " + str(len(bot.actions))) st.nextAction(AttachDrill()) return st
def parallelRotSolver(st, bot_num): global actions bot = st.bots[bot_num] path = pathfinder.bfsFind(st, bot.pos, parallelP(st, aimed), availP=drillableP(st, bot)) # if AttachDrill().validate(st, bot): # path2 = pathfinder.bfsFind(st, bot.pos, # wrapP(st), # availP=withDrillP(st)) # if path2 is not None and len(path2) < len(path): # # print("Attach DRILL at " + str(len(bot.actions))) # actions[bot_num].append(AttachDrill()) # path = path2 if path is None: return False pathToCommands(path, st, bot_num) if st.boosters[Booster.WHEEL] > 0 and bot.wheel_duration <= 0: if random.random() > WHEELS_PROC: # print("Attach WHEELS at " + str(len(bot.actions))) actions[bot_num].append(AttachWheels()) if st.boosters[Booster.DRILL] > 0 and bot.drill_duration <= 0: if random.random() > DRILL_PROC: # print("Attach DRILL at " + str(len(bot.actions))) actions[bot_num].append(AttachDrill()) return True
def useClone(st, bot_num): bot = st.bots[bot_num] path = pathfinder.bfsFind(st, bot.pos, spawnP(st), availP=drillableP(st, bot)) if path is None: print('Cant use SPAWN') return print('Go to SPAWN') pathToCommands(path, st, bot_num) global actions actions[bot_num].append(CloneAction())
def collectBoosters(st, bot_num, pred=boosterP): global boostersAvailable if not boostersAvailable: return False bot = st.bots[bot_num] path = pathfinder.bfsFind(st, bot.pos, pred(st), availP=drillableP(st, bot)) if path is None: boostersAvailable = False return False pathToCommands(path, st, bot_num) return True
def optimizeBlob(): for i in range(len(blobs)): if (len(blobs[i]) < 10): pos = next(iter(blobs[i])) otherPath = pathfinder.bfsFind( st, pos, lambda l, x, y: st.cell(x, y)[1] == Cell.ROT and (x, y) not in blobs[i]) if otherPath is None: return False otherPos = otherPath[len(otherPath) - 1] otherBlob = findBlob(otherPos) blobs[i] = otherBlob.union(blobs[i]) blobs.remove(otherBlob) return True return False
def collectBoosters(st, bot_num): bot = st.bots[bot_num] while True: path = pathfinder.bfsFind(st, bot.pos, boosterP(st), availP=drillableP(st, bot)) if path is None: break pathToCommands(path, st, bot_num) if st.boosters[Booster.MANIPULATOR] > 0: command = AttachManipulator(ATTACHER.get_position(bot)) # TODO (all boosters) else: continue st.nextAction(command)
def findPath(st, from_pos, to_pos): return pathfinder.bfsFind(st, from_pos, lambda l, x, y: (x, y) == to_pos)
def test1(self): state = State(self.contour, (0, 0), [self.obst1], []) path = bfsFind(state, (1, 1), lambda l, x, y: x == 2 and y == 4) self.assertEqual([(1, 1), (1, 2), (1, 3), (1, 4), (2, 4)], path)