예제 #1
0
def generate_court(size=50,
                   col_rows=10,
                   col_cols=17,
                   start_posx=0,
                   start_posy=0):
    hexes = Group()
    id, x = 0, 0
    posy = start_posy
    posx = start_posx
    for j in range(col_cols):
        posx += size // 1.45
        y = 0
        for i in range(col_rows):
            posy += size // 1.24
            hexes.add(
                Hexagon(posx=posx,
                        posy=posy,
                        id_and_pos=(id, x, y),
                        width=size,
                        height=size))
            x += 2
            y += 1
        id += 1
        if posy == size // 1.24 * col_rows + start_posy:
            posy = size // 2.4 + start_posy
            x = 1
        elif posy == size // 1.24 * col_rows + size // 2.4 + start_posy:
            posy = start_posy
            x = 0
    return hexes
예제 #2
0
 def make_hexagon(self, x, y, radius, phase=math.pi / 2):
     """Create an hexagon given its position in pixels and its radius."""
     points = []
     for a in range(6):
         points.append(
             (
                 int(x + radius * math.cos(a * 60 * math.pi / 180 + phase)),
                 int(y + radius * math.sin(a * 60 * math.pi / 180 + phase)),
             )
         )
     return Hexagon(points)
예제 #3
0
 def gen_grid(self):
     grid = []
     for x in range(-self.rad, self.rad + 1):
         for y in range(-self.rad, self.rad + 1):
             for z in range(-self.rad, self.rad + 1):
                 if (x + y + z == 0):
                     hexagon = Hexagon(self.myCanvas,
                                       x,
                                       y,
                                       z,
                                       size=self.size,
                                       rot=self.rot)
                     grid.append(hexagon)
     print("foo")
     return (grid)
예제 #4
0
                    _openings = list(
                        map(lambda x: str(int(x) + 1),
                            row['Openings'].strip().split(',')))
                except Exception as e:
                    import ipdb
                    ipdb.set_trace(context=5)
                    print(e)
            else:
                _openings = row['Openings'].strip().split(',')

            _new_hex = Hexagon(
                id=_hexagon_id,
                spreadsheet_row=row_id + 2,
                center=row['Center'].strip(),
                openings=_openings,
                link1=row['Link1'].strip(),
                link2=row['Link2'].strip(),
                link3=row['Link3'].strip(),
                link4=row['Link4'].strip(),
                link5=row['Link5'].strip(),
                link6=row['Link6'].strip(),
            )
            if _dup_hash.get(_new_hex.get_hash(), False):
                continue
            _dup_hash[_new_hex.get_hash()] = True

            _hexagons.append(_new_hex)
            for _missing_link_num in _hexagons[-1].get_available_links():
                _link_name = convert_num_to_link_name(_missing_link_num)
                _hexagon_missing_links[_link_name].append(_hexagon_id)

            _hexagon_id += 1
예제 #5
0
 def setUp(self):
     self.hexagon = Hexagon(10, 11, 12, 13, 14, 15)
예제 #6
0
    def __init__(self,
                 robot_id,
                 hex_internal_list,
                 hex_external_list,
                 ball,
                 hist_curve,
                 np_file='/tmp/magnetic_ground_truth.np',
                 height=40,
                 battery=99999,
                 debug=True):
        self.robot_id = robot_id
        self.debug = debug
        self.status = 'GOING_TO_NEXT_HEX'
        self.hex_index = 0
        self.base_battery = battery
        self.battery = self.base_battery

        self.movement_mode = 0
        self.max_delta = 10
        self.step_p = 0.6  # 0.06
        self.ball = ball

        self.np_file = np_file
        np_mat = np.loadtxt(self.np_file)
        np_mat *= 255.0 / np_mat.max()
        self.np_mat = np_mat

        self.height = height

        self.hex_external_list = hex_external_list
        self.hex_polygon = dict()

        for key in hex_internal_list.keys():
            hex_obj = Hexagon(hex_internal_list[key],
                              external_points=self.hex_external_list[key])
            self.hex_polygon[key] = hex_obj

        self.hex_internal_list = hex_internal_list

        self.hist_curve = hist_curve
        self.last_position = [ball.y, ball.x, ball.z]

        self.print_message("Starting pos: {0} {1} {2}".format(
            ball.x, ball.y, ball.z))

        self.home_position = self.last_position
        self.home_route = []
        self.return_to_route = []

        self.history = []
        self.wp_index = 0

        # Define first route to reach hexagon
        next_wp = self.hex_internal_list[self.hex_index][self.wp_index]
        go_next_wp = math_helper.get_line_points(self.last_position,
                                                 next_wp,
                                                 step=20)
        go_next_wp_3d = math_helper.get_3d_coverage_points(go_next_wp,
                                                           self.height,
                                                           self.np_file,
                                                           np_mat=self.np_mat)
        self.next_hexagon_route = go_next_wp_3d
        pass
