refactor(server): replace Chunk with ServerChunk

This commit is contained in:
2026-06-25 17:13:50 +08:00
parent b782b40133
commit 2c6dccfb53
9 changed files with 35 additions and 35 deletions

View File

@@ -4,13 +4,13 @@
namespace Cubed { namespace Cubed {
class Chunk; class ServerChunk;
struct TreeStructNode { struct TreeStructNode {
glm::ivec3 offset{0, 0, 0}; glm::ivec3 offset{0, 0, 0};
unsigned id = 0; unsigned id = 0;
}; };
bool build_tree(Chunk& chunk, const glm::ivec3& pos); bool build_tree(ServerChunk& chunk, const glm::ivec3& pos);
} // namespace Cubed } // namespace Cubed

View File

@@ -1,39 +1,39 @@
#include "Cubed/gameplay/builders/biome_builder.hpp" #include "Cubed/gameplay/builders/biome_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp" #include "Cubed/gameplay/chunk_generator.hpp"
#include "Cubed/gameplay/server_chunk.hpp"
namespace Cubed { namespace Cubed {
void BiomeBuilder::build_bottom() { void BiomeBuilder::build_bottom() {
ChunkGenerator& chunk_generator = get_chunk_generator(); ChunkGenerator& chunk_generator = get_chunk_generator();
Chunk& chunk = chunk_generator.chunk(); ServerChunk& chunk = chunk_generator.chunk();
auto& m_blocks = chunk.blocks(); auto& m_blocks = chunk.blocks();
for (int x = 0; x < CHUNK_SIZE; x++) { for (int x = 0; x < CHUNK_SIZE; x++) {
for (int y = 0; y < 5; y++) { for (int y = 0; y < 5; y++) {
for (int z = 0; z < CHUNK_SIZE; z++) { for (int z = 0; z < CHUNK_SIZE; z++) {
m_blocks[Chunk::index(x, y, z)] = 3; m_blocks[ServerChunk::index(x, y, z)] = 3;
} }
} }
} }
} }
void BiomeBuilder::place_grass() { void BiomeBuilder::place_grass() {
ChunkGenerator& chunk_generator = get_chunk_generator(); ChunkGenerator& chunk_generator = get_chunk_generator();
Chunk& chunk = chunk_generator.chunk(); ServerChunk& chunk = chunk_generator.chunk();
auto& blocks = chunk.blocks(); auto& blocks = chunk.blocks();
const auto& heightmap = chunk.get_heightmap(); const auto& heightmap = chunk.get_heightmap();
auto& random = chunk_generator.random(); auto& random = chunk_generator.random();
for (int x = 0; x < SIZE_X; ++x) { for (int x = 0; x < SIZE_X; ++x) {
for (int z = 0; z < SIZE_Z; ++z) { for (int z = 0; z < SIZE_Z; ++z) {
int y = heightmap[x][z]; int y = heightmap[x][z];
BlockType top_id = blocks[Chunk::index(x, y, z)]; BlockType top_id = blocks[ServerChunk::index(x, y, z)];
if (top_id != 1) { if (top_id != 1) {
continue; continue;
} }
if (blocks[Chunk::index(x, y + 1, z)] != 0) { if (blocks[ServerChunk::index(x, y + 1, z)] != 0) {
continue; continue;
} }
if (random.random_bool(0.2)) { if (random.random_bool(0.2)) {
if (y + 1 < SIZE_Y) { if (y + 1 < SIZE_Y) {
blocks[Chunk::index(x, y + 1, z)] = 9; blocks[ServerChunk::index(x, y + 1, z)] = 9;
} }
} }
} }
@@ -42,7 +42,7 @@ void BiomeBuilder::place_grass() {
void BiomeBuilder::ocean_water_build() { void BiomeBuilder::ocean_water_build() {
ChunkGenerator& chunk_generator = get_chunk_generator(); ChunkGenerator& chunk_generator = get_chunk_generator();
Chunk& chunk = chunk_generator.chunk(); ServerChunk& chunk = chunk_generator.chunk();
auto& blocks = chunk.blocks(); auto& blocks = chunk.blocks();
const auto& heightmap = chunk.get_heightmap(); const auto& heightmap = chunk.get_heightmap();
@@ -51,7 +51,7 @@ void BiomeBuilder::ocean_water_build() {
int height = heightmap[x][z]; int height = heightmap[x][z];
if (height <= SEA_LEVEL) { if (height <= SEA_LEVEL) {
for (int y = height; y <= SEA_LEVEL; y++) { for (int y = height; y <= SEA_LEVEL; y++) {
blocks[Chunk::index(x, y, z)] = 7; blocks[ServerChunk::index(x, y, z)] = 7;
} }
} }
} }

View File

@@ -1,7 +1,7 @@
#include "Cubed/gameplay/builders/desert_builder.hpp" #include "Cubed/gameplay/builders/desert_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp" #include "Cubed/gameplay/chunk_generator.hpp"
#include "Cubed/gameplay/server_chunk.hpp"
namespace Cubed { namespace Cubed {
DesertBuilder::DesertBuilder(ChunkGenerator& chunk_generator) DesertBuilder::DesertBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {} : m_chunk_generator(chunk_generator) {}
@@ -19,11 +19,11 @@ void DesertBuilder::build_blocks() {
for (int z = 0; z < CHUNK_SIZE; z++) { for (int z = 0; z < CHUNK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]); int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y < height - 5; y++) { for (int y = 5; y < height - 5; y++) {
m_blocks[Chunk::index(x, y, z)] = 3; m_blocks[ServerChunk::index(x, y, z)] = 3;
} }
for (int y = height - 5; y <= height; y++) { for (int y = height - 5; y <= height; y++) {
m_blocks[Chunk::index(x, y, z)] = 4; m_blocks[ServerChunk::index(x, y, z)] = 4;
} }
} }
} }

View File

@@ -1,7 +1,7 @@
#include "Cubed/gameplay/builders/forest_builder.hpp" #include "Cubed/gameplay/builders/forest_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp" #include "Cubed/gameplay/chunk_generator.hpp"
#include "Cubed/gameplay/server_chunk.hpp"
#include "Cubed/gameplay/tree.hpp" #include "Cubed/gameplay/tree.hpp"
#include <algorithm> #include <algorithm>
@@ -24,12 +24,12 @@ void ForestBuilder::build_blocks() {
for (int z = 0; z < CHUNK_SIZE; z++) { for (int z = 0; z < CHUNK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]); int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y < height - 5; y++) { for (int y = 5; y < height - 5; y++) {
m_blocks[Chunk::index(x, y, z)] = 3; m_blocks[ServerChunk::index(x, y, z)] = 3;
} }
for (int y = height - 5; y < height; y++) { for (int y = height - 5; y < height; y++) {
m_blocks[Chunk::index(x, y, z)] = 2; m_blocks[ServerChunk::index(x, y, z)] = 2;
} }
m_blocks[Chunk::index(x, height, z)] = 1; m_blocks[ServerChunk::index(x, height, z)] = 1;
} }
} }
} }

View File

@@ -1,7 +1,7 @@
#include "Cubed/gameplay/builders/mountain_builder.hpp" #include "Cubed/gameplay/builders/mountain_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp" #include "Cubed/gameplay/chunk_generator.hpp"
#include "Cubed/gameplay/server_chunk.hpp"
namespace Cubed { namespace Cubed {
MountainBuilder::MountainBuilder(ChunkGenerator& chunk_generator) MountainBuilder::MountainBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {} : m_chunk_generator(chunk_generator) {}
@@ -19,7 +19,7 @@ void MountainBuilder::build_blocks() {
for (int z = 0; z < CHUNK_SIZE; z++) { for (int z = 0; z < CHUNK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]); int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y <= height; y++) { for (int y = 5; y <= height; y++) {
m_blocks[Chunk::index(x, y, z)] = 3; m_blocks[ServerChunk::index(x, y, z)] = 3;
} }
} }
} }

