From efb268af7d042914e7adf9ef3e95ef29b1d4cfad Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 18 Apr 2026 05:25:23 +0200 Subject: [PATCH] Add ride breakdown hook for scripting (#24243) --- distribution/changelog.txt | 1 + distribution/openrct2.d.ts | 6 +++++ src/openrct2/libopenrct2.vcxproj | 1 + src/openrct2/ride/Ride.cpp | 21 ++++++++++++++++ src/openrct2/ride/RideBreakdownMap.h | 25 +++++++++++++++++++ src/openrct2/scripting/HookEngine.cpp | 1 + src/openrct2/scripting/HookEngine.h | 1 + src/openrct2/scripting/ScriptEngine.h | 2 +- .../scripting/bindings/ride/ScRide.cpp | 21 ++++------------ 9 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 src/openrct2/ride/RideBreakdownMap.h diff --git a/distribution/changelog.txt b/distribution/changelog.txt index 23b42ef284..8fb6b02c13 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -1,5 +1,6 @@ 0.5.1 (in development) ------------------------------------------------------------------------ +- Feature: [#24242] [Plugin] Add ride-breakdown hooktype. - Fix: [#26352] Large scenery items are incorrectly labelled as 'banners' in the tile inspector. - Fix: [#26352] The label for path additions is using the wrong text colour in the tile inspector. - Fix: [#26360] Inverted Lay-down Roller Coaster helices are invisible when loading old saves. diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index 46bc9ce92c..5ebad59e9d 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -514,6 +514,7 @@ declare global { subscribe(hook: "network.join", callback: (e: NetworkEventArgs) => void): IDisposable; subscribe(hook: "network.leave", callback: (e: NetworkEventArgs) => void): IDisposable; subscribe(hook: "park.guest.softcap.calculate", callback: (e: ParkCalculateGuestCapArgs) => void): IDisposable; + subscribe(hook: "ride.breakdown", callback: (e: RideBreakdownArgs) => void): IDisposable; subscribe(hook: "ride.ratings.calculate", callback: (e: RideRatingsCalculateArgs) => void): IDisposable; subscribe(hook: "vehicle.crash", callback: (e: VehicleCrashArgs) => void): IDisposable; @@ -1641,6 +1642,11 @@ declare global { cancel: boolean; } + interface RideBreakdownArgs { + readonly rideId: number; + breakdownReason: string; + } + interface RideRatingsCalculateArgs { readonly rideId: number; excitement: number; diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 1a9312c891..63c859228c 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -462,6 +462,7 @@ + diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 4807bce2bb..fa22c55ab8 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -46,6 +46,7 @@ #include "../profiling/Profiling.h" #include "../rct1/RCT1.h" #include "../scenario/Scenario.h" +#include "../scripting/ScriptEngine.h" #include "../ui/WindowManager.h" #include "../util/Util.h" #include "../windows/Intent.h" @@ -61,6 +62,7 @@ #include "../world/tile_element/TrackElement.h" #include "CableLift.h" #include "RideAudio.h" +#include "RideBreakdownMap.h" #include "RideConstruction.h" #include "RideData.h" #include "RideEntry.h" @@ -80,6 +82,7 @@ using namespace OpenRCT2; using namespace OpenRCT2::TrackMetadata; +using namespace OpenRCT2::Scripting; static constexpr auto kRideModeBlockSectionedCounterpart = std::to_array( { @@ -1313,6 +1316,24 @@ void RidePrepareBreakdown(Ride& ride, Breakdown breakdownReason) default: break; } +#ifdef ENABLE_SCRIPTING + auto& hookEngine = GetContext()->GetScriptEngine().GetHookEngine(); + if (hookEngine.HasSubscriptions(HookType::rideBreakDown)) + { + auto ctx = GetContext()->GetScriptEngine().GetContext(); + JSValue obj = JS_NewObject(ctx); + + JS_SetPropertyStr(ctx, obj, "rideId", JS_NewInt32(ctx, ride.id.ToUnderlying())); + + auto it = kBreakdownMap.find(breakdownReason); + if (it != kBreakdownMap.end()) + JS_SetPropertyStr(ctx, obj, "breakdownReason", JS_NewString(ctx, it->first.data())); + else + JS_SetPropertyStr(ctx, obj, "breakdownReason", JS_NewString(ctx, "none")); + + hookEngine.Call(HookType::rideBreakDown, obj, true); + } +#endif } /** diff --git a/src/openrct2/ride/RideBreakdownMap.h b/src/openrct2/ride/RideBreakdownMap.h new file mode 100644 index 0000000000..280f1536b0 --- /dev/null +++ b/src/openrct2/ride/RideBreakdownMap.h @@ -0,0 +1,25 @@ +/***************************************************************************** + * Copyright (c) 2014-2026 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 + +#include "../core/EnumMap.hpp" +#include "Ride.h" + +#ifdef ENABLE_SCRIPTING +static const EnumMap kBreakdownMap( + { { "safety_cut_out", Breakdown::safetyCutOut }, + { "restraints_stuck_closed", Breakdown::restraintsStuckClosed }, + { "restraints_stuck_open", Breakdown::restraintsStuckOpen }, + { "doors_stuck_closed", Breakdown::doorsStuckClosed }, + { "doors_stuck_open", Breakdown::doorsStuckOpen }, + { "vehicle_malfunction", Breakdown::vehicleMalfunction }, + { "brakes_failure", Breakdown::brakesFailure }, + { "control_failure", Breakdown::controlFailure } }); +#endif diff --git a/src/openrct2/scripting/HookEngine.cpp b/src/openrct2/scripting/HookEngine.cpp index daf5b46977..18b44ff3e0 100644 --- a/src/openrct2/scripting/HookEngine.cpp +++ b/src/openrct2/scripting/HookEngine.cpp @@ -34,6 +34,7 @@ static const EnumMap HooksLookupTable( { "map.changed", HookType::mapChanged }, { "map.save", HookType::mapSave }, { "park.guest.softcap.calculate", HookType::parkCalculateGuestCap }, + { "ride.breakdown", HookType::rideBreakDown }, }); HookType OpenRCT2::Scripting::GetHookType(const std::string& name) diff --git a/src/openrct2/scripting/HookEngine.h b/src/openrct2/scripting/HookEngine.h index 13ea6c37b4..61a2a8d3b9 100644 --- a/src/openrct2/scripting/HookEngine.h +++ b/src/openrct2/scripting/HookEngine.h @@ -43,6 +43,7 @@ namespace OpenRCT2::Scripting mapChanged, mapSave, parkCalculateGuestCap, + rideBreakDown, count, notDefined = -1, }; diff --git a/src/openrct2/scripting/ScriptEngine.h b/src/openrct2/scripting/ScriptEngine.h index 71dbc9d094..7f6dccf136 100644 --- a/src/openrct2/scripting/ScriptEngine.h +++ b/src/openrct2/scripting/ScriptEngine.h @@ -42,7 +42,7 @@ namespace OpenRCT2 namespace OpenRCT2::Scripting { - static constexpr int32_t kPluginApiVersion = 111; + static constexpr int32_t kPluginApiVersion = 112; // Versions marking breaking changes. static constexpr int32_t kApiVersionPeepDeprecation = 33; diff --git a/src/openrct2/scripting/bindings/ride/ScRide.cpp b/src/openrct2/scripting/bindings/ride/ScRide.cpp index 4b6dc7a2b0..449d9335e1 100644 --- a/src/openrct2/scripting/bindings/ride/ScRide.cpp +++ b/src/openrct2/scripting/bindings/ride/ScRide.cpp @@ -15,24 +15,13 @@ #include "../../../core/EnumMap.hpp" #include "../../../core/UnitConversion.h" #include "../../../ride/Ride.h" + #include "../../../ride/RideBreakdownMap.h" #include "../../../ride/RideData.h" #include "../../ScriptEngine.h" #include "../object/ScObject.hpp" namespace OpenRCT2::Scripting { - static const EnumMap BreakdownMap( - { - { "safety_cut_out", Breakdown::safetyCutOut }, - { "restraints_stuck_closed", Breakdown::restraintsStuckClosed }, - { "restraints_stuck_open", Breakdown::restraintsStuckOpen }, - { "doors_stuck_closed", Breakdown::doorsStuckClosed }, - { "doors_stuck_open", Breakdown::doorsStuckOpen }, - { "vehicle_malfunction", Breakdown::vehicleMalfunction }, - { "brakes_failure", Breakdown::brakesFailure }, - { "control_failure", Breakdown::controlFailure }, - }); - void ScRide::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { @@ -787,8 +776,8 @@ namespace OpenRCT2::Scripting { return JSFromStdString(ctx, "none"); } - auto it = BreakdownMap.find(ride->breakdownReason); - if (it != BreakdownMap.end()) + auto it = kBreakdownMap.find(ride->breakdownReason); + if (it != kBreakdownMap.end()) return JSFromStdString(ctx, std::string(it->first)); } return JSFromStdString(ctx, ""); @@ -802,8 +791,8 @@ namespace OpenRCT2::Scripting auto ride = GetRide(thisVal); if (ride != nullptr && ride->canBreakDown() && ride->status == RideStatus::open) { - auto it = BreakdownMap.find(breakDown); - if (it != BreakdownMap.end()) + auto it = kBreakdownMap.find(breakDown); + if (it != kBreakdownMap.end()) { RidePrepareBreakdown(*ride, it->second); }