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 checkForObstructionBetweenPoints(self, originPoint, destinationPoint, checkForEntitiesBetweenPoints=False, maxRange=100000) -> bool: #moveThru end_point = find_end_point_alt(self, originPoint, destinationPoint, BLOCKS_MOVEMENT, maxRange) d1 = destinationPoint - originPoint d2 = d1.normalize(new_tuple=False) d3 = (d2 * (maxRange + 1)).round n: Coords = d3 + originPoint return end_point != n
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
def getEndPoint(self, caster, targetCo, maxRange=100000): return find_end_point_alt(self, caster.co, targetCo, BLOCKS_MOVEMENT, maxRange)