Skip to content

Commit

Permalink
Add function to get all entities on a tile to plugin api
Browse files Browse the repository at this point in the history
Co-authored-by: Hielke Morsink <hielke.morsink@gmail.com>
  • Loading branch information
andrewpratt64 and Broxzier committed Mar 20, 2022
1 parent ea435f9 commit 5ea89b9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions distribution/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions distribution/openrct2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/scripting/ScriptEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 62 additions & 0 deletions src/openrct2/scripting/bindings/world/ScMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,67 @@ namespace OpenRCT2::Scripting
return result;
}

std::vector<DukValue> OpenRCT2::Scripting::ScMap::getAllEntitiesOnTile(
const std::string& type, const DukValue& tilePos) const
{
// Get the tile position
const auto pos = FromDuk<CoordsXY>(tilePos);

// Declare a vector that will hold the result to return
std::vector<DukValue> 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<Balloon>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->sprite_index)));
}
}
else if (type == "car")
{
for (auto sprite : EntityTileList<Vehicle>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScVehicle>(sprite->sprite_index)));
}
}
else if (type == "litter")
{
for (auto sprite : EntityTileList<Litter>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScLitter>(sprite->sprite_index)));
}
}
else if (type == "duck")
{
for (auto sprite : EntityTileList<Duck>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->sprite_index)));
}
}
else if (type == "guest")
{
for (auto sprite : EntityTileList<Guest>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->sprite_index)));
}
}
else if (type == "staff")
{
for (auto sprite : EntityTileList<Staff>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(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<typename TEntityType, typename TScriptType>
DukValue createEntityType(duk_context* ctx, const DukValue& initializer)
{
Expand Down Expand Up @@ -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");
}

Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/scripting/bindings/world/ScMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace OpenRCT2::Scripting

std::vector<DukValue> getAllEntities(const std::string& type) const;

std::vector<DukValue> getAllEntitiesOnTile(const std::string& type, const DukValue& tilePos) const;

DukValue createEntity(const std::string& type, const DukValue& initializer);

static void Register(duk_context* ctx);
Expand Down

0 comments on commit 5ea89b9

Please sign in to comment.