载入中...
搜索中...
未找到
OceanInteractionBindings.cpp
浏览该文件的文档.
4#include "physics/Body3D.h"
5
6namespace eve::fluids {
7using namespace ocean_script;
8namespace {
9struct WakeBuilder {
10 OceanWakeSettings settings;
11};
12struct BoatBuilder {
13 OceanHullSettings settings;
14};
15} // namespace
16
17void exposeOceanInteractionTypes(ssq::Table& table) {
18 const auto vm = table.getHandle();
19 auto wakeBuilder = table.addClass<WakeBuilder>(
20 "OceanWakeBuilder", std::function<WakeBuilder*()>([] { return new WakeBuilder(); }), true);
21 wakeBuilder.addFunc("settings", [vm](WakeBuilder* self) {
23 });
24 wakeBuilder.addFunc("configure", [vm](WakeBuilder* self, ssq::Object object) {
25 auto value = script::valueFromSquirrel(object);
26 if (!value) return script::projectStatusResult(vm, value.status());
27 auto decoded = decodeOceanWakeSettings(value.value());
28 if (!decoded) return script::projectStatusResult(vm, decoded.status());
29 self->settings = std::move(decoded).takeValue();
31 });
32 wakeBuilder.addFunc("build",
33 [vm](WakeBuilder* self) { return ownedPointerResult(vm, OceanWake::create(self->settings)); });
34 auto wake = table.addClass<OceanWake>("OceanWake", std::function<OceanWake*()>([] { return nullptr; }), true);
35 table.addClass<OceanWakeStep>("OceanWakeStep", std::function<OceanWakeStep*()>([] { return nullptr; }), true);
36 wake.addFunc("prepareStep", [vm](OceanWake* self, OceanSpectrumFrame* frame, SQInteger endTick, SQInteger rate) {
37 if (!frame) return script::projectStatusResult(vm, invalid("Missing spectral frame"));
38 auto time = fixedTime(endTick, rate);
39 if (!time) return script::projectStatusResult(vm, time.status());
40 return ownedPointerResult(vm, self->prepareStep(time.value().dt, time.value().time, *frame));
41 });
42 wake.addFunc("commitStep", [vm](OceanWake* self, OceanWakeStep* step) {
43 if (!step) return script::projectStatusResult(vm, invalid("Missing wake step candidate"));
44 return script::projectResult(vm, self->commitStep(*step));
45 });
46 wake.addFunc("recenter",
47 [vm](OceanWake* self, float x, float z) { return script::projectResult(vm, self->recenter({x, z})); });
48 wake.addFunc("sampleSurface", [vm](OceanWake* self, OceanSpectrumFrame* frame, float x, float z) {
49 if (!frame) return script::projectStatusResult(vm, invalid("Missing spectral frame"));
50 return script::projectResult(vm, OceanWaterView(*frame, *self).sample({x, z}), sampleValue);
51 });
52 wake.addFunc("sample", [vm](OceanWake* self, float x, float z) {
53 return script::projectResult(vm, self->sample({x, z}), [](const OceanWakeSample& s) {
54 return Value(Value::Object{{"height", Value(s.height)},
55 {"velocity", Value(s.velocity)},
56 {"gradient", Value::Array{Value(s.gradient.x), Value(s.gradient.y)}},
57 {"foam", Value(s.foam)}});
58 });
59 });
60 wake.addFunc("injectReceipt", [vm](OceanWake* self, OceanBoatReceipt* receipt, SQInteger rate) {
61 if (!receipt) return script::projectStatusResult(vm, invalid("Missing boat receipt"));
62 auto time = fixedTime(0, rate);
63 if (!time) return script::projectStatusResult(vm, time.status());
64 return script::projectResult(vm, injectOceanBoatWake(*self, *receipt, time.value().dt));
65 });
66
67 auto boatBuilder = table.addClass<BoatBuilder>(
68 "OceanBoatBuilder", std::function<BoatBuilder*()>([] { return new BoatBuilder(); }), true);
69 boatBuilder.addFunc("settings", [vm](BoatBuilder* self) {
71 });
72 boatBuilder.addFunc("configure", [vm](BoatBuilder* self, ssq::Object object) {
73 auto value = script::valueFromSquirrel(object);
74 if (!value) return script::projectStatusResult(vm, value.status());
75 auto decoded = decodeOceanHullSettings(value.value());
76 if (!decoded) return script::projectStatusResult(vm, decoded.status());
77 self->settings = std::move(decoded).takeValue();
79 });
80 boatBuilder.addFunc("build", [vm](BoatBuilder* self, physics::Body3D* body) {
81 if (!body) return script::projectStatusResult(vm, invalid("Missing live physics body"));
82 return ownedPointerResult(vm, OceanBoat::create(*body, self->settings));
83 });
84 auto boat = table.addClass<OceanBoat>("OceanBoat", std::function<OceanBoat*()>([] { return nullptr; }), true);
85 boat.addFunc("apply", [vm](OceanBoat* self, OceanSpectrumFrame* frame, ssq::Object wakeObject, SQInteger rate,
86 float throttle, float rudder) {
87 if (!frame) return script::projectStatusResult(vm, invalid("Missing spectral frame"));
88 auto time = fixedTime(0, rate);
89 if (!time) return script::projectStatusResult(vm, time.status());
90 const auto* localWake = wakeObject.isNull() ? nullptr : wakeObject.to<OceanWake*>();
91 const auto water = localWake ? OceanWaterView(*frame, *localWake) : OceanWaterView(*frame);
92 return ownedResult(vm, self->apply(water, time.value().dt, throttle, rudder));
93 });
94 auto receipt = table.addClass<OceanBoatReceipt>("OceanBoatReceipt",
95 std::function<OceanBoatReceipt*()>([] { return nullptr; }), true);
96 receipt.addFunc("summary", [vm](OceanBoatReceipt* self) {
99 Value::Object{{"time", Value(self->time)},
100 {"totalForce", vector(self->totalForce)},
101 {"submergedVolume", Value(self->submergedVolume)},
102 {"contacts", Value(static_cast<std::int64_t>(self->contacts.size()))}});
103 });
104}
105} // namespace eve::fluids
double z
double x
const std::string & s
HSQUIRRELVM vm
Definition ECS.cpp:38
OceanWakeSettings settings
ssq::Object wake
ssq::Object frame
ssq::Object water
std::string body
Object::Ptr value
float step
Definition TreeMesh.cpp:451
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
std::map< std::string, Value > Object
Definition Value.h:34
bool isNull() const noexcept
Return true when this value is null.
Definition Value.h:98
static EVENGINE_API_DOMAINS Result< std::unique_ptr< OceanBoat > > create(physics::Body3D &body, const OceanHullSettings &settings)
Validate/copy a bounded hull and attach to a live dynamic body.
Definition OceanBoat.cpp:31
Immutable spatial projection at one simulation timestamp, independent of its spectrum owner....
Owning, single-use candidate; no source frame, owner pointer or GPU resource is retained....
Definition OceanWake.h:119
Caller-owned wave field with finite-depth gravity dispersion and absorbing boundaries....
Definition OceanWake.h:53
static EVENGINE_API_DOMAINS Result< std::unique_ptr< OceanWake > > create(const OceanWakeSettings &settings)
Construct a checked field via explicit settings; copies settings and retains no input.
Definition OceanWake.cpp:30
Synchronous borrowed composition of the spectral sea and an optional local wake. Both owners must out...
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
ssq::Table ownedResult(HSQUIRRELVM vm, Result< T > result)
ssq::Table ownedPointerResult(HSQUIRRELVM vm, Result< std::unique_ptr< T > > result)
Result< FixedTime > fixedTime(SQInteger tick, SQInteger rate)
Status invalid(const char *message)
Value sampleValue(const OceanSurfaceSample &sample)
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
EVENGINE_API_DOMAINS void exposeOceanInteractionTypes(ssq::Table &table)
Register owning wake/boat adapters; VM owner thread, call-only table, no runtime objects retained.
Value encodeOceanHullSettings(const OceanHullSettings &s)
Encode owning transient hull arguments; physics links are deliberately absent. @thread Const/reentran...
Result< OceanHullSettings > decodeOceanHullSettings(const Value &value)
Strict structural decode of at most 64 cells; OceanBoat::create validates all physical domains.
Value encodeOceanWakeSettings(const OceanWakeSettings &s)
Encode owning transient wake construction arguments; not a persistent checkpoint. @thread Const/reent...
Result< OceanWakeSettings > decodeOceanWakeSettings(const Value &value)
Strict bounded structural decode; native OceanWake::create owns domain validation.
EVENGINE_API_DOMAINS Result< void > injectOceanBoatWake(OceanWake &wake, const OceanBoatReceipt &receipt, double dt)
Convert wet hull motion to bounded bow/stern dipoles and vertical impacts in a local wave field.
std::unordered_map< std::string, SkillDefinition > & table()
Definition Skill.cpp:73
ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status &status)
Project a checked native status that carries no payload.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
Result< Value > valueFromSquirrel(HSQUIRRELVM vm, SQInteger index, const SquirrelValueOptions &options)
Convert one Squirrel value into the canonical owning Value tree.
Local world-space height perturbation, world slope and vertical particle speed.
Definition OceanWake.h:35