def getEntitiesBetweenPoints(self, c1: Coords, c2: Coords, maxEntities=1000, maxRange=100000): diffrence = c2 - c1 entity_list = [ e for e in self.allEntities if e.co.distance(c1) <= maxRange and e.is_a_valid_target ] distance_between_points = c1.distance(c2) c_atan = atan2(diffrence.y, diffrence.x) entities = [] for e in entity_list: dist = c1.distance(e.co) # the distacne between the entity and c1 d: Coords = c1 + (diffrence * (dist / distance_between_points)).round if d == e.co: entities.append(e) """ local_e_co = e.co - c1 local_atan = atan2(local_e_co.y, local_e_co.x) if c_atan == local_atan: entities.append(e) """ return sorted(entities, key=lambda e: e.co.distance(c1), reverse=True)
def unit_test2(): from coords import Coords from level import Level, prep_level_for_unit_testing, BLOCKS_MOVEMENT from line_of_sight import find_end_point_alt gameLevel = Level(0) start_point = Coords(4, 4) targets = [ Coords(4, 12), Coords(12, 12), Coords(12, 4), Coords(2, 2), Coords(2, 4), Coords(1, 7) ] set_up_level(gameLevel, start_point, targets) for t in targets: ep = find_end_point_alt(gameLevel, start_point, t, BLOCKS_MOVEMENT, max_range=20) print('Start point: {}, target: {}, end point: {}, end point tile symbol: {}'.format( start_point, t, ep, gameLevel.grid[ep.y][ep.x].symbol)) print(start_point.distance(ep)) print('Max Range of 3') for t in targets: ep = find_end_point_alt(gameLevel, start_point, t, BLOCKS_MOVEMENT, max_range=3) print('Start point: {}, target: {}, end point: {}, end point tile symbol: {}'.format( start_point, t, ep, gameLevel.grid[ep.y][ep.x].symbol)) print(start_point.distance(ep)) for y in range(gameLevel.widthHeight.y): print(''.join([x.symbol for x in gameLevel.grid[y]]))
def unit_test4(): import tdl from line_of_sight import find_end_point_alt, get_entities_in_line_of_fire_hash from coords import Coords from terrain import ALL_TILE_DICT length_compatison = [] increase_amount = [] gc.set_debug(gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_STATS) pr = cProfile.Profile() from level import Level, prep_level_for_unit_testing gameLevel = Level(0) start_point = Coords(4, 4) targets = [ Coords(4, 12), Coords(12, 12), Coords(12, 4), Coords(2, 2), Coords(2, 4), Coords(1, 7), Coords(5, 7), Coords(6, 6) ] set_up_level(gameLevel, start_point, targets) console = tdl.init(gameLevel.widthHeight.x, gameLevel.widthHeight.y, title="Test", fullscreen=False) for y in range(gameLevel.widthHeight.y): console.draw_str(0, y, ''.join([s.symbol for s in gameLevel.grid[y]])) console.draw_char(start_point.x, start_point.y, '@') tdl.flush() quit = False test_crood_list = [ Coords(13, 20), Coords(16, 20), Coords(25, 20), Coords(30, 20), Coords(13, 22), Coords(16, 22), Coords(25, 22), Coords(30, 22), ] test_crood_list_y = [20, 22, 25, 28, 31, 35, 39, 42] test_crood_list_x = [13, 16, 20, 23, 25, 27, 30, 33] for y in test_crood_list_y: for x in test_crood_list_x: pr.enable() for y2 in range(gameLevel.widthHeight.y): console.draw_str(0, y2, ''.join([s.symbol for s in gameLevel.grid[y]])) console.draw_char(start_point.x, start_point.y, '@') for e in gameLevel.allEntities: console.draw_char(e.co.x, e.co.y, 'M') # tdl.flush() _x = x _y = y end_point = find_end_point_alt(gameLevel, start_point, Coords(_x, _y), BLOCKS_MOVEMENT, 20) distance_to_cursor = start_point.distance(_x, _y) distance_to_endpoint = start_point.distance(end_point) console.draw_char(end_point.x, end_point.y, '%') diff_x = end_point.x - start_point.x diff_y = end_point.y - start_point.y console.draw_char(_x, _y, '!') # tdl.flush() normed_co = Coords(diff_x, diff_y).normalize() hit_target = False new_x = start_point.x new_y = start_point.y while not hit_target: new_x += normed_co[0] new_y += normed_co[1] check_x = round(new_x) check_y = round(new_y) console.draw_char(check_x, check_y, '*') tdl.flush() hit_target = end_point.x == check_x and end_point.y == check_y ents = get_entities_in_line_of_fire_hash(gameLevel, gameLevel.player, end_point) for e in ents: console.draw_char(e.co.x, e.co.y, 'E') tdl.flush() pr.print_stats() print(gc.get_threshold()) length_compatison.append(len(gc.garbage)) if len(increase_amount) == 0: increase_amount.append(0) else: increase_amount.append(length_compatison[-1] - length_compatison[-2]) print('{} {}'.format(x, y)) print(length_compatison) print(increase_amount) # print(gc.garbage) pass
def unit_test3(): import tdl from line_of_sight import find_end_point_alt, get_entities_in_line_of_fire_hash from coords import Coords from terrain import ALL_TILE_DICT length_compatison = [] increase_amount = [] gc.set_debug(gc.DEBUG_LEAK | gc.DEBUG_STATS) pr = cProfile.Profile() from level import Level, prep_level_for_unit_testing gameLevel = Level(0) start_point = Coords(4, 4) targets = [ Coords(4, 12), Coords(12, 12), Coords(12, 4), Coords(2, 2), Coords(2, 4), Coords(1, 7), Coords(5, 7), Coords(6, 6) ] set_up_level(gameLevel, start_point, targets) console = tdl.init(gameLevel.widthHeight.x, gameLevel.widthHeight.y, title="Test", fullscreen=False) for y in range(gameLevel.widthHeight.y): console.draw_str(0, y, ''.join([s.symbol for s in gameLevel.grid[y]])) console.draw_char(start_point.x, start_point.y, '@') tdl.flush() quit = False while not quit: pr.enable() for y in range(gameLevel.widthHeight.y): console.draw_str(0, y, ''.join([s.symbol for s in gameLevel.grid[y]])) console.draw_char(start_point.x, start_point.y, '@') for e in gameLevel.allEntities: console.draw_char(e.co.x, e.co.y, 'M') tdl.flush() events = tdl.event.get() mouse_events = [e for e in events if e.type == "MOUSEUP"] key_events = [e for e in events if e.type == 'KEYDOWN'] for e in key_events: if e.key == 'ESCAPE': quit = True break if len(mouse_events) > 0: _x = mouse_events[0].cell[0] _y = mouse_events[0].cell[1] # console.draw_char(x, y, '^') end_point = find_end_point_alt(gameLevel, start_point, Coords(_x, _y), BLOCKS_MOVEMENT, 20) distance_to_cursor = start_point.distance(_x, _y) distance_to_endpoint = start_point.distance(end_point) console.draw_char(end_point.x, end_point.y, '%') diff_x = end_point.x - start_point.x diff_y = end_point.y - start_point.y console.draw_char(_x, _y, '!') tdl.flush() normed_co = Coords(diff_x, diff_y).normalize() hit_target = False new_x = start_point.x new_y = start_point.y while not hit_target: new_x += normed_co[0] new_y += normed_co[1] check_x = round(new_x) check_y = round(new_y) console.draw_char(check_x, check_y, '*') tdl.flush() hit_target = end_point.x == check_x and end_point.y == check_y ents = get_entities_in_line_of_fire_hash(gameLevel, gameLevel.player, end_point) for e in ents: console.draw_char(e.co.x, e.co.y, 'E') tdl.flush() pr.print_stats() print(gc.get_threshold()) length_compatison.append(len(gc.garbage)) if len(increase_amount) == 0: increase_amount.append(0) else: increase_amount.append(length_compatison[-1] - length_compatison[-2]) print(length_compatison) print(increase_amount) # print(gc.garbage) pass