mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-23 14:54:30 +01:00
Add API for getting track segments
This commit is contained in:
25
distribution/openrct2.d.ts
vendored
25
distribution/openrct2.d.ts
vendored
@@ -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" |
|
||||
|
||||
@@ -468,6 +468,7 @@
|
||||
<ClInclude Include="scripting\bindings\network\ScPlayer.hpp" />
|
||||
<ClInclude Include="scripting\bindings\network\ScPlayerGroup.hpp" />
|
||||
<ClInclude Include="scripting\bindings\ride\ScRideStation.hpp" />
|
||||
<ClInclude Include="scripting\bindings\ride\ScTrackSegment.h" />
|
||||
<ClInclude Include="scripting\bindings\world\ScParkMessage.hpp" />
|
||||
<ClInclude Include="scripting\bindings\world\ScTileElement.hpp" />
|
||||
<ClInclude Include="scripting\Duktape.hpp" />
|
||||
@@ -925,6 +926,7 @@
|
||||
<ClCompile Include="scripting\bindings\network\ScPlayerGroup.cpp" />
|
||||
<ClCompile Include="scripting\bindings\ride\ScRide.cpp" />
|
||||
<ClCompile Include="scripting\bindings\ride\ScRideStation.cpp" />
|
||||
<ClCompile Include="scripting\bindings\ride\ScTrackSegment.cpp" />
|
||||
<ClCompile Include="scripting\bindings\world\ScMap.cpp" />
|
||||
<ClCompile Include="scripting\bindings\world\ScPark.cpp" />
|
||||
<ClCompile Include="scripting\bindings\world\ScParkMessage.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);
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
# include "../game/ScConfiguration.hpp"
|
||||
# include "../game/ScDisposable.hpp"
|
||||
# include "../object/ScObject.hpp"
|
||||
# include "../ride/ScTrackSegment.h"
|
||||
|
||||
# include <cstdio>
|
||||
# include <memory>
|
||||
@@ -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<ScTrackSegment>(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");
|
||||
|
||||
71
src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp
Normal file
71
src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp
Normal file
@@ -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
|
||||
40
src/openrct2/scripting/bindings/ride/ScTrackSegment.h
Normal file
40
src/openrct2/scripting/bindings/ride/ScTrackSegment.h
Normal file
@@ -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 <cstdint>
|
||||
# include <string>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user