Initial changes.

* Recorder to recall player actions.
* Server precondtions to manage entity visiblity.
This commit is contained in:
wincent
2023-10-22 17:36:08 +02:00
parent 50921cce2d
commit 9e56725cff
18 changed files with 1101 additions and 66 deletions

View File

@@ -5,4 +5,6 @@ set(DGAME_DUTILITIES_SOURCES "BrickDatabase.cpp"
"Mail.cpp"
"Preconditions.cpp"
"SlashCommandHandler.cpp"
"Recorder.cpp"
"ServerPreconditions.cpp"
"VanityUtilities.cpp" PARENT_SCOPE)

View File

@@ -0,0 +1,474 @@
#include "Recorder.h"
#include "ControllablePhysicsComponent.h"
#include "GameMessages.h"
#include "InventoryComponent.h"
#include "../dWorldServer/ObjectIDManager.h"
#include "ChatPackets.h"
#include "EntityManager.h"
#include "EntityInfo.h"
#include "ServerPreconditions.hpp"
using namespace Recording;
std::unordered_map<LWOOBJID, Recorder*> m_Recorders = {};
Recorder::Recorder() {
this->m_StartTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
this->m_IsRecording = false;
}
Recorder::~Recorder() {
}
void Recorder::AddRecord(Record* record)
{
if (!this->m_IsRecording) {
return;
}
Game::logger->Log("Recorder", "Adding record");
// Time since start of recording
record->m_Timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()) - this->m_StartTime;
this->m_MovementRecords.push_back(record);
}
void Recorder::Act(Entity* actor) {
Game::logger->Log("Recorder", "Acting %d steps", m_MovementRecords.size());
// Loop through all records
for (auto* record : m_MovementRecords) {
record->Act(actor);
}
}
Entity* Recording::Recorder::ActFor(Entity* actorTemplate, Entity* player) {
EntityInfo info;
info.lot = actorTemplate->GetLOT();
info.pos = actorTemplate->GetPosition();
info.rot = actorTemplate->GetRotation();
info.scale = 1;
info.spawner = nullptr;
info.spawnerID = player->GetObjectID();
info.spawnerNodeID = 0;
info.settings = {
new LDFData<std::vector<std::u16string>>(u"syncLDF", { u"custom_script_client" }),
new LDFData<std::u16string>(u"custom_script_client", u"scripts\\ai\\SPEC\\MISSION_MINIGAME_CLIENT.lua")
};
// Spawn it
auto* actor = Game::entityManager->CreateEntity(info);
// Hide the template from the player
ServerPreconditions::AddExcludeFor(player->GetObjectID(), actorTemplate->GetObjectID());
// Solo act for the player
ServerPreconditions::AddSoloActor(actor->GetObjectID(), player->GetObjectID());
Game::entityManager->ConstructEntity(actor);
Act(actor);
return actor;
}
void Recording::Recorder::StopActingFor(Entity* actor, Entity* actorTemplate, LWOOBJID playerID) {
// Remove the exclude for the player
ServerPreconditions::RemoveExcludeFor(playerID, actorTemplate->GetObjectID());
Game::entityManager->DestroyEntity(actor);
}
bool Recorder::IsRecording() const {
return this->m_IsRecording;
}
void Recorder::StartRecording(LWOOBJID actorID) {
const auto& it = m_Recorders.find(actorID);
// Delete the old recorder if it exists
if (it != m_Recorders.end()) {
delete it->second;
m_Recorders.erase(it);
}
Recorder* recorder = new Recorder();
m_Recorders.insert_or_assign(actorID, recorder);
recorder->m_IsRecording = true;
}
void Recorder::StopRecording(LWOOBJID actorID) {
auto iter = m_Recorders.find(actorID);
if (iter == m_Recorders.end()) {
return;
}
iter->second->m_IsRecording = false;
}
Recorder* Recorder::GetRecorder(LWOOBJID actorID) {
auto iter = m_Recorders.find(actorID);
if (iter == m_Recorders.end()) {
return nullptr;
}
return iter->second;
}
Recording::MovementRecord::MovementRecord(const NiPoint3& position, const NiQuaternion& rotation, const NiPoint3& velocity, const NiPoint3& angularVelocity, bool onGround, bool dirtyVelocity, bool dirtyAngularVelocity) {
this->position = position;
this->rotation = rotation;
this->velocity = velocity;
this->angularVelocity = angularVelocity;
this->onGround = onGround;
this->dirtyVelocity = dirtyVelocity;
this->dirtyAngularVelocity = dirtyAngularVelocity;
}
void Recording::MovementRecord::Act(Entity* actor) {
// Calculate the amount of seconds (as a float) since the start of the recording
float time = m_Timestamp.count() / 1000.0f;
auto r = *this;
actor->AddCallbackTimer(time, [actor, r] () {
auto* controllableComponent = actor->GetComponent<ControllablePhysicsComponent>();
if (controllableComponent) {
controllableComponent->SetPosition(r.position);
controllableComponent->SetRotation(r.rotation);
controllableComponent->SetVelocity(r.velocity);
controllableComponent->SetAngularVelocity(r.angularVelocity);
controllableComponent->SetIsOnGround(r.onGround);
controllableComponent->SetDirtyVelocity(r.dirtyVelocity);
controllableComponent->SetDirtyAngularVelocity(r.dirtyAngularVelocity);
}
Game::entityManager->SerializeEntity(actor);
});
}
void Recording::MovementRecord::Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) {
auto* element = document.NewElement("MovementRecord");
element->SetAttribute("x", position.x);
element->SetAttribute("y", position.y);
element->SetAttribute("z", position.z);
element->SetAttribute("qx", rotation.x);
element->SetAttribute("qy", rotation.y);
element->SetAttribute("qz", rotation.z);
element->SetAttribute("qw", rotation.w);
element->SetAttribute("vx", velocity.x);
element->SetAttribute("vy", velocity.y);
element->SetAttribute("vz", velocity.z);
element->SetAttribute("avx", angularVelocity.x);
element->SetAttribute("avy", angularVelocity.y);
element->SetAttribute("avz", angularVelocity.z);
element->SetAttribute("g", onGround);
element->SetAttribute("dv", dirtyVelocity);
element->SetAttribute("dav", dirtyAngularVelocity);
element->SetAttribute("t", m_Timestamp.count());
parent->InsertEndChild(element);
}
void Recording::MovementRecord::Deserialize(tinyxml2::XMLElement* element) {
position.x = element->FloatAttribute("x");
position.y = element->FloatAttribute("y");
position.z = element->FloatAttribute("z");
rotation.x = element->FloatAttribute("qx");
rotation.y = element->FloatAttribute("qy");
rotation.z = element->FloatAttribute("qz");
rotation.w = element->FloatAttribute("qw");
velocity.x = element->FloatAttribute("vx");
velocity.y = element->FloatAttribute("vy");
velocity.z = element->FloatAttribute("vz");
angularVelocity.x = element->FloatAttribute("avx");
angularVelocity.y = element->FloatAttribute("avy");
angularVelocity.z = element->FloatAttribute("avz");
onGround = element->BoolAttribute("g");
dirtyVelocity = element->BoolAttribute("dv");
dirtyAngularVelocity = element->BoolAttribute("dav");
m_Timestamp = std::chrono::milliseconds(element->Int64Attribute("t"));
}
Recording::SpeakRecord::SpeakRecord(const std::string& text) {
this->text = text;
}
void Recording::SpeakRecord::Act(Entity* actor) {
// Calculate the amount of seconds (as a float) since the start of the recording
float time = m_Timestamp.count() / 1000.0f;
auto r = *this;
actor->AddCallbackTimer(time, [actor, r] () {
GameMessages::SendNotifyClientZoneObject(
actor->GetObjectID(), u"sendToclient_bubble", 0, 0, actor->GetObjectID(), r.text, UNASSIGNED_SYSTEM_ADDRESS);
Game::entityManager->SerializeEntity(actor);
});
}
void Recording::SpeakRecord::Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) {
auto* element = document.NewElement("SpeakRecord");
element->SetAttribute("text", text.c_str());
element->SetAttribute("t", m_Timestamp.count());
parent->InsertEndChild(element);
}
void Recording::SpeakRecord::Deserialize(tinyxml2::XMLElement* element) {
text = element->Attribute("text");
m_Timestamp = std::chrono::milliseconds(element->Int64Attribute("t"));
}
Recording::AnimationRecord::AnimationRecord(const std::string& animation) {
this->animation = animation;
}
void Recording::AnimationRecord::Act(Entity* actor) {
// Calculate the amount of seconds (as a float) since the start of the recording
float time = m_Timestamp.count() / 1000.0f;
auto r = *this;
actor->AddCallbackTimer(time, [actor, r] () {
GameMessages::SendPlayAnimation(actor, GeneralUtils::ASCIIToUTF16(r.animation));
Game::entityManager->SerializeEntity(actor);
});
}
void Recording::AnimationRecord::Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) {
auto* element = document.NewElement("AnimationRecord");
element->SetAttribute("animation", animation.c_str());
element->SetAttribute("t", m_Timestamp.count());
parent->InsertEndChild(element);
}
void Recording::AnimationRecord::Deserialize(tinyxml2::XMLElement* element) {
animation = element->Attribute("animation");
m_Timestamp = std::chrono::milliseconds(element->Int64Attribute("t"));
}
Recording::EquipRecord::EquipRecord(LOT item) {
this->item = item;
}
void Recording::EquipRecord::Act(Entity* actor) {
// Calculate the amount of seconds (as a float) since the start of the recording
float time = m_Timestamp.count() / 1000.0f;
auto r = *this;
actor->AddCallbackTimer(time, [actor, r] () {
auto* inventoryComponent = actor->GetComponent<InventoryComponent>();
const LWOOBJID id = ObjectIDManager::Instance()->GenerateObjectID();
const auto& info = Inventory::FindItemComponent(r.item);
if (inventoryComponent) {
inventoryComponent->UpdateSlot(info.equipLocation, { id, r.item, 1, 0 });
}
Game::entityManager->SerializeEntity(actor);
});
}
void Recording::EquipRecord::Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) {
auto* element = document.NewElement("EquipRecord");
element->SetAttribute("item", item);
element->SetAttribute("t", m_Timestamp.count());
parent->InsertEndChild(element);
}
void Recording::EquipRecord::Deserialize(tinyxml2::XMLElement* element) {
item = element->IntAttribute("item");
m_Timestamp = std::chrono::milliseconds(element->Int64Attribute("t"));
}
Recording::UnequipRecord::UnequipRecord(LOT item) {
this->item = item;
}
void Recording::UnequipRecord::Act(Entity* actor) {
// Calculate the amount of seconds (as a float) since the start of the recording
float time = m_Timestamp.count() / 1000.0f;
auto r = *this;
actor->AddCallbackTimer(time, [actor, r] () {
auto* inventoryComponent = actor->GetComponent<InventoryComponent>();
const auto& info = Inventory::FindItemComponent(r.item);
if (inventoryComponent) {
inventoryComponent->RemoveSlot(info.equipLocation);
}
Game::entityManager->SerializeEntity(actor);
});
}
void Recording::UnequipRecord::Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) {
auto* element = document.NewElement("UnequipRecord");
element->SetAttribute("item", item);
element->SetAttribute("t", m_Timestamp.count());
parent->InsertEndChild(element);
}
void Recording::UnequipRecord::Deserialize(tinyxml2::XMLElement* element) {
item = element->IntAttribute("item");
m_Timestamp = std::chrono::milliseconds(element->Int64Attribute("t"));
}
void Recording::ClearEquippedRecord::Act(Entity* actor) {
// Calculate the amount of seconds (as a float) since the start of the recording
float time = m_Timestamp.count() / 1000.0f;
auto r = *this;
actor->AddCallbackTimer(time, [actor, r] () {
auto* inventoryComponent = actor->GetComponent<InventoryComponent>();
if (inventoryComponent) {
auto equipped = inventoryComponent->GetEquippedItems();
for (auto entry : equipped) {
inventoryComponent->RemoveSlot(entry.first);
}
}
Game::entityManager->SerializeEntity(actor);
});
}
void Recording::ClearEquippedRecord::Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) {
auto* element = document.NewElement("ClearEquippedRecord");
element->SetAttribute("t", m_Timestamp.count());
parent->InsertEndChild(element);
}
void Recording::ClearEquippedRecord::Deserialize(tinyxml2::XMLElement* element) {
m_Timestamp = std::chrono::milliseconds(element->Int64Attribute("t"));
}
void Recording::Recorder::SaveToFile(const std::string& filename) {
tinyxml2::XMLDocument document;
auto* root = document.NewElement("Recorder");
for (auto* record : m_MovementRecords) {
record->Serialize(document, root);
}
document.InsertFirstChild(root);
document.SaveFile(filename.c_str());
}
float Recording::Recorder::GetDuration() const {
// Return the highest timestamp
float duration = 0.0f;
for (auto* record : m_MovementRecords) {
duration = std::max(duration, record->m_Timestamp.count() / 1000.0f);
}
return duration;
}
Recorder* Recording::Recorder::LoadFromFile(const std::string& filename) {
tinyxml2::XMLDocument document;
if (document.LoadFile(filename.c_str()) != tinyxml2::XML_SUCCESS) {
return nullptr;
}
auto* root = document.FirstChildElement("Recorder");
if (!root) {
return nullptr;
}
Recorder* recorder = new Recorder();
for (auto* element = root->FirstChildElement(); element; element = element->NextSiblingElement()) {
const std::string name = element->Name();
if (name == "MovementRecord") {
MovementRecord* record = new MovementRecord();
record->Deserialize(element);
recorder->m_MovementRecords.push_back(record);
} else if (name == "SpeakRecord") {
SpeakRecord* record = new SpeakRecord();
record->Deserialize(element);
recorder->m_MovementRecords.push_back(record);
} else if (name == "AnimationRecord") {
AnimationRecord* record = new AnimationRecord();
record->Deserialize(element);
recorder->m_MovementRecords.push_back(record);
} else if (name == "EquipRecord") {
EquipRecord* record = new EquipRecord();
record->Deserialize(element);
recorder->m_MovementRecords.push_back(record);
} else if (name == "UnequipRecord") {
UnequipRecord* record = new UnequipRecord();
record->Deserialize(element);
recorder->m_MovementRecords.push_back(record);
} else if (name == "ClearEquippedRecord") {
ClearEquippedRecord* record = new ClearEquippedRecord();
record->Deserialize(element);
recorder->m_MovementRecords.push_back(record);
}
}
return recorder;
}
void Recording::Recorder::AddRecording(LWOOBJID actorID, Recorder* recorder) {
const auto& it = m_Recorders.find(actorID);
// Delete the old recorder if it exists
if (it != m_Recorders.end()) {
delete it->second;
m_Recorders.erase(it);
}
m_Recorders.insert_or_assign(actorID, recorder);
}

