Skip to content

Commit

Permalink
chore: Simplify and move Player functionality to relevant component (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
EmosewaMC authored Jan 12, 2024
1 parent 66cc582 commit 929d029
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 280 deletions.
4 changes: 2 additions & 2 deletions dGame/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ControllablePhysicsComponent>();

if (controllable != nullptr) {
Expand Down Expand Up @@ -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<ControllablePhysicsComponent>();

if (controllable != nullptr) {
Expand Down
14 changes: 7 additions & 7 deletions dGame/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Entity {

virtual User* GetParentUser() const;

virtual SystemAddress GetSystemAddress() const { return UNASSIGNED_SYSTEM_ADDRESS; };
virtual const SystemAddress& GetSystemAddress() const { return UNASSIGNED_SYSTEM_ADDRESS; };

/**
* Setters
Expand All @@ -124,13 +124,13 @@ class Entity {

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) {};

Expand Down Expand Up @@ -229,8 +229,8 @@ class Entity {
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();
Expand Down
239 changes: 63 additions & 176 deletions dGame/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,218 +3,120 @@
#include <ctime>

#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*> Player::m_Players = {};
namespace {
std::vector<Player*> 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;
const std::vector<Player*>& Player::GetAllPlayers() {
return m_Players;
}

SystemAddress Player::GetSystemAddress() const {
return m_SystemAddress;
void Player::SetGhostReferencePoint(const NiPoint3& value) {
m_GhostReferencePoint = value;
}

void Player::SetSystemAddress(const SystemAddress& value) {
m_SystemAddress = value;
void Player::SetGhostOverridePoint(const NiPoint3& value) {
m_GhostOverridePoint = value;
}

void Player::SetRespawnPos(const NiPoint3 position) {
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::SetRespawnRot(const NiQuaternion& rotation) {
m_respawnRot = rotation;
}

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::SetSystemAddress(const SystemAddress& value) {
m_SystemAddress = value;
}

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<CharacterComponent>();
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;

if (character != nullptr && characterComponent != nullptr) {
character->SetZoneID(zoneID);
character->SetZoneInstance(zoneInstance);
character->SetZoneClone(zoneClone);
m_GhostReferencePoint = NiPoint3::ZERO;
m_GhostOverridePoint = NiPoint3::ZERO;
m_GhostOverride = false;

characterComponent->SetLastRocketConfig(u"");
int32_t initialObservedEntitiesCapacity = 256;
m_ObservedEntities.resize(initialObservedEntitiesCapacity);

character->SaveXMLToDatabase();
}
m_Character->SetEntity(this);

WorldPackets::SendTransferToWorld(sysAddr, serverIP, serverPort, mythranShift);
const auto& iter = std::find(m_Players.begin(), m_Players.end(), this);

Game::entityManager->DestructEntity(entity);
return;
});
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()) {
return;
const auto iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId);
if (iter == m_LimboConstructions.end()) {
m_LimboConstructions.push_back(objectId);
}

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);
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) {
for (const auto& objectId : m_LimboConstructions) {
auto* entity = Game::entityManager->GetEntity(objectId);

if (entity == nullptr) {
continue;
}
if (!entity) continue;

Game::entityManager->ConstructEntity(entity, m_SystemAddress);
}

m_LimboConstructions.clear();
}

std::map<LWOOBJID, Loot::Info>& 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;
}

void Player::SetGhostReferencePoint(const NiPoint3& value) {
m_GhostReferencePoint = value;
}

void Player::SetGhostOverridePoint(const NiPoint3& value) {
m_GhostOverridePoint = value;
}

const NiPoint3& Player::GetGhostOverridePoint() const {
return m_GhostOverridePoint;
}

void Player::SetGhostOverride(bool value) {
m_GhostOverride = value;
}

bool Player::GetGhostOverride() const {
return m_GhostOverride;
}

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;
}
}
}
Expand All @@ -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<Player*>(character);
player = dynamic_cast<Player*>(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*>& 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;
Expand Down
Loading

0 comments on commit 929d029

Please sign in to comment.