def __init__(self, grid, buildconfig): self.grid = grid self.buildconfig = buildconfig self.label = '0' # init this for reuse later self.dir_pairs = [] for d in ('e', 's', 'w', 'n'): direction = Direction(d) self.dir_pairs.append([direction, direction.right_turn()]) self.dir_pairs.append([direction, direction.left_turn()])
def parse_startpos(start, width, height): """Transform startpos string like (1,1) or nw to corresponding Point.""" # try (#,#) type syntax m = re.match(r'\(?(\d+)[,;](\d+)\)?', start) if m is not None: return (int(m.group(1)), int(m.group(2))) # try corner-coordinate syntax m = re.match(r'(ne|nw|se|sw)', start.lower()) if m is not None: # convert corner String -> Direction -> (x, y) then magnify (x, y) # by width and height to get the corner coordinate we want (x, y) = Direction(m.group(1)).delta() point = (max(0, x) * (width - 1), max(0, y) * (height - 1)) return point raise ParametersError("Invalid --position parameter '%s'" % start)
def get_nearest_plottable_area_from(grid, start): """ Find the nearest plottable area corner from start. Returns coordinate of nearest plottable area corner. """ # if start is out of bounds for grid, expand dimensions of grid if grid.is_out_of_bounds(*start): grid.expand_dimensions(start[0] + 1, start[1] + 1) # check the cell we started in: if it is plottable, it becomes our # starting cheapest_area cell = grid.get_cell(*start) if cell.plottable and cell.area: return start # start with the innermost ring of cells adjacent to start, then # expand outward ring by ring for ring in xrange(1, 1 + max([grid.width, grid.height])): # starting position in this ring (=NW corner cell of ring) pos = add_points(start, (-ring, -ring)) for direction in (Direction(d) for d in ['e', 's', 'w', 'n']): for _ in xrange(0, 2 * ring): pos = add_points(pos, direction.delta()) # step once in direction if grid.is_out_of_bounds(*pos): continue # outside grid bounds corner = grid.get_cell(*pos) if corner.plottable and corner.area: # cell has an area that can be plotted, return it return pos # found no position with an area we can plot return None
def trace_outline(self): """ Moves the cursor to the northwest corner, then clockwise to each other corner, before returning to the starting position. """ buildconfig = BuildConfig('dig') grid = self.layers[0].grid plotter = AreaPlotter(grid, buildconfig) plotter.expand_fixed_size_areas() # plot cells of d(5x5) format ks = Keystroker(grid, buildconfig) keys = [] # move to each corner beginning with NW, going clockwise, and wait # at each one lastpos = self.start for cornerdir in [ Direction(d) for d in ['nw', 'ne', 'se', 'sw', 'nw'] ]: (x, y) = cornerdir.delta() newpos = (max(0, x) * (grid.width - 1), max(0, y) * (grid.height - 1)) keys += ks.move(lastpos, newpos, allowjumps=False) + ['%'] lastpos = newpos keys += ks.move(lastpos, self.start, allowjumps=False) # trim any pauses off the ends while keys and keys[0] == '%': keys = keys[1:] while keys and keys[-1] == '%': keys = keys[:-1] # if the result is no keys, return a single wait key keys += ['%'] return keys
def move_randomly(self, game): self.facing = Direction(random.randint(1, 4)) new_pos = game._move_in_direction(self.pos, self.facing) if self.can_move_to(game.world, new_pos): self.pos = new_pos
''' Created on Jun 5, 2020 @author: Gosha ''' from geometry import Point, Camera, Direction, Triangle from objects import DPoint, DCamera, DSurface from screen import Screen if(__name__ == '__main__'): cam = Camera(Point((0, 0, 0)), Direction((0, 0, 0))) dCam = DCamera(cam) screen = Screen() screen.linkCamera(dCam) sides = [] sides.append(DSurface([Triangle(Point((-10, 10, 10)), Point((10, 10, 10)), Point((10, 10, -10))), Triangle(Point((-10, 10, 10)), Point((-10, 10, -10)), Point((10, 10, -10)))], (1, 1, 0), (0, 0, 0))) sides.append(DSurface([Triangle(Point((-10, -10, 10)), Point((10, -10, 10)), Point((10, -10, -10))), Triangle(Point((-10, -10, 10)), Point((-10, -10, -10)), Point((10, -10, -10)))], (0.5, 0.5, 0.5), (0, 0, 0))) sides.append(DSurface([Triangle(Point((10, -10, 10)), Point((10, 10, 10)), Point((10, 10, -10))), Triangle(Point((10, -10, 10)), Point((10, -10, -10)), Point((10, 10, -10)))], (0, 1, 0), (0, 0, 0))) for side in sides: dCam.show(side) for x in [-10, 10]: for y in [-10, 10]: for z in [-10, 10]: dCam.show(DPoint(Point((x, y, z))))
def move_randomly(self): self.facing = Direction(random.randint(1, 4)) new_pos = self.game._move_in_direction(self.pos, self.facing) if self.can_move_to(new_pos): self.pos = new_pos