[ChunkLightmap|TerrainGenerator] Optimized chunk sending.

This commit is contained in:
Quentin Bazin 2019-01-13 16:38:30 +01:00
parent 6f933ab34f
commit f5c9054e81
10 changed files with 40 additions and 50 deletions

2
.gitignore vendored
View File

@ -16,6 +16,8 @@ a.out
vgcore.* vgcore.*
.gdb_history .gdb_history
.valgrind_suppressions .valgrind_suppressions
gmon.out
gprof.txt
# CMake temporary files # CMake temporary files
CMakeFiles CMakeFiles

View File

@ -1,12 +1,3 @@
#------------------------------------------------------------------------------
# CMakeLists.txt
#------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8)
project(openminer)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Get source files # Get source files
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
@ -42,6 +33,8 @@ target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -DSOL_CHECK_ARGUMENTS
target_compile_features(${CMAKE_PROJECT_NAME} PRIVATE cxx_std_17) target_compile_features(${CMAKE_PROJECT_NAME} PRIVATE cxx_std_17)
# target_link_options(${CMAKE_PROJECT_NAME} PRIVATE -pg)
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Link options # Link options
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------

View File

@ -69,7 +69,7 @@ void ClientWorld::receiveChunkData(sf::Packet &packet) {
packet >> block >> light; packet >> block >> light;
chunk->setBlock(x, y, z, block & 0xffff); chunk->setBlockRaw(x, y, z, block & 0xffff);
// chunk->setData(x, y, z, block >> 16); // chunk->setData(x, y, z, block >> 16);
chunk->lightmap().setLightData(x, y, z, light); chunk->lightmap().setLightData(x, y, z, light);
} }

View File

@ -1,12 +1,3 @@
#------------------------------------------------------------------------------
# CMakeLists.txt
#------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8)
project(openminer)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Get source files # Get source files
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
@ -26,7 +17,7 @@ add_library(${CMAKE_PROJECT_NAME}_common STATIC ${SOURCE_FILES})
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Compiler flags # Compiler flags
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -O3 -ffast-math) # target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -O3 -ffast-math)
target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -g -Wall -Wextra -Wfatal-errors -Wno-variadic-macros) target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -g -Wall -Wextra -Wfatal-errors -Wno-variadic-macros)
target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -DDEBUG_ENABLED) target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -DDEBUG_ENABLED)
target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -DSOL_CHECK_ARGUMENTS target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -DSOL_CHECK_ARGUMENTS

View File

