Skip to content

Commit

Permalink
Change getAllEntitiesOnTile
Browse files Browse the repository at this point in the history
Change getAllEntitiesOnTile

- Make getAllEntitiesOnTile take a Tile object instead of two ints
- Remove obsolete type "peep" from getAllEntitiesOnTile
- Add comments
- Improve error message for invalid type argument
- Add error handling for invalid tile argument
-
  • Loading branch information
andrewpratt64 committed Nov 14, 2021
1 parent bebb54a commit ad43f93
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 55 deletions.
14 changes: 5 additions & 9 deletions distribution/openrct2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,15 +579,11 @@ declare global {
getAllEntities(type: "staff"): Staff[];
getAllEntities(type: "car"): Car[];
getAllEntities(type: "litter"): Litter[];
getAllEntitiesOnTile(type: EntityType, x: number, y: number): Entity[];
/**
* @deprecated since version 34, use guest or staff instead.
*/
getAllEntitiesOnTile(type: "peep", x: number, y: number): Peep[];
getAllEntitiesOnTile(type: "guest", x: number, y: number): Guest[];
getAllEntitiesOnTile(type: "staff", x: number, y: number): Staff[];
getAllEntitiesOnTile(type: "car", x: number, y: number): Car[];
getAllEntitiesOnTile(type: "litter", x: number, y: number): Litter[];
getAllEntitiesOnTile(type: EntityType, tile: Tile): Entity[];
getAllEntitiesOnTile(type: "guest", tile: Tile): Guest[];
getAllEntitiesOnTile(type: "staff", tile: Tile): Staff[];
getAllEntitiesOnTile(type: "car", tile: Tile): Car[];
getAllEntitiesOnTile(type: "litter", tile: Tile): Litter[];
createEntity(type: EntityType, initializer: object): Entity;
}

Expand Down
92 changes: 47 additions & 45 deletions src/openrct2/scripting/bindings/world/ScMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,70 +167,72 @@ namespace OpenRCT2::Scripting
return result;
}

std::vector<DukValue> OpenRCT2::Scripting::ScMap::getAllEntitiesOnTile(const std::string& type, int32_t x, int32_t y) const
std::vector<DukValue> OpenRCT2::Scripting::ScMap::getAllEntitiesOnTile(const std::string& type, const DukValue& tile) const
{
const auto pos = CoordsXY(x, y);

std::vector<DukValue> result;
if (type == "balloon")
try
{
for (auto sprite : EntityTileList<Balloon>(pos))
// Get the tile position
const auto pos = CoordsXY(tile["x"].as_int(), tile["y"].as_int());

// 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")
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->sprite_index)));
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))
else if (type == "car")
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScVehicle>(sprite->sprite_index)));
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))
else if (type == "litter")
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScLitter>(sprite->sprite_index)));
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))
else if (type == "duck")
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->sprite_index)));
}
}
else if (type == "peep")
{
for (auto sprite : EntityTileList<Guest>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->sprite_index)));
for (auto sprite : EntityTileList<Duck>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->sprite_index)));
}
}
for (auto sprite : EntityTileList<Staff>(pos))
else if (type == "guest")
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->sprite_index)));
for (auto sprite : EntityTileList<Guest>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->sprite_index)));
}
}
}
else if (type == "guest")
{
for (auto sprite : EntityTileList<Guest>(pos))
else if (type == "staff")
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->sprite_index)));
for (auto sprite : EntityTileList<Staff>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->sprite_index)));
}
}
}
else if (type == "staff")
{
for (auto sprite : EntityTileList<Staff>(pos))
else
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->sprite_index)));
// 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;
}
else
catch (const DukException&)
{
duk_error(_context, DUK_ERR_ERROR, "Invalid entity type.");
// Throw an error if an invalid Tile argument was passed
duk_error(_context, DUK_ERR_ERROR, "Invalid tile argument.");
}

return result;
}

template<typename TEntityType, typename TScriptType>
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/scripting/bindings/world/ScMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace OpenRCT2::Scripting

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

std::vector<DukValue> getAllEntitiesOnTile(const std::string& type, int32_t x, int32_t y) const;
std::vector<DukValue> getAllEntitiesOnTile(const std::string& type, const DukValue& tile) const;

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

Expand Down

0 comments on commit ad43f93

Please sign in to comment.