2022-08-06 03:01:59 +00:00
|
|
|
#include "Player.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include "Character.h"
|
|
|
|
#include "Database.h"
|
|
|
|
#include "MissionComponent.h"
|
|
|
|
#include "UserManager.h"
|
|
|
|
#include "EntityManager.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "ZoneInstanceManager.h"
|
|
|
|
#include "WorldPackets.h"
|
|
|
|
#include "dZoneManager.h"
|
|
|
|
#include "CharacterComponent.h"
|
|
|
|
#include "Mail.h"
|
2023-01-07 05:17:05 +00:00
|
|
|
#include "User.h"
|
2022-06-19 07:14:33 +00:00
|
|
|
#include "CppScripts.h"
|
2023-01-07 05:17:05 +00:00
|
|
|
#include "Loot.h"
|
2023-03-04 07:16:37 +00:00
|
|
|
#include "eReplicaComponentType.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
namespace {
|
|
|
|
std::vector<Player*> m_Players;
|
|
|
|
};
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
const std::vector<Player*>& Player::GetAllPlayers() {
|
|
|
|
return m_Players;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
void Player::SetGhostReferencePoint(const NiPoint3& value) {
|
|
|
|
m_GhostReferencePoint = value;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
void Player::SetGhostOverridePoint(const NiPoint3& value) {
|
|
|
|
m_GhostOverridePoint = value;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
void Player::SetRespawnPos(const NiPoint3& position) {
|
2023-12-22 04:12:52 +00:00
|
|
|
if (!m_Character) return;
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
m_respawnPos = position;
|
|
|
|
|
2023-07-17 22:55:33 +00:00
|
|
|
m_Character->SetRespawnPoint(Game::zoneManager->GetZone()->GetWorldID(), position);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
void Player::SetRespawnRot(const NiQuaternion& rotation) {
|
|
|
|
m_respawnRot = rotation;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
void Player::SetSystemAddress(const SystemAddress& value) {
|
|
|
|
m_SystemAddress = value;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
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;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
m_GhostReferencePoint = NiPoint3::ZERO;
|
|
|
|
m_GhostOverridePoint = NiPoint3::ZERO;
|
|
|
|
m_GhostOverride = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
int32_t initialObservedEntitiesCapacity = 256;
|
|
|
|
m_ObservedEntities.resize(initialObservedEntitiesCapacity);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
m_Character->SetEntity(this);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
const auto& iter = std::find(m_Players.begin(), m_Players.end(), this);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
if (iter == m_Players.end()) {
|
|
|
|
m_Players.push_back(this);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Player::AddLimboConstruction(LWOOBJID objectId) {
|
2024-01-11 12:33:53 +00:00
|
|
|
const auto iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId);
|
|
|
|
if (iter == m_LimboConstructions.end()) {
|
|
|
|
m_LimboConstructions.push_back(objectId);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Player::RemoveLimboConstruction(LWOOBJID objectId) {
|
2024-01-11 12:33:53 +00:00
|
|
|
const auto iter = std::find(m_LimboConstructions.begin(), m_LimboConstructions.end(), objectId);
|
|
|
|
if (iter != m_LimboConstructions.end()) {
|
|
|
|
m_LimboConstructions.erase(iter);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Player::ConstructLimboEntities() {
|
2024-01-11 12:33:53 +00:00
|
|
|
for (const auto& objectId : m_LimboConstructions) {
|
2023-07-15 20:56:33 +00:00
|
|
|
auto* entity = Game::entityManager->GetEntity(objectId);
|
2024-01-11 12:33:53 +00:00
|
|
|
if (!entity) continue;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-15 20:56:33 +00:00
|
|
|
Game::entityManager->ConstructEntity(entity, m_SystemAddress);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
m_LimboConstructions.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Player::ObserveEntity(int32_t id) {
|
2024-01-11 12:33:53 +00:00
|
|
|
for (auto& observedEntity : m_ObservedEntities) {
|
|
|
|
if (observedEntity == 0 || observedEntity == id) {
|
|
|
|
observedEntity = id;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
m_ObservedEntities.reserve(m_ObservedEntities.size() + 1);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
m_ObservedEntities.push_back(id);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool Player::IsObserved(int32_t id) {
|
2024-01-11 12:33:53 +00:00
|
|
|
return std::find(m_ObservedEntities.begin(), m_ObservedEntities.end(), id) != m_ObservedEntities.end();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void Player::GhostEntity(int32_t id) {
|
2024-01-11 12:33:53 +00:00
|
|
|
for (auto& observedEntity : m_ObservedEntities) {
|
|
|
|
if (observedEntity == id) {
|
|
|
|
observedEntity = 0;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Player* Player::GetPlayer(const SystemAddress& sysAddr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* entity = UserManager::Instance()->GetUser(sysAddr)->GetLastUsedChar()->GetEntity();
|
|
|
|
|
|
|
|
return static_cast<Player*>(entity);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Player* Player::GetPlayer(const std::string& name) {
|
2023-07-15 20:56:33 +00:00
|
|
|
const auto characters = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::CHARACTER);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
Player* player = nullptr;
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* character : characters) {
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!character->IsPlayer()) continue;
|
2023-11-16 01:29:00 +00:00
|
|
|
|
|
|
|
if (GeneralUtils::CaseInsensitiveStringCompare(name, character->GetCharacter()->GetName())) {
|
2024-01-11 12:33:53 +00:00
|
|
|
player = dynamic_cast<Player*>(character);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
return player;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Player* Player::GetPlayer(LWOOBJID playerID) {
|
2024-01-11 12:33:53 +00:00
|
|
|
Player* playerToReturn = nullptr;
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* player : m_Players) {
|
|
|
|
if (player->GetObjectID() == playerID) {
|
2024-01-11 12:33:53 +00:00
|
|
|
playerToReturn = player;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
return playerToReturn;
|
2021-12-11 13:21:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Player::~Player() {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Deleted player");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
for (auto& observedEntity : m_ObservedEntities) {
|
|
|
|
if (observedEntity == 0) continue;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
auto* entity = Game::entityManager->GetGhostCandidate(observedEntity);
|
|
|
|
if (!entity) continue;
|
|
|
|
|
|
|
|
entity->SetObservers(entity->GetObservers() - 1);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_LimboConstructions.clear();
|
|
|
|
|
2024-01-11 12:33:53 +00:00
|
|
|
const auto iter = std::find(m_Players.begin(), m_Players.end(), this);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (iter == m_Players.end()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 07:14:33 +00:00
|
|
|
if (IsPlayer()) {
|
2023-07-15 20:56:33 +00:00
|
|
|
Entity* zoneControl = Game::entityManager->GetZoneControlEntity();
|
2022-07-28 13:39:57 +00:00
|
|
|
for (CppScripts::Script* script : CppScripts::GetEntityScripts(zoneControl)) {
|
|
|
|
script->OnPlayerExit(zoneControl, this);
|
|
|
|
}
|
|
|
|
|
2023-07-15 20:56:33 +00:00
|
|
|
std::vector<Entity*> scriptedActs = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::SCRIPTED_ACTIVITY);
|
2022-07-28 13:39:57 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-19 07:14:33 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
m_Players.erase(iter);
|
|
|
|
}
|