def strafe_to_block(x, y): if map_data.is_valid_loc(x, y): x_diff = x - map_data.getX() y_diff = y - map_data.getY() #x movement distance = 0 dir = map_data.RIGHT if x_diff > 0: distance = blocklength * x_diff dir = map_data.RIGHT elif x_diff < 0: distance = blocklength * x_diff * -1 dir = map_data.LEFT move(distance, dir) map_data.setX(x) time.sleep(.4) #y movement distance = 0 if y_diff < 0: dir = map_data.UP distance = blocklength * y_diff * -1 elif y_diff > 0: dir = map_data.DOWN distance = blocklength * y_diff move(distance, dir) map_data.setY(y)
def move_to_block(x, y): if map_data.is_valid_loc(x, y): x_diff = x - map_data.getX() y_diff = y - map_data.getY() #x movement turn_angle = 0 distance = 0 if x_diff > 0: turn_angle = -1 * current_direction distance = blocklength * x_diff elif x_diff < 0: turn_angle = -1 * (current_direction - 180) distance = blocklength * x_diff * -1 if (turn_angle == 270) or (turn_angle == -270): turn_angle = turn_angle * (-1 / 3) turn(turn_angle) move(distance, 1) map_data.setX(x) #y movement turn_angle = 0 distance = 0 if y_diff > 0: turn_angle = -1 * (current_direction - 90) distance = blocklength * y_diff elif y_diff < 0: turn_angle = -1 * (current_direction - 270) distance = blocklength * y_diff * -1 if (turn_angle == 270) or (turn_angle == -270): turn_angle = turn_angle * (-1 / 3) turn(turn_angle) move(distance, 1) map_data.setY(y)
def checkAndMapObstacles(direction): # check for obstacles above map_data.set_obstacle_at(pos_x - 1, pos_y, IR_north.check()) # do that for all other directions if not direction: return x, y = map_data.coordsFor(map_data.getX(), map_data.getY(), direction) return map_data.has_obstacle_for_loc(x, y)
def avoidObstacle(dir): obstacleBlock = [] obstacleBlock = coordsFor(map_data.getX(), map_data.getY(), dir) rightOfMoveDir = [] rightOfMoveDir = coordsFor(map_data.getX(), map_data.getY(), dir - 1) #default go right (relative to dir) since we are usually going clockwise #check the block to the right is_valid = False #this is true when the desired block is safe to move to if map_data.has_been_explored(rightOfMoveDir[0], rightOfMoveDir[1]): is_valid = not map_data.has_obstacle_for_loc(rightOfMoveDir[0], rightOfMoveDir[1]) else: map_data.set_obstacle_at(rightOfMoveDir[0], rightOfMoveDir[1], IRSTUFF) if is_valid_loc: movement_wrapper.strafe_one_block(dir - 1) else: tryleft
def find_live_tunnel_perimeter(): # we start at the bottom left, start exploring north moveDirection = map_data.UP exploring = True checkAndMapObstacles() while exploring: if checkAndMapObstacles(moveDirection): # try going around the obstacle avoiding = True destX, destY = map_data.coordsFor(map_data.getX(), map_data.getY(), moveDirection) lastDirection = moveDirection circleAround = 1 while avoiding: for naiveDest in range(lastDirection + 1, lastDirection + 3): # will loop through all directions naiveDest = naiveDest + 4 + (circleAround * 4) direction = naiveDest % 4 if not map_data.has_obstacle_for_loc(direction): lastDirection = direction circleAround *= -1 break # out of the different directions movement_wrapper.strafe_one_block(lastDirection % 4) if map_data.getX() is destX and map_data.getY() is Y: avoiding = False break # out of this exploration round movement_wrapper.strafe_one_block(moveDirection) pos_x = map_data.getX() pos_y = map_data.getY() if is_infrastructure_below(): wireEnds.append([pos_x, pos_y]) analyzeCache() # it's beneath us! exploring = False # we are done checkAndMapObstacles() # [TODO]: don't move into an obstacle if pos_x == 0 and pos_y == 0: # top left moveDirection = map_data.RIGHT elif pos_x == 6 and pos_y == 0: # top right moveDirection = map_data.DOWN elif pos_x == 6 and pos_y == 6: # bottom right moveDirection = map_data.LEFT elif pos_x == 0 and pos_y == 6: # back at bottom left exploring = False return moveDirection
def mapOut(lastDirection): #lastDirection = map_data.UP # let's just start moving upwards if map_data.getY() is 0: # uh, if we're at the top, let's not lastDirection = map_data.DOWN exploring = True while exploring: if not wireBelowBlockIn( lastDirection): # try the preferred direction first for direction in range(0, 3): # will loop through all directions if direction is not lastDirection: # we don't want to waste time if wireBelowBlockIn(direction): lastDirection = direction # the wire probably moves towards this direction break # skip trying other directions else: # no wire :( # flip the direction if direction > 1: direction -= 2 else: direction += 2 # move back to the previous block movement_wrapper.strafe_one_block(direction) if map_data.getX() is 0 or map_data.getY() is 0: # we're at an edge, so we're back on the other side exploring = False
def strafe_one_block(direction): x = map_data.getX() y = map_data.getY() if direction == map_data.RIGHT: x = x + 1 elif direction == map_data.UP: y = y - 1 elif direction == map_data.LEFT: x = x - 1 elif direction == map_data.DOWN: y = y + 1 if map_data.is_valid_loc(x, y): map_data.setX(x) map_data.setY(y) move(blocklength, direction) print "moved to (%s, %s)" % (map_data.loc[0], map_data.loc[1]) else: #error in movement algorithm print "invalid location (%s, %s)" % (x, y)
def wireBelowBlockIn(direction): x, y = map_data.coordsFor(map_data.getX(), map_data.getY(), direction) if map_data.has_not_been_explored(x, y): movement_wrapper.strafe_one_block(direction) return is_infrastructure_below()