#include "chunk.h" #include "world.h" Chunk::Chunk(int x, int y) : 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()) { Perlin perlin = Perlin(8, 45.f, 7.f, PERLIN_SEED); m_blocks.Reset(BTYPE_AIR); 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) / (double)INT16_MAX; ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / (double)INT16_MAX; float height = (perlin.Get(xnoiz, ynoiz)) * 20.f + 5.f; 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) / (double)INT16_MAX; ynoiz = (double)(iz + y * CHUNK_SIZE_Z) / (double)INT16_MAX; float height = perlin.Get(xnoiz, ynoiz) * 5.f + 24.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) / (double)INT16_MAX; ynoiz = (double)(ix * CHUNK_SIZE_Y + y * CHUNK_SIZE_Z) / (double)INT16_MAX; bool tree = (int)(abs(perlin.Get(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(perlin.Get(xnoiz, ynoiz))) % 3 + 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); 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)]); } } Chunk::~Chunk() { 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); 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(); } } 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; } 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; } BlockType Chunk::GetBlock(int x, int y, int z) { return m_blocks.Get(x, y, z); } void Chunk::CheckNeighbors(int x, int z, World* world) { int 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 (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(int& x, int& y) const { x = m_posX; y = m_posY; } void Chunk::FlushMeshToVBO() { m_vertexBuffer.SetMeshData(m_vd, m_vcount); m_vcount = 0; delete[] m_vd; } void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST], World* world) { float u, v, s; // Update mesh if (m_isDirty) { int maxVertexCount = (CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z) * (6 * 4); m_vd = new VertexBuffer::VertexData[maxVertexCount]; m_vcount = 0; 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) { if (m_vcount > USHRT_MAX) break; BlockType bt = GetBlock(x, y, z); if (bt != BTYPE_AIR) { blockinfo[bt]->GetTexture(u, v, s); AddBlockToMesh(m_vd, m_vcount, bt, x, y, z, u, v, s, world); } } } } if (m_vcount > USHRT_MAX) { m_vcount = USHRT_MAX; std::cout << "[ Chunk :: Update ] Chunk data truncaned , too much vertices to have a 16 bit index " << std::endl; } } m_isDirty = false; } void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt, int x, int y, int z, float u, float v, float s, World* world) { int cex, cey; world->GetScope(cex, cey); int cx = x + (m_posX - cex) * CHUNK_SIZE_X, cy = z + (m_posY - cey) * CHUNK_SIZE_Z; if (y == CHUNK_SIZE_Y - 1 || GetBlock(x, y + 1, z) == BTYPE_AIR) { // y vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, .8f, .8f, .8f, u, v); vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, .8f, .8f, .8f, u, v + s); vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, .8f, .8f, .8f, u + s, v + s); vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, .8f, .8f, .8f, u + s, v); } if (y == 0 || GetBlock(x, y - 1, z) == BTYPE_AIR) { // -y vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, .2f, .2f, .2f, u, v); vd[count++] = VertexBuffer::VertexData(x, y, z, .2f, .2f, .2f, u, v + s); vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z, .2f, .2f, .2f, u + s, v + s); vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .2f, .2f, .2f, u + s, v); } if (world->BlockAt(cx + 1, y, cy) == BTYPE_AIR) { // x vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z, .9f, .9f, .9f, u, v); vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, .9f, .9f, .9f, u, v + s); vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, .9f, .9f, .9f, u + s, v + s); vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .9f, .9f, .9f, u + s, v); } if (world->BlockAt(cx - 1, y, cy) == BTYPE_AIR) { // -x vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, .5f, .5f, .5f, u, v + s); vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, .5f, .5f, .5f, u + s, v + s); vd[count++] = VertexBuffer::VertexData(x, y, z, .5f, .5f, .5f, u + s, v); vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, .5f, .5f, .5f, u, v); } if (world->BlockAt(cx, y, cy + 1) == BTYPE_AIR) { // z vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, .4f, .4f, .4f, u, v); vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .4f, .4f, .4f, u + s, v); vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, .4f, .4f, .4f, u + s, v + s); vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, .4f, .4f, .4f, u, v + s); } if (world->BlockAt(cx, y, cy - 1) == BTYPE_AIR) { // -z vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, 1.f, 1.f, 1.f, u, v + s); vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, 1.f, 1.f, 1.f, u + s, v + s); vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z , 1.f, 1.f, 1.f, u + s, v); vd[count++] = VertexBuffer::VertexData(x, y, z , 1.f, 1.f, 1.f, u, v); } } void Chunk::Render() const { m_vertexBuffer.Render(); } bool Chunk::IsDirty() const { return m_isDirty; } void Chunk::MakeDirty() { m_isDirty = true; } void Chunk::MakeModified() { m_isModified = true; }