mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-05-07 01:23:52 +00:00
Editor's orientation is measured from +renderX (which maps to west in WoW canonical), but AzerothCore creature.orientation is the WoW yaw measured from +X (north). Without conversion an editor 0deg NPC ended up facing west in-game, off by 90deg even after the radians fix. Now applies `wowYaw = π/2 - editorYaw` and normalizes to [0, 2π).
256 lines
11 KiB
C++
256 lines
11 KiB
C++
#include "sql_exporter.hpp"
|
|
#include "core/logger.hpp"
|
|
#include <fstream>
|
|
#include <filesystem>
|
|
#include <ctime>
|
|
#include <chrono>
|
|
|
|
namespace wowee {
|
|
namespace editor {
|
|
|
|
static std::string escapeSql(const std::string& s) {
|
|
std::string out;
|
|
for (char c : s) {
|
|
if (c == '\'') out += "''";
|
|
else if (c == '\\') out += "\\\\";
|
|
else out += c;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
bool SQLExporter::exportCreatures(const std::vector<CreatureSpawn>& spawns,
|
|
const std::string& path,
|
|
uint32_t mapId,
|
|
uint32_t startEntry) {
|
|
namespace fs = std::filesystem;
|
|
fs::create_directories(fs::path(path).parent_path());
|
|
|
|
std::ofstream f(path);
|
|
if (!f) return false;
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
auto time = std::chrono::system_clock::to_time_t(now);
|
|
char timeBuf[32];
|
|
std::strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", std::localtime(&time));
|
|
|
|
f << "-- Wowee World Editor — Creature Spawn Export\n";
|
|
f << "-- Generated: " << timeBuf << "\n";
|
|
f << "-- Target: AzerothCore / TrinityCore 3.3.5a\n";
|
|
f << "-- Map ID: " << mapId << "\n";
|
|
f << "-- Creatures: " << spawns.size() << "\n\n";
|
|
|
|
// creature_template entries
|
|
f << "-- =============================================\n";
|
|
f << "-- creature_template (NPC definitions)\n";
|
|
f << "-- =============================================\n\n";
|
|
|
|
for (size_t i = 0; i < spawns.size(); i++) {
|
|
const auto& s = spawns[i];
|
|
uint32_t entry = startEntry + static_cast<uint32_t>(i);
|
|
|
|
uint32_t npcFlags = 0;
|
|
if (s.questgiver) npcFlags |= 0x02;
|
|
if (s.vendor) npcFlags |= 0x80;
|
|
if (s.flightmaster) npcFlags |= 0x02000000;
|
|
if (s.innkeeper) npcFlags |= 0x10000;
|
|
|
|
uint32_t unitFlags = 0;
|
|
if (!s.hostile) unitFlags |= 0x02; // NON_ATTACKABLE
|
|
|
|
f << "INSERT INTO `creature_template` "
|
|
<< "(`entry`, `name`, `minlevel`, `maxlevel`, `minhealth`, `maxhealth`, "
|
|
<< "`mana`, `armor`, `mindmg`, `maxdmg`, `faction`, `npcflag`, "
|
|
<< "`unit_flags`, `modelid1`, `scale`) VALUES ("
|
|
<< entry << ", "
|
|
<< "'" << escapeSql(s.name) << "', "
|
|
<< s.level << ", " << s.level << ", "
|
|
<< s.health << ", " << s.health << ", "
|
|
<< s.mana << ", " << s.armor << ", "
|
|
<< s.minDamage << ", " << s.maxDamage << ", "
|
|
<< s.faction << ", " << npcFlags << ", "
|
|
<< unitFlags << ", "
|
|
<< s.displayId << ", "
|
|
<< s.scale
|
|
<< ") ON DUPLICATE KEY UPDATE `name`='" << escapeSql(s.name) << "';\n";
|
|
}
|
|
|
|
// creature spawn entries
|
|
f << "\n-- =============================================\n";
|
|
f << "-- creature (spawn positions)\n";
|
|
f << "-- =============================================\n\n";
|
|
|
|
for (size_t i = 0; i < spawns.size(); i++) {
|
|
const auto& s = spawns[i];
|
|
uint32_t entry = startEntry + static_cast<uint32_t>(i);
|
|
uint32_t guid = startEntry + static_cast<uint32_t>(i);
|
|
|
|
uint8_t movementType = 0;
|
|
if (s.behavior == CreatureBehavior::Wander) movementType = 1;
|
|
if (s.behavior == CreatureBehavior::Patrol) movementType = 2;
|
|
|
|
// Editor stores positions in render coords (renderX=wowY, renderY=wowX,
|
|
// renderZ=wowZ) but AzerothCore creature.position_x/y are WoW canonical
|
|
// (X=north-south, Y=west-east). Swap x/y on the way out.
|
|
const float wowX = s.position.y;
|
|
const float wowY = s.position.x;
|
|
const float wowZ = s.position.z;
|
|
// AzerothCore expects orientation in radians from +X (north). Editor's
|
|
// orientation is in degrees from +renderX (west). Convert via:
|
|
// wowYaw = π/2 - editorYaw
|
|
constexpr float kPi = 3.14159265358979323846f;
|
|
const float editorYawRad = s.orientation * kPi / 180.0f;
|
|
float orientRad = kPi * 0.5f - editorYawRad;
|
|
while (orientRad < 0.0f) orientRad += 2.0f * kPi;
|
|
while (orientRad >= 2.0f * kPi) orientRad -= 2.0f * kPi;
|
|
f << "INSERT INTO `creature` "
|
|
<< "(`guid`, `id`, `map`, `position_x`, `position_y`, `position_z`, "
|
|
<< "`orientation`, `spawntimesecs`, `wander_distance`, `MovementType`) VALUES ("
|
|
<< guid << ", " << entry << ", " << mapId << ", "
|
|
<< wowX << ", " << wowY << ", " << wowZ << ", "
|
|
<< orientRad << ", "
|
|
<< (s.respawnTimeMs / 1000) << ", "
|
|
<< s.wanderRadius << ", "
|
|
<< static_cast<int>(movementType)
|
|
<< ") ON DUPLICATE KEY UPDATE `position_x`=" << wowX << ";\n";
|
|
}
|
|
|
|
// Patrol waypoints
|
|
bool hasPatrols = false;
|
|
for (const auto& s : spawns) {
|
|
if (!s.patrolPath.empty()) { hasPatrols = true; break; }
|
|
}
|
|
if (hasPatrols) {
|
|
f << "\n-- =============================================\n";
|
|
f << "-- creature_addon + waypoint_data (patrol paths)\n";
|
|
f << "-- =============================================\n\n";
|
|
|
|
for (size_t i = 0; i < spawns.size(); i++) {
|
|
const auto& s = spawns[i];
|
|
if (s.patrolPath.empty()) continue;
|
|
uint32_t guid = startEntry + static_cast<uint32_t>(i);
|
|
uint32_t pathId = guid;
|
|
|
|
f << "INSERT INTO `creature_addon` (`guid`, `path_id`) VALUES ("
|
|
<< guid << ", " << pathId
|
|
<< ") ON DUPLICATE KEY UPDATE `path_id`=" << pathId << ";\n";
|
|
|
|
for (size_t pi = 0; pi < s.patrolPath.size(); pi++) {
|
|
const auto& wp = s.patrolPath[pi];
|
|
// Same render -> WoW canonical swap as the spawn position.
|
|
const float wpWowX = wp.position.y;
|
|
const float wpWowY = wp.position.x;
|
|
const float wpWowZ = wp.position.z;
|
|
f << "INSERT INTO `waypoint_data` "
|
|
<< "(`id`, `point`, `position_x`, `position_y`, `position_z`, `delay`) VALUES ("
|
|
<< pathId << ", " << (pi + 1) << ", "
|
|
<< wpWowX << ", " << wpWowY << ", " << wpWowZ << ", "
|
|
<< wp.waitTimeMs
|
|
<< ") ON DUPLICATE KEY UPDATE `position_x`=" << wpWowX << ";\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
LOG_INFO("SQL exported: ", path, " (", spawns.size(), " creatures)");
|
|
return true;
|
|
}
|
|
|
|
bool SQLExporter::exportQuests(const std::vector<Quest>& quests,
|
|
const std::string& path,
|
|
uint32_t startEntry) {
|
|
namespace fs = std::filesystem;
|
|
fs::create_directories(fs::path(path).parent_path());
|
|
|
|
std::ofstream f(path, std::ios::app);
|
|
if (!f) return false;
|
|
|
|
if (quests.empty()) return true;
|
|
|
|
f << "\n-- =============================================\n";
|
|
f << "-- quest_template (quest definitions)\n";
|
|
f << "-- =============================================\n\n";
|
|
|
|
for (const auto& q : quests) {
|
|
uint32_t entry = startEntry + q.id;
|
|
uint32_t rewardMoney = q.reward.gold * 10000 + q.reward.silver * 100 + q.reward.copper;
|
|
|
|
// Map up to 4 KillCreature objectives to RequiredNpcOrGo slots
|
|
// and report objective counts in matching slots. Item objectives go
|
|
// to RequiredItemId/Count. Editor stores the M2 path in targetName,
|
|
// so creature objectives can only be linked when the user has filled
|
|
// a numeric ID into targetName (rare); leave 0 otherwise.
|
|
uint32_t reqNpcOrGo[4] = {0, 0, 0, 0};
|
|
uint32_t reqNpcOrGoCount[4] = {0, 0, 0, 0};
|
|
uint32_t reqItemId[6] = {0, 0, 0, 0, 0, 0};
|
|
uint32_t reqItemCount[6] = {0, 0, 0, 0, 0, 0};
|
|
size_t npcSlot = 0, itemSlot = 0;
|
|
for (const auto& obj : q.objectives) {
|
|
uint32_t id = 0;
|
|
try { id = static_cast<uint32_t>(std::stoul(obj.targetName)); } catch (...) {}
|
|
if (obj.type == QuestObjectiveType::KillCreature && npcSlot < 4) {
|
|
reqNpcOrGo[npcSlot] = id;
|
|
reqNpcOrGoCount[npcSlot] = obj.targetCount;
|
|
npcSlot++;
|
|
} else if (obj.type == QuestObjectiveType::CollectItem && itemSlot < 6) {
|
|
reqItemId[itemSlot] = id;
|
|
reqItemCount[itemSlot] = obj.targetCount;
|
|
itemSlot++;
|
|
}
|
|
}
|
|
|
|
f << "INSERT INTO `quest_template` "
|
|
<< "(`ID`, `LogTitle`, `LogDescription`, `QuestCompletionLog`, "
|
|
<< "`MinLevel`, `RewardXP`, `RewardMoney`, "
|
|
<< "`RequiredNpcOrGo1`, `RequiredNpcOrGoCount1`, "
|
|
<< "`RequiredNpcOrGo2`, `RequiredNpcOrGoCount2`, "
|
|
<< "`RequiredNpcOrGo3`, `RequiredNpcOrGoCount3`, "
|
|
<< "`RequiredNpcOrGo4`, `RequiredNpcOrGoCount4`, "
|
|
<< "`RequiredItemId1`, `RequiredItemCount1`, "
|
|
<< "`RequiredItemId2`, `RequiredItemCount2`, "
|
|
<< "`RequiredItemId3`, `RequiredItemCount3`, "
|
|
<< "`RequiredItemId4`, `RequiredItemCount4`, "
|
|
<< "`RequiredItemId5`, `RequiredItemCount5`, "
|
|
<< "`RequiredItemId6`, `RequiredItemCount6`) VALUES ("
|
|
<< entry << ", "
|
|
<< "'" << escapeSql(q.title) << "', "
|
|
<< "'" << escapeSql(q.description) << "', "
|
|
<< "'" << escapeSql(q.completionText) << "', "
|
|
<< q.requiredLevel << ", "
|
|
<< q.reward.xp << ", " << rewardMoney;
|
|
for (int k = 0; k < 4; k++) f << ", " << reqNpcOrGo[k] << ", " << reqNpcOrGoCount[k];
|
|
for (int k = 0; k < 6; k++) f << ", " << reqItemId[k] << ", " << reqItemCount[k];
|
|
f << ") ON DUPLICATE KEY UPDATE `LogTitle`='" << escapeSql(q.title) << "';\n";
|
|
}
|
|
|
|
// creature_queststarter / creature_questender link quests to NPCs.
|
|
// Without these the player can pick up the quest from no one.
|
|
f << "\n-- =============================================\n";
|
|
f << "-- creature_queststarter / _questender (quest links)\n";
|
|
f << "-- =============================================\n\n";
|
|
for (const auto& q : quests) {
|
|
uint32_t entry = startEntry + q.id;
|
|
if (q.questGiverNpcId > 0) {
|
|
f << "INSERT IGNORE INTO `creature_queststarter` (`id`, `quest`) VALUES ("
|
|
<< q.questGiverNpcId << ", " << entry << ");\n";
|
|
}
|
|
if (q.turnInNpcId > 0) {
|
|
f << "INSERT IGNORE INTO `creature_questender` (`id`, `quest`) VALUES ("
|
|
<< q.turnInNpcId << ", " << entry << ");\n";
|
|
}
|
|
}
|
|
|
|
LOG_INFO("SQL quests exported: ", quests.size(), " quests");
|
|
return true;
|
|
}
|
|
|
|
bool SQLExporter::exportAll(const std::vector<CreatureSpawn>& spawns,
|
|
const std::vector<Quest>& quests,
|
|
const std::string& path,
|
|
uint32_t mapId,
|
|
uint32_t startEntry) {
|
|
if (!exportCreatures(spawns, path, mapId, startEntry)) return false;
|
|
if (!quests.empty()) exportQuests(quests, path, startEntry);
|
|
return true;
|
|
}
|
|
|
|
} // namespace editor
|
|
} // namespace wowee
|