Exemplo n.º 1
0
    def generate_yards(self):
        """
        Generate yards, building lots, path doors, building doors and place them within the self.surface member
        :returns: void (sets self.surface)
        """
        new_surface = copy(self.surface)
        probability_generate_yard = 0.90  # Chance of partition being a yard
        for p in self.partitions:
            partition_prob = float(
                (lehmer_2(lehmer_2(self.seed, p[1][0] - p[0][0]),
                          p[1][1] - p[0][1])) % 100) / 100
            if partition_prob < probability_generate_yard:
                new_surface = self.run_ca_on_partition(new_surface, p)
                new_surface = self.generate_building_lots(new_surface, p)
                new_surface = self.generate_building_door_blocks(
                    new_surface, p)
                new_surface = self.generate_door_blocks(new_surface, p)

                # draw
                if __name__ != "__main__":
                    for i in range(p[0][0], p[1][0]):
                        for j in range(p[0][1], p[1][1]):
                            if self.surface.surface_map[i][j].type == Block.YARD \
                                    and self.count_neighbours(i, j, p[0][0], p[1][0], p[0][1], p[1][1], Block.UNASSIGNED) > 1:
                                utilityFunctions.setBlock(
                                    self.level,
                                    BlockUtils.get_fence_block(
                                        self.surface.surface_map[i]
                                        [j].biome_id),
                                    self.surface.to_real_x(i),
                                    self.surface.surface_map[i][j].height + 1,
                                    self.surface.to_real_z(j))
                            if self.surface.surface_map[i][
                                    j].type == Block.YARD and self.surface.surface_map[
                                        i][j].is_water:
                                utilityFunctions.setBlock(
                                    self.level,
                                    BlockUtils.get_bridge_block(
                                        self.surface.surface_map[i]
                                        [j].biome_id),
                                    self.surface.to_real_x(i),
                                    self.surface.surface_map[i][j].height,
                                    self.surface.to_real_z(j))
        self.surface = new_surface
Exemplo n.º 2
0
    def find_paths_BFS(self, start, end):
        path = {}
        cell_queue = [start]
        path[start] = None
        while len(cell_queue) > 0:
            current = cell_queue.pop(0)
            if current == end:
                break
            for next in self.get_block_neighbors(current):
                if next not in path:
                    cell_queue.append(next)
                    path[next] = self.subtract(current, next)

        # Draw paths
        current = self.add(end, path[end])
        direction = path[end]
        prev = end
        while current != start:
            # find next in path
            x, z = current
            block = self.surface.surface_map[x][z]
            previous_block = self.surface.surface_map[prev[0]][prev[1]]
            block.type = Block.PATH
            steepness = previous_block.height - block.height
            new_height = block.height
            if steepness > 1:
                if steepness > 4:
                    new_height = self.surface.surface_map[prev[0]][
                        prev[1]].height
                else:
                    new_height = self.surface.surface_map[prev[0]][
                        prev[1]].height - 1
            elif previous_block.height - block.height < -1:
                new_height = self.surface.surface_map[prev[0]][
                    prev[1]].height + 1
            block.height = new_height
            # If path is moving horizontally, try to widen it by adding blocks above and below it
            if direction == (-1, 0) or direction == (1, 0):
                path_edges = [(x, z - 1), (x, z + 1)]
            # If path is moving vertically, try to widen it by adding blocks to the left and to the right of it
            else:
                path_edges = [(x - 1, z), (x + 1, z)]
            path_edges = filter(self.block_in_grid, path_edges)
            path_edges = filter(self.block_is_free, path_edges)
            for edge in path_edges:
                edge_block = self.surface.surface_map[edge[0]][edge[1]]
                edge_block.type = Block.PATH
                # Set path block in level
                level_block = BlockUtils.get_road_block(edge_block.biome_id)
                bridge_block = BlockUtils.get_bridge_block(edge_block.biome_id)
                if edge_block.is_water:
                    utilityFunctions.setBlock(
                        self.level, bridge_block,
                        self.surface.to_real_x(edge_block.x), new_height,
                        self.surface.to_real_z(edge_block.z))
                else:
                    utilityFunctions.setBlock(
                        self.level, level_block,
                        self.surface.to_real_x(edge_block.x), new_height,
                        self.surface.to_real_z(edge_block.z))
                # Remove all blocks above paths
                above = new_height + 1
                while self.level.blockAt(
                        self.surface.to_real_x(edge_block.x), above,
                        self.surface.to_real_z(edge_block.z)) != 0:
                    utilityFunctions.setBlock(
                        self.level, (0, 0),
                        self.surface.to_real_x(edge_block.x), above,
                        self.surface.to_real_z(edge_block.z))
                    above += 1
            # Set path block in level
            level_block = BlockUtils.get_road_block(block.biome_id)
            bridge_block = BlockUtils.get_bridge_block(block.biome_id)
            if block.is_water:
                utilityFunctions.setBlock(self.level, bridge_block,
                                          self.surface.to_real_x(block.x),
                                          new_height,
                                          self.surface.to_real_z(block.z))
            else:
                utilityFunctions.setBlock(self.level, level_block,
                                          self.surface.to_real_x(block.x),
                                          new_height,
                                          self.surface.to_real_z(block.z))
            above = new_height + 1
            # Remove all blocks above paths
            while self.level.blockAt(self.surface.to_real_x(x), above,
                                     self.surface.to_real_z(z)) != 0:
                utilityFunctions.setBlock(self.level, (0, 0),
                                          self.surface.to_real_x(block.x),
                                          above,
                                          self.surface.to_real_z(block.z))
                above += 1
            prev = current
            current = self.add(current, path[current])
            direction = path[current]