From 02cac686b63ec407193c726181418f0b185e6945 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 6 Apr 2020 00:09:57 -0400 Subject: [PATCH] Add getnpcnamebyid(npc_id) to Perl/Lua. --- common/database.cpp | 17 +++++++++++++++++ common/database.h | 1 + zone/embparser_api.cpp | 17 +++++++++++++++++ zone/lua_general.cpp | 5 +++++ zone/questmgr.cpp | 7 +++++++ zone/questmgr.h | 1 + 6 files changed, 48 insertions(+) diff --git a/common/database.cpp b/common/database.cpp index ab9b1ce84..1935406a2 100644 --- a/common/database.cpp +++ b/common/database.cpp @@ -941,6 +941,22 @@ const char* Database::GetCharNameByID(uint32 char_id) { return row[0]; } +const char* Database::GetNPCNameByID(uint32 npc_id) { + std::string query = fmt::format("SELECT `name` FROM `npc_types` WHERE id = {}", npc_id); + auto results = QueryDatabase(query); + + if (!results.Success()) { + return ""; + } + + if (results.RowCount() == 0) { + return ""; + } + + auto row = results.begin(); + return row[0]; +} + bool Database::LoadVariables() { auto results = QueryDatabase(StringFormat("SELECT varname, value, unix_timestamp() FROM variables where unix_timestamp(ts) >= %d", varcache.last_update)); @@ -2389,3 +2405,4 @@ int Database::GetInstanceID(uint32 char_id, uint32 zone_id) { return 0; } + diff --git a/common/database.h b/common/database.h index 44c264dfd..cf85acf9c 100644 --- a/common/database.h +++ b/common/database.h @@ -139,6 +139,7 @@ public: void GetAccountName(uint32 accountid, char* name, uint32* oLSAccountID = 0); void GetCharName(uint32 char_id, char* name); const char *GetCharNameByID(uint32 char_id); + const char *GetNPCNameByID(uint32 npc_id); void LoginIP(uint32 AccountID, const char* LoginIP); /* Instancing */ diff --git a/zone/embparser_api.cpp b/zone/embparser_api.cpp index dfb23ddc8..d926ffcbb 100644 --- a/zone/embparser_api.cpp +++ b/zone/embparser_api.cpp @@ -2858,6 +2858,22 @@ XS(XS__getitemname) { XSRETURN(1); } +XS(XS__getnpcnamebyid); +XS(XS__getnpcnamebyid) { + dXSARGS; + if (items != 1) + Perl_croak(aTHX_ "Usage: quest::getnpcnamebyid(uint32 npc_id)"); + + dXSTARG; + uint32 npc_id = (int) SvIV(ST(0)); + const char *npc_name = quest_manager.getnpcnamebyid(npc_id); + + sv_setpv(TARG, npc_name); + XSprePUSH; + PUSHTARG; + XSRETURN(1); +} + XS(XS__UpdateSpawnTimer); XS(XS__UpdateSpawnTimer) { dXSARGS; @@ -4110,6 +4126,7 @@ EXTERN_C XS(boot_quest) { newXS(strcpy(buf, "getinventoryslotid"), XS__getinventoryslotid, file); newXS(strcpy(buf, "getitemname"), XS__getitemname, file); newXS(strcpy(buf, "getItemName"), XS_qc_getItemName, file); + newXS(strcpy(buf, "getnpcnamebyid"), XS__getnpcnamebyid, file); newXS(strcpy(buf, "get_spawn_condition"), XS__get_spawn_condition, file); newXS(strcpy(buf, "getcharnamebyid"), XS__getcharnamebyid, file); newXS(strcpy(buf, "getguildnamebyid"), XS__getguildnamebyid, file); diff --git a/zone/lua_general.cpp b/zone/lua_general.cpp index 848348fbf..07b200315 100644 --- a/zone/lua_general.cpp +++ b/zone/lua_general.cpp @@ -890,6 +890,10 @@ int lua_get_group_id_by_char_id(uint32 char_id) { return database.GetGroupIDByCharID(char_id); } +const char *lua_get_npc_name_by_id(uint32 npc_id) { + return quest_manager.getnpcnamebyid(npc_id); +} + int lua_get_raid_id_by_char_id(uint32 char_id) { return database.GetRaidIDByCharID(char_id); } @@ -1776,6 +1780,7 @@ luabind::scope lua_register_general() { luabind::def("get_guild_name_by_id", &lua_get_guild_name_by_id), luabind::def("get_guild_id_by_char_id", &lua_get_guild_id_by_char_id), luabind::def("get_group_id_by_char_id", &lua_get_group_id_by_char_id), + luabind::def("get_npc_name_by_id", &lua_get_npc_name_by_id), luabind::def("get_raid_id_by_char_id", &lua_get_raid_id_by_char_id), luabind::def("create_instance", &lua_create_instance), luabind::def("destroy_instance", &lua_destroy_instance), diff --git a/zone/questmgr.cpp b/zone/questmgr.cpp index 0a61361c6..e2006222c 100644 --- a/zone/questmgr.cpp +++ b/zone/questmgr.cpp @@ -2725,6 +2725,13 @@ std::string QuestManager::getitemname(uint32 item_id) { return item_name; } +const char *QuestManager::getnpcnamebyid(uint32 npc_id) { + if (npc_id > 0) { + return database.GetNPCNameByID(npc_id); + } + return ""; +} + uint16 QuestManager::CreateInstance(const char *zone, int16 version, uint32 duration) { QuestManagerCurrentQuestVars(); diff --git a/zone/questmgr.h b/zone/questmgr.h index a3c7eab3d..934dd148a 100644 --- a/zone/questmgr.h +++ b/zone/questmgr.h @@ -260,6 +260,7 @@ public: const char* getguildnamebyid(int guild_id); int getguildidbycharid(uint32 char_id); int getgroupidbycharid(uint32 char_id); + const char* getnpcnamebyid(uint32 npc_id); int getraididbycharid(uint32 char_id); void SetRunning(bool val); bool IsRunning();