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

Actor activation support improvements #1843

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions skymp5-server/cpp/server_guest_lib/MpActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,38 @@ void MpActor::RemoveSpell(const uint32_t spellId)
});
}

espm::LookupResult MpActor::GetRace() const
{
uint32_t raceId = 0;

if (auto appearance = GetAppearance()) {
raceId = appearance->raceId;
} else {
raceId = EvaluateTemplate<espm::NPC_::UseTraits>(
GetParent(), GetBaseId(), GetTemplateChain(),
[](const auto& npcLookupResult, const auto& npcData) {
return npcLookupResult.ToGlobalId(npcData.race);
});
}

auto lookupRes = GetParent()->GetEspm().GetBrowser().LookupById(raceId);

if (!lookupRes.rec) {
spdlog::error("MpActor::GetRace - Race with id {:x} not found in espm",
raceId);
return {};
}

if (!(lookupRes.rec->GetType() == espm::RACE::kType)) {
spdlog::error(
"MpActor::GetRace - Expected record {:x} to be RACE, but it is {}",
raceId, lookupRes.rec->GetType().ToString());
return {};
}

return lookupRes;
}

void MpActor::SetRaceMenuOpen(bool isOpen)
{
EditChangeForm(
Expand Down
2 changes: 2 additions & 0 deletions skymp5-server/cpp/server_guest_lib/MpActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class MpActor : public MpObjectReference
void AddSpell(uint32_t spellId);
void RemoveSpell(uint32_t spellId);

espm::LookupResult GetRace() const;

private:
struct Impl;
std::shared_ptr<Impl> pImpl;
Expand Down
112 changes: 110 additions & 2 deletions skymp5-server/cpp/server_guest_lib/MpObjectReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Primitive.h"
#include "ScopedTask.h"
#include "ScriptVariablesHolder.h"
#include "SpSnippet.h"
#include "TimeUtils.h"
#include "WorldState.h"
#include "libespm/GroupUtils.h"
Expand Down Expand Up @@ -287,8 +288,15 @@ void MpObjectReference::VisitProperties(const PropertiesVisitor& visitor,
visitor("isOpen", "true");
}

if (auto actor = dynamic_cast<MpActor*>(this); actor && actor->IsDead()) {
visitor("isDead", "true");
if (auto actor = dynamic_cast<MpActor*>(this)) {
if (actor->IsDead()) {
visitor("isDead", "true");
}

if (this->occupant) {
auto occupantFormIdStr = std::to_string(this->occupant->GetFormId());
visitor("occupant", occupantFormIdStr.data());
}
}

if (mode == VisitPropertiesMode::All && !GetInventory().IsEmpty()) {
Expand Down Expand Up @@ -1338,6 +1346,106 @@ void MpObjectReference::ProcessActivate(MpObjectReference& activationSource)
this->occupant->RemoveEventSink(this->occupantDestroySink);
this->occupant = nullptr;
}
} else if (t == espm::NPC_::kType && actorActivator) {
if (auto thisActor = dynamic_cast<MpActor*>(this)) {
if (auto race = thisActor->GetRace(); race.rec) {
bool spSnippetSendActivate = false;

auto raceId = race.ToGlobalId(race.rec->GetId());
if (raceId == 0x000131fd) {
auto occupantPos =
this->occupant ? this->occupant->GetPos() : NiPoint3();
auto occupantCellOrWorld =
this->occupant ? this->occupant->GetCellOrWorld() : FormDesc();

constexpr float kOccupationReach = 512.f;
auto distanceToOccupant = (occupantPos - GetPos()).Length();

if (!this->occupant || this->occupant->IsDisabled() ||
distanceToOccupant > kOccupationReach ||
occupantCellOrWorld != GetCellOrWorld()) {
if (this->occupant) {
this->occupant->RemoveEventSink(this->occupantDestroySink);
}
spSnippetSendActivate = true;
} else if (this->occupant == &activationSource) {
this->occupant->RemoveEventSink(this->occupantDestroySink);
this->occupant = nullptr;
spSnippetSendActivate = true;
}
}

if (spSnippetSendActivate) {
auto j = nlohmann::json::array();
// akActivator
j.push_back(
nlohmann::json({ { "formId", actorActivator->GetFormId() },
{ "type", "ObjectReference" } }));
// abDefaultProcessingOnly
j.push_back(true);

std::string dump = j.dump();
SpSnippet("ObjectReference", "Activate", dump.data(),
this->GetFormId())
.Execute(actorActivator);

SendPropertyToListeners(
"occupant", this->occupant ? this->occupant->GetFormId() : 0);

// Prepare variables
auto& remote = *this;
auto remoteId = GetFormId();
auto me = actorActivator;
auto remoteAsActor = dynamic_cast<MpActor*>(&remote);

// Implementation based on ActionListener::OnHostAttempt
auto& hoster = GetParent()->hosters[remoteId];
const uint32_t prevHoster = hoster;
hoster = me->GetFormId();
remote.UpdateHoster(hoster);

uint64_t longFormId = remote.GetFormId();
if (remoteAsActor && longFormId < 0xff000000) {
longFormId += 0x100000000;
}

Networking::Format(
[&](Networking::PacketData data, size_t len) {
// The only reason for deferred here is that it still supports
// raw binary data send
// TODO: change to SendToUser
constexpr int kChannelHost = 2;
me->SendToUserDeferred(data, len, true, kChannelHost, false);
},
R"({ "type": "hostStart", "target": %llu })", longFormId);

if (MpActor* prevHosterActor = dynamic_cast<MpActor*>(
GetParent()->LookupFormById(prevHoster).get())) {
if (prevHosterActor != me) {
Networking::Format(
[&](Networking::PacketData data, size_t len) {
// The only reason for deferred here is that it still
// supports raw binary data send
// TODO: change to SendToUser
constexpr int kChannelHost = 2;
prevHosterActor->SendToUserDeferred(data, len, true,
kChannelHost, false);
},
R"({ "type": "hostStop", "target": %llu })", longFormId);
}
}
}
} else {
spdlog::error(
"MpObjectReference::ProcessActivate {:x} - Unable to find race",
GetFormId());
}
} else {
spdlog::error(
"MpObjectReference::ProcessActivate {:x} - NPC_ base type, but "
"not an MpActor",
GetFormId());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,31 +380,9 @@ VarValue PapyrusActor::GetRace(VarValue self,
return VarValue::None();
}

uint32_t raceId = 0;

if (auto appearance = actor->GetAppearance()) {
raceId = appearance->raceId;
} else {
raceId = EvaluateTemplate<espm::NPC_::UseTraits>(
actor->GetParent(), actor->GetBaseId(), actor->GetTemplateChain(),
[](const auto& npcLookupResult, const auto& npcData) {
return npcLookupResult.ToGlobalId(npcData.race);
});
}

auto lookupRes =
actor->GetParent()->GetEspm().GetBrowser().LookupById(raceId);
auto lookupRes = actor->GetRace();

if (!lookupRes.rec) {
spdlog::error("Actor.GetRace - Race with id {:x} not found in espm",
raceId);
return VarValue::None();
}

if (!(lookupRes.rec->GetType() == espm::RACE::kType)) {
spdlog::error(
"Actor.GetRace - Expected record {:x} to be RACE, but it is {}", raceId,
lookupRes.rec->GetType().ToString());
return VarValue::None();
}

Expand Down
Loading