From 78785fffbe0fb1618e4c2bbce1e066a4cd0e1ad1 Mon Sep 17 00:00:00 2001 From: Felix Date: Sat, 6 Mar 2021 23:25:26 +0100 Subject: [PATCH] Add plugin API for vehicle g-forces --- distribution/openrct2.d.ts | 13 +++++++++++++ src/openrct2/scripting/Duktape.hpp | 9 +++++++++ src/openrct2/scripting/ScEntity.hpp | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index 309b53eb27..6e1c81b921 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -114,6 +114,14 @@ declare global { rightBottom: CoordsXY; } + /** + * Represents lateral and vertical g-forces. + */ + interface GForces { + lateralG: number; + verticalG: number; + } + /** * Represents information about the plugin such as type, name, author and version. * It also includes the entry point. @@ -1153,6 +1161,11 @@ declare global { */ trackLocation: CoordsXYZD; + /** + * The current g-forces of this car. + */ + gForces: GForces; + /** * The progress on the current track piece, in steps. */ diff --git a/src/openrct2/scripting/Duktape.hpp b/src/openrct2/scripting/Duktape.hpp index 73cddc4e52..b62310a47b 100644 --- a/src/openrct2/scripting/Duktape.hpp +++ b/src/openrct2/scripting/Duktape.hpp @@ -13,6 +13,7 @@ # include "../core/Console.hpp" # include "../world/Map.h" +# include "../ride/Vehicle.h" # include # include @@ -408,6 +409,14 @@ namespace OpenRCT2::Scripting } } + template<> inline DukValue ToDuk(duk_context* ctx, const GForces& value) + { + DukObject dukGForces(ctx); + dukGForces.Set("lateralG", value.LateralG); + dukGForces.Set("verticalG", value.VerticalG); + return dukGForces.Take(); + } + template<> inline CoordsXYZD FromDuk(const DukValue& value) { CoordsXYZD result; diff --git a/src/openrct2/scripting/ScEntity.hpp b/src/openrct2/scripting/ScEntity.hpp index c9aded3b04..d5cb5aa1aa 100644 --- a/src/openrct2/scripting/ScEntity.hpp +++ b/src/openrct2/scripting/ScEntity.hpp @@ -270,6 +270,7 @@ namespace OpenRCT2::Scripting dukglue_register_property(ctx, &ScVehicle::poweredMaxSpeed_get, &ScVehicle::poweredMaxSpeed_set, "poweredMaxSpeed"); dukglue_register_property(ctx, &ScVehicle::status_get, &ScVehicle::status_set, "status"); dukglue_register_property(ctx, &ScVehicle::peeps_get, nullptr, "peeps"); + dukglue_register_property(ctx, &ScVehicle::gForces_get, nullptr, "gForces"); dukglue_register_method(ctx, &ScVehicle::travelBy, "travelBy"); } @@ -627,6 +628,18 @@ namespace OpenRCT2::Scripting return result; } + DukValue gForces_get() const + { + auto ctx = GetContext()->GetScriptEngine().GetContext(); + auto vehicle = GetVehicle(); + if (vehicle != nullptr) + { + GForces gForces = vehicle->GetGForces(); + return ToDuk(ctx, gForces); + } + return ToDuk(ctx, nullptr); + } + void travelBy(int32_t value) { ThrowIfGameStateNotMutable();