Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Player class removal #1445

Merged
merged 8 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dGame/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ set(DGAME_SOURCES "Character.cpp"
"Entity.cpp"
"EntityManager.cpp"
"LeaderboardManager.cpp"
"Player.cpp"
"PlayerManager.cpp"
"TeamManager.cpp"
"TradingManager.cpp"
Expand Down
2 changes: 2 additions & 0 deletions dGame/Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ class Character {

void SetBillboardVisible(bool visible);

User* GetParentUser() const { return m_ParentUser; }

private:
void UpdateInfoFromDatabase();
/**
Expand Down
118 changes: 87 additions & 31 deletions dGame/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "Spawner.h"
#include "UserManager.h"
#include "dpWorld.h"
#include "Player.h"
#include "LUTriggers.h"
#include "User.h"
#include "EntityTimer.h"
Expand All @@ -26,6 +25,7 @@
#include "eObjectBits.h"
#include "PositionUpdate.h"
#include "eChatMessageType.h"
#include "PlayerManager.h"

//Component includes:
#include "Component.h"
Expand Down Expand Up @@ -95,7 +95,7 @@
#include "CDSkillBehaviorTable.h"
#include "CDZoneTableTable.h"

Entity::Entity(const LWOOBJID& objectID, EntityInfo info, Entity* parentEntity) {
Entity::Entity(const LWOOBJID& objectID, EntityInfo info, User* parentUser, Entity* parentEntity) {
m_ObjectID = objectID;
m_TemplateID = info.lot;
m_ParentEntity = parentEntity;
Expand Down Expand Up @@ -124,9 +124,42 @@ Entity::Entity(const LWOOBJID& objectID, EntityInfo info, Entity* parentEntity)
m_SpawnerNodeID = info.spawnerNodeID;

if (info.lot != 1) m_PlayerIsReadyForUpdates = true;
if (parentUser) {
m_Character = parentUser->GetLastUsedChar();
parentUser->SetLoggedInChar(objectID);
m_GMLevel = m_Character->GetGMLevel();

m_Character->SetEntity(this);

PlayerManager::AddPlayer(this);
}
}

Entity::~Entity() {
if (IsPlayer()) {
LOG("Deleted player");

// Make sure the player exists first. Remove afterwards to prevent the OnPlayerExist functions from not being able to find the player.
if (!PlayerManager::RemovePlayer(this)) {
LOG("Unable to find player to remove from manager.");
return;
}

Entity* zoneControl = Game::entityManager->GetZoneControlEntity();
for (CppScripts::Script* script : CppScripts::GetEntityScripts(zoneControl)) {
script->OnPlayerExit(zoneControl, this);
}

std::vector<Entity*> scriptedActs = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::SCRIPTED_ACTIVITY);
for (Entity* scriptEntity : scriptedActs) {
if (scriptEntity->GetObjectID() != zoneControl->GetObjectID()) { // Don't want to trigger twice on instance worlds
for (CppScripts::Script* script : CppScripts::GetEntityScripts(scriptEntity)) {
script->OnPlayerExit(scriptEntity, this);
}
}
}
}

if (m_Character) {
m_Character->SaveXMLToDatabase();
}
Expand Down Expand Up @@ -212,7 +245,7 @@ void Entity::Initialize() {
* Not all components are implemented. Some are represented by a nullptr, as they hold no data.
*/

if (GetParentUser()) {
if (m_Character && m_Character->GetParentUser()) {
AddComponent<MissionComponent>()->LoadFromXml(m_Character->GetXMLDoc());
}

Expand Down Expand Up @@ -437,7 +470,8 @@ void Entity::Initialize() {

AddComponent<PlayerForcedMovementComponent>();

AddComponent<CharacterComponent>(m_Character)->LoadFromXml(m_Character->GetXMLDoc());
auto& systemAddress = m_Character->GetParentUser() ? m_Character->GetParentUser()->GetSystemAddress() : UNASSIGNED_SYSTEM_ADDRESS;
AddComponent<CharacterComponent>(m_Character, systemAddress)->LoadFromXml(m_Character->GetXMLDoc());

AddComponent<GhostComponent>();
}
Expand Down Expand Up @@ -788,14 +822,6 @@ bool Entity::operator!=(const Entity& other) const {
return other.m_ObjectID != m_ObjectID;
}

User* Entity::GetParentUser() const {
if (!IsPlayer()) {
return nullptr;
}

return static_cast<const Player*>(this)->GetParentUser();
}

Component* Entity::GetComponent(eReplicaComponentType componentID) const {
const auto& index = m_Components.find(componentID);

Expand Down Expand Up @@ -850,17 +876,12 @@ void Entity::SetProximityRadius(dpEntity* entity, std::string name) {

void Entity::SetGMLevel(eGameMasterLevel value) {
m_GMLevel = value;
if (GetParentUser()) {
Character* character = GetParentUser()->GetLastUsedChar();
if (m_Character) m_Character->SetGMLevel(value);

if (character) {
character->SetGMLevel(value);
}
}
auto* characterComponent = GetComponent<CharacterComponent>();
if (!characterComponent) return;

CharacterComponent* character = GetComponent<CharacterComponent>();
if (!character) return;
character->SetGMLevel(value);
characterComponent->SetGMLevel(value);

GameMessages::SendGMLevelBroadcast(m_ObjectID, value);

Expand Down Expand Up @@ -1630,18 +1651,23 @@ bool Entity::GetIsDead() const {

void Entity::AddLootItem(const Loot::Info& info) {
if (!IsPlayer()) return;
auto& droppedLoot = static_cast<Player*>(this)->GetDroppedLoot();

auto* characterComponent = GetComponent<CharacterComponent>();
if (!characterComponent) return;

auto& droppedLoot = characterComponent->GetDroppedLoot();
droppedLoot.insert(std::make_pair(info.id, info));
}

void Entity::PickupItem(const LWOOBJID& objectID) {
if (!IsPlayer()) return;
InventoryComponent* inv = GetComponent<InventoryComponent>();
if (!inv) return;
auto* characterComponent = GetComponent<CharacterComponent>();
if (!inv || !characterComponent) return;

CDObjectsTable* objectsTable = CDClientManager::Instance().GetTable<CDObjectsTable>();

auto& droppedLoot = static_cast<Player*>(this)->GetDroppedLoot();
auto& droppedLoot = characterComponent->GetDroppedLoot();

for (const auto& p : droppedLoot) {
if (p.first == objectID) {
Expand Down Expand Up @@ -1677,22 +1703,28 @@ void Entity::PickupItem(const LWOOBJID& objectID) {

bool Entity::CanPickupCoins(uint64_t count) {
if (!IsPlayer()) return false;
auto* player = static_cast<Player*>(this);
auto droppedCoins = player->GetDroppedCoins();

auto* characterComponent = GetComponent<CharacterComponent>();
if (!characterComponent) return false;

auto droppedCoins = characterComponent->GetDroppedCoins();
if (count > droppedCoins) {
return false;
} else {
player->SetDroppedCoins(droppedCoins - count);
characterComponent->SetDroppedCoins(droppedCoins - count);
return true;
}
}

void Entity::RegisterCoinDrop(uint64_t count) {
if (!IsPlayer()) return;
auto* player = static_cast<Player*>(this);
auto droppedCoins = player->GetDroppedCoins();

auto* characterComponent = GetComponent<CharacterComponent>();
if (!characterComponent) return;

auto droppedCoins = characterComponent->GetDroppedCoins();
droppedCoins += count;
player->SetDroppedCoins(droppedCoins);
characterComponent->SetDroppedCoins(droppedCoins);
}

void Entity::AddChild(Entity* child) {
Expand Down Expand Up @@ -1990,7 +2022,7 @@ std::vector<LWOOBJID> Entity::GetTargetsInPhantom() {
// Clean up invalid targets, like disconnected players
m_TargetsInPhantom.erase(std::remove_if(m_TargetsInPhantom.begin(), m_TargetsInPhantom.end(), [](const LWOOBJID id) {
return !Game::entityManager->GetEntity(id);
}), m_TargetsInPhantom.end());
}), m_TargetsInPhantom.end());

std::vector<LWOOBJID> enemies;
for (const auto id : m_TargetsInPhantom) {
Expand Down Expand Up @@ -2133,3 +2165,27 @@ void Entity::ProcessPositionUpdate(PositionUpdate& update) {

if (updateChar) Game::entityManager->SerializeEntity(this);
}

const SystemAddress& Entity::GetSystemAddress() const {
auto* characterComponent = GetComponent<CharacterComponent>();
return characterComponent ? characterComponent->GetSystemAddress() : UNASSIGNED_SYSTEM_ADDRESS;
}

const NiPoint3& Entity::GetRespawnPosition() const {
auto* characterComponent = GetComponent<CharacterComponent>();
return characterComponent ? characterComponent->GetRespawnPosition() : NiPoint3Constant::ZERO;
}

const NiQuaternion& Entity::GetRespawnRotation() const {
auto* characterComponent = GetComponent<CharacterComponent>();
return characterComponent ? characterComponent->GetRespawnRotation() : NiQuaternionConstant::IDENTITY;
}

void Entity::SetRespawnPos(const NiPoint3& position) {
auto* characterComponent = GetComponent<CharacterComponent>();
if (characterComponent) characterComponent->SetRespawnPos(position);
}
void Entity::SetRespawnRot(const NiQuaternion& rotation) {
auto* characterComponent = GetComponent<CharacterComponent>();
if (characterComponent) characterComponent->SetRespawnRot(rotation);
}
20 changes: 8 additions & 12 deletions dGame/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ namespace CppScripts {
*/
class Entity {
public:
explicit Entity(const LWOOBJID& objectID, EntityInfo info, Entity* parentEntity = nullptr);
virtual ~Entity();
explicit Entity(const LWOOBJID& objectID, EntityInfo info, User* parentUser = nullptr, Entity* parentEntity = nullptr);
~Entity();

virtual void Initialize();
void Initialize();

bool operator==(const Entity& other) const;
bool operator!=(const Entity& other) const;
Expand Down Expand Up @@ -104,9 +104,7 @@ class Entity {

const NiQuaternion& GetRotation() const;

virtual User* GetParentUser() const;

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

/**
* Setters
Expand All @@ -128,11 +126,9 @@ class Entity {

void SetRotation(const NiQuaternion& rotation);

virtual void SetRespawnPos(const NiPoint3& position) {}

virtual void SetRespawnRot(const NiQuaternion& rotation) {}
void SetRespawnPos(const NiPoint3& position);

virtual void SetSystemAddress(const SystemAddress& value) {};
void SetRespawnRot(const NiQuaternion& rotation);

/**
* Component management
Expand Down Expand Up @@ -229,8 +225,8 @@ class Entity {
void TriggerEvent(eTriggerEventType event, Entity* optionalTarget = nullptr);
void ScheduleDestructionAfterUpdate() { m_ShouldDestroyAfterUpdate = true; }

virtual const NiPoint3& GetRespawnPosition() const { return NiPoint3Constant::ZERO; }
virtual const NiQuaternion& GetRespawnRotation() const { return NiQuaternionConstant::IDENTITY; }
const NiPoint3& GetRespawnPosition() const;
const NiQuaternion& GetRespawnRotation() const;

void Sleep();
void Wake();
Expand Down
12 changes: 2 additions & 10 deletions dGame/EntityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "GeneralUtils.h"
#include "dServer.h"
#include "Spawner.h"
#include "Player.h"
#include "SkillComponent.h"
#include "SwitchComponent.h"
#include "UserManager.h"
Expand Down Expand Up @@ -118,14 +117,7 @@ Entity* EntityManager::CreateEntity(EntityInfo info, User* user, Entity* parentE

info.id = id;

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);
} else {
entity = new Entity(id, info, parentEntity);
}
Entity* entity = new Entity(id, info, user, parentEntity);

// Initialize the entity
entity->Initialize();
Expand Down Expand Up @@ -482,7 +474,7 @@ void EntityManager::UpdateGhosting() {
m_PlayersToUpdateGhosting.clear();
}

void EntityManager::UpdateGhosting(Player* player) {
void EntityManager::UpdateGhosting(Entity* player) {
if (player == nullptr) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion dGame/EntityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class EntityManager {
float GetGhostDistanceMin() const;
void QueueGhostUpdate(LWOOBJID playerID);
void UpdateGhosting();
void UpdateGhosting(Player* player);
void UpdateGhosting(Entity* player);
void CheckGhosting(Entity* entity);
Entity* GetGhostCandidate(int32_t id);
bool GetGhostingEnabled() const;
Expand Down
Loading
Loading