Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to get all entities on a tile to plugin api #15901

Merged
merged 7 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to take TileCoords instead of CoordsXY, so each tile increments X and Y by only 1 instead of 32. To get all entities on tile {x: 70, y: 75} for example, you now have to supply {x: 70 * 32, y: 75 * 32}.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, tiles should use tile coordinates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of big coords and tile coords seems to be rather mixed in the API. I've seen a couple plugins use game-actions where they multiply every value with 32 to iterate over some tiles, and the getTile function on the map takes two arguments for x and y where they are tile coords instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this solved (and thus PR mergeable?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it still takes big coords instead of tile coords. I tried changing it so that the pos was multiplied by 32, but my build environment get a little messed up after the new save format update was released. Is there anything special I'm supposed to do to update it? Running from the game launcher works fine but Visual Studio logs a thousand "Object not found." errors and I think it tries to load the older format

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to do a full rebuild (from the msbuild window) to make it download the updated objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I tried that, but I'll try again. If that dosen't work I'll just delete the local repository and clone it again

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the use of TileCoords and Coords are a bit mixed in the API. This is better left for another issue.


// 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