Skip to content

Commit

Permalink
todo: fix generator border
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Jun 4, 2024
1 parent 20bb73b commit 2eef921
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/plotcraft/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ using string = std::string;
namespace plotcraft::config {

struct _Config {
int version = 1;
int version = 2;

struct _Generator {
int plotWidth = 16; // 地皮大小
int plotSize = 16; // 地皮大小
int roadWidth = 3; // 道路宽度
int generatorY = -61; // 生成层

Expand Down
38 changes: 22 additions & 16 deletions src/plotcraft/core/PlotGenerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,41 +70,47 @@ int positiveMod(int value, int modulus) {


void PlotGenerator::loadChunk(LevelChunk& levelchunk, bool /* forceImmediateReplacementDataLoad */) {
auto& gen = config::cfg.generator;
// 方块配置
auto& gen = config::cfg.generator;
const Block& roadBlock = *Block::tryGetFromRegistry(gen.roadBlock, 0);
const Block& fillBlock = *Block::tryGetFromRegistry(gen.fillBlock, 0);
const Block& borderBlock = *Block::tryGetFromRegistry(gen.borderBlock, 0);

// 生成/计算
auto& chunkPos = levelchunk.getPosition();
auto blockSource = &getDimension().getBlockSourceFromMainChunkSource();
levelchunk.setBlockVolume(mPrototype, 0);

// 计算当前区块的全局坐标
int startX = chunkPos.x * 16;
int startZ = chunkPos.z * 16;

// 遍历区块内的每个方块位置
int totalSize = gen.plotSize + gen.roadWidth + 2; // 地皮大小 + 道路宽度 + 边框(1边1格)

for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
// 计算全局坐标
int globalX = startX + x;
int globalZ = startZ + z;

// 计算在地盘网格中的位置
int gridX = positiveMod(globalX, gen.plotWidth + gen.roadWidth); // 地皮 + 道路宽度
int gridZ = positiveMod(globalZ, gen.plotWidth + gen.roadWidth);
int gridX = positiveMod(globalX, totalSize);
int gridZ = positiveMod(globalZ, totalSize);

const Block* blockToPlace = nullptr;

// 判断是否为道路或边框
if (gridX >= gen.plotWidth || gridZ >= gen.plotWidth) {
// 道路
// 判断边框
if (gridX == 0 || gridX == totalSize - 1 || gridZ == 0 || gridZ == totalSize - 1) {
blockToPlace = &borderBlock;
}
// 判断道路
else if (gridX >= gen.plotSize + 1 && gridX < gen.plotSize + 1 + gen.roadWidth || gridZ >= gen.plotSize + 1 && gridZ < gen.plotSize + 1 + gen.roadWidth) {
blockToPlace = &roadBlock;
}
// 地皮内部
else if (gridX > 0 && gridX < gen.plotSize + 1 && gridZ > 0 && gridZ < gen.plotSize + 1) {
blockToPlace = &fillBlock;
}

if (blockToPlace) {
BlockPos blockPos(globalX, gen.generatorY, globalZ);
levelchunk
.setBlock(ChunkBlockPos{BlockPos(x, gen.generatorY, z), -64}, roadBlock, blockSource, nullptr);
} else {
// 地盘内部
levelchunk
.setBlock(ChunkBlockPos{BlockPos(x, gen.generatorY, z), -64}, fillBlock, blockSource, nullptr);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/plotcraft/core/PlotPos.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class PlotPos {
// 构造函数
PlotPos(int x, int z) : x(x), z(z) {
auto& gen = config::cfg.generator;
int plotSize = gen.plotWidth + gen.roadWidth;
int plotSize = gen.plotSize + gen.roadWidth;
minPos = Vec3{x * plotSize + gen.roadWidth, -64, z * plotSize + gen.roadWidth};
maxPos = Vec3{minPos.x + gen.plotWidth, 320, minPos.z + gen.plotWidth};
maxPos = Vec3{minPos.x + gen.plotSize, 320, minPos.z + gen.plotSize};
}

PlotPos(const Vec3& vec3)
: PlotPos(
vec3.x / (config::cfg.generator.plotWidth + config::cfg.generator.roadWidth),
vec3.z / (config::cfg.generator.plotWidth + config::cfg.generator.roadWidth)
vec3.x / (config::cfg.generator.plotSize + config::cfg.generator.roadWidth),
vec3.z / (config::cfg.generator.plotSize + config::cfg.generator.roadWidth)
) {}

PlotPos(const ChunkPos& chunkPos) : PlotPos(chunkPos.x * 16, chunkPos.z * 16) {}
Expand Down

0 comments on commit 2eef921

Please sign in to comment.