Skip to content

Commit

Permalink
[ChunkLightmap|TerrainGenerator] Optimized chunk sending.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jan 20, 2019
1 parent 6f933ab commit f5c9054
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -16,6 +16,8 @@ a.out
vgcore.*
.gdb_history
.valgrind_suppressions
gmon.out
gprof.txt

# CMake temporary files
CMakeFiles
Expand Down
11 changes: 2 additions & 9 deletions client/CMakeLists.txt
@@ -1,12 +1,3 @@
#------------------------------------------------------------------------------
# CMakeLists.txt
#------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8)

project(openminer)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#------------------------------------------------------------------------------
# Get source files
#------------------------------------------------------------------------------
Expand Down Expand Up @@ -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_link_options(${CMAKE_PROJECT_NAME} PRIVATE -pg)

#------------------------------------------------------------------------------
# Link options
#------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion client/source/world/ClientWorld.cpp
Expand Up @@ -69,7 +69,7 @@ void ClientWorld::receiveChunkData(sf::Packet &packet) {

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->lightmap().setLightData(x, y, z, light);
}
Expand Down
11 changes: 1 addition & 10 deletions common/CMakeLists.txt
@@ -1,12 +1,3 @@
#------------------------------------------------------------------------------
# CMakeLists.txt
#------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8)

project(openminer)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#------------------------------------------------------------------------------
# Get source files
#------------------------------------------------------------------------------
Expand All @@ -26,7 +17,7 @@ add_library(${CMAKE_PROJECT_NAME}_common STATIC ${SOURCE_FILES})
#------------------------------------------------------------------------------
# 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 -DDEBUG_ENABLED)
target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -DSOL_CHECK_ARGUMENTS
Expand Down
2 changes: 2 additions & 0 deletions common/include/world/Chunk.hpp
Expand Up @@ -49,6 +49,8 @@ class Chunk : public gk::NonCopyable {
void setBlock(int x, int y, int z, u16 type);
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);

s32 x() const { return m_x; }
Expand Down
4 changes: 2 additions & 2 deletions common/include/world/ChunkLightmap.hpp
Expand Up @@ -50,15 +50,15 @@ class ChunkLightmap {
void updateTorchlight();
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 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 setSunlight(int x, int y, int z, u8 val);

private:
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);

Expand Down
13 changes: 13 additions & 0 deletions common/source/world/Chunk.cpp
Expand Up @@ -120,6 +120,19 @@ void Chunk::setData(int x, int y, int z, u16 data) {
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
// 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;
Expand Down
4 changes: 0 additions & 4 deletions common/source/world/ChunkLightmap.cpp
Expand Up @@ -173,10 +173,6 @@ u8 ChunkLightmap::getTorchlight(int x, int y, int z) const {
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) {
m_lightMap[x][y][z] = val;

Expand Down
11 changes: 2 additions & 9 deletions server/CMakeLists.txt
@@ -1,12 +1,3 @@
#------------------------------------------------------------------------------
# CMakeLists.txt
#------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8)

project(openminer)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#------------------------------------------------------------------------------
# Get source files
#------------------------------------------------------------------------------
Expand Down Expand Up @@ -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_link_options(${CMAKE_PROJECT_NAME} PRIVATE -pg)

#------------------------------------------------------------------------------
# Link options
#------------------------------------------------------------------------------
Expand Down
30 changes: 15 additions & 15 deletions server/source/world/TerrainGenerator.cpp
Expand Up @@ -31,9 +31,9 @@ void TerrainGenerator::basicGeneration(ServerChunk &chunk) const {

for(u8 y = 0 ; y < CHUNK_HEIGHT ; y++) {
if(y + chunk.y() * CHUNK_HEIGHT < h) {
chunk.setBlock(x, y, z, 1);
chunk.setBlockRaw(x, y, z, 1);
} else {
chunk.setBlock(x, y, z, 0);
chunk.setBlockRaw(x, y, z, 0);
}
}
}
Expand All @@ -52,15 +52,15 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
for(int y = 0 ; y < CHUNK_HEIGHT ; y++) {
// Wood planks layer
// if (y == 0 && chunk.y() == 0) {
// chunk.setBlock(x, y, z, 16);
// chunk.setBlockRaw(x, y, z, 16);
// continue;
// }

// Are we above "ground" level?
if(y + chunk.y() * CHUNK_HEIGHT >= h) {
// if we are not yet up to sea level, fill with water blocks
if(y + chunk.y() * CHUNK_HEIGHT < SEALEVEL) {
chunk.setBlock(x, y, z, BlockType::Water);
chunk.setBlockRaw(x, y, z, BlockType::Water);
continue;
// Otherwise we are in the air
} else {
Expand All @@ -69,7 +69,8 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
// Trunk
h = (rand() & 0x3) + 3;
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);
}

Expand All @@ -78,22 +79,19 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
for(int iy = -3 ; iy <= 3 ; iy++) {
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)) {
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);
}
}
}
}
}
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 {
chunk.lightmap().addSunlight(x, y - 1, z, 15);

// FIXME: Temporary fix for air blocks light level
chunk.lightmap().addSunlight(x, y, z, 15);
}
break;
Expand All @@ -105,22 +103,24 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {

// Sand layer
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
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
else if(r < 1.25) {
chunk.setBlock(x, y, z, BlockType::Stone);
chunk.setBlockRaw(x, y, z, BlockType::Stone);
// Sometimes, ores!
} 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) {
Expand Down

0 comments on commit f5c9054

Please sign in to comment.