def tick(self): from board_object import BoardObject for board_obj in BoardObject.get_all(): if board_obj.curr_path: next_hex = board_obj.curr_path[0] board_obj.curr_path = board_obj.curr_path[1:] board_obj.move_to_hex(next_hex)
def mouse_right_click(self, x, y): curr_ctrl = Controller.get_controller() curr_hex = curr_ctrl.get_clicked_hex(x, y) if curr_hex and curr_hex.is_pathable(): from board_object import BoardObject for board_obj in BoardObject.get_all(): if board_obj.is_selected(): board_obj.set_destination(curr_hex)
def mouse_left_click(self, x, y): curr_ctrl = Controller.get_controller() curr_hex = curr_ctrl.get_clicked_hex(x, y) if curr_hex and curr_hex.get_board_object(): curr_board_obj = curr_hex.get_board_object() if curr_board_obj: from board_object import BoardObject for board_obj in BoardObject.get_all(): board_obj.set_selected(False) curr_board_obj.set_selected(True)
def get_board_object(self): from board_object import BoardObject c_pointer = hexagon_lib.Hexagon_get_board_object(self._c_pointer) return BoardObject.get_board_object(c_pointer)
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)
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)