View File

@@ -1,7 +1,7 @@
#include "Cubed/gameplay/builders/ocean_builder.hpp" #include "Cubed/gameplay/builders/ocean_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp" #include "Cubed/gameplay/chunk_generator.hpp"
#include "Cubed/gameplay/server_chunk.hpp"
namespace Cubed { namespace Cubed {
OceanBuilder::OceanBuilder(ChunkGenerator& chunk_generator) OceanBuilder::OceanBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {} : m_chunk_generator(chunk_generator) {}
@@ -19,7 +19,7 @@ void OceanBuilder::build_blocks() {
for (int z = 0; z < CHUNK_SIZE; z++) { for (int z = 0; z < CHUNK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]); int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y <= height; y++) { for (int y = 5; y <= height; y++) {
m_blocks[Chunk::index(x, y, z)] = 3; m_blocks[ServerChunk::index(x, y, z)] = 3;
} }
} }
} }

View File

@@ -1,7 +1,7 @@
#include "Cubed/gameplay/builders/plain_builder.hpp" #include "Cubed/gameplay/builders/plain_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp" #include "Cubed/gameplay/chunk_generator.hpp"
#include "Cubed/gameplay/server_chunk.hpp"
namespace Cubed { namespace Cubed {
PlainBuilder::PlainBuilder(ChunkGenerator& chunk_generator) PlainBuilder::PlainBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {} : m_chunk_generator(chunk_generator) {}
@@ -19,12 +19,12 @@ void PlainBuilder::build_blocks() {
for (int z = 0; z < CHUNK_SIZE; z++) { for (int z = 0; z < CHUNK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]); int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y < height - 5; y++) { for (int y = 5; y < height - 5; y++) {
m_blocks[Chunk::index(x, y, z)] = 3; m_blocks[ServerChunk::index(x, y, z)] = 3;
} }
for (int y = height - 5; y < height; y++) { for (int y = height - 5; y < height; y++) {
m_blocks[Chunk::index(x, y, z)] = 2; m_blocks[ServerChunk::index(x, y, z)] = 2;
} }
m_blocks[Chunk::index(x, height, z)] = 1; m_blocks[ServerChunk::index(x, height, z)] = 1;
} }
} }
} }

