Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

functions Game.getNpcs() & Game.getMonsters() #4195

Merged
merged 1 commit into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ class Game

const std::unordered_map<uint32_t, Player*>& getPlayers() const { return players; }
const std::map<uint32_t, Npc*>& getNpcs() const { return npcs; }
const std::map<uint32_t, Monster*>& getMonsters() const { return monsters; }

void addPlayer(Player* player);
void removePlayer(Player* player);
Expand Down
30 changes: 30 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2176,6 +2176,8 @@ void LuaScriptInterface::registerFunctions()

registerMethod("Game", "getSpectators", LuaScriptInterface::luaGameGetSpectators);
registerMethod("Game", "getPlayers", LuaScriptInterface::luaGameGetPlayers);
registerMethod("Game", "getNpcs", LuaScriptInterface::luaGameGetNpcs);
registerMethod("Game", "getMonsters", LuaScriptInterface::luaGameGetMonsters);
registerMethod("Game", "loadMap", LuaScriptInterface::luaGameLoadMap);

registerMethod("Game", "getExperienceStage", LuaScriptInterface::luaGameGetExperienceStage);
Expand Down Expand Up @@ -4471,6 +4473,34 @@ int LuaScriptInterface::luaGameGetPlayers(lua_State* L)
return 1;
}

int LuaScriptInterface::luaGameGetNpcs(lua_State* L)
{
// Game.getNpcs()
lua_createtable(L, g_game.getNpcsOnline(), 0);

int index = 0;
for (const auto& npcEntry : g_game.getNpcs()) {
pushUserdata<Npc>(L, npcEntry.second);
setMetatable(L, -1, "Npc");
lua_rawseti(L, -2, ++index);
}
return 1;
}

int LuaScriptInterface::luaGameGetMonsters(lua_State* L)
{
// Game.getMonsters()
lua_createtable(L, g_game.getMonstersOnline(), 0);

int index = 0;
for (const auto& monsterEntry : g_game.getMonsters()) {
pushUserdata<Monster>(L, monsterEntry.second);
setMetatable(L, -1, "Monster");
lua_rawseti(L, -2, ++index);
}
return 1;
}

int LuaScriptInterface::luaGameLoadMap(lua_State* L)
{
// Game.loadMap(path)
Expand Down
2 changes: 2 additions & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ class LuaScriptInterface
// Game
static int luaGameGetSpectators(lua_State* L);
static int luaGameGetPlayers(lua_State* L);
static int luaGameGetNpcs(lua_State* L);
static int luaGameGetMonsters(lua_State* L);
static int luaGameLoadMap(lua_State* L);

static int luaGameGetExperienceStage(lua_State* L);
Expand Down