170
dGame/dUtilities/Recorder.h Normal file
View File

@@ -0,0 +1,170 @@
#pragma once
#include "Player.h"
#include "Game.h"
#include "EntityManager.h"
#include "tinyxml2.h"
#include <chrono>
namespace Recording
{
class Record
{
public:
virtual void Act(Entity* actor) = 0;
virtual void Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) = 0;
virtual void Deserialize(tinyxml2::XMLElement* element) = 0;
std::chrono::milliseconds m_Timestamp;
};
class MovementRecord : public Record
{
public:
NiPoint3 position;
NiQuaternion rotation;
NiPoint3 velocity;
NiPoint3 angularVelocity;
bool onGround;
bool dirtyVelocity;
bool dirtyAngularVelocity;
MovementRecord() = default;
MovementRecord(
const NiPoint3& position,
const NiQuaternion& rotation,
const NiPoint3& velocity,
const NiPoint3& angularVelocity,
bool onGround, bool dirtyVelocity, bool dirtyAngularVelocity
);
void Act(Entity* actor) override;
void Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) override;
void Deserialize(tinyxml2::XMLElement* element) override;
};
class SpeakRecord : public Record
{
public:
std::string text;
SpeakRecord() = default;
SpeakRecord(const std::string& text);
void Act(Entity* actor) override;
void Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) override;
void Deserialize(tinyxml2::XMLElement* element) override;
};
class AnimationRecord : public Record
{
public:
std::string animation;
AnimationRecord() = default;
AnimationRecord(const std::string& animation);
void Act(Entity* actor) override;
void Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) override;
void Deserialize(tinyxml2::XMLElement* element) override;
};
class EquipRecord : public Record
{
public:
LOT item;
EquipRecord() = default;
EquipRecord(LOT item);
void Act(Entity* actor) override;
void Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) override;
void Deserialize(tinyxml2::XMLElement* element) override;
};
class UnequipRecord : public Record
{
public:
LOT item;
UnequipRecord() = default;
UnequipRecord(LOT item);
void Act(Entity* actor) override;
void Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) override;
void Deserialize(tinyxml2::XMLElement* element) override;
};
class ClearEquippedRecord : public Record
{
public:
ClearEquippedRecord() = default;
void Act(Entity* actor) override;
void Serialize(tinyxml2::XMLDocument& document, tinyxml2::XMLElement* parent) override;
void Deserialize(tinyxml2::XMLElement* element) override;
};
class Recorder
{
public:
Recorder();
~Recorder();
void AddRecord(Record* record);
void Act(Entity* actor);
Entity* ActFor(Entity* actorTemplate, Entity* player);
void StopActingFor(Entity* actor, Entity* actorTemplate, LWOOBJID playerID);
bool IsRecording() const;
void SaveToFile(const std::string& filename);
float GetDuration() const;
static Recorder* LoadFromFile(const std::string& filename);
static void AddRecording(LWOOBJID actorID, Recorder* recorder);
static void StartRecording(LWOOBJID actorID);
static void StopRecording(LWOOBJID actorID);
static Recorder* GetRecorder(LWOOBJID actorID);
private:
std::vector<Record*> m_MovementRecords;
bool m_IsRecording;
std::chrono::milliseconds m_StartTime;
};
}

