Beispiel #1
0
 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)
Beispiel #2
0
 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)
Beispiel #3
0
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
Beispiel #4
0
 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)