2021-01-07 21:06:49 +02:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 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
|
|
|
|
2024-07-09 16:23:31 +02:00
|
|
|
#include "../Diagnostic.h"
|
2018-06-22 23:17:03 +02:00
|
|
|
#include "../Game.h"
|
2024-03-15 00:22:18 +01:00
|
|
|
#include "../GameState.h"
|
2022-03-08 19:49:43 +00:00
|
|
|
#include "../core/Algorithm.hpp"
|
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"
|
2024-09-10 23:55:53 +02:00
|
|
|
#include "../core/String.hpp"
|
2025-03-01 17:32:32 +01:00
|
|
|
#include "../entity/EntityList.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"
|
2021-08-17 21:29:35 +01:00
|
|
|
#include "../peep/RideUseSystem.h"
|
2022-01-18 10:21:20 -08:00
|
|
|
#include "../profiling/Profiling.h"
|
2021-05-29 06:01:32 +01:00
|
|
|
#include "../ride/Vehicle.h"
|
|
|
|
|
#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
|
|
|
|
2024-07-09 16:43:03 +02:00
|
|
|
#include <cassert>
|
2018-06-22 23:17:03 +02:00
|
|
|
#include <cmath>
|
2018-11-21 23:16:04 +01:00
|
|
|
#include <iterator>
|
2021-03-26 05:48:38 +00:00
|
|
|
#include <numeric>
|
2021-01-23 07:36:46 +00:00
|
|
|
#include <vector>
|
2014-05-25 19:47:11 +01:00
|
|
|
|
2024-03-15 00:22:18 +01:00
|
|
|
using namespace OpenRCT2;
|
2025-04-30 16:50:29 -04:00
|
|
|
using namespace OpenRCT2::Core;
|
2021-11-24 15:58:16 +02:00
|
|
|
|
2022-02-12 23:31:06 +02:00
|
|
|
static std::array<std::list<EntityId>, EnumValue(EntityType::Count)> gEntityLists;
|
|
|
|
|
static std::vector<EntityId> _freeIdList;
|
2016-09-12 18:56:52 +01:00
|
|
|
|
2025-01-19 16:59:45 +01:00
|
|
|
static bool _entityFlashingList[kMaxEntities];
|
2017-06-17 12:55:41 +02:00
|
|
|
|
2024-09-27 11:43:02 +03:00
|
|
|
static constexpr const uint32_t kSpatialIndexSize = (kMaximumMapSizeTechnical * kMaximumMapSizeTechnical) + 1;
|
|
|
|
|
static constexpr uint32_t kSpatialIndexNullBucket = kSpatialIndexSize - 1;
|
|
|
|
|
|
2024-09-27 11:34:05 +03:00
|
|
|
static constexpr uint32_t kInvalidSpatialIndex = 0xFFFFFFFFu;
|
|
|
|
|
static constexpr uint32_t kSpatialIndexDirtyMask = 1u << 31;
|
2021-05-29 06:01:32 +01:00
|
|
|
|
2024-09-27 11:43:02 +03:00
|
|
|
static std::array<std::vector<EntityId>, kSpatialIndexSize> gEntitySpatialIndex;
|
2016-05-09 00:08:03 +01:00
|
|
|
|
2021-09-12 18:29:37 +02:00
|
|
|
static void FreeEntity(EntityBase& entity);
|
2021-08-17 21:29:35 +01:00
|
|
|
|
2024-09-27 11:34:05 +03:00
|
|
|
static constexpr uint32_t ComputeSpatialIndex(const CoordsXY& loc)
|
2021-01-23 07:36:46 +00:00
|
|
|
{
|
2021-09-29 09:19:47 -07:00
|
|
|
if (loc.IsNull())
|
2024-09-27 11:43:02 +03:00
|
|
|
return kSpatialIndexNullBucket;
|
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.
|
2024-07-26 18:23:25 +10:00
|
|
|
const auto tileX = std::abs(loc.x) / kCoordsXYStep;
|
|
|
|
|
const auto tileY = std::abs(loc.y) / kCoordsXYStep;
|
2021-01-23 07:36:46 +00:00
|
|
|
|
2024-03-17 14:41:36 +01:00
|
|
|
if (tileX >= kMaximumMapSizeTechnical || tileY >= kMaximumMapSizeTechnical)
|
2024-09-27 11:43:02 +03:00
|
|
|
return kSpatialIndexNullBucket;
|
2021-09-27 23:05:48 +03:00
|
|
|
|
2024-03-17 14:41:36 +01:00
|
|
|
return tileX * kMaximumMapSizeTechnical + tileY;
|
2021-01-23 07:36:46 +00:00
|
|
|
}
|
2017-01-28 22:12:07 +01:00
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
static constexpr uint32_t GetSpatialIndex(EntityBase& entity)
|
2024-09-27 11:34:05 +03:00
|
|
|
{
|
2025-04-30 16:50:29 -04:00
|
|
|
return entity.SpatialIndex & ~kSpatialIndexDirtyMask;
|
2024-09-27 11:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
2021-03-22 21:24:50 +00:00
|
|
|
constexpr bool EntityTypeIsMiscEntity(const EntityType type)
|
2021-01-10 15:14:34 +00:00
|
|
|
{
|
2021-03-22 21:24:50 +00:00
|
|
|
switch (type)
|
2021-03-17 08:04:41 +00:00
|
|
|
{
|
|
|
|
|
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)
|
2020-07-04 07:53:10 +01:00
|
|
|
{
|
2021-03-16 11:18:06 +00:00
|
|
|
return static_cast<uint16_t>(gEntityLists[EnumValue(type)].size());
|
2020-07-04 07:53:10 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-26 07:42:56 +00:00
|
|
|
uint16_t GetNumFreeEntities()
|
|
|
|
|
{
|
|
|
|
|
return static_cast<uint16_t>(_freeIdList.size());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
std::string EntitiesChecksum::ToString() const
|
2018-12-11 03:05:57 +01:00
|
|
|
{
|
2024-02-25 09:42:25 -03:00
|
|
|
return String::StringFromHex(raw);
|
2018-12-11 03:05:57 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-12 23:31:06 +02:00
|
|
|
EntityBase* TryGetEntity(EntityId entityIndex)
|
2017-03-10 17:26:08 +00:00
|
|
|
{
|
2025-03-20 19:47:53 +01:00
|
|
|
auto& gameState = getGameState();
|
2022-02-12 23:31:06 +02:00
|
|
|
const auto idx = entityIndex.ToUnderlying();
|
2025-03-20 19:47:53 +01:00
|
|
|
return idx >= kMaxEntities ? nullptr : &gameState.entities[idx].base;
|
2017-03-10 17:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-12 23:31:06 +02:00
|
|
|
EntityBase* GetEntity(EntityId entityIndex)
|
2016-07-17 22:14:44 +02:00
|
|
|
{
|
2022-02-12 23:31:06 +02:00
|
|
|
if (entityIndex.IsNull())
|
2019-03-09 12:29:33 +00:00
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2025-01-19 16:59:45 +01:00
|
|
|
Guard::Assert(entityIndex.ToUnderlying() < kMaxEntities, "Tried getting entity %u", entityIndex.ToUnderlying());
|
2021-11-24 15:37:47 +02:00
|
|
|
return TryGetEntity(entityIndex);
|
2016-07-17 22:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-12 23:31:06 +02:00
|
|
|
const std::vector<EntityId>& GetEntityTileList(const CoordsXY& spritePos)
|
2015-09-05 13:59:12 +01:00
|
|
|
{
|
2024-09-27 11:34:05 +03:00
|
|
|
return gEntitySpatialIndex[ComputeSpatialIndex(spritePos)];
|
2015-09-05 13:59:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-26 05:48:38 +00:00
|
|
|
static void ResetEntityLists()
|
2021-01-21 18:36:34 +00:00
|
|
|
{
|
|
|
|
|
for (auto& list : gEntityLists)
|
|
|
|
|
{
|
|
|
|
|
list.clear();
|
|
|
|
|
}
|
2021-03-26 05:48:38 +00:00
|
|
|
}
|
2021-01-21 18:36:34 +00:00
|
|
|
|
2021-03-26 05:48:38 +00:00
|
|
|
static void ResetFreeIds()
|
|
|
|
|
{
|
2021-01-26 07:42:56 +00:00
|
|
|
_freeIdList.clear();
|
2025-01-19 16:59:45 +01:00
|
|
|
_freeIdList.resize(kMaxEntities);
|
2022-02-12 23:31:06 +02:00
|
|
|
|
2021-01-26 07:42:56 +00:00
|
|
|
// List needs to be back to front to simplify removing
|
2022-02-12 23:31:06 +02:00
|
|
|
auto nextId = 0;
|
|
|
|
|
std::for_each(std::rbegin(_freeIdList), std::rend(_freeIdList), [&](auto& elem) {
|
|
|
|
|
elem = EntityId::FromUnderlying(nextId);
|
|
|
|
|
nextId++;
|
|
|
|
|
});
|
2021-01-21 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-12 23:31:06 +02:00
|
|
|
const std::list<EntityId>& 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
|
|
|
/**
|
2014-06-20 15:31:28 +01:00
|
|
|
*
|
2015-12-11 09:38:37 -06:00
|
|
|
* rct2: 0x0069EB13
|
2014-06-20 15:31:28 +01:00
|
|
|
*/
|
2021-11-24 15:27:48 +02:00
|
|
|
void ResetAllEntities()
|
2016-05-09 00:08:03 +01:00
|
|
|
{
|
2021-09-18 19:07:35 +01:00
|
|
|
// Free all associated Entity pointers prior to zeroing memory
|
2025-01-19 16:59:45 +01:00
|
|
|
for (int32_t i = 0; i < kMaxEntities; ++i)
|
2021-09-18 19:07:35 +01:00
|
|
|
{
|
2022-02-12 23:31:06 +02:00
|
|
|
auto* spr = GetEntity(EntityId::FromUnderlying(i));
|
2021-09-18 19:07:35 +01:00
|
|
|
if (spr == nullptr)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
FreeEntity(*spr);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 19:47:53 +01:00
|
|
|
auto& gameState = getGameState();
|
|
|
|
|
std::fill(std::begin(gameState.entities), std::end(gameState.entities), Entity_t());
|
2021-08-17 21:29:35 +01:00
|
|
|
OpenRCT2::RideUse::GetHistory().Clear();
|
|
|
|
|
OpenRCT2::RideUse::GetTypeHistory().Clear();
|
2025-01-19 16:59:45 +01:00
|
|
|
for (int32_t i = 0; i < kMaxEntities; ++i)
|
2018-06-22 23:17:03 +02:00
|
|
|
{
|
2022-02-12 23:31:06 +02:00
|
|
|
auto* spr = GetEntity(EntityId::FromUnderlying(i));
|
2020-07-06 22:02:25 +01:00
|
|
|
if (spr == nullptr)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-03-17 08:04:41 +00:00
|
|
|
spr->Type = EntityType::Null;
|
2023-01-27 19:18:44 +02:00
|
|
|
spr->Id = EntityId::FromUnderlying(i);
|
2014-06-20 15:31:28 +01:00
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
_entityFlashingList[i] = false;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2021-03-26 05:48:38 +00:00
|
|
|
ResetEntityLists();
|
|
|
|
|
ResetFreeIds();
|
2021-11-24 15:37:47 +02:00
|
|
|
ResetEntitySpatialIndices();
|
2014-06-21 13:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
static void EntitySpatialInsert(EntityBase& entity, const CoordsXY& newLoc);
|
2021-01-23 07:36:46 +00:00
|
|
|
|
2015-06-08 21:35:09 +01: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();
|
|
|
|
|
}
|
2025-01-19 16:59:45 +01:00
|
|
|
for (EntityId::UnderlyingType i = 0; i < kMaxEntities; i++)
|
2018-06-22 23:17:03 +02:00
|
|
|
{
|
2024-09-27 11:34:05 +03:00
|
|
|
auto* entity = GetEntity(EntityId::FromUnderlying(i));
|
|
|
|
|
if (entity != nullptr && entity->Type != EntityType::Null)
|
2018-06-22 23:17:03 +02:00
|
|
|
{
|
2025-04-30 16:50:29 -04:00
|
|
|
EntitySpatialInsert(*entity, { entity->x, entity->y });
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2014-09-04 23:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-24 21:32:57 +02:00
|
|
|
#ifndef DISABLE_NETWORK
|
|
|
|
|
|
2024-10-29 16:54:01 +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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 16:54:01 +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()
|
2016-07-24 21:32:57 +02:00
|
|
|
{
|
2021-11-24 15:37:47 +02:00
|
|
|
EntitiesChecksum checksum{};
|
2018-06-01 19:17:13 +01:00
|
|
|
|
2021-05-09 19:12:44 +01:00
|
|
|
OpenRCT2::ChecksumStream ms(checksum.raw);
|
|
|
|
|
DataSerialiser ds(true, ms);
|
|
|
|
|
NetworkSerialiseEntityTypes<Guest, Staff, Vehicle, Litter>(ds);
|
2018-12-11 03:05:57 +01:00
|
|
|
|
|
|
|
|
return checksum;
|
2016-07-24 21:32:57 +02:00
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
EntitiesChecksum GetAllEntitiesChecksum()
|
2016-07-24 21:32:57 +02:00
|
|
|
{
|
2021-11-24 15:37:47 +02:00
|
|
|
return EntitiesChecksum{};
|
2016-07-24 21:32:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // DISABLE_NETWORK
|
|
|
|
|
|
2025-04-30 16:50:29 -04: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
|
2025-04-30 16:50:29 -04:00
|
|
|
auto entityIndex = entity.Id;
|
2022-02-12 23:31:06 +02:00
|
|
|
_entityFlashingList[entityIndex.ToUnderlying()] = false;
|
2016-07-23 13:32:58 +02:00
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
Entity_t* tempEntity = reinterpret_cast<Entity_t*>(&entity);
|
2024-03-15 00:22:18 +01:00
|
|
|
*tempEntity = Entity_t();
|
2016-07-23 13:32:58 +02:00
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
entity.Id = entityIndex;
|
|
|
|
|
entity.Type = EntityType::Null;
|
2016-07-23 13:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-19 16:59:45 +01:00
|
|
|
static constexpr uint16_t kMaxMiscEntities = 1600;
|
2024-01-19 12:40:14 +02:00
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
static void AddToEntityList(EntityBase& entity)
|
2021-01-21 18:36:34 +00:00
|
|
|
{
|
2025-04-30 16:50:29 -04:00
|
|
|
auto& list = gEntityLists[EnumValue(entity.Type)];
|
|
|
|
|
|
|
|
|
|
// Entity list is sorted by Id to prevent desyncs.
|
|
|
|
|
Algorithm::sortedInsert(list, entity.Id);
|
2021-01-21 18:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-12 23:31:06 +02:00
|
|
|
static void AddToFreeList(EntityId index)
|
2021-01-26 07:42:56 +00:00
|
|
|
{
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
static void RemoveFromEntityList(EntityBase& entity)
|
2021-01-21 18:36:34 +00:00
|
|
|
{
|
2025-04-30 16:50:29 -04:00
|
|
|
auto& list = gEntityLists[EnumValue(entity.Type)];
|
|
|
|
|
auto ptr = Algorithm::binaryFind(std::begin(list), std::end(list), entity.Id);
|
2022-03-08 19:49:43 +00:00
|
|
|
if (ptr != std::end(list))
|
2021-01-21 18:36:34 +00:00
|
|
|
{
|
|
|
|
|
list.erase(ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-19 17:10:57 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
static void PrepareNewEntity(EntityBase& base, const EntityType type)
|
2021-03-26 05:48:38 +00:00
|
|
|
{
|
|
|
|
|
// 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);
|
2021-03-26 05:48:38 +00:00
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
base.Type = type;
|
2021-03-26 05:48:38 +00:00
|
|
|
AddToEntityList(base);
|
|
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
base.x = kLocationNull;
|
|
|
|
|
base.y = kLocationNull;
|
|
|
|
|
base.z = 0;
|
|
|
|
|
base.SpriteData.Width = 0x10;
|
|
|
|
|
base.SpriteData.HeightMin = 0x14;
|
|
|
|
|
base.SpriteData.HeightMax = 0x8;
|
|
|
|
|
base.SpriteData.SpriteRect = {};
|
|
|
|
|
base.SpatialIndex = kInvalidSpatialIndex;
|
2021-03-26 05:48:38 +00:00
|
|
|
|
2024-07-26 18:23:25 +10:00
|
|
|
EntitySpatialInsert(base, { kLocationNull, 0 });
|
2021-03-26 05:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 17:42:09 +03:00
|
|
|
EntityBase* CreateEntity(EntityType type)
|
2014-09-04 23:21:17 +02:00
|
|
|
{
|
2021-01-26 07:42:56 +00:00
|
|
|
if (_freeIdList.size() == 0)
|
2018-06-22 23:17:03 +02:00
|
|
|
{
|
2019-06-19 17:10:57 +02:00
|
|
|
// No free sprites.
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 21:24:50 +00:00
|
|
|
if (EntityTypeIsMiscEntity(type))
|
2019-06-19 17:10:57 +02:00
|
|
|
{
|
2024-01-12 17:48:39 +02:00
|
|
|
// Misc sprites are commonly used for effects, give other entity types higher priority.
|
2025-01-19 16:59:45 +01:00
|
|
|
if (GetMiscEntityCount() >= kMaxMiscEntities)
|
2024-01-12 17:48:39 +02:00
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-19 16:59:45 +01:00
|
|
|
// If there are less than kMaxMiscEntities free slots, ensure other entities can be created.
|
|
|
|
|
if (_freeIdList.size() < kMaxMiscEntities)
|
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
|
|
|
}
|
|
|
|
|
}
|
2014-09-04 23:21:17 +02:00
|
|
|
|
2021-05-29 08:18:23 +01:00
|
|
|
auto* entity = GetEntity(_freeIdList.back());
|
|
|
|
|
if (entity == nullptr)
|
2020-07-06 22:02:25 +01:00
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-01-26 07:42:56 +00:00
|
|
|
_freeIdList.pop_back();
|
|
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
PrepareNewEntity(*entity, type);
|
2014-09-04 23:21:17 +02:00
|
|
|
|
2021-05-29 08:18:23 +01:00
|
|
|
return entity;
|
2014-09-05 19:31:25 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-12 23:31:06 +02:00
|
|
|
EntityBase* CreateEntityAt(const EntityId index, const EntityType type)
|
2021-03-26 05:48:38 +00:00
|
|
|
{
|
2025-04-30 16:50:29 -04:00
|
|
|
auto id = Algorithm::binaryFind(std::rbegin(_freeIdList), std::rend(_freeIdList), index);
|
2022-03-08 19:49:43 +00:00
|
|
|
if (id == std::rend(_freeIdList))
|
2021-03-26 05:48:38 +00:00
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* entity = GetEntity(index);
|
|
|
|
|
if (entity == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_freeIdList.erase(std::next(id).base());
|
|
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
PrepareNewEntity(*entity, type);
|
2021-03-26 05:48:38 +00:00
|
|
|
return entity;
|
|
|
|
|
}
|
2015-06-29 17:22:01 +01:00
|
|
|
|
2024-10-29 16:54:01 +01:00
|
|
|
template<typename T>
|
|
|
|
|
void MiscUpdateAllType()
|
2015-03-23 10:39:47 +00:00
|
|
|
{
|
2021-03-16 11:18:06 +00:00
|
|
|
for (auto misc : EntityList<T>())
|
2018-06-22 23:17:03 +02:00
|
|
|
{
|
2021-01-21 08:29:02 +00:00
|
|
|
misc->Update();
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-03-23 10:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-29 16:54:01 +01:00
|
|
|
template<typename... T>
|
|
|
|
|
void MiscUpdateAllTypes()
|
2021-01-21 08:29:02 +00:00
|
|
|
{
|
|
|
|
|
(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
|
|
|
{
|
2022-01-18 10:21:20 -08:00
|
|
|
PROFILED_FUNCTION();
|
|
|
|
|
|
2021-01-21 08:29:02 +00:00
|
|
|
MiscUpdateAllTypes<
|
|
|
|
|
SteamParticle, MoneyEffect, VehicleCrashParticle, ExplosionCloud, CrashSplashParticle, ExplosionFlare, JumpingFountain,
|
|
|
|
|
Balloon, Duck>();
|
2014-09-20 16:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-23 16:04:25 +02:00
|
|
|
void UpdateMoneyEffect()
|
|
|
|
|
{
|
|
|
|
|
MiscUpdateAllTypes<MoneyEffect>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 10:46:26 +01:00
|
|
|
// Performs a search to ensure that insert keeps next_in_quadrant in sprite_index order
|
2025-04-30 16:50:29 -04:00
|
|
|
static void EntitySpatialInsert(EntityBase& entity, const CoordsXY& newLoc)
|
2020-04-09 10:46:26 +01:00
|
|
|
{
|
2024-09-27 11:34:05 +03:00
|
|
|
const auto newIndex = ComputeSpatialIndex(newLoc);
|
|
|
|
|
|
2021-11-24 15:27:48 +02:00
|
|
|
auto& spatialVector = gEntitySpatialIndex[newIndex];
|
2024-09-27 11:34:05 +03:00
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
Algorithm::sortedInsert(spatialVector, entity.Id);
|
|
|
|
|
|
|
|
|
|
entity.SpatialIndex = newIndex;
|
2020-04-09 10:46:26 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
static void EntitySpatialRemove(EntityBase& entity)
|
2020-04-09 10:46:26 +01:00
|
|
|
{
|
2024-09-27 11:34:05 +03:00
|
|
|
const auto currentIndex = GetSpatialIndex(entity);
|
|
|
|
|
|
2021-11-24 15:27:48 +02:00
|
|
|
auto& spatialVector = gEntitySpatialIndex[currentIndex];
|
2025-04-30 16:50:29 -04:00
|
|
|
auto index = Algorithm::binaryFind(std::begin(spatialVector), std::end(spatialVector), entity.Id);
|
2022-03-08 19:49:43 +00:00
|
|
|
if (index != std::end(spatialVector))
|
2021-01-23 07:36:46 +00:00
|
|
|
{
|
|
|
|
|
spatialVector.erase(index, index + 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-04-09 10:46:26 +01:00
|
|
|
{
|
2023-01-17 13:24:51 +01:00
|
|
|
LOG_WARNING("Bad sprite spatial index. Rebuilding the spatial index...");
|
2021-11-24 15:37:47 +02:00
|
|
|
ResetEntitySpatialIndices();
|
2020-04-09 10:46:26 +01:00
|
|
|
}
|
2024-09-27 11:34:05 +03:00
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
entity.SpatialIndex = kInvalidSpatialIndex;
|
2020-04-09 10:46:26 +01:00
|
|
|
}
|
|
|
|
|
|
2025-05-25 21:37:58 +01:00
|
|
|
static void UpdateEntitySpatialIndex(EntityBase& entity)
|
|
|
|
|
{
|
|
|
|
|
if (entity.SpatialIndex & kSpatialIndexDirtyMask)
|
|
|
|
|
{
|
|
|
|
|
if (entity.SpatialIndex != kInvalidSpatialIndex)
|
|
|
|
|
{
|
|
|
|
|
EntitySpatialRemove(entity);
|
|
|
|
|
}
|
|
|
|
|
EntitySpatialInsert(entity, { entity.x, entity.y });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 11:34:05 +03:00
|
|
|
void UpdateEntitiesSpatialIndex()
|
2020-04-09 10:46:26 +01:00
|
|
|
{
|
2024-09-27 11:40:18 +03:00
|
|
|
for (auto& entityList : gEntityLists)
|
2024-09-27 11:34:05 +03:00
|
|
|
{
|
2024-09-27 11:40:18 +03:00
|
|
|
for (auto& entityId : entityList)
|
2024-09-27 11:34:05 +03:00
|
|
|
{
|
2025-03-21 01:21:00 +02:00
|
|
|
auto* entity = TryGetEntity(entityId);
|
2025-05-25 21:37:58 +01:00
|
|
|
if (entity != nullptr && entity->Type != EntityType::Null)
|
2024-09-27 11:34:05 +03:00
|
|
|
{
|
2025-05-25 21:37:58 +01:00
|
|
|
UpdateEntitySpatialIndex(*entity);
|
2024-09-27 11:34:05 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-09 10:46:26 +01:00
|
|
|
}
|
|
|
|
|
|
2024-10-12 04:57:19 +03:00
|
|
|
CoordsXYZ EntityBase::GetLocation() const
|
|
|
|
|
{
|
|
|
|
|
return { x, y, z };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EntityBase::SetLocation(const CoordsXYZ& newLocation)
|
|
|
|
|
{
|
2025-03-21 01:21:00 +02:00
|
|
|
if (GetLocation() == newLocation)
|
|
|
|
|
{
|
|
|
|
|
// No change, this can happen quite often when the entity is interpolated.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 04:57:19 +03:00
|
|
|
x = newLocation.x;
|
|
|
|
|
y = newLocation.y;
|
|
|
|
|
z = newLocation.z;
|
2025-03-21 01:21:00 +02:00
|
|
|
|
|
|
|
|
if (SpatialIndex & kSpatialIndexDirtyMask)
|
|
|
|
|
{
|
|
|
|
|
// Already marked as dirty.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto newSpatialIndex = ComputeSpatialIndex({ x, y });
|
2025-04-30 16:50:29 -04:00
|
|
|
if (newSpatialIndex == GetSpatialIndex(*this))
|
2025-03-21 01:21:00 +02:00
|
|
|
{
|
|
|
|
|
// Avoid marking it dirty when we don't leave the current tile.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 04:57:19 +03:00
|
|
|
SpatialIndex |= kSpatialIndexDirtyMask;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 19:49:47 +02:00
|
|
|
static void EntitySetCoordinates(const CoordsXYZ& entityPos, EntityBase* entity)
|
|
|
|
|
{
|
|
|
|
|
auto screenCoords = Translate3DTo2DWithZ(GetCurrentRotation(), entityPos);
|
|
|
|
|
|
|
|
|
|
entity->SpriteData.SpriteRect = ScreenRect(
|
|
|
|
|
screenCoords - ScreenCoordsXY{ entity->SpriteData.Width, entity->SpriteData.HeightMin },
|
|
|
|
|
screenCoords + ScreenCoordsXY{ entity->SpriteData.Width, entity->SpriteData.HeightMax });
|
|
|
|
|
entity->SetLocation(entityPos);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 17:42:09 +03:00
|
|
|
void EntityBase::MoveTo(const CoordsXYZ& newLocation)
|
2017-01-28 22:12:07 +01:00
|
|
|
{
|
2024-07-26 18:23:25 +10:00
|
|
|
if (x != kLocationNull)
|
2020-12-31 18:43:48 +02:00
|
|
|
{
|
|
|
|
|
// Invalidate old position.
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 12:36:45 +02:00
|
|
|
auto loc = newLocation;
|
2022-10-12 06:35:20 +01:00
|
|
|
if (!MapIsLocationValid(loc))
|
2018-06-22 23:17:03 +02:00
|
|
|
{
|
2024-07-26 18:23:25 +10:00
|
|
|
loc.x = kLocationNull;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2014-09-20 16:18:02 +01:00
|
|
|
|
2024-07-26 18:23:25 +10:00
|
|
|
if (loc.x == kLocationNull)
|
2018-06-22 23:17:03 +02:00
|
|
|
{
|
2024-10-12 04:57:19 +03:00
|
|
|
SetLocation(loc);
|
2018-06-22 23:17:03 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-11-24 15:37:47 +02:00
|
|
|
EntitySetCoordinates(loc, this);
|
2020-12-31 18:43:48 +02:00
|
|
|
Invalidate(); // Invalidate new position.
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2016-08-06 10:21:08 -06:00
|
|
|
}
|
|
|
|
|
|
2025-05-25 21:37:58 +01:00
|
|
|
void EntityBase::MoveToAndUpdateSpatialIndex(const CoordsXYZ& newLocation)
|
|
|
|
|
{
|
|
|
|
|
MoveTo(newLocation);
|
|
|
|
|
UpdateEntitySpatialIndex(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 21:29:35 +01:00
|
|
|
/**
|
|
|
|
|
* 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-08-17 21:29:35 +01:00
|
|
|
{
|
2021-09-12 18:29:37 +02:00
|
|
|
auto* guest = entity.As<Guest>();
|
|
|
|
|
auto* staff = entity.As<Staff>();
|
2021-08-17 21:29:35 +01:00
|
|
|
if (staff != nullptr)
|
|
|
|
|
{
|
|
|
|
|
staff->SetName({});
|
2021-10-27 21:19:10 +01:00
|
|
|
staff->ClearPatrolArea();
|
2021-08-17 21:29:35 +01:00
|
|
|
}
|
|
|
|
|
else if (guest != nullptr)
|
|
|
|
|
{
|
|
|
|
|
guest->SetName({});
|
2025-01-06 20:04:39 +02:00
|
|
|
guest->GuestNextInQueue = EntityId::GetNull();
|
|
|
|
|
|
2023-01-27 19:18:44 +02:00
|
|
|
OpenRCT2::RideUse::GetHistory().RemoveHandle(guest->Id);
|
|
|
|
|
OpenRCT2::RideUse::GetTypeHistory().RemoveHandle(guest->Id);
|
2021-08-17 21:29:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-10 01:44:30 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* rct2: 0x0069EDB6
|
|
|
|
|
*/
|
2021-11-24 15:37:47 +02:00
|
|
|
void EntityRemove(EntityBase* entity)
|
2014-12-10 01:44:30 +00:00
|
|
|
{
|
2021-11-24 15:37:47 +02:00
|
|
|
FreeEntity(*entity);
|
2019-02-20 20:03:48 +00:00
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
EntityTweener::Get().RemoveEntity(entity);
|
2025-04-30 16:50:29 -04:00
|
|
|
RemoveFromEntityList(*entity); // remove from existing list
|
2023-01-27 19:18:44 +02:00
|
|
|
AddToFreeList(entity->Id);
|
2021-01-05 01:35:18 +02:00
|
|
|
|
2025-04-30 16:50:29 -04:00
|
|
|
EntitySpatialRemove(*entity);
|
|
|
|
|
EntityReset(*entity);
|
2015-06-28 17:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-16 09:48:26 +01:00
|
|
|
/**
|
2021-11-24 15:37:47 +02:00
|
|
|
* Loops through all floating entities and removes them.
|
2020-03-16 09:48:26 +01:00
|
|
|
* Returns the amount of removed objects as feedback.
|
|
|
|
|
*/
|
2021-11-24 15:37:47 +02:00
|
|
|
uint16_t RemoveFloatingEntities()
|
2020-03-16 09:48:26 +01:00
|
|
|
{
|
|
|
|
|
uint16_t removed = 0;
|
2021-03-16 11:18:06 +00:00
|
|
|
for (auto* balloon : EntityList<Balloon>())
|
2020-03-16 09:48:26 +01:00
|
|
|
{
|
2021-11-24 15:37:47 +02:00
|
|
|
EntityRemove(balloon);
|
2021-01-22 14:36:55 +00:00
|
|
|
removed++;
|
|
|
|
|
}
|
2021-03-16 11:18:06 +00:00
|
|
|
for (auto* duck : EntityList<Duck>())
|
2021-01-22 14:36:55 +00:00
|
|
|
{
|
|
|
|
|
if (duck->IsFlying())
|
2020-03-16 09:48:26 +01:00
|
|
|
{
|
2021-11-24 15:37:47 +02:00
|
|
|
EntityRemove(duck);
|
2020-03-16 09:48:26 +01:00
|
|
|
removed++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-16 11:18:06 +00:00
|
|
|
for (auto* money : EntityList<MoneyEffect>())
|
2021-01-22 14:36:55 +00:00
|
|
|
{
|
2021-11-24 15:37:47 +02:00
|
|
|
EntityRemove(money);
|
2021-01-22 14:36:55 +00:00
|
|
|
removed++;
|
|
|
|
|
}
|
2020-03-16 09:48:26 +01:00
|
|
|
return removed;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
void EntitySetFlashing(EntityBase* entity, bool flashing)
|
2017-06-17 12:55:41 +02:00
|
|
|
{
|
2025-01-19 16:59:45 +01:00
|
|
|
assert(entity->Id.ToUnderlying() < kMaxEntities);
|
2023-01-27 19:18:44 +02:00
|
|
|
_entityFlashingList[entity->Id.ToUnderlying()] = flashing;
|
2017-06-17 12:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
bool EntityGetFlashing(EntityBase* entity)
|
2017-06-17 12:55:41 +02:00
|
|
|
{
|
2025-01-19 16:59:45 +01:00
|
|
|
assert(entity->Id.ToUnderlying() < kMaxEntities);
|
2023-01-27 19:18:44 +02:00
|
|
|
return _entityFlashingList[entity->Id.ToUnderlying()];
|
2017-06-17 12:55:41 +02:00
|
|
|
}
|