2021-12-05 17:54:36 +00:00
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "RakNetTypes.h"
|
|
|
|
#include "Game.h"
|
|
|
|
#include "User.h"
|
|
|
|
#include "../dWorldServer/ObjectIDManager.h"
|
|
|
|
#include "Character.h"
|
|
|
|
#include "GeneralUtils.h"
|
|
|
|
#include "dServer.h"
|
|
|
|
#include "Spawner.h"
|
|
|
|
#include "Player.h"
|
|
|
|
#include "SkillComponent.h"
|
|
|
|
#include "SwitchComponent.h"
|
|
|
|
#include "UserManager.h"
|
|
|
|
#include "PacketUtils.h"
|
|
|
|
#include "Metrics.hpp"
|
|
|
|
#include "dZoneManager.h"
|
|
|
|
#include "MissionComponent.h"
|
|
|
|
#include "Game.h"
|
|
|
|
#include "dLogger.h"
|
2023-01-07 05:17:05 +00:00
|
|
|
#include "MessageIdentifiers.h"
|
2023-01-12 19:16:24 +00:00
|
|
|
#include "dConfig.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
EntityManager* EntityManager::m_Address = nullptr;
|
|
|
|
|
|
|
|
// Configure which zones have ghosting disabled, mostly small worlds.
|
|
|
|
std::vector<LWOMAPID> EntityManager::m_GhostingExcludedZones = {
|
|
|
|
// Small zones
|
|
|
|
1000,
|
2022-05-09 03:07:07 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Racing zones
|
|
|
|
1203,
|
2022-05-09 03:07:07 +00:00
|
|
|
1261,
|
2021-12-05 17:54:36 +00:00
|
|
|
1303,
|
|
|
|
1403,
|
2022-05-09 03:07:07 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Property zones
|
|
|
|
1150,
|
|
|
|
1151,
|
|
|
|
1250,
|
|
|
|
1251,
|
|
|
|
1350,
|
|
|
|
1450
|
|
|
|
};
|
|
|
|
|
|
|
|
// Configure some exceptions for ghosting, nessesary for some special objects.
|
|
|
|
std::vector<LOT> EntityManager::m_GhostingExcludedLOTs = {
|
|
|
|
// NT - Pipes
|
|
|
|
9524,
|
|
|
|
12408,
|
|
|
|
|
2022-02-05 11:28:17 +00:00
|
|
|
// AG - Footrace
|
2021-12-05 17:54:36 +00:00
|
|
|
4967
|
|
|
|
};
|
|
|
|
|
|
|
|
void EntityManager::Initialize() {
|
|
|
|
// Check if this zone has ghosting enabled
|
|
|
|
m_GhostingEnabled = std::find(
|
|
|
|
m_GhostingExcludedZones.begin(),
|
|
|
|
m_GhostingExcludedZones.end(),
|
|
|
|
dZoneManager::Instance()->GetZoneID().GetMapID()
|
|
|
|
) == m_GhostingExcludedZones.end();
|
2023-01-12 19:16:24 +00:00
|
|
|
|
|
|
|
// grab hardcore mode settings and load them with sane defaults
|
|
|
|
auto hcmode = Game::config->GetValue("hardcore_mode");
|
|
|
|
m_HardcoreMode = hcmode.empty() ? false : (hcmode == "1");
|
|
|
|
auto hcUscorePercent = Game::config->GetValue("hardcore_lose_uscore_on_death_percent");
|
|
|
|
m_HardcoreLoseUscoreOnDeathPercent = hcUscorePercent.empty() ? 10 : std::stoi(hcUscorePercent);
|
|
|
|
auto hcUscoreMult = Game::config->GetValue("hardcore_uscore_enemies_multiplier");
|
|
|
|
m_HardcoreUscoreEnemiesMultiplier = hcUscoreMult.empty() ? 2 : std::stoi(hcUscoreMult);
|
|
|
|
auto hcDropInv = Game::config->GetValue("hardcore_dropinventory_on_death");
|
|
|
|
m_HardcoreDropinventoryOnDeath = hcDropInv.empty() ? false : (hcDropInv == "1");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EntityManager::~EntityManager() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Entity* EntityManager::CreateEntity(EntityInfo info, User* user, Entity* parentEntity, const bool controller, const LWOOBJID explicitId) {
|
|
|
|
|
|
|
|
// Determine the objectID for the new entity
|
2022-07-28 13:39:57 +00:00
|
|
|
LWOOBJID id;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// If an explicit ID was provided, use it
|
|
|
|
if (explicitId != LWOOBJID_EMPTY) {
|
|
|
|
id = explicitId;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For non player entites, we'll generate a new ID or set the appropiate flags
|
|
|
|
else if (user == nullptr || info.lot != 1) {
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Entities with no ID already set, often spawned entities, we'll generate a new sequencial ID
|
2022-07-28 13:39:57 +00:00
|
|
|
if (info.id == 0) {
|
2021-12-05 17:54:36 +00:00
|
|
|
id = ObjectIDManager::Instance()->GenerateObjectID();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entities with an ID already set, often level entities, we'll use that ID as a base
|
2022-07-28 13:39:57 +00:00
|
|
|
else {
|
2021-12-05 17:54:36 +00:00
|
|
|
id = info.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exclude the zone control object from any flags
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!controller && info.lot != 14) {
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// The client flags means the client should render the entity
|
|
|
|
id = GeneralUtils::SetBit(id, OBJECT_BIT_CLIENT);
|
|
|
|
|
|
|
|
// Spawned entities require the spawned flag to render
|
|
|
|
if (info.spawnerID != 0) {
|
|
|
|
id = GeneralUtils::SetBit(id, OBJECT_BIT_SPAWNED);
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// For players, we'll use the persistent ID for that character
|
|
|
|
else {
|
|
|
|
id = user->GetLastUsedChar()->GetObjectID();
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
info.id = id;
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
Entity* entity;
|
|
|
|
|
|
|
|
// Check if the entitty if a player, in case use the extended player entity class
|
|
|
|
if (user != nullptr) {
|
|
|
|
entity = new Player(id, info, user, parentEntity);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
entity = new Entity(id, info, parentEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the entity
|
|
|
|
entity->Initialize();
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Add the entity to the entity map
|
|
|
|
m_Entities.insert_or_assign(id, entity);
|
|
|
|
|
|
|
|
// Set the zone control entity if the entity is a zone control object, this should only happen once
|
|
|
|
if (controller) {
|
|
|
|
m_ZoneControlEntity = entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this entity is a respawn point, if so add it to the registry
|
|
|
|
const auto& spawnName = entity->GetVar<std::u16string>(u"respawnname");
|
|
|
|
|
|
|
|
if (!spawnName.empty()) {
|
|
|
|
m_SpawnPoints.insert_or_assign(GeneralUtils::UTF16ToWTF8(spawnName), entity->GetObjectID());
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return entity;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::DestroyEntity(const LWOOBJID& objectID) {
|
|
|
|
DestroyEntity(GetEntity(objectID));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::DestroyEntity(Entity* entity) {
|
|
|
|
if (entity == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto id = entity->GetObjectID();
|
|
|
|
|
|
|
|
if (std::count(m_EntitiesToDelete.begin(), m_EntitiesToDelete.end(), id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destruct networked entities
|
|
|
|
if (entity->GetNetworkId() != 0) {
|
|
|
|
DestructEntity(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete this entity at the end of the frame
|
|
|
|
ScheduleForDeletion(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::UpdateEntities(const float deltaTime) {
|
|
|
|
for (const auto& e : m_Entities) {
|
2022-07-22 02:09:25 +00:00
|
|
|
e.second->Update(deltaTime);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 02:09:25 +00:00
|
|
|
for (auto entry = m_EntitiesToSerialize.begin(); entry != m_EntitiesToSerialize.end(); entry++) {
|
|
|
|
auto* entity = GetEntity(*entry);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (entity == nullptr) continue;
|
|
|
|
|
|
|
|
m_SerializationCounter++;
|
|
|
|
|
|
|
|
RakNet::BitStream stream;
|
|
|
|
stream.Write(static_cast<char>(ID_REPLICA_MANAGER_SERIALIZE));
|
|
|
|
stream.Write(static_cast<unsigned short>(entity->GetNetworkId()));
|
|
|
|
|
|
|
|
entity->WriteBaseReplicaData(&stream, PACKET_TYPE_SERIALIZATION);
|
|
|
|
entity->WriteComponents(&stream, PACKET_TYPE_SERIALIZATION);
|
|
|
|
|
2022-07-22 02:09:25 +00:00
|
|
|
if (entity->GetIsGhostingCandidate()) {
|
|
|
|
for (auto* player : Player::GetAllPlayers()) {
|
|
|
|
if (player->IsObserved(*entry)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::server->Send(&stream, player->GetSystemAddress(), false);
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 02:09:25 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::server->Send(&stream, UNASSIGNED_SYSTEM_ADDRESS, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_EntitiesToSerialize.clear();
|
|
|
|
|
2022-07-22 02:09:25 +00:00
|
|
|
for (auto entry = m_EntitiesToKill.begin(); entry != m_EntitiesToKill.end(); entry++) {
|
|
|
|
auto* entity = GetEntity(*entry);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (!entity) continue;
|
|
|
|
|
2022-07-22 02:09:25 +00:00
|
|
|
if (entity->GetScheduledKiller()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
entity->Smash(entity->GetScheduledKiller()->GetObjectID(), SILENT);
|
2022-07-22 02:09:25 +00:00
|
|
|
} else {
|
|
|
|
entity->Smash(LWOOBJID_EMPTY, SILENT);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_EntitiesToKill.clear();
|
|
|
|
|
2022-07-22 02:09:25 +00:00
|
|
|
for (auto entry = m_EntitiesToDelete.begin(); entry != m_EntitiesToDelete.end(); entry++) {
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-22 02:09:25 +00:00
|
|
|
// Get all this info first before we delete the player.
|
|
|
|
auto entityToDelete = GetEntity(*entry);
|
2022-06-19 07:14:33 +00:00
|
|
|
auto networkIdToErase = entityToDelete->GetNetworkId();
|
|
|
|
const auto& ghostingToDelete = std::find(m_EntitiesToGhost.begin(), m_EntitiesToGhost.end(), entityToDelete);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-22 02:09:25 +00:00
|
|
|
if (entityToDelete) {
|
2022-06-19 07:14:33 +00:00
|
|
|
// If we are a player run through the player destructor.
|
2022-07-22 02:09:25 +00:00
|
|
|
if (entityToDelete->IsPlayer()) {
|
2022-06-19 07:14:33 +00:00
|
|
|
delete dynamic_cast<Player*>(entityToDelete);
|
2022-07-22 02:09:25 +00:00
|
|
|
} else {
|
2022-06-19 07:14:33 +00:00
|
|
|
delete entityToDelete;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-06-19 07:14:33 +00:00
|
|
|
entityToDelete = nullptr;
|
2022-07-22 02:09:25 +00:00
|
|
|
if (networkIdToErase != 0) m_LostNetworkIds.push(networkIdToErase);
|
2022-06-19 07:14:33 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-22 02:09:25 +00:00
|
|
|
if (ghostingToDelete != m_EntitiesToGhost.end()) m_EntitiesToGhost.erase(ghostingToDelete);
|
2022-06-19 07:14:33 +00:00
|
|
|
|
2022-07-22 02:09:25 +00:00
|
|
|
m_Entities.erase(*entry);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
m_EntitiesToDelete.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Entity* EntityManager::GetEntity(const LWOOBJID& objectId) const {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& index = m_Entities.find(objectId);
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (index == m_Entities.end()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
return index->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Entity*> EntityManager::GetEntitiesInGroup(const std::string& group) {
|
|
|
|
std::vector<Entity*> entitiesInGroup;
|
|
|
|
for (const auto& entity : m_Entities) {
|
|
|
|
for (const auto& entityGroup : entity.second->GetGroups()) {
|
|
|
|
if (entityGroup == group) {
|
|
|
|
entitiesInGroup.push_back(entity.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entitiesInGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Entity*> EntityManager::GetEntitiesByComponent(const int componentType) const {
|
|
|
|
std::vector<Entity*> withComp;
|
|
|
|
for (const auto& entity : m_Entities) {
|
|
|
|
if (componentType != -1 && !entity.second->HasComponent(componentType)) continue;
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
withComp.push_back(entity.second);
|
|
|
|
}
|
|
|
|
return withComp;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::vector<Entity*> EntityManager::GetEntitiesByLOT(const LOT& lot) const {
|
|
|
|
std::vector<Entity*> entities;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& entity : m_Entities) {
|
|
|
|
if (entity.second->GetLOT() == lot)
|
|
|
|
entities.push_back(entity.second);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return entities;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Entity* EntityManager::GetZoneControlEntity() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return m_ZoneControlEntity;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Entity* EntityManager::GetSpawnPointEntity(const std::string& spawnName) const {
|
2021-12-05 17:54:36 +00:00
|
|
|
// Lookup the spawn point entity in the map
|
|
|
|
const auto& spawnPoint = m_SpawnPoints.find(spawnName);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (spawnPoint == m_SpawnPoints.end()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the spawn point entity is valid just in case
|
|
|
|
return GetEntity(spawnPoint->second);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const std::unordered_map<std::string, LWOOBJID>& EntityManager::GetSpawnPointEntities() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return m_SpawnPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::ConstructEntity(Entity* entity, const SystemAddress& sysAddr, const bool skipChecks) {
|
2022-07-28 13:39:57 +00:00
|
|
|
if (entity->GetNetworkId() == 0) {
|
2021-12-05 17:54:36 +00:00
|
|
|
uint16_t networkId;
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!m_LostNetworkIds.empty()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
networkId = m_LostNetworkIds.top();
|
|
|
|
m_LostNetworkIds.pop();
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
networkId = ++m_NetworkIdCounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
entity->SetNetworkId(networkId);
|
|
|
|
}
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto checkGhosting = entity->GetIsGhostingCandidate();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (checkGhosting) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& iter = std::find(m_EntitiesToGhost.begin(), m_EntitiesToGhost.end(), entity);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (iter == m_EntitiesToGhost.end()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_EntitiesToGhost.push_back(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (checkGhosting && sysAddr == UNASSIGNED_SYSTEM_ADDRESS) {
|
2021-12-05 17:54:36 +00:00
|
|
|
CheckGhosting(entity);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_SerializationCounter++;
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
RakNet::BitStream stream;
|
|
|
|
|
|
|
|
stream.Write(static_cast<char>(ID_REPLICA_MANAGER_CONSTRUCTION));
|
|
|
|
stream.Write(true);
|
|
|
|
stream.Write(static_cast<unsigned short>(entity->GetNetworkId()));
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
entity->WriteBaseReplicaData(&stream, PACKET_TYPE_CONSTRUCTION);
|
|
|
|
entity->WriteComponents(&stream, PACKET_TYPE_CONSTRUCTION);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) {
|
|
|
|
if (skipChecks) {
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::server->Send(&stream, UNASSIGNED_SYSTEM_ADDRESS, true);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
|
|
|
for (auto* player : Player::GetAllPlayers()) {
|
|
|
|
if (player->GetPlayerReadyForUpdates()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::server->Send(&stream, player->GetSystemAddress(), false);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
player->AddLimboConstruction(entity->GetObjectID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::server->Send(&stream, sysAddr, false);
|
|
|
|
}
|
|
|
|
|
2022-07-13 03:36:06 +00:00
|
|
|
// PacketUtils::SavePacket("[24]_"+std::to_string(entity->GetObjectID()) + "_" + std::to_string(m_SerializationCounter) + ".bin", (char*)stream.GetData(), stream.GetNumberOfBytesUsed());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (entity->IsPlayer()) {
|
|
|
|
if (entity->GetGMLevel() > GAME_MASTER_LEVEL_CIVILIAN) {
|
2021-12-05 17:54:36 +00:00
|
|
|
GameMessages::SendToggleGMInvis(entity->GetObjectID(), true, sysAddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::ConstructAllEntities(const SystemAddress& sysAddr) {
|
|
|
|
//ZoneControl is special:
|
|
|
|
ConstructEntity(m_ZoneControlEntity, sysAddr);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& e : m_Entities) {
|
2021-12-05 17:54:36 +00:00
|
|
|
if (e.second && (e.second->GetSpawnerID() != 0 || e.second->GetLOT() == 1) && !e.second->GetIsGhostingCandidate()) {
|
|
|
|
ConstructEntity(e.second, sysAddr);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
UpdateGhosting(Player::GetPlayer(sysAddr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::DestructEntity(Entity* entity, const SystemAddress& sysAddr) {
|
2022-07-28 13:39:57 +00:00
|
|
|
if (entity->GetNetworkId() == 0) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
RakNet::BitStream stream;
|
|
|
|
|
|
|
|
stream.Write(static_cast<char>(ID_REPLICA_MANAGER_DESTRUCTION));
|
|
|
|
stream.Write(static_cast<unsigned short>(entity->GetNetworkId()));
|
|
|
|
|
|
|
|
Game::server->Send(&stream, sysAddr, sysAddr == UNASSIGNED_SYSTEM_ADDRESS);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* player : Player::GetAllPlayers()) {
|
|
|
|
if (!player->GetPlayerReadyForUpdates()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
player->RemoveLimboConstruction(entity->GetObjectID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::SerializeEntity(Entity* entity) {
|
2022-07-28 13:39:57 +00:00
|
|
|
if (entity->GetNetworkId() == 0) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (std::find(m_EntitiesToSerialize.begin(), m_EntitiesToSerialize.end(), entity->GetObjectID()) == m_EntitiesToSerialize.end()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_EntitiesToSerialize.push_back(entity->GetObjectID());
|
|
|
|
}
|
|
|
|
|
|
|
|
//PacketUtils::SavePacket(std::to_string(m_SerializationCounter) + "_[27]_"+std::to_string(entity->GetObjectID()) + ".bin", (char*)stream.GetData(), stream.GetNumberOfBytesUsed());
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::DestructAllEntities(const SystemAddress& sysAddr) {
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& e : m_Entities) {
|
2021-12-05 17:54:36 +00:00
|
|
|
DestructEntity(e.second, sysAddr);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void EntityManager::SetGhostDistanceMax(float value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_GhostDistanceMaxSquared = value * value;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
float EntityManager::GetGhostDistanceMax() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return std::sqrt(m_GhostDistanceMaxSquared);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void EntityManager::SetGhostDistanceMin(float value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_GhostDistanceMinSqaured = value * value;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
float EntityManager::GetGhostDistanceMin() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return std::sqrt(m_GhostDistanceMinSqaured);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void EntityManager::QueueGhostUpdate(LWOOBJID playerID) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& iter = std::find(m_PlayersToUpdateGhosting.begin(), m_PlayersToUpdateGhosting.end(), playerID);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (iter == m_PlayersToUpdateGhosting.end()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_PlayersToUpdateGhosting.push_back(playerID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void EntityManager::UpdateGhosting() {
|
|
|
|
for (const auto playerID : m_PlayersToUpdateGhosting) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* player = Player::GetPlayer(playerID);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (player == nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateGhosting(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_PlayersToUpdateGhosting.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void EntityManager::UpdateGhosting(Player* player) {
|
|
|
|
if (player == nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* missionComponent = player->GetComponent<MissionComponent>();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (missionComponent == nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& referencePoint = player->GetGhostReferencePoint();
|
|
|
|
const auto isOverride = player->GetGhostOverride();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* entity : m_EntitiesToGhost) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto isAudioEmitter = entity->GetLOT() == 6368;
|
|
|
|
|
|
|
|
const auto& entityPoint = entity->GetPosition();
|
|
|
|
|
|
|
|
const int32_t id = entity->GetObjectID();
|
|
|
|
|
|
|
|
const auto observed = player->IsObserved(id);
|
|
|
|
|
|
|
|
const auto distance = NiPoint3::DistanceSquared(referencePoint, entityPoint);
|
|
|
|
|
|
|
|
auto ghostingDistanceMax = m_GhostDistanceMaxSquared;
|
|
|
|
auto ghostingDistanceMin = m_GhostDistanceMinSqaured;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (isAudioEmitter) {
|
2021-12-05 17:54:36 +00:00
|
|
|
ghostingDistanceMax = ghostingDistanceMin;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (observed && distance > ghostingDistanceMax && !isOverride) {
|
2021-12-05 17:54:36 +00:00
|
|
|
player->GhostEntity(id);
|
|
|
|
|
|
|
|
DestructEntity(entity, player->GetSystemAddress());
|
|
|
|
|
|
|
|
entity->SetObservers(entity->GetObservers() - 1);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else if (!observed && ghostingDistanceMin > distance) {
|
2021-12-05 17:54:36 +00:00
|
|
|
// Check collectables, don't construct if it has been collected
|
|
|
|
uint32_t collectionId = entity->GetCollectibleID();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (collectionId != 0) {
|
2021-12-05 17:54:36 +00:00
|
|
|
collectionId = static_cast<uint32_t>(collectionId) + static_cast<uint32_t>(Game::server->GetZoneID() << 8);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (missionComponent->HasCollectible(collectionId)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
player->ObserveEntity(id);
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
ConstructEntity(entity, player->GetSystemAddress());
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
entity->SetObservers(entity->GetObservers() + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void EntityManager::CheckGhosting(Entity* entity) {
|
|
|
|
if (entity == nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& referencePoint = entity->GetPosition();
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
auto ghostingDistanceMax = m_GhostDistanceMaxSquared;
|
|
|
|
auto ghostingDistanceMin = m_GhostDistanceMinSqaured;
|
|
|
|
|
|
|
|
const auto isAudioEmitter = entity->GetLOT() == 6368;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* player : Player::GetAllPlayers()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& entityPoint = player->GetGhostReferencePoint();
|
|
|
|
|
|
|
|
const int32_t id = entity->GetObjectID();
|
|
|
|
|
|
|
|
const auto observed = player->IsObserved(id);
|
|
|
|
|
|
|
|
const auto distance = NiPoint3::DistanceSquared(referencePoint, entityPoint);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (observed && distance > ghostingDistanceMax) {
|
2021-12-05 17:54:36 +00:00
|
|
|
player->GhostEntity(id);
|
|
|
|
|
|
|
|
DestructEntity(entity, player->GetSystemAddress());
|
|
|
|
|
|
|
|
entity->SetObservers(entity->GetObservers() - 1);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else if (!observed && ghostingDistanceMin > distance) {
|
2021-12-05 17:54:36 +00:00
|
|
|
player->ObserveEntity(id);
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
ConstructEntity(entity, player->GetSystemAddress());
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
entity->SetObservers(entity->GetObservers() + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Entity* EntityManager::GetGhostCandidate(int32_t id) {
|
|
|
|
for (auto* entity : m_EntitiesToGhost) {
|
|
|
|
if (entity->GetObjectID() == id) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool EntityManager::GetGhostingEnabled() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return m_GhostingEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::ResetFlags() {
|
|
|
|
for (const auto& e : m_Entities) {
|
|
|
|
e.second->ResetFlags();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityManager::ScheduleForKill(Entity* entity) {
|
|
|
|
// Deactivate switches if they die
|
|
|
|
if (!entity)
|
|
|
|
return;
|
2022-07-22 02:09:25 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
SwitchComponent* switchComp = entity->GetComponent<SwitchComponent>();
|
|
|
|
if (switchComp) {
|
|
|
|
entity->TriggerEvent("OnDectivated");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto objectId = entity->GetObjectID();
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (std::count(m_EntitiesToKill.begin(), m_EntitiesToKill.end(), objectId)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_EntitiesToKill.push_back(objectId);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void EntityManager::ScheduleForDeletion(LWOOBJID entity) {
|
|
|
|
if (std::count(m_EntitiesToDelete.begin(), m_EntitiesToDelete.end(), entity)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_EntitiesToDelete.push_back(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EntityManager::FireEventServerSide(Entity* origin, std::string args) {
|
|
|
|
for (std::pair<LWOOBJID, Entity*> e : m_Entities) {
|
|
|
|
if (e.second) {
|
|
|
|
e.second->OnFireEventServerSide(origin, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool EntityManager::IsExcludedFromGhosting(LOT lot) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return std::find(m_GhostingExcludedLOTs.begin(), m_GhostingExcludedLOTs.end(), lot) != m_GhostingExcludedLOTs.end();
|
|
|
|
}
|