From 929d029f1241817e6ca0d87a076c9e21769480ab Mon Sep 17 00:00:00 2001 From: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> Date: Fri, 12 Jan 2024 09:39:51 -0800 Subject: [PATCH] chore: Simplify and move Player functionality to relevant component (#1408) * Moving and organizing Player code - Move code to CharacterComponent - Remove extraneous interfaces - Simplify some code greatly - Change some types to return and take in const ref (only structs larger than 8 bytes benefit from this change.) - Update code to use CharacterComponent for sending to zone instead of Player*. * Moving and organizing Player code - Move code to CharacterComponent - Remove extraneous interfaces - Simplify some code greatly - Change some types to return and take in const ref (only structs larger than 8 bytes benefit from this change.) - Update code to use CharacterComponent for sending to zone instead of Player*. - Remove static storage container (static containers can be destroyed before exit/terminate handler executes) * remove player cast * Remove extra includes --- dGame/Entity.cpp | 4 +- dGame/Entity.h | 14 +- dGame/Player.cpp | 289 ++++++------------ dGame/Player.h | 64 ++-- dGame/dComponents/CharacterComponent.cpp | 35 ++- dGame/dComponents/CharacterComponent.h | 8 + dGame/dComponents/InventoryComponent.cpp | 9 +- .../PropertyManagementComponent.cpp | 4 +- dGame/dComponents/RacingControlComponent.cpp | 25 +- dScripts/BaseConsoleTeleportServer.cpp | 6 +- dScripts/BaseSurvivalServer.cpp | 9 +- dScripts/BaseWavesServer.cpp | 6 +- ...nceExitTransferPlayerToLastNonInstance.cpp | 11 +- .../ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp | 14 +- dScripts/zone/LUPs/WblGenericZone.cpp | 7 +- 15 files changed, 200 insertions(+), 305 deletions(-) diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index c693358d..1a99136e 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -1879,7 +1879,7 @@ const NiQuaternion& Entity::GetRotation() const { return NiQuaternion::IDENTITY; } -void Entity::SetPosition(NiPoint3 position) { +void Entity::SetPosition(const NiPoint3& position) { auto* controllable = GetComponent(); if (controllable != nullptr) { @@ -1907,7 +1907,7 @@ void Entity::SetPosition(NiPoint3 position) { Game::entityManager->SerializeEntity(this); } -void Entity::SetRotation(NiQuaternion rotation) { +void Entity::SetRotation(const NiQuaternion& rotation) { auto* controllable = GetComponent(); if (controllable != nullptr) { diff --git a/dGame/Entity.h b/dGame/Entity.h index 02fc14d3..36621d5c 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -106,7 +106,7 @@ public: virtual User* GetParentUser() const; - virtual SystemAddress GetSystemAddress() const { return UNASSIGNED_SYSTEM_ADDRESS; }; + virtual const SystemAddress& GetSystemAddress() const { return UNASSIGNED_SYSTEM_ADDRESS; }; /** * Setters @@ -124,13 +124,13 @@ public: void SetNetworkId(uint16_t id); - void SetPosition(NiPoint3 position); + void SetPosition(const NiPoint3& position); - void SetRotation(NiQuaternion rotation); + void SetRotation(const NiQuaternion& rotation); - virtual void SetRespawnPos(NiPoint3 position) {} + virtual void SetRespawnPos(const NiPoint3& position) {} - virtual void SetRespawnRot(NiQuaternion rotation) {} + virtual void SetRespawnRot(const NiQuaternion& rotation) {} virtual void SetSystemAddress(const SystemAddress& value) {}; @@ -229,8 +229,8 @@ public: void TriggerEvent(eTriggerEventType event, Entity* optionalTarget = nullptr); void ScheduleDestructionAfterUpdate() { m_ShouldDestroyAfterUpdate = true; } - virtual NiPoint3 GetRespawnPosition() const { return NiPoint3::ZERO; } - virtual NiQuaternion GetRespawnRotation() const { return NiQuaternion::IDENTITY; } + virtual const NiPoint3& GetRespawnPosition() const { return NiPoint3::ZERO; } + virtual const NiQuaternion& GetRespawnRotation() const { return NiQuaternion::IDENTITY; } void Sleep(); void Wake(); diff --git a/dGame/Player.cpp b/dGame/Player.cpp index abbbf059..d1009086 100644 --- a/dGame/Player.cpp +++ b/dGame/Player.cpp @@ -3,162 +3,22 @@ #include #include "Character.h" -#include "Database.h" -#include "MissionComponent.h" #include "UserManager.h" #include "EntityManager.h" +#include "Game.h" #include "Logger.h" -#include "ZoneInstanceManager.h" -#include "WorldPackets.h" #include "dZoneManager.h" -#include "CharacterComponent.h" -#include "Mail.h" #include "User.h" #include "CppScripts.h" #include "Loot.h" #include "eReplicaComponentType.h" -std::vector Player::m_Players = {}; +namespace { + std::vector m_Players; +}; -Player::Player(const LWOOBJID& objectID, const EntityInfo info, User* user, Entity* parentEntity) : Entity(objectID, info, parentEntity) { - m_ParentUser = user; - m_Character = m_ParentUser->GetLastUsedChar(); - m_ParentUser->SetLoggedInChar(objectID); - m_GMLevel = m_Character->GetGMLevel(); - m_SystemAddress = m_ParentUser->GetSystemAddress(); - m_DroppedLoot = {}; - m_DroppedCoins = 0; - - m_GhostReferencePoint = NiPoint3::ZERO; - m_GhostOverridePoint = NiPoint3::ZERO; - m_GhostOverride = false; - m_ObservedEntitiesLength = 256; - m_ObservedEntitiesUsed = 0; - m_ObservedEntities.resize(m_ObservedEntitiesLength); - - m_Character->SetEntity(this); - - const auto& iter = std::find(m_Players.begin(), m_Players.end(), this); - - if (iter != m_Players.end()) { - return; - } - - m_Players.push_back(this); -} - -User* Player::GetParentUser() const { - return m_ParentUser; -} - -SystemAddress Player::GetSystemAddress() const { - return m_SystemAddress; -} - -void Player::SetSystemAddress(const SystemAddress& value) { - m_SystemAddress = value; -} - -void Player::SetRespawnPos(const NiPoint3 position) { - if (!m_Character) return; - - m_respawnPos = position; - - m_Character->SetRespawnPoint(Game::zoneManager->GetZone()->GetWorldID(), position); -} - -void Player::SetRespawnRot(const NiQuaternion rotation) { - m_respawnRot = rotation; -} - -NiPoint3 Player::GetRespawnPosition() const { - return m_respawnPos; -} - -NiQuaternion Player::GetRespawnRotation() const { - return m_respawnRot; -} - -void Player::SendMail(const LWOOBJID sender, const std::string& senderName, const std::string& subject, const std::string& body, LOT attachment, uint16_t attachmentCount) const { - Mail::SendMail(sender, senderName, this, subject, body, attachment, attachmentCount); -} - -void Player::SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId) { - const auto objid = GetObjectID(); - - ZoneInstanceManager::Instance()->RequestZoneTransfer(Game::server, zoneId, cloneId, false, [objid](bool mythranShift, uint32_t zoneID, uint32_t zoneInstance, uint32_t zoneClone, std::string serverIP, uint16_t serverPort) { - auto* entity = Game::entityManager->GetEntity(objid); - - if (entity == nullptr) { - return; - } - - const auto sysAddr = entity->GetSystemAddress(); - - auto* character = entity->GetCharacter(); - auto* characterComponent = entity->GetComponent(); - - if (character != nullptr && characterComponent != nullptr) { - character->SetZoneID(zoneID); - character->SetZoneInstance(zoneInstance); - character->SetZoneClone(zoneClone); - - characterComponent->SetLastRocketConfig(u""); - - character->SaveXMLToDatabase(); - } - - WorldPackets::SendTransferToWorld(sysAddr, serverIP, serverPort, mythranShift); - - Game::entityManager->DestructEntity(entity); - return; - }); -} - -void Player::AddLimboConstruction(LWOOBJID objectId) { - const auto& iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId); - - if (iter != m_LimboConstructions.end()) { - return; - } - - m_LimboConstructions.push_back(objectId); -} - -void Player::RemoveLimboConstruction(LWOOBJID objectId) { - const auto& iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId); - - if (iter == m_LimboConstructions.end()) { - return; - } - - m_LimboConstructions.erase(iter); -} - -void Player::ConstructLimboEntities() { - for (const auto objectId : m_LimboConstructions) { - auto* entity = Game::entityManager->GetEntity(objectId); - - if (entity == nullptr) { - continue; - } - - Game::entityManager->ConstructEntity(entity, m_SystemAddress); - } - - m_LimboConstructions.clear(); -} - -std::map& Player::GetDroppedLoot() { - return m_DroppedLoot; -} - -const NiPoint3& Player::GetGhostReferencePoint() const { - return m_GhostOverride ? m_GhostOverridePoint : m_GhostReferencePoint; -} - -const NiPoint3& Player::GetOriginGhostReferencePoint() const { - return m_GhostReferencePoint; +const std::vector& Player::GetAllPlayers() { + return m_Players; } void Player::SetGhostReferencePoint(const NiPoint3& value) { @@ -169,52 +29,94 @@ void Player::SetGhostOverridePoint(const NiPoint3& value) { m_GhostOverridePoint = value; } -const NiPoint3& Player::GetGhostOverridePoint() const { - return m_GhostOverridePoint; +void Player::SetRespawnPos(const NiPoint3& position) { + if (!m_Character) return; + + m_respawnPos = position; + + m_Character->SetRespawnPoint(Game::zoneManager->GetZone()->GetWorldID(), position); + } -void Player::SetGhostOverride(bool value) { - m_GhostOverride = value; +void Player::SetRespawnRot(const NiQuaternion& rotation) { + m_respawnRot = rotation; } -bool Player::GetGhostOverride() const { - return m_GhostOverride; +void Player::SetSystemAddress(const SystemAddress& value) { + m_SystemAddress = value; +} + +Player::Player(const LWOOBJID& objectID, const EntityInfo info, User* user, Entity* parentEntity) : Entity(objectID, info, parentEntity) { + m_ParentUser = user; + m_Character = m_ParentUser->GetLastUsedChar(); + m_ParentUser->SetLoggedInChar(objectID); + m_GMLevel = m_Character->GetGMLevel(); + m_SystemAddress = m_ParentUser->GetSystemAddress(); + m_DroppedCoins = 0; + + m_GhostReferencePoint = NiPoint3::ZERO; + m_GhostOverridePoint = NiPoint3::ZERO; + m_GhostOverride = false; + + int32_t initialObservedEntitiesCapacity = 256; + m_ObservedEntities.resize(initialObservedEntitiesCapacity); + + m_Character->SetEntity(this); + + const auto& iter = std::find(m_Players.begin(), m_Players.end(), this); + + if (iter == m_Players.end()) { + m_Players.push_back(this); + } +} + +void Player::AddLimboConstruction(LWOOBJID objectId) { + const auto iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId); + if (iter == m_LimboConstructions.end()) { + m_LimboConstructions.push_back(objectId); + } +} + +void Player::RemoveLimboConstruction(LWOOBJID objectId) { + const auto iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId); + if (iter != m_LimboConstructions.end()) { + m_LimboConstructions.erase(iter); + } +} + +void Player::ConstructLimboEntities() { + for (const auto& objectId : m_LimboConstructions) { + auto* entity = Game::entityManager->GetEntity(objectId); + if (!entity) continue; + + Game::entityManager->ConstructEntity(entity, m_SystemAddress); + } + + m_LimboConstructions.clear(); } void Player::ObserveEntity(int32_t id) { - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) { - if (m_ObservedEntities[i] == 0 || m_ObservedEntities[i] == id) { - m_ObservedEntities[i] = id; + for (auto& observedEntity : m_ObservedEntities) { + if (observedEntity == 0 || observedEntity == id) { + observedEntity = id; return; } } - const auto index = m_ObservedEntitiesUsed++; + m_ObservedEntities.reserve(m_ObservedEntities.size() + 1); - if (m_ObservedEntitiesUsed > m_ObservedEntitiesLength) { - m_ObservedEntities.resize(m_ObservedEntitiesLength + m_ObservedEntitiesLength); - - m_ObservedEntitiesLength = m_ObservedEntitiesLength + m_ObservedEntitiesLength; - } - - m_ObservedEntities[index] = id; + m_ObservedEntities.push_back(id); } bool Player::IsObserved(int32_t id) { - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) { - if (m_ObservedEntities[i] == id) { - return true; - } - } - - return false; + return std::find(m_ObservedEntities.begin(), m_ObservedEntities.end(), id) != m_ObservedEntities.end(); } void Player::GhostEntity(int32_t id) { - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) { - if (m_ObservedEntities[i] == id) { - m_ObservedEntities[i] = 0; + for (auto& observedEntity : m_ObservedEntities) { + if (observedEntity == id) { + observedEntity = 0; } } } @@ -228,59 +130,44 @@ Player* Player::GetPlayer(const SystemAddress& sysAddr) { Player* Player::GetPlayer(const std::string& name) { const auto characters = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::CHARACTER); + Player* player = nullptr; for (auto* character : characters) { if (!character->IsPlayer()) continue; if (GeneralUtils::CaseInsensitiveStringCompare(name, character->GetCharacter()->GetName())) { - return dynamic_cast(character); + player = dynamic_cast(character); } } - return nullptr; + return player; } Player* Player::GetPlayer(LWOOBJID playerID) { + Player* playerToReturn = nullptr; for (auto* player : m_Players) { if (player->GetObjectID() == playerID) { - return player; + playerToReturn = player; } } - return nullptr; -} - -const std::vector& Player::GetAllPlayers() { - return m_Players; -} - -uint64_t Player::GetDroppedCoins() { - return m_DroppedCoins; -} - -void Player::SetDroppedCoins(uint64_t value) { - m_DroppedCoins = value; + return playerToReturn; } Player::~Player() { LOG("Deleted player"); - for (int32_t i = 0; i < m_ObservedEntitiesUsed; i++) { - const auto id = m_ObservedEntities[i]; + for (auto& observedEntity : m_ObservedEntities) { + if (observedEntity == 0) continue; - if (id == 0) { - continue; - } - - auto* entity = Game::entityManager->GetGhostCandidate(id); - - if (entity != nullptr) { - entity->SetObservers(entity->GetObservers() - 1); - } + auto* entity = Game::entityManager->GetGhostCandidate(observedEntity); + if (!entity) continue; + + entity->SetObservers(entity->GetObservers() - 1); } m_LimboConstructions.clear(); - const auto& iter = std::find(m_Players.begin(), m_Players.end(), this); + const auto iter = std::find(m_Players.begin(), m_Players.end(), this); if (iter == m_Players.end()) { return; diff --git a/dGame/Player.h b/dGame/Player.h index 287ee613..1b3e6299 100644 --- a/dGame/Player.h +++ b/dGame/Player.h @@ -18,64 +18,44 @@ public: * Getters */ - User* GetParentUser() const override; + User* GetParentUser() const override { return m_ParentUser; }; - SystemAddress GetSystemAddress() const override; + const SystemAddress& GetSystemAddress() const override { return m_SystemAddress; }; - NiPoint3 GetRespawnPosition() const override; + const NiPoint3& GetRespawnPosition() const override { return m_respawnPos; }; - NiQuaternion GetRespawnRotation() const override; + const NiQuaternion& GetRespawnRotation() const override { return m_respawnRot; }; - const NiPoint3& GetGhostReferencePoint() const; + const NiPoint3& GetGhostReferencePoint() const { return m_GhostOverride ? m_GhostOverridePoint : m_GhostReferencePoint; }; - const NiPoint3& GetOriginGhostReferencePoint() const; + const NiPoint3& GetOriginGhostReferencePoint() const { return m_GhostReferencePoint; }; - const NiPoint3& GetGhostOverridePoint() const; + const NiPoint3& GetGhostOverridePoint() const { return m_GhostOverridePoint; }; - bool GetGhostOverride() const; + bool GetGhostOverride() const { return m_GhostOverride; }; - std::map& GetDroppedLoot(); + std::map& GetDroppedLoot() { return m_DroppedLoot; }; - uint64_t GetDroppedCoins(); + uint64_t GetDroppedCoins() const { return m_DroppedCoins; }; /** * Setters */ + void SetGhostOverride(bool value) { m_GhostOverride = value; }; + + void SetDroppedCoins(const uint64_t value) { m_DroppedCoins = value; }; + void SetSystemAddress(const SystemAddress& value) override; - void SetRespawnPos(NiPoint3 position) override; + void SetRespawnPos(const NiPoint3& position) override; - void SetRespawnRot(NiQuaternion rotation) override; + void SetRespawnRot(const NiQuaternion& rotation) override; void SetGhostReferencePoint(const NiPoint3& value); void SetGhostOverridePoint(const NiPoint3& value); - void SetGhostOverride(bool value); - - void SetDroppedCoins(uint64_t value); - - /** - * Wrapper for sending an in-game mail. - * - * @param sender id of the sender. LWOOBJID_EMPTY for system mail - * @param senderName name of the sender. Max 32 characters. - * @param subject mail subject. Max 50 characters. - * @param body mail body. Max 400 characters. - * @param attachment LOT of the attached item. LOT_NULL if no attachment. - * @param attachmentCount stack size for attachment. - */ - void SendMail(LWOOBJID sender, const std::string& senderName, const std::string& subject, const std::string& body, LOT attachment, uint16_t attachmentCount) const; - - /** - * Wrapper for transfering the player to another instance. - * - * @param zoneId zoneID for the new instance. - * @param cloneId cloneID for the new instance. - */ - void SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId = 0); - /** * Ghosting */ @@ -86,11 +66,11 @@ public: void ConstructLimboEntities(); - void ObserveEntity(int32_t id); + void ObserveEntity(const int32_t id); - bool IsObserved(int32_t id); + bool IsObserved(const int32_t id); - void GhostEntity(int32_t id); + void GhostEntity(const int32_t id); /** * Static methods @@ -122,15 +102,9 @@ private: std::vector m_ObservedEntities; - int32_t m_ObservedEntitiesLength; - - int32_t m_ObservedEntitiesUsed; - std::vector m_LimboConstructions; std::map m_DroppedLoot; uint64_t m_DroppedCoins; - - static std::vector m_Players; }; diff --git a/dGame/dComponents/CharacterComponent.cpp b/dGame/dComponents/CharacterComponent.cpp index 702ab70f..70eec0cf 100644 --- a/dGame/dComponents/CharacterComponent.cpp +++ b/dGame/dComponents/CharacterComponent.cpp @@ -20,6 +20,8 @@ #include "Database.h" #include "CDRewardCodesTable.h" #include "Mail.h" +#include "ZoneInstanceManager.h" +#include "WorldPackets.h" #include CharacterComponent::CharacterComponent(Entity* parent, Character* character) : Component(parent) { @@ -763,12 +765,12 @@ void CharacterComponent::AwardClaimCodes() { if (!m_Parent) return; auto* user = m_Parent->GetParentUser(); if (!user) return; - + auto rewardCodes = Database::Get()->GetRewardCodesByAccountID(user->GetAccountID()); if (rewardCodes.empty()) return; auto* cdrewardCodes = CDClientManager::Instance().GetTable(); - for (auto const rewardCode: rewardCodes){ + for (auto const rewardCode : rewardCodes) { LOG_DEBUG("Processing RewardCode %i", rewardCode); const uint32_t rewardCodeIndex = rewardCode >> 6; const uint32_t bitIndex = rewardCode % 64; @@ -786,3 +788,32 @@ void CharacterComponent::AwardClaimCodes() { Mail::SendMail(LWOOBJID_EMPTY, "%[MAIL_SYSTEM_NOTIFICATION]", m_Parent, subject.str(), body.str(), attachmentLOT, 1); } } + +void CharacterComponent::SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId) const { + const auto objid = m_Parent->GetObjectID(); + + ZoneInstanceManager::Instance()->RequestZoneTransfer(Game::server, zoneId, cloneId, false, [objid](bool mythranShift, uint32_t zoneID, uint32_t zoneInstance, uint32_t zoneClone, std::string serverIP, uint16_t serverPort) { + auto* entity = Game::entityManager->GetEntity(objid); + + if (!entity) return; + + const auto sysAddr = entity->GetSystemAddress(); + + auto* character = entity->GetCharacter(); + auto* characterComponent = entity->GetComponent(); + + if (character && characterComponent) { + character->SetZoneID(zoneID); + character->SetZoneInstance(zoneInstance); + character->SetZoneClone(zoneClone); + + characterComponent->SetLastRocketConfig(u""); + + character->SaveXMLToDatabase(); + } + + WorldPackets::SendTransferToWorld(sysAddr, serverIP, serverPort, mythranShift); + + Game::entityManager->DestructEntity(entity); + }); +} diff --git a/dGame/dComponents/CharacterComponent.h b/dGame/dComponents/CharacterComponent.h index 158e011e..fae99dcb 100644 --- a/dGame/dComponents/CharacterComponent.h +++ b/dGame/dComponents/CharacterComponent.h @@ -281,6 +281,14 @@ public: LWOOBJID GetCurrentInteracting() {return m_CurrentInteracting;}; + /** + * Sends a player to another zone with an optional clone ID + * + * @param zoneId zoneID for the new instance. + * @param cloneId cloneID for the new instance. + */ + void SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId = 0) const; + /** * Character info regarding this character, including clothing styles, etc. */ diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index 92b5171a..23d5c163 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -31,6 +31,7 @@ #include "eMissionTaskType.h" #include "eStateChangeType.h" #include "eUseItemResponse.h" +#include "Mail.h" #include "CDComponentsRegistryTable.h" #include "CDInventoryComponentTable.h" @@ -264,17 +265,11 @@ void InventoryComponent::AddItem( } if (slot == -1) { - auto* player = dynamic_cast(GetParent()); - - if (player == nullptr) { - return; - } - outOfSpace += size; switch (sourceType) { case 0: - player->SendMail(LWOOBJID_EMPTY, "Darkflame Universe", "Lost Reward", "You received an item and didn't have room for it.", lot, size); + Mail::SendMail(LWOOBJID_EMPTY, "Darkflame Universe", m_Parent, "Lost Reward", "You received an item and didn't have room for it.", lot, size); break; case 1: diff --git a/dGame/dComponents/PropertyManagementComponent.cpp b/dGame/dComponents/PropertyManagementComponent.cpp index 75466043..f0115676 100644 --- a/dGame/dComponents/PropertyManagementComponent.cpp +++ b/dGame/dComponents/PropertyManagementComponent.cpp @@ -20,6 +20,7 @@ #include "InventoryComponent.h" #include "eMissionTaskType.h" #include "eObjectBits.h" +#include "CharacterComponent.h" #include #include "CppScripts.h" @@ -247,7 +248,8 @@ void PropertyManagementComponent::OnStartBuilding() { for (auto* player : players) { if (player == ownerEntity) continue; - player->SendToZone(zoneId); + auto* characterComponent = player->GetComponent(); + if (characterComponent) characterComponent->SendToZone(zoneId); } auto inventoryComponent = ownerEntity->GetComponent(); diff --git a/dGame/dComponents/RacingControlComponent.cpp b/dGame/dComponents/RacingControlComponent.cpp index 5b93dbb7..5e3d81f4 100644 --- a/dGame/dComponents/RacingControlComponent.cpp +++ b/dGame/dComponents/RacingControlComponent.cpp @@ -71,10 +71,8 @@ void RacingControlComponent::OnPlayerLoaded(Entity* player) { // If the race has already started, send the player back to the main world. if (m_Loaded || !vehicle) { - auto* playerInstance = dynamic_cast(player); - if (playerInstance) { - playerInstance->SendToZone(m_MainWorld); - } + auto* characterComponent = player->GetComponent(); + if (characterComponent) characterComponent->SendToZone(m_MainWorld); return; } @@ -105,10 +103,11 @@ void RacingControlComponent::LoadPlayerVehicle(Entity* player, if (item == nullptr) { LOG("Failed to find item"); - auto* playerInstance = dynamic_cast(player); - if (playerInstance) { + auto* characterComponent = player->GetComponent(); + + if (characterComponent) { m_LoadedPlayers--; - playerInstance->SendToZone(m_MainWorld); + characterComponent->SendToZone(m_MainWorld); } return; @@ -427,9 +426,9 @@ void RacingControlComponent::HandleMessageBoxResponse(Entity* player, int32_t bu m_Parent->GetObjectID(), 3, 0, LWOOBJID_EMPTY, u"", player->GetObjectID(), UNASSIGNED_SYSTEM_ADDRESS); - auto* playerInstance = dynamic_cast(player); + auto* characterComponent = player->GetComponent(); - playerInstance->SendToZone(m_MainWorld); + if (characterComponent) characterComponent->SendToZone(m_MainWorld); vehicle->Kill(); } @@ -561,9 +560,9 @@ void RacingControlComponent::Update(float deltaTime) { continue; } - auto* playerInstance = dynamic_cast(playerEntity); + auto* characterComponent = playerEntity->GetComponent(); - playerInstance->SendToZone(m_MainWorld); + if (characterComponent) characterComponent->SendToZone(m_MainWorld); } m_LobbyPlayers.clear(); @@ -623,9 +622,9 @@ void RacingControlComponent::Update(float deltaTime) { continue; } - auto* playerInstance = dynamic_cast(playerEntity); + auto* characterComponent = playerEntity->GetComponent(); - playerInstance->SendToZone(m_MainWorld); + if (characterComponent) characterComponent->SendToZone(m_MainWorld); } return; diff --git a/dScripts/BaseConsoleTeleportServer.cpp b/dScripts/BaseConsoleTeleportServer.cpp index 27d2bcfc..d4a49299 100644 --- a/dScripts/BaseConsoleTeleportServer.cpp +++ b/dScripts/BaseConsoleTeleportServer.cpp @@ -1,6 +1,6 @@ #include "BaseConsoleTeleportServer.h" #include "GameMessages.h" -#include "Player.h" +#include "CharacterComponent.h" #include "RenderComponent.h" #include "EntityManager.h" #include "eTerminateType.h" @@ -94,7 +94,9 @@ void BaseConsoleTeleportServer::TransferPlayer(Entity* self, Entity* player, int const auto& teleportZone = self->GetVar(u"transferZoneID"); - static_cast(player)->SendToZone(std::stoi(GeneralUtils::UTF16ToWTF8(teleportZone))); + auto* characterComponent = player->GetComponent(); + + if (characterComponent) characterComponent->SendToZone(std::stoi(GeneralUtils::UTF16ToWTF8(teleportZone))); UpdatePlayerTable(self, player, false); } diff --git a/dScripts/BaseSurvivalServer.cpp b/dScripts/BaseSurvivalServer.cpp index 93476d78..ecdcad64 100644 --- a/dScripts/BaseSurvivalServer.cpp +++ b/dScripts/BaseSurvivalServer.cpp @@ -3,7 +3,7 @@ #include "DestroyableComponent.h" #include "EntityManager.h" #include "dZoneManager.h" -#include "Player.h" +#include "CharacterComponent.h" #include "eMissionTaskType.h" #include "eMissionState.h" #include "MissionComponent.h" @@ -23,7 +23,8 @@ void BaseSurvivalServer::BasePlayerLoaded(Entity* self, Entity* player) { const auto& playersIter = std::find(state.players.begin(), state.players.end(), player->GetObjectID()); if (waitingIter != state.waitingPlayers.end() || playersIter != state.players.end()) { - static_cast(player)->SendToZone(player->GetCharacter()->GetLastNonInstanceZoneID()); + auto* characterComponent = player->GetComponent(); + if (characterComponent) characterComponent->SendToZone(player->GetCharacter()->GetLastNonInstanceZoneID()); return; } @@ -161,8 +162,8 @@ void BaseSurvivalServer::BaseMessageBoxResponse(Entity* self, Entity* sender, in if (sender->IsPlayer()) { auto* character = sender->GetCharacter(); if (character != nullptr) { - auto* player = dynamic_cast(sender); - player->SendToZone(character->GetLastNonInstanceZoneID()); + auto* characterComponent = sender->GetComponent(); + if (characterComponent) characterComponent->SendToZone(character->GetLastNonInstanceZoneID()); } } } diff --git a/dScripts/BaseWavesServer.cpp b/dScripts/BaseWavesServer.cpp index 9be19806..e2f646f9 100644 --- a/dScripts/BaseWavesServer.cpp +++ b/dScripts/BaseWavesServer.cpp @@ -3,7 +3,7 @@ #include "DestroyableComponent.h" #include "EntityManager.h" #include "dZoneManager.h" -#include "Player.h" +#include "CharacterComponent.h" #include "eMissionTaskType.h" #include "eMissionState.h" #include "MissionComponent.h" @@ -162,8 +162,8 @@ void BaseWavesServer::BaseMessageBoxResponse(Entity* self, Entity* sender, int32 if (sender->IsPlayer()) { auto* character = sender->GetCharacter(); if (character != nullptr) { - auto* player = dynamic_cast(sender); - player->SendToZone(character->GetLastNonInstanceZoneID()); + auto* characterComponent = sender->GetComponent(); + if (characterComponent) characterComponent->SendToZone(character->GetLastNonInstanceZoneID()); } } } diff --git a/dScripts/ai/GENERAL/InstanceExitTransferPlayerToLastNonInstance.cpp b/dScripts/ai/GENERAL/InstanceExitTransferPlayerToLastNonInstance.cpp index de1c62e0..667f41bd 100644 --- a/dScripts/ai/GENERAL/InstanceExitTransferPlayerToLastNonInstance.cpp +++ b/dScripts/ai/GENERAL/InstanceExitTransferPlayerToLastNonInstance.cpp @@ -1,6 +1,6 @@ #include "InstanceExitTransferPlayerToLastNonInstance.h" #include "GameMessages.h" -#include "Player.h" +#include "CharacterComponent.h" #include "Character.h" #include "dServer.h" #include "eTerminateType.h" @@ -23,10 +23,8 @@ void InstanceExitTransferPlayerToLastNonInstance::OnUse(Entity* self, Entity* us } void InstanceExitTransferPlayerToLastNonInstance::OnMessageBoxResponse(Entity* self, Entity* sender, int32_t button, const std::u16string& identifier, const std::u16string& userData) { - auto* player = dynamic_cast(sender); - if (player == nullptr) - return; - + if (!sender->IsPlayer()) return; + auto* character = sender->GetCharacter(); if (character != nullptr) { if (identifier == u"Instance_Exit" && button == 1) { @@ -47,7 +45,8 @@ void InstanceExitTransferPlayerToLastNonInstance::OnMessageBoxResponse(Entity* s } } - player->SendToZone(lastInstance); + auto* characterComponent = sender->GetComponent(); + if (characterComponent) characterComponent->SendToZone(lastInstance); } } diff --git a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp index 1a298843..c8563b53 100644 --- a/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp +++ b/dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp @@ -453,16 +453,12 @@ void SGCannon::SpawnNewModel(Entity* self) { void SGCannon::RemovePlayer(LWOOBJID playerID) { auto* player = Game::entityManager->GetEntity(playerID); - if (player == nullptr) - return; + if (!player) return; - auto* playerObject = dynamic_cast(player); - if (playerObject == nullptr) - return; - - auto* character = playerObject->GetCharacter(); - if (character != nullptr) { - playerObject->SendToZone(character->GetLastNonInstanceZoneID()); + auto* character = player->GetCharacter(); + auto* characterComponent = player->GetComponent(); + if (characterComponent && character) { + characterComponent->SendToZone(character->GetLastNonInstanceZoneID()); } } diff --git a/dScripts/zone/LUPs/WblGenericZone.cpp b/dScripts/zone/LUPs/WblGenericZone.cpp index 5a670d8e..600628fa 100644 --- a/dScripts/zone/LUPs/WblGenericZone.cpp +++ b/dScripts/zone/LUPs/WblGenericZone.cpp @@ -1,10 +1,11 @@ #include "WblGenericZone.h" -#include "Player.h" +#include "CharacterComponent.h" void WblGenericZone::OnFireEventServerSide(Entity* self, Entity* sender, std::string args, int32_t param1, int32_t param2, int32_t param3) { if (args == m_WblAbortMsg) { if (!sender) return; - auto player = dynamic_cast(sender); - if (player) player->SendToZone(m_WblMainZone); + + auto* characterComponent = sender->GetComponent(); + if (characterComponent) characterComponent->SendToZone(m_WblMainZone); } }