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

Game.getMonsterTypes() #2798

Merged
merged 2 commits into from Mar 11, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/luascript.cpp
Expand Up @@ -1971,6 +1971,7 @@ void LuaScriptInterface::registerFunctions()
registerMethod("Game", "getMonsterCount", LuaScriptInterface::luaGameGetMonsterCount);
registerMethod("Game", "getPlayerCount", LuaScriptInterface::luaGameGetPlayerCount);
registerMethod("Game", "getNpcCount", LuaScriptInterface::luaGameGetNpcCount);
registerMethod("Game", "getMonsterTypes", LuaScriptInterface::luaGameGetMonsterTypes);

registerMethod("Game", "getTowns", LuaScriptInterface::luaGameGetTowns);
registerMethod("Game", "getHouses", LuaScriptInterface::luaGameGetHouses);
Expand Down Expand Up @@ -4299,6 +4300,20 @@ int LuaScriptInterface::luaGameGetNpcCount(lua_State* L)
return 1;
}

int LuaScriptInterface::luaGameGetMonsterTypes(lua_State* L)
{
// Game.getMonsterTypes()
auto& type = g_monsters.monsters;
lua_createtable(L, type.size(), 0);

for (auto& mType : type) {
pushUserdata<MonsterType>(L, &mType.second);
setMetatable(L, -1, "MonsterType");
lua_setfield(L, -2, mType.first.c_str());
}
return 1;
}

int LuaScriptInterface::luaGameGetTowns(lua_State* L)
{
// Game.getTowns()
Expand Down
1 change: 1 addition & 0 deletions src/luascript.h
Expand Up @@ -539,6 +539,7 @@ class LuaScriptInterface
static int luaGameGetMonsterCount(lua_State* L);
static int luaGameGetPlayerCount(lua_State* L);
static int luaGameGetNpcCount(lua_State* L);
static int luaGameGetMonsterTypes(lua_State* L);

static int luaGameGetTowns(lua_State* L);
static int luaGameGetHouses(lua_State* L);
Expand Down
2 changes: 1 addition & 1 deletion src/monsters.h
Expand Up @@ -241,6 +241,7 @@ class Monsters
bool deserializeSpell(MonsterSpell* spell, spellBlock_t& sb, const std::string& description = "");

std::unique_ptr<LuaScriptInterface> scriptInterface;
std::map<std::string, MonsterType> monsters;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of moving that from being private, maybe we should add public method to acquire that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't really feel the necessity to do so, as we just use this one time.
If we had several cases where we would need the public class, then yes it should be done but other than adding one more line it wont really gives us any kind of benefit (atleast that's what I think)


private:
ConditionDamage* getDamageCondition(ConditionType_t conditionType,
Expand All @@ -252,7 +253,6 @@ class Monsters
void loadLootContainer(const pugi::xml_node& node, LootBlock&);
bool loadLootItem(const pugi::xml_node& node, LootBlock&);

std::map<std::string, MonsterType> monsters;
std::map<std::string, std::string> unloadedMonsters;

bool loaded = false;
Expand Down