def test_coordinate_interpollating(self): """Test coordinate interpollating.""" grid = Grid(5) grid.create_grid() result = 20 interp = grid.interpolate(0, 100, 0.2) self.assertEqual(interp, result)
def test_spiral_ring(self): """Test spiral rings.""" grid = Grid(7) grid.create_grid() hexagon = Hex(-1, 1, 0) result = [ Hex(-2, 1, 1), Hex(-1, 0, 1), Hex(0, 0, 0), Hex(0, 1, -1), Hex(-1, 2, -1), Hex(-2, 2, 0), Hex(-3, 1, 2), Hex(-2, 0, 2), Hex(-1, -1, 2), Hex(0, -1, 1), Hex(1, -1, 0), Hex(1, 0, -1), Hex(1, 1, -2), Hex(0, 2, -2), Hex(-1, 3, -2), Hex(-2, 3, -1), Hex(-3, 3, 0), Hex(-3, 2, 1), Hex(-1, 1, 0) ] self.assertEqual(grid.spiral_ring(hexagon, 2), result)
def test_wrap_around(self): """Test wrap-around coordinates.""" grid = Grid(5) grid.create_grid() coordinates = (-3, 3, 0) result = (0, -2, 2) self.assertEqual(grid.wrap_around(coordinates), result)
def test_hex_rounding(self): """Test coordinate rounding.""" grid = Grid(5) grid.create_grid() float_coordinates = (1.75, -0.75, -1.25) result_coordinates = (2, -1, -1) self.assertEqual(grid.hex_round(float_coordinates), result_coordinates)
def test_pointy(self): grid = Grid(OrientationPointy, Point(10, 20), Point(20, 10)) self.assertHexEqual(Hex(-21, 43), grid.hex_at(Point(13, 666))) self.assertHexEqual(Hex(19, 0), grid.hex_at(Point(666, 13))) self.assertHexEqual(Hex(22, -46), grid.hex_at(Point(-13, -666))) self.assertHexEqual(Hex(-19, -2), grid.hex_at(Point(-666, -13)))
def main(): # grid = Grid(coordinate_system=OFFSET, hexagon_type=FLAT) # grid[-1, -1] = None # grid[-1, 0] = None # grid[0, 0] = None # grid[0, 1] = None # grid[1, 0] = None # grid[1, 1] = None # grid[1, 2] = None # path = grid.shortest_path_coordinates((-1, -1), (1, 2)) # print(path) grid = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.CUBIC) for i in range(-3, 4): for j in range(-3, 4): c = Grid.convert((i, j), CoordinateSystem.AXIAL, CoordinateSystem.CUBIC) grid[c] = None path = grid.shortest_path_coordinates((-3, 2, 1), (3, -2, -1)) draw = DrawGrid(grid) draw.draw_hexagons(grid.keys(), labels=True, fill='#7070ff') draw.draw_hexagons(path, labels=True, fill='#ff7070') draw.draw()
def test_hex_interpollating(self): """Test hex interpollating.""" grid = Grid(5) grid.create_grid() hex_a = Hex(-1, 0, 1) hex_b = Hex(1, 0, -1) hex_result = (0, 0, 0) self.assertEqual(grid.hex_interpolate(hex_a, hex_b, 0.5), hex_result)
def test_intersecting_ranges(self): """Test intersecting hex ranges.""" grid = Grid(7) grid.create_grid() range1 = [Hex(-1, 1, 0), Hex(0, 0, 0)] range2 = [Hex(1, -1, 0), Hex(0, 0, 0)] result = [Hex(0, 0, 0)] self.assertEqual(grid.intersecting_hex_ranges(range1, range2), result)
def main(): """Test functionality.""" grid = Grid(9) hexagon = grid.get_hextile((0, 1, -1)) print(hexagon) pointy = Layout((10, 10), (200, 200)) cursor = pointy.hex_to_pixel(hexagon) print(cursor) print() ph = pointy.pixel_to_hex(cursor) result_tile = grid.hex_round((ph.x, ph.y, ph.z)) r = grid.get_hextile(result_tile) print(r)
def test_neighbors(self): grid = Grid(OrientationFlat, Point(10, 20), Point(20, 10)) hex = grid.hex_at(Point(666, 666)) expected_codes = [ 920, 922, 944, 915, 921, 923, 945, 916, 918, 926, 948, 917, 919, 925, 927, 960, 962, 968 ] neighbors = grid.hex_neighbors(hex, 2) for expected_code, neighbor_hex in zip(expected_codes, neighbors): code = grid.hex_to_code(neighbor_hex) self.assertEqual(code, expected_code, neighbor_hex)
def test_flat(self): grid = Grid(OrientationFlat, Point(10, 20), Point(20, 10)) self.assertHexEqual(Hex(0, 37), grid.hex_at(Point(13, 666))) self.assertHexEqual(Hex(22, -11), grid.hex_at(Point(666, 13))) self.assertHexEqual(Hex(-1, -39), grid.hex_at(Point(-13, -666))) self.assertHexEqual(Hex(-22, 9), grid.hex_at(Point(-666, -13))) grid = Grid(OrientationFlat, Point(0, 0), Point(5, 5)) self.assertHexEqual(Hex(0, 37), grid.hex_at(Point(0, 320)))
def test_hex_linedrawing(self): """Test line drawing.""" grid = Grid(5) grid.create_grid() hex_a = Hex(-2, 0, 2) hex_b = Hex(2, 0, -2) result = [ Hex(-2, 0, 2), Hex(-1, 0, 1), Hex(0, 0, 0), Hex(1, 0, -1), Hex(2, 0, -2) ] self.assertEqual(grid.hex_linedraw(hex_a, hex_b), result)
def test_dijkstra(self): """Test dijkstras.""" grid = Grid(5) grid.create_grid() hexagon = Hex(-1, 1, 0) grid.get_hextile((0, 0, 0)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) grid.get_hextile((0, 1, -1)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) grid.get_hextile((-1, 0, 1)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) result = [ Hex(-1, 1, 0), Hex(-1, 2, -1), Hex(-2, 2, 0), Hex(-2, 1, 1), Hex(0, 2, -2), Hex(2, 0, -2), Hex(2, -1, -1), Hex(-2, 0, 2), Hex(2, -2, 0), Hex(1, -2, 1), Hex(0, -2, 2) ] dijkstra = [x for x in grid.dijkstra(hexagon, 2)] result = set(result).difference(dijkstra) self.assertEqual(set(), result)
def main(): g = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.CUBIC) for i in range(-3, 4): for j in range(-3, 4): c = Grid.convert((i, j), CoordinateSystem.AXIAL, CoordinateSystem.CUBIC) g[c] = None # g = Grid(HexagonType.FLAT, CoordinateSystem.AXIAL) # for i in range(5): # for j in range(5): # g[i, j] = None draw_obj = DrawGrid(g) draw_obj.draw_hexagons(g.keys(), labels=True, fill='#7070ff') draw_obj.draw()
def test_region(self): grid = Grid(OrientationFlat, Point(10, 20), Point(20, 10)) geometry = [ Point(20, 19.99999), Point(20, 40), Point(40, 60), Point(60, 40), Point(50, 30), Point(40, 40) ] region = grid.make_region(geometry) hexes = region.hexes expected_codes = [0, 2, 1, 3, 9, 4] self.assertEqual(len(hexes), len(expected_codes)) for hex, expected_code in zip(hexes, expected_codes): code = grid.hex_to_code(hex) self.assertEqual(code, expected_code)
def test_offset_conversion(self): coords = [(0, 0), (0, 1), (0, 2), (-1, 2), (3, -4)] expected = [(0, 0), (1, 1), (0, 2), (-1, 2), (3, -4)] for c, e in zip(coords, expected): out = Grid.convert(c, OFFSET_ODD_ROWS, OFFSET_EVEN_ROWS) self.assertSequenceEqual(out, e) coords = [(0, 0), (0, 1), (0, 2), (-1, 2), (3, -4)] expected = [(0, 0), (0, 1), (-1, 1), (-2, 1), (5, -2)] for c, e in zip(coords, expected): out = Grid.convert(c, OFFSET_ODD_ROWS, OFFSET_ODD_COLUMNS) self.assertSequenceEqual(out, e) coords = [(0, 0), (0, 1), (0, 2), (-1, 2), (3, -4)] expected = [(0, 0), (0, 1), (-1, 2), (-2, 1), (5, -1)] for c, e in zip(coords, expected): out = Grid.convert(c, OFFSET_ODD_ROWS, OFFSET_EVEN_COLUMNS) self.assertSequenceEqual(out, e)
def test_subtracting_hex_from_hex(self): """Test subtracting hexs.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((1, 0, -1)) hex_b = grid.get_hextile((-1, 2, -1)) hex_result = Hex(2, -2, 0) self.assertEqual(grid.sub_coords(hex_a, hex_b), hex_result)
def test_adding_hex_to_hex(self): """Test adding hexs.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((1, 0, -1)) hex_b = grid.get_hextile((-1, 2, -1)) hex_result = Hex(0, 2, -2) self.assertEqual(grid.add_coords(hex_a, hex_b), hex_result)
def test_coordinates_pointy(self): grid = Grid(OrientationPointy, Point(10, 20), Point(20, 10)) hex = grid.hex_at(Point(666, 666)) self.assertPointEqual(Point(650.85880, 665.00000), grid.hex_center(hex), 0.00001) corners = grid.hex_corners(hex) expected_corners = [ Point(668.17930, 670.00000), Point(650.85880, 675.00000), Point(633.53829, 670.00000), Point(633.53829, 660.00000), Point(650.85880, 655.00000), Point(668.17930, 660.00000), ] for expected_point, point in zip(expected_corners, corners): self.assertPointEqual(expected_point, point, 0.00001)
def test_coordinates_flat(self): grid = Grid(OrientationFlat, Point(10, 20), Point(20, 10)) hex = grid.hex_at(Point(666, 666)) self.assertPointEqual(Point(670.00000, 660.85880), grid.hex_center(hex), 0.00001) corners = grid.hex_corners(hex) expected_corners = [ Point(690.00000, 660.85880), Point(680.00000, 669.51905), Point(660.00000, 669.51905), Point(650.00000, 660.85880), Point(660.00000, 652.19854), Point(680.00000, 652.19854), ] for expected_point, point in zip(expected_corners, corners): self.assertPointEqual(expected_point, point, 0.00001)
def main(): grid = Grid(coordinate_system=OFFSET, hexagon_type=FLAT) grid[-1, -1] = None grid[0, 0] = None grid[0, 1] = None grid[1, 0] = None grid[1, 1] = None grid[1, 2] = None neighbors = grid.neighbor_coordinates((0, 0)) line = grid.line_coordinates((0, 0), (4, 4), validate=False) draw = DrawGrid(grid) draw.draw_hexagons(grid.keys(), labels=True, fill='#7070ff') draw.draw_hexagons(neighbors, labels=True, fill='#ff7070') draw.draw_hexagons(line, labels=True, fill='#70ff70') draw.draw()
def test_rotate_left_of_hex(self): """Test rotate left.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((1, 0, -1)) hex_result = Hex(1, -1, 0) self.assertEqual(grid.rotate_left(hex_a), hex_result)
def test_getting_neighbour_of_hex_in_direction(self): """Test get neighbour in direction.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((1, -1, 0)) hex_result = Hex(2, -1, -1) self.assertEqual(grid.get_neighbour_in_direction(hex_a, 1), hex_result)
def test_scaling_of_hex(self): """Test hex scaling.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((0, 1, -1)) hex_result = Hex(0, 2, -2) self.assertEqual(grid.scale(hex_a, 2), hex_result)
def test_shortest_path(self): """Test shortest path.""" grid = Grid(5) grid.create_grid() start = Hex(-1, 1, 0) end = Hex(1, -1, 0) grid.get_hextile((0, 0, 0)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) grid.get_hextile((0, 1, -1)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) grid.get_hextile((-1, 0, 1)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) result = [Hex(-1, 2, -1), Hex(2, -2, 0), Hex(1, -1, 0)] self.assertEqual(grid.shortest_path(start, end, 3), result)
def test_distance_between_hexes(self): """Test distance between hexs.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((-2, 0, 2)) hex_b = grid.get_hextile((2, -2, 0)) result = 4 self.assertEqual(grid.hex_distance(hex_a, hex_b), result)
def test_insert_remove(self): g = Grid() g[0, 0] = True g[1, 1] = False self.assertTrue((0, 0) in g) self.assertTrue((1, 1) in g) self.assertEqual(len(g), 2) self.assertEqual(g[0, 0], True) self.assertEqual(g[1, 1], False) del g[0, 0] self.assertFalse((0, 0) in g) self.assertEqual(len(g), 1) del g[1, 1] self.assertFalse((1, 1) in g) self.assertEqual(len(g), 0)
def test_neighbors(self): g = Grid(hexagon_type=POINTY, coordinate_system=OFFSET_ODD_ROWS) g[1, 1] = 'center' g[1, 0] = 'adjacent 1' g[2, 0] = 'adjacent 2' g[3, 3] = 'not adjacent' n = g.neighbors((1, 1)) self.assertSequenceEqual(n, ['adjacent 2', 'adjacent 1']) g = Grid(hexagon_type=POINTY, coordinate_system=CUBIC) g[0, 0, 0] = 'center' g[1, 0, -1] = 'adjacent 1' g[-1, 0, 1] = 'adjacent 2' g[0, 2, -2] = 'not adjacent' n = g.neighbors((0, 0, 0)) self.assertSequenceEqual(n, ['adjacent 1', 'adjacent 2']) g = Grid() g[0, 0] = 'center' self.assertEqual([], g.neighbors((0, 0)))
def test_ring(self): g = Grid(HexagonType.FLAT, CoordinateSystem.AXIAL) for i in range(5): for j in range(5): g[i, j] = None expected = [None] * 3 self.assertCountEqual(g.ring((0, 0), 2), expected) expected = g.neighbors((1, 2)) self.assertCountEqual(g.ring((1, 2), 1), expected) g = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.CUBIC) for i in range(-3, 4): for j in range(-3, 4): c = Grid.convert((i, j), CoordinateSystem.AXIAL, CoordinateSystem.CUBIC) g[c] = None expected = [None] * 4 self.assertCountEqual(g.ring((3, -6, 3), 3), expected)
def join_game(self): """Ask the server to join a game.""" join_game_action = action.JoinGameAction() reply = self.send_action(join_game_action, self.con) if reply.type == "ServerError": self._log.error(reply.obj) # raise action.ServerError(reply.obj) else: self._log.info("Joined game player id = " + str(reply.obj)) game_id, self.id = reply.obj grid = Grid(20) self._game_state = GameState(game_id, 1, grid, self._log) self._game_state._grid.create_grid() self._game_state._grid.static_map() civ = Civilisation(self.id, self._game_state._grid, self._log) self._game_state.add_civ(civ) self._game_state._my_id = self.id