예제 #7
0
 def setUp(self):
     path = tempfile.mktemp('.ldb')
     db = leveldb.LevelDB(path)
     self.triangle = Hexagon(db)
예제 #8
0
    def init_board(self):
        from player_input import PlayerInput
        self.player_input = PlayerInput()
        controller_lib.Controller_set_player_input(
            self.player_input._c_pointer)

        self.zoom = GlobalConsts.START_ZOOM
        self.rotation = GlobalConsts.START_ROTATION
        self.view_range = GlobalConsts.BOARD_WIDTH

        print 'Creating Board...'
        for j in range(GlobalConsts.BOARD_HEIGHT):
            for i in range(GlobalConsts.BOARD_WIDTH):
                x = i * 1.5
                y = j if not i % 2 else j + 0.5

                curr_hex = Hexagon(x * GlobalConsts.COS_60,
                                   y * GlobalConsts.SIN_60)

                controller_lib.Controller_push_hexagon(curr_hex._c_pointer)

        print "Linking Segments..."
        self.link_segments()

        if GlobalConsts.GENERATE_HILLS:
            print 'Generating hills',
            for i in range(int(20 * (GlobalConsts.BOARD_WIDTH / 100.0))):
                print '.',
                x_start = int(random() * GlobalConsts.BOARD_WIDTH)
                y_start = int(random() * GlobalConsts.BOARD_WIDTH)
                RollingHillsGen.generate(self.get_hexagon(x_start, y_start),
                                         750 *
                                         (GlobalConsts.BOARD_WIDTH / 100.0),
                                         height_range=(0, 0.015))
                RollingHillsGen.generate(self.get_hexagon(x_start, y_start),
                                         750 *
                                         (GlobalConsts.BOARD_WIDTH / 100.0),
                                         height_range=(0, -0.0225))
            print

        if GlobalConsts.GENERATE_MOUNTAINS:
            print 'Generating mountains',
            for i in range(int(7 * (GlobalConsts.BOARD_WIDTH / 100.0))):
                print '.',
                x_start = int(random() * GlobalConsts.BOARD_WIDTH)
                y_start = int(random() * GlobalConsts.BOARD_WIDTH)
                MountainRangeGen.generate(
                    self.get_hexagon(x_start, y_start),
                    Hexagon.NEIGHBOR_DIRECTION[int(
                        random() * len(Hexagon.NEIGHBOR_DIRECTION))],
                    60 * (GlobalConsts.BOARD_WIDTH / 100.0),
                    2,
                    height_range=(0.25, 1.0))
            print

        road_hexagons = []
        for i in range(3):
            while True:
                curr_hex = self.get_hexagon(
                    int(random() * GlobalConsts.BOARD_WIDTH),
                    int(random() * GlobalConsts.BOARD_HEIGHT))
                if curr_hex.is_pathable():
                    road_hexagons.append(curr_hex)
                    break

        RoadGen.generate(road_hexagons[0], road_hexagons[1])
        RoadGen.generate(road_hexagons[1], road_hexagons[2])
        RoadGen.generate(road_hexagons[2], road_hexagons[0])

        from timeit import Timer
        from functools import partial

        def get_execution_time(function, *args, **kwargs):
            """Return the execution time of a function in seconds."""
            numberOfExecTime = kwargs.pop('numberOfExecTime', 20)
            return Timer(partial(function, *args,
                                 **kwargs)).timeit(numberOfExecTime)

        from util import a_star
        # return a_star(hex_a, hex_b)

        for i in range(3):
            print 'cpp:', get_execution_time(Hexagon.find_path,
                                             road_hexagons[i - 1],
                                             road_hexagons[i])
            print 'pyc:', get_execution_time(a_star, road_hexagons[i - 1],
                                             road_hexagons[i])
            print '---'

        # while True:
        #     curr_hex = self.get_hexagon(int(random() * GlobalConsts.BOARD_WIDTH), int(random() * GlobalConsts.BOARD_HEIGHT))
        #     if curr_hex.is_pathable():
        #         break

        # # print 'dist: ', dist_between(curr_hex, curr_hex.get_neighbor('N').get_neighbor('N')), 'should be 2'
        # # print 'dist: ', dist_between(curr_hex, curr_hex.get_neighbor('S').get_neighbor('S')), 'should be 2'
        # # print 'dist: ', dist_between(curr_hex, curr_hex.get_neighbor('S').get_neighbor('S').get_neighbor('SE').get_neighbor('SE')), 'should be 3ish'

        # RoadGen.generate(
        #     curr_hex, curr_hex.get_neighbor('N').get_neighbor('N')
        # )

        # Hexagon.find_path(curr_hex, curr_hex.get_neighbor('N').get_neighbor('N'))

        # from util import dist_between
        # print dist_between(road_hexagons[0], road_hexagons[0].get_neighbor('N'))
        # print 1 / dist_between(road_hexagons[0], road_hexagons[0].get_neighbor('S'))
        # print dist_between(road_hexagons[0], road_hexagons[0].get_neighbor('NW'))

        height_list = [hex.get_height() for hex in Hexagon.get_all_hexagons()]

        max_height = max(height_list)
        min_height = min(height_list)

        if max_height and min_height:
            for hex in Hexagon.get_all_hexagons():
                height = hex.get_height()

                if height > 0.0:
                    height_percent = (height / max_height)

                    if height_percent < 0.5:
                        curr_percent = height_percent * 2.0

                        red = curr_percent * 0.7
                        green = 0.5 + curr_percent * 0.2
                        blue = curr_percent * 0.7
                    else:
                        curr_percent = (height_percent - 0.5) * 2.0

                        red = 0.7 + curr_percent * 0.3
                        green = 0.7 + curr_percent * 0.3
                        blue = 0.7 + curr_percent * 0.3
                else:
                    height_percent = abs(height / min_height)

                    if height_percent < 0.5:
                        curr_percent = height_percent * 2.0

                        red = curr_percent * 0.3
                        green = 0.5 - curr_percent * 0.5
                        blue = 0
                    else:
                        curr_percent = (height_percent - 0.5) * 2.0

                        red = 0.3 + curr_percent * 0.7
                        green = 0
                        blue = 0

                hex.set_hex_color(red, green, blue)
        else:
            for hex in Hexagon.get_all_hexagons():
                hex.set_hex_color(0, 0.5, 0)

        img_list = [
            'media/test_a.png',
            'media/test_b.png',
        ]

        for i in range(1):
            while True:
                curr_hex = self.get_hexagon(
                    int(random() * GlobalConsts.BOARD_WIDTH),
                    int(random() * GlobalConsts.BOARD_HEIGHT))
                if curr_hex.is_pathable():
                    board_obj = BoardObject(curr_hex, img_list[i])
                    break

        # orig_hex = self.get_hexagon(int(random() * GlobalConsts.BOARD_WIDTH), int(random() * GlobalConsts.BOARD_HEIGHT))
        # for i in range(10):
        #     curr_hex = orig_hex
        #     for j in range(13):
        #         curr_hex.set_select_color(1, 0, 1)
        #         k = 0.0
        #         for neigh_hex in curr_hex.get_neighbors().values():
        #             k += 1.0 / 6.0
        #             neigh_hex.set_select_color(1, k, 0)
        #         curr_hex = curr_hex.get_neighbor("N").get_neighbor("N").get_neighbor("N")

        #     orig_hex = orig_hex.get_neighbor("NE").get_neighbor("SE").get_neighbor("NE").get_neighbor("SE")

        board_obj.set_selected(True)