import block, perlin, perlin3d CHUNK_WIDTH = 32 CHUNK_HEIGHT = 32 SURFACE_CHUNK = 2 class StructureType: SURFACE = 0 class Structure: def __init__(self, width, height, blocks, struct_type): self.width = width self.height = height self.blocks = blocks self.struct_type = struct_type def get(self, x, y): return self.blocks[x + self.width * y] structures = [ Structure(3, 5, [9, 9, 9, 9, 9, 9, 0, 8, 0, 0, 8, 0, 0, 8, 0], StructureType.SURFACE), # TREE ] class Chunk: def get_height(self, x): return int(90 + perlin3d.noise2(x / 10, 0.1) * 10) def gen_block(self, x: int, y: int, height): stone = 4 + perlin3d.noise2(x, 0.5 ) * 2 if y < height: return block.BlockAir() if y == height: return block.BlockGrass() if y < height + stone: return block.BlockDirt() if y >= height + stone: cave = perlin3d.noise3(x / 20, y / 20, 0.05) > 0 if cave: return block.BlockAir() if perlin3d.noise3(x / 5, y / 5, 0.05) > 0.4: return block.BlockCoal() if perlin3d.noise3(x / 5, y / 5, 1.05) > 0.41: return block.BlockIron() if perlin3d.noise3(x / 5, y / 5, 2.05) > 0.42: return block.BlockGold() if perlin3d.noise3(x / 15, y / 15, 3.05) > 0.47: return block.BlockDiamond() return block.BlockStone() return block.BlockAir() def get(self, x: int, y: int) -> block.Block(id): return self.blocks[x + CHUNK_WIDTH * y] def apply_strucutre(self, struct, x, y): for i in range(struct.width): if x + i >= CHUNK_WIDTH: continue for j in range(struct.height): if y + j >= CHUNK_HEIGHT: continue if struct.blocks[i + j * struct.width] == 0: continue self.blocks[x + i + CHUNK_WIDTH * (y + j)] = block.with_id(struct.blocks[i + j * struct.width]) def apply_surface(self, struct): if self.y > SURFACE_CHUNK: return for i in range(struct.width // 2, CHUNK_WIDTH): if i % struct.width != 0: continue if perlin3d.noise2(i, self.x / 10) > 0: self.apply_strucutre(struct, i, self.get_height(self.x * CHUNK_WIDTH + i + 1) - self.y * CHUNK_HEIGHT - struct.height) def __init__(self, x: int, y: int): self.x = x self.y = y self.blocks = [0 for i in range(CHUNK_WIDTH * CHUNK_HEIGHT)] x *= CHUNK_WIDTH y *= CHUNK_HEIGHT for i in range(CHUNK_WIDTH): height = self.get_height(i + x) for j in range(CHUNK_HEIGHT): self.blocks[i + j * CHUNK_WIDTH] = self.gen_block(i + x, j + y, height) for structure in structures: if structure.struct_type == StructureType.SURFACE: self.apply_surface(structure)