From 8c398813531d2d64b6ec93315dfd426c8376080b Mon Sep 17 00:00:00 2001 From: Bas Date: Fri, 19 Aug 2022 22:10:46 +0200 Subject: [PATCH] Implement plugin getter for subposition coordinates --- distribution/changelog.txt | 2 +- distribution/openrct2.d.ts | 25 +++++++++++++++---- .../bindings/ride/ScTrackSegment.cpp | 17 +++++++++++++ .../scripting/bindings/ride/ScTrackSegment.h | 13 ++++++++++ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/distribution/changelog.txt b/distribution/changelog.txt index fd8f2b9745..575550dbf6 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -6,7 +6,7 @@ - Feature: [#16662] Show a warning message when g2.dat is mismatched. - Feature: [#17107] Ride operating settings can be set via text input. - Feature: [#17638] Added Zero G rolls, medium loops and large corkscrews to the Hybrid and Single-Rail coasters. -- Feature: [#17821] [Plugin] Add API for track subposition length and vehicle subposition. +- Feature: [#17821] [Plugin] Add API for track subpositions and vehicle subposition. - Feature: [#17877] Add three real-life flying roller coaster colour schemes. - Feature: [#17900] Add “Classic Wooden Coaster” with shallow banked turns. - Feature: [#6570, #10860, #17929] Fully support RollerCoaster Tycoon Classic as a RCT2 base install path. diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index e73a71257c..ffd68e89e1 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -884,7 +884,7 @@ declare global { readonly imageId: number; readonly spriteNumImages: number; } - + /** * Represents the sprite groups of a vehicle */ @@ -915,7 +915,7 @@ declare global { readonly restraintAnimation?: SpriteGroup; readonly curvedLiftHill?: SpriteGroup; } - + /** * Represents a defined vehicle within a Ride object definition. */ @@ -1244,11 +1244,17 @@ declare global { * Gets a list of the elements that make up the track segment. */ readonly elements: TrackSegmentElement[]; - + /** * Gets a length of the subpositions list for this track segment. */ getSubpositionLength(subpositionType: number, direction: Direction): number; + + /** + * Gets all of the subpositions for this track segment. These subpositions are used for the + * pathing of vehicles when moving along the track. + */ + getSubpositions(subpositionType: number, direction: Direction): TrackSubposition[]; } enum TrackSlope { @@ -1268,7 +1274,16 @@ declare global { UpsideDown = 15 } - interface TrackSegmentElement extends CoordsXYZ { + interface TrackSegmentElement extends Readonly { + } + + /** + * A single subposition on a track piece. These subpositions are used for the pathing of vehicles + * when moving along the track. + */ + interface TrackSubposition extends Readonly { + readonly angle: TrackSlope; + readonly banking: TrackBanking; } interface TrackIterator { @@ -1473,7 +1488,7 @@ declare global { readonly remainingDistance: number; /** - * The type of subposition coordinates that this vehicle is using to find its + * The type of subposition coordinates that this vehicle is using to find its * position on the track. */ readonly subposition: number; diff --git a/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp b/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp index ad9c1653d2..f2a706b121 100644 --- a/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp +++ b/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp @@ -37,6 +37,7 @@ void ScTrackSegment::Register(duk_context* ctx) dukglue_register_property(ctx, &ScTrackSegment::endDirection_get, nullptr, "endDirection"); dukglue_register_property(ctx, &ScTrackSegment::length_get, nullptr, "length"); dukglue_register_method(ctx, &ScTrackSegment::getSubpositionLength, "getSubpositionLength"); + dukglue_register_method(ctx, &ScTrackSegment::getSubpositions, "getSubpositions"); } int32_t ScTrackSegment::type_get() const @@ -148,4 +149,20 @@ uint16_t ScTrackSegment::getSubpositionLength(uint8_t trackSubposition, uint8_t return vehicle_get_move_info_size(static_cast(trackSubposition), _type, direction); } +std::vector ScTrackSegment::getSubpositions(uint8_t trackSubposition, uint8_t direction) const +{ + const auto ctx = GetContext()->GetScriptEngine().GetContext(); + const uint16_t size = getSubpositionLength(trackSubposition, direction); + const uint16_t typeAndDirection = (_type << 2) | (direction & 3); + + std::vector result; + + for (auto idx = 0; idx < size; idx++) + { + result.push_back(ToDuk( + ctx, gTrackVehicleInfo[static_cast(trackSubposition)][typeAndDirection]->info[idx])); + } + return result; +} + #endif diff --git a/src/openrct2/scripting/bindings/ride/ScTrackSegment.h b/src/openrct2/scripting/bindings/ride/ScTrackSegment.h index 4a8f26f3ac..547705a418 100644 --- a/src/openrct2/scripting/bindings/ride/ScTrackSegment.h +++ b/src/openrct2/scripting/bindings/ride/ScTrackSegment.h @@ -19,6 +19,18 @@ namespace OpenRCT2::Scripting { + template<> inline DukValue ToDuk(duk_context* ctx, const rct_vehicle_info& value) + { + DukObject dukSubposition(ctx); + dukSubposition.Set("x", value.x); + dukSubposition.Set("y", value.y); + dukSubposition.Set("z", value.z); + dukSubposition.Set("direction", value.direction); + dukSubposition.Set("angle", value.Pitch); + dukSubposition.Set("banking", value.bank_rotation); + return dukSubposition.Take(); + } + class ScTrackSegment { private: @@ -45,6 +57,7 @@ namespace OpenRCT2::Scripting int32_t length_get() const; DukValue elements_get() const; uint16_t getSubpositionLength(uint8_t trackSubposition, uint8_t direction) const; + std::vector getSubpositions(uint8_t trackSubposition, uint8_t direction) const; }; } // namespace OpenRCT2::Scripting