View File

@@ -0,0 +1,113 @@
#include "ServerPreconditions.hpp"
#include "tinyxml2.h"
using namespace ServerPreconditions;
std::unordered_map<LOT, std::vector<std::pair<bool, PreconditionExpression>>> ServerPreconditions::m_Preconditions;
std::unordered_map<LWOOBJID, LWOOBJID> ServerPreconditions::m_SoloActors;
std::unordered_map<LWOOBJID, std::unordered_set<LWOOBJID>> ServerPreconditions::m_ExcludeForPlayer;
void ServerPreconditions::LoadPreconditions(std::string file) {
tinyxml2::XMLDocument doc;
doc.LoadFile(file.c_str());
tinyxml2::XMLElement* root = doc.FirstChildElement("Preconditions");
if (!root) {
return;
}
for (tinyxml2::XMLElement* element = root->FirstChildElement("Entity"); element; element = element->NextSiblingElement("Entity")) {
LOT lot = element->UnsignedAttribute("lot");
std::vector<std::pair<bool, PreconditionExpression>> preconditions;
for (tinyxml2::XMLElement* precondition = element->FirstChildElement("Precondition"); precondition; precondition = precondition->NextSiblingElement("Precondition")) {
const auto condition = Preconditions::CreateExpression(precondition->GetText());
int64_t inverted;
if (precondition->QueryInt64Attribute("not", &inverted) == tinyxml2::XML_SUCCESS) {
preconditions.push_back(std::make_pair(inverted > 0, condition));
}
else {
preconditions.push_back(std::make_pair(false, condition));
}
}
m_Preconditions[lot] = preconditions;
}
}
bool ServerPreconditions::CheckPreconditions(Entity* target, Entity* entity) {
if (IsExcludedFor(entity->GetObjectID(), target->GetObjectID())) {
return false;
}
if (IsSoloActor(target->GetObjectID())) {
return IsActingFor(target->GetObjectID(), entity->GetObjectID());
}
if (m_Preconditions.find(target->GetLOT()) == m_Preconditions.end()) {
return true;
}
for (const auto& [inverse, precondition] : m_Preconditions[target->GetLOT()]) {
if (precondition.Check(entity) == inverse) {
return false;
}
}
return true;
}
bool ServerPreconditions::IsSoloActor(LWOOBJID actor) {
return m_SoloActors.find(actor) != m_SoloActors.end();
}
bool ServerPreconditions::IsActingFor(LWOOBJID actor, LWOOBJID target) {
return m_SoloActors.find(actor) != m_SoloActors.end() && m_SoloActors[actor] == target;
}
void ServerPreconditions::AddSoloActor(LWOOBJID actor, LWOOBJID target) {
m_SoloActors[actor] = target;
}
void ServerPreconditions::RemoveSoloActor(LWOOBJID actor) {
m_SoloActors.erase(actor);
}
void ServerPreconditions::AddExcludeFor(LWOOBJID player, LWOOBJID target) {
const auto& it = m_ExcludeForPlayer.find(player);
if (it == m_ExcludeForPlayer.end()) {
m_ExcludeForPlayer[player] = std::unordered_set<LWOOBJID>();
}
m_ExcludeForPlayer[player].insert(target);
}
void ServerPreconditions::RemoveExcludeFor(LWOOBJID player, LWOOBJID target) {
const auto& it = m_ExcludeForPlayer.find(player);
if (it == m_ExcludeForPlayer.end()) {
return;
}
m_ExcludeForPlayer[player].erase(target);
if (m_ExcludeForPlayer[player].empty()) {
m_ExcludeForPlayer.erase(player);
}
}
bool ServerPreconditions::IsExcludedFor(LWOOBJID player, LWOOBJID target) {
const auto& it = m_ExcludeForPlayer.find(player);
if (it == m_ExcludeForPlayer.end()) {
return false;
}
return it->second.find(target) != it->second.end();
}

