diff --git a/src/plotcraft/core/PlotPos.cc b/src/plotcraft/core/PlotPos.cc new file mode 100644 index 0000000..3a82451 --- /dev/null +++ b/src/plotcraft/core/PlotPos.cc @@ -0,0 +1,71 @@ +#include "PlotPos.h" +#include "fmt/core.h" +#include "fmt/format.h" +#include "plotcraft/config/Config.h" +#include + + +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(std::floor(vec3.x)) % totalSize; + int localZ = static_cast(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::getAdjacentPlots() { + return {PlotPos(x - 1, z), PlotPos(x + 1, z), PlotPos(x, z - 1), PlotPos(x, z + 1)}; +} + + +} // namespace plotcraft::core \ No newline at end of file diff --git a/src/plotcraft/core/PlotPos.h b/src/plotcraft/core/PlotPos.h index 3822d6c..9ea758a 100644 --- a/src/plotcraft/core/PlotPos.h +++ b/src/plotcraft/core/PlotPos.h @@ -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(vec3.x) / (cfg.plotWidth + cfg.roadWidth); - z = static_cast(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(vec3.x) % (cfg.plotWidth + cfg.roadWidth); - int localZ = static_cast(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 getAdjacentPlots() { - return {PlotPos(x - 1, z), PlotPos(x + 1, z), PlotPos(x, z - 1), PlotPos(x, z + 1)}; - } + std::vector getAdjacentPlots(); };