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.
This commit is contained in:
Michael Cook (mackal) 2018-08-05 22:55:30 -04:00
parent 93749bc509
commit e3c01d4143
2 changed files with 18 additions and 0 deletions

View File

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

View File

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