From a39a825045a1a5090bb5fca9c8a954f942aba031 Mon Sep 17 00:00:00 2001 From: Kinglykrab <89047260+Kinglykrab@users.noreply.github.com> Date: Sat, 2 Apr 2022 17:51:26 -0400 Subject: [PATCH] [Quest API] Add GetBotListByCharacterID() to Perl/Lua. (#2069) --- zone/entity.cpp | 17 +++++++++++++++++ zone/entity.h | 1 + zone/lua_entity_list.cpp | 29 +++++++++++++++++++++++++---- zone/lua_entity_list.h | 1 + zone/perl_entity.cpp | 33 ++++++++++++++++++++++++++++++++- 5 files changed, 76 insertions(+), 5 deletions(-) diff --git a/zone/entity.cpp b/zone/entity.cpp index 20c58ca45..9ea28c107 100644 --- a/zone/entity.cpp +++ b/zone/entity.cpp @@ -5062,6 +5062,23 @@ void EntityList::GetBotList(std::list &b_list) } } +std::vector EntityList::GetBotListByCharacterID(uint32 character_id) +{ + std::vector client_bot_list; + + if (!character_id) { + return client_bot_list; + } + + for (auto bot : bot_list) { + if (bot->GetOwner() && bot->GetBotOwnerCharacterID() == character_id) { + client_bot_list.push_back(bot); + } + } + + return client_bot_list; +} + std::vector EntityList::GetBotListByClientName(std::string client_name) { std::vector client_bot_list; diff --git a/zone/entity.h b/zone/entity.h index 767a87e2e..b8fe43a22 100644 --- a/zone/entity.h +++ b/zone/entity.h @@ -526,6 +526,7 @@ public: inline const std::unordered_map &GetClientList() { return client_list; } #ifdef BOTS inline const std::list &GetBotList() { return bot_list; } + std::vector GetBotListByCharacterID(uint32 character_id); std::vector GetBotListByClientName(std::string client_name); #endif inline const std::unordered_map &GetCorpseList() { return corpse_list; } diff --git a/zone/lua_entity_list.cpp b/zone/lua_entity_list.cpp index 2b3a2f903..fac099959 100644 --- a/zone/lua_entity_list.cpp +++ b/zone/lua_entity_list.cpp @@ -375,8 +375,25 @@ Lua_Bot_List Lua_EntityList::GetBotList() { Lua_Safe_Call_Class(Lua_Bot_List); Lua_Bot_List ret; auto &bot_list = self->GetBotList(); - for (auto bot : bot_list) { - ret.entries.push_back(Lua_Bot(bot)); + + if (bot_list.size()) { + for (auto bot : bot_list) { + ret.entries.push_back(Lua_Bot(bot)); + } + } + + return ret; +} + +Lua_Bot_List Lua_EntityList::GetBotListByCharacterID(uint32 character_id) { + Lua_Safe_Call_Class(Lua_Bot_List); + Lua_Bot_List ret; + auto bot_list = self->GetBotListByCharacterID(character_id); + + if (bot_list.size()) { + for (auto bot : bot_list) { + ret.entries.push_back(Lua_Bot(bot)); + } } return ret; @@ -386,8 +403,11 @@ Lua_Bot_List Lua_EntityList::GetBotListByClientName(std::string client_name) { Lua_Safe_Call_Class(Lua_Bot_List); Lua_Bot_List ret; auto bot_list = self->GetBotListByClientName(client_name); - for (auto bot : bot_list) { - ret.entries.push_back(Lua_Bot(bot)); + + if (bot_list.size()) { + for (auto bot : bot_list) { + ret.entries.push_back(Lua_Bot(bot)); + } } return ret; @@ -530,6 +550,7 @@ luabind::scope lua_register_entity_list() { .def("GetBotByID", (Lua_Bot(Lua_EntityList::*)(uint32))&Lua_EntityList::GetBotByID) .def("GetBotByName", (Lua_Bot(Lua_EntityList::*)(std::string))&Lua_EntityList::GetBotByName) .def("GetBotList", (Lua_Bot_List(Lua_EntityList::*)(void))&Lua_EntityList::GetBotList) + .def("GetBotListByCharacterID", (Lua_Bot_List(Lua_EntityList::*)(uint32))&Lua_EntityList::GetBotListByCharacterID) .def("GetBotListByClientName", (Lua_Bot_List(Lua_EntityList::*)(std::string))&Lua_EntityList::GetBotListByClientName) #endif .def("GetClientByAccID", (Lua_Client(Lua_EntityList::*)(uint32))&Lua_EntityList::GetClientByAccID) diff --git a/zone/lua_entity_list.h b/zone/lua_entity_list.h index a2cdf282d..09c525db7 100644 --- a/zone/lua_entity_list.h +++ b/zone/lua_entity_list.h @@ -128,6 +128,7 @@ public: Lua_Bot GetBotByID(uint32 bot_id); Lua_Bot GetBotByName(std::string bot_name); Lua_Bot_List GetBotList(); + Lua_Bot_List GetBotListByCharacterID(uint32 character_id); Lua_Bot_List GetBotListByClientName(std::string client_name); #endif }; diff --git a/zone/perl_entity.cpp b/zone/perl_entity.cpp index 254a2feeb..79694a98c 100644 --- a/zone/perl_entity.cpp +++ b/zone/perl_entity.cpp @@ -1347,6 +1347,35 @@ XS(XS_EntityList_GetBotList) { XSRETURN(bot_count); } +XS(XS_EntityList_GetBotListByCharacterID); +XS(XS_EntityList_GetBotListByCharacterID) { + dXSARGS; + if (items != 2) { + Perl_croak(aTHX_ "Usage: EntityList::GetBotListByCharacterID(THIS, uint32 character_id)"); // @categories Script Utility, Bot + } + + EntityList *THIS; + uint32 character_id = (uint32) SvUV(ST(1)); + VALIDATE_THIS_IS_ENTITY; + + auto current_bot_list = THIS->GetBotListByCharacterID(character_id); + auto bot_count = current_bot_list.size(); + + if (bot_count) { + EXTEND(sp, bot_count); + for (int index = 0; index < bot_count; ++index) { + ST(index) = sv_newmortal(); + sv_setref_pv(ST(index), "Bot", (void *) current_bot_list[index]); + XPUSHs(ST(index)); + } + XSRETURN(bot_count); + } + + SV* return_value = &PL_sv_undef; + ST(0) = return_value; + XSRETURN(1); +} + XS(XS_EntityList_GetBotListByClientName); XS(XS_EntityList_GetBotListByClientName) { dXSARGS; @@ -1361,7 +1390,7 @@ XS(XS_EntityList_GetBotListByClientName) { auto current_bot_list = THIS->GetBotListByClientName(client_name); auto bot_count = current_bot_list.size(); - if (bot_count > 0) { + if (bot_count) { EXTEND(sp, bot_count); for (int index = 0; index < bot_count; ++index) { ST(index) = sv_newmortal(); @@ -1370,6 +1399,7 @@ XS(XS_EntityList_GetBotListByClientName) { } XSRETURN(bot_count); } + SV* return_value = &PL_sv_undef; ST(0) = return_value; XSRETURN(1); @@ -1576,6 +1606,7 @@ XS(boot_EntityList) { newXSproto(strcpy(buf, "GetBotByID"), XS_EntityList_GetBotByID, file, "$$"); newXSproto(strcpy(buf, "GetBotByName"), XS_EntityList_GetBotByName, file, "$$"); newXSproto(strcpy(buf, "GetBotList"), XS_EntityList_GetBotList, file, "$"); + newXSproto(strcpy(buf, "GetBotListByCharacterID"), XS_EntityList_GetBotListByCharacterID, file, "$$"); newXSproto(strcpy(buf, "GetBotListByClientName"), XS_EntityList_GetBotListByClientName, file, "$$"); #endif newXSproto(strcpy(buf, "GetClientByAccID"), XS_EntityList_GetClientByAccID, file, "$$");