2021-01-07 21:06:49 +02:00
|
|
|
/*****************************************************************************
|
2024-01-01 05:52:28 -06:00
|
|
|
* Copyright (c) 2014-2024 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"
|
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"
|
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"
|
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>
|
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
|
|
|
|
2021-11-24 15:58:16 +02:00
|
|
|
union Entity
|
|
|
|
|
{
|
2023-01-21 16:39:35 +01:00
|
|
|
uint8_t Pad00[0x200];
|
2021-11-24 18:25:43 +02:00
|
|
|
EntityBase base;
|
|
|
|
|
Entity()
|
2023-01-21 16:39:35 +01:00
|
|
|
: Pad00()
|
2021-11-24 18:25:43 +02:00
|
|
|
{
|
|
|
|
|
}
|
2021-11-24 15:58:16 +02:00
|
|
|
};
|
|
|
|
|
|
2021-11-24 16:22:29 +02:00
|
|
|
static Entity _entities[MAX_ENTITIES]{};
|
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
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
static bool _entityFlashingList[MAX_ENTITIES];
|
2017-06-17 12:55:41 +02:00
|
|
|
|
2021-05-29 06:01:32 +01:00
|
|
|
constexpr const uint32_t SPATIAL_INDEX_SIZE = (MAXIMUM_MAP_SIZE_TECHNICAL * MAXIMUM_MAP_SIZE_TECHNICAL) + 1;
|
2023-06-24 11:45:26 +02:00
|
|
|
constexpr uint32_t SPATIAL_INDEX_LOCATION_NULL = SPATIAL_INDEX_SIZE - 1;
|
2021-05-29 06:01:32 +01:00
|
|
|
|
2022-02-12 23:31:06 +02:00
|
|
|
static std::array<std::vector<EntityId>, 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-08-17 21:29:35 +01:00
|
|
|
|
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())
|
2021-09-27 23:05:48 +03:00
|
|
|
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-27 23:05:48 +03:00
|
|
|
|
2021-09-29 09:19:47 -07:00
|
|
|
return tileX * MAXIMUM_MAP_SIZE_TECHNICAL + tileY;
|
2021-01-23 07:36:46 +00:00
|
|
|
}
|
2017-01-28 22:12:07 +01: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
|
|
|
{
|
|
|
|
|
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));
|
2018-12-11 03:05:57 +01:00
|
|
|
result.append(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 23:31:06 +02:00
|
|
|
EntityBase* TryGetEntity(EntityId entityIndex)
|
2017-03-10 17:26:08 +00:00
|
|
|
{
|
2022-02-12 23:31:06 +02:00
|
|
|
const auto idx = entityIndex.ToUnderlying();
|
|
|
|
|
return idx >= MAX_ENTITIES ? nullptr : &_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;
|
|
|
|
|
}
|
2023-07-02 17:42:46 +03:00
|
|
|
Guard::Assert(entityIndex.ToUnderlying() < MAX_ENTITIES, "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
|
|
|
{
|
2021-11-24 15:27:48 +02:00
|
|
|
return gEntitySpatialIndex[GetSpatialIndexOffset(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();
|
2021-03-26 05:48:38 +00:00
|
|
|
_freeIdList.resize(MAX_ENTITIES);
|
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
|
|
|
|
|
for (int32_t i = 0; i < MAX_ENTITIES; ++i)
|
|
|
|
|
{
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
std::fill(std::begin(_entities), std::end(_entities), Entity());
|
2021-08-17 21:29:35 +01:00
|
|
|
OpenRCT2::RideUse::GetHistory().Clear();
|
|
|
|
|
OpenRCT2::RideUse::GetTypeHistory().Clear();
|
2021-02-24 23:34:34 +01:00
|
|
|
for (int32_t i = 0; i < MAX_ENTITIES; ++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
|
|
|
}
|
|
|
|
|
|
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-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();
|
|
|
|
|
}
|
2022-02-13 01:48:36 +02:00
|
|
|
for (EntityId::UnderlyingType i = 0; i < MAX_ENTITIES; i++)
|
2018-06-22 23:17:03 +02:00
|
|
|
{
|
2022-02-12 23:31:06 +02:00
|
|
|
auto* spr = GetEntity(EntityId::FromUnderlying(i));
|
2021-03-17 08:04:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2014-09-04 23:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-24 21:32:57 +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()
|
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
|
|
|
|
|
|
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
|
2023-01-27 19:18:44 +02: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
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
Entity* spr = reinterpret_cast<Entity*>(entity);
|
|
|
|
|
*spr = Entity();
|
2016-07-23 13:32:58 +02:00
|
|
|
|
2023-01-27 19:18:44 +02:00
|
|
|
entity->Id = entityIndex;
|
2021-11-24 15:37:47 +02:00
|
|
|
entity->Type = EntityType::Null;
|
2016-07-23 13:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 12:40:14 +02:00
|
|
|
static constexpr uint16_t MAX_MISC_SPRITES = 1600;
|
|
|
|
|
|
2021-08-25 17:42:09 +03:00
|
|
|
static void AddToEntityList(EntityBase* entity)
|
2021-01-21 18:36:34 +00:00
|
|
|
{
|
2021-03-17 08:04:41 +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
|
2023-01-27 19:18:44 +02:00
|
|
|
list.insert(std::lower_bound(std::begin(list), std::end(list), entity->Id), 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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 17:42:09 +03:00
|
|
|
static void RemoveFromEntityList(EntityBase* entity)
|
2021-01-21 18:36:34 +00:00
|
|
|
{
|
2021-03-17 08:04:41 +00:00
|
|
|
auto& list = gEntityLists[EnumValue(entity->Type)];
|
2023-01-27 19:18:44 +02:00
|
|
|
auto ptr = 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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 17:42:09 +03: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
|
|
|
|
|
|
|
|
base->Type = type;
|
|
|
|
|
AddToEntityList(base);
|
|
|
|
|
|
|
|
|
|
base->x = LOCATION_NULL;
|
|
|
|
|
base->y = LOCATION_NULL;
|
|
|
|
|
base->z = 0;
|
2023-04-07 19:47:26 +03:00
|
|
|
base->SpriteData.Width = 0x10;
|
|
|
|
|
base->SpriteData.HeightMin = 0x14;
|
|
|
|
|
base->SpriteData.HeightMax = 0x8;
|
2023-04-07 19:37:47 +03:00
|
|
|
base->SpriteData.SpriteRect = {};
|
2021-03-26 05:48:38 +00:00
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
EntitySpatialInsert(base, { LOCATION_NULL, 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.
|
|
|
|
|
if (GetMiscEntityCount() >= MAX_MISC_SPRITES)
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there are less than MAX_MISC_SPRITES free slots, ensure other entities can be created.
|
|
|
|
|
if (_freeIdList.size() < MAX_MISC_SPRITES)
|
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();
|
|
|
|
|
|
2021-05-29 08:18:23 +01: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
|
|
|
{
|
2023-01-16 20:24:52 +01:00
|
|
|
auto id = 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());
|
|
|
|
|
|
|
|
|
|
PrepareNewEntity(entity, type);
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
2015-06-29 17:22:01 +01:00
|
|
|
|
2021-01-21 08:29:02 +00: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
|
|
|
}
|
|
|
|
|
|
2021-01-21 08:29:02 +00: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
|
|
|
{
|
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
|
2021-11-24 15:37:47 +02:00
|
|
|
static void EntitySpatialInsert(EntityBase* entity, const CoordsXY& newLoc)
|
2020-04-09 10:46:26 +01:00
|
|
|
{
|
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];
|
2023-01-27 19:18:44 +02:00
|
|
|
auto index = std::lower_bound(std::begin(spatialVector), std::end(spatialVector), entity->Id);
|
|
|
|
|
spatialVector.insert(index, entity->Id);
|
2020-04-09 10:46:26 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
static void EntitySpatialRemove(EntityBase* entity)
|
2020-04-09 10:46:26 +01:00
|
|
|
{
|
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];
|
2023-01-27 19:18:44 +02:00
|
|
|
auto index = 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
static void EntitySpatialMove(EntityBase* entity, const CoordsXY& newLoc)
|
2020-04-09 10:46:26 +01:00
|
|
|
{
|
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 });
|
2020-04-09 10:46:26 +01:00
|
|
|
if (newIndex == currentIndex)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
EntitySpatialRemove(entity);
|
|
|
|
|
EntitySpatialInsert(entity, newLoc);
|
2020-04-09 10:46:26 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 17:42:09 +03:00
|
|
|
void EntityBase::MoveTo(const CoordsXYZ& newLocation)
|
2017-01-28 22:12:07 +01:00
|
|
|
{
|
2020-12-31 18:43:48 +02:00
|
|
|
if (x != LOCATION_NULL)
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
{
|
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);
|
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
|
|
|
}
|
|
|
|
|
|
2021-11-24 15:37:47 +02:00
|
|
|
void EntitySetCoordinates(const CoordsXYZ& entityPos, EntityBase* entity)
|
2018-06-22 23:17:03 +02:00
|
|
|
{
|
2023-01-16 21:14:50 +01:00
|
|
|
auto screenCoords = Translate3DTo2DWithZ(GetCurrentRotation(), entityPos);
|
2014-09-20 16:18:02 +01:00
|
|
|
|
2023-04-07 19:37:47 +03:00
|
|
|
entity->SpriteData.SpriteRect = ScreenRect(
|
2023-04-07 19:47:26 +03:00
|
|
|
screenCoords - ScreenCoordsXY{ entity->SpriteData.Width, entity->SpriteData.HeightMin },
|
|
|
|
|
screenCoords + ScreenCoordsXY{ entity->SpriteData.Width, entity->SpriteData.HeightMax });
|
2021-11-24 15:37:47 +02:00
|
|
|
entity->SetLocation(entityPos);
|
2014-11-10 00:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
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({});
|
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);
|
|
|
|
|
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
|
|
|
|
2021-11-24 15:37:47 +02: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
|
|
|
{
|
2023-01-27 19:18:44 +02:00
|
|
|
assert(entity->Id.ToUnderlying() < MAX_ENTITIES);
|
|
|
|
|
_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
|
|
|
{
|
2023-01-27 19:18:44 +02:00
|
|
|
assert(entity->Id.ToUnderlying() < MAX_ENTITIES);
|
|
|
|
|
return _entityFlashingList[entity->Id.ToUnderlying()];
|
2017-06-17 12:55:41 +02:00
|
|
|
}
|