载入中...
搜索中...
未找到
GpuAgentsEditorModule.cpp
浏览该文件的文档.
2
3#include "common/Capability.h"
10
11#include <simplesquirrel/simplesquirrel.hpp>
12
13#include <algorithm>
14#include <cmath>
15#include <memory>
16#include <string_view>
17#include <utility>
18#include <vector>
19
20// GpuAgentsEditorModule.cpp
22namespace {
23
26 auto target = std::make_unique<gpuagents_editing::GpuAgentsDocumentTarget>(id.value());
27 const auto snapshot = request.find("snapshot");
28 if (snapshot != request.end()) {
29 auto loaded = target->loadSnapshot(snapshot->second);
30 if (!loaded.ok()) {
32 }
33 }
35 owned.target = std::move(target);
36 return eve::editing::applied<editor::AutomationOwnedTarget>(std::move(owned));
37}
38
39} // namespace
40
42
43std::vector<std::string_view> GpuAgentsAutomationTargetFactory::types() const { return {"gpuagents-effect"}; }
44
46 const editor::TargetId& target, std::string_view type, const editor::EditorValue::Object& request) {
47 if (type != "gpuagents-effect") {
48 return eve::editing::failed<editor::AutomationOwnedTarget>(editor::EditorStatus::Rejected,
49 editor::RuleId("editor.gpuagents.factory.type"),
50 "Unsupported GPU Agents automation target type");
51 }
52 return makeTarget(target, request);
53}
54
56 : factory_(std::make_unique<GpuAgentsAutomationTargetFactory>()),
57 packageProvider_(std::make_unique<GpuAgentsEditorPackageProvider>()) {
58 eve::cap::addListener<editor::IEditorAutomationTargetFactory>(factory_.get());
59 eve::cap::addListener<editing::IEditorPackageProvider>(packageProvider_.get());
60}
61
63 eve::cap::removeListener<editing::IEditorPackageProvider>(packageProvider_.get());
64 eve::cap::removeListener<editor::IEditorAutomationTargetFactory>(factory_.get());
65}
66
67void GpuAgentsEditorModule::expose(ssq::Table& table) { table.addClass(name, GpuAgentsEditorModule::create, false); }
68
69void GpuAgentsEditorModule::expose(ssq::Class&) {}
70
71} // namespace eve::gpuagents_editor
72
73// GpuAgentsPreview.cpp
74namespace eve::gpuagents_editor {
75
77 const GpuAgentsPreviewRequest& request) const {
79 out.documentRevision = target.describe().revision;
80 out.diagnostics = target.validate();
81 if (std::any_of(out.diagnostics.begin(), out.diagnostics.end(),
82 [](const auto& d) { return d.severity() == gpuagents_editing::DiagnosticSeverity::Error; })) {
83 out.status = EditorStatus::Failed;
84 return out;
85 }
86 if (request.agentCount <= 0 || request.seconds < 0.0 || request.fixedStep <= 0.0 || request.maximumSteps == 0) {
87 out.status = EditorStatus::Failed;
88 out.diagnostics.push_back(eve::editing::ruleDiagnostic(
89 eve::DiagnosticCode::InvalidArgument, gpuagents_editing::RuleId("editor.gpuagents.preview.request"),
90 gpuagents_editing::DiagnosticSeverity::Error, "Invalid GPU Agents preview request"));
91 return out;
92 }
93
95 auto backend = std::make_unique<gpuagents::EffectBackend>();
97 auto applied = applier.apply(target, backend.get(), &world);
98 if (!applied.ok()) {
99 out.status = EditorStatus::Failed;
101 eve::DiagnosticCode::Failed, gpuagents_editing::RuleId("editor.gpuagents.preview.apply"),
102 gpuagents_editing::DiagnosticSeverity::Error, "Failed to apply GPU Agents document to preview runtime"));
103 return out;
104 }
105
106 const int count = std::min(request.agentCount, backend->simulation().capacity());
107 auto spawned = backend->simulation().initialize(count, [&](int i) {
109 const float a = static_cast<float>(i) * 0.6180339f;
110 const float r = static_cast<float>(request.spawnSpread) * (0.2f + 0.8f * std::fmod(a, 1.f));
111 s.position = {static_cast<float>(request.centerX) + std::cos(a * 6.2831853f) * r,
112 static_cast<float>(request.centerY),
113 static_cast<float>(request.centerZ) + std::sin(a * 6.2831853f) * r};
114 s.velocity = {std::cos(a), 0.f, std::sin(a)};
115 if (target.settings().kind == "Petal") {
116 s.velocity = {0.f, -1.f, 0.f};
117 s.customData[1] = 1.f;
118 s.position.y += 2.f;
119 }
120 if (target.settings().kind == "LifeNetwork") s.position.y = static_cast<float>(request.centerY);
121 return s;
122 });
123 if (!spawned.ok()) {
124 out.status = EditorStatus::Failed;
125 return out;
126 }
127
128 auto registered = world.registerBackend("preview", backend.get());
129 if (!registered.ok()) {
130 out.status = EditorStatus::Failed;
131 return out;
132 }
133 const double step = request.fixedStep;
134 std::uint32_t steps = 0;
135 double t = 0.0;
136 while (t + 1e-12 < request.seconds && steps < request.maximumSteps) {
137 auto stepped = world.stepAll(static_cast<float>(step));
138 if (!stepped.ok()) {
139 out.status = EditorStatus::Failed;
140 return out;
141 }
142 t += step;
143 ++steps;
144 }
145 out.simulatedSeconds = t;
146 for (const auto& s : backend->simulation().states()) {
147 if (!s.alive) continue;
149 a.x = s.position.x;
150 a.y = s.position.y;
151 a.z = s.position.z;
152 a.vx = s.velocity.x;
153 a.vy = s.velocity.y;
154 a.vz = s.velocity.z;
155 a.age = s.age;
156 a.custom0 = s.customData[0];
157 out.agents.push_back(a);
158 }
159 if (target.settings().kind == "LifeNetwork") {
160 for (const auto& c : world.surface().lifeField) out.lifeTrailEnergy += c.r;
161 }
162 out.status = EditorStatus::Applied;
163 return out;
164}
165
166} // namespace eve::gpuagents_editor
LogicalId target
ActiveSource owned
const std::string & s
bool applied
std::string name
Definition ECS.cpp:154
const GltfImportRequest & request
std::uint32_t spawned
double r
std::int32_t c
Snapshot snapshot
MeleePoint3 a
Definition MeleeHit.cpp:40
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:27
OceanRenderBackend backend
std::unordered_set< std::string > loaded
Definition Plugins.cpp:24
Object::Ptr value
float d
int steps
float t
ScriptStructure out
int count
float step
Definition TreeMesh.cpp:451
const Status & status() const noexcept
Inspect the structured operation status.
Definition Result.h:521
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
const std::vector< Diagnostic > & diagnostics() const noexcept
All diagnostics in stable insertion order.
Definition Status.h:128
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:313
std::map< std::string, Value > Object
World-level registry for GPU agent simulations and environment semantics.
Reversible document for GPU Agents effect authoring.
Optional bridge projecting a validated document into live World/Backend. @ownership Borrowed world/ba...
Result< void > apply(const GpuAgentsDocumentTarget &target, gpuagents::EffectBackend *backend, gpuagents::GpuAgentWorld *world) const
Configure backend from document and optionally re-init the world surface domain.
Creates schema-driven GPU Agents documents for editor automation. @ownership Returned targets own the...
std::vector< std::string_view > types() const override
Accepted GPU Agents target type names.
editor::Result< editor::AutomationOwnedTarget > create(const editor::TargetId &target, std::string_view type, const editor::EditorValue::Object &request) override
Create a GPU Agents document and optionally load its snapshot request field.
Composition adapter that publishes the GPU Agents automation-target factory. @ownership Owns the regi...
~GpuAgentsEditorModule() override
Gpu agents editor module.
GpuAgentsPreviewSnapshot build(const GpuAgentsDocumentTarget &target, const GpuAgentsPreviewRequest &request) const
Validate, spawn, step and capture a renderer-neutral preview frame.
Diagnostic ruleDiagnostic(eve::DiagnosticCode code, RuleId rule, DiagnosticSeverity severity, std::string message)
Build an owning common diagnostic carrying an open editing rule identity.
Target plus optional borrowed-dependency owner returned by an automation factory.
std::unique_ptr< IEditableTarget > target
Standardized per-agent simulation output shared by all effect kinds.
Definition AgentState.h:12
One agent sample in a preview snapshot.
Deterministic scrub request for editor preview.
Revision-bound preview snapshot with optional life-field energy.