diff --git a/src/BedrockServerClientInterface.cpp b/src/BedrockServerClientInterface.cpp index 100eae5..7452dbe 100644 --- a/src/BedrockServerClientInterface.cpp +++ b/src/BedrockServerClientInterface.cpp @@ -15,7 +15,7 @@ namespace bsci { struct BedrockServerClientInterface::Impl { #ifdef TEST - std::unique_ptr geoTest = GeometryGroup::createDefault(); + std::unique_ptr geoTest; std::vector gids; #endif }; @@ -68,6 +68,7 @@ bool BedrockServerClientInterface::enable() { loadConfig(); } #ifdef TEST + impl->geoTest = GeometryGroup::createDefault(); test::registerTestCommand(impl->geoTest, impl->gids); std::thread([this] { auto geo = bsci::GeometryGroup::createDefault(); diff --git a/src/bsci/GeometryGroup.cpp b/src/bsci/GeometryGroup.cpp index c255832..1d0e546 100644 --- a/src/bsci/GeometryGroup.cpp +++ b/src/bsci/GeometryGroup.cpp @@ -4,9 +4,9 @@ #include #include "BedrockServerClientInterface.h" -#include "bsci/utils/Math.h" -#include "bsci/particle/ParticleSpawner.h" #include "bsci/debug_draw/DebugDrawingHandler.h" +#include "bsci/particle/ParticleSpawner.h" +#include "bsci/utils/Math.h" namespace bsci { @@ -123,7 +123,7 @@ GeometryGroup::GeoId GeometryGroup::cylinder( auto const delta = std::numbers::pi * 2 / (double)points; std::vector ids; - ids.reserve(points); + ids.reserve(3 * points); Vec3 lastPos = t * radius; for (size_t i{1}; i <= points; i++) { double theta = (double)i * delta; @@ -218,4 +218,60 @@ GeometryGroup::GeoId GeometryGroup:: text(DimensionType, Vec3 const&, std::string, mce::Color const&, std::optional) { return GeoId::invalid(); } + +GeometryGroup::GeoId GeometryGroup::cone( + DimensionType dim, + Vec3 const& topCenter, + Vec3 const& bottomCenter, + float topRadius, + float bottomRadius, + mce::Color const& color, + std::optional thickness +) { + auto const& config = BedrockServerClientInterface::getInstance().getConfig().particle; + + size_t const points = + (std::clamp( + (size_t)std::ceil(topRadius * std::numbers::pi * 2 / config.minCircleSpacing), + 7ui64, + config.maxCircleSegments + + ) + + std::clamp( + (size_t)std::ceil(bottomRadius * std::numbers::pi * 2 / config.minCircleSpacing), + 7ui64, + config.maxCircleSegments + + )) + / 2; + auto const [t, b] = branchlessONB((topCenter - bottomCenter).normalize()); + auto const delta = std::numbers::pi * 2 / (double)points; + + std::vector ids; + ids.reserve(3 * points); + Vec3 lastTopOffset = t * topRadius; + Vec3 lastBottomOffset = t * bottomRadius; + for (size_t i{1}; i <= points; i++) { + double theta = (double)i * delta; + Vec3 topOffset = t * (topRadius * std::cos(theta)) + b * topRadius * std::sin(theta); + Vec3 bottomOffset = + t * (bottomRadius * std::cos(theta)) + b * bottomRadius * std::sin(theta); + ids.emplace_back( + line(dim, topCenter + lastTopOffset, topCenter + topOffset, color, thickness) + ); + ids.emplace_back( + line(dim, topCenter + topOffset, bottomCenter + bottomOffset, color, thickness) + ); + ids.emplace_back(line( + dim, + bottomCenter + lastBottomOffset, + bottomCenter + bottomOffset, + color, + thickness + )); + lastTopOffset = topOffset; + lastBottomOffset = bottomOffset; + } + return merge(ids); +} } // namespace bsci diff --git a/src/bsci/GeometryGroup.h b/src/bsci/GeometryGroup.h index ab1fc23..16f53d1 100644 --- a/src/bsci/GeometryGroup.h +++ b/src/bsci/GeometryGroup.h @@ -101,6 +101,16 @@ class GeometryGroup { mce::Color const& color = mce::Color::WHITE(), std::optional scale = {} ); + + BSCI_API virtual GeoId cone( + DimensionType dim, + Vec3 const& topCenter, + Vec3 const& bottomCenter, + float topRadius, + float bottomRadius, + mce::Color const& color = mce::Color::WHITE(), + std::optional thickness = {} + ); }; } // namespace bsci diff --git a/src/bsci/debug_draw/DebugDrawingHandler.cpp b/src/bsci/debug_draw/DebugDrawingHandler.cpp index 06cd723..eb24b68 100644 --- a/src/bsci/debug_draw/DebugDrawingHandler.cpp +++ b/src/bsci/debug_draw/DebugDrawingHandler.cpp @@ -10,8 +10,6 @@ #include #include -#include "bsci/particle/ParticleSpawner.h" - #include #include #include @@ -219,7 +217,7 @@ GeometryGroup::GeoId DebugDrawingHandler::box( ShapeDataPayload shape; shape.mNetworkId = nextId_.fetch_sub(1); shape.mShapeType = ScriptModuleDebugUtilities::ScriptDebugShapeType::Box; - shape.mLocation = box.min; + shape.mLocation = (box.min + box.max) / 2; shape.mColor = color; shape.mDimensionId = dim; shape.mExtraDataPayload = BoxDataPayload{.mBoxBound = box.max - box.min}; diff --git a/src/bsci/test/Test.cpp b/src/bsci/test/Test.cpp index 46c73dd..1b1000c 100644 --- a/src/bsci/test/Test.cpp +++ b/src/bsci/test/Test.cpp @@ -1,5 +1,3 @@ -#include "BedrockServerClientInterface.h" - #ifdef TEST #include "bsci/GeometryGroup.h" #include "ll/api/command/CommandHandle.h" @@ -33,7 +31,7 @@ void registerTestCommand( ll::command::RuntimeCommand const& self ) { Vec3 pos = self["pos"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); @@ -56,12 +54,12 @@ void registerTestCommand( ll::command::RuntimeCommand const& self ) { Vec3 begin = self["begin"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); Vec3 end = self["end"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); @@ -81,12 +79,12 @@ void registerTestCommand( ll::command::RuntimeCommand const& self ) { Vec3 begin = self["begin"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); Vec3 end = self["end"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); @@ -106,7 +104,7 @@ void registerTestCommand( ll::command::RuntimeCommand const& self ) { Vec3 center = self["center"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); @@ -116,27 +114,6 @@ void registerTestCommand( output.success("draw circle"); }); - cmd.runtimeOverload() - .text("circle_with_thick") - .required("dim", ll::command::ParamKind::Dimension) - .required("center", ll::command::ParamKind::Vec3) - .required("radius", ll::command::ParamKind::Float) - .execute([&geo, &gids]( - CommandOrigin const& origin, - CommandOutput& output, - ll::command::RuntimeCommand const& self - ) { - Vec3 center = self["center"].get().getPosition( - CommandVersion::CurrentVersion(), - origin, - Vec3::ZERO() - ); - auto radius = self["radius"].get(); - DimensionType dim = self["dim"].get().id; - gids.emplace_back(geo->circle(dim, center, {0, 1, 0}, radius, mce::Color::WHITE(), 1)); - output.success("draw circle"); - }); - cmd.runtimeOverload() .text("cylinder") .required("dim", ll::command::ParamKind::Dimension) @@ -149,13 +126,13 @@ void registerTestCommand( ll::command::RuntimeCommand const& self ) { Vec3 topCenter = self["topCenter"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); Vec3 bottomCenter = self["bottomCenter"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); @@ -176,7 +153,7 @@ void registerTestCommand( ll::command::RuntimeCommand const& self ) { Vec3 center = self["center"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); @@ -205,7 +182,7 @@ void registerTestCommand( ll::command::RuntimeCommand const& self ) { Vec3 offset = self["offset"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); @@ -225,7 +202,7 @@ void registerTestCommand( ll::command::RuntimeCommand const& self ) { Vec3 offset = self["offset"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); @@ -236,48 +213,6 @@ void registerTestCommand( output.success("clear all"); }); - cmd.runtimeOverload() - .text("circle2") - .required("dim", ll::command::ParamKind::Dimension) - .required("center", ll::command::ParamKind::Vec3) - .required("radius", ll::command::ParamKind::Float) - .execute([&geo, &gids]( - CommandOrigin const& origin, - CommandOutput& output, - ll::command::RuntimeCommand const& self - ) { - Vec3 center = self["center"].get().getPosition( - CommandVersion::CurrentVersion(), - origin, - Vec3::ZERO() - ); - auto radius = self["radius"].get(); - DimensionType dim = self["dim"].get().id; - gids.emplace_back(geo->circle2(dim, center, {0, 1, 0}, radius, mce::Color::WHITE())); - output.success("draw circle"); - }); - - cmd.runtimeOverload() - .text("sphere2") - .required("dim", ll::command::ParamKind::Dimension) - .required("center", ll::command::ParamKind::Vec3) - .required("radius", ll::command::ParamKind::Float) - .execute([&geo, &gids]( - CommandOrigin const& origin, - CommandOutput& output, - ll::command::RuntimeCommand const& self - ) { - Vec3 center = self["center"].get().getPosition( - CommandVersion::CurrentVersion(), - origin, - Vec3::ZERO() - ); - auto radius = self["radius"].get(); - DimensionType dim = self["dim"].get().id; - gids.emplace_back(geo->sphere2(dim, center, radius)); - output.success("draw sphere"); - }); - cmd.runtimeOverload() .text("arrow") .required("dim", ll::command::ParamKind::Dimension) @@ -289,12 +224,12 @@ void registerTestCommand( ll::command::RuntimeCommand const& self ) { Vec3 begin = self["begin"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); Vec3 end = self["end"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); @@ -308,21 +243,54 @@ void registerTestCommand( .required("dim", ll::command::ParamKind::Dimension) .required("pos", ll::command::ParamKind::Vec3) .required("text", ll::command::ParamKind::String) + .required("scale", ll::command::ParamKind::Float) .execute([&geo, &gids]( CommandOrigin const& origin, CommandOutput& output, ll::command::RuntimeCommand const& self ) { Vec3 pos = self["pos"].get().getPosition( - CommandVersion::CurrentVersion(), + static_cast(CurrentCmdVersion::Latest), origin, Vec3::ZERO() ); - std::string text = self["text"].get(); - DimensionType dim = self["dim"].get().id; - gids.emplace_back(geo->text(dim, pos + Vec3{1, 1, 1}, text, mce::Color::WHITE(), 30)); + std::string text = self["text"].get(); + DimensionType dim = self["dim"].get().id; + float scale = self["scale"].get(); + gids.emplace_back(geo->text(dim, pos + Vec3{1, 1, 1}, text, mce::Color::WHITE(), scale) + ); output.success("draw text"); }); + + cmd.runtimeOverload() + .text("cone") + .required("dim", ll::command::ParamKind::Dimension) + .required("topCenter", ll::command::ParamKind::Vec3) + .required("bottomCenter", ll::command::ParamKind::Vec3) + .required("topRadius", ll::command::ParamKind::Float) + .required("bottomRadius", ll::command::ParamKind::Float) + .execute([&geo, &gids]( + CommandOrigin const& origin, + CommandOutput& output, + ll::command::RuntimeCommand const& self + ) { + Vec3 topCenter = self["topCenter"].get().getPosition( + static_cast(CurrentCmdVersion::Latest), + origin, + Vec3::ZERO() + ); + Vec3 bottomCenter = + self["bottomCenter"].get().getPosition( + static_cast(CurrentCmdVersion::Latest), + origin, + Vec3::ZERO() + ); + auto topRadius = self["topRadius"].get(); + auto bottomRadius = self["bottomRadius"].get(); + DimensionType dim = self["dim"].get().id; + gids.emplace_back(geo->cone(dim, topCenter, bottomCenter, topRadius, bottomRadius)); + output.success("draw cylinder"); + }); } } // namespace bsci::test #endif \ No newline at end of file diff --git a/src/bsci/test/Test.h b/src/bsci/test/Test.h index a66aab3..a470e0d 100644 --- a/src/bsci/test/Test.h +++ b/src/bsci/test/Test.h @@ -1,7 +1,5 @@ #pragma once -#include "BedrockServerClientInterface.h" - #ifdef TEST #include "bsci/GeometryGroup.h" #include diff --git a/xmake.lua b/xmake.lua index 3e3f6c1..d0e525a 100644 --- a/xmake.lua +++ b/xmake.lua @@ -20,6 +20,7 @@ target("BedrockServerClientInterface") add_rules("@levibuildscript/modpacker") add_cxflags( "/EHa", "/utf-8", "/W4", "/w44265", "/w44289", "/w44296", "/w45263", "/w44738", "/w45204") add_defines( "_HAS_CXX23=1", "NOMINMAX", "UNICODE", "BSCI_EXPORTS") + -- add_defines("TEST") add_files("src/**.cpp") add_headerfiles("src/(bsci/**.h)") add_includedirs("src")