载入中...
搜索中...
未找到
PcgBuildConfig.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <simplesquirrel/simplesquirrel.hpp>
6#include "scene/PcgGrowth.h"
7#include "scene/Scene.h"
9
10// PcgBuildConfig.cpp
11namespace eve::scene {
12namespace {}
14 if (type < 0 || type > 1)
16 DiagnosticCode::InvalidArgument, "publication type must be 0 or 1", {}, {}, "scene.pcgBuildConfig"));
17 publicationType_ = static_cast<PcgPublicationType>(type);
18 return Result<void>::success();
19}
20Result<void> PcgBuildConfig::addHistory(int category, const std::string& sceneName, int64_t timestamp) {
21 if (category < 0 || category > 6)
23 "build log category must be in range 0..6", {}, {},
24 "scene.pcgBuildConfig"));
25 history_.push_back({static_cast<PcgBuildLogCategory>(category), sceneName, timestamp});
26 return Result<void>::success();
27}
29 if (i >= history_.size())
31 DiagnosticCode::InvalidArgument, "build history index is out of range", {}, {}, "scene.pcgBuildConfig"));
32 return Result<int>::success(static_cast<int>(history_[i].category));
33}
35 if (i >= history_.size())
37 DiagnosticCode::InvalidArgument, "build history index is out of range", {}, {}, "scene.pcgBuildConfig"));
38 return Result<std::string>::success(history_[i].sceneName);
39}
41 if (i >= history_.size())
43 DiagnosticCode::InvalidArgument, "build history index is out of range", {}, {}, "scene.pcgBuildConfig"));
44 return Result<int64_t>::success(history_[i].timestamp);
45}
47 auto vm = t.getHandle();
48 auto c = t.addClass("PcgBuildConfig", ssq::Class::Ctor<PcgBuildConfig()>());
49 c.addFunc("setPublicationType",
50 [vm](PcgBuildConfig* s, int v) { return eve::script::projectResult(vm, s->setPublicationType(v)); });
51 c.addFunc("getPublicationType", [](const PcgBuildConfig* s) { return s->getPublicationType(); });
52 c.addFunc("addHistory", [vm](PcgBuildConfig* s, int category, const std::string& n, int64_t ts) {
53 return eve::script::projectResult(vm, s->addHistory(category, n, ts));
54 });
55 c.addFunc("clearHistory", [](PcgBuildConfig* s) { s->clearHistory(); });
56 c.addFunc("getHistoryCount", [](const PcgBuildConfig* s) { return s->getHistoryCount(); });
57 c.addFunc("getHistoryCategory", [vm](const PcgBuildConfig* s, uint64_t i) {
58 return eve::script::projectResult(vm, s->getHistoryCategory(i), [](int v) { return v; });
59 });
60 c.addFunc("getHistorySceneName", [vm](const PcgBuildConfig* s, uint64_t i) {
61 return eve::script::projectResult(vm, s->getHistorySceneName(i), [](const std::string& v) { return v; });
62 });
63 c.addFunc("getHistoryTimestamp", [vm](const PcgBuildConfig* s, uint64_t i) {
64 return eve::script::projectResult(vm, s->getHistoryTimestamp(i), [](int64_t v) { return v; });
65 });
66}
67} // namespace eve::scene
68
69// PcgGrowth.cpp
70namespace eve::scene {
71namespace {
72float random01(std::uint32_t x) {
73 x ^= x >> 16;
74 x *= 0x7feb352du;
75 x ^= x >> 15;
76 x *= 0x846ca68bu;
77 x ^= x >> 16;
78 return float(x >> 8) * (1.f / 16777216.f);
79}
80} // namespace
81Result<void> PcgGrowth::configure(float a, float b, float v, float d) {
82 if (!std::isfinite(a) || !std::isfinite(b) || !std::isfinite(v) || !std::isfinite(d) || a < .1f || a > 2 ||
83 b < .1f || b > 2 || v < 0 || v > 2 || d < .5f || d > 60)
85 "growth configuration is outside Pcg ranges",
86 "scene.pcgGrowth.configure"));
87 startScale_ = a;
88 endScale_ = b;
89 variance_ = v;
90 duration_ = d;
91 return Result<void>::success();
92}
93Result<void> PcgGrowth::start(float now, std::uint32_t seed) {
94 if (!std::isfinite(now))
96 Diagnostic::error(DiagnosticCode::InvalidArgument, "start time must be finite", "scene.pcgGrowth.start"));
97 startTime_ = now;
98 actualEnd_ = endScale_ + random01(seed) * variance_;
99 scale_ = startScale_;
100 started_ = true;
101 finished_ = false;
102 dying_ = false;
103 return Result<void>::success();
104}
106 if (!started_)
108 Diagnostic::error(DiagnosticCode::InvalidArgument, "growth has not started", "scene.pcgGrowth.advance"));
109 if (!std::isfinite(now) || now < startTime_)
111 DiagnosticCode::InvalidArgument, "time must be finite and monotonic", "scene.pcgGrowth.advance"));
112 float t = std::min((now - startTime_) / duration_, 1.f);
113 scale_ = startScale_ + t * (actualEnd_ - startScale_);
114 finished_ = t >= 1;
115 return Result<float>::success(scale_);
116}
118 if (!std::isfinite(now))
120 Diagnostic::error(DiagnosticCode::InvalidArgument, "death time must be finite", "scene.pcgGrowth.die"));
121 deathTime_ = now + 5;
122 dying_ = true;
123 return Result<void>::success();
124}
126 if (!std::isfinite(now))
128 Diagnostic::error(DiagnosticCode::InvalidArgument, "time must be finite", "scene.pcgGrowth.destroy"));
129 return Result<bool>::success(dying_ && now >= deathTime_);
130}
132 auto due = g.shouldDestroy(now);
133 if (!due) return Result<bool>::failure(due.status());
134 if (due.value()) {
135 auto* s = node.getScene();
136 if (!s)
138 "growth node cannot be removed", "scene.pcgGrowth.node"));
139 auto removed = s->removeLeafAt(node.getHostName(), node.getNodeId());
140 if (!removed.ok()) return Result<bool>::failure(removed.status());
141 return Result<bool>::success(true);
142 }
143 auto scale = g.advance(now);
144 if (!scale) return Result<bool>::failure(scale.status());
145 if (!node.setScale(scale.value(), scale.value(), scale.value()))
147 Diagnostic::error(DiagnosticCode::InvalidArgument, "growth node cannot be scaled", "scene.pcgGrowth.node"));
148 return Result<bool>::success(false);
149}
150void exposePcgGrowthBindings(ssq::Table& t) {
151 auto c = t.addClass("PcgGrowth", ssq::Class::Ctor<PcgGrowth()>());
152 auto vm = t.getHandle();
153 c.addFunc("configure", [vm](PcgGrowth* s, float a, float b, float v, float d) {
154 return eve::script::projectResult(vm, s->configure(a, b, v, d));
155 });
156 c.addFunc("start", [vm](PcgGrowth* s, float n, int seed) {
157 return eve::script::projectResult(vm, s->start(n, std::uint32_t(seed)));
158 });
159 c.addFunc("advance", [vm](PcgGrowth* s, float n) {
160 return eve::script::projectResult(vm, s->advance(n), [](float v) { return Value(v); });
161 });
162 c.addFunc("die", [vm](PcgGrowth* s, float n) { return eve::script::projectResult(vm, s->die(n)); });
163 c.addFunc("shouldDestroy", [vm](const PcgGrowth* s, float n) {
164 return eve::script::projectResult(vm, s->shouldDestroy(n), [](bool v) { return Value(v); });
165 });
166 c.addFunc("getActualEndScale", [](const PcgGrowth* s) { return s->getActualEndScale(); });
167 c.addFunc("getScale", [](const PcgGrowth* s) { return s->getScale(); });
168 c.addFunc("getFinished", [](const PcgGrowth* s) { return s->getFinished(); });
169 t.addFunc("applyPcgGrowth", [vm](PcgGrowth* g, SceneNodeRef* n, float now) {
170 auto r = g && n ? applyPcgGrowth(*g, *n, now)
172 DiagnosticCode::InvalidArgument, "growth and node are required", "scene.pcgGrowth.node"));
173 return eve::script::projectResult(vm, std::move(r), [](bool v) { return Value(v); });
174 });
175}
176} // namespace eve::scene
double x
const std::string & s
HSQUIRRELVM vm
Definition ECS.cpp:38
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec3 n
Definition Grass.cpp:63
double r
float v
std::int32_t c
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float d
float t
const RoadNode * node
int removed
The single Squirrel projection for common Result, Status and Value.
uint32_t seed
Definition TreeMesh.cpp:276
const char * category
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Caller-owned Pcg publication configuration and ordered build history.
Result< void > addHistory(int category, const std::string &sceneName, int64_t timestamp)
Append one ordered build history entry.
Result< int > getHistoryCategory(uint64_t index) const
Return category at index.
Result< std::string > getHistorySceneName(uint64_t index) const
Return scene name at index.
Result< void > setPublicationType(int type)
Set publication type, where 0 is Addressables and 1 is RegularBuild.
Result< int64_t > getHistoryTimestamp(uint64_t index) const
Return Unix timestamp at index.
Caller-owned one-shot Pcg Growth state with explicit time and random seed.
Definition PcgGrowth.h:8
Result< void > configure(float startScale, float endScale, float variance, float duration)
Configure scale interval, variance, and duration atomically.
Result< void > die(float now)
Schedule destruction five seconds from absolute real time.
Result< float > advance(float now)
Evaluate the linear scale at absolute real time.
Result< void > start(float now, std::uint32_t seed)
Choose the final scale from seed and begin at absolute real time.
Result< bool > shouldDestroy(float now) const
Return whether delayed destruction is due.
Script handle to a scene node (hostName + nodeId). Deliberately holds strings rather than arena point...
PcgBuildLogCategory
PcgBuildLogCategory public API.
PcgPublicationType
PcgPublicationType public API.
void exposePcgBuildConfigBindings(ssq::Table &t)
Register Pcg build-config bindings.
Result< bool > applyPcgGrowth(PcgGrowth &g, SceneNodeRef &node, float now)
Advance growth on a real scene leaf; returns true after removal, and rejects nodes with children.
void exposePcgGrowthBindings(ssq::Table &t)
Register Pcg Growth bindings.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.