[Quest API] Add GetRandomBot() to Perl/Lua (#2543)

* [Quest API] Add GetRandomBot() to Perl/lua.

# Perl
- Add `$entity_list->GetRandomBot()` to Perl.
- Add `$entity_list->GetRandomBot(x, y, z, distance)` to Perl.
- Add `$entity_list->GetRandomBot(x, y, z, distance, exclude_bot)` to Perl.

# Lua
- Add `eq.get_entity_list():GetRandomBot()` to Lua.
- Add `eq.get_entity_list():GetRandomBot(x, y, z, distance)` to Lua.
- Add `eq.get_entity_list():GetRandomBot(x, y, z, distance, exclude_bot)` to Lua.

# Notes
- Allows operators to grab a random Bot from entity list similar to Client, Mob, and NPC.

* Cleanup and fix Perl distance.

- Perl distance was sending as already squared, Lua was not.
- Send as non-squared and square in the method so that both work the same.

* Update entity.cpp

* Update entity.cpp
This commit is contained in:
Kinglykrab 2022-11-16 18:54:15 -05:00 committed by GitHub
parent 93d8471487
commit bd95daa1f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 12 deletions

View File

@ -1863,6 +1863,39 @@ Client *EntityList::GetClientByLSID(uint32 iLSID)
return nullptr;
}
#ifdef BOTS
Bot* EntityList::GetRandomBot(const glm::vec3& location, float distance, Bot* exclude_bot)
{
auto is_whole_zone = false;
if (location.x == 0.0f && location.y == 0.0f) {
is_whole_zone = true;
}
auto distance_squared = (distance * distance);
std::vector<Bot*> bots_in_range;
for (const auto& b : bot_list) {
if (
b != exclude_bot &&
(
is_whole_zone ||
DistanceSquared(static_cast<glm::vec3>(b->GetPosition()), location) <= distance_squared
)
) {
bots_in_range.push_back(b);
}
}
if (bots_in_range.empty()) {
return nullptr;
}
return bots_in_range[zone->random.Int(0, bots_in_range.size() - 1)];
}
#endif
Client *EntityList::GetRandomClient(const glm::vec3& location, float distance, Client *exclude_client)
{
auto is_whole_zone = false;
@ -1870,6 +1903,8 @@ Client *EntityList::GetRandomClient(const glm::vec3& location, float distance, C
is_whole_zone = true;
}
auto distance_squared = (distance * distance);
std::vector<Client*> clients_in_range;
for (const auto& client : client_list) {
@ -1877,7 +1912,7 @@ Client *EntityList::GetRandomClient(const glm::vec3& location, float distance, C
client.second != exclude_client &&
(
is_whole_zone ||
DistanceSquared(static_cast<glm::vec3>(client.second->GetPosition()), location) <= distance
DistanceSquared(static_cast<glm::vec3>(client.second->GetPosition()), location) <= distance_squared
)
) {
clients_in_range.push_back(client.second);
@ -1898,6 +1933,8 @@ NPC* EntityList::GetRandomNPC(const glm::vec3& location, float distance, NPC* ex
is_whole_zone = true;
}
auto distance_squared = (distance * distance);
std::vector<NPC*> npcs_in_range;
for (const auto& npc : npc_list) {
@ -1905,7 +1942,7 @@ NPC* EntityList::GetRandomNPC(const glm::vec3& location, float distance, NPC* ex
npc.second != exclude_npc &&
(
is_whole_zone ||
DistanceSquared(static_cast<glm::vec3>(npc.second->GetPosition()), location) <= distance
DistanceSquared(static_cast<glm::vec3>(npc.second->GetPosition()), location) <= distance_squared
)
) {
npcs_in_range.push_back(npc.second);
@ -1926,6 +1963,8 @@ Mob* EntityList::GetRandomMob(const glm::vec3& location, float distance, Mob* ex
is_whole_zone = true;
}
auto distance_squared = (distance * distance);
std::vector<Mob*> mobs_in_range;
for (const auto& mob : mob_list) {
@ -1933,7 +1972,7 @@ Mob* EntityList::GetRandomMob(const glm::vec3& location, float distance, Mob* ex
mob.second != exclude_mob &&
(
is_whole_zone ||
DistanceSquared(static_cast<glm::vec3>(mob.second->GetPosition()), location) <= distance
DistanceSquared(static_cast<glm::vec3>(mob.second->GetPosition()), location) <= distance_squared
)
) {
mobs_in_range.push_back(mob.second);

View File

@ -189,6 +189,11 @@ public:
Client *GetClientByWID(uint32 iWID);
Client *GetClientByLSID(uint32 iLSID);
Client *GetClient(uint32 ip, uint16 port);
#ifdef BOTS
Bot* GetRandomBot(const glm::vec3& location = glm::vec3(0.f), float distance = 0, Bot* exclude_bot = nullptr);
#endif
Client* GetRandomClient(const glm::vec3& location = glm::vec3(0.f), float distance = 0, Client* exclude_client = nullptr);
NPC* GetRandomNPC(const glm::vec3& location = glm::vec3(0.f), float distance = 0, NPC* exclude_npc = nullptr);
Mob* GetRandomMob(const glm::vec3& location = glm::vec3(0.f), float distance = 0, Mob* exclude_mob = nullptr);

View File

@ -570,6 +570,23 @@ void Lua_EntityList::Marquee(uint32 type, uint32 priority, uint32 fade_in, uint3
self->Marquee(type, priority, fade_in, fade_out, duration, message);
}
#ifdef BOTS
Lua_Bot Lua_EntityList::GetRandomBot() {
Lua_Safe_Call_Class(Lua_Bot);
return self->GetRandomBot();
}
Lua_Bot Lua_EntityList::GetRandomBot(float x, float y, float z, float distance) {
Lua_Safe_Call_Class(Lua_Bot);
return self->GetRandomBot(glm::vec3(x, y, z), distance);
}
Lua_Bot Lua_EntityList::GetRandomBot(float x, float y, float z, float distance, Lua_Bot exclude_bot) {
Lua_Safe_Call_Class(Lua_Bot);
return self->GetRandomBot(glm::vec3(x, y, z), distance, exclude_bot);
}
#endif
luabind::scope lua_register_entity_list() {
return luabind::class_<Lua_EntityList>("EntityList")
.def(luabind::constructor<>())
@ -624,6 +641,11 @@ luabind::scope lua_register_entity_list() {
.def("GetObjectList", (Lua_Object_List(Lua_EntityList::*)(void))&Lua_EntityList::GetObjectList)
.def("GetRaidByClient", (Lua_Raid(Lua_EntityList::*)(Lua_Client))&Lua_EntityList::GetRaidByClient)
.def("GetRaidByID", (Lua_Raid(Lua_EntityList::*)(int))&Lua_EntityList::GetRaidByID)
#ifdef BOTS
.def("GetRandomBot", (Lua_Bot(Lua_EntityList::*)(void))&Lua_EntityList::GetRandomBot)
.def("GetRandomBot", (Lua_Bot(Lua_EntityList::*)(float,float,float,float))&Lua_EntityList::GetRandomBot)
.def("GetRandomBot", (Lua_Bot(Lua_EntityList::*)(float,float,float,float,Lua_Bot))&Lua_EntityList::GetRandomBot)
#endif
.def("GetRandomClient", (Lua_Client(Lua_EntityList::*)(void))&Lua_EntityList::GetRandomClient)
.def("GetRandomClient", (Lua_Client(Lua_EntityList::*)(float,float,float,float))&Lua_EntityList::GetRandomClient)
.def("GetRandomClient", (Lua_Client(Lua_EntityList::*)(float,float,float,float,Lua_Client))&Lua_EntityList::GetRandomClient)

View File

@ -136,6 +136,9 @@ public:
Lua_Bot_List GetBotList();
Lua_Bot_List GetBotListByCharacterID(uint32 character_id);
Lua_Bot_List GetBotListByClientName(std::string client_name);
Lua_Bot GetRandomBot();
Lua_Bot GetRandomBot(float x, float y, float z, float distance);
Lua_Bot GetRandomBot(float x, float y, float z, float distance, Lua_Bot exclude_bot);
#endif
};

View File

@ -354,17 +354,17 @@ void Perl_EntityList_MessageGroup(EntityList* self, Mob* sender, bool skip_close
Client* Perl_EntityList_GetRandomClient(EntityList* self) // @categories Account and Character, Script Utility
{
return entity_list.GetRandomClient();
return self->GetRandomClient();
}
Client* Perl_EntityList_GetRandomClient(EntityList* self, float x, float y, float z, float distance) // @categories Account and Character, Script Utility
{
return entity_list.GetRandomClient(glm::vec3(x, y, z), (distance * distance));
return self->GetRandomClient(glm::vec3(x, y, z), distance);
}
Client* Perl_EntityList_GetRandomClient(EntityList* self, float x, float y, float z, float distance, Client* exclude_client) // @categories Account and Character, Script Utility
{
return entity_list.GetRandomClient(glm::vec3(x, y, z), (distance * distance), exclude_client);
return self->GetRandomClient(glm::vec3(x, y, z), distance, exclude_client);
}
perl::array Perl_EntityList_GetMobList(EntityList* self) // @categories Script Utility
@ -503,32 +503,32 @@ void Perl_EntityList_SignalAllClients(EntityList* self, uint32_t data) // @categ
Mob* Perl_EntityList_GetRandomMob(EntityList* self) // @categories Account and Character, Script Utility
{
return entity_list.GetRandomMob();
return self->GetRandomMob();
}
Mob* Perl_EntityList_GetRandomMob(EntityList* self, float x, float y, float z, float distance) // @categories Account and Character, Script Utility
{
return entity_list.GetRandomMob(glm::vec3(x, y, z), (distance * distance));
return self->GetRandomMob(glm::vec3(x, y, z), distance);
}
Mob* Perl_EntityList_GetRandomMob(EntityList* self, float x, float y, float z, float distance, Mob* exclude_mob) // @categories Account and Character, Script Utility
{
return entity_list.GetRandomMob(glm::vec3(x, y, z), (distance * distance), exclude_mob);
return self->GetRandomMob(glm::vec3(x, y, z), distance, exclude_mob);
}
NPC* Perl_EntityList_GetRandomNPC(EntityList* self) // @categories Account and Character, Script Utility
{
return entity_list.GetRandomNPC();
return self->GetRandomNPC();
}
NPC* Perl_EntityList_GetRandomNPC(EntityList* self, float x, float y, float z, float distance) // @categories Account and Character, Script Utility
{
return entity_list.GetRandomNPC(glm::vec3(x, y, z), (distance * distance));
return self->GetRandomNPC(glm::vec3(x, y, z), distance);
}
NPC* Perl_EntityList_GetRandomNPC(EntityList* self, float x, float y, float z, float distance, NPC* exclude_npc) // @categories Account and Character, Script Utility
{
return entity_list.GetRandomNPC(glm::vec3(x, y, z), (distance * distance), exclude_npc);
return self->GetRandomNPC(glm::vec3(x, y, z), distance, exclude_npc);
}
void Perl_EntityList_Marquee(EntityList* self, uint32 type, std::string message)
@ -546,6 +546,23 @@ void Perl_EntityList_Marquee(EntityList* self, uint32 type, uint32 priority, uin
self->Marquee(type, priority, fade_in, fade_out, duration, message);
}
#ifdef BOTS
Bot* Perl_EntityList_GetRandomBot(EntityList* self) // @categories Bots, Script Utility
{
return self->GetRandomBot();
}
Bot* Perl_EntityList_GetRandomBot(EntityList* self, float x, float y, float z, float distance) // @categories Bot, Script Utility
{
return self->GetRandomBot(glm::vec3(x, y, z), distance);
}
Bot* Perl_EntityList_GetRandomBot(EntityList* self, float x, float y, float z, float distance, Bot* exclude_bot) // @categories Bot, Script Utility
{
return self->GetRandomBot(glm::vec3(x, y, z), distance, exclude_bot);
}
#endif
void perl_register_entitylist()
{
perl::interpreter perl(PERL_GET_THX);
@ -599,6 +616,11 @@ void perl_register_entitylist()
package.add("GetObjectList", &Perl_EntityList_GetObjectList);
package.add("GetRaidByClient", &Perl_EntityList_GetRaidByClient);
package.add("GetRaidByID", &Perl_EntityList_GetRaidByID);
#ifdef BOTS
package.add("GetRandomBot", (Bot*(*)(EntityList*))&Perl_EntityList_GetRandomBot);
package.add("GetRandomBot", (Bot*(*)(EntityList*, float, float, float, float))&Perl_EntityList_GetRandomBot);
package.add("GetRandomBot", (Bot*(*)(EntityList*, float, float, float, float, Bot*))&Perl_EntityList_GetRandomBot);
#endif
package.add("GetRandomClient", (Client*(*)(EntityList*))&Perl_EntityList_GetRandomClient);
package.add("GetRandomClient", (Client*(*)(EntityList*, float, float, float, float))&Perl_EntityList_GetRandomClient);
package.add("GetRandomClient", (Client*(*)(EntityList*, float, float, float, float, Client*))&Perl_EntityList_GetRandomClient);