[Bot] Add GetBotOwnerByBotID Method (#2715)

* [Bot] Add GetBotOwnerByBotID Method

* Cleanup.

* Remove EVENT_DESPAWN exports in Lua.

Co-authored-by: Kinglykrab <kinglykrab@gmail.com>
This commit is contained in:
Aeadoin 2023-01-10 21:14:42 -05:00 committed by GitHub
parent 0d23ffe5e5
commit 8fe02b5ed1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 107 additions and 10 deletions

View File

@ -8752,18 +8752,32 @@ Bot* EntityList::GetBotByBotName(std::string botName) {
return Result; return Result;
} }
Client* EntityList::GetBotOwnerByBotEntityID(uint16 entityID) { Client* EntityList::GetBotOwnerByBotEntityID(uint32 entity_id) {
Client* Result = nullptr; Client* c = nullptr;
if (entityID > 0) {
for (std::list<Bot*>::iterator botListItr = bot_list.begin(); botListItr != bot_list.end(); ++botListItr) { if (entity_id) {
Bot* tempBot = *botListItr; for (const auto& b : bot_list) {
if (tempBot && tempBot->GetID() == entityID) { if (b && b->GetID() == entity_id) {
Result = tempBot->GetBotOwner()->CastToClient(); c = b->GetBotOwner()->CastToClient();
break; break;
} }
} }
} }
return Result;
return c;
}
Client* EntityList::GetBotOwnerByBotID(const uint32 bot_id) {
Client* c = nullptr;
if (bot_id) {
const auto owner_id = database.botdb.GetOwnerID(bot_id);
if (owner_id) {
c = GetClientByCharID(owner_id);
}
}
return c;
} }
void EntityList::AddBot(Bot *new_bot, bool send_spawn_packet, bool dont_queue) { void EntityList::AddBot(Bot *new_bot, bool send_spawn_packet, bool dont_queue) {
@ -8790,7 +8804,7 @@ void EntityList::AddBot(Bot *new_bot, bool send_spawn_packet, bool dont_queue) {
mob_list.insert(std::pair<uint16, Mob*>(new_bot->GetID(), new_bot)); mob_list.insert(std::pair<uint16, Mob*>(new_bot->GetID(), new_bot));
parse->EventBot(EVENT_SPAWN, new_bot, nullptr, "", 0); parse->EventBot(EVENT_SPAWN, new_bot, nullptr, "", 0);
new_bot->DispatchZoneControllerEvent(EVENT_SPAWN_ZONE, new_bot, "", 0, nullptr); new_bot->DispatchZoneControllerEvent(EVENT_SPAWN_ZONE, new_bot, "", 0, nullptr);
} }
} }

View File

