From 732f74de91286b4377a480b388952a5502925f71 Mon Sep 17 00:00:00 2001 From: Louis-CharlesG Date: Mon, 20 Nov 2023 15:39:11 -0500 Subject: [PATCH 1/6] Changement dans le chunk a ajout des arbes avec le seed. --- SQCSim-common/chunk.cpp | 328 +++++++++++++++++++++++++++------------- SQCSim-common/chunk.h | 2 +- 2 files changed, 226 insertions(+), 104 deletions(-) diff --git a/SQCSim-common/chunk.cpp b/SQCSim-common/chunk.cpp index cb93218..7a5485e 100644 --- a/SQCSim-common/chunk.cpp +++ b/SQCSim-common/chunk.cpp @@ -1,137 +1,259 @@ #include "chunk.h" #include "world.h" +#include -Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y) { - //std::ostringstream pos; // Vérifie l'existence d'un fichier .chunk avec sa position. - //pos << CHUNK_PATH << x << '_' << y << ".chunk"; - //std::ifstream input(pos.str(), std::fstream::binary); - //if (input.fail()) { - OpenSimplexNoise::Noise simplex = OpenSimplexNoise::Noise(seed); - m_blocks.Reset(BTYPE_AIR); +Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y) { + //std::ostringstream pos; // Vérifie l'existence d'un fichier .chunk avec sa position. + //pos << CHUNK_PATH << x << '_' << y << ".chunk"; + //std::ifstream input(pos.str(), std::fstream::binary); - for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Montagnes - for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { - float xnoiz, ynoiz; - xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 4096.; - ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 4096.; - double height = 0; - for (int x = 0; x < 39; ++x) { - height += simplex.eval(xnoiz, ynoiz); - height *= .79; - xnoiz *= 1.139; - ynoiz *= 1.139; - } - height = height * 2000. * simplex.eval((double)(ix + x * CHUNK_SIZE_X) / 512., (double)(iz + y * CHUNK_SIZE_Z) / 512.); - height /= (CHUNK_SIZE_Y / 1.9); - height += 15.; - for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy) - SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); - } - for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Collines - for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { - float xnoiz, ynoiz; - xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 512.; - ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 512.; - float height = simplex.eval(xnoiz, ynoiz) * 50.f;// +1.f; - for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy) { - if (GetBlock(ix, iy, iz) == BTYPE_AIR) - SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); - } - } - for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Lacs" - for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { - for (int iy = 0; iy < 13; ++iy) { - if (GetBlock(ix, iy, iz) == BTYPE_AIR) - SetBlock(ix, iy, iz, BTYPE_ICE, nullptr); - } - } - //for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Arbres" - // for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { - // float xnoiz, ynoiz; - // xnoiz = (double)(iz * CHUNK_SIZE_Y + x * CHUNK_SIZE_X) / 256.; - // ynoiz = (double)(ix * CHUNK_SIZE_Y + y * CHUNK_SIZE_Z) / 256.; - // bool tree = (int)(abs(simplex.eval(xnoiz, ynoiz)) * 17933.f) % CHUNK_SIZE_Y > 126 ? true : false; - // for (int iy = 0; iy < CHUNK_SIZE_Y - 10; ++iy) - // if (GetBlock(ix, iy, iz) == BTYPE_AIR) - // if (GetBlock(ix, iy - 1, iz) == BTYPE_GRASS) - // if (tree) { - // for (int i = 0; i < (int)(abs(simplex.eval(xnoiz, ynoiz) * 4)) % 42 + 1; ++i) - // SetBlock(ix, iy + i, iz, BTYPE_DIRT, nullptr); - // break; - // } - // } - /* } - else { - input.seekg(0, std::ios_base::end); - int size = input.tellg(); - input.seekg(0, std::ios_base::beg); + //if (input.fail()) { + OpenSimplexNoise::Noise simplex = OpenSimplexNoise::Noise(seed); - char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; - input.read(data, size); - input.close(); + m_blocks.Reset(BTYPE_AIR); + +#pragma region Montagnes et Grass des montagnes + for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) + for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { + float xnoiz, ynoiz; + xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 4796.; + ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 4796.; + double height = 0; + for (int x = 0; x < 39; ++x) { + height += simplex.eval(xnoiz, ynoiz); + height *= .79; + xnoiz *= 1.1305; + ynoiz *= 1.1305; + } + height = height * 2000. * simplex.eval((double)(ix + x * CHUNK_SIZE_X) / 512., (double)(iz + y * CHUNK_SIZE_Z) / 512.); + height /= (CHUNK_SIZE_Y / 1.9); + height += 15.; + for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy) { + if (iy < 20) + { + //std::cout << "" << ynoiz << std::endl; + SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); // Blocs de montagnes + } + else if (iy == 20 || iy == 21) { + // Utilisez la partie décimale de la valeur de ynoiz pour déterminer le type de bloc + double fractionalPart = ynoiz - static_cast(ynoiz); + + // Variation pour iy égal à 24 + if (iy == 20) { + if (fractionalPart < 0.3) { + SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); // Blocs de montagnes - Grass + } + else { + SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); // Blocs de montagnes - Metal + } + } + // Variation pour iy égal à 25 + else if (iy == 21) { + if (fractionalPart < 0.6) { + SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); // Blocs de montagnes - Grass + } + else { + SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); // Blocs de montagnes - Metal + } + } + } + else + { + SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); // Grass des montagnes + } + } + } +#pragma endregion + +#pragma region Lacs + + + + for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Lacs" + for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { + for (int iy = 0; iy < 13; ++iy) { + if (iy < 5 && GetBlock(ix, iy, iz) == BTYPE_AIR) { + SetBlock(ix, iy, iz, BTYPE_ICE, nullptr); // Partie inférieure du lac + } + else if (iy >= 5 && GetBlock(ix, iy, iz) == BTYPE_AIR) { + SetBlock(ix, iy, iz, BTYPE_ICE, nullptr); // Partie supérieure du lac (simulée avec de l'eau) + } + } + } +#pragma endregion + + +#pragma region Arbre + + for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) { + for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { + + + + // Vérifiez d'abord que le bloc n'est pas un lac + if (GetBlock(ix, 0, iz) != BTYPE_ICE) { + // Recalculez la hauteur en fonction de la topographie du terrain + float xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 5096.; + float ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 5096.; + double height = 0; + for (int i = 0; i < 39; ++i) { + height += simplex.eval(xnoiz, ynoiz); + height *= .79; + xnoiz *= 1.1305; + ynoiz *= 1.1305; + } + height = height * 2000. * simplex.eval((double)(ix + x * CHUNK_SIZE_X) / 512., (double)(iz + y * CHUNK_SIZE_Z) / 512.); + height /= (CHUNK_SIZE_Y / 1.9); + height += 15.; + + + double minInput = 3068.84; + double maxInput = 3074.7; + double minOutput = 0.0; + double maxOutput = 100.0; + double valeurRnd = ((ynoiz - minInput) * (maxOutput - minOutput)) / (maxInput - minInput); + + //std::cout << valeurRnd << std::endl; + + + // Ajout des arbres uniquement sur des blocs de type BTYPE_GRASS et avec une probabilité réduite + if (GetBlock(ix, 0, iz) != BTYPE_ICE && GetBlock(ix, (int)height, iz) == BTYPE_GRASS) { + // Vérifiez si vous souhaitez placer un arbre en fonction de votre logique de génération + if (valeurRnd < 20) { + // Calculez la position et la hauteur de l'arbre + + // Exemple : Place un arbre à la position calculée avec une hauteur donnée + // + //PlaceTree(ix, height, iz, 10, 5 <= 5); + + } + } + } + } + } + + + + + +#pragma endregion + + + + //else { + // input.seekg(0, std::ios_base::end); + // int size = input.tellg(); + // input.seekg(0, std::ios_base::beg); + + // char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; + // input.read(data, size); + // input.close(); + + // for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) + // for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) + // for (int iy = 0; iy < CHUNK_SIZE_Y; ++iy) + // m_blocks.Set(ix, iy, iz, data[ix + (iz * CHUNK_SIZE_X) + (iy * CHUNK_SIZE_Z * CHUNK_SIZE_X)]); + //}*/ + + + + + //for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // Collines +// for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { +// float xnoiz, ynoiz; +// xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 512.; +// ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 512.; +// float height = simplex.eval(xnoiz, ynoiz) * 50.f;// +1.f; +// for (int iy = 0; iy <= (int)height % CHUNK_SIZE_Y; ++iy) { +// if (iy < 10 && GetBlock(ix, iy, iz) == BTYPE_AIR) { +// SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); // Collines +// } +// else if (iy >= 10 && GetBlock(ix, iy, iz) == BTYPE_AIR) { +// SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); // Grass des collines +// } +// } +// } - for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) - for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) - for (int iy = 0; iy < CHUNK_SIZE_Y; ++iy) - m_blocks.Set(ix, iy, iz, data[ix + (iz * CHUNK_SIZE_X) + (iy * CHUNK_SIZE_Z * CHUNK_SIZE_X)]); - }*/ } +void Chunk::PlaceTree(int x, int y, int z, int height, int valeurRnd) +{ + // Place le tronc + for (int iy = 0; iy < height; ++iy) { + SetBlock(x, y + iy, z, BTYPE_LAST, nullptr); + } + + // Place les feuilles autour du tronc + int foliageHeight = height - 2; + for (int dy = 1; dy < foliageHeight; ++dy) { + for (int dx = -2; dx <= 2; ++dx) { + for (int dz = -2; dz <= 2; ++dz) { + if (valeurRnd < 50) { + SetBlock(x + dx, y + height + dy, z + dz, BTYPE_DIRT, nullptr); + } + } + } + } +} + + Chunk::~Chunk() { - /*if (m_isModified) { - char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; + /*if (m_isModified) { + char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; - for (int x = 0; x < CHUNK_SIZE_X; ++x) - for (int z = 0; z < CHUNK_SIZE_Z; ++z) - for (int y = 0; y < CHUNK_SIZE_Y; ++y) - data[x + (z * CHUNK_SIZE_X) + (y * CHUNK_SIZE_Z * CHUNK_SIZE_X)] = (char)GetBlock(x, y, z); + for (int x = 0; x < CHUNK_SIZE_X; ++x) + for (int z = 0; z < CHUNK_SIZE_Z; ++z) + for (int y = 0; y < CHUNK_SIZE_Y; ++y) + data[x + (z * CHUNK_SIZE_X) + (y * CHUNK_SIZE_Z * CHUNK_SIZE_X)] = (char)GetBlock(x, y, z); - std::ostringstream pos; - pos << CHUNK_PATH << m_posX << '_' << m_posY << ".chunk"; + std::ostringstream pos; + pos << CHUNK_PATH << m_posX << '_' << m_posY << ".chunk"; - std::ofstream output(pos.str(), std::fstream::binary); - output.write(data, sizeof(data)); - output.close(); - }*/ + std::ofstream output(pos.str(), std::fstream::binary); + output.write(data, sizeof(data)); + output.close(); + }*/ } void Chunk::RemoveBlock(int x, int y, int z, World* world) { - m_blocks.Set(x, y, z, BTYPE_AIR); - CheckNeighbors(x, y, world); - m_isDirty = true; + m_blocks.Set(x, y, z, BTYPE_AIR); + CheckNeighbors(x, y, world); + m_isDirty = true; } void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) { - m_blocks.Set(x, y, z, type); - if (world) CheckNeighbors(x, z, world); // Si nullptr, ne pas vérifier les chunks voisines. - m_isDirty = true; + m_blocks.Set(x, y, z, type); + if (world) CheckNeighbors(x, z, world); // Si nullptr, ne pas vérifier les chunks voisines. + m_isDirty = true; } BlockType Chunk::GetBlock(int x, int y, int z) { return m_blocks.Get(x, y, z); } void Chunk::CheckNeighbors(unsigned int x, unsigned int z, World* world) { - unsigned int cx, cy; + unsigned int cx, cy; - world->GetScope(cx, cy); + world->GetScope(cx, cy); - if (x == 0 && m_posX - cx >= 0 && - world->ChunkAt((m_posX - cx - 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)) - world->ChunkAt((m_posX - cx - 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)->MakeDirty(); - else if (x == CHUNK_SIZE_X - 1 && m_posX - cx < WORLD_SIZE_X && - world->ChunkAt((m_posX - cx + 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)) - world->ChunkAt((m_posX - cx + 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)->MakeDirty(); + if (x == 0 && m_posX - cx >= 0 && + world->ChunkAt((m_posX - cx - 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)) + world->ChunkAt((m_posX - cx - 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)->MakeDirty(); + else if (x == CHUNK_SIZE_X - 1 && m_posX - cx < WORLD_SIZE_X && + world->ChunkAt((m_posX - cx + 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)) + world->ChunkAt((m_posX - cx + 1) * CHUNK_SIZE_X, 1, (m_posY - cy) * CHUNK_SIZE_Z)->MakeDirty(); - if (z == 0 && m_posY - cy >= 0 && - world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy - 1) * CHUNK_SIZE_Z)) - world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy - 1) * CHUNK_SIZE_Z)->MakeDirty(); - else if (z == CHUNK_SIZE_X - 1 && m_posY - cy < WORLD_SIZE_Y && - world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy + 1) * CHUNK_SIZE_Z)) - world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy + 1) * CHUNK_SIZE_Z)->MakeDirty(); + if (z == 0 && m_posY - cy >= 0 && + world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy - 1) * CHUNK_SIZE_Z)) + world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy - 1) * CHUNK_SIZE_Z)->MakeDirty(); + else if (z == CHUNK_SIZE_X - 1 && m_posY - cy < WORLD_SIZE_Y && + world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy + 1) * CHUNK_SIZE_Z)) + world->ChunkAt((m_posX - cx) * CHUNK_SIZE_X, 1, (m_posY - cy + 1) * CHUNK_SIZE_Z)->MakeDirty(); } void Chunk::GetPosition(unsigned int& x, unsigned int& y) const { x = m_posX; y = m_posY; } + + bool Chunk::IsDirty() const { return m_isDirty; } void Chunk::MakeDirty() { m_isDirty = true; } diff --git a/SQCSim-common/chunk.h b/SQCSim-common/chunk.h index b053928..61570ad 100644 --- a/SQCSim-common/chunk.h +++ b/SQCSim-common/chunk.h @@ -27,7 +27,7 @@ class Chunk { BlockType GetBlock(int x, int y, int z); void CheckNeighbors(unsigned int x, unsigned int z, World* world); void GetPosition(unsigned int& x, unsigned int& y) const; - + void PlaceTree(int x, int y, int z, int height,int valeurRnd); bool IsDirty() const; void MakeDirty(); void MakeClean(); From e3b59e37ebc44a3a1f2e05be2ec77a58e3ef7848 Mon Sep 17 00:00:00 2001 From: Louis-CharlesG Date: Mon, 27 Nov 2023 16:47:10 -0500 Subject: [PATCH 2/6] Les arbres avec le seed. --- SQCSim-common/chunk.cpp | 109 ++++++++++++++++++++++------------------ SQCSim-common/chunk.h | 2 +- 2 files changed, 60 insertions(+), 51 deletions(-) diff --git a/SQCSim-common/chunk.cpp b/SQCSim-common/chunk.cpp index 7a5485e..63fe17e 100644 --- a/SQCSim-common/chunk.cpp +++ b/SQCSim-common/chunk.cpp @@ -33,51 +33,45 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y if (iy < 20) { //std::cout << "" << ynoiz << std::endl; - SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); // Blocs de montagnes + SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); } else if (iy == 20 || iy == 21) { - // Utilisez la partie décimale de la valeur de ynoiz pour déterminer le type de bloc double fractionalPart = ynoiz - static_cast(ynoiz); - // Variation pour iy égal à 24 if (iy == 20) { if (fractionalPart < 0.3) { - SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); // Blocs de montagnes - Grass + SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); } else { - SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); // Blocs de montagnes - Metal + SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); } } - // Variation pour iy égal à 25 else if (iy == 21) { if (fractionalPart < 0.6) { - SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); // Blocs de montagnes - Grass + SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); } else { - SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); // Blocs de montagnes - Metal + SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); } } } else { - SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); // Grass des montagnes + SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); } } } #pragma endregion #pragma region Lacs - - - - for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) // "Lacs" + for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { for (int iy = 0; iy < 13; ++iy) { if (iy < 5 && GetBlock(ix, iy, iz) == BTYPE_AIR) { - SetBlock(ix, iy, iz, BTYPE_ICE, nullptr); // Partie inférieure du lac + SetBlock(ix, iy, iz, BTYPE_ICE, nullptr); } else if (iy >= 5 && GetBlock(ix, iy, iz) == BTYPE_AIR) { - SetBlock(ix, iy, iz, BTYPE_ICE, nullptr); // Partie supérieure du lac (simulée avec de l'eau) + SetBlock(ix, iy, iz, BTYPE_ICE, nullptr); } } } @@ -86,16 +80,19 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y #pragma region Arbre + double valeurRnd = 0; + int treeheight = 10; + + int lastTreeX = -1; + int lastTreeZ = -1; + + int minDistanceBetweenTrees = 10; // Définir la distance minimale entre les arbres + for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) { for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { - - - - // Vérifiez d'abord que le bloc n'est pas un lac if (GetBlock(ix, 0, iz) != BTYPE_ICE) { - // Recalculez la hauteur en fonction de la topographie du terrain - float xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 5096.; - float ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 5096.; + float xnoiz = (double)(ix + x * CHUNK_SIZE_X) / 4796.; + float ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / 4796.; double height = 0; for (int i = 0; i < 39; ++i) { height += simplex.eval(xnoiz, ynoiz); @@ -107,40 +104,48 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y height /= (CHUNK_SIZE_Y / 1.9); height += 15.; + if (GetBlock(ix, (int)height, iz) == BTYPE_GRASS || (GetBlock(ix, (int)height, iz) == BTYPE_METAL)) { + valeurRnd = simplex.eval(xnoiz, ynoiz); - double minInput = 3068.84; - double maxInput = 3074.7; - double minOutput = 0.0; - double maxOutput = 100.0; - double valeurRnd = ((ynoiz - minInput) * (maxOutput - minOutput)) / (maxInput - minInput); + int distanceThreshold = 20; - //std::cout << valeurRnd << std::endl; + // Vérifie si l'emplacement n'est pas à l'intérieur des lacs + bool isInsideLake = false; + for (int iy = 0; iy < 13; ++iy) { + if (GetBlock(ix, iy, iz) == BTYPE_ICE) { + isInsideLake = true; + break; + } + } + if (!isInsideLake && ((valeurRnd > -0.4 && valeurRnd < -0.38) || (valeurRnd > -0.35 && valeurRnd < -0.31) + || (valeurRnd > 0.3 && valeurRnd < 0.32) || (valeurRnd > 0.37 && valeurRnd < 0.39)) + ) { + if (lastTreeX == -1 || abs(ix - lastTreeX) > minDistanceBetweenTrees || abs(iz - lastTreeZ) > minDistanceBetweenTrees) { + if (valeurRnd < 0.1) + treeheight = 10; + else { + treeheight = valeurRnd * 20; + if (treeheight < 5) + treeheight = 5; + } - // Ajout des arbres uniquement sur des blocs de type BTYPE_GRASS et avec une probabilité réduite - if (GetBlock(ix, 0, iz) != BTYPE_ICE && GetBlock(ix, (int)height, iz) == BTYPE_GRASS) { - // Vérifiez si vous souhaitez placer un arbre en fonction de votre logique de génération - if (valeurRnd < 20) { - // Calculez la position et la hauteur de l'arbre - - // Exemple : Place un arbre à la position calculée avec une hauteur donnée - // - //PlaceTree(ix, height, iz, 10, 5 <= 5); + PlaceTree(ix, height, iz, treeheight); + lastTreeX = ix; + lastTreeZ = iz; + } } } } } } - - - - #pragma endregion + //else { // input.seekg(0, std::ios_base::end); // int size = input.tellg(); @@ -177,19 +182,18 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y } -void Chunk::PlaceTree(int x, int y, int z, int height, int valeurRnd) -{ - // Place le tronc - for (int iy = 0; iy < height; ++iy) { +void Chunk::PlaceTree(int x, int y, int z, int height) { + + for (int iy = 0; iy < height + 1; ++iy) { SetBlock(x, y + iy, z, BTYPE_LAST, nullptr); } - // Place les feuilles autour du tronc - int foliageHeight = height - 2; + int foliageHeight = height / 2; + for (int dy = 1; dy < foliageHeight; ++dy) { - for (int dx = -2; dx <= 2; ++dx) { - for (int dz = -2; dz <= 2; ++dz) { - if (valeurRnd < 50) { + for (int dx = -3; dx <= 3; ++dx) { + for (int dz = -3; dz <= 3; ++dz) { + if (dx * dx + dy * dy + dz * dz < 10) { SetBlock(x + dx, y + height + dy, z + dz, BTYPE_DIRT, nullptr); } } @@ -198,6 +202,9 @@ void Chunk::PlaceTree(int x, int y, int z, int height, int valeurRnd) } + + + Chunk::~Chunk() { /*if (m_isModified) { char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; @@ -254,6 +261,8 @@ void Chunk::GetPosition(unsigned int& x, unsigned int& y) const { x = m_posX; y + + bool Chunk::IsDirty() const { return m_isDirty; } void Chunk::MakeDirty() { m_isDirty = true; } diff --git a/SQCSim-common/chunk.h b/SQCSim-common/chunk.h index 61570ad..6b80e75 100644 --- a/SQCSim-common/chunk.h +++ b/SQCSim-common/chunk.h @@ -27,7 +27,7 @@ class Chunk { BlockType GetBlock(int x, int y, int z); void CheckNeighbors(unsigned int x, unsigned int z, World* world); void GetPosition(unsigned int& x, unsigned int& y) const; - void PlaceTree(int x, int y, int z, int height,int valeurRnd); + void PlaceTree(int x, int y, int z, int height); bool IsDirty() const; void MakeDirty(); void MakeClean(); From 841947bc7a9ab294611ee39acebbd32b20bcf211 Mon Sep 17 00:00:00 2001 From: Louis-CharlesG Date: Mon, 4 Dec 2023 15:05:27 -0500 Subject: [PATCH 3/6] Changment pour lac/arbre --- SQCSim-common/chunk.cpp | 2 +- SQCSim2021/engine.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/SQCSim-common/chunk.cpp b/SQCSim-common/chunk.cpp index 63fe17e..7026166 100644 --- a/SQCSim-common/chunk.cpp +++ b/SQCSim-common/chunk.cpp @@ -86,7 +86,7 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y int lastTreeX = -1; int lastTreeZ = -1; - int minDistanceBetweenTrees = 10; // Définir la distance minimale entre les arbres + int minDistanceBetweenTrees = 10; for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) { for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index 76f379b..77bb3e7 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -394,8 +394,8 @@ void Engine::LoadResource() { LoadTexture(MenuQuitTexture, MENU_ITEM_PATH "BasicQuit.png"); LoadTexture(MenuOptionsTexture, MENU_ITEM_PATH "test.png"); LoadTexture(MenuStartTexture, MENU_ITEM_PATH "BasicPlay.png"); - TextureAtlas::TextureIndex texDirtIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal3.png"); - TextureAtlas::TextureIndex texIceIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal2.png"); + TextureAtlas::TextureIndex texDirtIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal2.png"); + TextureAtlas::TextureIndex texIceIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal3.png"); TextureAtlas::TextureIndex texGrassIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "grass.png"); TextureAtlas::TextureIndex texMetalIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "dirt.png"); From bb100fd5cb8fb0d19162957ccf0c7b68efe471ea Mon Sep 17 00:00:00 2001 From: Louis-CharlesG Date: Mon, 4 Dec 2023 16:12:02 -0500 Subject: [PATCH 4/6] ajustement texture. --- SQCSim-common/chunk.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/SQCSim-common/chunk.cpp b/SQCSim-common/chunk.cpp index 2e843da..fa361d6 100644 --- a/SQCSim-common/chunk.cpp +++ b/SQCSim-common/chunk.cpp @@ -10,7 +10,9 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y //if (input.fail()) { OpenSimplexNoise::Noise simplex = OpenSimplexNoise::Noise(seed); + int ratio = 0; + ratio = x * y % 7; m_blocks.Reset(BTYPE_AIR); #pragma region Montagnes et Grass des montagnes @@ -33,14 +35,14 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y if (iy < 20) { //std::cout << "" << ynoiz << std::endl; - SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); + SetBlock(ix, iy, iz, BTYPE_GREENGRASS, nullptr); } else if (iy == 20 || iy == 21) { double fractionalPart = ynoiz - static_cast(ynoiz); if (iy == 20) { if (fractionalPart < 0.3) { - SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); + SetBlock(ix, iy, iz, BTYPE_GREENGRASS, nullptr); } else { SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); @@ -48,7 +50,7 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y } else if (iy == 21) { if (fractionalPart < 0.6) { - SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); + SetBlock(ix, iy, iz, BTYPE_GREENGRASS, nullptr); } else { SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); @@ -136,7 +138,7 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y height /= (CHUNK_SIZE_Y / 1.9); height += 15.; - if (GetBlock(ix, (int)height, iz) == BTYPE_GRASS || (GetBlock(ix, (int)height, iz) == BTYPE_METAL)) { + if (GetBlock(ix, (int)height, iz) == BTYPE_GREENGRASS || (GetBlock(ix, (int)height, iz) == BTYPE_METAL)) { valeurRnd = simplex.eval(xnoiz, ynoiz); int distanceThreshold = 20; @@ -220,7 +222,7 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y void Chunk::PlaceTree(int x, int y, int z, int height) { for (int iy = 0; iy < height + 1; ++iy) { - SetBlock(x, y + iy, z, BTYPE_LAST, nullptr); + SetBlock(x, y + iy, z, BTYPE_GRASS, nullptr); } int foliageHeight = height / 2; From d444b4de30bc04dd1e688121ae8a6fda91c612e8 Mon Sep 17 00:00:00 2001 From: Louis-CharlesG Date: Mon, 4 Dec 2023 16:17:59 -0500 Subject: [PATCH 5/6] Re-Changement des textures. --- SQCSim-common/chunk.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/SQCSim-common/chunk.cpp b/SQCSim-common/chunk.cpp index fa361d6..9dc2482 100644 --- a/SQCSim-common/chunk.cpp +++ b/SQCSim-common/chunk.cpp @@ -35,14 +35,14 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y if (iy < 20) { //std::cout << "" << ynoiz << std::endl; - SetBlock(ix, iy, iz, BTYPE_GREENGRASS, nullptr); + SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); } else if (iy == 20 || iy == 21) { double fractionalPart = ynoiz - static_cast(ynoiz); if (iy == 20) { if (fractionalPart < 0.3) { - SetBlock(ix, iy, iz, BTYPE_GREENGRASS, nullptr); + SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); } else { SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); @@ -50,7 +50,7 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y } else if (iy == 21) { if (fractionalPart < 0.6) { - SetBlock(ix, iy, iz, BTYPE_GREENGRASS, nullptr); + SetBlock(ix, iy, iz, BTYPE_GRASS, nullptr); } else { SetBlock(ix, iy, iz, BTYPE_METAL, nullptr); @@ -138,7 +138,7 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y height /= (CHUNK_SIZE_Y / 1.9); height += 15.; - if (GetBlock(ix, (int)height, iz) == BTYPE_GREENGRASS || (GetBlock(ix, (int)height, iz) == BTYPE_METAL)) { + if (GetBlock(ix, (int)height, iz) == BTYPE_DIRT || (GetBlock(ix, (int)height, iz) == BTYPE_METAL)) { valeurRnd = simplex.eval(xnoiz, ynoiz); int distanceThreshold = 20; @@ -222,7 +222,7 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y void Chunk::PlaceTree(int x, int y, int z, int height) { for (int iy = 0; iy < height + 1; ++iy) { - SetBlock(x, y + iy, z, BTYPE_GRASS, nullptr); + SetBlock(x, y + iy, z, BTYPE_DIRT, nullptr); } int foliageHeight = height / 2; @@ -231,7 +231,7 @@ void Chunk::PlaceTree(int x, int y, int z, int height) { for (int dx = -3; dx <= 3; ++dx) { for (int dz = -3; dz <= 3; ++dz) { if (dx * dx + dy * dy + dz * dz < 10) { - SetBlock(x + dx, y + height + dy, z + dz, BTYPE_DIRT, nullptr); + SetBlock(x + dx, y + height + dy, z + dz, BTYPE_GREENGRASS, nullptr); } } } From 2c73a2ed00e3695ec9ab5c0587cf758bf6676d95 Mon Sep 17 00:00:00 2001 From: Louis-CharlesG Date: Mon, 4 Dec 2023 17:17:15 -0500 Subject: [PATCH 6/6] Correction des arbres. --- SQCSim-common/chunk.cpp | 69 +++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/SQCSim-common/chunk.cpp b/SQCSim-common/chunk.cpp index 9dc2482..0f30cb8 100644 --- a/SQCSim-common/chunk.cpp +++ b/SQCSim-common/chunk.cpp @@ -113,14 +113,13 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y #pragma region Arbre - double valeurRnd = 0; int treeheight = 10; - int lastTreeX = -1; int lastTreeZ = -1; - int minDistanceBetweenTrees = 10; + int minDistanceBetweenTrees = 10; // Définir la distance minimale entre les arbres + for (int ix = 0; ix < CHUNK_SIZE_X; ++ix) { for (int iz = 0; iz < CHUNK_SIZE_Z; ++iz) { @@ -137,13 +136,10 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y height = height * 2000. * simplex.eval((double)(ix + x * CHUNK_SIZE_X) / 512., (double)(iz + y * CHUNK_SIZE_Z) / 512.); height /= (CHUNK_SIZE_Y / 1.9); height += 15.; - - if (GetBlock(ix, (int)height, iz) == BTYPE_DIRT || (GetBlock(ix, (int)height, iz) == BTYPE_METAL)) { + if (GetBlock(ix, (int)height, iz) == BTYPE_GRASS || (GetBlock(ix, (int)height, iz) == BTYPE_METAL)) { valeurRnd = simplex.eval(xnoiz, ynoiz); - int distanceThreshold = 20; - - // V�rifie si l'emplacement n'est pas � l'int�rieur des lacs + // Vérifie si l'emplacement n'est pas à l'intérieur des lacs bool isInsideLake = false; for (int iy = 0; iy < 13; ++iy) { if (GetBlock(ix, iy, iz) == BTYPE_ICE) { @@ -151,7 +147,6 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y break; } } - if (!isInsideLake && ((valeurRnd > -0.4 && valeurRnd < -0.38) || (valeurRnd > -0.35 && valeurRnd < -0.31) || (valeurRnd > 0.3 && valeurRnd < 0.32) || (valeurRnd > 0.37 && valeurRnd < 0.39)) ) { @@ -163,9 +158,7 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y if (treeheight < 5) treeheight = 5; } - PlaceTree(ix, height, iz, treeheight); - lastTreeX = ix; lastTreeZ = iz; } @@ -174,7 +167,6 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y } } } - #pragma endregion @@ -183,6 +175,7 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y + //else { // input.seekg(0, std::ios_base::end); // int size = input.tellg(); @@ -220,18 +213,32 @@ Chunk::Chunk(unsigned int x, unsigned int y, int64_t seed) : m_posX(x), m_posY(y } void Chunk::PlaceTree(int x, int y, int z, int height) { - - for (int iy = 0; iy < height + 1; ++iy) { - SetBlock(x, y + iy, z, BTYPE_DIRT, nullptr); + // Vérifie si les coordonnées x, y, z sont dans les limites du chunk + if (x < 0 || x >= CHUNK_SIZE_X || y < 0 || y >= CHUNK_SIZE_Y || z < 0 || z >= CHUNK_SIZE_Z) { + // Coordonnées hors limites du chunk, sortie anticipée pour éviter tout accès non valide + return; } - int foliageHeight = height / 2; + // Place la tige de l'arbre + for (int iy = 0; iy < height + 1; ++iy) { + if (y + iy < CHUNK_SIZE_Y) { // Vérifie si la hauteur est à l'intérieur des limites du chunk + SetBlock(x, y + iy, z, BTYPE_DIRT, nullptr); + } + } - for (int dy = 1; dy < foliageHeight; ++dy) { - for (int dx = -3; dx <= 3; ++dx) { - for (int dz = -3; dz <= 3; ++dz) { - if (dx * dx + dy * dy + dz * dz < 10) { - SetBlock(x + dx, y + height + dy, z + dz, BTYPE_GREENGRASS, nullptr); + // Place les feuilles de l'arbre + int foliageHeight = height / 2; + for (int dy = 0; dy < foliageHeight; ++dy) { + for (int dx = -4; dx <= 4; ++dx) { + for (int dz = -4; dz <= 4; ++dz) { + // Vérifie que les coordonnées se trouvent à l'intérieur des limites du chunk + if (x + dx >= 0 && x + dx < CHUNK_SIZE_X && y + height + dy >= 0 && y + height + dy < CHUNK_SIZE_Y && + z + dz >= 0 && z + dz < CHUNK_SIZE_Z) { + // Vérifie si le bloc est à une distance acceptable du centre des feuilles pour les placer + double distanceSquared = dx * dx + dy * dy + dz * dz; + if (distanceSquared < 20) { + SetBlock(x + dx, y + height + dy, z + dz, BTYPE_GREENGRASS, nullptr); + } } } } @@ -311,28 +318,8 @@ void Chunk::MakeModified() { m_isModified = true; } void Chunk::Structure(int x, int y, int z,int height) { - - - for (int i = 0; i < height; i++) { SetBlock(x, i + y, z, BTYPE_GRASS, nullptr); } - - - - - - - - - - - - - - - - - }