def merge_walls(obstacles): """ Creates new artificial obstacles to enable diagonal walls. :param obstacles: all obstacles within sight :return: set of new imaginary obstacles """ new_obstacles = set() diag_dist = Vision._distance(Cell(0,0), Cell(1,1)) for o1 in obstacles: for o2 in obstacles: if Vision._distance(o1, o2) == diag_dist: new_obstacles.add(Cell( (o1.x+o2.x)/2 , (o1.y+o2.y)/2 )) return new_obstacles
def test_borders(game, hero): bf = game.battlefield assert bf.unit_facings[hero] is Facing.NORTH bf.move(hero, Cell(1, 1)) vision = Vision(bf) cells_seen_before = vision.std_seen_cells(hero) bf.move(hero, Cell(7, 7)) cells_seen_after = vision.std_seen_cells(hero) assert len(cells_seen_after) < len(cells_seen_before) assert Cell(8, 8) not in cells_seen_after
def test_walls_diag_block(hero, game, steel_wall): bf = game.battlefield bf.unit_locations = {} bf.units_at = {} bf.place(hero, Cell(1, 1)) assert bf.unit_facings[hero] is Facing.NORTH bf.place(steel_wall.clone(), Cell(1, 2)) bf.place(steel_wall.clone(), Cell(2, 1)) vision = Vision(bf) cells_seen = vision.std_seen_cells(hero) assert Cell(2, 2) not in cells_seen
def std_vision_field(sight_range, facing, cell_from, bf): w = bf.h h = bf.h v1 = (sight_range * 2 / 3) * 1j v2 = sight_range c1 = facing * v1 + cell_from.complex c2 = facing * (v2 - v1) + cell_from.complex xmin, xmax = min(c1.real, c2.real), max(c1.real, c2.real) ymin, ymax = min(c1.imag, c2.imag), max(c1.imag, c2.imag) xmin, xmax, ymin, ymax = [int(x) for x in [xmin, xmax, ymin, ymax]] xmin = max(0, xmin) ymin = max(0, ymin) xmax = min(w - 1, xmax) ymax = min(h - 1, ymax) visible_cells = set() if facing.real: metric = Vision.dist_eliptic_y else: metric = Vision.dist_eliptic_x for x in range(xmin, xmax + 1): for y in range(ymin, ymax + 1): cell = Cell(x, y) if metric(cell_from, cell) <= sight_range: visible_cells.add(cell) return visible_cells
def test_units_no_diag_block(hero, game, pirate_band): bf = game.battlefield bf.unit_locations = {} bf.units_at = {} bf.place(hero, Cell(1, 1)) assert bf.unit_facings[hero] is Facing.NORTH p1 = pirate_band[0] p2 = pirate_band[1] bf.place(p1, Cell(1, 2)) bf.place(p2, Cell(2, 1)) vision = Vision(bf) cells_seen = vision.std_seen_cells(hero) assert Cell(2, 2) in cells_seen
def test_backstab(game, hero, pirate): bf = game.battlefield bf.move(hero, Cell(1, 1)) bf.place(pirate, Cell(1, 2)) bf.unit_facings[hero] = Facing.NORTH bf.unit_facings[pirate] = Facing.NORTH ae = AttackEvent(pirate, hero) assert not ae.is_backstab assert ae.is_blind bf.unit_facings[pirate] = Facing.SOUTH ae = AttackEvent(pirate, hero) assert not ae.is_backstab assert not ae.is_blind bf.unit_facings[hero] = Facing.SOUTH ae = AttackEvent(pirate, hero) assert ae.is_backstab assert not ae.is_blind
def test_direction(game, hero): bf = game.battlefield assert bf.unit_facings[hero] is Facing.NORTH vision = Vision(bf) bf.unit_locations = {} bf.units_at = {} bf.place(hero, Cell(4, 4)) cells_seen_before = vision.std_seen_cells(hero) bf.unit_facings[hero] = Facing.SOUTH cells_seen_after = vision.std_seen_cells(hero) assert len(cells_seen_after) == len(cells_seen_before) assert cells_seen_before != cells_seen_after
def test_visibility(game, hero): bf = game.battlefield bf.unit_locations = {} bf.units_at = {} bf.place(hero, Cell(1, 1)) assert bf.unit_facings[hero] is Facing.NORTH vision = Vision(bf) cells_seen = vision.std_seen_cells(hero) assert Cell(0, 0) not in cells_seen assert Cell(7, 7) not in cells_seen assert Cell(7, 0) not in cells_seen assert Cell(0, 7) not in cells_seen assert Cell(1, 1) in cells_seen cell = Cell(1, int(1 + hero.sight_range)) print(cells_seen) assert cell in cells_seen cell = Cell(1 + int(hero.sight_range), 1) assert cell not in cells_seen
def obstacle(game): obstacle = Obstacle("dummy", 500, 0, None, None) game.add_obstacle(obstacle, Cell(1, 2)) return obstacle