@ -319,6 +319,20 @@ bool BotDatabase::LoadOwnerID(const uint32 bot_id, uint32& owner_id)
return true; return true;
} }
uint32 BotDatabase::GetOwnerID(const uint32 bot_id)
{
if (!bot_id) {
return 0;
}
const auto& l = BotDataRepository::FindOne(database, bot_id);
if (!l.bot_id) {
return 0;
}
return l.owner_id;
}
bool BotDatabase::LoadBotID(const uint32 owner_id, const std::string& bot_name, uint32& bot_id) bool BotDatabase::LoadBotID(const uint32 owner_id, const std::string& bot_name, uint32& bot_id)
{ {
if (!owner_id || bot_name.empty()) { if (!owner_id || bot_name.empty()) {

View File

@ -55,6 +55,7 @@ public:
bool LoadOwnerID(const std::string& bot_name, uint32& owner_id); bool LoadOwnerID(const std::string& bot_name, uint32& owner_id);
bool LoadOwnerID(const uint32 bot_id, uint32& owner_id); bool LoadOwnerID(const uint32 bot_id, uint32& owner_id);
uint32 GetOwnerID(const uint32 bot_id);
bool LoadBotID(const uint32 owner_id, const std::string& bot_name, uint32& bot_id); bool LoadBotID(const uint32 owner_id, const std::string& bot_name, uint32& bot_id);
bool LoadBotID(const uint32 owner_id, const std::string& bot_name, uint32& bot_id, uint8& bot_class_id); bool LoadBotID(const uint32 owner_id, const std::string& bot_name, uint32& bot_id, uint8& bot_class_id);

View File

@ -2025,6 +2025,21 @@ void PerlembParser::ExportEventVariables(
break; break;
} }
case EVENT_DESPAWN: {
ExportVar(package_name.c_str(), "despawned_entity_id", npcmob->GetID());
if (npcmob->IsNPC()) {
ExportVar(package_name.c_str(), "despawned_bot_id", 0);
ExportVar(package_name.c_str(), "despawned_npc_id", npcmob->GetNPCTypeID());
#ifdef BOTS
} else if (npcmob->IsBot()) {
ExportVar(package_name.c_str(), "despawned_bot_id", npcmob->CastToBot()->GetBotID());
ExportVar(package_name.c_str(), "despawned_npc_id", 0);
#endif
}
break;
}
case EVENT_DESPAWN_ZONE: { case EVENT_DESPAWN_ZONE: {
ExportVar(package_name.c_str(), "despawned_entity_id", mob->GetID()); ExportVar(package_name.c_str(), "despawned_entity_id", mob->GetID());

View File

@ -628,7 +628,8 @@ private:
Mob* GetMobByBotID(uint32 botID); Mob* GetMobByBotID(uint32 botID);
Bot* GetBotByBotID(uint32 botID); Bot* GetBotByBotID(uint32 botID);
Bot* GetBotByBotName(std::string botName); Bot* GetBotByBotName(std::string botName);
Client* GetBotOwnerByBotEntityID(uint16 entityID); Client* GetBotOwnerByBotEntityID(uint32 entity_id);
Client* GetBotOwnerByBotID(const uint32 bot_id);
std::list<Bot*> GetBotsByBotOwnerCharacterID(uint32 botOwnerCharacterID); std::list<Bot*> GetBotsByBotOwnerCharacterID(uint32 botOwnerCharacterID);
bool Bot_AICheckCloseBeneficialSpells(Bot* caster, uint8 iChance, float iRange, uint32 iSpellTypes); // TODO: Evaluate this closesly in hopes to eliminate bool Bot_AICheckCloseBeneficialSpells(Bot* caster, uint8 iChance, float iRange, uint32 iSpellTypes); // TODO: Evaluate this closesly in hopes to eliminate

View File

@ -399,6 +399,16 @@ Lua_Bot_List Lua_EntityList::GetBotList() {
return ret; return ret;
} }
Lua_Client Lua_EntityList::GetBotOwnerByBotEntityID(uint32 entity_id) {
Lua_Safe_Call_Class(Lua_Client);
return Lua_Client(self->GetBotOwnerByBotEntityID(entity_id));
}
Lua_Client Lua_EntityList::GetBotOwnerByBotID(uint32 bot_id) {
Lua_Safe_Call_Class(Lua_Client);
return Lua_Client(self->GetBotOwnerByBotID(bot_id));
}
Lua_Bot_List Lua_EntityList::GetBotListByCharacterID(uint32 character_id) { Lua_Bot_List Lua_EntityList::GetBotListByCharacterID(uint32 character_id) {
Lua_Safe_Call_Class(Lua_Bot_List); Lua_Safe_Call_Class(Lua_Bot_List);
Lua_Bot_List ret; Lua_Bot_List ret;
@ -649,6 +659,8 @@ luabind::scope lua_register_entity_list() {
.def("GetBotByID", (Lua_Bot(Lua_EntityList::*)(uint32))&Lua_EntityList::GetBotByID) .def("GetBotByID", (Lua_Bot(Lua_EntityList::*)(uint32))&Lua_EntityList::GetBotByID)
.def("GetBotByName", (Lua_Bot(Lua_EntityList::*)(std::string))&Lua_EntityList::GetBotByName) .def("GetBotByName", (Lua_Bot(Lua_EntityList::*)(std::string))&Lua_EntityList::GetBotByName)
.def("GetBotList", (Lua_Bot_List(Lua_EntityList::*)(void))&Lua_EntityList::GetBotList) .def("GetBotList", (Lua_Bot_List(Lua_EntityList::*)(void))&Lua_EntityList::GetBotList)
.def("GetBotOwnerByBotEntityID", (Lua_Client(Lua_EntityList::*)(uint32))&Lua_EntityList::GetBotOwnerByBotEntityID)
.def("GetBotOwnerByBotID", (Lua_Client(Lua_EntityList::*)(uint32))&Lua_EntityList::GetBotOwnerByBotID)
.def("GetBotListByCharacterID", (Lua_Bot_List(Lua_EntityList::*)(uint32))&Lua_EntityList::GetBotListByCharacterID) .def("GetBotListByCharacterID", (Lua_Bot_List(Lua_EntityList::*)(uint32))&Lua_EntityList::GetBotListByCharacterID)
.def("GetBotListByCharacterID", (Lua_Bot_List(Lua_EntityList::*)(uint32,uint8))&Lua_EntityList::GetBotListByCharacterID) .def("GetBotListByCharacterID", (Lua_Bot_List(Lua_EntityList::*)(uint32,uint8))&Lua_EntityList::GetBotListByCharacterID)
.def("GetBotListByClientName", (Lua_Bot_List(Lua_EntityList::*)(std::string))&Lua_EntityList::GetBotListByClientName) .def("GetBotListByClientName", (Lua_Bot_List(Lua_EntityList::*)(std::string))&Lua_EntityList::GetBotListByClientName)

View File

@ -133,6 +133,8 @@ public:
#ifdef BOTS #ifdef BOTS
Lua_Bot GetBotByID(uint32 bot_id); Lua_Bot GetBotByID(uint32 bot_id);
Lua_Bot GetBotByName(std::string bot_name); Lua_Bot GetBotByName(std::string bot_name);
Lua_Client GetBotOwnerByBotEntityID(uint32 entity_id);
Lua_Client GetBotOwnerByBotID(uint32 bot_id);
Lua_Bot_List GetBotList(); Lua_Bot_List GetBotList();
Lua_Bot_List GetBotListByCharacterID(uint32 character_id); Lua_Bot_List GetBotListByCharacterID(uint32 character_id);
Lua_Bot_List GetBotListByCharacterID(uint32 character_id, uint8 class_id); Lua_Bot_List GetBotListByCharacterID(uint32 character_id, uint8 class_id);

View File

@ -213,6 +213,7 @@ LuaParser::LuaParser() {
NPCArgumentDispatch[EVENT_LOOT_ZONE] = handle_npc_loot_zone; NPCArgumentDispatch[EVENT_LOOT_ZONE] = handle_npc_loot_zone;
NPCArgumentDispatch[EVENT_SPAWN_ZONE] = handle_npc_spawn_zone; NPCArgumentDispatch[EVENT_SPAWN_ZONE] = handle_npc_spawn_zone;
NPCArgumentDispatch[EVENT_PAYLOAD] = handle_npc_payload; NPCArgumentDispatch[EVENT_PAYLOAD] = handle_npc_payload;
NPCArgumentDispatch[EVENT_DESPAWN_ZONE] = handle_npc_despawn_zone;
PlayerArgumentDispatch[EVENT_SAY] = handle_player_say; PlayerArgumentDispatch[EVENT_SAY] = handle_player_say;
PlayerArgumentDispatch[EVENT_ENVIRONMENTAL_DAMAGE] = handle_player_environmental_damage; PlayerArgumentDispatch[EVENT_ENVIRONMENTAL_DAMAGE] = handle_player_environmental_damage;

View File

@ -438,6 +438,21 @@ void handle_npc_spawn_zone(
lua_setfield(L, -2, "other"); lua_setfield(L, -2, "other");
} }
void handle_npc_despawn_zone(
QuestInterface *parse,
lua_State* L,
NPC* npc,
Mob *init,
std::string data,
uint32 extra_data,
std::vector<std::any> *extra_pointers
) {
Lua_Mob l_mob(init);
luabind::adl::object l_mob_o = luabind::adl::object(L, l_mob);
l_mob_o.push(L);
lua_setfield(L, -2, "other");
}
// Player // Player
void handle_player_say( void handle_player_say(
QuestInterface *parse, QuestInterface *parse,

View File

@ -203,6 +203,16 @@ void handle_npc_payload(
std::vector<std::any> *extra_pointers std::vector<std::any> *extra_pointers
); );
void handle_npc_despawn_zone(
QuestInterface *parse,
lua_State* L,
NPC* npc,
Mob *init,
std::string data,
uint32 extra_data,
std::vector<std::any> *extra_pointers
);
// Player // Player
void handle_player_say( void handle_player_say(
QuestInterface *parse, QuestInterface *parse,

View File

@ -406,6 +406,16 @@ Bot* Perl_EntityList_GetBotByName(EntityList* self, std::string bot_name) // @ca
return self->GetBotByBotName(bot_name); return self->GetBotByBotName(bot_name);
} }
Client* Perl_EntityList_GetBotOwnerByBotEntityID(EntityList* self, uint32_t entity_id) // @categories Script Utility, Bot
{
return self->GetBotOwnerByBotEntityID(entity_id);
}
Client* Perl_EntityList_GetBotOwnerByBotID(EntityList* self, uint32_t bot_id) // @categories Script Utility, Bot
{
return self->GetBotOwnerByBotID(bot_id);
}
perl::array Perl_EntityList_GetBotList(EntityList* self) // @categories Script Utility, Bot perl::array Perl_EntityList_GetBotList(EntityList* self) // @categories Script Utility, Bot
{ {
perl::array result; perl::array result;
@ -618,6 +628,8 @@ void perl_register_entitylist()
package.add("GetBotByID", &Perl_EntityList_GetBotByID); package.add("GetBotByID", &Perl_EntityList_GetBotByID);
package.add("GetBotByName", &Perl_EntityList_GetBotByName); package.add("GetBotByName", &Perl_EntityList_GetBotByName);
package.add("GetBotList", &Perl_EntityList_GetBotList); package.add("GetBotList", &Perl_EntityList_GetBotList);
package.add("GetBotOwnerByBotEntityID", (Client*(*)(EntityList*, uint32))&Perl_EntityList_GetBotOwnerByBotEntityID);
package.add("GetBotOwnerByBotID", (Client*(*)(EntityList*, uint32))&Perl_EntityList_GetBotOwnerByBotID);
package.add("GetBotListByCharacterID", (perl::array(*)(EntityList*, uint32))&Perl_EntityList_GetBotListByCharacterID); package.add("GetBotListByCharacterID", (perl::array(*)(EntityList*, uint32))&Perl_EntityList_GetBotListByCharacterID);
package.add("GetBotListByCharacterID", (perl::array(*)(EntityList*, uint32, uint8))&Perl_EntityList_GetBotListByCharacterID); package.add("GetBotListByCharacterID", (perl::array(*)(EntityList*, uint32, uint8))&Perl_EntityList_GetBotListByCharacterID);
package.add("GetBotListByClientName", (perl::array(*)(EntityList*, std::string))&Perl_EntityList_GetBotListByClientName); package.add("GetBotListByClientName", (perl::array(*)(EntityList*, std::string))&Perl_EntityList_GetBotListByClientName);