diff --git a/zone/entity.cpp b/zone/entity.cpp index f04ea0717..ed0b73141 100644 --- a/zone/entity.cpp +++ b/zone/entity.cpp @@ -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 bots_in_range; + + for (const auto& b : bot_list) { + if ( + b != exclude_bot && + ( + is_whole_zone || + DistanceSquared(static_cast(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 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(client.second->GetPosition()), location) <= distance + DistanceSquared(static_cast(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 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(npc.second->GetPosition()), location) <= distance + DistanceSquared(static_cast(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 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(mob.second->GetPosition()), location) <= distance + DistanceSquared(static_cast(mob.second->GetPosition()), location) <= distance_squared ) ) { mobs_in_range.push_back(mob.second); diff --git a/zone/entity.h b/zone/entity.h index 38184bf0a..65c1eb7e7 100644 --- a/zone/entity.h +++ b/zone/entity.h @@ -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); diff --git a/zone/lua_entity_list.cpp b/zone/lua_entity_list.cpp index fcf44619e..f8b237a01 100644 --- a/zone/lua_entity_list.cpp +++ b/zone/lua_entity_list.cpp @@ -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_("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) diff --git a/zone/lua_entity_list.h b/zone/lua_entity_list.h index 112f7ceab..2c8d1dc11 100644 --- a/zone/lua_entity_list.h +++ b/zone/lua_entity_list.h @@ -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 }; diff --git a/zone/perl_entity.cpp b/zone/perl_entity.cpp index 867b58f72..94b9119cb 100644 --- a/zone/perl_entity.cpp +++ b/zone/perl_entity.cpp @@ -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);