Use TileElementsView for LargeSceneryRemoveAction and cleanup

This commit is contained in:
Matt 2021-02-05 17:10:17 +02:00
parent 5bd6ba25b7
commit 5e2e7143cd
No known key found for this signature in database
GPG Key ID: 6D4C24A61C93E208
2 changed files with 23 additions and 52 deletions

View File

@ -21,6 +21,9 @@
#include "../world/Park.h"
#include "../world/SmallScenery.h"
#include "../world/Sprite.h"
#include "../world/TileElementsView.h"
using namespace OpenRCT2;
LargeSceneryRemoveAction::LargeSceneryRemoveAction(const CoordsXYZD& location, uint16_t tileIndex)
: _loc(location)
@ -59,7 +62,7 @@ GameActions::Result::Ptr LargeSceneryRemoveAction::Query() const
res->Expenditure = ExpenditureType::Landscaping;
res->Cost = 0;
TileElement* tileElement = FindLargeSceneryElement();
TileElement* tileElement = FindLargeSceneryElement(_loc, _tileIndex);
if (tileElement == nullptr)
{
log_warning("Invalid game command for scenery removal, x = %d, y = %d", _loc.x, _loc.y);
@ -132,7 +135,7 @@ GameActions::Result::Ptr LargeSceneryRemoveAction::Execute() const
res->Expenditure = ExpenditureType::Landscaping;
res->Cost = 0;
TileElement* tileElement = FindLargeSceneryElement();
TileElement* tileElement = FindLargeSceneryElement(_loc, _tileIndex);
if (tileElement == nullptr)
{
log_warning("Invalid game command for scenery removal, x = %d, y = %d", _loc.x, _loc.y);
@ -171,37 +174,13 @@ GameActions::Result::Ptr LargeSceneryRemoveAction::Execute() const
}
}
TileElement* sceneryElement = map_get_first_element_at(currentTile);
bool element_found = false;
auto* sceneryElement = FindLargeSceneryElement(currentTile, i);
if (sceneryElement != nullptr)
{
do
{
if (sceneryElement->GetType() != TILE_ELEMENT_TYPE_LARGE_SCENERY)
continue;
if (sceneryElement->GetDirection() != _loc.direction)
continue;
if (sceneryElement->AsLargeScenery()->GetSequenceIndex() != i)
continue;
if (sceneryElement->GetBaseZ() != currentTile.z)
continue;
// If we are removing ghost elements
if ((flags & GAME_COMMAND_FLAG_GHOST) && sceneryElement->IsGhost() == false)
continue;
map_invalidate_tile_full(currentTile);
tile_element_remove(sceneryElement);
element_found = true;
break;
} while (!(sceneryElement++)->IsLastForTile());
map_invalidate_tile_full(currentTile);
tile_element_remove(sceneryElement);
}
if (element_found == false)
else
{
log_error("Tile not found when trying to remove element!");
}
@ -212,33 +191,25 @@ GameActions::Result::Ptr LargeSceneryRemoveAction::Execute() const
return res;
}
TileElement* LargeSceneryRemoveAction::FindLargeSceneryElement() const
TileElement* LargeSceneryRemoveAction::FindLargeSceneryElement(const CoordsXYZ& pos, int32_t sequenceIndex) const
{
TileElement* tileElement = map_get_first_element_at(_loc);
if (tileElement == nullptr)
return nullptr;
do
for (auto* sceneryElement : TileElementsView<LargeSceneryElement>(pos))
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_LARGE_SCENERY)
continue;
if (tileElement->GetBaseZ() != _loc.z)
continue;
if (tileElement->AsLargeScenery()->GetSequenceIndex() != _tileIndex)
continue;
if (tileElement->GetDirection() != _loc.direction)
continue;
// If we are removing ghost elements
if ((GetFlags() & GAME_COMMAND_FLAG_GHOST) && tileElement->IsGhost() == false)
if ((GetFlags() & GAME_COMMAND_FLAG_GHOST) && sceneryElement->IsGhost() == false)
continue;
return tileElement;
if (sceneryElement->GetDirection() != _loc.direction)
continue;
} while (!(tileElement++)->IsLastForTile());
if (sceneryElement->GetSequenceIndex() != sequenceIndex)
continue;
if (sceneryElement->GetBaseZ() != pos.z)
continue;
return sceneryElement->as<TileElement>();
}
return nullptr;
}

View File

@ -30,5 +30,5 @@ public:
GameActions::Result::Ptr Execute() const override;
private:
TileElement* FindLargeSceneryElement() const;
TileElement* FindLargeSceneryElement(const CoordsXYZ& pos, int32_t sequenceIndex) const;
};