diff --git a/contributors.md b/contributors.md index 76e13142bc..181b88237f 100644 --- a/contributors.md +++ b/contributors.md @@ -93,7 +93,7 @@ The following people are not part of the development team, but have been contrib * Keith Stellyes (keithstellyes) - Misc. * Bas Cantrijn (Basssiiie) - Various plugin additions, misc. * Adrian Zdanowicz (CookiePLMonster) - Misc. -* Andrew Pratt (andrewpratt64) - Added api hook for vehicle crashes +* Andrew Pratt (andrewpratt64) - Added api hook for vehicle crashes, api function to get entities on a tile ## Bug fixes * (KirilAngelov) diff --git a/distribution/changelog.txt b/distribution/changelog.txt index 762c819353..3712ef0473 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -11,6 +11,7 @@ - Feature: [#13858] Flatride bases can be made invisible. - Feature: [#14676] [Plugin] Allow plugins to store data in .park files. - Feature: [#15367] Individual track elements can now be drawn as another ride type. +- Feature: [#15901] [Plugin] Add map.getAllEntitiesOnTile to API. - Feature: [#16029] [Plugin] Add TrackElement.rideType to API. - Feature: [#16097] The Looping Roller Coaster can now draw all elements from the LIM Launched Roller Coaster. - Feature: [#16132, #16389] The Corkscrew, Twister and Vertical Drop Roller Coasters can now draw inline twists. diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index 5dd80aad1c..4608c50f50 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -604,6 +604,11 @@ declare global { getAllEntities(type: "staff"): Staff[]; getAllEntities(type: "car"): Car[]; getAllEntities(type: "litter"): Litter[]; + getAllEntitiesOnTile(type: EntityType, tilePos: CoordsXY): Entity[]; + getAllEntitiesOnTile(type: "guest", tilePos: CoordsXY): Guest[]; + getAllEntitiesOnTile(type: "staff", tilePos: CoordsXY): Staff[]; + getAllEntitiesOnTile(type: "car", tilePos: CoordsXY): Car[]; + getAllEntitiesOnTile(type: "litter", tilePos: CoordsXY): Litter[]; createEntity(type: EntityType, initializer: object): Entity; } diff --git a/src/openrct2/scripting/ScriptEngine.h b/src/openrct2/scripting/ScriptEngine.h index 00b1f053fb..ffe723e6a5 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 = 48; + static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 49; // Versions marking breaking changes. static constexpr int32_t API_VERSION_33_PEEP_DEPRECATION = 33; diff --git a/src/openrct2/scripting/bindings/world/ScMap.cpp b/src/openrct2/scripting/bindings/world/ScMap.cpp index c811d24c30..e4bcfd123b 100644 --- a/src/openrct2/scripting/bindings/world/ScMap.cpp +++ b/src/openrct2/scripting/bindings/world/ScMap.cpp @@ -167,6 +167,67 @@ namespace OpenRCT2::Scripting return result; } + std::vector OpenRCT2::Scripting::ScMap::getAllEntitiesOnTile( + const std::string& type, const DukValue& tilePos) const + { + // Get the tile position + const auto pos = FromDuk(tilePos); + + // Declare a vector that will hold the result to return + std::vector result; + + // Use EntityTileList to iterate all entities of the given type on the tile, and push them to result + if (type == "balloon") + { + for (auto sprite : EntityTileList(pos)) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "car") + { + for (auto sprite : EntityTileList(pos)) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "litter") + { + for (auto sprite : EntityTileList(pos)) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "duck") + { + for (auto sprite : EntityTileList(pos)) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "guest") + { + for (auto sprite : EntityTileList(pos)) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else if (type == "staff") + { + for (auto sprite : EntityTileList(pos)) + { + result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); + } + } + else + { + // If the given type isn't valid, throw an error + duk_error(_context, DUK_ERR_ERROR, "Invalid entity type: %s", type.c_str()); + } + + return result; + } + template DukValue createEntityType(duk_context* ctx, const DukValue& initializer) { @@ -252,6 +313,7 @@ namespace OpenRCT2::Scripting dukglue_register_method(ctx, &ScMap::getTile, "getTile"); dukglue_register_method(ctx, &ScMap::getEntity, "getEntity"); dukglue_register_method(ctx, &ScMap::getAllEntities, "getAllEntities"); + dukglue_register_method(ctx, &ScMap::getAllEntitiesOnTile, "getAllEntitiesOnTile"); dukglue_register_method(ctx, &ScMap::createEntity, "createEntity"); } diff --git a/src/openrct2/scripting/bindings/world/ScMap.hpp b/src/openrct2/scripting/bindings/world/ScMap.hpp index 2d6ca62549..02c1c856e9 100644 --- a/src/openrct2/scripting/bindings/world/ScMap.hpp +++ b/src/openrct2/scripting/bindings/world/ScMap.hpp @@ -42,6 +42,8 @@ namespace OpenRCT2::Scripting std::vector getAllEntities(const std::string& type) const; + std::vector getAllEntitiesOnTile(const std::string& type, const DukValue& tilePos) const; + DukValue createEntity(const std::string& type, const DukValue& initializer); static void Register(duk_context* ctx);