Skip to content

Commit

Permalink
feat(LuaEngine/MapMethods): add GetCreatures and GetCreaturesByAreaId…
Browse files Browse the repository at this point in the history
… methods (#230)

Co-authored-by: 55Honey <[email protected]>
  • Loading branch information
iThorgrim and 55Honey authored Jan 26, 2025
1 parent 32d15a0 commit 60b5e26
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/LuaEngine/LuaFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
57 changes: 57 additions & 0 deletions src/LuaEngine/methods/MapMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 60b5e26

Please sign in to comment.