载入中...
搜索中...
未找到
ProcgenCapabilities.cpp
浏览该文件的文档.
1#include "common/Capability.h"
3#include "procgen/MeshBuild.h"
4#include "procgen/Params.h"
5#include "procgen/Procgen.h"
6
7#include <cstdint>
8#include <cstdlib>
9#include <string>
10#include <utility>
11#include <vector>
12
13namespace eve::procgen {
14namespace {
15
16class ProcgenQueryImpl final : public eve::IProcgenQuery {
17public:
18 Procgen *pg() { return eve::ModuleManager::getInstance<Procgen>("Procgen"); }
19
20 std::vector<std::string> algorithms() override {
21 std::vector<std::string> out;
22 if (auto* p = pg())
23 for (int i = 0; i < p->getAlgorithmCount(); ++i) out.push_back(p->getAlgorithmId(i));
24 return out;
25 }
26
27 std::vector<std::string> meshRecipes() override {
28 std::vector<std::string> out;
29 if (auto* p = pg())
30 for (int i = 0; i < p->getMeshRecipeCount(); ++i) out.push_back(p->getMeshRecipeId(i));
31 return out;
32 }
33
34 std::vector<std::string> textureRecipes() override {
35 std::vector<std::string> out;
36 if (auto* p = pg())
37 for (int i = 0; i < p->getTextureRecipeCount(); ++i)
38 out.push_back(p->getTextureRecipeId(i));
39 return out;
40 }
41
42 std::vector<std::string> pbrRecipes() override {
43 std::vector<std::string> out;
44 if (auto* p = pg())
45 for (int i = 0; i < p->getPbrRecipeCount(); ++i) out.push_back(p->getPbrRecipeId(i));
46 return out;
47 }
48
49 eve::Result<std::string> generateMap(const std::string& algorithm, int width, int height, uint32_t seed,
50 const std::vector<std::pair<std::string, std::string>>& params) override {
51 auto* p = pg();
52 if (!p) {
54 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "Procgen module not available", "procgen"));
55 }
56 auto parResult = Procgen::newParamsHandle();
57 if (!parResult) {
59 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "parameter allocation failed", "procgen"));
60 }
61 const auto parRef = std::move(parResult).takeValue();
62 auto parView = Procgen::resolve(parRef);
63 if (!parView.isBound()) {
64 Procgen::release(parRef).ignore("release failed after stale parameter lookup");
66 eve::Diagnostic::error(eve::DiagnosticCode::StaleHandle, "parameter handle became stale", "procgen"));
67 }
68 auto* par = parView.get();
69 par->setSize(width, height);
70 par->setSeed(seed);
71 for (const auto& kv : params) {
72 if (kv.second.find_first_not_of("0123456789-") == std::string::npos)
73 par->setInt(kv.first, std::atoi(kv.second.c_str()));
74 else
75 par->setString(kv.first, kv.second);
76 }
77 auto gridResult = p->generateHandle(algorithm, parRef);
78 if (!gridResult) {
79 Procgen::release(parRef).ignore("release failed after map generation failure");
81 eve::DiagnosticCode::Failed, "generate failed: " + gridResult.status().describe(), "procgen"));
82 }
83 auto jsonResult = p->gridToJson(std::move(gridResult).takeValue());
84 if (!jsonResult) {
85 Procgen::release(parRef).ignore("release failed after map serialization failure");
87 eve::DiagnosticCode::Failed, "serialization failed: " + jsonResult.status().describe(), "procgen"));
88 }
89 std::string json = std::move(jsonResult).takeValue();
90 Procgen::release(parRef).ignore("release failed after map generation");
91 return eve::Result<std::string>::success(std::move(json));
92 }
93
94 eve::Result<std::string> buildMesh(const std::string& recipe, uint32_t seed, int width, int height,
95 int depth) override {
96 auto* p = pg();
97 if (!p) {
99 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "Procgen module not available", "procgen"));
100 }
101 auto parResult = Procgen::newParamsHandle();
102 if (!parResult) {
104 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "parameter allocation failed", "procgen"));
105 }
106 const auto parRef = std::move(parResult).takeValue();
107 auto parView = Procgen::resolve(parRef);
108 if (!parView.isBound()) {
109 Procgen::release(parRef).ignore("release failed after stale parameter lookup");
111 eve::Diagnostic::error(eve::DiagnosticCode::StaleHandle, "parameter handle became stale", "procgen"));
112 }
113 auto* par = parView.get();
114 par->setSeed(seed);
115 if (width > 0) par->setInt("width", width);
116 if (height > 0) par->setInt("height", height);
117 if (depth > 0) par->setInt("depth", depth);
118 auto meshResult = p->buildMeshHandle(recipe, parRef);
119 if (!meshResult) {
120 Procgen::release(parRef).ignore("release failed after mesh generation failure");
122 eve::DiagnosticCode::Failed, "build failed: " + meshResult.status().describe(), "procgen"));
123 }
124 const auto meshRef = std::move(meshResult).takeValue();
125 auto mesh = p->resolveMeshBuild(meshRef);
126 if (!mesh.isBound()) {
127 p->releaseMeshBuild(meshRef).ignore("release failed after stale mesh lookup");
128 Procgen::release(parRef).ignore("release failed after stale mesh lookup");
130 eve::DiagnosticCode::StaleHandle, "build failed: mesh handle became stale", "procgen"));
131 }
132 std::string json = "{\"vertices\":" + std::to_string(mesh->getVertexCount()) +
133 ",\"triangles\":" + std::to_string(mesh->getIndexCount() / 3) + "}";
134 p->releaseMeshBuild(meshRef).ignore("release mesh after capability query");
135 Procgen::release(parRef).ignore("release failed after mesh generation");
136 return eve::Result<std::string>::success(std::move(json));
137 }
138};
139
140} // namespace
141
143 static ProcgenQueryImpl impl;
144 eve::cap::provide<eve::IProcgenQuery>(&impl);
145}
146
147} // namespace eve::procgen
void * impl
glm::vec4 p[6]
std::uint32_t height
std::uint32_t width
std::vector< RecipeParam > params
OceanMeshSettings mesh
BakeRecipe recipe
ScriptStructure out
uint32_t seed
Definition TreeMesh.cpp:276
std::uint32_t depth
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Procedural generation query surface (provided by the procgen module).
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Definition Result.h:571
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static eve::script::Borrowed< Params > resolve(ProcgenParamsHandleRef reference) noexcept
Resolves parameters as a non-owning observation.
static eve::Result< ProcgenParamsHandleRef > newParamsHandle()
Allocates module-owned generation parameters.
static eve::Result< void > release(ProcgenParamsHandleRef reference)
Releases module-owned parameters.
void registerProcgenCapabilities()
Registers procgen capabilities.