Skip to content
The Chat Archive
claude-3-5-sonnet-20241022· Anthropic LMArena Human Preference 140K

Je voudrais que tu genere une class TerrainGenerator qui dans un moteur de jeu de Voxel…

  1. User

    Je voudrais que tu genere une class TerrainGenerator qui dans un moteur de jeu de Voxel permet la generation procedural du terrain avec FastNoiseLite. Utilise ces Enums `enum BiomeType { DESERT, FOREST, PLAINS, MOUNTAINS, SNOWY };

    enum TextureType { BEDROCK, BRICKS, COBBLESTONE, DIRT, GLASS, GRASS_TOP, GRASS_SIDE, GRAVEL, OAK_LEAVES, OAK_LOG_TOP, OAK_LOG, OAK_PLANKS, SAND, SNOW, STONE_BRICKS, STONE, // ORES COAL_ORE, COPPER_ORE, DIAMOND_ORE, EMERALD_ORE, GOLD_ORE, IRON_ORE, LAPIS_ORE, REDSTONE_ORE, WATER, COUNT, // Keep last AIR // Keep after count beacuse AIR is not a texture }; La class doit avoir une fonction qui sera appeller par chaque chunk avec en parametre le X et Z qui sont les coordonnées en voxel dans le mondes (pas besoin de les multiplier par la taille du chunk c'est deja fait) et cette fonction doit renvoyer un std::array<Voxel, CHUNK_VOLUME> et voici les constexpr pour le chunk static constexpr int CHUNK_SIZE = 16; // Size of a chunk in voxels static constexpr int CHUNK_HEIGHT = 256; // Height of a chunk in voxels static constexpr int CHUNK_VOLUME = CHUNK_SIZE * CHUNK_HEIGHT * CHUNK_SIZE; // Total number of voxels in a chunk`

  2. Assistant

    Je vais vous aider à créer une classe TerrainGenerator qui utilise FastNoiseLite pour générer un terrain de type Minecraft. Voici une implémentation :

    #include <FastNoiseLite.h>
    #include <array>
    #include <cmath>
    
    class TerrainGenerator {
    private:
        FastNoiseLite terrainNoise;
        FastNoiseLite biomeNoise;
        FastNoiseLite caveNoise;
        
        static constexpr float TERRAIN_SCALE = 100.0f;
        static constexpr float BIOME_SCALE = 500.0f;
        static constexpr float CAVE_SCALE = 50.0f;
        static constexpr int SEA_LEVEL = 64;
        static constexpr int MOUNTAIN_HEIGHT = 128;
        
    public:
        TerrainGenerator(int seed) {
            terrainNoise.SetSeed(seed);
            terrainNoise.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
            terrainNoise.SetFrequency(1.0f / TERRAIN_SCALE);
            
            biomeNoise.SetSeed(seed + 1);
            biomeNoise.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
            biomeNoise.SetFrequency(1.0f / BIOME_SCALE);
            
            caveNoise.SetSeed(seed + 2);
            caveNoise.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
            caveNoise.SetFrequency(1.0f / CAVE_SCALE);
        }
    
        BiomeType getBiomeAt(int x, int z) {
            float biomeValue = biomeNoise.GetNoise((float)x, (float)z);
            
            if (biomeValue < -0.6f) return BiomeType::SNOWY;
            if (biomeValue < -0.2f) return BiomeType::MOUNTAINS;
            if (biomeValue < 0.2f) return BiomeType::PLAINS;
            if (biomeValue < 0.6f) return BiomeType::FOREST;
            return BiomeType::DESERT;
        }
    
        std::array<Voxel, CHUNK_VOLUME> generateChunk(int chunkX, int chunkZ) {
            std::array<Voxel, CHUNK_VOLUME> chunk{};
            
            for (int x = 0; x < CHUNK_SIZE; x++) {
                for (int z = 0; z < CHUNK_SIZE; z++) {
                    int worldX = chunkX + x;
                    int worldZ = chunkZ + z;
                    
                    BiomeType biome = getBiomeAt(worldX, worldZ);
                    float heightNoise = terrainNoise.GetNoise((float)worldX, (float)worldZ);
                    
                    // Calcul de la hauteur en fonction du biome
                    int height = SEA_LEVEL;
                    switch (biome) {
                        case BiomeType::MOUNTAINS:
                            height += (int)(heightNoise * MOUNTAIN_HEIGHT);
                            break;
                        case BiomeType::PLAINS:
                        case BiomeType::FOREST:
                            height += (int)(heightNoise * 20);
                            break;
                        case BiomeType::DESERT:
                            height += (int)(heightNoise * 15);
                            break;
                        case BiomeType::SNOWY:
                            height += (int)(heightNoise * 25);
                            break;
                    }
                    
                    for (int y = 0; y < CHUNK_HEIGHT; y++) {
                        int index = y * CHUNK_SIZE * CHUNK_SIZE + z * CHUNK_SIZE + x;
                        
                        // Génération des caves
                        float caveValue = caveNoise.GetNoise((float)worldX, (float)y, (float)worldZ);
                        bool isCave = caveValue > 0.5f && y < height - 5 && y > 10;
                        
                        if (y > height) {
                            chunk[index].type = y <= SEA_LEVEL ? TextureType::WATER : TextureType::AIR;
                        } else if (isCave) {
                            chunk[index].type = TextureType::AIR;
                        } else {
                            // Définition des blocs en fonction du biome et de la profondeur
                            if (y == 0) {
                                chunk[index].type = TextureType::BEDROCK;
                            } else if (y == height) {
                                switch (biome) {
                                    case BiomeType::DESERT:
                                        chunk[index].type = TextureType::SAND;
                                        break;
                                    case BiomeType::SNOWY:
                                        chunk[index].type = TextureType::SNOW;
                                        break;
                                    default:
                                        chunk[index].type = TextureType::GRASS_TOP;
                                }
                            } else if (y > height - 4) {
                                chunk[index].type = biome == BiomeType::DESERT ? TextureType::SAND : TextureType::DIRT;
                            } else if (y > height - 8) {
                                chunk[index].type = TextureType::STONE;
                            } else {
                                // Génération des minerais
                                float oreNoise = terrainNoise.GetNoise((float)worldX * 2, (float)y * 2, (float)worldZ * 2);
                                if (oreNoise > 0.8f) {
                                    if (y < 16) chunk[index].type = TextureType::DIAMOND_ORE;
                                    else if (y < 32) chunk[index].type = TextureType::GOLD_ORE;
                                    else if (y < 64) chunk[index].type = TextureType::IRON_ORE;
                                    else chunk[index].type = TextureType::COAL_ORE;
                                } else {
                                    chunk[index].type = TextureType::STONE;
                                }
                            }
                        }
                    }
                }
            }
            
            return chunk;
        }
    };
    

    Cette implémentation inclut :

    1. Trois générateurs de bruit différents :

      • terrainNoise pour la hauteur du terrain
      • biomeNoise pour la distribution des biomes
      • caveNoise pour la génération des caves
    2. Une fonction getBiomeAt qui détermine le biome en fonction des coordonnées

    3. La fonction principale generateChunk qui :

      • Génère le terrain de base selon le biome
      • Ajoute des caves
      • Place les différents types de blocs (surface, sous-sol, minerais)
      • Gère l'eau au niveau de la mer
      • Adapte la hauteur et le type de surface selon le biome
    4. Des constantes pour contrôler la génération :

      • Échelles de bruit différentes pour le terrain, les biomes et les caves
      • Niveau de la mer
      • Hauteur maximum des montagnes

    Vous pouvez ajuster les paramètres (TERRAIN_SCALE, BIOME_SCALE, etc.) pour obtenir différents types de terrain. La génération des minerais est simplifiée mais peut être rendue plus complexe en ajoutant des conditions supplémentaires.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.