2021-12-05 17:54:36 +00:00
|
|
|
#include "VanityUtilities.h"
|
|
|
|
|
|
|
|
#include "DestroyableComponent.h"
|
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "GameMessages.h"
|
|
|
|
#include "InventoryComponent.h"
|
|
|
|
#include "PhantomPhysicsComponent.h"
|
|
|
|
#include "ProximityMonitorComponent.h"
|
|
|
|
#include "ScriptComponent.h"
|
|
|
|
#include "dCommonVars.h"
|
|
|
|
#include "dConfig.h"
|
|
|
|
#include "dServer.h"
|
|
|
|
#include "tinyxml2.h"
|
2022-07-05 06:00:10 +00:00
|
|
|
#include "Game.h"
|
|
|
|
#include "dLogger.h"
|
2022-11-27 11:59:59 +00:00
|
|
|
#include "BinaryPathFinder.h"
|
2023-01-07 05:17:05 +00:00
|
|
|
#include "EntityInfo.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
std::vector<VanityNPC> VanityUtilities::m_NPCs = {};
|
|
|
|
std::vector<VanityParty> VanityUtilities::m_Parties = {};
|
|
|
|
std::vector<std::string> VanityUtilities::m_PartyPhrases = {};
|
|
|
|
|
|
|
|
void VanityUtilities::SpawnVanity() {
|
|
|
|
if (Game::config->GetValue("disable_vanity") == "1") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t zoneID = Game::server->GetZoneID();
|
|
|
|
|
2022-11-27 11:59:59 +00:00
|
|
|
ParseXML((BinaryPathFinder::GetBinaryDir() / "vanity/NPC.xml").string());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// Loop through all parties
|
|
|
|
for (const auto& party : m_Parties) {
|
|
|
|
const auto chance = party.m_Chance;
|
|
|
|
const auto zone = party.m_Zone;
|
|
|
|
|
|
|
|
if (zone != Game::server->GetZoneID()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
float rate = GeneralUtils::GenerateRandomNumber<float>(0, 1);
|
|
|
|
if (chance < rate) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy m_NPCs into a new vector
|
|
|
|
std::vector<VanityNPC> npcList = m_NPCs;
|
|
|
|
std::vector<uint32_t> taken = {};
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("VanityUtilities", "Spawning party with %i locations", party.m_Locations.size());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// Loop through all locations
|
|
|
|
for (const auto& location : party.m_Locations) {
|
|
|
|
rate = GeneralUtils::GenerateRandomNumber<float>(0, 1);
|
|
|
|
if (0.75f < rate) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a random NPC
|
|
|
|
auto npcIndex = GeneralUtils::GenerateRandomNumber<uint32_t>(0, npcList.size() - 1);
|
|
|
|
|
|
|
|
while (std::find(taken.begin(), taken.end(), npcIndex) != taken.end()) {
|
|
|
|
npcIndex = GeneralUtils::GenerateRandomNumber<uint32_t>(0, npcList.size() - 1);
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
auto& npc = npcList[npcIndex];
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
taken.push_back(npcIndex);
|
|
|
|
|
|
|
|
// Spawn the NPC
|
2023-01-11 07:10:14 +00:00
|
|
|
Game::logger->Log("VanityUtilities", "ldf size is %i", npc.ldf.size());
|
|
|
|
if (npc.ldf.empty()) {
|
|
|
|
npc.ldf = {
|
|
|
|
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")
|
|
|
|
};
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// Spawn the NPC
|
2023-01-11 07:10:14 +00:00
|
|
|
auto* npcEntity = SpawnNPC(npc.m_LOT, npc.m_Name, location.m_Position, location.m_Rotation, npc.m_Equipment, npc.ldf);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
npcEntity->SetVar<std::vector<std::string>>(u"chats", m_PartyPhrases);
|
|
|
|
|
|
|
|
SetupNPCTalk(npcEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all NPCs
|
2023-01-11 07:10:14 +00:00
|
|
|
for (auto& npc : m_NPCs) {
|
|
|
|
if (npc.m_Locations.find(Game::server->GetZoneID()) == npc.m_Locations.end())
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
const std::vector<VanityNPCLocation>& locations = npc.m_Locations.at(Game::server->GetZoneID());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// Pick a random location
|
|
|
|
const auto& location = locations[GeneralUtils::GenerateRandomNumber<int>(
|
|
|
|
static_cast<size_t>(0), static_cast<size_t>(locations.size() - 1))];
|
|
|
|
|
|
|
|
float rate = GeneralUtils::GenerateRandomNumber<float>(0, 1);
|
|
|
|
if (location.m_Chance < rate) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
if (npc.ldf.empty()) {
|
|
|
|
npc.ldf = {
|
|
|
|
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")
|
|
|
|
};
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// Spawn the NPC
|
2023-01-11 07:10:14 +00:00
|
|
|
auto* npcEntity = SpawnNPC(npc.m_LOT, npc.m_Name, location.m_Position, location.m_Rotation, npc.m_Equipment, npc.ldf);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
npcEntity->SetVar<std::vector<std::string>>(u"chats", npc.m_Phrases);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-06-07 07:23:50 +00:00
|
|
|
auto scriptComponent = npcEntity->GetComponent<ScriptComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (scriptComponent != nullptr) {
|
2023-01-11 07:10:14 +00:00
|
|
|
scriptComponent->SetScript(npc.m_Script);
|
2021-12-05 17:54:36 +00:00
|
|
|
scriptComponent->SetSerialized(false);
|
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
for (const auto& npc : npc.m_Flags) {
|
|
|
|
npcEntity->SetVar<bool>(GeneralUtils::ASCIIToUTF16(npc.first), npc.second);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
SetupNPCTalk(npcEntity);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (zoneID == 1200) {
|
|
|
|
{
|
|
|
|
EntityInfo info;
|
|
|
|
info.lot = 8139;
|
|
|
|
info.pos = { 259.5f, 246.4f, -705.2f };
|
|
|
|
info.rot = { 0.0f, 0.0f, 1.0f, 0.0f };
|
|
|
|
info.spawnerID = EntityManager::Instance()->GetZoneControlEntity()->GetObjectID();
|
|
|
|
|
|
|
|
info.settings = { new LDFData<bool>(u"hasCustomText", true),
|
2022-11-27 11:59:59 +00:00
|
|
|
new LDFData<std::string>(u"customText", ParseMarkdown((BinaryPathFinder::GetBinaryDir() / "vanity/TESTAMENT.md").string())) };
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
auto* entity = EntityManager::Instance()->CreateEntity(info);
|
|
|
|
|
|
|
|
EntityManager::Instance()->ConstructEntity(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Entity* VanityUtilities::SpawnNPC(LOT lot, const std::string& name, const NiPoint3& position,
|
|
|
|
const NiQuaternion& rotation, const std::vector<LOT>& inventory, const std::vector<LDFBaseData*>& ldf) {
|
|
|
|
EntityInfo info;
|
|
|
|
info.lot = lot;
|
|
|
|
info.pos = position;
|
|
|
|
info.rot = rotation;
|
|
|
|
info.spawnerID = EntityManager::Instance()->GetZoneControlEntity()->GetObjectID();
|
|
|
|
info.settings = ldf;
|
|
|
|
|
|
|
|
auto* entity = EntityManager::Instance()->CreateEntity(info);
|
|
|
|
entity->SetVar(u"npcName", name);
|
|
|
|
|
2023-06-07 07:23:50 +00:00
|
|
|
auto inventoryComponent = entity->GetComponent<InventoryComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (inventoryComponent != nullptr) {
|
|
|
|
inventoryComponent->SetNPCItems(inventory);
|
|
|
|
}
|
|
|
|
|
2023-06-07 07:23:50 +00:00
|
|
|
auto destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (destroyableComponent != nullptr) {
|
|
|
|
destroyableComponent->SetIsGMImmune(true);
|
|
|
|
destroyableComponent->SetMaxHealth(0);
|
|
|
|
destroyableComponent->SetHealth(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityManager::Instance()->ConstructEntity(entity);
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VanityUtilities::ParseXML(const std::string& file) {
|
|
|
|
// Read the entire file
|
|
|
|
std::ifstream xmlFile(file);
|
|
|
|
std::string xml((std::istreambuf_iterator<char>(xmlFile)), std::istreambuf_iterator<char>());
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Parse the XML
|
|
|
|
tinyxml2::XMLDocument doc;
|
|
|
|
doc.Parse(xml.c_str(), xml.size());
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Read the NPCs
|
|
|
|
auto* npcs = doc.FirstChildElement("npcs");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (npcs == nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse NPCs");
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
for (auto* party = npcs->FirstChildElement("party"); party != nullptr; party = party->NextSiblingElement("party")) {
|
|
|
|
// Get 'zone' as uint32_t and 'chance' as float
|
|
|
|
uint32_t zone = 0;
|
|
|
|
float chance = 0.0f;
|
|
|
|
|
|
|
|
if (party->Attribute("zone") != nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
zone = std::stoul(party->Attribute("zone"));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (party->Attribute("chance") != nullptr) {
|
|
|
|
chance = std::stof(party->Attribute("chance"));
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
VanityParty partyInfo;
|
|
|
|
partyInfo.m_Zone = zone;
|
2021-12-05 17:54:36 +00:00
|
|
|
partyInfo.m_Chance = chance;
|
|
|
|
|
|
|
|
auto* locations = party->FirstChildElement("locations");
|
|
|
|
|
|
|
|
if (locations == nullptr) {
|
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse party locations");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* location = locations->FirstChildElement("location"); location != nullptr;
|
|
|
|
location = location->NextSiblingElement("location")) {
|
|
|
|
// Get the location data
|
|
|
|
auto* x = location->Attribute("x");
|
|
|
|
auto* y = location->Attribute("y");
|
|
|
|
auto* z = location->Attribute("z");
|
|
|
|
auto* rw = location->Attribute("rw");
|
|
|
|
auto* rx = location->Attribute("rx");
|
|
|
|
auto* ry = location->Attribute("ry");
|
|
|
|
auto* rz = location->Attribute("rz");
|
|
|
|
|
|
|
|
if (x == nullptr || y == nullptr || z == nullptr || rw == nullptr || rx == nullptr || ry == nullptr
|
|
|
|
|| rz == nullptr) {
|
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse party location data");
|
|
|
|
continue;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
VanityNPCLocation locationData;
|
|
|
|
locationData.m_Position = { std::stof(x), std::stof(y), std::stof(z) };
|
2021-12-05 17:54:36 +00:00
|
|
|
locationData.m_Rotation = { std::stof(rw), std::stof(rx), std::stof(ry), std::stof(rz) };
|
|
|
|
locationData.m_Chance = 1.0f;
|
|
|
|
|
|
|
|
partyInfo.m_Locations.push_back(locationData);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
m_Parties.push_back(partyInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* partyPhrases = npcs->FirstChildElement("partyphrases");
|
|
|
|
|
|
|
|
if (partyPhrases == nullptr) {
|
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse party phrases");
|
2022-07-28 13:39:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
for (auto* phrase = partyPhrases->FirstChildElement("phrase"); phrase != nullptr;
|
2021-12-05 17:54:36 +00:00
|
|
|
phrase = phrase->NextSiblingElement("phrase")) {
|
|
|
|
// Get the phrase
|
|
|
|
auto* text = phrase->GetText();
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
if (text == nullptr) {
|
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse party phrase");
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
m_PartyPhrases.push_back(text);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
for (auto* npc = npcs->FirstChildElement("npc"); npc != nullptr; npc = npc->NextSiblingElement("npc")) {
|
|
|
|
// Get the NPC name
|
|
|
|
auto* name = npc->Attribute("name");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
if (!name) name = "";
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Get the NPC lot
|
|
|
|
auto* lot = npc->Attribute("lot");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (lot == nullptr) {
|
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse NPC lot");
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Get the equipment
|
|
|
|
auto* equipment = npc->FirstChildElement("equipment");
|
|
|
|
std::vector<LOT> inventory;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
if (equipment) {
|
|
|
|
auto* text = equipment->GetText();
|
|
|
|
|
|
|
|
if (text != nullptr) {
|
|
|
|
std::string equipmentString(text);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
std::vector<std::string> splitEquipment = GeneralUtils::SplitString(equipmentString, ',');
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
for (auto& item : splitEquipment) {
|
|
|
|
inventory.push_back(std::stoi(item));
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Get the phrases
|
|
|
|
auto* phrases = npc->FirstChildElement("phrases");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
std::vector<std::string> phraseList = {};
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
if (phrases) {
|
|
|
|
for (auto* phrase = phrases->FirstChildElement("phrase"); phrase != nullptr;
|
|
|
|
phrase = phrase->NextSiblingElement("phrase")) {
|
|
|
|
// Get the phrase
|
|
|
|
auto* text = phrase->GetText();
|
|
|
|
if (text == nullptr) {
|
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse NPC phrase");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
phraseList.push_back(text);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Get the script
|
|
|
|
auto* scriptElement = npc->FirstChildElement("script");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
std::string scriptName = "";
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (scriptElement != nullptr) {
|
|
|
|
auto* scriptNameAttribute = scriptElement->Attribute("name");
|
2023-01-11 07:10:14 +00:00
|
|
|
if (scriptNameAttribute) scriptName = scriptNameAttribute;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
auto* ldfElement = npc->FirstChildElement("ldf");
|
|
|
|
std::vector<std::u16string> keys = {};
|
|
|
|
|
|
|
|
std::vector<LDFBaseData*> ldf = {};
|
|
|
|
if(ldfElement) {
|
|
|
|
for (auto* entry = ldfElement->FirstChildElement("entry"); entry != nullptr;
|
|
|
|
entry = entry->NextSiblingElement("entry")) {
|
|
|
|
// Get the ldf data
|
|
|
|
auto* data = entry->Attribute("data");
|
|
|
|
if (!data) continue;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-01-11 07:10:14 +00:00
|
|
|
LDFBaseData* ldfData = LDFBaseData::DataFromString(data);
|
|
|
|
keys.push_back(ldfData->GetKey());
|
|
|
|
ldf.push_back(ldfData);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2023-01-11 07:10:14 +00:00
|
|
|
if (!keys.empty()) ldf.push_back(new LDFData<std::vector<std::u16string>>(u"syncLDF", keys));
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
VanityNPC npcData;
|
|
|
|
npcData.m_Name = name;
|
|
|
|
npcData.m_LOT = std::stoi(lot);
|
|
|
|
npcData.m_Equipment = inventory;
|
|
|
|
npcData.m_Phrases = phraseList;
|
|
|
|
npcData.m_Script = scriptName;
|
2023-01-11 07:10:14 +00:00
|
|
|
npcData.ldf = ldf;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Get flags
|
|
|
|
auto* flags = npc->FirstChildElement("flags");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (flags != nullptr) {
|
|
|
|
for (auto* flag = flags->FirstChildElement("flag"); flag != nullptr;
|
|
|
|
flag = flag->NextSiblingElement("flag")) {
|
|
|
|
// Get the flag name
|
|
|
|
auto* name = flag->Attribute("name");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (name == nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse NPC flag name");
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Get the flag value
|
|
|
|
auto* value = flag->Attribute("value");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (value == nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse NPC flag value");
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
npcData.m_Flags[name] = std::stoi(value);
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Get the zones
|
|
|
|
for (auto* zone = npc->FirstChildElement("zone"); zone != nullptr; zone = zone->NextSiblingElement("zone")) {
|
|
|
|
// Get the zone ID
|
|
|
|
auto* zoneID = zone->Attribute("id");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (zoneID == nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse NPC zone ID");
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
// Get the locations
|
|
|
|
auto* locations = zone->FirstChildElement("locations");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (locations == nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse NPC locations");
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
for (auto* location = locations->FirstChildElement("location"); location != nullptr;
|
|
|
|
location = location->NextSiblingElement("location")) {
|
|
|
|
// Get the location data
|
|
|
|
auto* x = location->Attribute("x");
|
|
|
|
auto* y = location->Attribute("y");
|
|
|
|
auto* z = location->Attribute("z");
|
|
|
|
auto* rw = location->Attribute("rw");
|
|
|
|
auto* rx = location->Attribute("rx");
|
|
|
|
auto* ry = location->Attribute("ry");
|
|
|
|
auto* rz = location->Attribute("rz");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (x == nullptr || y == nullptr || z == nullptr || rw == nullptr || rx == nullptr || ry == nullptr
|
|
|
|
|| rz == nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("VanityUtilities", "Failed to parse NPC location data");
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
VanityNPCLocation locationData;
|
|
|
|
locationData.m_Position = { std::stof(x), std::stof(y), std::stof(z) };
|
|
|
|
locationData.m_Rotation = { std::stof(rw), std::stof(rx), std::stof(ry), std::stof(rz) };
|
|
|
|
locationData.m_Chance = 1.0f;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (location->Attribute("chance") != nullptr) {
|
|
|
|
locationData.m_Chance = std::stof(location->Attribute("chance"));
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& it = npcData.m_Locations.find(std::stoi(zoneID));
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (it != npcData.m_Locations.end()) {
|
|
|
|
it->second.push_back(locationData);
|
|
|
|
} else {
|
|
|
|
std::vector<VanityNPCLocation> locations;
|
|
|
|
locations.push_back(locationData);
|
|
|
|
npcData.m_Locations.insert(std::make_pair(std::stoi(zoneID), locations));
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
m_NPCs.push_back(npcData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VanityNPC* VanityUtilities::GetNPC(const std::string& name) {
|
|
|
|
for (size_t i = 0; i < m_NPCs.size(); i++) {
|
|
|
|
if (m_NPCs[i].m_Name == name) {
|
|
|
|
return &m_NPCs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string VanityUtilities::ParseMarkdown(const std::string& file) {
|
|
|
|
// This function will read the file and return the content formatted as ASCII text.
|
|
|
|
|
|
|
|
// Read the file into a string
|
|
|
|
std::ifstream t(file);
|
|
|
|
|
|
|
|
// If the file does not exist, return an empty string.
|
|
|
|
if (!t.good()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream buffer;
|
|
|
|
buffer << t.rdbuf();
|
|
|
|
std::string fileContents = buffer.str();
|
|
|
|
|
|
|
|
// Loop through all lines in the file.
|
|
|
|
// Replace all instances of the markdown syntax with the corresponding HTML.
|
|
|
|
// Only care about headers
|
|
|
|
std::stringstream output;
|
|
|
|
std::string line;
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << fileContents;
|
|
|
|
while (std::getline(ss, line)) {
|
|
|
|
|
|
|
|
#define TOSTRING(x) #x
|
|
|
|
#define STRINGIFY(x) TOSTRING(x)
|
|
|
|
|
|
|
|
// Replace "__TIMESTAMP__" with the __TIMESTAMP__
|
|
|
|
GeneralUtils::ReplaceInString(line, "__TIMESTAMP__", __TIMESTAMP__);
|
|
|
|
// Replace "__VERSION__" wit'h the PROJECT_VERSION
|
|
|
|
GeneralUtils::ReplaceInString(line, "__VERSION__", STRINGIFY(PROJECT_VERSION));
|
|
|
|
// Replace "__SOURCE__" with SOURCE
|
|
|
|
GeneralUtils::ReplaceInString(line, "__SOURCE__", Game::config->GetValue("source"));
|
|
|
|
// Replace "__LICENSE__" with LICENSE
|
|
|
|
GeneralUtils::ReplaceInString(line, "__LICENSE__", STRINGIFY(LICENSE));
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (line.find("##") != std::string::npos) {
|
|
|
|
// Add "<font size='18' color='#000000'>" before the header
|
|
|
|
output << "<font size=\"14\" color=\"#000000\">";
|
|
|
|
// Add the header without the markdown syntax
|
|
|
|
output << line.substr(3);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
output << "</font>";
|
|
|
|
} else if (line.find("#") != std::string::npos) {
|
|
|
|
// Add "<font size='18' color='#000000'>" before the header
|
|
|
|
output << "<font size=\"18\" color=\"#000000\">";
|
|
|
|
// Add the header without the markdown syntax
|
|
|
|
output << line.substr(2);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
output << "</font>";
|
|
|
|
} else {
|
|
|
|
output << line;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
output << "\n";
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
return output.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VanityUtilities::SetupNPCTalk(Entity* npc) {
|
|
|
|
npc->AddCallbackTimer(15.0f, [npc]() { NPCTalk(npc); });
|
|
|
|
|
|
|
|
npc->SetProximityRadius(20.0f, "talk");
|
|
|
|
}
|
|
|
|
|
|
|
|
void VanityUtilities::NPCTalk(Entity* npc) {
|
2023-06-07 07:23:50 +00:00
|
|
|
auto proximityMonitorComponent = npc->GetComponent<ProximityMonitorComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (!proximityMonitorComponent->GetProximityObjects("talk").empty()) {
|
|
|
|
const auto& chats = npc->GetVar<std::vector<std::string>>(u"chats");
|
|
|
|
|
|
|
|
if (chats.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& selected
|
|
|
|
= chats[GeneralUtils::GenerateRandomNumber<int32_t>(0, static_cast<int32_t>(chats.size() - 1))];
|
|
|
|
|
|
|
|
GameMessages::SendNotifyClientZoneObject(
|
|
|
|
npc->GetObjectID(), u"sendToclient_bubble", 0, 0, npc->GetObjectID(), selected, UNASSIGNED_SYSTEM_ADDRESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityManager::Instance()->SerializeEntity(npc);
|
|
|
|
|
|
|
|
const float nextTime = GeneralUtils::GenerateRandomNumber<float>(15, 60);
|
|
|
|
|
|
|
|
npc->AddCallbackTimer(nextTime, [npc]() { NPCTalk(npc); });
|
|
|
|
}
|