[Quest API] Add GetBotListByCharacterID() to Perl/Lua. (#2069)

This commit is contained in:
Kinglykrab 2022-04-02 17:51:26 -04:00 committed by GitHub
parent ccd0713b33
commit a39a825045
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 5 deletions

View File

@ -5062,6 +5062,23 @@ void EntityList::GetBotList(std::list<Bot *> &b_list)
}
}
std::vector<Bot *> EntityList::GetBotListByCharacterID(uint32 character_id)
{
std::vector<Bot *> 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<Bot *> EntityList::GetBotListByClientName(std::string client_name)
{
std::vector<Bot *> client_bot_list;

View File

@ -526,6 +526,7 @@ public:
inline const std::unordered_map<uint16, Client *> &GetClientList() { return client_list; }
#ifdef BOTS
inline const std::list<Bot *> &GetBotList() { return bot_list; }
std::vector<Bot *> GetBotListByCharacterID(uint32 character_id);
std::vector<Bot *> GetBotListByClientName(std::string client_name);
#endif
inline const std::unordered_map<uint16, Corpse *> &GetCorpseList() { return corpse_list; }

View File

@ -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)

View File

@ -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
};

View File

@ -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, "$$");