10#include <Poco/Dynamic/Var.h>
11#include <Poco/JSON/Array.h>
12#include <Poco/JSON/Object.h>
13#include <Poco/JSON/Parser.h>
37const std::vector<RecipeDef>& builtinRecipes() {
38 static const std::vector<RecipeDef> kRecipes = {
39 {
"empty.clear",
"Clear staged scene_director props (reset only).", {}},
41 "Moonlit ruins: ground plane, ruin block, and seeded trees.",
42 {{
"seed",
"int",
"Placement RNG seed", 7.0}, {
"trees",
"int",
"Number of trees (0-12)", 3.0}}},
44 "Sunlit plaza: ground, center marker, and perimeter posts.",
45 {{
"seed",
"int",
"Placement RNG seed", 11.0}, {
"posts",
"int",
"Perimeter posts (0-8)", 4.0}}},
50const RecipeDef* findRecipe(std::string_view
id) {
51 for (
const auto&
recipe : builtinRecipes()) {
57std::string textPayload(Poco::JSON::Object::Ptr
object,
bool isError =
false) {
61std::string errorPayload(
const std::string&
message,
const std::string& hint = {}) {
62 Poco::JSON::Object::Ptr
out = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
63 out->set(
"ok",
false);
65 if (
message ==
"deferred-to-update")
out->set(
"retryable",
true);
66 if (!hint.empty())
out->set(
"hint", hint);
67 return textPayload(
out,
true);
70Poco::JSON::Object::Ptr parseJsonObject(
const std::string&
text) {
72 Poco::JSON::Parser parser;
74 if (
root.type() ==
typeid(Poco::JSON::Object::Ptr))
return root.extract<Poco::JSON::Object::Ptr>();
80int paramInt(Poco::JSON::Object::Ptr
params,
const char*
key,
int fallback) {
87 return static_cast<int>(std::lround(
params->getValue<
double>(
key)));
93uint32_t lcgNext(uint32_t& state) {
98double lcgUnit(uint32_t& state) {
return static_cast<double>(lcgNext(state) & 0xFFFFFFu) / 16777216.0; }
100Poco::JSON::Object::Ptr actionObject(
const std::string& action,
const std::string&
target,
101 Poco::JSON::Object::Ptr
params) {
102 Poco::JSON::Object::Ptr item = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
103 item->set(
"action", action);
109Poco::JSON::Object::Ptr spawnParams(
const std::string&
id,
const std::string&
kind,
double x,
double y,
double z,
110 double sx = 1.0,
double sy = 1.0,
double sz = 1.0,
int seed = 7) {
111 Poco::JSON::Object::Ptr
p = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
113 p->set(
"kind",
kind);
114 p->set(
"seed",
seed);
115 Poco::JSON::Array::Ptr
pos = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
120 Poco::JSON::Array::Ptr
scale = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
128Poco::JSON::Array::Ptr expandRecipeActions(
const RecipeDef&
recipe, Poco::JSON::Object::Ptr
params) {
129 Poco::JSON::Array::Ptr
actions = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
130 if (std::string_view(
recipe.id) ==
"empty.clear") {
131 actions->add(actionObject(
"reset",
"", {}));
134 if (std::string_view(
recipe.id) ==
"ruins.moonlit") {
136 int trees = paramInt(
params,
"trees", 3);
137 if (trees < 0) trees = 0;
138 if (trees > 12) trees = 12;
139 actions->add(actionObject(
"reset",
"", {}));
140 actions->add(actionObject(
"spawn",
"", spawnParams(
"ground",
"ground", 0.0, 0.0, 0.0, 12.0, 1.0, 12.0,
seed)));
142 actionObject(
"spawn",
"", spawnParams(
"ruin_core",
"box", 0.0, 0.6, 0.0, 2.2, 1.4, 2.2,
seed + 1)));
144 Poco::JSON::Object::Ptr
light = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
145 light->set(
"ambient", 0.18);
146 light->set(
"moonlit",
true);
149 uint32_t rng =
static_cast<uint32_t
>(
seed) * 2654435761u + 1u;
150 for (
int i = 0; i < trees; ++i) {
151 const double angle = lcgUnit(rng) * 6.283185307179586;
152 const double radius = 3.5 + lcgUnit(rng) * 3.0;
157 spawnParams(
"tree_" + std::to_string(i),
"tree",
x, 0.0,
z, 1.0, 1.0, 1.0,
seed + 10 + i)));
161 if (std::string_view(
recipe.id) ==
"plaza.sunlit") {
162 const int seed = paramInt(
params,
"seed", 11);
163 int posts = paramInt(
params,
"posts", 4);
164 if (posts < 0) posts = 0;
165 if (posts > 8) posts = 8;
166 actions->add(actionObject(
"reset",
"", {}));
168 actionObject(
"spawn",
"", spawnParams(
"plaza_ground",
"ground", 0.0, 0.0, 0.0, 10.0, 1.0, 10.0,
seed)));
170 actionObject(
"spawn",
"", spawnParams(
"plaza_marker",
"box", 0.0, 0.4, 0.0, 1.0, 0.8, 1.0,
seed + 2)));
171 for (
int i = 0; i < posts; ++i) {
172 const double angle = (6.283185307179586 *
static_cast<double>(i)) /
static_cast<double>(posts);
173 const double x = std::cos(
angle) * 4.0;
174 const double z = std::sin(
angle) * 4.0;
177 spawnParams(
"post_" + std::to_string(i),
"box",
x, 0.7,
z, 0.25, 1.4, 0.25,
seed + 20 + i)));
184std::string recipeList() {
185 Poco::JSON::Object::Ptr
out = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
186 out->set(
"ok",
true);
187 out->set(
"schemaId",
"eve.recipe.list");
188 out->set(
"schemaVersion", 1);
189 Poco::JSON::Array::Ptr recipes = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
190 for (
const auto&
recipe : builtinRecipes()) {
191 Poco::JSON::Object::Ptr item = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
192 item->set(
"id", std::string(
recipe.id));
193 item->set(
"description", std::string(
recipe.description));
194 Poco::JSON::Array::Ptr
params = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
196 Poco::JSON::Object::Ptr
p = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
197 p->set(
"name", std::string(param.name));
198 p->set(
"kind", std::string(param.kind));
199 p->set(
"description", std::string(param.description));
200 p->set(
"default", param.defaultValue);
203 item->set(
"params",
params);
206 out->set(
"recipes", recipes);
207 out->set(
"count",
static_cast<int>(builtinRecipes().
size()));
208 out->set(
"hint",
"Call eve_recipe_apply with {id, params?} under content.scene / workshop.rpg");
209 return textPayload(
out);
212std::string recipeApply(Poco::JSON::Object::Ptr args) {
213 if (!args || !args->has(
"id")) {
214 return errorPayload(
"id is required",
"Pass {id:\"ruins.moonlit\", params:{seed,trees}} from eve_recipe_list");
217 const RecipeDef*
recipe = findRecipe(
id);
219 return errorPayload(
"unknown recipe id '" +
id +
"'",
220 "Call eve_recipe_list; ids are registry strings, not free-form geometry");
225 if (!
vm)
return errorPayload(
"no VM",
"Attach a live eve run --mcp-port session first");
229 if (err ==
"deferred-to-update") {
230 return errorPayload(
"deferred-to-update",
231 "scene_director install was queued for the next eve.dev.poll; retry eve_recipe_apply");
233 return errorPayload(err.empty() ?
"scene_director kit unavailable" : err);
236 Poco::JSON::Object::Ptr
params;
237 if (args->has(
"params")) {
239 params = args->getObject(
"params");
241 return errorPayload(
"params must be a JSON object");
246 Poco::JSON::Object::Ptr
options = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
249 if (!actionsLiteral.ok())
return errorPayload(actionsLiteral.status().describe());
251 if (!optionsLiteral.ok())
return errorPayload(optionsLiteral.status().describe());
254 const std::string snippet =
255 "return ::scene_director.batch(" + actionsLiteral.value() +
"," + optionsLiteral.value() +
");";
257 if (!err.empty())
return errorPayload(err);
259 Poco::JSON::Object::Ptr report = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
260 report->set(
"schemaId",
"eve.recipe.apply-report");
261 report->set(
"schemaVersion", 1);
262 report->set(
"id",
id);
263 auto sceneObj = parseJsonObject(sceneOut);
265 report->set(
"scene", sceneObj);
266 const bool ok = sceneObj->optValue<
bool>(
"ok",
false);
267 report->set(
"ok",
ok);
268 if (sceneObj->has(
"revision")) report->set(
"revision", sceneObj->get(
"revision"));
270 report->set(
"error",
"recipe apply failed");
271 report->set(
"hint",
"Inspect scene.results; recipe apply is transactional when stopOnError=true");
272 return textPayload(report,
true);
275 report->set(
"ok",
false);
276 report->set(
"error",
"scene batch returned non-object JSON");
277 report->set(
"raw", sceneOut);
278 return textPayload(report,
true);
281 if (args->has(
"expectedRevision")) {
282 const int expected =
getArgInt(args,
"expectedRevision", -1);
283 const int actual = sceneObj->optValue<
int>(
"revision", -1);
284 if (expected >= 0 && actual >= 0 && expected != actual) {
285 report->set(
"ok",
false);
286 report->set(
"error",
"revision mismatch");
287 report->set(
"expectedRevision", expected);
288 report->set(
"revision", actual);
289 return textPayload(report,
true);
296 if (
auto info = parseJsonObject(infoOut)) report->set(
"info",
info);
298 report->set(
"hint",
"Recipe applied; escalate to verify.play for reachable/dialogue criteria");
299 return textPayload(report);
307 if (
name ==
"eve_recipe_list")
return recipeList();
308 if (
name ==
"eve_recipe_apply")
return recipeApply(args);
309 return errorPayload(
"unknown recipe tool '" + std::string(
name) +
"'");
313 return R
"json({"name":"eve_recipe_list","description":"List registered content recipes (id + params). Prefer recipe id over free-form prop scripts under content.scene.","inputSchema":{"type":"object","properties":{}}},{"name":"eve_recipe_apply","description":"Apply one registered content recipe as a single scene_director.batch transaction. Returns {ok,id,scene,revision?,info}. Retry when retryable deferred-to-update.","inputSchema":{"type":"object","properties":{"id":{"type":"string","description":"Recipe id from eve_recipe_list"},"params":{"type":"object","description":"Recipe parameters (seed, trees, …)"},"expectedRevision":{"type":"integer","description":"Optional optimistic concurrency check"},"stopOnError":{"type":"boolean"}},"required":["id"]}})json";
struct SQVM * HSQUIRRELVM
std::array< float, 3 > scale
Shared helpers for MCP tools that drive the embedded scene_director kit.
std::vector< ActionSpec > actions
const SquirrelValueOptions & options
static Debugger & instance()
bool ensureSceneDirectorInstalled(HSQUIRRELVM vm, std::string *err)
Convenience overload: allow heavy install immediately (tests / explicit install tool).
bool getArgBool(Poco::JSON::Object::Ptr args, const char *key, bool def=false)
Boolean argument, or def when absent or not convertible.
Result< std::string > sceneLiteral(const Poco::Dynamic::Var &value, int depth=0)
Encode owning JSON data as a Squirrel literal without losing pointer containers or precision.
bool isMcpRecipeTool(std::string_view name)
Whether name is eve_recipe_list or eve_recipe_apply.
std::string mcpStringify(const Poco::Dynamic::Var &value)
Compact single-line JSON text for a newline-framed MCP payload.
std::string textContentResult(const std::string &text, bool isError=false)
MCP tools/call result object carrying one text content item.
std::string_view mcpRecipeToolSchemas()
Comma-free MCP tool schema fragments for the recipe family.
int getArgInt(Poco::JSON::Object::Ptr args, const char *key, int def=0)
Integer argument, or def when absent or not convertible.
std::string callMcpRecipeTool(std::string_view name, Poco::JSON::Object::Ptr args)
Execute one recipe tool.
std::string getArgString(Poco::JSON::Object::Ptr args, const char *key, const std::string &def={})
String argument, or def when absent or not convertible.
std::string callSceneDirectorReturn(HSQUIRRELVM vm, const std::string &snippet, std::string *err)
Compile+run a snippet that returns a value; serialize it as JSON.