@ -49,6 +49,8 @@ class Chunk : public gk::NonCopyable {
void setBlock(int x, int y, int z, u16 type); void setBlock(int x, int y, int z, u16 type);
void setData(int x, int y, int z, u16 data); void setData(int x, int y, int z, u16 data);
void setBlockRaw(int x, int y, int z, u16 block);
// BlockData *getBlockData(int x, int y, int z); // BlockData *getBlockData(int x, int y, int z);
s32 x() const { return m_x; } s32 x() const { return m_x; }

View File

@ -50,15 +50,15 @@ class ChunkLightmap {
void updateTorchlight(); void updateTorchlight();
void updateSunlight(); void updateSunlight();
u8 getLightData(int x, int y, int z) const { return m_lightMap[x][y][z]; }
u8 getSunlight(int x, int y, int z) const; u8 getSunlight(int x, int y, int z) const;
u8 getTorchlight(int x, int y, int z) const; u8 getTorchlight(int x, int y, int z) const;
u8 getLightData(int x, int y, int z) const;
void setLightData(int x, int y, int z, u8 val); void setLightData(int x, int y, int z, u8 val);
void setSunlight(int x, int y, int z, u8 val);
private: private:
void setTorchlight(int x, int y, int z, u8 val); void setTorchlight(int x, int y, int z, u8 val);
void setSunlight(int x, int y, int z, u8 val);
void updateSurroundingChunks(int x, int y, int z); void updateSurroundingChunks(int x, int y, int z);

View File

@ -120,6 +120,19 @@ void Chunk::setData(int x, int y, int z, u16 data) {
m_hasChanged = true; m_hasChanged = true;
} }
void Chunk::setBlockRaw(int x, int y, int z, u16 type) {
if(x < 0) { if(m_surroundingChunks[0]) m_surroundingChunks[0]->setBlockRaw(x + Chunk::width, y, z, type); return; }
if(x >= Chunk::width) { if(m_surroundingChunks[1]) m_surroundingChunks[1]->setBlockRaw(x - Chunk::width, y, z, type); return; }
if(y < 0) { if(m_surroundingChunks[4]) m_surroundingChunks[4]->setBlockRaw(x, y + Chunk::height, z, type); return; }
if(y >= Chunk::height) { if(m_surroundingChunks[5]) m_surroundingChunks[5]->setBlockRaw(x, y - Chunk::height, z, type); return; }
if(z < 0) { if(m_surroundingChunks[2]) m_surroundingChunks[2]->setBlockRaw(x, y, z + Chunk::depth, type); return; }
if(z >= Chunk::depth) { if(m_surroundingChunks[3]) m_surroundingChunks[3]->setBlockRaw(x, y, z - Chunk::depth, type); return; }
m_data[x][y][z] = type;
m_hasChanged = true;
}
// FIXME // FIXME
// BlockData *Chunk::getBlockData(int x, int y, int z) { // BlockData *Chunk::getBlockData(int x, int y, int z) {
// if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getBlockData(x + CHUNK_WIDTH, y, z) : 0; // if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getBlockData(x + CHUNK_WIDTH, y, z) : 0;

View File

@ -173,10 +173,6 @@ u8 ChunkLightmap::getTorchlight(int x, int y, int z) const {
return m_lightMap[x][y][z] & 0xf; return m_lightMap[x][y][z] & 0xf;
} }
u8 ChunkLightmap::getLightData(int x, int y, int z) const {
return m_lightMap[x][y][z];
}
void ChunkLightmap::setLightData(int x, int y, int z, u8 val) { void ChunkLightmap::setLightData(int x, int y, int z, u8 val) {
m_lightMap[x][y][z] = val; m_lightMap[x][y][z] = val;

View File

@ -1,12 +1,3 @@
#------------------------------------------------------------------------------
# CMakeLists.txt
#------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8)
project(openminer)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Get source files # Get source files
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
@ -42,6 +33,8 @@ target_compile_options(${CMAKE_PROJECT_NAME}_server PRIVATE -DSOL_CHECK_ARGUMENT
target_compile_features(${CMAKE_PROJECT_NAME}_server PRIVATE cxx_std_17) target_compile_features(${CMAKE_PROJECT_NAME}_server PRIVATE cxx_std_17)
# target_link_options(${CMAKE_PROJECT_NAME} PRIVATE -pg)
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# Link options # Link options
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------

View File

@ -31,9 +31,9 @@ void TerrainGenerator::basicGeneration(ServerChunk &chunk) const {
for(u8 y = 0 ; y < CHUNK_HEIGHT ; y++) { for(u8 y = 0 ; y < CHUNK_HEIGHT ; y++) {
if(y + chunk.y() * CHUNK_HEIGHT < h) { if(y + chunk.y() * CHUNK_HEIGHT < h) {
chunk.setBlock(x, y, z, 1); chunk.setBlockRaw(x, y, z, 1);
} else { } else {
chunk.setBlock(x, y, z, 0); chunk.setBlockRaw(x, y, z, 0);
} }
} }
} }
@ -52,7 +52,7 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
for(int y = 0 ; y < CHUNK_HEIGHT ; y++) { for(int y = 0 ; y < CHUNK_HEIGHT ; y++) {
// Wood planks layer // Wood planks layer
// if (y == 0 && chunk.y() == 0) { // if (y == 0 && chunk.y() == 0) {
// chunk.setBlock(x, y, z, 16); // chunk.setBlockRaw(x, y, z, 16);
// continue; // continue;
// } // }
@ -60,7 +60,7 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
if(y + chunk.y() * CHUNK_HEIGHT >= h) { if(y + chunk.y() * CHUNK_HEIGHT >= h) {
// if we are not yet up to sea level, fill with water blocks // if we are not yet up to sea level, fill with water blocks
if(y + chunk.y() * CHUNK_HEIGHT < SEALEVEL) { if(y + chunk.y() * CHUNK_HEIGHT < SEALEVEL) {
chunk.setBlock(x, y, z, BlockType::Water); chunk.setBlockRaw(x, y, z, BlockType::Water);
continue; continue;
// Otherwise we are in the air // Otherwise we are in the air
} else { } else {
@ -69,7 +69,8 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
// Trunk // Trunk
h = (rand() & 0x3) + 3; h = (rand() & 0x3) + 3;
for(int i = 0 ; i < h ; i++) { for(int i = 0 ; i < h ; i++) {
chunk.setBlock(x, y + i, z, BlockType::Wood); chunk.setBlockRaw(x, y + i, z, BlockType::Wood);
chunk.lightmap().addSunlight(x, y + i, z, 15); chunk.lightmap().addSunlight(x, y + i, z, 15);
} }
@ -78,9 +79,8 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
for(int iy = -3 ; iy <= 3 ; iy++) { for(int iy = -3 ; iy <= 3 ; iy++) {
for(int iz = -3 ; iz <= 3 ; iz++) { for(int iz = -3 ; iz <= 3 ; iz++) {
if(ix * ix + iy * iy + iz * iz < 8 + (rand() & 1) && !chunk.getBlock(x + ix, y + h + iy, z + iz)) { if(ix * ix + iy * iy + iz * iz < 8 + (rand() & 1) && !chunk.getBlock(x + ix, y + h + iy, z + iz)) {
chunk.setBlock(x + ix, y + h + iy, z + iz, BlockType::Leaves); chunk.setBlockRaw(x + ix, y + h + iy, z + iz, BlockType::Leaves);
// if (iy == 2)
chunk.lightmap().addSunlight(x + ix, y + h + iy, z + iz, 15); chunk.lightmap().addSunlight(x + ix, y + h + iy, z + iz, 15);
} }
} }
@ -88,12 +88,10 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
} }
} }
else if(chunk.getBlock(x, y - 1, z) == BlockType::Grass && (rand() & 0xff) == 0) { else if(chunk.getBlock(x, y - 1, z) == BlockType::Grass && (rand() & 0xff) == 0) {
chunk.setBlock(x, y, z, BlockType::Flower); chunk.setBlockRaw(x, y, z, BlockType::Flower);
chunk.lightmap().addSunlight(x, y, z, 15);
} }
else { else {
chunk.lightmap().addSunlight(x, y - 1, z, 15);
// FIXME: Temporary fix for air blocks light level
chunk.lightmap().addSunlight(x, y, z, 15); chunk.lightmap().addSunlight(x, y, z, 15);
} }
break; break;
@ -105,22 +103,24 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
// Sand layer // Sand layer
if(n + r * 5 < 4) { if(n + r * 5 < 4) {
chunk.setBlock(x, y, z, BlockType::Sand); chunk.setBlockRaw(x, y, z, BlockType::Sand);
} }
// Dirt layer, but use grass blocks for the top // Dirt layer, but use grass blocks for the top
else if(n + r * 5 < 8) { else if(n + r * 5 < 8) {
chunk.setBlock(x, y, z, (h < SEALEVEL || y + chunk.y() * CHUNK_HEIGHT < h - 1) ? BlockType::Dirt : BlockType::Grass); chunk.setBlockRaw(x, y, z, (h < SEALEVEL || y + chunk.y() * CHUNK_HEIGHT < h - 1) ? BlockType::Dirt : BlockType::Grass);
} }
// Rock layer // Rock layer
else if(r < 1.25) { else if(r < 1.25) {
chunk.setBlock(x, y, z, BlockType::Stone); chunk.setBlockRaw(x, y, z, BlockType::Stone);
// Sometimes, ores! // Sometimes, ores!
} else { } else {
chunk.setBlock(x, y, z, BlockType::CoalOre); chunk.setBlockRaw(x, y, z, BlockType::CoalOre);
} }
} }
} }
} }
chunk.lightmap().updateLights();
} }
float TerrainGenerator::noise2d(float x, float y, int octaves, float persistence) { float TerrainGenerator::noise2d(float x, float y, int octaves, float persistence) {