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
This commit is contained in:
David Markowitz
2024-01-12 09:39:51 -08:00
committed by GitHub
parent 66cc582a9a
commit 929d029f12
15 changed files with 200 additions and 305 deletions

View File

@@ -20,6 +20,8 @@
#include "Database.h"
#include "CDRewardCodesTable.h"
#include "Mail.h"
#include "ZoneInstanceManager.h"
#include "WorldPackets.h"
#include <ctime>
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<CDRewardCodesTable>();
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<CharacterComponent>();
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);
});
}

View File

@@ -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.
*/

View File

@@ -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<Player*>(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&apos;t have room for it.", lot, size);
Mail::SendMail(LWOOBJID_EMPTY, "Darkflame Universe", m_Parent, "Lost Reward", "You received an item and didn&apos;t have room for it.", lot, size);
break;
case 1:

View File

@@ -20,6 +20,7 @@
#include "InventoryComponent.h"
#include "eMissionTaskType.h"
#include "eObjectBits.h"
#include "CharacterComponent.h"
#include <vector>
#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<CharacterComponent>();
if (characterComponent) characterComponent->SendToZone(zoneId);
}
auto inventoryComponent = ownerEntity->GetComponent<InventoryComponent>();

View File

@@ -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*>(player);
if (playerInstance) {
playerInstance->SendToZone(m_MainWorld);
}
auto* characterComponent = player->GetComponent<CharacterComponent>();
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*>(player);
if (playerInstance) {
auto* characterComponent = player->GetComponent<CharacterComponent>();
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*>(player);
auto* characterComponent = player->GetComponent<CharacterComponent>();
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<Player*>(playerEntity);
auto* characterComponent = playerEntity->GetComponent<CharacterComponent>();
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<Player*>(playerEntity);
auto* characterComponent = playerEntity->GetComponent<CharacterComponent>();
playerInstance->SendToZone(m_MainWorld);
if (characterComponent) characterComponent->SendToZone(m_MainWorld);
}
return;