Files
DarkflameServer/dGame/dComponents/Character/GhostComponent.cpp
Aaron Kimbrell d08b35815e feat(gm): GM invisibility is kept in the character save
LWOGhostComponent::LoadFromSaveData reads <ghst i> under <obj> and
the client hides its own character again once the scene has loaded when it is
set. Nothing wrote or read the element, so a GM was visible again after every
world change.

GhostComponent now writes <ghst i="1"/> while invisible and reads it back. A
save without the element loads as visible and is not given one; an element
that is set goes back to 0 when the GM is visible again.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-10 21:21:33 -06:00

146 lines
4.7 KiB
C++

#include "GhostComponent.h"
#include "PlayerManager.h"
#include "Character.h"
#include "ControllablePhysicsComponent.h"
#include "UserManager.h"
#include "User.h"
#include "Amf3.h"
#include "tinyxml2.h"
#include "GameMessages.h"
#include "PlayerMessages.h"
GhostComponent::GhostComponent(Entity* parent, const int32_t componentID) : Component(parent, componentID) {
m_GhostReferencePoint = NiPoint3Constant::ZERO;
m_GhostOverridePoint = NiPoint3Constant::ZERO;
m_GhostOverride = false;
RegisterMsg(&GhostComponent::OnToggleGMInvis);
RegisterMsg(&GhostComponent::OnGetGMInvis);
RegisterMsg(&GhostComponent::MsgGetObjectReportInfo);
}
GhostComponent::~GhostComponent() {
for (auto& observedEntity : m_ObservedEntities) {
if (observedEntity == LWOOBJID_EMPTY) continue;
auto* entity = Game::entityManager->GetGhostCandidate(observedEntity);
if (!entity) continue;
entity->SetObservers(entity->GetObservers() - 1);
}
}
void GhostComponent::SetGhostReferencePoint(const NiPoint3& value) {
m_GhostReferencePoint = value;
}
void GhostComponent::SetGhostOverridePoint(const NiPoint3& value) {
m_GhostOverridePoint = value;
}
void GhostComponent::AddLimboConstruction(LWOOBJID objectId) {
m_LimboConstructions.insert(objectId);
}
void GhostComponent::RemoveLimboConstruction(LWOOBJID objectId) {
m_LimboConstructions.erase(objectId);
}
void GhostComponent::ConstructLimboEntities() {
for (const auto& objectId : m_LimboConstructions) {
auto* entity = Game::entityManager->GetEntity(objectId);
if (!entity) continue;
Game::entityManager->ConstructEntity(entity, m_Parent->GetSystemAddress());
}
m_LimboConstructions.clear();
}
void GhostComponent::ObserveEntity(LWOOBJID id) {
m_ObservedEntities.insert(id);
}
bool GhostComponent::IsObserved(LWOOBJID id) {
return m_ObservedEntities.contains(id);
}
void GhostComponent::GhostEntity(LWOOBJID id) {
m_ObservedEntities.erase(id);
}
void GhostComponent::LoadFromSaveData(const tinyxml2::XMLDocument& doc) {
const auto* const obj = doc.FirstChildElement("obj");
const auto* const ghost = obj ? obj->FirstChildElement("ghst") : nullptr;
// A save without the element, or with an i that is not a boolean, is a visible character
if (ghost) ghost->QueryBoolAttribute("i", &m_IsGMInvisible);
}
void GhostComponent::WriteSaveData(tinyxml2::XMLDocument& doc) {
auto* const obj = doc.FirstChildElement("obj");
if (!obj) return;
auto* ghost = obj->FirstChildElement("ghst");
if (!ghost) {
if (!m_IsGMInvisible) return;
ghost = obj->InsertNewChildElement("ghst");
}
// Only a value that says something else is rewritten
bool saved = false;
const bool readable = ghost->QueryBoolAttribute("i", &saved) == tinyxml2::XML_SUCCESS;
if (m_IsGMInvisible) {
if (!readable || !saved) ghost->SetAttribute("i", 1);
} else if (readable && saved) {
ghost->SetAttribute("i", 0);
}
}
bool GhostComponent::OnToggleGMInvis(GameMessages::ToggleGMInvisEvent& event) {
auto& gmInvisMsg = event.msg;
gmInvisMsg.target = event.target; // the wire message goes out with the target the event was delivered to
gmInvisMsg.bStateOut = !m_IsGMInvisible;
m_IsGMInvisible = !m_IsGMInvisible;
LOG_DEBUG("GM Invisibility toggled to: %s", m_IsGMInvisible ? "true" : "false");
gmInvisMsg.Send(UNASSIGNED_SYSTEM_ADDRESS);
auto* thisUser = UserManager::Instance()->GetUser(m_Parent->GetSystemAddress());
if (!thisUser) {
LOG("Unable to find user for entity %llu when toggling GM invisibility!", m_Parent->GetObjectID());
return false;
}
for (const auto& player : PlayerManager::GetAllPlayers()) {
if (!player || player->GetObjectID() == m_Parent->GetObjectID()) continue;
auto* toUser = UserManager::Instance()->GetUser(player->GetSystemAddress());
if (m_IsGMInvisible) {
if (toUser->GetMaxGMLevel() < thisUser->GetMaxGMLevel()) {
Game::entityManager->DestructEntity(m_Parent, player->GetSystemAddress());
}
} else {
if (toUser->GetMaxGMLevel() < thisUser->GetMaxGMLevel()) {
Game::entityManager->ConstructEntity(m_Parent, player->GetSystemAddress());
auto* controllableComp = m_Parent->GetComponent<ControllablePhysicsComponent>();
controllableComp->SetDirtyPosition(true);
}
}
}
Game::entityManager->SerializeEntity(m_Parent);
return true;
}
bool GhostComponent::OnGetGMInvis(GameMessages::GetGMInvis& gmInvisMsg) {
LOG_DEBUG("GM Invisibility requested: %s", m_IsGMInvisible ? "true" : "false");
gmInvisMsg.bGMInvis = m_IsGMInvisible;
return gmInvisMsg.bGMInvis;
}
bool GhostComponent::MsgGetObjectReportInfo(GameMessages::GetObjectReportInfo& reportMsg) {
auto& cmptType = reportMsg.info->PushDebug("Ghost");
cmptType.PushDebug<AMFIntValue>("Component ID") = GetComponentID();
cmptType.PushDebug<AMFBoolValue>("Is GM Invis") = m_IsGMInvisible;
return true;
}