Skip to content

Commit

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

- Use FromDuk instead of manually creating a CoordsXY object
- Remove now-obselete try catch block
  • Loading branch information
andrewpratt64 authored and Broxzier committed Mar 20, 2022
1 parent 9ba4fc1 commit b36dd65
Showing 1 changed file with 39 additions and 47 deletions.
86 changes: 39 additions & 47 deletions src/openrct2/scripting/bindings/world/ScMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,70 +170,62 @@ namespace OpenRCT2::Scripting
std::vector<DukValue> OpenRCT2::Scripting::ScMap::getAllEntitiesOnTile(
const std::string& type, const DukValue& tilePos) const
{
try
{
// Get the tile position
const auto pos = CoordsXY(tilePos["x"].as_int(), tilePos["y"].as_int());
// Get the tile position
const auto pos = FromDuk<CoordsXY>(tilePos);

// Declare a vector that will hold the result to return
std::vector<DukValue> result;
// 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")
// 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))
{
for (auto sprite : EntityTileList<Vehicle>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScVehicle>(sprite->sprite_index)));
}
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->sprite_index)));
}
else if (type == "litter")
}
else if (type == "car")
{
for (auto sprite : EntityTileList<Vehicle>(pos))
{
for (auto sprite : EntityTileList<Litter>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScLitter>(sprite->sprite_index)));
}
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScVehicle>(sprite->sprite_index)));
}
else if (type == "duck")
}
else if (type == "litter")
{
for (auto sprite : EntityTileList<Litter>(pos))
{
for (auto sprite : EntityTileList<Duck>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->sprite_index)));
}
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScLitter>(sprite->sprite_index)));
}
else if (type == "guest")
}
else if (type == "duck")
{
for (auto sprite : EntityTileList<Duck>(pos))
{
for (auto sprite : EntityTileList<Guest>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->sprite_index)));
}
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScEntity>(sprite->sprite_index)));
}
else if (type == "staff")
}
else if (type == "guest")
{
for (auto sprite : EntityTileList<Guest>(pos))
{
for (auto sprite : EntityTileList<Staff>(pos))
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->sprite_index)));
}
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->sprite_index)));
}
else
}
else if (type == "staff")
{
for (auto sprite : EntityTileList<Staff>(pos))
{
// If the given type isn't valid, throw an error
duk_error(_context, DUK_ERR_ERROR, "Invalid entity type: %s", type.c_str());
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->sprite_index)));
}

return result;
}
catch (const DukException&)
else
{
// Throw an error if an invalid tilePos argument was passed
duk_error(_context, DUK_ERR_ERROR, "Invalid tilePos argument.");
// 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>
Expand Down

0 comments on commit b36dd65

Please sign in to comment.