def test_get_radius_include_0(self): s = Space((6,6,3)) p = PathManager(s) for x in range(6): for y in range(6): s[(x,y,0)] = 1 self.assertEqual(p.radius_include((2,2,0),(2,2,0)), set([(2,2,0)]))
def test_find_path(self): s = Space((6,6,3)) p = PathManager(s) for x in range(6): for y in range(6): s[(x,y,0)] = 1 self.assertEqual(len(p.find_path((1,2,1),(4,5,1))), 4)
def test_adjacent_even_x(self): p = PathManager(Space((4,4,3))) t = (2,1) a = set([ (2,0), (1,0), (3,0), (1,1), (3,1), (2,2)]) self.assertEqual(p.adjacent_xy(t), a)
def test_adjacent_edge(self): p = PathManager(Space((4,4,3))) t = (0,1) a = set([(0,0), (1,0), (1,1), (0,2)]) self.assertEqual(p.adjacent_xy(t), a)
def test_find_path_inside_block(self): s = Space((3,2,3)) p = PathManager(s) for x in range(3): for y in range(2): s[(x,y,0)] = 1 s[(1,0,2)] = 1 self.assertEqual(p.find_path((2,0,1),(1,0,2)), None)
def test_get_radius_1(self): s = Space((6,6,3)) p = PathManager(s) for x in range(6): for y in range(6): s[(x,y,0)] = 1 self.assertEqual(p.within_radius((2,2,0),1), set([(2,1,0),(3,1,0),(3,2,0),(2,3,0),(1,2,0), (1,1,0),(2,2,0)]))
def test_find_path_3d(self): s = Space((3,2,3)) p = PathManager(s) for x in range(3): for y in range(2): s[(x,y,0)] = 1 s[(1,0,1)] = 1 e = [(1,0,2),(0,0,1)] self.assertEqual(p.find_path((2,0,1),(0,0,1)), e)
def test_no_path(self): s = Space((3,2,3)) p = PathManager(s) for x in range(3): for y in range(2): s[(x,y,0)] = 1 s[(1,0,1)] = 1 s[(1,0,2)] = 1 s[(1,1,1)] = 1 s[(1,1,2)] = 1 self.assertEqual(p.find_path((2,0,1),(0,0,1)), None)
def test_get_radius_include_2(self): s = Space((6,6,3)) p = PathManager(s) for x in range(6): for y in range(6): s[(x,y,0)] = 1 self.assertEqual(p.radius_include((2,2,0),(1,0,0)), set([(2,1,0),(3,1,0),(3,2,0),(2,3,0),(1,2,0), (1,1,0),(2,2,0),(2,0,0),(3,0,0),(4,1,0), (4,2,0),(4,3,0),(3,3,0),(2,4,0),(1,3,0), (0,3,0),(0,2,0),(0,1,0),(1,0,0)]))
def test_find_path_out_of_limits(self): s = Space((48,32,8)) p = PathManager(s) for x in range(48): for y in range(32): s[(x,y,6)] = 1 s[(31,27,7)] = 1 try: p.find_path((31,28,7),(31,27,8)) self.fail('Expected IndexError') except IndexError: pass
def test_open_adjacent(self): s = Space((4,4,3)) for x in range(4): for y in range(4): s[(x,y,0)] = 1 s[(1,0,1)] = s[(1,0,2)] = 1 s[(2,2,1)] = s[(2,2,2)] = 1 p = PathManager(s) t = (2,1,1) a = set([ (2,0,1), (3,0,1), (1,1,1), (3,1,1)]) self.assertEqual(p.open_adjacent(t), a)
def test_open_adjacent_step_down_blocked(self): s = Space((4,4,3)) for x in range(4): for y in range(4): s[(x,y,0)] = 1 s[(x,y,1)] = 1 s[(1,0,1)] = 0 s[(1,0,2)] = 1 s[(2,2,1)] = 0 p = PathManager(s) t = (2,1,2) a = set([ (2,0,2), (3,0,2), (1,1,2), (3,1,2), (2,2,1)]) self.assertEqual(p.open_adjacent(t), a)
def test_find_path_prefer_flat(self): s = Space((4,2,4)) p = PathManager(s) for x in range(4): for y in range(2): s[(x,y,0)] = 1 # two paths, via 2,0,1 or 2,1,1: find the preferred path for flat f = p.find_path((1,0,1),(3,0,1)) not_f0 = (2,0,1) if f[0] == (2,1,1) else (2,1,1) # turn into a hill s[f[0]] = 1 # should path around it h = p.find_path((1,0,1),(3,0,1)) self.assertEqual(h[0], not_f0) # block new path entirely s[h[0]] = 1 s[(h[0][0],h[0][1],h[0][2]+1)] = 1 # should be back to first, one step up f[0] = (f[0][0], f[0][1], f[0][2]+1) self.assertEqual(p.find_path((1,0,1),(3,0,1)), f)
def __init__(self, dim): self.dim = (dim[0], dim[1], dim[2]) self.pathing = PathManager(self) self.cache = {} self.changed = False
class Space(object): def __init__(self, dim): self.dim = (dim[0], dim[1], dim[2]) self.pathing = PathManager(self) self.cache = {} self.changed = False def maketree(self, loc): tree = Tree(loc, choice(Wood.__subclasses__()), Leaf) surround = self.pathing.adjacent_xy(loc[0:2]) height = randint(6,18) branch = None for i in range(height): trunk = (loc[0], loc[1], loc[2] + i) tile = TreeTrunk(tree) tree.trunk.append(trunk) self.cache[trunk] = tile color = tile.color if i > 3: branch = choice([b for b in surround if b != branch] + [None]) if branch is not None: varient = -1 if branch[0] == loc[0]: if branch[1] == loc[1] - 1: varient = 1 # N else: varient = 0 # S elif branch[0] < loc[0]: if branch[1] == loc[1] + (loc[0]&1): varient = 2 # SW else: varient = 4 # NW else: if branch[1] == loc[1] + (loc[0]&1): varient = 3 # SE else: varient = 5 # NE self.cache[branch + (loc[2]+i,)] = Branch(tree, varient) tree.branches.append(branch + (loc[2]+i,)) if i > 2: available = [s for s in surround if s + (loc[2]+i,) not in self.cache] leaves = sample(available, len(available)-1) for leaf in leaves: self.cache[leaf + (loc[2]+i,)] = Leaves(tree) tree.leaves.append(leaf + (loc[2]+i,)) self.cache[loc[0:2] + (loc[2]+height,)] = Leaves(tree) tree.leaves.append(loc[0:2] + (loc[2]+height,)) def get_dimensions(self): return self.dim def groundlevel(self, x, y): return 64 def soillevel(self, x, y): return 3 def __getitem__(self, loc): if not all([0 <= loc[i] < self.dim[i] for i in range(3)]): return None g = self.groundlevel(loc[0], loc[1]) if loc not in self.cache: if loc[2] == g: tile = Floor(randint(0,3), Grass) elif loc[2] > g: tile = Empty() elif loc[2] > g - self.soillevel(loc[0], loc[1]): tile = Earth(Clay, randint(0,3)) else: tile = Earth(Stone, randint(0,3)) if not isinstance(tile, Empty): for x, y in self.pathing.adjacent_xy(loc[0:2]): if self.groundlevel(x, y) <= loc[2]: tile.revealed = True break self.cache[loc] = tile else: tile = self.cache[loc] if loc[2] >= g: tile.revealed = True return tile def __setitem__(self, loc, item): if loc not in self.cache: raise ValueError self.cache[loc] = item self.changed = True
def test_distance_y(self): p = PathManager(Space((4,4,3))) self.assertEqual(p.distance_xy((1,0),(1,2)), 2)
def test_adjacent_corner(self): p = PathManager(Space((4,4,3))) t = (3,3) a = set([ (3,2), (2,3)]) self.assertEqual(p.adjacent_xy(t), a)
def test_distance_x_y_2(self): p = PathManager(Space((4,4,3))) self.assertEqual(p.distance_xy((0,0),(2,3)), 4)
def test_distance_x_y_3(self): p = PathManager(Space((8,8,3))) self.assertEqual(p.distance_xy((5,5),(1,2)), 5)
def test_distance_x_y_4(self): p = PathManager(Space((8,8,3))) self.assertEqual(p.distance_xy((1,0),(5,5)), 7)
def test_distance_x_3(self): p = PathManager(Space((4,4,3))) self.assertEqual(p.distance_xy((2,1),(3,1)), 1)