View File

@@ -0,0 +1,38 @@
#pragma once
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "dCommonVars.h"
#include "Preconditions.h"
class Entity;
namespace ServerPreconditions
{
extern std::unordered_map<LOT, std::vector<std::pair<bool, PreconditionExpression>>> m_Preconditions;
extern std::unordered_map<LWOOBJID, LWOOBJID> m_SoloActors;
extern std::unordered_map<LWOOBJID, std::unordered_set<LWOOBJID>> m_ExcludeForPlayer;
void LoadPreconditions(std::string file);
bool CheckPreconditions(Entity* target, Entity* entity);
bool IsSoloActor(LWOOBJID actor);
bool IsActingFor(LWOOBJID actor, LWOOBJID target);
void AddSoloActor(LWOOBJID actor, LWOOBJID target);
void RemoveSoloActor(LWOOBJID actor);
void AddExcludeFor(LWOOBJID player, LWOOBJID target);
void RemoveExcludeFor(LWOOBJID player, LWOOBJID target);
bool IsExcludedFor(LWOOBJID player, LWOOBJID target);
}

View File

@@ -86,6 +86,9 @@
#include "CDObjectsTable.h"
#include "CDZoneTableTable.h"
#include "Recorder.h"
#include "ServerPreconditions.hpp"
void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entity* entity, const SystemAddress& sysAddr) {
auto commandCopy = command;
// Sanity check that a command was given
@@ -711,6 +714,33 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
return;
}
if (chatCommand == "removemission" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) {
if (args.size() == 0) return;
uint32_t missionID;
if (!GeneralUtils::TryParse(args[0], missionID)) {
ChatPackets::SendSystemMessage(sysAddr, u"Invalid mission id.");
return;
}
auto* comp = static_cast<MissionComponent*>(entity->GetComponent(eReplicaComponentType::MISSION));
if (comp == nullptr) {
return;
}
auto* mission = comp->GetMission(missionID);
if (mission == nullptr) {
return;
}
comp->RemoveMission(missionID);
return;
}
if (chatCommand == "playeffect" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() >= 3) {
int32_t effectID = 0;
@@ -883,6 +913,86 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
GameMessages::SendSetName(entity->GetObjectID(), GeneralUtils::UTF8ToUTF16(name), UNASSIGNED_SYSTEM_ADDRESS);
}
if (chatCommand == "record-act" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) {
EntityInfo info;
info.lot = 2097253;
info.pos = entity->GetPosition();
info.rot = entity->GetRotation();
info.scale = 1;
info.spawner = nullptr;
info.spawnerID = entity->GetObjectID();
info.spawnerNodeID = 0;
info.settings = {
new LDFData<std::vector<std::u16string>>(u"syncLDF", { u"custom_script_client" }),
new LDFData<std::u16string>(u"custom_script_client", u"scripts\\ai\\SPEC\\MISSION_MINIGAME_CLIENT.lua")
};
// If there is an argument, set the lot
if (args.size() > 0) {
if (!GeneralUtils::TryParse(args[0], info.lot)) {
ChatPackets::SendSystemMessage(sysAddr, u"Invalid lot.");
return;
}
}
// Spawn it
auto* actor = Game::entityManager->CreateEntity(info);
// If there is an argument, set the actors name
if (args.size() > 1) {
actor->SetVar(u"npcName", args[1]);
}
// Construct it
Game::entityManager->ConstructEntity(actor);
auto* record = Recording::Recorder::GetRecorder(entity->GetObjectID());
if (record) {
record->Act(actor);
} else {
Game::logger->Log("SlashCommandHandler", "Failed to get recorder for objectID: %llu", entity->GetObjectID());
}
}
if (chatCommand == "record-start" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) {
Recording::Recorder::StartRecording(entity->GetObjectID());
auto* record = Recording::Recorder::GetRecorder(entity->GetObjectID());
if (record) {
if (args.size() > 0 && args[0] == "clear") {
record->AddRecord(new Recording::ClearEquippedRecord());
}
} else {
Game::logger->Log("SlashCommandHandler", "Failed to get recorder for objectID: %llu", entity->GetObjectID());
}
}
if (chatCommand == "record-stop" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER) {
Recording::Recorder::StopRecording(entity->GetObjectID());
}
if (chatCommand == "record-save" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() == 1) {
auto* record = Recording::Recorder::GetRecorder(entity->GetObjectID());
if (record) {
record->SaveToFile(args[0]);
} else {
Game::logger->Log("SlashCommandHandler", "Failed to get recorder for objectID: %llu", entity->GetObjectID());
}
}
if (chatCommand == "record-load" && entity->GetGMLevel() >= eGameMasterLevel::DEVELOPER && args.size() == 1) {
auto* record = Recording::Recorder::LoadFromFile(args[0]);
if (record) {
Recording::Recorder::AddRecording(entity->GetObjectID(), record);
} else {
Game::logger->Log("SlashCommandHandler", "Failed to load recording from file: %s", args[0].c_str());
}
}
if ((chatCommand == "teleport" || chatCommand == "tele") && entity->GetGMLevel() >= eGameMasterLevel::JUNIOR_MODERATOR) {
NiPoint3 pos{};
if (args.size() == 3) {

View File

@@ -117,18 +117,23 @@ void VanityUtilities::SpawnVanity() {
if (!npc.m_Phrases.empty()){
npcEntity->SetVar<std::vector<std::string>>(u"chats", npc.m_Phrases);
auto* scriptComponent = npcEntity->GetComponent<ScriptComponent>();
if (scriptComponent && !npc.m_Script.empty()) {
scriptComponent->SetScript(npc.m_Script);
scriptComponent->SetSerialized(false);
for (const auto& npc : npc.m_Flags) {
npcEntity->SetVar<bool>(GeneralUtils::ASCIIToUTF16(npc.first), npc.second);
}
}
SetupNPCTalk(npcEntity);
}
auto* scriptComponent = npcEntity->GetComponent<ScriptComponent>();
Game::logger->Log("VanityUtilities", "Script: %s", npc.m_Script.c_str());
if (scriptComponent && !npc.m_Script.empty()) {
scriptComponent->SetScript(npc.m_Script);
scriptComponent->SetSerialized(false);
Game::logger->Log("VanityUtilities", "Setting script to %s", npc.m_Script.c_str());
for (const auto& npc : npc.m_Flags) {
npcEntity->SetVar<bool>(GeneralUtils::ASCIIToUTF16(npc.first), npc.second);
}
}
}
if (zoneID == 1200) {
@@ -162,6 +167,9 @@ Entity* VanityUtilities::SpawnNPC(LOT lot, const std::string& name, const NiPoin
entity->SetVar(u"npcName", name);
if (entity->GetVar<bool>(u"noGhosting")) entity->SetIsGhostingCandidate(false);
// Debug print
Game::logger->Log("VanityUtilities", "Spawning NPC %s (%i) at %f, %f, %f", name.c_str(), lot, position.x, position.y, position.z);
auto* inventoryComponent = entity->GetComponent<InventoryComponent>();
if (inventoryComponent && !inventory.empty()) {
@@ -176,6 +184,15 @@ Entity* VanityUtilities::SpawnNPC(LOT lot, const std::string& name, const NiPoin
destroyableComponent->SetHealth(0);
}
auto* scriptComponent = entity->GetComponent<ScriptComponent>();
if (scriptComponent == nullptr)
{
entity->AddComponent(eReplicaComponentType::SCRIPT, new ScriptComponent(entity, "", false));
}
Game::logger->Log("VanityUtilities", "NPC has script component? %s", (entity->GetComponent<ScriptComponent>() != nullptr) ? "true" : "false");
Game::entityManager->ConstructEntity(entity);
return entity;