Files
OpenRCT2-Web/src/openrct2/world/MapAnimation.cpp
T

426 lines
13 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 23:17:03 +02:00
#include "MapAnimation.h"
2018-03-14 12:44:16 +01:00
#include "../Context.h"
2024-07-09 16:23:31 +02:00
#include "../Diagnostic.h"
2017-11-30 18:17:06 +01:00
#include "../Game.h"
#include "../GameState.h"
2021-11-24 16:48:33 +02:00
#include "../entity/EntityList.h"
2021-11-25 23:47:24 +02:00
#include "../entity/Peep.h"
2018-06-22 23:17:03 +02:00
#include "../interface/Viewport.h"
2023-01-26 18:44:42 +00:00
#include "../object/LargeSceneryEntry.h"
#include "../object/SmallSceneryEntry.h"
#include "../object/StationObject.h"
2023-01-25 19:56:29 +00:00
#include "../object/WallSceneryEntry.h"
2022-01-18 10:21:20 -08:00
#include "../profiling/Profiling.h"
2017-12-31 13:21:34 +01:00
#include "../ride/Ride.h"
2018-01-10 00:00:09 +01:00
#include "../ride/RideData.h"
2017-10-17 13:51:47 +02:00
#include "../ride/Track.h"
2019-02-14 22:41:48 +01:00
#include "Banner.h"
2018-06-22 23:17:03 +02:00
#include "Footpath.h"
2018-01-11 10:59:26 +01:00
#include "Scenery.h"
2024-10-03 12:42:53 +02:00
#include "tile_element/EntranceElement.h"
2024-10-15 22:02:31 +02:00
#include "tile_element/LargeSceneryElement.h"
#include "tile_element/PathElement.h"
#include "tile_element/SmallSceneryElement.h"
#include "tile_element/TileElement.h"
#include "tile_element/TrackElement.h"
#include "tile_element/WallElement.h"
2018-06-22 23:17:03 +02:00
#include <algorithm>
#include <set>
using namespace OpenRCT2;
struct TileCoordsXYCmp
{
constexpr bool operator()(const TileCoordsXY& lhs, const TileCoordsXY& rhs) const noexcept
{
// NOTE: Ordering should match GetFirstElementAt which is x + (y * size)
return std::tie(lhs.y, lhs.x) < std::tie(rhs.y, rhs.x);
}
};
// A list of tile coordinates which contains animated tile elements.
static std::set<TileCoordsXY, TileCoordsXYCmp> _mapAnimations;
static bool UpdateEntranceAnimation(const EntranceElement& entrance, const CoordsXYZ& loc, const int32_t baseZ)
{
if (entrance.GetEntranceType() == ENTRANCE_TYPE_RIDE_ENTRANCE)
{
const auto ride = GetRide(entrance.GetRideIndex());
if (ride != nullptr)
{
const auto stationObj = ride->getStationObject();
if (stationObj != nullptr)
{
MapInvalidateTileZoom1({ loc, baseZ + stationObj->Height + 8, baseZ + stationObj->Height + 24 });
return true;
}
}
}
else if (entrance.GetEntranceType() == ENTRANCE_TYPE_PARK_ENTRANCE && entrance.GetSequenceIndex() == 0)
{
const int32_t direction = (entrance.GetDirection() + GetCurrentRotation()) & 3;
if (direction == TILE_ELEMENT_DIRECTION_SOUTH || direction == TILE_ELEMENT_DIRECTION_WEST)
{
MapInvalidateTileZoom1({ loc, baseZ + 32, baseZ + 64 });
}
return true;
}
return false;
}
static bool UpdatePathAnimation(const PathElement& path, const CoordsXYZ& loc, const int32_t baseZ)
{
if (path.IsQueue() && path.HasQueueBanner())
{
const int32_t direction = (path.GetQueueBannerDirection() + GetCurrentRotation()) & 3;
if (direction == TILE_ELEMENT_DIRECTION_NORTH || direction == TILE_ELEMENT_DIRECTION_EAST)
{
MapInvalidateTileZoom1({ loc, baseZ + 16, baseZ + 30 });
}
return true;
}
return false;
}
static bool UpdateSmallSceneryAnimation(const SmallSceneryElement& scenery, const CoordsXYZ& loc, const int32_t baseZ)
{
const auto entry = scenery.GetEntry();
if (entry == nullptr)
2018-06-22 23:17:03 +02:00
{
2017-06-06 23:24:18 +02:00
return false;
}
if (entry->HasFlag(
SMALL_SCENERY_FLAG_FOUNTAIN_SPRAY_1 | SMALL_SCENERY_FLAG_FOUNTAIN_SPRAY_4 | SMALL_SCENERY_FLAG_SWAMP_GOO
| SMALL_SCENERY_FLAG_HAS_FRAME_OFFSETS | SMALL_SCENERY_FLAG_IS_CLOCK))
2018-06-22 23:17:03 +02:00
{
// Don't apply anything to peeps when the scenery is a ghost.
if (!scenery.IsGhost())
2018-06-22 23:17:03 +02:00
{
if (entry->HasFlag(SMALL_SCENERY_FLAG_IS_CLOCK) && !(getGameState().currentTicks & 0x3FF))
2018-06-22 23:17:03 +02:00
{
const int32_t direction = scenery.GetDirection();
auto quad = EntityTileList<Peep>(CoordsXY{ loc.x, loc.y } - CoordsDirectionDelta[direction]);
2020-06-08 20:13:50 +01:00
for (auto peep : quad)
2018-06-22 23:17:03 +02:00
{
if (peep->State != PeepState::Walking || peep->z != baseZ || peep->Action < PeepActionType::Idle)
2017-06-06 23:24:18 +02:00
continue;
peep->Action = PeepActionType::CheckTime;
2024-09-22 16:12:04 +02:00
peep->AnimationFrameNum = 0;
peep->AnimationImageIdOffset = 0;
peep->UpdateCurrentAnimationType();
peep->Invalidate();
2017-06-06 23:24:18 +02:00
break;
}
}
}
const auto clearZ = scenery.GetClearanceZ();
MapInvalidateTileZoom1({ loc, baseZ, clearZ });
return true;
}
2017-10-15 15:53:16 +02:00
return false;
}
static bool UpdateTrackAnimation(TrackElement& track, const CoordsXYZ& loc, const int32_t baseZ)
{
const auto clearZ = track.GetClearanceZ();
switch (track.GetTrackType())
2018-06-22 23:17:03 +02:00
{
case TrackElemType::Waterfall:
MapInvalidateTileZoom1({ loc, baseZ + 14, baseZ + 46 });
return true;
case TrackElemType::Rapids:
case TrackElemType::Whirlpool:
MapInvalidateTileZoom1({ loc, baseZ + 14, baseZ + 18 });
return true;
case TrackElemType::SpinningTunnel:
MapInvalidateTileZoom1({ loc, baseZ + 14, baseZ + 32 });
return true;
case TrackElemType::OnRidePhoto:
if (track.IsTakingPhoto())
{
MapInvalidateTileZoom1({ loc, baseZ, clearZ });
track.DecrementPhotoTimeout();
return true;
}
default:
break;
}
return false;
}
static bool UpdateLargeSceneryAnimation(const LargeSceneryElement& scenery, const CoordsXYZ& loc, const int32_t baseZ)
{
const auto entry = scenery.GetEntry();
if (entry != nullptr && (entry->flags & LARGE_SCENERY_FLAG_ANIMATED))
{
MapInvalidateTileZoom1({ loc, baseZ, baseZ + 16 });
return true;
}
return false;
}
static bool UpdateWallAnimation(WallElement& wall, const CoordsXYZ& loc, const int32_t baseZ)
{
const auto entry = wall.GetEntry();
if (entry == nullptr)
{
2017-06-06 23:24:18 +02:00
return false;
}
if (entry->flags & WALL_SCENERY_IS_DOOR)
2018-06-22 23:17:03 +02:00
{
if (getGameState().currentTicks & 1)
2018-06-22 23:17:03 +02:00
{
2021-09-15 22:22:15 +02:00
return true;
2017-06-06 23:24:18 +02:00
}
bool removeAnim = true;
const auto currentFrame = wall.GetAnimationFrame();
2018-06-22 23:17:03 +02:00
if (currentFrame != 0)
{
auto newFrame = currentFrame;
2018-06-22 23:17:03 +02:00
if (currentFrame == 15)
{
newFrame = 0;
2018-06-22 23:17:03 +02:00
}
else
{
removeAnim = false;
2018-06-22 23:17:03 +02:00
if (currentFrame != 5)
{
newFrame++;
2017-02-18 08:26:33 +00:00
if (newFrame == 13 && !(entry->flags & WALL_SCENERY_LONG_DOOR_ANIMATION))
newFrame = 15;
2017-06-06 23:24:18 +02:00
}
}
if (currentFrame != newFrame)
{
wall.SetAnimationFrame(newFrame);
MapInvalidateTileZoom1({ loc, baseZ, baseZ + 32 });
}
2017-06-06 23:24:18 +02:00
}
return !removeAnim;
}
else if ((entry->flags2 & WALL_SCENERY_2_ANIMATED) || entry->scrolling_mode != kScrollingModeNone)
{
MapInvalidateTileZoom1({ loc, baseZ, baseZ + 16 });
return true;
}
return false;
}
static bool UpdateBannerAnimation([[maybe_unused]] const BannerElement& banner, const CoordsXYZ& loc, const int32_t baseZ)
{
MapInvalidateTileZoom1({ loc, baseZ, baseZ + 16 });
return true;
}
2018-06-22 23:17:03 +02:00
static bool UpdateTileAnimations(const TileCoordsXY& coords)
{
auto tileElement = MapGetFirstElementAt(coords);
if (tileElement == nullptr)
{
return false;
}
bool hasAnimations = false;
do
{
const auto baseZ = tileElement->GetBaseZ();
const CoordsXYZ loc{ coords.ToCoordsXY(), baseZ };
switch (tileElement->GetType())
{
case TileElementType::Entrance:
hasAnimations |= UpdateEntranceAnimation(*tileElement->AsEntrance(), loc, baseZ);
break;
case TileElementType::Path:
hasAnimations |= UpdatePathAnimation(*tileElement->AsPath(), loc, baseZ);
break;
case TileElementType::SmallScenery:
hasAnimations |= UpdateSmallSceneryAnimation(*tileElement->AsSmallScenery(), loc, baseZ);
break;
case TileElementType::Track:
hasAnimations |= UpdateTrackAnimation(*tileElement->AsTrack(), loc, baseZ);
break;
case TileElementType::Banner:
hasAnimations |= UpdateBannerAnimation(*tileElement->AsBanner(), loc, baseZ);
break;
case TileElementType::LargeScenery:
hasAnimations |= UpdateLargeSceneryAnimation(*tileElement->AsLargeScenery(), loc, baseZ);
break;
case TileElementType::Wall:
hasAnimations |= UpdateWallAnimation(*tileElement->AsWall(), loc, baseZ);
break;
default:
break;
}
} while (!(tileElement++)->IsLastForTile());
return hasAnimations;
}
static bool IsElementAnimated(const TileElementBase& element, const bool skipDoors)
{
switch (element.GetType())
{
case TileElementType::Banner:
return true;
case TileElementType::Wall:
{
const auto wall = element.AsWall();
const auto entry = wall->GetEntry();
if (entry != nullptr)
{
if ((entry->flags2 & WALL_SCENERY_2_ANIMATED) || entry->scrolling_mode != kScrollingModeNone)
{
return true;
}
if (!skipDoors && (entry->flags & WALL_SCENERY_IS_DOOR))
{
return true;
}
}
break;
}
case TileElementType::SmallScenery:
{
const auto scenery = element.AsSmallScenery();
const auto entry = scenery->GetEntry();
if (entry != nullptr && entry->HasFlag(SMALL_SCENERY_FLAG_ANIMATED))
{
return true;
}
break;
}
case TileElementType::LargeScenery:
{
const auto scenery = element.AsLargeScenery();
const auto entry = scenery->GetEntry();
if (entry != nullptr && (entry->flags & LARGE_SCENERY_FLAG_ANIMATED))
{
return true;
}
break;
}
case TileElementType::Path:
{
const auto path = element.AsPath();
if (path->HasQueueBanner())
{
return true;
}
break;
}
case TileElementType::Entrance:
{
const auto entrance = element.AsEntrance();
if (entrance->GetEntranceType() == ENTRANCE_TYPE_PARK_ENTRANCE && entrance->GetSequenceIndex() == 0)
{
return true;
}
else if (entrance->GetEntranceType() == ENTRANCE_TYPE_RIDE_ENTRANCE)
{
return true;
}
break;
}
case TileElementType::Track:
{
const auto track = element.AsTrack();
switch (track->GetTrackType())
{
case TrackElemType::Waterfall:
case TrackElemType::Rapids:
case TrackElemType::Whirlpool:
case TrackElemType::SpinningTunnel:
case TrackElemType::OnRidePhoto:
return true;
2024-10-11 15:30:27 +02:00
default:
break;
}
break;
}
default:
break;
}
return false;
}
void MapAnimation::Create(const CoordsXY coords)
{
_mapAnimations.insert(TileCoordsXY(coords));
}
void MapAnimation::CreateAll()
{
ClearAll();
TileElementIterator it;
TileElementIteratorBegin(&it);
while (TileElementIteratorNext(&it))
{
if (IsElementAnimated(*it.element, true))
{
_mapAnimations.insert(TileCoordsXY(it.x, it.y));
}
}
2018-01-11 10:59:26 +01:00
}
void MapAnimation::UpdateAll()
{
PROFILED_FUNCTION();
auto it = _mapAnimations.begin();
while (it != _mapAnimations.end())
{
if (UpdateTileAnimations(*it))
{
++it; // Tile was updated, keep it
}
else
{
it = _mapAnimations.erase(it); // No updates, remove tile
}
}
}
void MapAnimation::ClearAll()
{
_mapAnimations.clear();
}
void MapAnimation::ShiftAll(const CoordsXY amount)
{
if (amount.x == 0 && amount.y == 0)
return;
std::set<TileCoordsXY, TileCoordsXYCmp> newMapAnimations;
for (const auto& a : _mapAnimations)
{
newMapAnimations.insert(a + TileCoordsXY(amount));
}
_mapAnimations = std::move(newMapAnimations);
}