diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index cb3b6329d1..e945771bfc 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -224,6 +224,8 @@ declare global { getAllObjects(type: ObjectType): LoadedObject[]; getAllObjects(type: "ride"): RideObject[]; + getTrackSegment(type: number): TrackSegment | undefined; + /** * Gets a random integer within the specified range using the game's pseudo- * random number generator. This is part of the game state and shared across @@ -1128,6 +1130,29 @@ declare global { exit: CoordsXYZD; } + interface TrackSegment { + /** + * The track segment type. + */ + readonly type: number; + + /** + * Gets the localised description of the track segment. + */ + readonly description: string; + + /** + * Gets a list of the elements that make up the track segment. + */ + readonly elements: TrackSegmentElement; + } + + interface TrackSegmentElement { + x: number; + y: number; + z: number; + } + type EntityType = "balloon" | "car" | diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 28cc879860..f0be46cdfe 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -468,6 +468,7 @@ + @@ -925,6 +926,7 @@ + diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index 070f611806..dc7251e8b4 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -413,6 +413,7 @@ void ScriptEngine::Initialise() ScRideObjectVehicle::Register(ctx); ScTile::Register(ctx); ScTileElement::Register(ctx); + ScTrackSegment::Register(ctx); ScEntity::Register(ctx); ScLitter::Register(ctx); ScVehicle::Register(ctx); diff --git a/src/openrct2/scripting/bindings/game/ScContext.hpp b/src/openrct2/scripting/bindings/game/ScContext.hpp index e2d960ac12..7abaa2f135 100644 --- a/src/openrct2/scripting/bindings/game/ScContext.hpp +++ b/src/openrct2/scripting/bindings/game/ScContext.hpp @@ -23,6 +23,7 @@ # include "../game/ScConfiguration.hpp" # include "../game/ScDisposable.hpp" # include "../object/ScObject.hpp" +# include "../ride/ScTrackSegment.h" # include # include @@ -218,6 +219,19 @@ namespace OpenRCT2::Scripting return result; } + DukValue getTrackSegment(track_type_t type) + { + auto ctx = GetContext()->GetScriptEngine().GetContext(); + if (type >= TrackElemType::Count) + { + return ToDuk(ctx, undefined); + } + else + { + return GetObjectAsDukValue(ctx, std::make_shared(type)); + } + } + int32_t getRandom(int32_t min, int32_t max) { ThrowIfGameStateNotMutable(); @@ -457,6 +471,7 @@ namespace OpenRCT2::Scripting dukglue_register_method(ctx, &ScContext::captureImage, "captureImage"); dukglue_register_method(ctx, &ScContext::getObject, "getObject"); dukglue_register_method(ctx, &ScContext::getAllObjects, "getAllObjects"); + dukglue_register_method(ctx, &ScContext::getTrackSegment, "getTrackSegment"); dukglue_register_method(ctx, &ScContext::getRandom, "getRandom"); dukglue_register_method_varargs(ctx, &ScContext::formatString, "formatString"); dukglue_register_method(ctx, &ScContext::subscribe, "subscribe"); diff --git a/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp b/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp new file mode 100644 index 0000000000..8b6d1d1bd4 --- /dev/null +++ b/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp @@ -0,0 +1,71 @@ +/***************************************************************************** + * Copyright (c) 2022 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#ifdef ENABLE_SCRIPTING + +# include "ScTrackSegment.h" + +# include "../../../Context.h" +# include "../../../ride/TrackData.h" +# include "../../ScriptEngine.h" + +using namespace OpenRCT2::Scripting; +using namespace OpenRCT2::TrackMetaData; + +ScTrackSegment::ScTrackSegment(track_type_t type) + : _type(type) +{ +} + +void ScTrackSegment::Register(duk_context* ctx) +{ + dukglue_register_property(ctx, &ScTrackSegment::type_get, nullptr, "type"); + dukglue_register_property(ctx, &ScTrackSegment::description_get, nullptr, "description"); + dukglue_register_property(ctx, &ScTrackSegment::elements_get, nullptr, "elements"); +} + +int32_t ScTrackSegment::type_get() const +{ + return _type; +} + +std::string ScTrackSegment::description_get() const +{ + const auto& ted = GetTrackElementDescriptor(_type); + return language_get_string(ted.Description); +} + +DukValue ScTrackSegment::elements_get() const +{ + auto& scriptEngine = GetContext()->GetScriptEngine(); + auto ctx = scriptEngine.GetContext(); + + const auto& ted = GetTrackElementDescriptor(_type); + + duk_push_array(ctx); + + duk_uarridx_t index = 0; + for (auto* block = ted.Block; block->index != 0xFF; block++) + { + duk_push_object(ctx); + duk_push_number(ctx, block->x); + duk_put_prop_string(ctx, -2, "x"); + duk_push_number(ctx, block->y); + duk_put_prop_string(ctx, -2, "y"); + duk_push_number(ctx, block->z); + duk_put_prop_string(ctx, -2, "z"); + + duk_put_prop_index(ctx, -2, index); + index++; + } + + return DukValue::take_from_stack(ctx); +} + +#endif diff --git a/src/openrct2/scripting/bindings/ride/ScTrackSegment.h b/src/openrct2/scripting/bindings/ride/ScTrackSegment.h new file mode 100644 index 0000000000..95449faa1e --- /dev/null +++ b/src/openrct2/scripting/bindings/ride/ScTrackSegment.h @@ -0,0 +1,40 @@ +/***************************************************************************** + * Copyright (c) 2022 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#ifdef ENABLE_SCRIPTING + +# include "../../../world/TileElement.h" +# include "../../Duktape.hpp" + +# include +# include + +namespace OpenRCT2::Scripting +{ + class ScTrackSegment + { + private: + track_type_t _type; + + public: + ScTrackSegment(track_type_t type); + + static void Register(duk_context* ctx); + + private: + int32_t type_get() const; + std::string description_get() const; + DukValue elements_get() const; + }; + +} // namespace OpenRCT2::Scripting + +#endif