View File

@@ -1,7 +1,7 @@
#include "Cubed/gameplay/builders/snowy_plain_builder.hpp" #include "Cubed/gameplay/builders/snowy_plain_builder.hpp"
#include "Cubed/gameplay/chunk.hpp"
#include "Cubed/gameplay/chunk_generator.hpp" #include "Cubed/gameplay/chunk_generator.hpp"
#include "Cubed/gameplay/server_chunk.hpp"
namespace Cubed { namespace Cubed {
SnowyPlainBuilder::SnowyPlainBuilder(ChunkGenerator& chunk_generator) SnowyPlainBuilder::SnowyPlainBuilder(ChunkGenerator& chunk_generator)
: m_chunk_generator(chunk_generator) {} : m_chunk_generator(chunk_generator) {}
@@ -19,12 +19,12 @@ void SnowyPlainBuilder::build_blocks() {
for (int z = 0; z < CHUNK_SIZE; z++) { for (int z = 0; z < CHUNK_SIZE; z++) {
int height = static_cast<int>(m_heightmap[x][z]); int height = static_cast<int>(m_heightmap[x][z]);
for (int y = 5; y < height - 5; y++) { for (int y = 5; y < height - 5; y++) {
m_blocks[Chunk::index(x, y, z)] = 3; m_blocks[ServerChunk::index(x, y, z)] = 3;
} }
for (int y = height - 5; y < height; y++) { for (int y = height - 5; y < height; y++) {
m_blocks[Chunk::index(x, y, z)] = 2; m_blocks[ServerChunk::index(x, y, z)] = 2;
} }
m_blocks[Chunk::index(x, height, z)] = 8; m_blocks[ServerChunk::index(x, height, z)] = 8;
} }
} }
} }

View File

@@ -1,6 +1,6 @@
#include "Cubed/gameplay/tree.hpp" #include "Cubed/gameplay/tree.hpp"
#include "Cubed/gameplay/chunk.hpp" #include "Cubed/gameplay/server_chunk.hpp"
#include <array> #include <array>
@@ -27,10 +27,10 @@ static constexpr std::array<TreeStructNode, 62> TREE{{
{{-1, 3, -2}, 6}, {{-2, 3, -1}, 6}, {{-1, 3, -2}, 6}, {{-2, 3, -1}, 6},
}}; }};
bool build_tree(Chunk& chunk, const glm::ivec3& pos) { bool build_tree(ServerChunk& chunk, const glm::ivec3& pos) {
auto& block = chunk.get_chunk_blocks(); auto& block = chunk.get_chunk_blocks();
if (block[Chunk::index(pos)] != 1) { if (block[ServerChunk::index(pos)] != 1) {
return false; return false;
} }
for (const auto& d : TREE) { for (const auto& d : TREE) {
@@ -42,13 +42,13 @@ bool build_tree(Chunk& chunk, const glm::ivec3& pos) {
z >= CHUNK_SIZE) { z >= CHUNK_SIZE) {
return false; return false;
} }
if (block[Chunk::index(tree_node)] != 0) { if (block[ServerChunk::index(tree_node)] != 0) {
return false; return false;
} }
} }
for (const auto& d : TREE) { for (const auto& d : TREE) {
auto tree_node = pos + d.offset; auto tree_node = pos + d.offset;
chunk.set_chunk_block(Chunk::index(tree_node), d.id); chunk.set_chunk_block(ServerChunk::index(tree_node), d.id);
} }
return true; return true;
} }