Skip to content

Commit

Permalink
fix: fixed PlotPos
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Jun 5, 2024
1 parent e6e66ba commit 5e6a421
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 57 deletions.
71 changes: 71 additions & 0 deletions src/plotcraft/core/PlotPos.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "PlotPos.h"
#include "fmt/core.h"
#include "fmt/format.h"
#include "plotcraft/config/Config.h"
#include <cmath>


namespace plotcraft::core {

/*
基准点:Vec3{0,0,0}
地皮(0,0).minPos = Vec3{0,-64,0}
地皮(0,0).maxPos = Vec3{0 + config::cfg.generator.plotWidth -1 ,320,0 + config::cfg.generator.plotWidth -1}
横向x轴,纵向z轴
-1,1 0,1 1,1
-1,0 0,0 1,0
-1,-1 0,-1 1,-1
*/

PlotPos::PlotPos(int x, int z) : x(x), z(z) {
auto& cfg = config::cfg.generator;
int totalSize = cfg.plotWidth + cfg.roadWidth;
minPos = Vec3{x * totalSize, cfg.generatorY, z * totalSize};
maxPos = Vec3{minPos.x + cfg.plotWidth - 1, 320, minPos.z + cfg.plotWidth - 1};
}

PlotPos::PlotPos(const Vec3& vec3) {
auto& cfg = config::cfg.generator;
int totalSize = cfg.plotWidth + cfg.roadWidth;
int gridX = std::floor(vec3.x / totalSize);
int gridZ = std::floor(vec3.z / totalSize);
int localX = static_cast<int>(std::floor(vec3.x)) % totalSize;
int localZ = static_cast<int>(std::floor(vec3.z)) % totalSize;

if (localX < 0) localX += totalSize;
if (localZ < 0) localZ += totalSize;

if (localX >= cfg.plotWidth || localZ >= cfg.plotWidth) {
// Point is on the road
minPos = Vec3{0, 0, 0};
maxPos = Vec3{0, 0, 0};
x = 0;
z = 0;
} else {
x = gridX;
z = gridZ;
minPos = Vec3{x * totalSize, cfg.generatorY, z * totalSize};
maxPos = Vec3{minPos.x + cfg.plotWidth - 1, 320, minPos.z + cfg.plotWidth - 1};
}
}


Vec3 PlotPos::getMin() { return minPos; }

Vec3 PlotPos::getMax() { return maxPos; }

string PlotPos::toString() { return fmt::format("({0},{1})", x, z); }

string PlotPos::toDebug() { return fmt::format("{0} | {1} => {2}", toString(), minPos.toString(), maxPos.toString()); }

bool PlotPos::isPosInPlot(const Vec3& vec3) {
return vec3.x >= minPos.x && vec3.x <= maxPos.x && vec3.z >= minPos.z && vec3.z <= maxPos.z;
}

std::vector<PlotPos> PlotPos::getAdjacentPlots() {
return {PlotPos(x - 1, z), PlotPos(x + 1, z), PlotPos(x, z - 1), PlotPos(x, z + 1)};
}


} // namespace plotcraft::core
66 changes: 9 additions & 57 deletions src/plotcraft/core/PlotPos.h
Original file line number Diff line number Diff line change
@@ -1,79 +1,31 @@
#pragma once
#include "../config/Config.h"
#include "fmt/core.h"
#include "fmt/format.h"
#include "mc/math/Vec3.h"
#include "mc/world/level/ChunkPos.h"
#include "plotcraft/config/Config.h"

namespace plotcraft::core {


/*
基准点:Vec3{0,0,0}
地皮(0,0).minPos = Vec3{0,-64,0}
地皮(0,0).maxPos = Vec3{0 + config::cfg.generator.plotWidth,320,0 + config::cfg.generator.plotWidth}
横向x轴,纵向z轴
-1,1 0,1 1,1
-1,0 0,0 1,0
-1,-1 0,-1 1,-1
*/


class PlotPos {
public:
int x, z; // 地皮坐标
Vec3 minPos; // 地皮小端坐标
Vec3 maxPos; // 地皮大端坐标

// 使用地皮坐标构造
PlotPos(int x, int z) : x(x), z(z) {
auto& cfg = config::cfg.generator;
minPos = Vec3{x * (cfg.plotWidth + cfg.roadWidth), -64, z * (cfg.plotWidth + cfg.roadWidth)};
maxPos = Vec3{minPos.x + cfg.plotWidth - 1, 320, minPos.z + cfg.plotWidth - 1};
}
PlotPos(int x, int z);

PlotPos(const Vec3& vec3);

// 使用Vec3构造
PlotPos(const Vec3& vec3) {
auto& cfg = config::cfg.generator;
x = static_cast<int>(vec3.x) / (cfg.plotWidth + cfg.roadWidth);
z = static_cast<int>(vec3.z) / (cfg.plotWidth + cfg.roadWidth);
minPos = Vec3{x * (cfg.plotWidth + cfg.roadWidth), -64, z * (cfg.plotWidth + cfg.roadWidth)};
maxPos = Vec3{minPos.x + cfg.plotWidth - 1, 320, minPos.z + cfg.plotWidth - 1};
Vec3 getMin();

// 检查是否在道路上
int localX = static_cast<int>(vec3.x) % (cfg.plotWidth + cfg.roadWidth);
int localZ = static_cast<int>(vec3.z) % (cfg.plotWidth + cfg.roadWidth);
if (localX >= cfg.plotWidth || localZ >= cfg.plotWidth) {
minPos = Vec3{0, 0, 0};
maxPos = Vec3{0, 0, 0};
}
}
Vec3 getMax();

// 使用ChunkPos构造
PlotPos(const ChunkPos& chunkPos) {
auto& cfg = config::cfg.generator;
x = chunkPos.x * 16 / (cfg.plotWidth + cfg.roadWidth);
z = chunkPos.z * 16 / (cfg.plotWidth + cfg.roadWidth);
minPos = Vec3{x * (cfg.plotWidth + cfg.roadWidth), -64, z * (cfg.plotWidth + cfg.roadWidth)};
maxPos = Vec3{minPos.x + cfg.plotWidth - 1, 320, minPos.z + cfg.plotWidth - 1};
}
string toString();

Vec3 getMin() { return minPos; }
Vec3 getMax() { return maxPos; }
string toString() { return fmt::format("({0},{1})", x, z); }
string toDebug() { return fmt::format("{0} | {1} => {2}", toString(), minPos.toString(), maxPos.toString()); }
string toDebug();

// 检查给定的 Vec3 坐标是否在地皮内
bool isPosInPlot(const Vec3& vec3) {
return vec3.x >= minPos.x && vec3.x <= maxPos.x && vec3.z >= minPos.z && vec3.z <= maxPos.z;
}
bool isPosInPlot(const Vec3& vec3);

// 获取相邻的地皮
std::vector<PlotPos> getAdjacentPlots() {
return {PlotPos(x - 1, z), PlotPos(x + 1, z), PlotPos(x, z - 1), PlotPos(x, z + 1)};
}
std::vector<PlotPos> getAdjacentPlots();
};


Expand Down

0 comments on commit 5e6a421

Please sign in to comment.