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

918 lines
22 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2014-05-25 19:47:11 +01:00
*
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
2014-05-25 19:47:11 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-05-25 19:47:11 +01:00
*****************************************************************************/
2018-06-22 23:17:03 +02:00
#include "Sprite.h"
2017-12-13 13:02:24 +01:00
#include "../Cheats.h"
2018-06-22 23:17:03 +02:00
#include "../Game.h"
#include "../OpenRCT2.h"
#include "../audio/audio.h"
2021-05-09 19:12:44 +01:00
#include "../core/ChecksumStream.h"
2018-05-23 20:27:00 +01:00
#include "../core/Crypt.h"
2021-05-09 19:12:44 +01:00
#include "../core/DataSerialiser.h"
2018-03-13 13:14:02 +01:00
#include "../core/Guard.hpp"
2021-05-09 19:12:44 +01:00
#include "../core/MemoryStream.h"
2018-01-05 23:45:53 +00:00
#include "../interface/Viewport.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/Date.h"
#include "../localisation/Localisation.h"
2018-01-02 17:58:43 +00:00
#include "../scenario/Scenario.h"
2017-03-23 17:56:39 +00:00
#include "Fountain.h"
2018-06-22 23:17:03 +02:00
#include <algorithm>
#include <cmath>
2018-11-21 23:16:04 +01:00
#include <iterator>
#include <numeric>
2021-01-23 07:36:46 +00:00
#include <vector>
2014-05-25 19:47:11 +01:00
static rct_sprite _spriteList[MAX_ENTITIES];
2021-03-16 11:18:06 +00:00
static std::array<std::list<uint16_t>, EnumValue(EntityType::Count)> gEntityLists;
static std::vector<uint16_t> _freeIdList;
static bool _spriteFlashingList[MAX_ENTITIES];
2021-01-23 07:36:46 +00:00
static std::array<std::vector<uint16_t>, SPATIAL_INDEX_SIZE> gSpriteSpatialIndex;
2016-05-09 00:08:03 +01:00
2021-01-23 07:36:46 +00:00
constexpr size_t GetSpatialIndexOffset(int32_t x, int32_t y)
{
size_t index = SPATIAL_INDEX_LOCATION_NULL;
if (x != LOCATION_NULL)
{
x = std::clamp(x, 0, 0xFFFF);
y = std::clamp(y, 0, 0xFFFF);
int16_t flooredX = floor2(x, 32);
uint8_t tileY = y >> 5;
index = (flooredX << 3) | tileY;
}
if (index >= sizeof(gSpriteSpatialIndex))
{
return SPATIAL_INDEX_LOCATION_NULL;
}
return index;
}
// Required for GetEntity to return a default
template<> bool SpriteBase::Is<SpriteBase>() const
{
return true;
}
template<> bool SpriteBase::Is<Litter>() const
{
return Type == EntityType::Litter;
}
constexpr bool EntityTypeIsMiscEntity(const EntityType type)
2021-01-10 15:14:34 +00:00
{
switch (type)
{
case EntityType::SteamParticle:
case EntityType::MoneyEffect:
case EntityType::CrashedVehicleParticle:
case EntityType::ExplosionCloud:
case EntityType::CrashSplash:
case EntityType::ExplosionFlare:
case EntityType::JumpingFountain:
case EntityType::Balloon:
case EntityType::Duck:
return true;
default:
return false;
}
2021-01-10 15:14:34 +00:00
}
template<> bool SpriteBase::Is<MiscEntity>() const
{
return EntityTypeIsMiscEntity(Type);
}
template<> bool SpriteBase::Is<SteamParticle>() const
{
return Type == EntityType::SteamParticle;
}
template<> bool SpriteBase::Is<ExplosionFlare>() const
{
return Type == EntityType::ExplosionFlare;
}
template<> bool SpriteBase::Is<ExplosionCloud>() const
{
return Type == EntityType::ExplosionCloud;
}
2021-03-16 11:18:06 +00:00
uint16_t GetEntityListCount(EntityType type)
{
2021-03-16 11:18:06 +00:00
return static_cast<uint16_t>(gEntityLists[EnumValue(type)].size());
}
uint16_t GetNumFreeEntities()
{
return static_cast<uint16_t>(_freeIdList.size());
}
std::string rct_sprite_checksum::ToString() const
{
std::string result;
result.reserve(raw.size() * 2);
for (auto b : raw)
{
char buf[3];
2021-05-09 19:12:44 +01:00
snprintf(buf, 3, "%02x", static_cast<int32_t>(b));
result.append(buf);
}
return result;
}
2020-08-01 11:29:54 +01:00
SpriteBase* try_get_sprite(size_t spriteIndex)
2017-03-10 17:26:08 +00:00
{
return spriteIndex >= MAX_ENTITIES ? nullptr : &_spriteList[spriteIndex].misc;
2017-03-10 17:26:08 +00:00
}
2020-08-01 11:29:54 +01:00
SpriteBase* get_sprite(size_t spriteIndex)
2016-07-17 22:14:44 +02:00
{
2020-08-01 11:29:54 +01:00
if (spriteIndex == SPRITE_INDEX_NULL)
{
return nullptr;
}
openrct2_assert(spriteIndex < MAX_ENTITIES, "Tried getting sprite %u", spriteIndex);
2020-08-01 11:29:54 +01:00
return try_get_sprite(spriteIndex);
2016-07-17 22:14:44 +02:00
}
2021-01-23 07:36:46 +00:00
const std::vector<uint16_t>& GetEntityTileList(const CoordsXY& spritePos)
2015-09-05 13:59:12 +01:00
{
return gSpriteSpatialIndex[GetSpatialIndexOffset(spritePos.x, spritePos.y)];
2015-09-05 13:59:12 +01:00
}
void SpriteBase::Invalidate()
2015-03-25 23:57:09 +00:00
{
if (sprite_left == LOCATION_NULL)
return;
int32_t maxZoom = 0;
switch (Type)
{
case EntityType::Vehicle:
case EntityType::Guest:
case EntityType::Staff:
maxZoom = 2;
break;
case EntityType::CrashedVehicleParticle:
case EntityType::JumpingFountain:
maxZoom = 0;
break;
case EntityType::Duck:
maxZoom = 1;
break;
case EntityType::SteamParticle:
case EntityType::MoneyEffect:
case EntityType::ExplosionCloud:
case EntityType::CrashSplash:
case EntityType::ExplosionFlare:
case EntityType::Balloon:
maxZoom = 2;
break;
case EntityType::Litter:
maxZoom = 0;
break;
default:
break;
}
viewports_invalidate(sprite_left, sprite_top, sprite_right, sprite_bottom, maxZoom);
2015-03-25 23:57:09 +00:00
}
2021-03-16 11:18:06 +00:00
static void ResetEntityLists()
2021-01-21 18:36:34 +00:00
{
for (auto& list : gEntityLists)
{
list.clear();
}
}
2021-01-21 18:36:34 +00:00
static void ResetFreeIds()
{
_freeIdList.clear();
_freeIdList.resize(MAX_ENTITIES);
// List needs to be back to front to simplify removing
std::iota(std::rbegin(_freeIdList), std::rend(_freeIdList), 0);
2021-01-21 18:36:34 +00:00
}
2021-03-16 11:18:06 +00:00
const std::list<uint16_t>& GetEntityList(const EntityType id)
2021-01-21 18:36:34 +00:00
{
return gEntityLists[EnumValue(id)];
}
2015-03-25 23:57:09 +00:00
2015-12-11 09:38:37 -06:00
/**
*
2015-12-11 09:38:37 -06:00
* rct2: 0x0069EB13
*/
2016-05-09 00:08:03 +01:00
void reset_sprite_list()
{
2017-06-06 23:24:18 +02:00
gSavedAge = 0;
2020-02-18 00:00:19 +01:00
std::memset(static_cast<void*>(_spriteList), 0, sizeof(_spriteList));
for (int32_t i = 0; i < MAX_ENTITIES; ++i)
2018-06-22 23:17:03 +02:00
{
auto* spr = GetEntity(i);
2020-07-06 22:02:25 +01:00
if (spr == nullptr)
{
continue;
}
spr->Type = EntityType::Null;
spr->sprite_index = i;
_spriteFlashingList[i] = false;
2017-06-06 23:24:18 +02:00
}
ResetEntityLists();
ResetFreeIds();
2017-06-06 23:24:18 +02:00
reset_sprite_spatial_index();
2014-06-21 13:31:28 +01:00
}
2021-01-23 07:36:46 +00:00
static void SpriteSpatialInsert(SpriteBase* sprite, const CoordsXY& newLoc);
/**
2015-12-11 09:38:37 -06:00
*
* rct2: 0x0069EBE4
2014-06-21 13:31:28 +01:00
* This function looks as though it sets some sort of order for sprites.
2017-04-29 23:39:24 -05:00
* Sprites can share their position if this is the case.
2014-06-21 13:31:28 +01:00
*/
void reset_sprite_spatial_index()
2016-05-09 00:08:03 +01:00
{
2021-01-23 07:36:46 +00:00
for (auto& vec : gSpriteSpatialIndex)
{
vec.clear();
}
for (size_t i = 0; i < MAX_ENTITIES; i++)
2018-06-22 23:17:03 +02:00
{
auto* spr = GetEntity(i);
if (spr != nullptr && spr->Type != EntityType::Null)
2018-06-22 23:17:03 +02:00
{
2021-01-23 07:36:46 +00:00
SpriteSpatialInsert(spr, { spr->x, spr->y });
2017-06-06 23:24:18 +02:00
}
}
}
#ifndef DISABLE_NETWORK
2021-05-09 19:12:44 +01:00
template<typename T> void NetworkSerialseEntityType(DataSerialiser& ds)
2021-03-27 09:36:41 +00:00
{
for (auto* ent : EntityList<T>())
{
2021-05-09 19:12:44 +01:00
ent->Serialise(ds);
2021-03-27 09:36:41 +00:00
}
}
2021-05-09 19:12:44 +01:00
template<typename... T> void NetworkSerialiseEntityTypes(DataSerialiser& ds)
2021-03-27 09:36:41 +00:00
{
2021-05-09 19:12:44 +01:00
(NetworkSerialseEntityType<T>(ds), ...);
2021-03-27 09:36:41 +00:00
}
rct_sprite_checksum sprite_checksum()
{
2021-05-09 19:12:44 +01:00
rct_sprite_checksum checksum{};
2021-05-09 19:12:44 +01:00
OpenRCT2::ChecksumStream ms(checksum.raw);
DataSerialiser ds(true, ms);
NetworkSerialiseEntityTypes<Guest, Staff, Vehicle, Litter>(ds);
return checksum;
}
#else
2018-12-11 09:57:04 +01:00
rct_sprite_checksum sprite_checksum()
{
2018-12-11 09:57:04 +01:00
return rct_sprite_checksum{};
}
#endif // DISABLE_NETWORK
2020-01-19 17:02:43 +00:00
static void sprite_reset(SpriteBase* sprite)
2016-07-23 13:32:58 +02:00
{
2017-06-06 23:24:18 +02:00
// Need to retain how the sprite is linked in lists
uint16_t sprite_index = sprite->sprite_index;
_spriteFlashingList[sprite_index] = false;
2016-07-23 13:32:58 +02:00
std::memset(sprite, 0, sizeof(rct_sprite));
2016-07-23 13:32:58 +02:00
2017-06-06 23:24:18 +02:00
sprite->sprite_index = sprite_index;
sprite->Type = EntityType::Null;
2016-07-23 13:32:58 +02:00
}
/**
2018-06-22 23:17:03 +02:00
* Clears all the unused sprite memory to zero. Probably so that it can be compressed better when saving.
* rct2: 0x0069EBA4
*/
void sprite_clear_all_unused()
{
for (auto index : _freeIdList)
2018-06-22 23:17:03 +02:00
{
auto* entity = GetEntity(index);
if (entity == nullptr)
{
continue;
}
sprite_reset(entity);
_spriteFlashingList[entity->sprite_index] = false;
}
}
static constexpr uint16_t MAX_MISC_SPRITES = 300;
static void AddToEntityList(SpriteBase* entity)
2021-01-21 18:36:34 +00:00
{
auto& list = gEntityLists[EnumValue(entity->Type)];
2021-01-21 18:36:34 +00:00
// Entity list must be in sprite_index order to prevent desync issues
list.insert(std::lower_bound(std::begin(list), std::end(list), entity->sprite_index), entity->sprite_index);
}
static void AddToFreeList(uint16_t index)
{
// Free list must be in reverse sprite_index order to prevent desync issues
_freeIdList.insert(std::upper_bound(std::rbegin(_freeIdList), std::rend(_freeIdList), index).base(), index);
}
2021-01-21 18:36:34 +00:00
static void RemoveFromEntityList(SpriteBase* entity)
{
auto& list = gEntityLists[EnumValue(entity->Type)];
2021-01-21 18:36:34 +00:00
auto ptr = std::lower_bound(std::begin(list), std::end(list), entity->sprite_index);
if (ptr != std::end(list) && *ptr == entity->sprite_index)
{
list.erase(ptr);
}
}
2021-03-16 11:18:06 +00:00
uint16_t GetMiscEntityCount()
{
uint16_t count = 0;
for (auto id : { EntityType::SteamParticle, EntityType::MoneyEffect, EntityType::CrashedVehicleParticle,
EntityType::ExplosionCloud, EntityType::CrashSplash, EntityType::ExplosionFlare,
EntityType::JumpingFountain, EntityType::Balloon, EntityType::Duck })
{
count += GetEntityListCount(id);
}
return count;
}
static void PrepareNewEntity(SpriteBase* base, const EntityType type)
{
// Need to reset all sprite data, as the uninitialised values
// may contain garbage and cause a desync later on.
sprite_reset(base);
base->Type = type;
AddToEntityList(base);
base->x = LOCATION_NULL;
base->y = LOCATION_NULL;
base->z = 0;
base->sprite_width = 0x10;
base->sprite_height_negative = 0x14;
base->sprite_height_positive = 0x8;
base->sprite_left = LOCATION_NULL;
SpriteSpatialInsert(base, { LOCATION_NULL, 0 });
}
2021-03-16 11:18:06 +00:00
rct_sprite* create_sprite(EntityType type)
{
if (_freeIdList.size() == 0)
2018-06-22 23:17:03 +02:00
{
// No free sprites.
return nullptr;
}
if (EntityTypeIsMiscEntity(type))
{
// Misc sprites are commonly used for effects, if there are less than MAX_MISC_SPRITES
// free it will fail to keep slots for more relevant sprites.
// Also there can't be more than MAX_MISC_SPRITES sprites in this list.
2021-03-16 11:18:06 +00:00
uint16_t miscSlotsRemaining = MAX_MISC_SPRITES - GetMiscEntityCount();
if (miscSlotsRemaining >= _freeIdList.size())
2018-06-22 23:17:03 +02:00
{
2018-01-29 17:06:01 +01:00
return nullptr;
2017-06-06 23:24:18 +02:00
}
}
auto* sprite = GetEntity(_freeIdList.back());
2020-07-06 22:02:25 +01:00
if (sprite == nullptr)
{
return nullptr;
}
_freeIdList.pop_back();
PrepareNewEntity(sprite, type);
2020-04-17 16:08:22 -03:00
return reinterpret_cast<rct_sprite*>(sprite);
}
SpriteBase* CreateEntityAt(const uint16_t index, const EntityType type)
{
auto id = std::lower_bound(std::rbegin(_freeIdList), std::rend(_freeIdList), index);
if (id == std::rend(_freeIdList) || *id != index)
{
return nullptr;
}
auto* entity = GetEntity(index);
if (entity == nullptr)
{
return nullptr;
}
_freeIdList.erase(std::next(id).base());
PrepareNewEntity(entity, type);
return entity;
}
2015-06-29 17:22:01 +01:00
/**
*
2015-12-11 09:38:37 -06:00
* rct2: 0x00673200
2015-06-29 17:22:01 +01:00
*/
2020-07-10 14:16:03 +01:00
void SteamParticle::Update()
2015-06-29 17:22:01 +01:00
{
2017-11-22 19:08:14 +00:00
// Move up 1 z every 3 ticks (Starts after 4 ticks)
Invalidate();
2020-07-10 14:16:03 +01:00
time_to_move++;
if (time_to_move >= 4)
2018-06-22 23:17:03 +02:00
{
2020-07-10 14:16:03 +01:00
time_to_move = 1;
MoveTo({ x, y, z + 1 });
2017-06-06 23:24:18 +02:00
}
2020-07-10 14:16:03 +01:00
frame += 64;
if (frame >= (56 * 64))
2018-06-22 23:17:03 +02:00
{
2020-07-10 14:16:03 +01:00
sprite_remove(this);
2017-06-06 23:24:18 +02:00
}
2015-06-29 17:22:01 +01:00
}
/**
*
2015-08-22 16:39:24 +01:00
* rct2: 0x0067363D
2015-06-29 17:22:01 +01:00
*/
void sprite_misc_explosion_cloud_create(const CoordsXYZ& cloudPos)
2015-06-29 17:22:01 +01:00
{
2021-03-16 11:18:06 +00:00
auto* sprite = CreateEntity<ExplosionCloud>();
2018-06-22 23:17:03 +02:00
if (sprite != nullptr)
{
2017-06-06 23:24:18 +02:00
sprite->sprite_width = 44;
sprite->sprite_height_negative = 32;
sprite->sprite_height_positive = 34;
sprite->MoveTo(cloudPos + CoordsXYZ{ 0, 0, 4 });
2017-06-06 23:24:18 +02:00
sprite->frame = 0;
}
2015-06-29 17:22:01 +01:00
}
/**
*
2015-12-11 09:38:37 -06:00
* rct2: 0x00673385
2015-06-29 17:22:01 +01:00
*/
2020-07-10 14:16:03 +01:00
void ExplosionCloud::Update()
2015-06-29 17:22:01 +01:00
{
Invalidate();
2020-07-10 14:16:03 +01:00
frame += 128;
if (frame >= (36 * 128))
2018-06-22 23:17:03 +02:00
{
2020-07-10 14:16:03 +01:00
sprite_remove(this);
2017-06-06 23:24:18 +02:00
}
2015-06-29 17:22:01 +01:00
}
/**
*
2015-08-22 16:39:24 +01:00
* rct2: 0x0067366B
2015-06-29 17:22:01 +01:00
*/
void sprite_misc_explosion_flare_create(const CoordsXYZ& flarePos)
2015-06-29 17:22:01 +01:00
{
2021-03-16 11:18:06 +00:00
MiscEntity* sprite = CreateEntity<ExplosionFlare>();
2018-06-22 23:17:03 +02:00
if (sprite != nullptr)
{
2017-06-06 23:24:18 +02:00
sprite->sprite_width = 25;
sprite->sprite_height_negative = 85;
sprite->sprite_height_positive = 8;
sprite->MoveTo(flarePos + CoordsXYZ{ 0, 0, 4 });
2017-06-06 23:24:18 +02:00
sprite->frame = 0;
}
2015-06-29 17:22:01 +01:00
}
/**
*
2015-12-11 09:38:37 -06:00
* rct2: 0x006733B4
2015-06-29 17:22:01 +01:00
*/
2020-07-10 14:16:03 +01:00
void ExplosionFlare::Update()
2015-06-29 17:22:01 +01:00
{
Invalidate();
2020-07-10 14:16:03 +01:00
frame += 64;
if (frame >= (124 * 64))
2018-06-22 23:17:03 +02:00
{
2020-07-10 14:16:03 +01:00
sprite_remove(this);
2017-06-06 23:24:18 +02:00
}
2015-06-29 17:22:01 +01:00
}
template<typename T> void MiscUpdateAllType()
{
2021-03-16 11:18:06 +00:00
for (auto misc : EntityList<T>())
2018-06-22 23:17:03 +02:00
{
misc->Update();
2017-06-06 23:24:18 +02:00
}
}
template<typename... T> void MiscUpdateAllTypes()
{
(MiscUpdateAllType<T>(), ...);
}
2014-10-09 20:31:58 +01:00
/**
*
2015-12-11 09:38:37 -06:00
* rct2: 0x00672AA4
2014-10-09 20:31:58 +01:00
*/
void sprite_misc_update_all()
2014-10-09 20:31:58 +01:00
{
MiscUpdateAllTypes<
SteamParticle, MoneyEffect, VehicleCrashParticle, ExplosionCloud, CrashSplashParticle, ExplosionFlare, JumpingFountain,
Balloon, Duck>();
2014-09-20 16:18:02 +01:00
}
// Performs a search to ensure that insert keeps next_in_quadrant in sprite_index order
static void SpriteSpatialInsert(SpriteBase* sprite, const CoordsXY& newLoc)
{
size_t newIndex = GetSpatialIndexOffset(newLoc.x, newLoc.y);
2021-01-23 07:36:46 +00:00
auto& spatialVector = gSpriteSpatialIndex[newIndex];
auto index = std::lower_bound(std::begin(spatialVector), std::end(spatialVector), sprite->sprite_index);
spatialVector.insert(index, sprite->sprite_index);
}
static void SpriteSpatialRemove(SpriteBase* sprite)
{
size_t currentIndex = GetSpatialIndexOffset(sprite->x, sprite->y);
2021-01-23 07:36:46 +00:00
auto& spatialVector = gSpriteSpatialIndex[currentIndex];
auto index = std::lower_bound(std::begin(spatialVector), std::end(spatialVector), sprite->sprite_index);
if (index != std::end(spatialVector) && *index == sprite->sprite_index)
{
spatialVector.erase(index, index + 1);
}
else
{
log_warning("Bad sprite spatial index. Rebuilding the spatial index...");
reset_sprite_spatial_index();
}
}
static void SpriteSpatialMove(SpriteBase* sprite, const CoordsXY& newLoc)
{
size_t newIndex = GetSpatialIndexOffset(newLoc.x, newLoc.y);
size_t currentIndex = GetSpatialIndexOffset(sprite->x, sprite->y);
if (newIndex == currentIndex)
return;
SpriteSpatialRemove(sprite);
SpriteSpatialInsert(sprite, newLoc);
}
2020-05-11 12:36:45 +02:00
void SpriteBase::MoveTo(const CoordsXYZ& newLocation)
{
if (x != LOCATION_NULL)
{
// Invalidate old position.
Invalidate();
}
2020-05-11 12:36:45 +02:00
auto loc = newLocation;
if (!map_is_location_valid(loc))
2018-06-22 23:17:03 +02:00
{
2020-05-11 12:36:45 +02:00
loc.x = LOCATION_NULL;
2017-06-06 23:24:18 +02:00
}
2014-09-20 16:18:02 +01:00
2020-05-11 12:36:45 +02:00
SpriteSpatialMove(this, loc);
2014-09-20 16:18:02 +01:00
2020-05-11 12:36:45 +02:00
if (loc.x == LOCATION_NULL)
2018-06-22 23:17:03 +02:00
{
2020-05-11 12:36:45 +02:00
sprite_left = LOCATION_NULL;
x = loc.x;
y = loc.y;
z = loc.z;
2018-06-22 23:17:03 +02:00
}
else
{
2020-06-21 13:50:54 -03:00
sprite_set_coordinates(loc, this);
Invalidate(); // Invalidate new position.
2017-06-06 23:24:18 +02:00
}
2016-08-06 10:21:08 -06:00
}
CoordsXYZ SpriteBase::GetLocation() const
{
return { x, y, z };
}
void SpriteBase::SetLocation(const CoordsXYZ& newLocation)
{
x = static_cast<int16_t>(newLocation.x);
y = static_cast<int16_t>(newLocation.y);
z = static_cast<int16_t>(newLocation.z);
}
2020-06-21 13:50:54 -03:00
void sprite_set_coordinates(const CoordsXYZ& spritePos, SpriteBase* sprite)
2018-06-22 23:17:03 +02:00
{
2020-06-21 13:50:54 -03:00
auto screenCoords = translate_3d_to_2d_with_z(get_current_rotation(), spritePos);
2014-09-20 16:18:02 +01:00
2020-01-19 16:22:17 +00:00
sprite->sprite_left = screenCoords.x - sprite->sprite_width;
sprite->sprite_right = screenCoords.x + sprite->sprite_width;
sprite->sprite_top = screenCoords.y - sprite->sprite_height_negative;
sprite->sprite_bottom = screenCoords.y + sprite->sprite_height_positive;
2020-06-21 13:50:54 -03:00
sprite->x = spritePos.x;
sprite->y = spritePos.y;
sprite->z = spritePos.z;
}
/**
*
* rct2: 0x0069EDB6
*/
2020-01-19 16:12:29 +00:00
void sprite_remove(SpriteBase* sprite)
{
2020-06-06 10:55:01 +01:00
auto peep = sprite->As<Peep>();
if (peep != nullptr)
{
2019-07-21 20:44:19 +01:00
peep->SetName({});
}
EntityTweener::Get().RemoveEntity(sprite);
2021-01-21 18:36:34 +00:00
RemoveFromEntityList(sprite); // remove from existing list
AddToFreeList(sprite->sprite_index);
2020-08-01 11:29:54 +01:00
SpriteSpatialRemove(sprite);
2021-01-16 15:48:39 +00:00
sprite_reset(sprite);
}
2020-06-21 14:14:44 -03:00
static bool litter_can_be_at(const CoordsXYZ& mapPos)
{
2018-11-01 13:53:50 +01:00
TileElement* tileElement;
2020-06-21 14:14:44 -03:00
if (!map_is_location_owned(mapPos))
2017-06-06 23:24:18 +02:00
return false;
2020-06-21 14:14:44 -03:00
tileElement = map_get_first_element_at(mapPos);
if (tileElement == nullptr)
return false;
2018-06-22 23:17:03 +02:00
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_PATH)
2017-06-06 23:24:18 +02:00
continue;
2019-12-22 22:14:02 +01:00
int32_t pathZ = tileElement->GetBaseZ();
2020-06-21 14:14:44 -03:00
if (pathZ < mapPos.z || pathZ >= mapPos.z + 32)
2017-06-06 23:24:18 +02:00
continue;
2019-05-10 22:00:38 +02:00
return !tile_element_is_underground(tileElement);
} while (!(tileElement++)->IsLastForTile());
2017-06-06 23:24:18 +02:00
return false;
}
/**
*
* rct2: 0x0067375D
*/
void Litter::Create(const CoordsXYZD& litterPos, Type type)
{
2017-06-06 23:24:18 +02:00
if (gCheatsDisableLittering)
return;
2020-06-21 14:13:22 -03:00
auto offsetLitterPos = litterPos
+ CoordsXY{ CoordsDirectionDelta[litterPos.direction >> 3].x / 8,
CoordsDirectionDelta[litterPos.direction >> 3].y / 8 };
2020-06-21 14:14:44 -03:00
if (!litter_can_be_at(offsetLitterPos))
2017-06-06 23:24:18 +02:00
return;
2021-03-16 11:18:06 +00:00
if (GetEntityListCount(EntityType::Litter) >= 500)
2018-06-22 23:17:03 +02:00
{
2020-01-19 15:50:01 +00:00
Litter* newestLitter = nullptr;
uint32_t newestLitterCreationTick = 0;
2021-03-16 11:18:06 +00:00
for (auto litter : EntityList<Litter>())
2018-06-22 23:17:03 +02:00
{
if (newestLitterCreationTick <= litter->creationTick)
{
2017-06-06 23:24:18 +02:00
newestLitterCreationTick = litter->creationTick;
newestLitter = litter;
}
}
2018-06-22 23:17:03 +02:00
if (newestLitter != nullptr)
{
newestLitter->Invalidate();
2020-01-19 16:12:29 +00:00
sprite_remove(newestLitter);
2017-06-06 23:24:18 +02:00
}
}
2021-03-16 11:18:06 +00:00
Litter* litter = CreateEntity<Litter>();
2018-01-29 17:06:01 +01:00
if (litter == nullptr)
2017-06-06 23:24:18 +02:00
return;
2020-06-21 14:13:22 -03:00
litter->sprite_direction = offsetLitterPos.direction;
2017-06-06 23:24:18 +02:00
litter->sprite_width = 6;
litter->sprite_height_negative = 6;
litter->sprite_height_positive = 3;
2021-01-10 15:14:34 +00:00
litter->SubType = type;
2020-06-21 14:13:22 -03:00
litter->MoveTo(offsetLitterPos);
2017-06-06 23:24:18 +02:00
litter->creationTick = gScenarioTicks;
2015-06-29 17:22:01 +01:00
}
2015-07-02 18:22:38 +01:00
/**
*
* rct2: 0x006738E1
*/
void Litter::RemoveAt(const CoordsXYZ& litterPos)
2015-07-02 18:22:38 +01:00
{
2021-01-23 07:36:46 +00:00
std::vector<Litter*> removals;
2020-06-21 14:06:56 -03:00
for (auto litter : EntityTileList<Litter>(litterPos))
2018-06-22 23:17:03 +02:00
{
2020-06-21 14:06:56 -03:00
if (abs(litter->z - litterPos.z) <= 16)
2018-06-22 23:17:03 +02:00
{
2020-06-21 14:06:56 -03:00
if (abs(litter->x - litterPos.x) <= 8 && abs(litter->y - litterPos.y) <= 8)
2018-06-22 23:17:03 +02:00
{
2021-01-23 07:36:46 +00:00
removals.push_back(litter);
2017-06-06 23:24:18 +02:00
}
}
}
2021-01-23 07:36:46 +00:00
for (auto* litter : removals)
{
litter->Invalidate();
sprite_remove(litter);
}
2015-07-02 18:22:38 +01:00
}
2016-12-11 00:08:37 +00:00
2021-05-13 16:23:04 +02:00
static const rct_string_id litterNames[12] = {
STR_LITTER_VOMIT,
STR_LITTER_VOMIT,
STR_SHOP_ITEM_SINGULAR_EMPTY_CAN,
STR_SHOP_ITEM_SINGULAR_RUBBISH,
STR_SHOP_ITEM_SINGULAR_EMPTY_BURGER_BOX,
STR_SHOP_ITEM_SINGULAR_EMPTY_CUP,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOX,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOTTLE,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_RED,
STR_SHOP_ITEM_SINGULAR_EMPTY_DRINK_CARTON,
STR_SHOP_ITEM_SINGULAR_EMPTY_JUICE_CUP,
STR_SHOP_ITEM_SINGULAR_EMPTY_BOWL_BLUE,
};
rct_string_id Litter::GetName() const
{
if (EnumValue(SubType) >= sizeof(litterNames))
return STR_NONE;
return litterNames[EnumValue(SubType)];
}
/**
* Loops through all sprites, finds floating objects and removes them.
* Returns the amount of removed objects as feedback.
*/
uint16_t remove_floating_sprites()
{
uint16_t removed = 0;
2021-03-16 11:18:06 +00:00
for (auto* balloon : EntityList<Balloon>())
{
sprite_remove(balloon);
removed++;
}
2021-03-16 11:18:06 +00:00
for (auto* duck : EntityList<Duck>())
{
if (duck->IsFlying())
{
sprite_remove(duck);
removed++;
}
}
2021-03-16 11:18:06 +00:00
for (auto* money : EntityList<MoneyEffect>())
{
sprite_remove(money);
removed++;
}
return removed;
}
2021-03-16 11:18:06 +00:00
void EntityTweener::PopulateEntities()
2016-12-11 00:08:37 +00:00
{
2021-03-16 11:18:06 +00:00
for (auto ent : EntityList<Guest>())
{
Entities.push_back(ent);
PrePos.emplace_back(ent->x, ent->y, ent->z);
}
for (auto ent : EntityList<Staff>())
{
Entities.push_back(ent);
PrePos.emplace_back(ent->x, ent->y, ent->z);
}
for (auto ent : EntityList<Vehicle>())
2018-06-22 23:17:03 +02:00
{
Entities.push_back(ent);
2021-01-05 19:05:47 +02:00
PrePos.emplace_back(ent->x, ent->y, ent->z);
2017-06-06 23:24:18 +02:00
}
2016-12-11 00:08:37 +00:00
}
void EntityTweener::PreTick()
2016-12-11 00:08:37 +00:00
{
Restore();
Reset();
2021-03-16 11:18:06 +00:00
PopulateEntities();
2016-12-11 00:08:37 +00:00
}
void EntityTweener::PostTick()
2016-12-11 00:08:37 +00:00
{
2021-01-05 19:05:47 +02:00
for (auto* ent : Entities)
{
if (ent == nullptr)
{
// Sprite was removed, add a dummy position to keep the index aligned.
2021-01-05 19:05:47 +02:00
PostPos.emplace_back(0, 0, 0);
}
else
{
2021-01-05 19:05:47 +02:00
PostPos.emplace_back(ent->x, ent->y, ent->z);
}
}
2016-12-11 00:08:37 +00:00
}
void EntityTweener::RemoveEntity(SpriteBase* entity)
{
if (!entity->Is<Peep>() && !entity->Is<Vehicle>())
{
// Only peeps and vehicles are tweened, bail if type is incorrect.
return;
}
2021-01-05 19:05:47 +02:00
auto it = std::find(Entities.begin(), Entities.end(), entity);
if (it != Entities.end())
*it = nullptr;
}
void EntityTweener::Tween(float alpha)
2016-12-11 00:08:37 +00:00
{
const float inv = (1.0f - alpha);
2021-01-05 19:05:47 +02:00
for (size_t i = 0; i < Entities.size(); ++i)
2018-06-22 23:17:03 +02:00
{
2021-01-05 19:05:47 +02:00
auto* ent = Entities[i];
if (ent == nullptr)
2020-07-06 22:02:25 +01:00
continue;
2021-01-05 19:05:47 +02:00
auto& posA = PrePos[i];
auto& posB = PostPos[i];
if (posA == posB)
continue;
sprite_set_coordinates(
{ static_cast<int32_t>(std::round(posB.x * alpha + posA.x * inv)),
static_cast<int32_t>(std::round(posB.y * alpha + posA.y * inv)),
static_cast<int32_t>(std::round(posB.z * alpha + posA.z * inv)) },
ent);
ent->Invalidate();
2017-06-06 23:24:18 +02:00
}
2016-12-11 00:08:37 +00:00
}
void EntityTweener::Restore()
{
2021-01-05 19:05:47 +02:00
for (size_t i = 0; i < Entities.size(); ++i)
{
2021-01-05 19:05:47 +02:00
auto* ent = Entities[i];
if (ent == nullptr)
continue;
2021-01-05 19:05:47 +02:00
sprite_set_coordinates(PostPos[i], ent);
ent->Invalidate();
}
}
void EntityTweener::Reset()
{
2021-01-05 19:05:47 +02:00
Entities.clear();
PrePos.clear();
PostPos.clear();
}
static EntityTweener tweener;
EntityTweener& EntityTweener::Get()
{
return tweener;
}
2020-01-19 17:02:43 +00:00
void sprite_set_flashing(SpriteBase* sprite, bool flashing)
{
assert(sprite->sprite_index < MAX_ENTITIES);
2020-01-19 17:02:43 +00:00
_spriteFlashingList[sprite->sprite_index] = flashing;
}
2020-01-19 17:02:43 +00:00
bool sprite_get_flashing(SpriteBase* sprite)
{
assert(sprite->sprite_index < MAX_ENTITIES);
2020-01-19 17:02:43 +00:00
return _spriteFlashingList[sprite->sprite_index];
}