From e3c01d414315f7b31699e3d0b6a52a30f69ee636 Mon Sep 17 00:00:00 2001 From: "Michael Cook (mackal)" Date: Sun, 5 Aug 2018 22:55:30 -0400 Subject: [PATCH] Add lua entity_list function GetShuffledClientList() There is cases where we need to get a random client, but GetRandomClient() is not sufficient enough due to complex predicates required on what tells us is a valid client. This way we can just loop over a shuffled list and verify a client is valid instead of multiple GetRandomClient() calls. --- zone/lua_entity_list.cpp | 17 +++++++++++++++++ zone/lua_entity_list.h | 1 + 2 files changed, 18 insertions(+) diff --git a/zone/lua_entity_list.cpp b/zone/lua_entity_list.cpp index ef4057ed1..e6a572eca 100644 --- a/zone/lua_entity_list.cpp +++ b/zone/lua_entity_list.cpp @@ -340,6 +340,22 @@ Lua_Client_List Lua_EntityList::GetClientList() { return ret; } +Lua_Client_List Lua_EntityList::GetShuffledClientList() { + Lua_Safe_Call_Class(Lua_Client_List); + Lua_Client_List ret; + auto &t_list = self->GetClientList(); + + auto iter = t_list.begin(); + while(iter != t_list.end()) { + ret.entries.push_back(Lua_Client(iter->second)); + ++iter; + } + + zone->random.Shuffle(ret.entries.begin(), ret.entries.end()); + + return ret; +} + Lua_NPC_List Lua_EntityList::GetNPCList() { Lua_Safe_Call_Class(Lua_NPC_List); Lua_NPC_List ret; @@ -480,6 +496,7 @@ luabind::scope lua_register_entity_list() { .def("GetRandomClient", (Lua_Client(Lua_EntityList::*)(float, float, float, float, Lua_Client))&Lua_EntityList::GetRandomClient) .def("GetMobList", (Lua_Mob_List(Lua_EntityList::*)(void))&Lua_EntityList::GetMobList) .def("GetClientList", (Lua_Client_List(Lua_EntityList::*)(void))&Lua_EntityList::GetClientList) + .def("GetShuffledClientList", (Lua_Client_List(Lua_EntityList::*)(void))&Lua_EntityList::GetShuffledClientList) .def("GetNPCList", (Lua_NPC_List(Lua_EntityList::*)(void))&Lua_EntityList::GetNPCList) .def("GetCorpseList", (Lua_Corpse_List(Lua_EntityList::*)(void))&Lua_EntityList::GetCorpseList) .def("GetObjectList", (Lua_Object_List(Lua_EntityList::*)(void))&Lua_EntityList::GetObjectList) diff --git a/zone/lua_entity_list.h b/zone/lua_entity_list.h index 17b00579e..6db846220 100644 --- a/zone/lua_entity_list.h +++ b/zone/lua_entity_list.h @@ -101,6 +101,7 @@ public: Lua_Client GetRandomClient(float x, float y, float z, float dist, Lua_Client exclude); Lua_Mob_List GetMobList(); Lua_Client_List GetClientList(); + Lua_Client_List GetShuffledClientList(); Lua_NPC_List GetNPCList(); Lua_Corpse_List GetCorpseList(); Lua_Object_List GetObjectList();