Files
OpenRCT2-Web/src/openrct2/entity/EntityRegistry.cpp
T

552 lines
14 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
*****************************************************************************/
2021-11-24 16:58:01 +02:00
#include "EntityRegistry.h"
2018-06-22 23:17:03 +02:00
#include "../Game.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"
2021-11-25 23:47:24 +02:00
#include "../entity/Peep.h"
#include "../entity/Staff.h"
2018-01-05 23:45:53 +00:00
#include "../interface/Viewport.h"
#include "../peep/RideUseSystem.h"
2021-05-29 06:01:32 +01:00
#include "../ride/Vehicle.h"
2018-01-02 17:58:43 +00:00
#include "../scenario/Scenario.h"
2021-05-29 06:01:32 +01:00
#include "Balloon.h"
#include "Duck.h"
2021-05-29 07:47:49 +01:00
#include "EntityTweener.h"
2017-03-23 17:56:39 +00:00
#include "Fountain.h"
2021-05-29 06:01:32 +01:00
#include "MoneyEffect.h"
#include "Particle.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
2021-11-24 15:58:16 +02:00
union Entity
{
uint8_t pad_00[0x200];
EntityBase base;
Entity()
: pad_00()
{
}
2021-11-24 15:58:16 +02:00
};
2021-11-24 16:22:29 +02:00
static Entity _entities[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;
2021-11-24 15:37:47 +02:00
static bool _entityFlashingList[MAX_ENTITIES];
2021-05-29 06:01:32 +01:00
constexpr const uint32_t SPATIAL_INDEX_SIZE = (MAXIMUM_MAP_SIZE_TECHNICAL * MAXIMUM_MAP_SIZE_TECHNICAL) + 1;
constexpr const uint32_t SPATIAL_INDEX_LOCATION_NULL = SPATIAL_INDEX_SIZE - 1;
2021-11-24 15:27:48 +02:00
static std::array<std::vector<uint16_t>, SPATIAL_INDEX_SIZE> gEntitySpatialIndex;
2016-05-09 00:08:03 +01:00
2021-09-12 18:29:37 +02:00
static void FreeEntity(EntityBase& entity);
2021-09-29 09:19:47 -07:00
static constexpr size_t GetSpatialIndexOffset(const CoordsXY& loc)
2021-01-23 07:36:46 +00:00
{
2021-09-29 09:19:47 -07:00
if (loc.IsNull())
return SPATIAL_INDEX_LOCATION_NULL;
2021-01-23 07:36:46 +00:00
2021-09-29 09:19:47 -07:00
// NOTE: The input coordinate is rotated and can have negative components.
const auto tileX = std::abs(loc.x) / COORDS_XY_STEP;
const auto tileY = std::abs(loc.y) / COORDS_XY_STEP;
2021-01-23 07:36:46 +00:00
2021-09-29 09:19:47 -07:00
if (tileX >= MAXIMUM_MAP_SIZE_TECHNICAL || tileY >= MAXIMUM_MAP_SIZE_TECHNICAL)
2021-01-23 07:36:46 +00:00
return SPATIAL_INDEX_LOCATION_NULL;
2021-09-29 09:19:47 -07:00
return tileX * MAXIMUM_MAP_SIZE_TECHNICAL + tileY;
2021-01-23 07:36:46 +00:00
}
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
}
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());
}
2021-11-24 15:37:47 +02:00
std::string EntitiesChecksum::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;
}
2021-11-24 15:37:47 +02:00
EntityBase* TryGetEntity(size_t entityIndex)
2017-03-10 17:26:08 +00:00
{
2021-11-24 15:37:47 +02:00
return entityIndex >= MAX_ENTITIES ? nullptr : &_entities[entityIndex].base;
2017-03-10 17:26:08 +00:00
}
2021-11-24 15:37:47 +02:00
EntityBase* GetEntity(size_t entityIndex)
2016-07-17 22:14:44 +02:00
{
2021-11-24 15:37:47 +02:00
if (entityIndex == SPRITE_INDEX_NULL)
{
return nullptr;
}
2021-11-24 19:03:41 +02:00
openrct2_assert(entityIndex < MAX_ENTITIES, "Tried getting entity %u", entityIndex);
2021-11-24 15:37:47 +02:00
return TryGetEntity(entityIndex);
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
{
2021-11-24 15:27:48 +02:00
return gEntitySpatialIndex[GetSpatialIndexOffset(spritePos)];
2015-09-05 13:59:12 +01: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
*/
2021-11-24 15:27:48 +02:00
void ResetAllEntities()
2016-05-09 00:08:03 +01:00
{
2017-06-06 23:24:18 +02:00
gSavedAge = 0;
// Free all associated Entity pointers prior to zeroing memory
for (int32_t i = 0; i < MAX_ENTITIES; ++i)
{
auto* spr = GetEntity(i);
if (spr == nullptr)
{
continue;
}
FreeEntity(*spr);
}
2021-11-24 15:37:47 +02:00
std::fill(std::begin(_entities), std::end(_entities), Entity());
OpenRCT2::RideUse::GetHistory().Clear();
OpenRCT2::RideUse::GetTypeHistory().Clear();
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;
2021-11-24 15:37:47 +02:00
_entityFlashingList[i] = false;
2017-06-06 23:24:18 +02:00
}
ResetEntityLists();
ResetFreeIds();
2021-11-24 15:37:47 +02:00
ResetEntitySpatialIndices();
2014-06-21 13:31:28 +01:00
}
2021-11-24 15:37:47 +02:00
static void EntitySpatialInsert(EntityBase* entity, const CoordsXY& newLoc);
2021-01-23 07:36:46 +00:00
/**
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
*/
2021-11-24 15:37:47 +02:00
void ResetEntitySpatialIndices()
2016-05-09 00:08:03 +01:00
{
2021-11-24 15:27:48 +02:00
for (auto& vec : gEntitySpatialIndex)
2021-01-23 07:36:46 +00:00
{
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-11-24 15:37:47 +02:00
EntitySpatialInsert(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
}
2021-11-24 15:37:47 +02:00
EntitiesChecksum GetAllEntitiesChecksum()
{
2021-11-24 15:37:47 +02:00
EntitiesChecksum 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
2021-11-24 15:37:47 +02:00
EntitiesChecksum GetAllEntitiesChecksum()
{
2021-11-24 15:37:47 +02:00
return EntitiesChecksum{};
}
#endif // DISABLE_NETWORK
2021-11-24 15:37:47 +02:00
static void EntityReset(EntityBase* entity)
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
2021-11-24 15:37:47 +02:00
uint16_t entityIndex = entity->sprite_index;
_entityFlashingList[entityIndex] = false;
2016-07-23 13:32:58 +02:00
2021-11-24 15:37:47 +02:00
Entity* spr = reinterpret_cast<Entity*>(entity);
*spr = Entity();
2016-07-23 13:32:58 +02:00
2021-11-24 15:37:47 +02:00
entity->sprite_index = entityIndex;
entity->Type = EntityType::Null;
2016-07-23 13:32:58 +02:00
}
static constexpr uint16_t MAX_MISC_SPRITES = 300;
2021-08-25 17:42:09 +03:00
static void AddToEntityList(EntityBase* 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-08-25 17:42:09 +03:00
static void RemoveFromEntityList(EntityBase* entity)
2021-01-21 18:36:34 +00:00
{
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;
}
2021-08-25 17:42:09 +03:00
static void PrepareNewEntity(EntityBase* base, const EntityType type)
{
// Need to reset all sprite data, as the uninitialised values
// may contain garbage and cause a desync later on.
2021-11-24 15:37:47 +02:00
EntityReset(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->SpriteRect = {};
2021-11-24 15:37:47 +02:00
EntitySpatialInsert(base, { LOCATION_NULL, 0 });
}
2021-08-25 17:42:09 +03:00
EntityBase* CreateEntity(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* entity = GetEntity(_freeIdList.back());
if (entity == nullptr)
2020-07-06 22:02:25 +01:00
{
return nullptr;
}
_freeIdList.pop_back();
PrepareNewEntity(entity, type);
return entity;
}
2021-08-25 17:42:09 +03:00
EntityBase* 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
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
*/
2021-11-24 15:37:47 +02:00
void UpdateAllMiscEntities()
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
2021-11-24 15:37:47 +02:00
static void EntitySpatialInsert(EntityBase* entity, const CoordsXY& newLoc)
{
2021-09-29 09:19:47 -07:00
size_t newIndex = GetSpatialIndexOffset(newLoc);
2021-11-24 15:27:48 +02:00
auto& spatialVector = gEntitySpatialIndex[newIndex];
2021-11-24 15:37:47 +02:00
auto index = std::lower_bound(std::begin(spatialVector), std::end(spatialVector), entity->sprite_index);
spatialVector.insert(index, entity->sprite_index);
}
2021-11-24 15:37:47 +02:00
static void EntitySpatialRemove(EntityBase* entity)
{
2021-11-24 15:37:47 +02:00
size_t currentIndex = GetSpatialIndexOffset({ entity->x, entity->y });
2021-11-24 15:27:48 +02:00
auto& spatialVector = gEntitySpatialIndex[currentIndex];
2021-11-24 15:37:47 +02:00
auto index = std::lower_bound(std::begin(spatialVector), std::end(spatialVector), entity->sprite_index);
if (index != std::end(spatialVector) && *index == entity->sprite_index)
2021-01-23 07:36:46 +00:00
{
spatialVector.erase(index, index + 1);
}
else
{
log_warning("Bad sprite spatial index. Rebuilding the spatial index...");
2021-11-24 15:37:47 +02:00
ResetEntitySpatialIndices();
}
}
2021-11-24 15:37:47 +02:00
static void EntitySpatialMove(EntityBase* entity, const CoordsXY& newLoc)
{
2021-09-29 09:19:47 -07:00
size_t newIndex = GetSpatialIndexOffset(newLoc);
2021-11-24 15:37:47 +02:00
size_t currentIndex = GetSpatialIndexOffset({ entity->x, entity->y });
if (newIndex == currentIndex)
return;
2021-11-24 15:37:47 +02:00
EntitySpatialRemove(entity);
EntitySpatialInsert(entity, newLoc);
}
2021-08-25 17:42:09 +03:00
void EntityBase::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
2021-11-24 15:37:47 +02:00
EntitySpatialMove(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
x = loc.x;
y = loc.y;
z = loc.z;
2018-06-22 23:17:03 +02:00
}
else
{
2021-11-24 15:37:47 +02:00
EntitySetCoordinates(loc, this);
Invalidate(); // Invalidate new position.
2017-06-06 23:24:18 +02:00
}
2016-08-06 10:21:08 -06:00
}
2021-11-24 15:37:47 +02:00
void EntitySetCoordinates(const CoordsXYZ& entityPos, EntityBase* entity)
2018-06-22 23:17:03 +02:00
{
2021-11-24 15:37:47 +02:00
auto screenCoords = translate_3d_to_2d_with_z(get_current_rotation(), entityPos);
2014-09-20 16:18:02 +01:00
2021-11-24 15:37:47 +02:00
entity->SpriteRect = ScreenRect(
screenCoords - ScreenCoordsXY{ entity->sprite_width, entity->sprite_height_negative },
screenCoords + ScreenCoordsXY{ entity->sprite_width, entity->sprite_height_positive });
entity->SetLocation(entityPos);
}
/**
* Frees any dynamically attached memory to the entity, such as peep name.
*/
2021-09-12 18:29:37 +02:00
static void FreeEntity(EntityBase& entity)
{
2021-09-12 18:29:37 +02:00
auto* guest = entity.As<Guest>();
auto* staff = entity.As<Staff>();
if (staff != nullptr)
{
staff->SetName({});
2021-10-27 21:19:10 +01:00
staff->ClearPatrolArea();
}
else if (guest != nullptr)
{
guest->SetName({});
OpenRCT2::RideUse::GetHistory().RemoveHandle(guest->sprite_index);
OpenRCT2::RideUse::GetTypeHistory().RemoveHandle(guest->sprite_index);
}
}
/**
*
* rct2: 0x0069EDB6
*/
2021-11-24 15:37:47 +02:00
void EntityRemove(EntityBase* entity)
{
2021-11-24 15:37:47 +02:00
FreeEntity(*entity);
2021-11-24 15:37:47 +02:00
EntityTweener::Get().RemoveEntity(entity);
RemoveFromEntityList(entity); // remove from existing list
AddToFreeList(entity->sprite_index);
2021-11-24 15:37:47 +02:00
EntitySpatialRemove(entity);
EntityReset(entity);
}
/**
2021-11-24 15:37:47 +02:00
* Loops through all floating entities and removes them.
* Returns the amount of removed objects as feedback.
*/
2021-11-24 15:37:47 +02:00
uint16_t RemoveFloatingEntities()
{
uint16_t removed = 0;
2021-03-16 11:18:06 +00:00
for (auto* balloon : EntityList<Balloon>())
{
2021-11-24 15:37:47 +02:00
EntityRemove(balloon);
removed++;
}
2021-03-16 11:18:06 +00:00
for (auto* duck : EntityList<Duck>())
{
if (duck->IsFlying())
{
2021-11-24 15:37:47 +02:00
EntityRemove(duck);
removed++;
}
}
2021-03-16 11:18:06 +00:00
for (auto* money : EntityList<MoneyEffect>())
{
2021-11-24 15:37:47 +02:00
EntityRemove(money);
removed++;
}
return removed;
}
2021-11-24 15:37:47 +02:00
void EntitySetFlashing(EntityBase* entity, bool flashing)
{
2021-11-24 15:37:47 +02:00
assert(entity->sprite_index < MAX_ENTITIES);
_entityFlashingList[entity->sprite_index] = flashing;
}
2021-11-24 15:37:47 +02:00
bool EntityGetFlashing(EntityBase* entity)
{
2021-11-24 15:37:47 +02:00
assert(entity->sprite_index < MAX_ENTITIES);
return _entityFlashingList[entity->sprite_index];
}