From 7b5aeece5951963c4ca2193210298b1607af4e71 Mon Sep 17 00:00:00 2001 From: Bas Date: Sun, 14 Aug 2022 19:52:22 +0200 Subject: [PATCH 1/3] Implement plugin getters for subposition and subposition length --- distribution/changelog.txt | 1 + distribution/openrct2.d.ts | 13 ++++++++++++- src/openrct2/ride/Vehicle.cpp | 2 +- src/openrct2/ride/Vehicle.h | 1 + .../scripting/bindings/entity/ScVehicle.cpp | 7 +++++++ .../scripting/bindings/entity/ScVehicle.hpp | 2 ++ .../scripting/bindings/ride/ScTrackSegment.cpp | 7 +++++++ .../scripting/bindings/ride/ScTrackSegment.h | 1 + 8 files changed, 32 insertions(+), 2 deletions(-) diff --git a/distribution/changelog.txt b/distribution/changelog.txt index 0809bea578..fd8f2b9745 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -6,6 +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: [#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 61258556a9..e73a71257c 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -107,7 +107,7 @@ declare global { * The direction is between 0 and 3. */ interface CoordsXYZD extends CoordsXYZ { - direction: number; + direction: Direction; } /** @@ -1244,6 +1244,11 @@ 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; } enum TrackSlope { @@ -1467,6 +1472,12 @@ declare global { */ readonly remainingDistance: number; + /** + * The type of subposition coordinates that this vehicle is using to find its + * position on the track. + */ + readonly subposition: number; + /** * List of guest IDs ordered by seat. * @deprecated since version 34, use guests instead. diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index fd12a4397d..fe04774ac7 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -664,7 +664,7 @@ const rct_vehicle_info* Vehicle::GetMoveInfo() const return vehicle_get_move_info(TrackSubposition, GetTrackType(), GetTrackDirection(), track_progress); } -static uint16_t vehicle_get_move_info_size(VehicleTrackSubposition trackSubposition, track_type_t type, uint8_t direction) +uint16_t vehicle_get_move_info_size(VehicleTrackSubposition trackSubposition, track_type_t type, uint8_t direction) { uint16_t typeAndDirection = (type << 2) | (direction & 3); diff --git a/src/openrct2/ride/Vehicle.h b/src/openrct2/ride/Vehicle.h index 9f375850a1..2a03add9d9 100644 --- a/src/openrct2/ride/Vehicle.h +++ b/src/openrct2/ride/Vehicle.h @@ -526,6 +526,7 @@ enum Vehicle* try_get_vehicle(EntityId spriteIndex); void vehicle_update_all(); void vehicle_sounds_update(); +uint16_t vehicle_get_move_info_size(VehicleTrackSubposition trackSubposition, track_type_t type, uint8_t direction); void RideUpdateMeasurementsSpecialElements_Default(Ride* ride, const track_type_t trackType); void RideUpdateMeasurementsSpecialElements_MiniGolf(Ride* ride, const track_type_t trackType); diff --git a/src/openrct2/scripting/bindings/entity/ScVehicle.cpp b/src/openrct2/scripting/bindings/entity/ScVehicle.cpp index 9e681bd386..7007931fea 100644 --- a/src/openrct2/scripting/bindings/entity/ScVehicle.cpp +++ b/src/openrct2/scripting/bindings/entity/ScVehicle.cpp @@ -75,6 +75,7 @@ namespace OpenRCT2::Scripting dukglue_register_property(ctx, &ScVehicle::trackLocation_get, &ScVehicle::trackLocation_set, "trackLocation"); dukglue_register_property(ctx, &ScVehicle::trackProgress_get, nullptr, "trackProgress"); dukglue_register_property(ctx, &ScVehicle::remainingDistance_get, nullptr, "remainingDistance"); + dukglue_register_property(ctx, &ScVehicle::subposition_get, nullptr, "subposition"); dukglue_register_property( ctx, &ScVehicle::poweredAcceleration_get, &ScVehicle::poweredAcceleration_set, "poweredAcceleration"); dukglue_register_property(ctx, &ScVehicle::poweredMaxSpeed_get, &ScVehicle::poweredMaxSpeed_set, "poweredMaxSpeed"); @@ -386,6 +387,12 @@ namespace OpenRCT2::Scripting return vehicle != nullptr ? vehicle->remaining_distance : 0; } + uint8_t ScVehicle::subposition_get() const + { + auto vehicle = GetVehicle(); + return vehicle != nullptr ? static_cast(vehicle->TrackSubposition) : 0; + } + uint8_t ScVehicle::poweredAcceleration_get() const { auto vehicle = GetVehicle(); diff --git a/src/openrct2/scripting/bindings/entity/ScVehicle.hpp b/src/openrct2/scripting/bindings/entity/ScVehicle.hpp index 7c5c5170a7..8c5b91cd6e 100644 --- a/src/openrct2/scripting/bindings/entity/ScVehicle.hpp +++ b/src/openrct2/scripting/bindings/entity/ScVehicle.hpp @@ -77,6 +77,8 @@ namespace OpenRCT2::Scripting int32_t remainingDistance_get() const; + uint8_t subposition_get() const; + uint8_t poweredAcceleration_get() const; void poweredAcceleration_set(uint8_t value); diff --git a/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp b/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp index dc79a15a3e..ad9c1653d2 100644 --- a/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp +++ b/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp @@ -13,6 +13,7 @@ # include "../../../Context.h" # include "../../../ride/TrackData.h" +# include "../../../ride/Vehicle.h" # include "../../ScriptEngine.h" using namespace OpenRCT2::Scripting; @@ -35,6 +36,7 @@ void ScTrackSegment::Register(duk_context* ctx) dukglue_register_property(ctx, &ScTrackSegment::endZ_get, nullptr, "endZ"); 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"); } int32_t ScTrackSegment::type_get() const @@ -141,4 +143,9 @@ DukValue ScTrackSegment::elements_get() const return DukValue::take_from_stack(ctx); } +uint16_t ScTrackSegment::getSubpositionLength(uint8_t trackSubposition, uint8_t direction) const +{ + return vehicle_get_move_info_size(static_cast(trackSubposition), _type, direction); +} + #endif diff --git a/src/openrct2/scripting/bindings/ride/ScTrackSegment.h b/src/openrct2/scripting/bindings/ride/ScTrackSegment.h index 24277f500f..4a8f26f3ac 100644 --- a/src/openrct2/scripting/bindings/ride/ScTrackSegment.h +++ b/src/openrct2/scripting/bindings/ride/ScTrackSegment.h @@ -44,6 +44,7 @@ namespace OpenRCT2::Scripting int32_t endBank_get() const; int32_t length_get() const; DukValue elements_get() const; + uint16_t getSubpositionLength(uint8_t trackSubposition, uint8_t direction) const; }; } // namespace OpenRCT2::Scripting From 8c398813531d2d64b6ec93315dfd426c8376080b Mon Sep 17 00:00:00 2001 From: Bas Date: Fri, 19 Aug 2022 22:10:46 +0200 Subject: [PATCH 2/3] 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 From 885be637976c052dd20aa0daf0d82ca67d1a6345 Mon Sep 17 00:00:00 2001 From: Bas Date: Sun, 21 Aug 2022 22:08:22 +0200 Subject: [PATCH 3/3] Rename subposition rotations to yaw, pitch, roll --- distribution/openrct2.d.ts | 13 +++++++------ src/openrct2/scripting/ScriptEngine.h | 2 +- .../scripting/bindings/ride/ScTrackSegment.h | 6 +++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index ffd68e89e1..ceb687adf6 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -1245,12 +1245,12 @@ declare global { */ 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. */ @@ -1281,9 +1281,10 @@ declare global { * 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 TrackSubposition extends Readonly { + readonly yaw: number; + readonly pitch: TrackSlope; + readonly roll: TrackBanking; } interface TrackIterator { @@ -1489,7 +1490,7 @@ declare global { /** * The type of subposition coordinates that this vehicle is using to find its - * position on the track. + * position on the track. */ readonly subposition: number; diff --git a/src/openrct2/scripting/ScriptEngine.h b/src/openrct2/scripting/ScriptEngine.h index bd18d70a19..f5cbe26a17 100644 --- a/src/openrct2/scripting/ScriptEngine.h +++ b/src/openrct2/scripting/ScriptEngine.h @@ -46,7 +46,7 @@ namespace OpenRCT2 namespace OpenRCT2::Scripting { - static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 58; + static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 59; // Versions marking breaking changes. static constexpr int32_t API_VERSION_33_PEEP_DEPRECATION = 33; diff --git a/src/openrct2/scripting/bindings/ride/ScTrackSegment.h b/src/openrct2/scripting/bindings/ride/ScTrackSegment.h index 547705a418..d5791ecce6 100644 --- a/src/openrct2/scripting/bindings/ride/ScTrackSegment.h +++ b/src/openrct2/scripting/bindings/ride/ScTrackSegment.h @@ -25,9 +25,9 @@ namespace OpenRCT2::Scripting 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); + dukSubposition.Set("yaw", value.direction); + dukSubposition.Set("pitch", value.Pitch); + dukSubposition.Set("roll", value.bank_rotation); return dukSubposition.Take(); }