feat(LuaEngine/MapMethods): add GetCreatures and GetCreaturesByAreaId methods (#230)

Co-authored-by: 55Honey <71938210+55Honey@users.noreply.github.com>
This commit is contained in:
iThorgrim
2025-01-26 18:48:32 +01:00
committed by GitHub
parent 32d15a0003
commit 60b5e260d0
2 changed files with 60 additions and 0 deletions

View File

@@ -1202,6 +1202,9 @@ ElunaRegister<Map> MapMethods[] =
{ "GetAreaId", &LuaMap::GetAreaId },
{ "GetHeight", &LuaMap::GetHeight },
{ "GetWorldObject", &LuaMap::GetWorldObject },
{ "GetCreatures", &LuaMap::GetCreatures },
{ "GetCreaturesByAreaId", &LuaMap::GetCreaturesByAreaId },
// Setters
{ "SetWeather", &LuaMap::SetWeather },

View File

@@ -325,5 +325,62 @@ namespace LuaMap
lua_settop(L, tbl);
return 1;
}
/**
* Returns a table with all the current [Creature]s in the map
*
* @return table mapCreatures
*/
int GetCreatures(lua_State* L, Map* map)
{
const auto& creatures = map->GetCreatureBySpawnIdStore();
lua_createtable(L, creatures.size(), 0);
int tbl = lua_gettop(L);
for (const auto& pair : creatures)
{
Creature* creature = pair.second;
Eluna::Push(L, creature);
lua_rawseti(L, tbl, creature->GetSpawnId());
}
lua_settop(L, tbl);
return 1;
}
/**
* Returns a table with all the current [Creature]s in the specific area id
*
* @param number areaId : specific area id
* @return table mapCreatures
*/
int GetCreaturesByAreaId(lua_State* L, Map* map)
{
uint32 areaId = Eluna::CHECKVAL<uint32>(L, 2, -1);
std::vector<Creature*> filteredCreatures;
for (const auto& pair : map->GetCreatureBySpawnIdStore())
{
Creature* creature = pair.second;
if (areaId == -1 || creature->GetAreaId() == areaId)
{
filteredCreatures.push_back(creature);
}
}
lua_createtable(L, filteredCreatures.size(), 0);
int tbl = lua_gettop(L);
for (Creature* creature : filteredCreatures)
{
Eluna::Push(L, creature);
lua_rawseti(L, tbl, creature->GetSpawnId());
}
lua_settop(L, tbl);
return 1;
}
};
#endif